Questions about Goals and Future Direction for Matrix Multiplication #584
-
Hello Everyone, I worked on some tests and type fixes for I previously mentioned I was going to open an issue to outline what I found while working on tests and types, but there's enough to talk about that I thought it would be better to try opening a discussion instead. After some consensus is reached, I'll breakdown the decisions into individual issues and more focused pull requests. Reference ImplementationI'm testing possible implementation details and running tests with an adaptation of I already have testing and code coverage setup for that project so it's easier for me to play around with things there. And I plan on porting the relevant tests to The private project also includes tests for simulating prota-/deutera-/trita-nopia on sRGB colors using ObservationsPriorityWhat I gleaned from a previous version of the API docs is that Arguments & Return TypesIn
I did a rough scan of the code to get these numbers, they aren't definitive. Current
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the detailed analysis. Looks like some of the types need to be cleaned up. I was doing some profiling of a benchmark that used Color.js on the weekend and found that
I agree that |
Beta Was this translation helpful? Give feedback.
-
I found some benchmarks on matrix math. It only benchmarks element-wise addition of matrices but could be useful in structuring future specialized functions. |
Beta Was this translation helpful? Give feedback.
-
I took a look at the CSS 4 Editor's Draft's Color Conversion Code section My understanding of the linked article on column-major ordering, the wikipedia article on matrix multiplication, Matrices are 2 dimensional rectangular collections and we're only interested in collections of numbers. So matrices can be described as tables where each cell in the table can contain a collected number.
MathematicallyMathematically, matrices can be accessed or manipulated by element, row, or column. All 3 are equally important and each can be preferred depending how the matrix will be used. The trouble comes with how to use vectors and matrices together. A matrix can be described as made up of both rows and columns at the same time. But a vector is 1 dimensional and can only be described as either a row or column. The transpose operator easily changes a row to a column and column to a row. So for consistency it needs to be stated if a vector is by default a representation of a row or a column. This is where row-major order and column-major order come in. The -major order states the default format of vectors. Row vectors and column vectors each require a specific multiplication order to be equivalent:
This importance of multiplication ordering especially applies to successive multiplications, but Computer ImplementationComputer implementations can extend the notational representation of row-major order and column-major order and actually use the -major ordering as a low level structure of the data in memory. There are performance reason and optimizations that can be made for how the data is accessed and stored in-memory, but we can ignore any of that since In But plain arrays in js don't contain enough information to say if they represent row, or column vectors. To absolutely specify the type requires representing the vector as a nested array. So, to force the array
|
Beta Was this translation helpful? Give feedback.
-
This would seem to be the far more likely need in this library, essentially a vector dot. If you want to multiply it some other way, matrixMultiply would likely not be the choice, and some generic multiplication function could be used instead.
The above is essentially what Numpy does (a scientific library in Python). The library is row major by default, but >>> import numpy as np
>>> np.matmul([1, 2, 3], [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array([30, 36, 42])
>>> np.matmul([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [1, 2, 3])
array([14, 32, 50])
>>> np.matmul([1, 2, 3], [4, 5, 6])
32 I think they don't allow scalar operations in this function as a scalar is obviously not a matrix, but we could do whatever we want. This is just my thoughts. I'm familiar with this approach, so I'm biased, but I'll adapt to whatever. |
Beta Was this translation helpful? Give feedback.
Just submitted a pull request that includes the flexible function interface like Numpy's
matmul
uses. I did also include multiplying vectors and returning a scalar in it as well.At this point, I'm thinking
multiplyMatrices
will end up being a slow generic matrix multiplication operator. Probably mostly used in a prototype/rapid iteration way.I suspect the speed-up @lloydk got from using specialized
Vector3
andMatrix3x3
multiplication means the standard use will be specialized functions. And the existingdot3
forVector3
dot products, andmultiply_v3_m3x3
already supersedes almost all uses ofmultiplyMatrices
.Once a specialized
multiply_m3x3_v3
andmultiply_m3x3
are implemented, there …