just a matrix vector multiplication

by de Montalembert Jonathan

HTML

<div>
  How it works, say I have a matrix:
<code>
<pre>
    a b
    c d
    e f
</pre>

  
</code>


Then you'd call 
<code><pre>
vecmatrice([
[a, b], 
[c, d],
[e, f]
], myVector)</pre></code>
</div>
<div class="result"></div>

JavaScript

function vecmatrix(matrix, vec) {

  function calc() {
    return matrix.map(function(row, idx) {
    	if(row.length != vec.length) {
      	throw Error("Matrix number of column must be equal to vector number of rows")
      }
      var res = 0;
      vec.forEach(function(vecVal, index) {
        if (isNaN(row[index])) {
          throw Error("The matrix is malformated")
        }
        res += row[index] * vecVal;
      });
      return res;
    });
  }

  try {
    return calc()
  } catch (e) {
    return e;
  }
}

var a = vecmatrix([
  [8, 2, 3],
  [4, 5, 6],
  [4, 6, 3],
  [6, 7, 3]
], [2, 4, 3]);

jQuery('.result').text(a)

var actual = vecmatrix([
  [1, 2, 3],
  [4, 5, 6]
], [2, 4, 3]);

expected = [19, 46]

console.log(actual, expected)