简体   繁体   中英

Unable to run simple example using library https://github.com/hiddentao/linear-algebra

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.

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 )(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM