4 ms·
Somewhat related: check out gl-matrix for some fast matrix math in JavaScript - https://github.com/toji/gl-matrix https://github.com/toji/gl-matrix
by vecplane 8y ago
Somewhat related: check out gl-matrix for some fast matrix math in JavaScript - https://github.com/toji/gl-matrix https://github.com/toji/gl-matrix
- svantana 8y agoThis looks like it's only for 3x3 and 4x4 matrices, e.g. what you usually use in 3d graphics. Or is it possible to do arbitrary matrix multiplication with this lib?
- Lerc 8y agoIt does not appear to be the case. On the other hand, optimal speed and JavaScript are often different ball parks. I threw together this thing for when I need some matrix stuff in a little script. Not fast, but flexible. var vop = op=>( (a,b)=>( a.map((v,i)=>op(v,b[i])) ) ); var vdiff = vop((a,b)=>a-b); var vadd = vop((a,b)=>a+b); var vdot= (a,b)=>a.reduce( (ac,av,i)=>ac+=av*b[i],0); var vlength = a=>Math.sqrt(vdot(a,a)); var vscale = (a,b)=>a.map(v=>v*b); var vdistance = (a,b)=>vlength(vdiff(a,b)); var vnormalised = (a,b=1)=>vscale(a,b/vlength(a)); var project = (point, matrix) => matrix.map( p=>vdot(p,[...point,1])); var transpose = matrix=> ( matrix.reduce(($, row) => row.map((_, i) => [...($[i] || []), row[i]]), []) ); var multiply = (a,b,...rest)=>(!b)?a:multiply(a.map( (p=>transpose(b).map(q=>vdot(p,q)))),...rest); I look forward to the day when the sufficiently-smart JIT implements the optimal Matrix Multiplication from that. :-)
- svantana 8y agoThanks! I find it weird that performance-focused javascript projects don't have live benchmarks up so you can judge for yourself. Personally I've found that a semi-clever matrix multiplier in c++ compiled with emscripten can give about 1.5 gflops (singlethreaded on a 2.5GHz i7 macbook pro), and existing js libs like sushi are a lot slower.
- megalodon 8y agoFor anyone working with linear algebra in Node.js, I wrote C++ bindings to BLAS a couple of years ago [0]. I also started working on bindings to LAPACK, but never had the time to finish them [1]. [0] https://github.com/mateogianolio/nblas https://github.com/mateogianolio/nblas [1] https://github.com/mateogianolio/nlapack https://github.com/mateogianolio/nlapack