@pawells/math-extended 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/package.json +1 -1
  2. package/build/angles.spec.d.ts +0 -2
  3. package/build/angles.spec.d.ts.map +0 -1
  4. package/build/angles.spec.js +0 -147
  5. package/build/angles.spec.js.map +0 -1
  6. package/build/clamp.spec.d.ts +0 -2
  7. package/build/clamp.spec.d.ts.map +0 -1
  8. package/build/clamp.spec.js +0 -19
  9. package/build/clamp.spec.js.map +0 -1
  10. package/build/documentation-validation.spec.d.ts +0 -11
  11. package/build/documentation-validation.spec.d.ts.map +0 -1
  12. package/build/documentation-validation.spec.js +0 -401
  13. package/build/documentation-validation.spec.js.map +0 -1
  14. package/build/interpolation.spec.d.ts +0 -2
  15. package/build/interpolation.spec.d.ts.map +0 -1
  16. package/build/interpolation.spec.js +0 -480
  17. package/build/interpolation.spec.js.map +0 -1
  18. package/build/matrices/arithmetic.spec.d.ts +0 -2
  19. package/build/matrices/arithmetic.spec.d.ts.map +0 -1
  20. package/build/matrices/arithmetic.spec.js +0 -915
  21. package/build/matrices/arithmetic.spec.js.map +0 -1
  22. package/build/matrices/asserts.spec.d.ts +0 -2
  23. package/build/matrices/asserts.spec.d.ts.map +0 -1
  24. package/build/matrices/asserts.spec.js +0 -565
  25. package/build/matrices/asserts.spec.js.map +0 -1
  26. package/build/matrices/core.spec.d.ts +0 -2
  27. package/build/matrices/core.spec.d.ts.map +0 -1
  28. package/build/matrices/core.spec.js +0 -634
  29. package/build/matrices/core.spec.js.map +0 -1
  30. package/build/matrices/decompositions.spec.d.ts +0 -2
  31. package/build/matrices/decompositions.spec.d.ts.map +0 -1
  32. package/build/matrices/decompositions.spec.js +0 -195
  33. package/build/matrices/decompositions.spec.js.map +0 -1
  34. package/build/matrices/linear-algebra.spec.d.ts +0 -2
  35. package/build/matrices/linear-algebra.spec.d.ts.map +0 -1
  36. package/build/matrices/linear-algebra.spec.js +0 -355
  37. package/build/matrices/linear-algebra.spec.js.map +0 -1
  38. package/build/matrices/normalization.spec.d.ts +0 -2
  39. package/build/matrices/normalization.spec.d.ts.map +0 -1
  40. package/build/matrices/normalization.spec.js +0 -335
  41. package/build/matrices/normalization.spec.js.map +0 -1
  42. package/build/matrices/transformations.spec.d.ts +0 -2
  43. package/build/matrices/transformations.spec.d.ts.map +0 -1
  44. package/build/matrices/transformations.spec.js +0 -755
  45. package/build/matrices/transformations.spec.js.map +0 -1
  46. package/build/quaternions/asserts.spec.d.ts +0 -2
  47. package/build/quaternions/asserts.spec.d.ts.map +0 -1
  48. package/build/quaternions/asserts.spec.js +0 -320
  49. package/build/quaternions/asserts.spec.js.map +0 -1
  50. package/build/quaternions/conversions.spec.d.ts +0 -2
  51. package/build/quaternions/conversions.spec.d.ts.map +0 -1
  52. package/build/quaternions/conversions.spec.js +0 -344
  53. package/build/quaternions/conversions.spec.js.map +0 -1
  54. package/build/quaternions/core.spec.d.ts +0 -2
  55. package/build/quaternions/core.spec.d.ts.map +0 -1
  56. package/build/quaternions/core.spec.js +0 -294
  57. package/build/quaternions/core.spec.js.map +0 -1
  58. package/build/quaternions/interpolation.spec.d.ts +0 -2
  59. package/build/quaternions/interpolation.spec.d.ts.map +0 -1
  60. package/build/quaternions/interpolation.spec.js +0 -64
  61. package/build/quaternions/interpolation.spec.js.map +0 -1
  62. package/build/quaternions/predefined.spec.d.ts +0 -2
  63. package/build/quaternions/predefined.spec.d.ts.map +0 -1
  64. package/build/quaternions/predefined.spec.js +0 -35
  65. package/build/quaternions/predefined.spec.js.map +0 -1
  66. package/build/random.spec.d.ts +0 -2
  67. package/build/random.spec.d.ts.map +0 -1
  68. package/build/random.spec.js +0 -267
  69. package/build/random.spec.js.map +0 -1
  70. package/build/vectors/asserts.spec.d.ts +0 -2
  71. package/build/vectors/asserts.spec.d.ts.map +0 -1
  72. package/build/vectors/asserts.spec.js +0 -260
  73. package/build/vectors/asserts.spec.js.map +0 -1
  74. package/build/vectors/core.spec.d.ts +0 -2
  75. package/build/vectors/core.spec.d.ts.map +0 -1
  76. package/build/vectors/core.spec.js +0 -343
  77. package/build/vectors/core.spec.js.map +0 -1
  78. package/build/vectors/interpolation.spec.d.ts +0 -2
  79. package/build/vectors/interpolation.spec.d.ts.map +0 -1
  80. package/build/vectors/interpolation.spec.js +0 -378
  81. package/build/vectors/interpolation.spec.js.map +0 -1
  82. package/build/vectors/predefined.spec.d.ts +0 -2
  83. package/build/vectors/predefined.spec.d.ts.map +0 -1
  84. package/build/vectors/predefined.spec.js +0 -333
  85. package/build/vectors/predefined.spec.js.map +0 -1
