-1

I have some working code, which I wrote a while ago, where I created my own sumProduct functions, for performing this operation on singe and multi dimensional arrays:

function sumProduct1D(m1, m2) {
    var result = 0;
    var len = m1.length;
    for (var i = 0; i < len; i++) {
        result += m1[i] * m2[i];
    }
    return result;
}
function sumProduct2D(m1, m2) {
    var result = 0;
    var len1 = m1.length;
    for (var i1 = 0; i1 < len1; i1++) {
        var len2 = m1[i1].length;
        for (var i2 = 0; i2 < len2; i2++) {
            result += m1[i1][i2] * m2[i1][i2];
        }
    }
    return result;
}

(These functions go through both arrays multiplying associated indexes and adding the total all together - in case you're not familiar with sumProduct).

At some point I started using mathJS for some of its matrix/array manipulation methods and I realised that my sumProduct1D is the same as math.dot

var a = [1,2,3];
var b = [3,2,1];

console.log(sumProduct1D(a,b));
console.log(math.dot(a,b));

 function sumProduct1D(m1, m2) {
   var result = 0;
   var len = m1.length;
   for (var i = 0; i < len; i++) {
     result += m1[i] * m2[i];
   }
   return result;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.8.0/math.js"></script>

The above code gives the same result for mine and using math.dot - perfect!


My question is around my other method - it sums a multidimensional array, and mathJS doesnt like it - I get an error Uncaught RangeError: Vector expected.

var c = [[1,2,3],[1,2,3]];
var d = [[3,2,1],[3,2,1]];
console.log(sumProduct2D(c,d))
console.log(math.dot(c,d))

function sumProduct2D(m1, m2) {
  var result = 0;
  var len1 = m1.length;
  for (var i1 = 0; i1 < len1; i1++) {
    var len2 = m1[i1].length;
    for (var i2 = 0; i2 < len2; i2++) {
      result += m1[i1][i2] * m2[i1][i2];
    }
  }
  return result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.8.0/math.js"></script>

I have had a look through the mathJS docs, and I cannot for the life of me put together the combination of calls which duplicate my sumProduct2D. But there must be a way.

Can anyone replicate my sumProduct method using mathJS functions?

Jamiec
  • 133,658
  • 13
  • 134
  • 193

1 Answers1

3

In case this helps anyone else, I found the answer eventually. It was math.sum(math.dotMultiply(c,d)) I was after.

var c = [[1,2,3],[1,2,3]];
var d = [[3,2,1],[3,2,1]];
console.log(sumProduct2D(c,d))
console.log(math.sum(math.dotMultiply(c,d)))

function sumProduct2D(m1, m2) {
  var result = 0;
  var len1 = m1.length;
  for (var i1 = 0; i1 < len1; i1++) {
    var len2 = m1[i1].length;
    for (var i2 = 0; i2 < len2; i2++) {
      result += m1[i1][i2] * m2[i1][i2];
    }
  }
  return result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.8.0/math.js"></script>
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Thanks a lot! Helpful for me! Your function doesn't work with `var c = [1,2,3]; var d = [3,2,1];` but mathjs does. – Tan Bui Sep 29 '18 at 20:13