fork download
  1. module strassens_matmul;
  2.  
  3. debug {
  4. static import std;
  5. }
  6.  
  7. package {
  8. ulong getRowSize(T)(T[][] mat) {
  9. return mat[0].length;
  10. }
  11.  
  12. ulong getColumnSize(T)(T[][] mat) {
  13. return mat.length;
  14. }
  15.  
  16. T[][] createMatrix(T)(const ulong rowSize, const ulong columnSize) {
  17. return new T[][](rowSize, columnSize);
  18. }
  19.  
  20. /// row and column are 0 index-based
  21. T* getPointPtr(T)(T[][] mat, const ulong row, const ulong column) {
  22. return &mat[row][column];
  23. }
  24.  
  25. T getPointCopy(T)(T[][] mat, const ulong row, const ulong column) {
  26. return mat[row][column];
  27. }
  28.  
  29. T[][] mulIterative(T)(scope const T[][] mat1, scope const T[][] mat2) {
  30. auto result = createMatrix!T(mat1.getRowSize, mat2.getColumnSize);
  31. foreach (row; 0 .. mat1.getRowSize()) {
  32. foreach (column; 0 .. mat2.getColumnSize()) {
  33. T value;
  34. foreach (i; 0 .. mat1.getRowSize()) {
  35. value += mat1.getPointCopy(row, i) * mat2.getPointCopy(i, column);
  36. }
  37. *result.getPointPtr(row, column) = value;
  38. }
  39. }
  40. return result;
  41. }
  42. }
  43.  
  44. void main() {
  45. const uint[][] matA = [[10, 20, 10], [4, 5, 6], [2, 3, 5]];
  46. const uint[][] matB = [[3, 2, 4], [3, 3, 9], [4, 4, 2]];
  47. const uint[][] matC = [[130, 120, 240], [51, 47, 73], [35, 33, 45]];
  48. assert(matA.mulIterative(matB) == matC);
  49. }
  50.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.d:30:42: error: template strassens_matmul.getRowSize cannot deduce function from argument types !()(const(uint[][])), candidates are:
         auto result = createMatrix!T(mat1.getRowSize, mat2.getColumnSize);
                                          ^
prog.d:8:11: note: strassens_matmul.getRowSize(T)(T[][] mat)
     ulong getRowSize(T)(T[][] mat) {
           ^
prog.d:30:59: error: template strassens_matmul.getColumnSize cannot deduce function from argument types !()(const(uint[][])), candidates are:
         auto result = createMatrix!T(mat1.getRowSize, mat2.getColumnSize);
                                                           ^
prog.d:12:11: note: strassens_matmul.getColumnSize(T)(T[][] mat)
     ulong getColumnSize(T)(T[][] mat) {
           ^
prog.d:31:43: error: template strassens_matmul.getRowSize cannot deduce function from argument types !()(const(uint[][])), candidates are:
         foreach (row; 0 .. mat1.getRowSize()) {
                                           ^
prog.d:8:11: note: strassens_matmul.getRowSize(T)(T[][] mat)
     ulong getRowSize(T)(T[][] mat) {
           ^
prog.d:48:29: error: template instance strassens_matmul.mulIterative!uint error instantiating
     assert(matA.mulIterative(matB) == matC);
                             ^
prog.d:48:29: error: function strassens_matmul.mulIterative!uint.mulIterative is not accessible from module strassens_matmul
     assert(matA.mulIterative(matB) == matC);
                             ^
stdout
Standard output is empty