@@ -1,634 +0,0 @@
1
- import { AssertMatrixRow, AssertMatrixValue } from './asserts.js';
2
- import { MatrixCreate, MatrixSize, MatrixSizeSquare, MatrixIsValid, MatrixIsSquare, MatrixIdentity, MatrixClone, MatrixEquals, MatrixToString, MatrixRank, MatrixTrace, MatrixTranspose, MatrixMap, MatrixIsZero } from './core.js';
3
- describe('Matrices Core', () => {
4
- describe('MatrixCreate', () => {
5
- test('should create a 1x1 matrix with default parameters', () => {
6
- const matrix = MatrixCreate();
7
- expect(matrix).toEqual([[0]]);
8
- });
9
- test('should create a 1x1 matrix when size is 1', () => {
10
- const matrix = MatrixCreate(1);
11
- expect(matrix).toEqual([[0]]);
12
- });
13
- test('should create a 2x2 matrix when size is 2', () => {
14
- const matrix = MatrixCreate(2);
15
- expect(matrix).toEqual([[0, 0], [0, 0]]);
16
- });
17
- test('should create a 3x3 matrix when size is 3', () => {
18
- const matrix = MatrixCreate(3);
19
- expect(matrix).toEqual([[0, 0, 0], [0, 0, 0], [0, 0, 0]]);
20
- });
21
- test('should create a 4x4 matrix when size is 4', () => {
22
- const matrix = MatrixCreate(4);
23
- expect(matrix).toEqual([
24
- [0, 0, 0, 0],
25
- [0, 0, 0, 0],
26
- [0, 0, 0, 0],
27
- [0, 0, 0, 0],
28
- ]);
29
- });
30
- test('should create a rectangular matrix with specified rows and columns', () => {
31
- const matrix = MatrixCreate(2, 3);
32
- expect(matrix).toEqual([[0, 0, 0], [0, 0, 0]]);
33
- });
34
- test('should create a large square matrix', () => {
35
- const matrix = MatrixCreate(5, 5);
36
- expect(matrix).toHaveLength(5);
37
- expect(matrix[0]).toHaveLength(5);
38
- expect(matrix.every((row) => row.every((val) => val === 0))).toBe(true);
39
- });
40
- test('should throw error for negative rows', () => {
41
- expect(() => MatrixCreate(-1)).toThrow('Rows must be a non-negative integer');
42
- });
43
- test('should throw error for negative columns', () => {
44
- expect(() => MatrixCreate(2, -1)).toThrow('Columns must be a non-negative integer');
45
- });
46
- test('should throw error for non-integer rows', () => {
47
- expect(() => MatrixCreate(2.5)).toThrow('Rows must be a non-negative integer');
48
- });
49
- test('should throw error for non-integer columns', () => {
50
- expect(() => MatrixCreate(2, 3.5)).toThrow('Columns must be a non-negative integer');
51
- });
52
- test('should create empty matrix when size is 0', () => {
53
- const matrix = MatrixCreate(0, 0);
54
- expect(matrix).toEqual([]);
55
- });
56
- });
57
- describe('MatrixSize', () => {
58
- test('should return correct size for square matrix', () => {
59
- const matrix = [[1, 2], [3, 4]];
60
- expect(MatrixSize(matrix)).toEqual([2, 2]);
61
- });
62
- test('should return correct size for rectangular matrix', () => {
63
- const matrix = [[1, 2, 3], [4, 5, 6]];
64
- expect(MatrixSize(matrix)).toEqual([2, 3]);
65
- });
66
- test('should return [0, 0] for empty matrix', () => {
67
- const matrix = [];
68
- expect(MatrixSize(matrix)).toEqual([0, 0]);
69
- });
70
- test('should return correct size for single row matrix', () => {
71
- const matrix = [[1, 2, 3, 4]];
72
- expect(MatrixSize(matrix)).toEqual([1, 4]);
73
- });
74
- test('should return correct size for single column matrix', () => {
75
- const matrix = [[1], [2], [3]];
76
- expect(MatrixSize(matrix)).toEqual([3, 1]);
77
- });
78
- test('should throw error for invalid matrix', () => {
79
- expect(() => MatrixSize('invalid')).toThrow();
80
- });
81
- });
82
- describe('MatrixSizeSquare', () => {
83
- test('should return size for square matrix', () => {
84
- const matrix = [[1, 2], [3, 4]];
85
- expect(MatrixSizeSquare(matrix)).toBe(2);
86
- });
87
- test('should return size for 3x3 matrix', () => {
88
- const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
89
- expect(MatrixSizeSquare(matrix)).toBe(3);
90
- });
91
- test('should throw error for non-square matrix', () => {
92
- const matrix = [[1, 2, 3], [4, 5, 6]];
93
- expect(() => MatrixSizeSquare(matrix)).toThrow();
94
- });
95
- test('should return 1 for 1x1 matrix', () => {
96
- const matrix = [[5]];
97
- expect(MatrixSizeSquare(matrix)).toBe(1);
98
- });
99
- test('should throw for empty matrix', () => {
100
- const matrix = [];
101
- expect(() => MatrixSizeSquare(matrix)).toThrow('Matrix must have at least one row and one column');
102
- });
103
- });
104
- describe('MatrixIsValid', () => {
105
- test('should return true for valid matrix', () => {
106
- expect(MatrixIsValid([[1, 2], [3, 4]])).toBe(true);
107
- });
108
- test('should return false for empty matrix', () => {
109
- expect(MatrixIsValid([])).toBe(false);
110
- });
111
- test('should return false for non-array input', () => {
112
- expect(MatrixIsValid('invalid')).toBe(false);
113
- expect(MatrixIsValid(123)).toBe(false);
114
- expect(MatrixIsValid(null)).toBe(false);
115
- expect(MatrixIsValid(undefined)).toBe(false);
116
- });
117
- test('should return false for matrix with non-number values', () => {
118
- expect(MatrixIsValid([[1, 'invalid'], [3, 4]])).toBe(false);
119
- });
120
- test('should return false for matrix with inconsistent row lengths', () => {
121
- expect(MatrixIsValid([[1, 2], [3]])).toBe(false);
122
- });
123
- test('should return true for single element matrix', () => {
124
- expect(MatrixIsValid([[42]])).toBe(true);
125
- });
126
- });
127
- describe('MatrixIsSquare', () => {
128
- test('should return true for square matrix', () => {
129
- expect(MatrixIsSquare([[1, 2], [3, 4]])).toBe(true);
130
- });
131
- test('should return false for rectangular matrix', () => {
132
- expect(MatrixIsSquare([[1, 2, 3], [4, 5, 6]])).toBe(false);
133
- });
134
- test('should return true for 1x1 matrix', () => {
135
- expect(MatrixIsSquare([[1]])).toBe(true);
136
- });
137
- test('should return false for empty matrix', () => {
138
- expect(MatrixIsSquare([])).toBe(false);
139
- });
140
- test('should return true for 3x3 matrix', () => {
141
- expect(MatrixIsSquare([[1, 2, 3], [4, 5, 6], [7, 8, 9]])).toBe(true);
142
- });
143
- });
144
- describe('MatrixIsZero', () => {
145
- test('should return true for all-zero matrix', () => {
146
- const matrix = [[0, 0], [0, 0]];
147
- expect(MatrixIsZero(matrix)).toBe(true);
148
- });
149
- test('should return false for matrix with non-zero values', () => {
150
- const matrix = [[1, 0], [0, 0]];
151
- expect(MatrixIsZero(matrix)).toBe(false);
152
- });
153
- test('should return true for single element zero matrix', () => {
154
- const matrix = [[0]];
155
- expect(MatrixIsZero(matrix)).toBe(true);
156
- });
157
- test('should return false for single element non-zero matrix', () => {
158
- const matrix = [[1]];
159
- expect(MatrixIsZero(matrix)).toBe(false);
160
- });
161
- test('should throw for empty matrix', () => {
162
- const matrix = [];
163
- expect(() => MatrixIsZero(matrix)).toThrow('Matrix must have at least one row and one column');
164
- });
165
- test('should handle matrices with very small values within default threshold', () => {
166
- const matrix = [[1e-15, 0], [0, -1e-15]];
167
- expect(MatrixIsZero(matrix)).toBe(true);
168
- });
169
- test('should handle matrices with values above default threshold', () => {
170
- const matrix = [[1e-13, 0], [0, 0]];
171
- expect(MatrixIsZero(matrix)).toBe(false);
172
- });
173
- test('should respect custom threshold', () => {
174
- const matrix = [[0.001, 0], [0, 0]];
175
- expect(MatrixIsZero(matrix, 0.01)).toBe(true);
176
- expect(MatrixIsZero(matrix, 0.0001)).toBe(false);
177
- });
178
- test('should handle negative values within threshold', () => {
179
- const matrix = [[-1e-15, 1e-15], [0, 0]];
180
- expect(MatrixIsZero(matrix)).toBe(true);
181
- });
182
- test('should handle negative values above threshold', () => {
183
- const matrix = [[-0.1, 0], [0, 0]];
184
- expect(MatrixIsZero(matrix)).toBe(false);
185
- });
186
- test('should handle rectangular matrices', () => {
187
- const zeroMatrix = [[0, 0, 0], [0, 0, 0]];
188
- const nonZeroMatrix = [[0, 0, 1], [0, 0, 0]];
189
- expect(MatrixIsZero(zeroMatrix)).toBe(true);
190
- expect(MatrixIsZero(nonZeroMatrix)).toBe(false);
191
- });
192
- test('should handle large matrices', () => {
193
- const largeZeroMatrix = Array(100).fill(null).map(() => Array(100).fill(0));
194
- expect(MatrixIsZero(largeZeroMatrix)).toBe(true);
195
- // Add one non-zero element
196
- // eslint-disable-next-line prefer-destructuring
197
- const row = largeZeroMatrix[50];
198
- if (row)
199
- row[50] = 1e-13;
200
- expect(MatrixIsZero(largeZeroMatrix)).toBe(false);
201
- });
202
- test('should handle floating point precision edge cases', () => {
203
- const matrix = [[0.1 + 0.2 - 0.3, 0], [0, 0]]; // Should be close to 0 due to floating point precision
204
- expect(MatrixIsZero(matrix)).toBe(true); // Should be within default threshold
205
- });
206
- test('should throw error for invalid matrix', () => {
207
- expect(() => MatrixIsZero('invalid')).toThrow();
208
- });
209
- test('should handle zero threshold', () => {
210
- const matrix = [[0, 0], [0, 0]];
211
- expect(MatrixIsZero(matrix, 0)).toBe(true);
212
- const matrixWithTinyValue = [[1e-100, 0], [0, 0]];
213
- expect(MatrixIsZero(matrixWithTinyValue, 0)).toBe(false);
214
- });
215
- test('should handle single row zero matrix', () => {
216
- const matrix = [[0, 0, 0, 0]];
217
- expect(MatrixIsZero(matrix)).toBe(true);
218
- });
219
- test('should handle single column zero matrix', () => {
220
- const matrix = [[0], [0], [0]];
221
- expect(MatrixIsZero(matrix)).toBe(true);
222
- });
223
- test('should handle mixed positive and negative small values', () => {
224
- const matrix = [[1e-15, -1e-15], [-1e-15, 1e-15]];
225
- expect(MatrixIsZero(matrix)).toBe(true);
226
- });
227
- });
228
- describe('MatrixIdentity', () => {
229
- test('should create 2x2 identity matrix', () => {
230
- const identity = MatrixIdentity(2);
231
- expect(identity).toEqual([[1, 0], [0, 1]]);
232
- });
233
- test('should create 3x3 identity matrix', () => {
234
- const identity = MatrixIdentity(3);
235
- expect(identity).toEqual([
236
- [1, 0, 0],
237
- [0, 1, 0],
238
- [0, 0, 1],
239
- ]);
240
- });
241
- test('should create 1x1 identity matrix', () => {
242
- const identity = MatrixIdentity(1);
243
- expect(identity).toEqual([[1]]);
244
- });
245
- test('should create empty identity matrix for size 0', () => {
246
- const identity = MatrixIdentity(0);
247
- expect(identity).toEqual([]);
248
- });
249
- test('should throw error for negative size', () => {
250
- expect(() => MatrixIdentity(-1)).toThrow('Size must be a non-negative integer');
251
- });
252
- test('should throw error for non-integer size', () => {
253
- expect(() => MatrixIdentity(2.5)).toThrow('Size must be a non-negative integer');
254
- });
255
- // Test overloaded versions for specific matrix types
256
- test('should create 1x1 identity matrix with IMatrix1 overload', () => {
257
- const identity = MatrixIdentity(1);
258
- expect(identity).toEqual([[1]]);
259
- // TypeScript compilation ensures correct typing
260
- expect(Array.isArray(identity)).toBe(true);
261
- expect(identity.length).toBe(1);
262
- expect(identity[0].length).toBe(1);
263
- });
264
- test('should create 2x2 identity matrix with IMatrix2 overload', () => {
265
- const identity = MatrixIdentity(2);
266
- expect(identity).toEqual([[1, 0], [0, 1]]);
267
- // TypeScript compilation ensures correct typing
268
- expect(Array.isArray(identity)).toBe(true);
269
- expect(identity.length).toBe(2);
270
- expect(identity[0].length).toBe(2);
271
- expect(identity[1].length).toBe(2);
272
- });
273
- test('should create 3x3 identity matrix with IMatrix3 overload', () => {
274
- const identity = MatrixIdentity(3);
275
- expect(identity).toEqual([
276
- [1, 0, 0],
277
- [0, 1, 0],
278
- [0, 0, 1],
279
- ]);
280
- // TypeScript compilation ensures correct typing
281
- expect(Array.isArray(identity)).toBe(true);
282
- expect(identity.length).toBe(3);
283
- expect(identity[0].length).toBe(3);
284
- expect(identity[1].length).toBe(3);
285
- expect(identity[2].length).toBe(3);
286
- });
287
- test('should create 4x4 identity matrix with IMatrix4 overload', () => {
288
- const identity = MatrixIdentity(4);
289
- expect(identity).toEqual([
290
- [1, 0, 0, 0],
291
- [0, 1, 0, 0],
292
- [0, 0, 1, 0],
293
- [0, 0, 0, 1],
294
- ]);
295
- // TypeScript compilation ensures correct typing
296
- expect(Array.isArray(identity)).toBe(true);
297
- expect(identity.length).toBe(4);
298
- expect(identity[0].length).toBe(4);
299
- expect(identity[1].length).toBe(4);
300
- expect(identity[2].length).toBe(4);
301
- expect(identity[3].length).toBe(4);
302
- });
303
- test('should create identity matrices with proper diagonal elements for overloaded types', () => {
304
- // Test IMatrix1
305
- const id1 = MatrixIdentity(1);
306
- expect(id1[0][0]).toBe(1);
307
- // Test IMatrix2
308
- const id2 = MatrixIdentity(2);
309
- expect(id2[0][0]).toBe(1);
310
- expect(id2[1][1]).toBe(1);
311
- expect(id2[0][1]).toBe(0);
312
- expect(id2[1][0]).toBe(0);
313
- // Test IMatrix3
314
- const id3 = MatrixIdentity(3);
315
- expect(id3[0][0]).toBe(1);
316
- expect(id3[1][1]).toBe(1);
317
- expect(id3[2][2]).toBe(1);
318
- // Check all off-diagonal elements are 0
319
- for (let i = 0; i < 3; i++) {
320
- const id3Row = id3[i];
321
- AssertMatrixRow(id3Row);
322
- for (let j = 0; j < 3; j++) {
323
- const id3Value = id3Row[j];
324
- AssertMatrixValue(id3Value, { rowIndex: i, columnIndex: j });
325
- if (i !== j)
326
- expect(id3Value).toBe(0);
327
- }
328
- }
329
- // Test IMatrix4
330
- const id4 = MatrixIdentity(4);
331
- expect(id4[0][0]).toBe(1);
332
- expect(id4[1][1]).toBe(1);
333
- expect(id4[2][2]).toBe(1);
334
- expect(id4[3][3]).toBe(1);
335
- // Check all off-diagonal elements are 0
336
- for (let i = 0; i < 4; i++) {
337
- const id4Row = id4[i];
338
- AssertMatrixRow(id4Row);
339
- for (let j = 0; j < 4; j++) {
340
- const id4Value = id4Row[j];
341
- AssertMatrixValue(id4Value, { rowIndex: i, columnIndex: j });
342
- if (i !== j)
343
- expect(id4Value).toBe(0);
344
- }
345
- }
346
- });
347
- test('should handle edge cases for overloaded matrix types', () => {
348
- // Test that all diagonal elements are exactly 1
349
- // Test each size individually to ensure proper overload resolution
350
- [1, 2, 3, 4].forEach((size) => {
351
- let identity;
352
- switch (size) {
353
- case 1:
354
- identity = MatrixIdentity(1);
355
- break;
356
- case 2:
357
- identity = MatrixIdentity(2);
358
- break;
359
- case 3:
360
- identity = MatrixIdentity(3);
361
- break;
362
- case 4:
363
- identity = MatrixIdentity(4);
364
- break;
365
- default:
366
- throw new Error(`Unexpected size: ${size}`);
367
- }
368
- // Verify all diagonal elements are 1
369
- for (let i = 0; i < size; i++) {
370
- const row = identity[i];
371
- AssertMatrixRow(row, { rowIndex: i });
372
- const value = row[i];
373
- AssertMatrixValue(value, { rowIndex: i, columnIndex: i });
374
- expect(value).toBe(1);
375
- }
376
- // Verify all off-diagonal elements are 0
377
- for (let i = 0; i < size; i++) {
378
- const row = identity[i];
379
- AssertMatrixRow(row, { rowIndex: i });
380
- for (let j = 0; j < size; j++) {
381
- const value = row[j];
382
- AssertMatrixValue(value, { rowIndex: i, columnIndex: j });
383
- if (i !== j)
384
- expect(value).toBe(0);
385
- }
386
- }
387
- });
388
- });
389
- });
390
- describe('MatrixClone', () => {
391
- test('should create independent copy of matrix', () => {
392
- const original = [[1, 2], [3, 4]];
393
- const cloned = MatrixClone(original);
394
- expect(cloned).toEqual(original);
395
- expect(cloned).not.toBe(original);
396
- expect(cloned[0]).not.toBe(original[0]);
397
- // Modify original and ensure clone is unchanged
398
- const [row] = original;
399
- AssertMatrixRow(row);
400
- const [value] = row;
401
- AssertMatrixValue(value, { rowIndex: 0, columnIndex: 0 });
402
- if (original[0]) {
403
- original[0][0] = 999;
404
- }
405
- expect(cloned[0]).toBeDefined();
406
- if (cloned[0])
407
- expect(cloned[0][0]).toBe(1);
408
- });
409
- test('should throw for empty matrix', () => {
410
- const original = [];
411
- expect(() => MatrixClone(original)).toThrow('Matrix must have at least one row and one column');
412
- });
413
- test('should clone single element matrix', () => {
414
- const original = [[42]];
415
- const cloned = MatrixClone(original);
416
- expect(cloned).toEqual([[42]]);
417
- expect(cloned).not.toBe(original);
418
- });
419
- test('should throw error for invalid matrix', () => {
420
- expect(() => MatrixClone('invalid')).toThrow();
421
- });
422
- });
423
- describe('MatrixEquals', () => {
424
- test('should return true for identical matrices', () => {
425
- const a = [[1, 2], [3, 4]];
426
- const b = [[1, 2], [3, 4]];
427
- expect(MatrixEquals(a, b)).toBe(true);
428
- });
429
- test('should return false for different matrices', () => {
430
- const a = [[1, 2], [3, 4]];
431
- const b = [[1, 2], [3, 5]];
432
- expect(MatrixEquals(a, b)).toBe(false);
433
- });
434
- test('should return false for matrices with different dimensions', () => {
435
- const a = [[1, 2], [3, 4]];
436
- const b = [[1, 2, 3], [4, 5, 6]];
437
- expect(MatrixEquals(a, b)).toBe(false);
438
- });
439
- test('should return true for matrices within tolerance', () => {
440
- const a = [[1, 2], [3, 4]];
441
- const b = [[1.0001, 2], [3, 4]];
442
- expect(MatrixEquals(a, b, 0.001)).toBe(true);
443
- });
444
- test('should return false for matrices outside tolerance', () => {
445
- const a = [[1, 2], [3, 4]];
446
- const b = [[1.1, 2], [3, 4]];
447
- expect(MatrixEquals(a, b, 0.001)).toBe(false);
448
- });
449
- test('should throw for empty matrices', () => {
450
- expect(() => MatrixEquals([], [])).toThrow('Matrix must have at least one row and one column');
451
- });
452
- test('should use default tolerance', () => {
453
- const a = [[1, 2], [3, 4]];
454
- const b = [[1.0000000001, 2], [3, 4]];
455
- expect(MatrixEquals(a, b)).toBe(true);
456
- });
457
- test('should throw error for negative tolerance', () => {
458
- const a = [[1, 2]];
459
- const b = [[1, 2]];
460
- expect(() => MatrixEquals(a, b, -1)).toThrow('Tolerance must be a non-negative number');
461
- });
462
- });
463
- describe('MatrixToString', () => {
464
- test('should format matrix with default precision', () => {
465
- const matrix = [[1.234, 2.567], [3.891, 4.123]];
466
- const result = MatrixToString(matrix);
467
- expect(result).toBe('[ 1.23, 2.57 ]\n[ 3.89, 4.12 ]');
468
- });
469
- test('should format matrix with custom precision', () => {
470
- const matrix = [[1.234, 2.567]];
471
- const result = MatrixToString(matrix, 1);
472
- expect(result).toBe('[ 1.2, 2.6 ]');
473
- });
474
- test('should format matrix with zero precision', () => {
475
- const matrix = [[1.234, 2.567]];
476
- const result = MatrixToString(matrix, 0);
477
- expect(result).toBe('[ 1, 3 ]');
478
- });
479
- test('should format single element matrix', () => {
480
- const matrix = [[42.123]];
481
- const result = MatrixToString(matrix);
482
- expect(result).toBe('[ 42.12 ]');
483
- });
484
- test('should throw for empty matrix', () => {
485
- const matrix = [];
486
- expect(() => MatrixToString(matrix)).toThrow('Matrix must have at least one row and one column');
487
- });
488
- test('should throw error for negative precision', () => {
489
- const matrix = [[1, 2]];
490
- expect(() => MatrixToString(matrix, -1)).toThrow('Precision must be a non-negative integer');
491
- });
492
- test('should throw error for non-integer precision', () => {
493
- const matrix = [[1, 2]];
494
- expect(() => MatrixToString(matrix, 2.5)).toThrow('Precision must be a non-negative integer');
495
- });
496
- });
497
- describe('MatrixRank', () => {
498
- test('should return rank 2 for identity matrix', () => {
499
- const matrix = [[1, 0], [0, 1]];
500
- expect(MatrixRank(matrix)).toBe(2);
501
- });
502
- test('should return rank 1 for linearly dependent rows', () => {
503
- const matrix = [[1, 2], [2, 4]];
504
- expect(MatrixRank(matrix)).toBe(1);
505
- });
506
- test('should return rank 0 for zero matrix', () => {
507
- const matrix = [[0, 0], [0, 0]];
508
- expect(MatrixRank(matrix)).toBe(0);
509
- });
510
- test('should return rank 3 for full rank 3x3 matrix', () => {
511
- const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 10]];
512
- expect(MatrixRank(matrix)).toBe(3);
513
- });
514
- test('should return rank 2 for rectangular matrix', () => {
515
- const matrix = [[1, 2, 3], [4, 5, 6]];
516
- expect(MatrixRank(matrix)).toBe(2);
517
- });
518
- test('should throw for empty matrix', () => {
519
- const matrix = [];
520
- expect(() => MatrixRank(matrix)).toThrow('Matrix must have at least one row and one column');
521
- });
522
- test('should use custom tolerance', () => {
523
- const matrix = [[1, 2], [1.0000001, 2.0000001]];
524
- expect(MatrixRank(matrix, 1e-5)).toBe(1);
525
- expect(MatrixRank(matrix, 1e-8)).toBe(2);
526
- });
527
- test('should throw error for negative tolerance', () => {
528
- const matrix = [[1, 2]];
529
- expect(() => MatrixRank(matrix, -1)).toThrow('Tolerance must be a non-negative number');
530
- });
531
- });
532
- describe('MatrixTrace', () => {
533
- test('should return trace of 2x2 matrix', () => {
534
- const matrix = [[1, 2], [3, 4]];
535
- expect(MatrixTrace(matrix)).toBe(5); // 1 + 4
536
- });
537
- test('should return trace of 3x3 matrix', () => {
538
- const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
539
- expect(MatrixTrace(matrix)).toBe(15); // 1 + 5 + 9
540
- });
541
- test('should return trace of rectangular matrix', () => {
542
- const matrix = [[1, 2, 3], [4, 5, 6]];
543
- expect(MatrixTrace(matrix)).toBe(6); // 1 + 5
544
- });
545
- test('should return trace of single element matrix', () => {
546
- const matrix = [[42]];
547
- expect(MatrixTrace(matrix)).toBe(42);
548
- });
549
- test('should throw for empty matrix', () => {
550
- const matrix = [];
551
- expect(() => MatrixTrace(matrix)).toThrow('Matrix must have at least one row and one column');
552
- });
553
- test('should handle negative values', () => {
554
- const matrix = [[-1, 2], [3, -4]];
555
- expect(MatrixTrace(matrix)).toBe(-5); // -1 + (-4)
556
- });
557
- });
558
- describe('MatrixTranspose', () => {
559
- test('should transpose 2x2 matrix', () => {
560
- const matrix = [[1, 2], [3, 4]];
561
- const transposed = MatrixTranspose(matrix);
562
- expect(transposed).toEqual([[1, 3], [2, 4]]);
563
- });
564
- test('should transpose rectangular matrix', () => {
565
- const matrix = [[1, 2, 3], [4, 5, 6]];
566
- const transposed = MatrixTranspose(matrix);
567
- expect(transposed).toEqual([[1, 4], [2, 5], [3, 6]]);
568
- });
569
- test('should transpose single row matrix', () => {
570
- const matrix = [[1, 2, 3]];
571
- const transposed = MatrixTranspose(matrix);
572
- expect(transposed).toEqual([[1], [2], [3]]);
573
- });
574
- test('should transpose single column matrix', () => {
575
- const matrix = [[1], [2], [3]];
576
- const transposed = MatrixTranspose(matrix);
577
- expect(transposed).toEqual([[1, 2, 3]]);
578
- });
579
- test('should transpose single element matrix', () => {
580
- const matrix = [[42]];
581
- const transposed = MatrixTranspose(matrix);
582
- expect(transposed).toEqual([[42]]);
583
- });
584
- test('should throw for empty matrix', () => {
585
- const matrix = [];
586
- expect(() => MatrixTranspose(matrix)).toThrow('Matrix must have at least one row and one column');
587
- });
588
- test('should not modify original matrix', () => {
589
- const original = [[1, 2], [3, 4]];
590
- const transposed = MatrixTranspose(original);
591
- expect(original).toEqual([[1, 2], [3, 4]]);
592
- expect(transposed).not.toBe(original);
593
- });
594
- });
595
- describe('MatrixMap', () => {
596
- test('should apply function to each element', () => {
597
- const matrix = [[1, 2], [3, 4]];
598
- const squared = MatrixMap(matrix, (value) => value * value);
599
- expect(squared).toEqual([[1, 4], [9, 16]]);
600
- });
601
- test('should provide row and column indices to function', () => {
602
- const matrix = [[1, 2], [3, 4]];
603
- const result = MatrixMap(matrix, (value, row, col) => value + row + col);
604
- expect(result).toEqual([[1, 3], [4, 6]]);
605
- });
606
- test('should throw for empty matrix', () => {
607
- const matrix = [];
608
- expect(() => MatrixMap(matrix, (value) => value * 2)).toThrow('Matrix must have at least one row and one column');
609
- });
610
- test('should handle single element matrix', () => {
611
- const matrix = [[5]];
612
- const result = MatrixMap(matrix, (value) => value * 10);
613
- expect(result).toEqual([[50]]);
614
- });
615
- test('should not modify original matrix', () => {
616
- const original = [[1, 2], [3, 4]];
617
- const mapped = MatrixMap(original, (value) => value * 2);
618
- expect(original).toEqual([[1, 2], [3, 4]]);
619
- expect(mapped).toEqual([[2, 4], [6, 8]]);
620
- expect(mapped).not.toBe(original);
621
- });
622
- test('should handle negative values', () => {
623
- const matrix = [[-1, 2], [-3, 4]];
624
- const result = MatrixMap(matrix, (value) => Math.abs(value));
625
- expect(result).toEqual([[1, 2], [3, 4]]);
626
- });
627
- test('should work with complex transformations', () => {
628
- const matrix = [[1, 2, 3], [4, 5, 6]];
629
- const result = MatrixMap(matrix, (value, row, col) => (row + 1) * (col + 1) * value);
630
- expect(result).toEqual([[1, 4, 9], [8, 20, 36]]);
631
- });
632
- });
633
- });
634
- //# sourceMappingURL=core.spec.js.map