UNPKG

maths.ts

Version:

Math utilities library for TypeScript, JavaScript and Node.js

137 lines (136 loc) 4.71 kB
/** * @author Hector J. Vasquez <ipi.vasquez@gmail.com> * * @licence * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Node, { ValidNumber } from '../core/Node'; /** * An implementation of a Matrix in mathematics. This matrix may be a matrix * of any kind of mathematical expression: number, expression... */ export default class Matrix { protected matrix: Node[][]; /** * Builds a matrix from the dimensions of the matrix or its representation * as an Array or as a Matrix object. * @param m May be a the representation of the matrix as an Array or as * a Matrix; or, it may be the number of rows for this Matrix. * @param n The number of columns of the matrix in case of the first * argument (m) is a number. */ constructor(m?: ValidNumber[][] | Matrix | number, n?: number); /** * Gets the number of rows in this matrix. * @return The number of rows in matrix. */ readonly M: number; /** * Get the number of columns in this matrix. * @return The number of columns in this matrix. */ readonly N: number; /** * Creates a copy of the matrix this object represents as an array. * @return A two dimensional array of which this Matrix is representing. */ readonly nodeMatrix: Node[][]; /** * Creates a copy of the matrix this object represents as a two * dimensional array of numbers. If this Matrix contains elements that * cannot be converted as numbers (such as expression with variables) it * will return an undefined in the element that it did not convert. * @return A two dimensional array of numbers of the matrix this object * represents. */ readonly numberMatrix: number[][]; /** * Creates a two dimensional array of strings representing each element * in this Matrix. * @return A two dimensional array of each representation of this * elements matrix. */ readonly stringMatrix: string[][]; /** * Checks if this is a square matrix. * @return true if this is a square matrix, false otherwise. */ readonly isSquare: boolean; /** * Generates the transpose of this matrix. * @return The transpose of this matrix. */ readonly transpose: Matrix; /** * Calculates adj(this). * @return The adjucate matrix of this. */ readonly adjucate: Matrix; /** * Generates the inverse of this matrix. * @return This matrix inverse. */ readonly inverse: Matrix; /** * Calculates the determinant of this Matrix recursively. */ readonly determinant: Node; /** * Adds this to another matrix in a new Matrix. * @param m The matrix to add to this. * @return The result of this + m. */ add(m: Matrix): Matrix; /** * Multiplies this to another matrix in a new Matrix. * @param m The matrix to multiply to this. * @return The result of this * m. */ multiply(m: Matrix): Matrix; /** * Considering A as this matrix, Ai as the i-th row and Aij as the * element on the j-th column of i-th row. Eliminates Aij from this * matrix, where i = [0, y)U(y, m) and j = x. * @param x Column to pivot. * @param y Row to pivot. */ pivoting(x: number, y: number): void; /** * Generates a string for representing this matrix. * @return The representation of this as a string. */ toString(): string; /** * Generates an HTML table representative of this matrix. * @return This as a HTML table. * @throws Error if there is no window with a document. */ toHTMLElement(): HTMLElement; /** * Creates a copy of this matrix. * @return A clone of this. */ clone(): Matrix; /** * Builds this as a MxN Matrix of NaNs. * @param m The number of rows. * @param n The number of columns. */ private mxnConstructor(m, n); /** * Builds this as a matrix by copying matrix. * @param matrix The matrix wanted to be represented as a two * dimensional array of numbers, strings or expressions. */ private matrixConstructor(matrix); }