Hedgehog-Computing/math

Example function that handles Mat/JS arrays (lusolve from mathjs)

Gaoooooo opened this issue · 0 comments

/**
* 
* @author Jason Reynolds 
* @param - inputM the matrix in the equation Ax=b, namely A
* @param - input_col the column vector in the equation Ax = b, namely b
* @returns - the matrix answer , x, to the equation Ax=b using lu decomposition
*
* Function that uniquely solves the equation Ax = b for a square matrix/Mat A and column vector b
*/


function lusolve(inputM, input_col) {

*import math: deep_copy

    //declare raw_in, raw_in_col as input.clone() if Mat otherwise, inputM, input_col themselves 
    let in_type_M = (inputM instanceof Mat);
    let raw_in = (in_type_M) ? inputM.clone() : deep_copy(inputM);
    let in_type_col = (input_col instanceof Mat);
    let raw_in_col = (in_type_col) ? input_col.clone() : deep_copy(input_col);

    //if any input is Mat, degrade to JS array
    if (raw_in instanceof Mat) {
        raw_in = raw_in.val;
    }
    if (input_col instanceof Mat) {
        raw_in_col = raw_in_col.val;
    }

    //check: 0 dims? not square? column vector not equal to length of matrix row length? (if input is m x n, b is m x 1, while solution is n x 1 
    if (raw_in.length === 0 || raw_in[0].length === 0 || !(raw_in.length === raw_in[0].length) || !(raw_in_col.length === raw_in.length) ) {
        throw new Error('Wrong dimensions. Need nxn matrix, may want to try lusolveAll or check column vector');
    }
    //define the result to be the lusolve output ie the solution
    let result = mathjs.lusolve(raw_in, raw_in_col);
    //return it as a Mat object
    return new Mat(result);
}```