dot method
Function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication. For 1-D arrays, it is the inner product of the vectors. For N-dimensional arrays, it is a sum product over the last axis of a and the second-last axis of b.
var dot = m2d.dot([[1,2],[3,4]], [[11,12],[13,14]])
print(dot);
//[[37, 40], [85, 92]]
Implementation
List dot(List list1, List list2) {
if (!_checkArray(list1) || !_checkArray(list1)) {
throw ('Uneven array dimension');
}
var list1Shape = shape(list1);
var list2Shape = shape(list2);
if (list1Shape.length < 2 || list2Shape.length < 2) {
throw new Exception(
'Currently support 2D operations or put that values inside a list of list');
}
if (list1Shape[1] != list2Shape[0]) {
throw new Exception('Shapes not aligned');
}
// todo needs to use zero fun
var result = new List<num>.filled(list1.length, 0)
.map((e) => List<num>.filled(list2[0].length, 0))
.toList();
// list1 x list2 matrix
for (var r = 0; r < list1.length; r++) {
for (var c = 0; c < list2[0].length; c++) {
for (var i = 0; i < list1[0].length; i++) {
result[r][c] += list1[r][i] * list2[i][c];
}
}
}
return result;
}