1

I'm performing matrix calculations (e.g. computing determinants, eigenvalues, inverse matrices) in JavaScript using Math.js. I would like the computations to be performed more rapidly (it's computing them on 2X2 matrices, but it should be performing many every second, and it now causes noticeable delay), but precision isn't of that great importance (I think a couple decimal places of precision would be more than enough). Is there a way of specifying how precise the computation should be? Thanks!

Here is an example computation:

var A = [[3.5, 9], [9, 1]]
for (let x=0;x<10**5;x++){
  math.eigs(A)
  math.inv(A)
  math.det(A)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/8.1.0/math.js" integrity="sha512-ne87j5uORxbrU7+bsqeJJWfWj5in65R9PCjaQL161xtH5cesZgULVbeVAkzAAN7hnYOcrHeBas9Wbd/Lm8gXFA==" crossorigin="anonymous"></script>

In reality, I'm having these computations done every time a slider position is changed (and of course with a different matrix each time).

  • How do you expect a response when you give no code as a reference to how you are doing it now? What do you expect as a more rapid computation? – jmrker Dec 28 '20 at 19:07
  • I added sample code. I would expect decreasing the run time of that sample code by a factor of around 10 would be significantly more rapid computation. – Ephraim Bryski Dec 28 '20 at 19:36
  • May not speed up response times,but you could change the loop a bit: for (let x=0, len=10**5; x – jmrker Dec 29 '20 at 21:10
  • In reality, there's no loop. That was just to say I wanted to compute it very rapidly. (In reality, there's a slider a user can toggle, and I want to repeat the computation every time the slider changes position). – Ephraim Bryski Dec 30 '20 at 22:40

2 Answers2

0

Looking at the documentation: Yes

https://mathjs.org/docs/core/configuration.html

precision. The maximum number of significant digits for BigNumbers. This setting only applies to BigNumbers, not to numbers. Default value is 64.

I have no idea if this will actually speed up your calculations or solve your problem, but there are a number of configuration options that might help.

Will
  • 3,201
  • 1
  • 19
  • 17
  • It seems that BigNumbers are used for greater precision though, not less: https://mathjs.org/docs/datatypes/bignumbers.html – Ephraim Bryski Dec 28 '20 at 19:35
0

Try the ml-matrix library instead of math.js. Besides having an efficient decomposition algorithm, it also generalizes better. Math.js is limited to solving only symmetric real matrices. Here is an example of solving a more difficult binary sparse matrix with ml-matrix:

matrix = 
[
  [ 0, 1, 0, 1, 0, 1 ],
  [ 0, 0, 0, 0, 0, 0 ],
  [ 0, 1, 0, 1, 0, 1 ],
  [ 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0 ],
  [ 1, 0, 1, 0, 0, 0 ]
]

const mlMatrix = require('ml-matrix');
const M = new mlMatrix.Matrix(matrix);
const e = new mlMatrix.EigenvalueDecomposition(M);

e.realEigenvalues -> [-1.4142135623730945, 2.220446049250313e-16, 1.4142135623730954, 0, 0, 0 ]
e.imaginaryEigenvalues -> [ 0, 0, 0, 0, 0, 0 ]
jrbe228
  • 356
  • 3
  • 11