@pawells/math-extended 2.0.0 → 3.0.0
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.
- package/README.md +15 -21
- package/build/clamp.d.ts +5 -5
- package/build/clamp.js +5 -5
- package/build/core.d.ts +23 -0
- package/build/core.d.ts.map +1 -0
- package/build/core.js +25 -0
- package/build/core.js.map +1 -0
- package/build/index.d.ts +1 -4
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -6
- package/build/index.js.map +1 -1
- package/build/interpolation.d.ts +158 -171
- package/build/interpolation.d.ts.map +1 -1
- package/build/interpolation.js +162 -181
- package/build/interpolation.js.map +1 -1
- package/build/matrices/arithmetic.d.ts +132 -132
- package/build/matrices/arithmetic.d.ts.map +1 -1
- package/build/matrices/arithmetic.js +194 -226
- package/build/matrices/arithmetic.js.map +1 -1
- package/build/matrices/asserts.d.ts +30 -408
- package/build/matrices/asserts.d.ts.map +1 -1
- package/build/matrices/asserts.js +98 -542
- package/build/matrices/asserts.js.map +1 -1
- package/build/matrices/core.d.ts +117 -46
- package/build/matrices/core.d.ts.map +1 -1
- package/build/matrices/core.js +127 -103
- package/build/matrices/core.js.map +1 -1
- package/build/matrices/decompositions.d.ts +95 -96
- package/build/matrices/decompositions.d.ts.map +1 -1
- package/build/matrices/decompositions.js +119 -160
- package/build/matrices/decompositions.js.map +1 -1
- package/build/matrices/index.d.ts +0 -1
- package/build/matrices/index.d.ts.map +1 -1
- package/build/matrices/index.js +0 -3
- package/build/matrices/index.js.map +1 -1
- package/build/matrices/linear-algebra.d.ts +45 -8
- package/build/matrices/linear-algebra.d.ts.map +1 -1
- package/build/matrices/linear-algebra.js +76 -32
- package/build/matrices/linear-algebra.js.map +1 -1
- package/build/matrices/normalization.d.ts +29 -8
- package/build/matrices/normalization.d.ts.map +1 -1
- package/build/matrices/normalization.js +32 -23
- package/build/matrices/normalization.js.map +1 -1
- package/build/matrices/transformations.d.ts +116 -148
- package/build/matrices/transformations.d.ts.map +1 -1
- package/build/matrices/transformations.js +69 -91
- package/build/matrices/transformations.js.map +1 -1
- package/build/matrices/types.d.ts +32 -60
- package/build/matrices/types.d.ts.map +1 -1
- package/build/matrices/types.js +63 -1
- package/build/matrices/types.js.map +1 -1
- package/build/quaternions/asserts.d.ts +29 -38
- package/build/quaternions/asserts.d.ts.map +1 -1
- package/build/quaternions/asserts.js +61 -77
- package/build/quaternions/asserts.js.map +1 -1
- package/build/quaternions/conversions.d.ts +26 -26
- package/build/quaternions/conversions.d.ts.map +1 -1
- package/build/quaternions/conversions.js +24 -24
- package/build/quaternions/core.d.ts +70 -69
- package/build/quaternions/core.d.ts.map +1 -1
- package/build/quaternions/core.js +71 -71
- package/build/quaternions/core.js.map +1 -1
- package/build/quaternions/index.d.ts +0 -1
- package/build/quaternions/index.d.ts.map +1 -1
- package/build/quaternions/index.js +0 -2
- package/build/quaternions/index.js.map +1 -1
- package/build/quaternions/interpolation.d.ts +16 -16
- package/build/quaternions/interpolation.d.ts.map +1 -1
- package/build/quaternions/interpolation.js +21 -21
- package/build/quaternions/interpolation.js.map +1 -1
- package/build/quaternions/predefined.d.ts +10 -10
- package/build/quaternions/predefined.d.ts.map +1 -1
- package/build/quaternions/predefined.js +9 -9
- package/build/quaternions/types.d.ts +23 -12
- package/build/quaternions/types.d.ts.map +1 -1
- package/build/quaternions/types.js +51 -1
- package/build/quaternions/types.js.map +1 -1
- package/build/random.d.ts +66 -20
- package/build/random.d.ts.map +1 -1
- package/build/random.js +73 -20
- package/build/random.js.map +1 -1
- package/build/vectors/asserts.d.ts +50 -220
- package/build/vectors/asserts.d.ts.map +1 -1
- package/build/vectors/asserts.js +141 -327
- package/build/vectors/asserts.js.map +1 -1
- package/build/vectors/core.d.ts +182 -229
- package/build/vectors/core.d.ts.map +1 -1
- package/build/vectors/core.js +234 -288
- package/build/vectors/core.js.map +1 -1
- package/build/vectors/index.d.ts +0 -1
- package/build/vectors/index.d.ts.map +1 -1
- package/build/vectors/index.js +0 -2
- package/build/vectors/index.js.map +1 -1
- package/build/vectors/interpolation.d.ts +40 -40
- package/build/vectors/interpolation.d.ts.map +1 -1
- package/build/vectors/interpolation.js +75 -86
- package/build/vectors/interpolation.js.map +1 -1
- package/build/vectors/predefined.d.ts +31 -7
- package/build/vectors/predefined.d.ts.map +1 -1
- package/build/vectors/predefined.js +30 -6
- package/build/vectors/predefined.js.map +1 -1
- package/build/vectors/types.d.ts +26 -4
- package/build/vectors/types.d.ts.map +1 -1
- package/build/vectors/types.js +50 -1
- package/build/vectors/types.js.map +1 -1
- package/package.json +3 -2
- package/build/matrices/_exports.d.ts +0 -13
- package/build/matrices/_exports.d.ts.map +0 -1
- package/build/matrices/_exports.js +0 -13
- package/build/matrices/_exports.js.map +0 -1
- package/build/quaternions/_exports.d.ts +0 -11
- package/build/quaternions/_exports.d.ts.map +0 -1
- package/build/quaternions/_exports.js +0 -11
- package/build/quaternions/_exports.js.map +0 -1
- package/build/vectors/_exports.d.ts +0 -10
- package/build/vectors/_exports.d.ts.map +0 -1
- package/build/vectors/_exports.js +0 -10
- package/build/vectors/_exports.js.map +0 -1
|
@@ -1,43 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/**
|
|
4
|
-
* Custom error class for matrix-related validation failures.
|
|
5
|
-
*
|
|
6
|
-
* This error class provides a specific error type for matrix validation
|
|
7
|
-
* failures, making it easier to catch and handle matrix-specific errors
|
|
8
|
-
* separately from other types of errors in your application.
|
|
9
|
-
*
|
|
10
|
-
* @class MatrixError
|
|
11
|
-
* @extends Error
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* ```typescript
|
|
15
|
-
* try {
|
|
16
|
-
* AssertMatrix(invalidMatrix);
|
|
17
|
-
* } catch (error) {
|
|
18
|
-
* if (error instanceof MatrixError) {
|
|
19
|
-
* console.log('Matrix validation failed:', error.message);
|
|
20
|
-
* }
|
|
21
|
-
* }
|
|
22
|
-
* ```
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export class MatrixError extends Error {
|
|
26
|
-
code = 'MATRIX_ERROR';
|
|
27
|
-
/**
|
|
28
|
-
* Creates a new MatrixError instance.
|
|
29
|
-
*
|
|
30
|
-
* @param message - Error message describing the validation failure
|
|
31
|
-
* @param options - Optional error context
|
|
32
|
-
* @param options.cause - Original error that caused this error
|
|
33
|
-
*/
|
|
34
|
-
constructor(message, options) {
|
|
35
|
-
super(message, options);
|
|
36
|
-
this.name = 'MatrixError';
|
|
37
|
-
// Maintains proper prototype chain for instanceof checks
|
|
38
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
1
|
+
import { MatrixSize } from '../index.js';
|
|
2
|
+
import { MATRIX1_SCHEMA, MATRIX2_SCHEMA, MATRIX3_SCHEMA, MATRIX4_SCHEMA, MATRIX_SCHEMA, MATRIX_SQUARE_SCHEMA } from './types.js';
|
|
41
3
|
/**
|
|
42
4
|
* Validates that an unknown value is a valid matrix conforming to the TMatrix interface.
|
|
43
5
|
*
|
|
@@ -50,544 +12,138 @@ export class MatrixError extends Error {
|
|
|
50
12
|
* @param matrix - The value to validate as a matrix
|
|
51
13
|
* @param args - Validation configuration options
|
|
52
14
|
* @param exception - Custom exception details if validation fails
|
|
53
|
-
* @throws {
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* ```typescript
|
|
58
|
-
* // Validate a 3x3 square matrix
|
|
59
|
-
* AssertMatrix(someValue, { square: true, size: 3 });
|
|
60
|
-
* // Validate minimum dimensions
|
|
61
|
-
* AssertMatrix(someValue, { minRows: 2, minColumns: 3 });
|
|
62
|
-
* // Validate exact dimensions
|
|
63
|
-
* AssertMatrix(someValue, { rows: 4, columns: 5 });
|
|
64
|
-
* ```
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export function AssertMatrix(matrix, args = {}, exception = {}) {
|
|
68
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
69
|
-
SetExceptionClass(exception, MatrixError);
|
|
70
|
-
// First validation: ensure the input is a valid 2D array structure
|
|
71
|
-
AssertArray2D(matrix, {}, exception);
|
|
72
|
-
// Second validation: verify all elements are numeric values
|
|
73
|
-
// Note: We check both type and NaN since NaN has type 'number' but isn't a valid matrix element
|
|
74
|
-
for (let i = 0; i < matrix.length; i++) {
|
|
75
|
-
const row = matrix[i];
|
|
76
|
-
if (row) {
|
|
77
|
-
for (let j = 0; j < row.length; j++) {
|
|
78
|
-
if (typeof row[j] !== 'number' || Number.isNaN(row[j])) {
|
|
79
|
-
SetExceptionMessage(exception, `Matrix[${i}][${j}] Not a Number`);
|
|
80
|
-
ThrowException(exception);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
// Extract matrix dimensions for constraint validation
|
|
86
|
-
const [rows, columns] = MatrixSize(matrix);
|
|
87
|
-
// Reject empty matrices (zero rows or zero columns)
|
|
88
|
-
if (rows === 0 || columns === 0) {
|
|
89
|
-
SetExceptionMessage(exception, 'Matrix must have at least one row and one column');
|
|
90
|
-
ThrowException(exception);
|
|
91
|
-
}
|
|
92
|
-
// Validate exact size constraint when specified as [rows, columns] tuple
|
|
93
|
-
if (Array.isArray(args.size)) {
|
|
94
|
-
const [expectedRows, expectedColumns] = args.size;
|
|
95
|
-
if (rows !== expectedRows) {
|
|
96
|
-
SetExceptionMessage(exception, `Matrix has ${rows} rows, expected exactly ${expectedRows}`);
|
|
97
|
-
ThrowException(exception);
|
|
98
|
-
}
|
|
99
|
-
if (columns !== expectedColumns) {
|
|
100
|
-
SetExceptionMessage(exception, `Matrix has ${columns} columns, expected exactly ${expectedColumns}`);
|
|
101
|
-
ThrowException(exception);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
else if (typeof args.size === 'number') {
|
|
105
|
-
// Validate exact size constraint when specified as single number (assumes square matrix)
|
|
106
|
-
if (rows !== args.size) {
|
|
107
|
-
SetExceptionMessage(exception, `Square matrix has ${rows} rows, expected exactly ${args.size}`);
|
|
108
|
-
ThrowException(exception);
|
|
109
|
-
}
|
|
110
|
-
if (columns !== args.size) {
|
|
111
|
-
SetExceptionMessage(exception, `Square matrix has ${columns} columns, expected exactly ${args.size}`);
|
|
112
|
-
ThrowException(exception);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
// Validate exact row count constraint when explicitly specified
|
|
116
|
-
if (args.rows !== undefined && rows !== args.rows) {
|
|
117
|
-
SetExceptionMessage(exception, `Matrix has ${rows} rows, expected exactly ${args.rows}`);
|
|
118
|
-
ThrowException(exception);
|
|
119
|
-
}
|
|
120
|
-
// Validate exact column count constraint when explicitly specified
|
|
121
|
-
if (args.columns !== undefined && columns !== args.columns) {
|
|
122
|
-
SetExceptionMessage(exception, `Matrix has ${columns} columns, expected exactly ${args.columns}`);
|
|
123
|
-
ThrowException(exception);
|
|
124
|
-
}
|
|
125
|
-
// Validate minimum row count constraint (useful for operations requiring minimum dimensions)
|
|
126
|
-
if (args.minRows !== undefined && rows < args.minRows) {
|
|
127
|
-
SetExceptionMessage(exception, `Matrix has ${rows} rows, minimum required is ${args.minRows}`);
|
|
128
|
-
ThrowException(exception);
|
|
129
|
-
}
|
|
130
|
-
// Validate maximum row count constraint (useful for memory or performance limits)
|
|
131
|
-
if (args.maxRows !== undefined && rows > args.maxRows) {
|
|
132
|
-
SetExceptionMessage(exception, `Matrix has ${rows} rows, maximum allowed is ${args.maxRows}`);
|
|
133
|
-
ThrowException(exception);
|
|
134
|
-
}
|
|
135
|
-
// Validate minimum column count constraint (useful for operations requiring minimum dimensions)
|
|
136
|
-
if (args.minColumns !== undefined && columns < args.minColumns) {
|
|
137
|
-
SetExceptionMessage(exception, `Matrix has ${columns} columns, minimum required is ${args.minColumns}`);
|
|
138
|
-
ThrowException(exception);
|
|
139
|
-
}
|
|
140
|
-
// Validate maximum column count constraint (useful for memory or performance limits)
|
|
141
|
-
if (args.maxColumns !== undefined && columns > args.maxColumns) {
|
|
142
|
-
SetExceptionMessage(exception, `Matrix has ${columns} columns, maximum allowed is ${args.maxColumns}`);
|
|
143
|
-
ThrowException(exception);
|
|
144
|
-
}
|
|
145
|
-
// Validate square matrix constraint (required for operations like determinant, inverse, etc.)
|
|
146
|
-
if (args.square === true && rows !== columns) {
|
|
147
|
-
SetExceptionMessage(exception, `Matrix must be square but has ${rows} rows and ${columns} columns`);
|
|
148
|
-
ThrowException(exception);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Validates that an unknown value is a valid matrix row (array of numbers).
|
|
153
|
-
*
|
|
154
|
-
* This function ensures that the provided value is a proper matrix row,
|
|
155
|
-
* which should be an array containing only numeric values.
|
|
156
|
-
*
|
|
157
|
-
* @param row - The value to validate as a matrix row
|
|
158
|
-
* @param exception - Custom exception details (optionally includes `rowIndex` for richer messages)
|
|
159
|
-
* @throws {MatrixError} When the row is not a valid array of finite numbers
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* ```typescript
|
|
163
|
-
* ```typescript
|
|
164
|
-
* // Validate a matrix row
|
|
165
|
-
* AssertMatrixRow([1, 2, 3, 4]);
|
|
166
|
-
* // Validate with row index in exception for better error messages
|
|
167
|
-
* AssertMatrixRow([1, 2, 3, 4], { rowIndex: 0 });
|
|
168
|
-
* // This would throw MatrixError
|
|
169
|
-
* AssertMatrixRow([1, '2', 3]); // Contains non-numeric value
|
|
170
|
-
* ```
|
|
171
|
-
* ```
|
|
172
|
-
*/
|
|
173
|
-
export function AssertMatrixRow(row, exception = {}) {
|
|
174
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
175
|
-
SetExceptionClass(exception, MatrixError);
|
|
176
|
-
// First validation: ensure the input is an array
|
|
177
|
-
if (!Array.isArray(row)) {
|
|
178
|
-
const rowInfo = exception.rowIndex !== undefined ? ` at row ${exception.rowIndex}` : '';
|
|
179
|
-
SetExceptionMessage(exception, `Matrix row${rowInfo} must be an array`);
|
|
180
|
-
ThrowException(exception);
|
|
181
|
-
}
|
|
182
|
-
// Safe cast to array since we've verified it's an array
|
|
183
|
-
const rowArray = row;
|
|
184
|
-
// Second validation: ensure all elements are finite numbers
|
|
185
|
-
// This excludes NaN, Infinity, and -Infinity which are technically numbers but invalid for matrices
|
|
186
|
-
for (let i = 0; i < rowArray.length; i++) {
|
|
187
|
-
if (typeof rowArray[i] !== 'number' || !isFinite(rowArray[i])) {
|
|
188
|
-
const rowInfo = exception.rowIndex !== undefined ? ` at row ${exception.rowIndex}` : '';
|
|
189
|
-
SetExceptionMessage(exception, `Matrix row${rowInfo} element at index ${i} is not a finite number`);
|
|
190
|
-
ThrowException(exception);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Validates that an unknown value is a valid matrix element (finite number).
|
|
196
|
-
*
|
|
197
|
-
* This function ensures that the provided value is a proper matrix element,
|
|
198
|
-
* which should be a finite number (not NaN, Infinity, or -Infinity).
|
|
199
|
-
*
|
|
200
|
-
* @param value - The value to validate as a matrix element
|
|
201
|
-
* @param exception - Custom exception details (optionally includes `rowIndex` and `columnIndex` for richer messages)
|
|
202
|
-
* @throws {MatrixError} When the value is not a finite number
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* ```typescript
|
|
206
|
-
* ```typescript
|
|
207
|
-
* // Validate a matrix element
|
|
208
|
-
* AssertMatrixValue(42);
|
|
209
|
-
* // Validate with position information for better error messages
|
|
210
|
-
* AssertMatrixValue(3.14, { rowIndex: 0, columnIndex: 1 });
|
|
211
|
-
* // These would throw MatrixError
|
|
212
|
-
* AssertMatrixValue(NaN); // Not a number
|
|
213
|
-
* AssertMatrixValue(Infinity); // Not a finite number
|
|
214
|
-
* AssertMatrixValue('5'); // Not a number type
|
|
215
|
-
* ```
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
|
-
export function AssertMatrixValue(value, exception = {}) {
|
|
219
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
220
|
-
SetExceptionClass(exception, MatrixError);
|
|
221
|
-
// Build position information for error messages when row and column indices are available
|
|
222
|
-
// This provides more context about where the validation failure occurred
|
|
223
|
-
const position = exception.rowIndex !== undefined && exception.columnIndex !== undefined ? ` at row ${exception.rowIndex}, column ${exception.columnIndex}` : '';
|
|
224
|
-
SetExceptionMessage(exception, `Matrix value${position} must be a finite number`);
|
|
225
|
-
// Delegate to the general number assertion which handles finite number validation
|
|
226
|
-
// Wrap any thrown error as MatrixError to maintain consistent error handling
|
|
227
|
-
try {
|
|
228
|
-
AssertNumber(value, { finite: true }, exception);
|
|
229
|
-
}
|
|
230
|
-
catch {
|
|
231
|
-
// Re-throw as MatrixError to maintain API contract
|
|
232
|
-
throw new MatrixError(exception.message ?? 'Matrix value must be a finite number');
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Validates compatibility between two matrices for mathematical operations.
|
|
237
|
-
*
|
|
238
|
-
* This function checks that two matrices are compatible for operations like
|
|
239
|
-
* multiplication, addition, or other matrix operations that require specific
|
|
240
|
-
* dimensional relationships.
|
|
241
|
-
*
|
|
242
|
-
* @param a - The first matrix to validate
|
|
243
|
-
* @param b - The second matrix to validate
|
|
244
|
-
* @param args - Validation configuration options
|
|
245
|
-
* @param exception - Custom exception details if validation fails
|
|
246
|
-
* @throws {IAssertException} When the matrices are not compatible
|
|
247
|
-
*
|
|
248
|
-
* @example
|
|
249
|
-
* ```typescript
|
|
250
|
-
* ```typescript
|
|
251
|
-
* // Validate matrices for multiplication (A columns must equal B rows)
|
|
252
|
-
* AssertMatrices(matrixA, matrixB);
|
|
253
|
-
* // Allow transposed compatibility
|
|
254
|
-
* AssertMatrices(matrixA, matrixB, { transposition: true });
|
|
255
|
-
* ```
|
|
256
|
-
* ```
|
|
257
|
-
*/
|
|
258
|
-
export function AssertMatrices(a, b, args = {}, exception = {}) {
|
|
259
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
260
|
-
SetExceptionClass(exception, MatrixError);
|
|
261
|
-
// First validation: ensure both inputs are valid matrices
|
|
262
|
-
AssertMatrix(a, {}, exception);
|
|
263
|
-
AssertMatrix(b, {}, exception);
|
|
264
|
-
// Safe cast to TMatrix since we've validated both inputs
|
|
265
|
-
const matrixA = a;
|
|
266
|
-
const matrixB = b;
|
|
267
|
-
// Extract dimensions from both matrices for compatibility checking
|
|
268
|
-
const rowsA = matrixA.length;
|
|
269
|
-
const columnsA = rowsA > 0 && matrixA[0] ? matrixA[0].length : 0;
|
|
270
|
-
const rowsB = matrixB.length;
|
|
271
|
-
const columnsB = rowsB > 0 && matrixB[0] ? matrixB[0].length : 0;
|
|
272
|
-
// Check compatibility based on whether transposition is allowed
|
|
273
|
-
if (args.transposition === true) {
|
|
274
|
-
// With transposition allowed, check all possible multiplication combinations:
|
|
275
|
-
// - A × B (standard): columns of A must equal rows of B
|
|
276
|
-
// - A × B^T (B transposed): columns of A must equal columns of B
|
|
277
|
-
// - A^T × B (A transposed): rows of A must equal rows of B
|
|
278
|
-
// - A^T × B^T (both transposed): rows of A must equal columns of B
|
|
279
|
-
const canMultiplyAB = columnsA === rowsB;
|
|
280
|
-
const canMultiplyABT = columnsA === columnsB;
|
|
281
|
-
const canMultiplyATB = rowsA === rowsB;
|
|
282
|
-
const canMultiplyATBT = rowsA === columnsB;
|
|
283
|
-
if (!canMultiplyAB && !canMultiplyABT && !canMultiplyATB && !canMultiplyATBT) {
|
|
284
|
-
SetExceptionMessage(exception, `Matrices are not compatible for multiplication even with transposition. Matrix A is ${rowsA}×${columnsA}, Matrix B is ${rowsB}×${columnsB}`);
|
|
285
|
-
ThrowException(exception);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
// Without transposition: matrices must have identical dimensions
|
|
290
|
-
// This is required for element-wise operations like addition and subtraction
|
|
291
|
-
if (rowsA !== rowsB || columnsA !== columnsB) {
|
|
292
|
-
SetExceptionMessage(exception, `Matrices must have identical dimensions for addition/subtraction. Matrix A is ${rowsA}×${columnsA}, Matrix B is ${rowsB}×${columnsB}`);
|
|
293
|
-
ThrowException(exception);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Validates that an unknown value is a valid 1x1 matrix.
|
|
299
|
-
*
|
|
300
|
-
* This function ensures that the provided value is a proper 1x1 matrix,
|
|
301
|
-
* which should be a 2D array with exactly 1 row and 1 column containing a number.
|
|
302
|
-
*
|
|
303
|
-
* @param matrix - The value to validate as a 1x1 matrix
|
|
304
|
-
* @param exception - Custom exception details if validation fails
|
|
305
|
-
* @throws {IAssertException} When the matrix is not a valid 1x1 matrix
|
|
306
|
-
*
|
|
307
|
-
* @example
|
|
308
|
-
* ```typescript
|
|
309
|
-
* ```typescript
|
|
310
|
-
* // Validate a 1x1 matrix
|
|
311
|
-
* AssertMatrix1([[5]]);
|
|
312
|
-
* // This would throw an exception
|
|
313
|
-
* AssertMatrix1([[1, 2]]); // Too many columns
|
|
314
|
-
* ```
|
|
315
|
-
* ```
|
|
316
|
-
*/
|
|
317
|
-
export function AssertMatrix1(matrix, exception = {}) {
|
|
318
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
319
|
-
SetExceptionClass(exception, MatrixError);
|
|
320
|
-
// Delegate to the general matrix assertion with 1x1 square matrix constraints
|
|
321
|
-
// This ensures the matrix is exactly 1 row by 1 column
|
|
322
|
-
AssertMatrix(matrix, { square: true, size: 1 }, exception);
|
|
323
|
-
}
|
|
324
|
-
/**
|
|
325
|
-
* Validates that an unknown value is a valid 2x2 matrix.
|
|
326
|
-
*
|
|
327
|
-
* This function ensures that the provided value is a proper 2x2 matrix,
|
|
328
|
-
* which should be a 2D array with exactly 2 rows and 2 columns containing numbers.
|
|
329
|
-
*
|
|
330
|
-
* @param matrix - The value to validate as a 2x2 matrix
|
|
331
|
-
* @param exception - Custom exception details if validation fails
|
|
332
|
-
* @throws {IAssertException} When the matrix is not a valid 2x2 matrix
|
|
333
|
-
*
|
|
334
|
-
* @example
|
|
335
|
-
* ```typescript
|
|
336
|
-
* ```typescript
|
|
337
|
-
* // Validate a 2x2 matrix
|
|
338
|
-
* AssertMatrix2([[1, 2], [3, 4]]);
|
|
339
|
-
* // This would throw an exception
|
|
340
|
-
* AssertMatrix2([[1, 2, 3], [4, 5, 6]]); // Too many columns
|
|
341
|
-
* ```
|
|
342
|
-
* ```
|
|
343
|
-
*/
|
|
344
|
-
export function AssertMatrix2(matrix, exception = {}) {
|
|
345
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
346
|
-
SetExceptionClass(exception, MatrixError);
|
|
347
|
-
// Delegate to the general matrix assertion with 2x2 square matrix constraints
|
|
348
|
-
// This ensures the matrix is exactly 2 rows by 2 columns
|
|
349
|
-
AssertMatrix(matrix, { square: true, size: 2 }, exception);
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Validates that an unknown value is a valid 3x3 matrix.
|
|
353
|
-
*
|
|
354
|
-
* This function ensures that the provided value is a proper 3x3 matrix,
|
|
355
|
-
* which should be a 2D array with exactly 3 rows and 3 columns containing numbers.
|
|
356
|
-
*
|
|
357
|
-
* @param matrix - The value to validate as a 3x3 matrix
|
|
358
|
-
* @param exception - Custom exception details if validation fails
|
|
359
|
-
* @throws {IAssertException} When the matrix is not a valid 3x3 matrix
|
|
360
|
-
*
|
|
361
|
-
* @example
|
|
362
|
-
* ```typescript
|
|
363
|
-
* ```typescript
|
|
364
|
-
* // Validate a 3x3 matrix
|
|
365
|
-
* AssertMatrix3([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
|
|
366
|
-
* // This would throw an exception
|
|
367
|
-
* AssertMatrix3([[1, 2], [3, 4]]); // Too few rows and columns
|
|
368
|
-
* ```
|
|
369
|
-
* ```
|
|
370
|
-
*/
|
|
371
|
-
export function AssertMatrix3(matrix, exception = {}) {
|
|
372
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
373
|
-
SetExceptionClass(exception, MatrixError);
|
|
374
|
-
// Delegate to the general matrix assertion with 3x3 square matrix constraints
|
|
375
|
-
// This ensures the matrix is exactly 3 rows by 3 columns
|
|
376
|
-
AssertMatrix(matrix, { square: true, size: 3 }, exception);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Validates that an unknown value is a valid 4x4 matrix.
|
|
380
|
-
*
|
|
381
|
-
* This function ensures that the provided value is a proper 4x4 matrix,
|
|
382
|
-
* which should be a 2D array with exactly 4 rows and 4 columns containing numbers.
|
|
383
|
-
*
|
|
384
|
-
* @param matrix - The value to validate as a 4x4 matrix
|
|
385
|
-
* @param exception - Custom exception details if validation fails
|
|
386
|
-
* @throws {IAssertException} When the matrix is not a valid 4x4 matrix
|
|
387
|
-
*
|
|
388
|
-
* @example
|
|
389
|
-
* ```typescript
|
|
390
|
-
* ```typescript
|
|
391
|
-
* // Validate a 4x4 matrix
|
|
392
|
-
* AssertMatrix4([
|
|
393
|
-
* [1, 2, 3, 4],
|
|
394
|
-
* [5, 6, 7, 8],
|
|
395
|
-
* [9, 10, 11, 12],
|
|
396
|
-
* [13, 14, 15, 16]
|
|
397
|
-
* ]);
|
|
398
|
-
* // This would throw an exception
|
|
399
|
-
* AssertMatrix4([[1, 2], [3, 4]]); // Too few rows and columns
|
|
400
|
-
* ```
|
|
401
|
-
* ```
|
|
402
|
-
*/
|
|
403
|
-
export function AssertMatrix4(matrix, exception = {}) {
|
|
404
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
405
|
-
SetExceptionClass(exception, MatrixError);
|
|
406
|
-
// Delegate to the general matrix assertion with 4x4 square matrix constraints
|
|
407
|
-
// This ensures the matrix is exactly 4 rows by 4 columns
|
|
408
|
-
AssertMatrix(matrix, { square: true, size: 4 }, exception);
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Validates that an unknown value is a valid matrix without throwing an error.
|
|
412
|
-
*
|
|
413
|
-
* This function performs the same validation as AssertMatrix but returns
|
|
414
|
-
* a boolean instead of throwing an exception, making it suitable for
|
|
415
|
-
* conditional logic where exceptions are not desired.
|
|
416
|
-
*
|
|
417
|
-
* @param matrix - The value to validate as a matrix
|
|
418
|
-
* @param args - Validation configuration options
|
|
419
|
-
* @returns true if the matrix is valid, false otherwise
|
|
15
|
+
* @throws {MatrixError} When the matrix doesn't meet the specified criteria
|
|
420
16
|
*
|
|
421
17
|
* @example
|
|
422
18
|
* ```typescript
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
* }
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Validate a 3x3 square matrix
|
|
21
|
+
* AssertMatrix(someValue, { square: true, size: 3 });
|
|
22
|
+
* // Validate minimum dimensions
|
|
23
|
+
* AssertMatrix(someValue, { minRows: 2, minColumns: 3 });
|
|
24
|
+
* // Validate exact dimensions
|
|
25
|
+
* AssertMatrix(someValue, { rows: 4, columns: 5 });
|
|
26
|
+
* ```
|
|
426
27
|
* ```
|
|
427
28
|
*/
|
|
428
|
-
export function ValidateMatrix(matrix, args = {}) {
|
|
429
|
-
try {
|
|
430
|
-
AssertMatrix(matrix, args);
|
|
431
|
-
return true;
|
|
432
|
-
}
|
|
433
|
-
catch {
|
|
434
|
-
return false;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
29
|
/**
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
* @param matrix - The value to validate as a 1×1 matrix
|
|
441
|
-
* @returns true if the matrix is valid, false otherwise
|
|
30
|
+
* Matrix error class for validation failures and matrix operations.
|
|
31
|
+
* Extends Error to provide detailed error information with optional cause chain.
|
|
442
32
|
*
|
|
443
33
|
* @example
|
|
444
34
|
* ```typescript
|
|
445
|
-
*
|
|
446
|
-
* // Process the valid 1×1 matrix
|
|
447
|
-
* }
|
|
35
|
+
* throw new MatrixError('Invalid matrix dimensions', { cause: originalError });
|
|
448
36
|
* ```
|
|
449
37
|
*/
|
|
450
|
-
export
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
38
|
+
export class MatrixError extends Error {
|
|
39
|
+
code = 'MATRIX_ERROR';
|
|
40
|
+
constructor(message, options) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = 'MatrixError';
|
|
43
|
+
if (options?.cause) {
|
|
44
|
+
this.cause = options.cause;
|
|
45
|
+
}
|
|
457
46
|
}
|
|
458
47
|
}
|
|
459
48
|
/**
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
* @
|
|
463
|
-
* @returns true if the matrix is valid, false otherwise
|
|
464
|
-
*
|
|
465
|
-
* @example
|
|
466
|
-
* ```typescript
|
|
467
|
-
* if (ValidateMatrix2(someValue)) {
|
|
468
|
-
* // Process the valid 2×2 matrix
|
|
469
|
-
* }
|
|
470
|
-
* ```
|
|
49
|
+
* Factory function to create a Validate* type guard from an Assert* function.
|
|
50
|
+
* @param assert - The assertion function to wrap
|
|
51
|
+
* @returns A type guard function that returns true if valid, false otherwise
|
|
471
52
|
*/
|
|
472
|
-
|
|
53
|
+
function makeValidate(assert) {
|
|
54
|
+
return (value) => {
|
|
55
|
+
try {
|
|
56
|
+
assert(value);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function AssertMatrix(matrix) {
|
|
473
65
|
try {
|
|
474
|
-
|
|
475
|
-
return true;
|
|
66
|
+
MATRIX_SCHEMA.parse(matrix);
|
|
476
67
|
}
|
|
477
|
-
catch {
|
|
478
|
-
|
|
68
|
+
catch (error) {
|
|
69
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
70
|
+
throw new MatrixError(`Invalid matrix: ${message}`, {
|
|
71
|
+
cause: error instanceof Error ? error : undefined,
|
|
72
|
+
});
|
|
479
73
|
}
|
|
480
74
|
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
*
|
|
484
|
-
* @param matrix - The value to validate as a 3×3 matrix
|
|
485
|
-
* @returns true if the matrix is valid, false otherwise
|
|
486
|
-
*
|
|
487
|
-
* @example
|
|
488
|
-
* ```typescript
|
|
489
|
-
* if (ValidateMatrix3(someValue)) {
|
|
490
|
-
* // Process the valid 3×3 matrix
|
|
491
|
-
* }
|
|
492
|
-
* ```
|
|
493
|
-
*/
|
|
494
|
-
export function ValidateMatrix3(matrix) {
|
|
75
|
+
export const ValidateMatrix = makeValidate(AssertMatrix);
|
|
76
|
+
export function AssertMatrix1(matrix) {
|
|
495
77
|
try {
|
|
496
|
-
|
|
497
|
-
return true;
|
|
78
|
+
MATRIX1_SCHEMA.parse(matrix);
|
|
498
79
|
}
|
|
499
|
-
catch {
|
|
500
|
-
|
|
80
|
+
catch (error) {
|
|
81
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
82
|
+
throw new MatrixError(`Invalid 1x1 matrix: ${message}`, {
|
|
83
|
+
cause: error instanceof Error ? error : undefined,
|
|
84
|
+
});
|
|
501
85
|
}
|
|
502
86
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
*
|
|
506
|
-
* @param matrix - The value to validate as a 4×4 matrix
|
|
507
|
-
* @returns true if the matrix is valid, false otherwise
|
|
508
|
-
*
|
|
509
|
-
* @example
|
|
510
|
-
* ```typescript
|
|
511
|
-
* if (ValidateMatrix4(someValue)) {
|
|
512
|
-
* // Process the valid 4×4 matrix
|
|
513
|
-
* }
|
|
514
|
-
* ```
|
|
515
|
-
*/
|
|
516
|
-
export function ValidateMatrix4(matrix) {
|
|
87
|
+
export const ValidateMatrix1 = makeValidate(AssertMatrix1);
|
|
88
|
+
export function AssertMatrix2(matrix) {
|
|
517
89
|
try {
|
|
518
|
-
|
|
519
|
-
return true;
|
|
90
|
+
MATRIX2_SCHEMA.parse(matrix);
|
|
520
91
|
}
|
|
521
|
-
catch {
|
|
522
|
-
|
|
92
|
+
catch (error) {
|
|
93
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
94
|
+
throw new MatrixError(`Invalid 2x2 matrix: ${message}`, {
|
|
95
|
+
cause: error instanceof Error ? error : undefined,
|
|
96
|
+
});
|
|
523
97
|
}
|
|
524
98
|
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
*
|
|
528
|
-
* @param row - The value to validate as a matrix row
|
|
529
|
-
* @returns true if the row is valid, false otherwise
|
|
530
|
-
*
|
|
531
|
-
* @example
|
|
532
|
-
* ```typescript
|
|
533
|
-
* if (ValidateMatrixRow([1, 2, 3])) {
|
|
534
|
-
* // Process the valid row
|
|
535
|
-
* }
|
|
536
|
-
* ```
|
|
537
|
-
*/
|
|
538
|
-
export function ValidateMatrixRow(row) {
|
|
99
|
+
export const ValidateMatrix2 = makeValidate(AssertMatrix2);
|
|
100
|
+
export function AssertMatrix3(matrix) {
|
|
539
101
|
try {
|
|
540
|
-
|
|
541
|
-
return true;
|
|
102
|
+
MATRIX3_SCHEMA.parse(matrix);
|
|
542
103
|
}
|
|
543
|
-
catch {
|
|
544
|
-
|
|
104
|
+
catch (error) {
|
|
105
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
106
|
+
throw new MatrixError(`Invalid 3x3 matrix: ${message}`, {
|
|
107
|
+
cause: error instanceof Error ? error : undefined,
|
|
108
|
+
});
|
|
545
109
|
}
|
|
546
110
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
*
|
|
550
|
-
* @param value - The value to validate as a matrix element
|
|
551
|
-
* @returns true if the value is valid, false otherwise
|
|
552
|
-
*
|
|
553
|
-
* @example
|
|
554
|
-
* ```typescript
|
|
555
|
-
* if (ValidateMatrixValue(someValue)) {
|
|
556
|
-
* // Process the valid numeric value
|
|
557
|
-
* }
|
|
558
|
-
* ```
|
|
559
|
-
*/
|
|
560
|
-
export function ValidateMatrixValue(value) {
|
|
111
|
+
export const ValidateMatrix3 = makeValidate(AssertMatrix3);
|
|
112
|
+
export function AssertMatrix4(matrix) {
|
|
561
113
|
try {
|
|
562
|
-
|
|
563
|
-
return true;
|
|
114
|
+
MATRIX4_SCHEMA.parse(matrix);
|
|
564
115
|
}
|
|
565
|
-
catch {
|
|
566
|
-
|
|
116
|
+
catch (error) {
|
|
117
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
118
|
+
throw new MatrixError(`Invalid 4x4 matrix: ${message}`, {
|
|
119
|
+
cause: error instanceof Error ? error : undefined,
|
|
120
|
+
});
|
|
567
121
|
}
|
|
568
122
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
*
|
|
572
|
-
* @param a - The first matrix to validate
|
|
573
|
-
* @param b - The second matrix to validate
|
|
574
|
-
* @param args - Validation configuration options
|
|
575
|
-
* @returns true if both matrices are valid and compatible, false otherwise
|
|
576
|
-
*
|
|
577
|
-
* @example
|
|
578
|
-
* ```typescript
|
|
579
|
-
* if (ValidateMatrices(matrixA, matrixB, { transposition: true })) {
|
|
580
|
-
* // Process the valid and compatible matrices
|
|
581
|
-
* }
|
|
582
|
-
* ```
|
|
583
|
-
*/
|
|
584
|
-
export function ValidateMatrices(a, b, args = {}) {
|
|
123
|
+
export const ValidateMatrix4 = makeValidate(AssertMatrix4);
|
|
124
|
+
export function AssertMatrixSquare(matrix) {
|
|
585
125
|
try {
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
126
|
+
MATRIX_SQUARE_SCHEMA.parse(matrix);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
130
|
+
throw new MatrixError(`Invalid square matrix: ${message}`, {
|
|
131
|
+
cause: error instanceof Error ? error : undefined,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
export const ValidateMatrixSquare = makeValidate(AssertMatrixSquare);
|
|
136
|
+
export function AssertMatricesCompatible(...matrices) {
|
|
137
|
+
for (const matrix of matrices) {
|
|
138
|
+
AssertMatrix(matrix);
|
|
139
|
+
}
|
|
140
|
+
// Validate that all matrices have the same dimensions
|
|
141
|
+
const [firstRows, firstCols] = MatrixSize(matrices[0]);
|
|
142
|
+
for (let i = 1; i < matrices.length; i++) {
|
|
143
|
+
const [rows, cols] = MatrixSize(matrices[i]);
|
|
144
|
+
if (rows !== firstRows || cols !== firstCols) {
|
|
145
|
+
throw new MatrixError(`All matrices must have the same dimensions. Expected [${firstRows}, ${firstCols}], but got [${rows}, ${cols}] at index ${i}`);
|
|
146
|
+
}
|
|
591
147
|
}
|
|
592
148
|
}
|
|
593
149
|
//# sourceMappingURL=asserts.js.map
|