JSMatrix
We will be dealing with lots of vectors and matrices. This course uses the JSMatrix library for all vector and matrix operations.
While probably not the fastest library, it should be decently feature complete, with some advanced features like matrix decompositions. Additionally it is designed to provide lots of ways to access and work with elements of matrices and vectors, such as non-copy transpose, block-views, reductions and more. I am also the author, so sorry about the blatant self promotion!
Documentation can be found at the linked repository.
All functions are accessible under the variable jsm.
Since JavaScript sadly does not have operator overloading, math operations such as have to be computed using functions, in this case jsm.add.
To make things slightly easier, a number of common functions are already provided at the scope of your scripts.
For any of these functions that take an out parameter, you can omit it, in which case a new matrix is created. This is mainly for optimization.
Here is a list of those:
add(a, b, out): Add two matrices/vectorssub(a, b, out): Subtract a matrix/vector from another onemult(a, b, out): Multiply two matricesscale(a, v, out): Scale a matrix/vectoraby a scalarvdot(a, b: The dot product between two vectorscross(a, b, out): The cross product between two 3D vectorsabs(a, out): Computes the element-wise absolute value of a matrix/vectornormalize(a, out): Divides a vector by it's lengthcwiseMin(a, b, out): Compute the element-wise minimum of two matrices/vectorscwiseMax(a, b, out): Compute the element-wise maximum of two matrices/vectorscwiseMult(a, b, out): Compute the element-wise multiplication of two matrices/vectorssubvec(v, start, rows): Get a view of a part of a vector, so it can match dimensions with other operations. Start indicates the index of the initial coordinate and rows the number of rows. Rows is optional and defaults to all remaining rows after start.diag(m): Get a view of the diagonal of a matrix. Behaves just like a vectorblock(m, i, j, rows = m.rows() - i, cols = m.cols() - j): Get a view of a part of a matrix, so it can match dimensions with other operations, such as multiplication or filling.rowsandcolsare optional and default to the remaining values from the starting point.transpose(m): Get a view of the transpose of a matrixinsert(a, b): Fills a matrix/vectprawith the values of another oneb. The dimensions must match. Can be combined with viewsfill(a, v): Sets each element of a matrix/vectorato a scalar valuev. Can be combined with viewscopy(m): Copy the given matrix/vectorhvec(v, hcoord = 1): Creates a new vector from the given vectorvwith an additional constant coordinate (default = 1) equal tohcoordv32: Factory functions for float vectors. Equal tojsm.VecF32m32: Factory functions for float matrices. Equal tojsm.MatF32
Additionally, as it is commonly used, we defined some helper functions for the common 2D, 3D and 4D vectors:
vec2(x = 0, y = 0): This is just a shorthand forv32.from([x,y])vec3(x = 0, y = 0, z = 0): This is just a shorthand forv32.from([x,y,z])vec4(x = 0, y = 0, z = 0, w = 0): This is just a shorthand forv32.from([x,y,z,w])mix(a, b, t): Computes a linear interpolation with the parametertfor the vectors/matricesaandbceil(a, out): Computes the per component ceil functionfloor(a, out): Computes the per component floor functionisAny(a, b, cmp): Does a element-wise comparison ofaandb. Returnstrue, if any of those comparisons wheretrue,falseotherwise.cmpis any functioncmp(elem_a, elem_b) => {true|false}
You can try out these functions or any others below to get a feeling. The code section contains example code, but you can change it as you like.