Possible to compute matrix decompositions asynchronously? #2859
-
I'm currently working on a website to interactively learn about a variety of numerical methods. Many of these require the solution of a system of linear equations, for which I'm currently using the LU decomposition (future support for Cholesky decomposition would be great, but that's for another topic). Now, for moderately large systems, the LU decomposition might take a while, thereby essentially freezing the page. Is it possible to instead compute such decompositions asynchronously? I tried the approach below, but it still appears to be synchronous — async function applyLU(M) {
return decomp = await math.lup(M);
}
const M = math.random([400, 400]);
console.log("Pre");
applyLU(M).then(decomp => console.log(decomp.L, decomp.U));
console.log("Post"); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Good question. Yes that is possible. JavaScript is single threaded, but you can run computations in a separate webworker (browser) or child_process/thread (node.js). There is an example on how to do this: https://mathjs.org/examples/browser/webworkers/index.html If you use data classes like Matrix, you'll probably need to serialize/deserialize the data as described in the docs on Serialization, that isn't worked out in the example. You can also use a library like https://github.com/josdejong/workerpool to have a request/response API instead of messages. |
Beta Was this translation helpful? Give feedback.
Good question. Yes that is possible. JavaScript is single threaded, but you can run computations in a separate webworker (browser) or child_process/thread (node.js).
There is an example on how to do this: https://mathjs.org/examples/browser/webworkers/index.html
If you use data classes like Matrix, you'll probably need to serialize/deserialize the data as described in the docs on Serialization, that isn't worked out in the example. You can also use a library like https://github.com/josdejong/workerpool to have a request/response API instead of messages.