import { Vector } from "./vector" ; /**
 * 实现矩阵的加减以及逆运算
 */ class Matrix { /**
     * 矩阵行数
     */ row : number ; /**
     * 矩阵列数
     */ col : number ; /**
     * 矩阵数据
     */ data : number [ ] [ ] ; param { number } row param { number } col constructor ( row , col , data = [ ] ) { this . row = row ; this . col = col ; this . data = Array ( this . row ) ; for ( let i = 0 ; i < this . row ; i ++ ) { this . data [ i ] = new Array ( this . col ) ; } } /**
     * 初始化为一个单位矩阵
     */ Indentity ( ) { for ( let i = 0 ; i < this . col ; i ++ ) { for ( let j = 0 ; j < this . row ; j ++ ) { if ( i == j ) { this . data [ i ] [ j ] = 1 ; } else { this . data [ i ] [ j ] = 0 ; } } } return this ; } param { Matrix } matrix return { Matrix } product ( matrix ) { let tempMatrix = new Matrix ( this . row , matrix . col ) ; if ( matrix instanceof Matrix ) { if ( this . col == matrix . row ) { for ( let i = 0 ; i < this . row ; i ++ ) { for ( let j = 0 ; j < matrix . col ; j ++ ) { tempMatrix . data [ i ] [ j ] = 0 ; for ( let k = 0 ; k < this . col ; k ++ ) { tempMatrix . data [ i ] [ j ] += this . data [ i ] [ k ] * matrix . data [ k ] [ j ] ; } } } return ( this , tempMatrix ) ; } } } param data returns Hardamard ( data ) { if ( data instanceof Matrix ) { let tempMatrix = new Matrix ( data . row , data . col ) ; for ( let i = 0 ; i < data . col ; i ++ ) { for ( let j = 0 ; j < data . row ; j ++ ) { tempMatrix [ i ] [ j ] = this . data [ i ] [ j ] * data . data [ i ] [ j ] ; } } return tempMatrix ; } if ( data instanceof Vector ) { let tempV = new Vector ( new Array ( data . data . length ) ) ; if ( data . data . length == this . data . length ) { for ( let i = 0 ; i < data . data . length ; i ++ ) { for ( let j = 0 ; j < this . row ; j ++ ) { tempV [ i ] = data . data [ i ] * this . data [ i ] [ j ] ; } } } return tempV ; } } param { Matrix } matrix return { number } Tr ( matrix ) { let total = 0 ; for ( let i = 0 ; i < matrix . col ; i ++ ) { for ( let j = 0 ; j < matrix . row ; j ++ ) { if ( i == j ) { total += matrix . data [ i ] [ j ] ; } } } return ( this , total ) ; } param { Matrix } matrix return { Array < number > } downDimensionality ( matrix_ ) { let tempAry = [ ] ; if ( matrix_ instanceof Matrix ) { for ( let i = 0 ; i < matrix_ . col ; i ++ ) { for ( let j = 0 ; j < matrix_ . row ; j ++ ) { tempAry . push ( matrix_ [ i ] [ j ] ) ; } } } return tempAry ; } param { Matrix } A frobenius ( A ) { let tempNums = 0 ; for ( let i = 0 ; i < A . row ; i ++ ) { for ( let j = 0 ; j < A . col ; j ++ ) { tempNums += A . data [ i ] [ j ] ** 2 ; } } return ( this , tempNums ** ( 1 / 2 ) ) ; } param { Matrix } A return { Matrix } tran ( A ) { let NMatrix = new Matrix ( A . col , A . row ) ; for ( let i = 0 ; i < A . row ; i ++ ) { for ( let j = 0 ; j < A . col ; j ++ ) { NMatrix . data [ j ] [ i ] = A . data [ i ] [ j ] ; } } return NMatrix ; } param { void } 无输入 returns { Array } flat ( ) { var tempAry = [ ] ; for ( let i = 0 ; i < this . row ; i ++ ) { for ( let j = 0 ; j < this . col ; j ++ ) { tempAry . push ( this . data [ i ] [ j ] ) ; } } return tempAry ; } } export { Matrix } ;