@@ -3277,6 +3277,27 @@ describe('Matrix', () => {
32773277 } )
32783278 } )
32793279
3280+ describe ( 'spectralRadius' , ( ) => {
3281+ test ( 'default' , ( ) => {
3282+ const mat = Matrix . randn ( 5 , 5 ) . gram ( )
3283+ const r = mat . spectralRadius ( )
3284+ expect ( r ) . toBeGreaterThan ( 0 )
3285+
3286+ const pmat = mat . copy ( )
3287+ const mmat = mat . copy ( )
3288+ for ( let k = 0 ; k < 5 ; k ++ ) {
3289+ pmat . subAt ( k , k , r )
3290+ mmat . addAt ( k , k , r )
3291+ }
3292+ expect ( pmat . det ( ) * mmat . det ( ) ) . toBeCloseTo ( 0 )
3293+ } )
3294+
3295+ test ( 'fail' , ( ) => {
3296+ const mat = new Matrix ( 5 , 4 )
3297+ expect ( ( ) => mat . spectralRadius ( ) ) . toThrow ( 'Spectral radius only define square matrix.' )
3298+ } )
3299+ } )
3300+
32803301 test . each ( [
32813302 [ 'not' , v => + ! v ] ,
32823303 [ 'bitnot' , v => ~ v ] ,
@@ -4946,6 +4967,162 @@ describe('Matrix', () => {
49464967 } )
49474968 } )
49484969
4970+ describe ( 'solveJacobi' , ( ) => {
4971+ test . each ( [ 0 , 1 , 5 ] ) ( 'success %i' , n => {
4972+ const x = Matrix . randn ( n , n )
4973+ x . add ( Matrix . eye ( n , n , 100 ) )
4974+ const b = Matrix . randn ( n , 1 )
4975+
4976+ const a = x . solveJacobi ( b )
4977+
4978+ const t = x . dot ( a )
4979+ for ( let i = 0 ; i < b . rows ; i ++ ) {
4980+ for ( let j = 0 ; j < b . cols ; j ++ ) {
4981+ expect ( t . at ( i , j ) ) . toBeCloseTo ( b . at ( i , j ) )
4982+ }
4983+ }
4984+ } )
4985+
4986+ test . each ( [ 0 , 1 , 5 ] ) ( 'excessive columns (%i)' , n => {
4987+ const x = Matrix . randn ( n , n )
4988+ x . add ( Matrix . eye ( n , n , 100 ) )
4989+ const b = Matrix . randn ( n , 1 + Math . floor ( Math . random ( ) * 10 ) )
4990+
4991+ const a = x . solveJacobi ( b )
4992+
4993+ const t = x . dot ( a )
4994+ for ( let i = 0 ; i < b . rows ; i ++ ) {
4995+ for ( let j = 0 ; j < b . cols ; j ++ ) {
4996+ expect ( t . at ( i , j ) ) . toBeCloseTo ( b . at ( i , j ) )
4997+ }
4998+ }
4999+ } )
5000+
5001+ test ( 'fail invalid columns' , ( ) => {
5002+ const a = Matrix . randn ( 10 , 4 )
5003+ const b = Matrix . randn ( 10 , 1 )
5004+ expect ( ( ) => a . solveJacobi ( b ) ) . toThrow ( 'solveJacobi only define square matrix.' )
5005+ } )
5006+
5007+ test ( 'fail invalid rows' , ( ) => {
5008+ const a = Matrix . randn ( 3 , 3 )
5009+ const b = Matrix . randn ( 4 , 1 )
5010+ expect ( ( ) => a . solveJacobi ( b ) ) . toThrow ( 'b size is invalid.' )
5011+ } )
5012+
5013+ test ( 'fail can not calculate' , ( ) => {
5014+ const a = Matrix . randn ( 4 , 4 )
5015+ const b = Matrix . randn ( 4 , 1 )
5016+ expect ( ( ) => a . solveJacobi ( b ) ) . toThrow ( 'Can not calculate solved value.' )
5017+ } )
5018+ } )
5019+
5020+ describe ( 'solveGaussSeidel' , ( ) => {
5021+ test . each ( [ 0 , 1 , 5 ] ) ( 'success %i' , n => {
5022+ const x = Matrix . randn ( n , n )
5023+ x . add ( Matrix . eye ( n , n , 100 ) )
5024+ const b = Matrix . randn ( n , 1 )
5025+
5026+ const a = x . solveGaussSeidel ( b )
5027+
5028+ const t = x . dot ( a )
5029+ for ( let i = 0 ; i < b . rows ; i ++ ) {
5030+ for ( let j = 0 ; j < b . cols ; j ++ ) {
5031+ expect ( t . at ( i , j ) ) . toBeCloseTo ( b . at ( i , j ) )
5032+ }
5033+ }
5034+ } )
5035+
5036+ test . each ( [ 0 , 1 , 5 ] ) ( 'excessive columns (%i)' , n => {
5037+ const x = Matrix . randn ( n , n )
5038+ x . add ( Matrix . eye ( n , n , 100 ) )
5039+ const b = Matrix . randn ( n , 1 + Math . floor ( Math . random ( ) * 10 ) )
5040+
5041+ const a = x . solveGaussSeidel ( b )
5042+
5043+ const t = x . dot ( a )
5044+ for ( let i = 0 ; i < b . rows ; i ++ ) {
5045+ for ( let j = 0 ; j < b . cols ; j ++ ) {
5046+ expect ( t . at ( i , j ) ) . toBeCloseTo ( b . at ( i , j ) )
5047+ }
5048+ }
5049+ } )
5050+
5051+ test ( 'fail invalid columns' , ( ) => {
5052+ const a = Matrix . randn ( 10 , 4 )
5053+ const b = Matrix . randn ( 10 , 1 )
5054+ expect ( ( ) => a . solveGaussSeidel ( b ) ) . toThrow ( 'solveGaussSeidel only define square matrix.' )
5055+ } )
5056+
5057+ test ( 'fail invalid rows' , ( ) => {
5058+ const a = Matrix . randn ( 3 , 3 )
5059+ const b = Matrix . randn ( 4 , 1 )
5060+ expect ( ( ) => a . solveGaussSeidel ( b ) ) . toThrow ( 'b size is invalid.' )
5061+ } )
5062+
5063+ test ( 'fail can not calculate' , ( ) => {
5064+ const a = Matrix . randn ( 4 , 4 )
5065+ const b = Matrix . randn ( 4 , 1 )
5066+ expect ( ( ) => a . solveGaussSeidel ( b ) ) . toThrow ( 'Can not calculate solved value.' )
5067+ } )
5068+ } )
5069+
5070+ describe ( 'solveSOR' , ( ) => {
5071+ test . each ( [ 0 , 1 , 5 ] ) ( 'success %i' , n => {
5072+ const x = Matrix . randn ( n , n )
5073+ x . add ( Matrix . eye ( n , n , 100 ) )
5074+ const b = Matrix . randn ( n , 1 )
5075+
5076+ const a = x . solveSOR ( b , 0.8 )
5077+
5078+ const t = x . dot ( a )
5079+ for ( let i = 0 ; i < b . rows ; i ++ ) {
5080+ for ( let j = 0 ; j < b . cols ; j ++ ) {
5081+ expect ( t . at ( i , j ) ) . toBeCloseTo ( b . at ( i , j ) )
5082+ }
5083+ }
5084+ } )
5085+
5086+ test . each ( [ 0 , 1 , 5 ] ) ( 'excessive columns (%i)' , n => {
5087+ const x = Matrix . randn ( n , n )
5088+ x . add ( Matrix . eye ( n , n , 100 ) )
5089+ const b = Matrix . randn ( n , 1 + Math . floor ( Math . random ( ) * 10 ) )
5090+
5091+ const a = x . solveSOR ( b , 0.8 )
5092+
5093+ const t = x . dot ( a )
5094+ for ( let i = 0 ; i < b . rows ; i ++ ) {
5095+ for ( let j = 0 ; j < b . cols ; j ++ ) {
5096+ expect ( t . at ( i , j ) ) . toBeCloseTo ( b . at ( i , j ) )
5097+ }
5098+ }
5099+ } )
5100+
5101+ test ( 'fail invalid columns' , ( ) => {
5102+ const a = Matrix . randn ( 10 , 4 )
5103+ const b = Matrix . randn ( 10 , 1 )
5104+ expect ( ( ) => a . solveSOR ( b , 0.8 ) ) . toThrow ( 'solveSOR only define square matrix.' )
5105+ } )
5106+
5107+ test ( 'fail invalid rows' , ( ) => {
5108+ const a = Matrix . randn ( 3 , 3 )
5109+ const b = Matrix . randn ( 4 , 1 )
5110+ expect ( ( ) => a . solveSOR ( b , 0.8 ) ) . toThrow ( 'b size is invalid.' )
5111+ } )
5112+
5113+ test . each ( [ - 1 , 0 , 2 , 3 ] ) ( 'fail invalid w' , w => {
5114+ const a = Matrix . randn ( 4 , 4 )
5115+ const b = Matrix . randn ( 4 , 1 )
5116+ expect ( ( ) => a . solveSOR ( b , w ) ) . toThrow ( 'w must be positive and less than 2.' )
5117+ } )
5118+
5119+ test ( 'fail can not calculate' , ( ) => {
5120+ const a = Matrix . randn ( 4 , 4 )
5121+ const b = Matrix . randn ( 4 , 1 )
5122+ expect ( ( ) => a . solveSOR ( b , 0.8 ) ) . toThrow ( 'Can not calculate solved value.' )
5123+ } )
5124+ } )
5125+
49495126 describe ( 'bidiag' , ( ) => {
49505127 test . each ( [
49515128 [ 2 , 3 ] ,
0 commit comments