Proposal: Add new 'Matrix' object

# Ed Saleh (6 years ago)

Hello,

Matrices are widely used today in in Computer Science, Engineering, and AI. I am proposing a new object type of Matrix([ []... ]) which would make working with matrices easier, easily doing operations such matrices multiplication and addition.

Thank you,

# kai zhu (6 years ago)

@ed, general-purpose matrix-ops work poorly with malformed data like [1.2, null, 4.234] which are ubiquitous in ux-workflow programming. my experience in javascript linear-algebra mostly devolves to writing divide-by-zero "technical-debt" that's non-reusable/application-specific. it ends up more cost-efficient for me to write custom for-loops, with malformed-data checks embedded in them.

# J Decker (6 years ago)

On Sun, May 12, 2019 at 2:51 AM Ed Saleh <medozs at outlook.com> wrote:

Hello,

Matrices are widely used today in in Computer Science, Engineering, and AI. I am proposing a new object type of Matrix([ []... ]) which would make working with matrices easier, easily doing operations such matrices multiplication and addition.

There is a pretty good library for that...

js.tensorflow.org/api/latest/#Operations-Arithmetic :) they call them 'tensors' though.

# guest271314 (6 years ago)

Is this proposal different from DOMMatrix() developer.mozilla.org/en-US/docs/Web/API/DOMMatrix ?

# kai zhu (6 years ago)

from what i can tell 1) w3c dommatrix is application-specific to 3d visualization and 2) tfjs is application-specific to gpu-accelerated machine-learning.

the question is, is it feasible to spec a tc39 "jack-of-all-trades" linear-algebra proposal that can underpin the above two (and many other) diverse use-cases, w/o ending up being a "master-of-none".

# guest271314 (6 years ago)

Neither DOMMatrix nor tensorflow functionality are exclusive to any single use case. What is the use case of the Matrix object described at this proposal?

# David Teller (6 years ago)

According to the specs, DOMMatrix is limited to 4d matrices. They can be used to emulate 1d-3d matrices trivially. However, many applications (e.g. in graph theory) require arbitrary numbers of dimensions.

I'm not really familiar with Tensorflow, but if I read the API correctly, it seems to be limited to 1d-5d tensors. If I read the API correctly, arithmetic operations are tensor operations, rather than matrix operations, which have very different definitions.

# kai zhu (6 years ago)

is that a tentative "no" as in tc39 can't easily spec some low-level linear-algebra primitives that would be useful for both dommatrix, and [gpu-accelerated] tensoflow?

i do find value to industry for enhancing client-side performance of 3d visualization and ML. and i'm starting to see @Ed's original question as whether things like this falls in the scope of javascript language-design (as a fundamental UX-workflow problem), or should remain an embedder/userland concern.

# guest271314 (6 years ago)

A matrix could be viewed as an array of arrays or sets or maps or other values. How the values in the arrays or indexes are mapped is dependent upon the requirement. The requirement could be to map the networks of this entire planet earth; create sets of permutations; create cross word puzzles. What is the basic functionality of the Matrix described at this proposal?

# kai zhu (6 years ago)

this is wishful thinking, but i've wondered whether [wasm] sqlite3 has better potential for general-purpose multidimensional vector-operations than whatever gets spec'ed out in javascript. probably not, but there's been research on sql "dot-product-joins" [1].

[1] Dot-Product Join: Scalable In-Database Linear Algebra for Big Model Analytics faculty.ucmerced.edu/frusu/Papers/Conference/2017-ssdbm-dot-product-join.pdf

# Isiah Meadows (6 years ago)

Check out the old (and retracted) SIMD.js proposal that aimed to bring SIMD support to JS and the related offshoot that attempted to cover WebAssembly interop as well.

tc39/ecmascript_simd, stoklund/portable-simd/blob/master/README.md

Also, WebAssembly has a proposal, spearheaded from that work with JS, to try to bring SIMD support to it. The WebAssembly proposal is considerably lower level, but has broad implementor interest.

WebAssembly/simd/blob/master/proposals/simd/SIMD.md

Hardware SIMD and matrices may seem like two entirely different things, but a large area of overlap exists between small matrix math and CPU vector instructions. Those instruction sets were designed with coordinate, vector, and small matrix math in mind. For instance:

  • 2x2 matrix multiplication is maybe a dozen or so SSE instructions in modern Intel x86-64 assembly.
  • Scalar matrix multiplication is only a single "multiply vector by scalar" instruction.
  • 2x2 discriminant is a swizzle, a multiply, clear top two lanes, and horizontal subtract

And even with larger data sets, vector instructions can and do help. (Consider BLAS.)