This code is released under the CreativeCommons Attribution-NonCommercial-NoDerivatives 4.0 License. You may not use it for commercial products without my permission. Donating to this project is not a substitute for permission.

Matrix multiplication is a complex problem, and I'm not going to even pretend that I've found the fastest way to accomplish it in Javascript. What I have created, however, is a faster and marginally-more-memory-efficient method of performing matrix operations using a single array, rather than a series of nested arrays.

Let's say you have the following matrix:

1     2     3
4     5     6
7     8     9

...and want a way to store it as a variable in Javascript. The traditional way to do it is as follows:

let matrix = [
     [    1,    4,    7    ],
     [    2,    5,    8    ],
     [    3,    6,    9    ]
];

where matrix[x][y] represents a particular value of the matrix. And this is, generally, a pretty acceptable solution. However, it can get complicated when performing operations on very large matrices: you'll need at least two nested for commands to iterate through the values of the matrix to perform anything, which takes up a decent amount of processing time. Additionally, each sub-array in that configuration takes up unnecessary memory, as each has its own prototype method and various identifiers.

What if, instead of using nested arrays, you could use a single array? That way, the processing time would be significantly reduced, and the memory usage would be somewhat lighter.

MatrJS uses the following method to create and store arrays:

let matrix = {
     data : [  1,  2,  3,  4,  5,  6,  7,  8,  9  ],
     columns : 3,
     rows : 3,
     column : function(x) {function to return a specific column},
     row : function(y) {function to return a specific row},
     point : function(x, y) {function to return a specific point}
}

It's not structured exactly like that, but it's close enough for you to get the point. I'm not particularly sure how it relates to other linear algebra modules in terms of memory usage, but it's verifiably fast even on low-end systems. It's specifically more memory-efficient and has faster processing speeds depending on the length of the matrix, with higher returns for longer matrices.

I've also provided many basic matrix operations as a demonstration on its speed, and I intend on adding as many as needed until it functions as a completely-independent linear algebra module.

Current list of included operations:

  • Addition / Subtraction
  • Kronecker sum
  • Direct sum
  • Transposition
  • Dot product (normal matrix multiplication)
  • Hadamard product
  • Kronecker product
  • Scalar product
  • Row/column switching
  • Row/column scaling
  • Row/column addition

Operations added in v0.11:

  • Frobenius inner product
  • Cracovian product
  • Vector dot product
  • Vector outer product

The tech demo above does three things during every update cycle: it creates two new matrix objects with randomized values, finds the Kronecker sum of the two matrices (involving two Kronecker product function calls, two identity matrix creation function calls, and a basic addition function call -- the most computationally complex matrix operation as of yet), and prints the values one-by-one to the screen. The counter in the upper-left hand corner shows how many times it's done this since the page loaded; as you can see, it's quite fast, especially if you intend only on using it on the back end with no UI. 

Currently, MatrJS is structured as a Node.JS module, intended for use with Pixelbox as part of a project I'm still working on. At some point in the future, this tech demo will be updated with a better GUI to let the user have control over matrix creation and operations, but that's a topic for another day.

Usage:

var Matr = requires('./matr');

Current speed statistics: On my Surface Pro, a single iteration takes between ~5 and ~15 ms, depending on the complexity of the matrices. If you're not going to be printing the matrices or generating new ones every iteration, you can effectively half that number -- at least. It also appears that speed (obviously) scales linearly with matrix complexity:

  • Matrices with less than 100 items take 0.5ms to go through the Kronecker sum operation
  • Matrices with less than 1000 items take between 1-10ms to go through the Kronecker sum operation
  • Matrices with approximately 10000 items take 1000ms (1s) to go through the Kronecker sum operation
StatusReleased
CategoryTool
PlatformsHTML5
Rating
Rated 5.0 out of 5 stars
(1 total ratings)
AuthorCormorant42

Download

Download NowName your own price

Click download now to get access to the following files:

matr.js 38 kB

Comments

Log in with itch.io to leave a comment.

Is there a particular benefit to this over a standard linear (or 1d) array?

It *is* a 1d array, but the algorithms allow it to be used for 2d-based calculations. The main benefit is because JavaScript is an interpreted language, it's faster by an order of magnitude to only have to use 1 dimension. It wouldn't be faster in c++, for example.