0

I'm attempting to use library : https://github.com/hiddentao/linear-algebra

Doc states to use :

Include dist/linear-algebra.js script into your HTML.

In the browser the library is exposed via the linearAlgebra() function.

But using code :

<script src="linear-algebra.js"></script>

<!-- https://github.com/hiddentao/linear-algebra
 -->

<script>

var m = new linearAlgebra().Matrix([ [1, 2, 3], [4, 5, 6] ]);
console.log( m.rows );     // 2
console.log( m.cols );     // 3
console.log( m.data );     // [ [1, 2, 3], [4, 5, 6] ]

</script>

Causes Chrome error :

Uncaught TypeError: Cannot read property 'rows' of undefined

I'm not using the library in the correct way , should be just able to use linearAlgebra() ref ?

Any other recommendations js math libraries appreciated.

blue-sky
  • 51,962
  • 152
  • 427
  • 752

1 Answers1

1
new linearAlgebra().Matrix(...)

is interpreted as

( new linearAlgebra() ).Matrix(...)

due to JS precedence rules (see here for details).

Enclose it in parentheses to get what you want:

new ( linearAlgebra().Matrix )(...)
Community
  • 1
  • 1
georg
  • 211,518
  • 52
  • 313
  • 390
  • 1
    `new (linearAlgebra()).Matrix` would work as well. Or you can follow the documentation for your library and import the symbols distinctly, such as `var linAlg = linearAlgebra(), Vector = linAlg.Vector, Matrix = linAlg.Matrix;` See [this JSFiddle](https://jsfiddle.net/0z8eu2y2/) for some examples. – TML Feb 20 '16 at 18:20