import { Real } from "./real" ; /**
 *  https public export type Matrix = Array < Array < number > > ; public export namespace Matrix { export function isMatrix ( value ) { return ( Array . isArray ( value ) && value . every ( ( r ) => Array . isArray ( r ) && r . every ( ( v ) => typeof v === "number" ) ) ) ; } export function isSquare ( m ) { return rows ( m ) === columns ( m ) ; } export function clone ( m ) { return m . map ( ( r ) => r . slice ( 0 ) ) ; } export function equals ( m , n , e ? ) { return ( m . length === n . length && m . every ( ( r , i ) => r . length === n [ i ] . length && r . every ( ( v , j ) => Real . equals ( v , n [ i ] [ j ] , e ) ) ) ) ; } export function identity ( n ) { return new Array < Array < number > > ( n ) . fill ( [ ] ) . map ( ( _ , i ) => { const r = new Array < number > ( n ) . fill ( 0 ) ; r [ i ] = 1 ; return r ; } ) ; } export function size ( m ) { return [ rows ( m ) , columns ( m ) ] ; } export function rows ( m ) { return m . length ; } export function columns ( m ) { return m . length === 0 ? 0 : m [ 0 ] . length ; } export function row ( m , i ) { return m [ i ] ; } export function column ( m , i ) { return m . map ( ( r ) => r [ i ] ) ; } export function add ( m , n ) { return m . map ( ( r , i ) => r . map ( ( v , j ) => v + n ?. [ i ] ?. [ j ] ) ) ; } export function subtract ( m , n ) { return m . map ( ( r , i ) => r . map ( ( v , j ) => v - n ?. [ i ] ?. [ j ] ) ) ; } /**
     *  https export function multiply ( m , n ) { return typeof n === "number" ? m . map ( ( r ) => r . map ( ( v ) => v * n ) ) : m . map ( ( r , i ) => n [ 0 ] . map ( ( _ , j ) => r . reduce ( ( s , _ , k ) => s + m [ i ] [ k ] * n ?. [ k ] ?. [ j ] , 0 ) ) ) ; } /**
     * Compute the transpose of a matrix.
     *
     *  https export function transpose ( m ) { return m . length === 0 ? m : m [ 0 ] . map ( ( _ , i ) => m . map ( ( row ) => row [ i ] ) ) ; } /**
     * Compute the determinant of a non-empty, square matrix.
     *
     *  https *  https remarks export function determinant ( m ) { switch ( m . length ) { case 1 : return m [ 0 ] [ 0 ] ; case 2 : return m [ 0 ] ?. [ 0 ] * m [ 1 ] ?. [ 1 ] - m [ 0 ] ?. [ 1 ] * m [ 1 ] ?. [ 0 ] ; default : return m . reduce ( ( d , _ , i ) => d + m [ 0 ] ?. [ i ] * cofactor ( m , 0 , i ) , 0 ) ; } } /**
     * Compute the inverse of an invertible matrix.
     *
     *  https *  https remarks export function inverse ( m ) { return multiply ( adjugate ( m ) , 1 / determinant ( m ) ) ; } /**
     * Compute the (i, j) cofactor of a non-empty, square matrix.
     *
     *  https internal function cofactor ( m , i , j ) { return ( - 1 ) ** ( i + j ) * minor ( m , i , j ) ; } /**
     * Compute the (i, j) minor of a non-empty, square matrix.
     *
     *  https internal function minor ( m , i , j ) { return determinant ( m . filter ( ( _ , k ) => k !== i ) . map ( ( r ) => r . filter ( ( _ , l ) => l !== j ) ) ) ; } /**
     * Compute the adjugate of a non-empty, square matrix.
     *
     *  https internal function adjugate ( m ) { return transpose ( m . map ( ( r , i ) => r . map ( ( _ , j ) => cofactor ( m , i , j ) ) ) ) ; } }