Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

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

Open
Gaoooooo opened this issue Apr 29, 2022 · 0 comments
Open

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

Gaoooooo opened this issue Apr 29, 2022 · 0 comments

Comments

@Gaoooooo
Copy link
Collaborator

Gaoooooo commented Apr 29, 2022

/**
* 
* @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);
}```
@Gaoooooo Gaoooooo changed the title Ex function that handles Mat/JS arrays (lusolve from mathjs) Example function that handles Mat/JS arrays (lusolve from mathjs) Apr 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant