This post is to assess the appetite for size-agnostic Matrix class proposal.
For a new SVG2 class I’m working on, I wanted a few functions for calculating the (surprisingly complex) ArcTo SVG2 command. It was relatively straightforward to abstract those calculations to a generic Matrix class that seamlessly handles different matrix sizes (particularly 2x2 and 3x3) and incorporate vectors, and handle the output of matrix multiply sizes.
Is there general interest for a Matrix-like class that works with different matrix and vector sizes?
Here are the implementation notes so far:
// This new Matrix class is nominally designed to (seamlessly?) support
// Vector2, Vector3, and Matrix sizes 2x2, 3x3, and 4x4
// with element-wise calculations (one or more parallel matrices) as well as Matrix calculations (multiply).
// Current implementation notes
// In a review of Babylon Matrix, Vector3, Vector2, and Tensor classes,
// most non-static method functions are trivial extentions of Methods supplied by this Matrix class.
// Most calculation functions are implemented with ToRef and accepting a result parameter.
// ToNew is obtained when result parameter is null or undefined
// InPlace is obtained by specifying the curremt matrix for the result parameter.
// multiplyByToRef is currently the only function with an output size different than the input sizes.
// multiplyByToRef is specifically designed to also accept vectors (1-column matrix) and larger matrices.
// vectors are implemented strictly as 1-column Matrix (number of rows = number of vector elements/dimensions)
// Performance note:
// Most functions contain internal Array index calculations, nested loops, and/or iterators
// to handle the variable dimensions. This also includes Array size checks on the suitability
// of input and output Matrix sizes and throw on Exceptions.
// When underlying output array size is exact and Matrix dimemsions are not,
// Matrix dimemsions are silently adjusted.
// Matrix has properties of:
// dim - a number array specifying dimensions of the array, usually [rows,cols] but can be more
// m - the underlying Float32Array containing the matrix data.
// storageMin (calculated) - minimum amount of Float32Array elements needed to store matrix data
// colfirst - true if data is stored in column priority, false for row priority
// There is tepid support for tensors (incorporated only in some access functions):
// dimSig - generalization of colfirst to higher order tensors
// arrayPositionOf - obtain float value at given matrix coordinates (variable number of dimensions)
// General use of dim Array instead of rows,columns. Though dim value is nominally [rows,cols].
// Some functions currently require [rows,cols].
// dim and dimSig are not yet implemented everywhere they need to be.
// Identity(size) - return square identity matrix of size x size
// TranslationFromArray(vArray) - Create a Translation Transformation Matrix of size 1 greater than array.
// ScaleFromArray(vArray) - Create a Scaling Transformation Matrix of size 1 greater than input array.
// RotationFromAngle(size,angle) - Create a 2d Rotation Matrix with the given angle. Only sizes 2 and 3.
// multiplyByToRef - multiply two arrays of sizes I x J and J x K resulting in a I x K Matrix
// transpose - swap colums and rows (currently only square matrices)
// addToRef - uses elementWiseToRef to add two matrices' components
// subToRef - uses elementWiseToRef to subtract a matrices' components from initial matrix
// scaleToRef - uses scalarFunctionToRef to multiply each matrix element by a scalar value
// dot - sum of the products formed by multiplying parallel elements in two matrices
// multiplyElements - product of all elements
// productSquares - product of all elements squared
// magnitudeSquared - sum of squared elements
// Iterate over elements with a function call:
// elementWiseToRef - operate on any number of "parallel" matrices grouped element by element
// scalarFunctionToRef - operate on single matrix with a scalar argument and function
// Generators:
// eachElement - yield each element one by one
// groupedElements - yield parallel elements from multiple matrices
// consider implementing : determinant, minor, co-factor, adjugate (transpose of cofactor matrix)
// what about: extract row, extract column, iterate row, iterate column
// also possible: equation solver?
// and: tensor-related functions?
I have the functions above coded but untested. Is there general interest for cleaning this up for general use and TypeScript-ifying?