@@ -1293,7 +1293,39 @@ describe('Matrix', () => {
1293
1293
} )
1294
1294
} )
1295
1295
1296
- test . todo ( 'sort' )
1296
+ describe ( 'sort' , ( ) => {
1297
+ test ( 'axis 0' , ( ) => {
1298
+ const org = Matrix . randn ( 10 , 5 )
1299
+ const mat = org . copy ( )
1300
+ const p = mat . sort ( 0 )
1301
+ for ( let i = 0 ; i < org . rows ; i ++ ) {
1302
+ let comp = true
1303
+ for ( let j = 0 ; j < org . cols ; j ++ ) {
1304
+ expect ( mat . at ( i , j ) ) . toBe ( org . at ( p [ i ] , j ) )
1305
+ if ( i > 0 && comp ) {
1306
+ expect ( mat . at ( i , j ) ) . toBeGreaterThanOrEqual ( mat . at ( i - 1 , j ) )
1307
+ comp &= mat . at ( i , j ) === mat . at ( i - 1 , j )
1308
+ }
1309
+ }
1310
+ }
1311
+ } )
1312
+
1313
+ test ( 'axis 1' , ( ) => {
1314
+ const org = Matrix . randn ( 5 , 10 )
1315
+ const mat = org . copy ( )
1316
+ const p = mat . sort ( 1 )
1317
+ for ( let j = 0 ; j < org . cols ; j ++ ) {
1318
+ let comp = true
1319
+ for ( let i = 0 ; i < org . rows ; i ++ ) {
1320
+ expect ( mat . at ( i , j ) ) . toBe ( org . at ( i , p [ j ] ) )
1321
+ if ( j > 0 && comp ) {
1322
+ expect ( mat . at ( i , j ) ) . toBeGreaterThanOrEqual ( mat . at ( i , j - 1 ) )
1323
+ comp &= mat . at ( i , j ) === mat . at ( i , j - 1 )
1324
+ }
1325
+ }
1326
+ }
1327
+ } )
1328
+ } )
1297
1329
1298
1330
test . todo ( 'shuffle' )
1299
1331
@@ -2181,11 +2213,75 @@ describe('Matrix', () => {
2181
2213
test . todo ( 'fail' )
2182
2214
} )
2183
2215
2184
- test . todo ( 'invLowerTriangular' )
2216
+ describe ( 'invLowerTriangular' , ( ) => {
2217
+ test . each ( [ 0 , 1 , 2 , 3 , 10 ] ) ( 'sizes[%i]' , n => {
2218
+ const mat = Matrix . randn ( n , n )
2219
+ for ( let i = 0 ; i < n ; i ++ ) {
2220
+ for ( let j = i + 1 ; j < n ; j ++ ) {
2221
+ mat . set ( i , j , 0 )
2222
+ }
2223
+ }
2224
+ const inv = mat . invLowerTriangular ( )
2225
+
2226
+ const eye = mat . dot ( inv )
2227
+ for ( let i = 0 ; i < n ; i ++ ) {
2228
+ for ( let j = 0 ; j < n ; j ++ ) {
2229
+ if ( i === j ) {
2230
+ expect ( eye . at ( i , j ) ) . toBeCloseTo ( 1 )
2231
+ } else {
2232
+ expect ( eye . at ( i , j ) ) . toBeCloseTo ( 0 )
2233
+ }
2234
+ }
2235
+ }
2236
+ } )
2237
+
2238
+ test . todo ( 'fail' )
2239
+ } )
2240
+
2241
+ describe ( 'invUpperTriangular' , ( ) => {
2242
+ test . each ( [ 0 , 1 , 2 , 3 , 10 ] ) ( 'sizes[%i]' , n => {
2243
+ const mat = Matrix . randn ( n , n )
2244
+ for ( let i = 0 ; i < n ; i ++ ) {
2245
+ for ( let j = 0 ; j < i ; j ++ ) {
2246
+ mat . set ( i , j , 0 )
2247
+ }
2248
+ }
2249
+ const inv = mat . invUpperTriangular ( )
2250
+
2251
+ const eye = mat . dot ( inv )
2252
+ for ( let i = 0 ; i < n ; i ++ ) {
2253
+ for ( let j = 0 ; j < n ; j ++ ) {
2254
+ if ( i === j ) {
2255
+ expect ( eye . at ( i , j ) ) . toBeCloseTo ( 1 )
2256
+ } else {
2257
+ expect ( eye . at ( i , j ) ) . toBeCloseTo ( 0 )
2258
+ }
2259
+ }
2260
+ }
2261
+ } )
2262
+
2263
+ test . todo ( 'fail' )
2264
+ } )
2265
+
2266
+ describe ( 'invRowReduction' , ( ) => {
2267
+ test . each ( [ 0 , 1 , 2 , 3 , 10 ] ) ( 'symmetric sizes[%i]' , n => {
2268
+ const mat = Matrix . randn ( n , n ) . gram ( )
2269
+ const inv = mat . invRowReduction ( )
2185
2270
2186
- test . todo ( 'invUpperTriangular' )
2271
+ const eye = mat . dot ( inv )
2272
+ for ( let i = 0 ; i < n ; i ++ ) {
2273
+ for ( let j = 0 ; j < n ; j ++ ) {
2274
+ if ( i === j ) {
2275
+ expect ( eye . at ( i , j ) ) . toBeCloseTo ( 1 )
2276
+ } else {
2277
+ expect ( eye . at ( i , j ) ) . toBeCloseTo ( 0 )
2278
+ }
2279
+ }
2280
+ }
2281
+ } )
2187
2282
2188
- test . todo ( 'invRowReduction' )
2283
+ test . todo ( 'fail' )
2284
+ } )
2189
2285
2190
2286
test . todo ( 'invLU' )
2191
2287
0 commit comments