@pawells/math-extended 1.1.1 → 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/angles.d.ts +28 -18
- package/build/angles.d.ts.map +1 -1
- package/build/angles.js +28 -18
- package/build/angles.js.map +1 -1
- package/build/clamp.d.ts +2 -0
- package/build/clamp.d.ts.map +1 -1
- package/build/clamp.js +2 -0
- package/build/clamp.js.map +1 -1
- 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 +62 -13
- package/build/interpolation.d.ts.map +1 -1
- package/build/interpolation.js +66 -23
- package/build/interpolation.js.map +1 -1
- package/build/matrices/arithmetic.d.ts +65 -76
- package/build/matrices/arithmetic.d.ts.map +1 -1
- package/build/matrices/arithmetic.js +65 -107
- package/build/matrices/arithmetic.js.map +1 -1
- package/build/matrices/asserts.d.ts +26 -273
- package/build/matrices/asserts.d.ts.map +1 -1
- package/build/matrices/asserts.js +106 -350
- package/build/matrices/asserts.js.map +1 -1
- package/build/matrices/core.d.ts +150 -79
- package/build/matrices/core.d.ts.map +1 -1
- package/build/matrices/core.js +128 -104
- package/build/matrices/core.js.map +1 -1
- package/build/matrices/decompositions.d.ts +41 -44
- package/build/matrices/decompositions.d.ts.map +1 -1
- package/build/matrices/decompositions.js +51 -94
- package/build/matrices/decompositions.js.map +1 -1
- package/build/matrices/index.d.ts +2 -2
- package/build/matrices/index.d.ts.map +1 -1
- package/build/matrices/index.js +2 -2
- package/build/matrices/index.js.map +1 -1
- package/build/matrices/linear-algebra.d.ts +56 -19
- package/build/matrices/linear-algebra.d.ts.map +1 -1
- package/build/matrices/linear-algebra.js +80 -36
- package/build/matrices/linear-algebra.js.map +1 -1
- package/build/matrices/normalization.d.ts +36 -15
- 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 +39 -52
- package/build/matrices/transformations.d.ts.map +1 -1
- package/build/matrices/transformations.js +8 -11
- package/build/matrices/transformations.js.map +1 -1
- package/build/matrices/types.d.ts +39 -67
- 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 +114 -15
- package/build/quaternions/asserts.d.ts.map +1 -1
- package/build/quaternions/asserts.js +189 -51
- package/build/quaternions/asserts.js.map +1 -1
- package/build/quaternions/conversions.d.ts +18 -8
- package/build/quaternions/conversions.d.ts.map +1 -1
- package/build/quaternions/conversions.js +14 -4
- package/build/quaternions/conversions.js.map +1 -1
- package/build/quaternions/core.d.ts +31 -2
- package/build/quaternions/core.d.ts.map +1 -1
- package/build/quaternions/core.js +32 -4
- package/build/quaternions/core.js.map +1 -1
- package/build/quaternions/index.d.ts +2 -2
- package/build/quaternions/index.d.ts.map +1 -1
- package/build/quaternions/index.js +3 -2
- package/build/quaternions/index.js.map +1 -1
- package/build/quaternions/interpolation.d.ts +7 -1
- package/build/quaternions/interpolation.d.ts.map +1 -1
- package/build/quaternions/interpolation.js +12 -6
- package/build/quaternions/interpolation.js.map +1 -1
- package/build/quaternions/predefined.d.ts +7 -1
- package/build/quaternions/predefined.d.ts.map +1 -1
- package/build/quaternions/predefined.js +6 -0
- package/build/quaternions/predefined.js.map +1 -1
- package/build/quaternions/types.d.ts +24 -13
- 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 +33 -99
- package/build/vectors/asserts.d.ts.map +1 -1
- package/build/vectors/asserts.js +145 -181
- package/build/vectors/asserts.js.map +1 -1
- package/build/vectors/core.d.ts +76 -44
- package/build/vectors/core.d.ts.map +1 -1
- package/build/vectors/core.js +128 -119
- package/build/vectors/core.js.map +1 -1
- package/build/vectors/index.d.ts.map +1 -1
- package/build/vectors/index.js +1 -0
- package/build/vectors/index.js.map +1 -1
- package/build/vectors/interpolation.d.ts +13 -1
- package/build/vectors/interpolation.d.ts.map +1 -1
- package/build/vectors/interpolation.js +48 -47
- package/build/vectors/interpolation.js.map +1 -1
- package/build/vectors/predefined.d.ts +73 -25
- package/build/vectors/predefined.d.ts.map +1 -1
- package/build/vectors/predefined.js +62 -18
- 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 +4 -3
|
@@ -1,40 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { MatrixSize } from '../index.js';
|
|
2
|
+
import { MATRIX1_SCHEMA, MATRIX2_SCHEMA, MATRIX3_SCHEMA, MATRIX4_SCHEMA, MATRIX_SCHEMA, MATRIX_SQUARE_SCHEMA } from './types.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
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
|
-
* try {
|
|
15
|
-
* AssertMatrix(invalidMatrix);
|
|
16
|
-
* } catch (error) {
|
|
17
|
-
* if (error instanceof MatrixError) {
|
|
18
|
-
* console.log('Matrix validation failed:', error.message);
|
|
19
|
-
* }
|
|
20
|
-
* }
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export class MatrixError extends Error {
|
|
24
|
-
/**
|
|
25
|
-
* Creates a new MatrixError instance.
|
|
26
|
-
*
|
|
27
|
-
* @param message - Optional error message describing the validation failure
|
|
28
|
-
*/
|
|
29
|
-
constructor(message) {
|
|
30
|
-
super(message);
|
|
31
|
-
this.name = 'MatrixError';
|
|
32
|
-
// Maintains proper prototype chain for instanceof checks
|
|
33
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Validates that an unknown value is a valid matrix conforming to the IMatrix interface.
|
|
4
|
+
* Validates that an unknown value is a valid matrix conforming to the TMatrix interface.
|
|
38
5
|
*
|
|
39
6
|
* This function performs comprehensive validation of matrix structure including:
|
|
40
7
|
* - Type checking to ensure the value is a proper matrix
|
|
@@ -45,349 +12,138 @@ export class MatrixError extends Error {
|
|
|
45
12
|
* @param matrix - The value to validate as a matrix
|
|
46
13
|
* @param args - Validation configuration options
|
|
47
14
|
* @param exception - Custom exception details if validation fails
|
|
48
|
-
* @throws {
|
|
15
|
+
* @throws {MatrixError} When the matrix doesn't meet the specified criteria
|
|
49
16
|
*
|
|
50
17
|
* @example
|
|
51
18
|
* ```typescript
|
|
19
|
+
* ```typescript
|
|
52
20
|
* // Validate a 3x3 square matrix
|
|
53
21
|
* AssertMatrix(someValue, { square: true, size: 3 });
|
|
54
|
-
*
|
|
55
22
|
* // Validate minimum dimensions
|
|
56
23
|
* AssertMatrix(someValue, { minRows: 2, minColumns: 3 });
|
|
57
|
-
*
|
|
58
24
|
* // Validate exact dimensions
|
|
59
25
|
* AssertMatrix(someValue, { rows: 4, columns: 5 });
|
|
60
26
|
* ```
|
|
27
|
+
* ```
|
|
61
28
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
// Extract matrix dimensions for constraint validation
|
|
81
|
-
const [rows, columns] = MatrixSize(matrix);
|
|
82
|
-
// Reject empty matrices (zero rows or zero columns)
|
|
83
|
-
if (rows === 0 || columns === 0) {
|
|
84
|
-
SetExceptionMessage(exception, 'Matrix must have at least one row and one column');
|
|
85
|
-
ThrowException(exception);
|
|
86
|
-
}
|
|
87
|
-
// Validate exact size constraint when specified as [rows, columns] tuple
|
|
88
|
-
if (Array.isArray(args.size)) {
|
|
89
|
-
const [expectedRows, expectedColumns] = args.size;
|
|
90
|
-
if (rows !== expectedRows) {
|
|
91
|
-
SetExceptionMessage(exception, `Matrix has ${rows} rows, expected exactly ${expectedRows}`);
|
|
92
|
-
ThrowException(exception);
|
|
93
|
-
}
|
|
94
|
-
if (columns !== expectedColumns) {
|
|
95
|
-
SetExceptionMessage(exception, `Matrix has ${columns} columns, expected exactly ${expectedColumns}`);
|
|
96
|
-
ThrowException(exception);
|
|
29
|
+
/**
|
|
30
|
+
* Matrix error class for validation failures and matrix operations.
|
|
31
|
+
* Extends Error to provide detailed error information with optional cause chain.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* throw new MatrixError('Invalid matrix dimensions', { cause: originalError });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
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;
|
|
97
45
|
}
|
|
98
46
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
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
|
|
52
|
+
*/
|
|
53
|
+
function makeValidate(assert) {
|
|
54
|
+
return (value) => {
|
|
55
|
+
try {
|
|
56
|
+
assert(value);
|
|
57
|
+
return true;
|
|
104
58
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
ThrowException(exception);
|
|
59
|
+
catch {
|
|
60
|
+
return false;
|
|
108
61
|
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function AssertMatrix(matrix) {
|
|
65
|
+
try {
|
|
66
|
+
MATRIX_SCHEMA.parse(matrix);
|
|
109
67
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
+
});
|
|
114
73
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
74
|
+
}
|
|
75
|
+
export const ValidateMatrix = makeValidate(AssertMatrix);
|
|
76
|
+
export function AssertMatrix1(matrix) {
|
|
77
|
+
try {
|
|
78
|
+
MATRIX1_SCHEMA.parse(matrix);
|
|
119
79
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
+
});
|
|
124
85
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
86
|
+
}
|
|
87
|
+
export const ValidateMatrix1 = makeValidate(AssertMatrix1);
|
|
88
|
+
export function AssertMatrix2(matrix) {
|
|
89
|
+
try {
|
|
90
|
+
MATRIX2_SCHEMA.parse(matrix);
|
|
129
91
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
+
});
|
|
134
97
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
98
|
+
}
|
|
99
|
+
export const ValidateMatrix2 = makeValidate(AssertMatrix2);
|
|
100
|
+
export function AssertMatrix3(matrix) {
|
|
101
|
+
try {
|
|
102
|
+
MATRIX3_SCHEMA.parse(matrix);
|
|
139
103
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
+
});
|
|
144
109
|
}
|
|
145
110
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
* which should be an array containing only numeric values.
|
|
151
|
-
*
|
|
152
|
-
* @param row - The value to validate as a matrix row
|
|
153
|
-
* @param exception - Custom exception details (optionally includes `rowIndex` for richer messages)
|
|
154
|
-
* @throws {MatrixError} When the row is not a valid array of finite numbers
|
|
155
|
-
*
|
|
156
|
-
* @example
|
|
157
|
-
* ```typescript
|
|
158
|
-
* // Validate a matrix row
|
|
159
|
-
* AssertMatrixRow([1, 2, 3, 4]);
|
|
160
|
-
*
|
|
161
|
-
* // Validate with row index in exception for better error messages
|
|
162
|
-
* AssertMatrixRow([1, 2, 3, 4], { rowIndex: 0 });
|
|
163
|
-
*
|
|
164
|
-
* // This would throw MatrixError
|
|
165
|
-
* AssertMatrixRow([1, '2', 3]); // Contains non-numeric value
|
|
166
|
-
* ```
|
|
167
|
-
*/
|
|
168
|
-
export function AssertMatrixRow(row, exception = {}) {
|
|
169
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
170
|
-
SetExceptionClass(exception, MatrixError);
|
|
171
|
-
// First validation: ensure the input is an array
|
|
172
|
-
if (!Array.isArray(row)) {
|
|
173
|
-
const rowInfo = exception.rowIndex !== undefined ? ` at row ${exception.rowIndex}` : '';
|
|
174
|
-
SetExceptionMessage(exception, `Matrix row${rowInfo} must be an array`);
|
|
175
|
-
ThrowException(exception);
|
|
111
|
+
export const ValidateMatrix3 = makeValidate(AssertMatrix3);
|
|
112
|
+
export function AssertMatrix4(matrix) {
|
|
113
|
+
try {
|
|
114
|
+
MATRIX4_SCHEMA.parse(matrix);
|
|
176
115
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (typeof rowArray[i] !== 'number' || !isFinite(rowArray[i])) {
|
|
183
|
-
const rowInfo = exception.rowIndex !== undefined ? ` at row ${exception.rowIndex}` : '';
|
|
184
|
-
SetExceptionMessage(exception, `Matrix row${rowInfo} element at index ${i} is not a finite number`);
|
|
185
|
-
ThrowException(exception);
|
|
186
|
-
}
|
|
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
|
+
});
|
|
187
121
|
}
|
|
188
122
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
* which should be a finite number (not NaN, Infinity, or -Infinity).
|
|
194
|
-
*
|
|
195
|
-
* @param value - The value to validate as a matrix element
|
|
196
|
-
* @param exception - Custom exception details (optionally includes `rowIndex` and `columnIndex` for richer messages)
|
|
197
|
-
* @throws {MatrixError} When the value is not a finite number
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* ```typescript
|
|
201
|
-
* // Validate a matrix element
|
|
202
|
-
* AssertMatrixValue(42);
|
|
203
|
-
*
|
|
204
|
-
* // Validate with position information for better error messages
|
|
205
|
-
* AssertMatrixValue(3.14, { rowIndex: 0, columnIndex: 1 });
|
|
206
|
-
*
|
|
207
|
-
* // These would throw MatrixError
|
|
208
|
-
* AssertMatrixValue(NaN); // Not a number
|
|
209
|
-
* AssertMatrixValue(Infinity); // Not a finite number
|
|
210
|
-
* AssertMatrixValue('5'); // Not a number type
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
export function AssertMatrixValue(value, exception = {}) {
|
|
214
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
215
|
-
SetExceptionClass(exception, MatrixError);
|
|
216
|
-
// Build position information for error messages when row and column indices are available
|
|
217
|
-
// This provides more context about where the validation failure occurred
|
|
218
|
-
const position = exception.rowIndex !== undefined && exception.columnIndex !== undefined ? ` at row ${exception.rowIndex}, column ${exception.columnIndex}` : '';
|
|
219
|
-
SetExceptionMessage(exception, `Matrix value${position} must be a finite number`);
|
|
220
|
-
// Delegate to the general number assertion which handles finite number validation
|
|
221
|
-
AssertNumber(value, { finite: true }, exception);
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Validates compatibility between two matrices for mathematical operations.
|
|
225
|
-
*
|
|
226
|
-
* This function checks that two matrices are compatible for operations like
|
|
227
|
-
* multiplication, addition, or other matrix operations that require specific
|
|
228
|
-
* dimensional relationships.
|
|
229
|
-
*
|
|
230
|
-
* @param a - The first matrix to validate
|
|
231
|
-
* @param b - The second matrix to validate
|
|
232
|
-
* @param args - Validation configuration options
|
|
233
|
-
* @param exception - Custom exception details if validation fails
|
|
234
|
-
* @throws {IAssertException} When the matrices are not compatible
|
|
235
|
-
*
|
|
236
|
-
* @example
|
|
237
|
-
* ```typescript
|
|
238
|
-
* // Validate matrices for multiplication (A columns must equal B rows)
|
|
239
|
-
* AssertMatrices(matrixA, matrixB);
|
|
240
|
-
*
|
|
241
|
-
* // Allow transposed compatibility
|
|
242
|
-
* AssertMatrices(matrixA, matrixB, { transposition: true });
|
|
243
|
-
* ```
|
|
244
|
-
*/
|
|
245
|
-
export function AssertMatrices(a, b, args = {}, exception = {}) {
|
|
246
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
247
|
-
SetExceptionClass(exception, MatrixError);
|
|
248
|
-
// First validation: ensure both inputs are valid matrices
|
|
249
|
-
AssertMatrix(a, {}, exception);
|
|
250
|
-
AssertMatrix(b, {}, exception);
|
|
251
|
-
// Safe cast to IMatrix since we've validated both inputs
|
|
252
|
-
const matrixA = a;
|
|
253
|
-
const matrixB = b;
|
|
254
|
-
// Extract dimensions from both matrices for compatibility checking
|
|
255
|
-
const rowsA = matrixA.length;
|
|
256
|
-
const columnsA = rowsA > 0 && matrixA[0] ? matrixA[0].length : 0;
|
|
257
|
-
const rowsB = matrixB.length;
|
|
258
|
-
const columnsB = rowsB > 0 && matrixB[0] ? matrixB[0].length : 0;
|
|
259
|
-
// Check compatibility based on whether transposition is allowed
|
|
260
|
-
if (args.transposition === true) {
|
|
261
|
-
// With transposition allowed, check all possible multiplication combinations:
|
|
262
|
-
// - A × B (standard): columns of A must equal rows of B
|
|
263
|
-
// - A × B^T (B transposed): columns of A must equal columns of B
|
|
264
|
-
// - A^T × B (A transposed): rows of A must equal rows of B
|
|
265
|
-
// - A^T × B^T (both transposed): rows of A must equal columns of B
|
|
266
|
-
const canMultiplyAB = columnsA === rowsB;
|
|
267
|
-
const canMultiplyABT = columnsA === columnsB;
|
|
268
|
-
const canMultiplyATB = rowsA === rowsB;
|
|
269
|
-
const canMultiplyATBT = rowsA === columnsB;
|
|
270
|
-
if (!canMultiplyAB && !canMultiplyABT && !canMultiplyATB && !canMultiplyATBT) {
|
|
271
|
-
SetExceptionMessage(exception, `Matrices are not compatible for multiplication even with transposition. Matrix A is ${rowsA}×${columnsA}, Matrix B is ${rowsB}×${columnsB}`);
|
|
272
|
-
ThrowException(exception);
|
|
273
|
-
}
|
|
123
|
+
export const ValidateMatrix4 = makeValidate(AssertMatrix4);
|
|
124
|
+
export function AssertMatrixSquare(matrix) {
|
|
125
|
+
try {
|
|
126
|
+
MATRIX_SQUARE_SCHEMA.parse(matrix);
|
|
274
127
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
ThrowException(exception);
|
|
281
|
-
}
|
|
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
|
+
});
|
|
282
133
|
}
|
|
283
134
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
* AssertMatrix1([[5]]);
|
|
298
|
-
*
|
|
299
|
-
* // This would throw an exception
|
|
300
|
-
* AssertMatrix1([[1, 2]]); // Too many columns
|
|
301
|
-
* ```
|
|
302
|
-
*/
|
|
303
|
-
export function AssertMatrix1(matrix, exception = {}) {
|
|
304
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
305
|
-
SetExceptionClass(exception, MatrixError);
|
|
306
|
-
// Delegate to the general matrix assertion with 1x1 square matrix constraints
|
|
307
|
-
// This ensures the matrix is exactly 1 row by 1 column
|
|
308
|
-
AssertMatrix(matrix, { square: true, size: 1 }, exception);
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* Validates that an unknown value is a valid 2x2 matrix.
|
|
312
|
-
*
|
|
313
|
-
* This function ensures that the provided value is a proper 2x2 matrix,
|
|
314
|
-
* which should be a 2D array with exactly 2 rows and 2 columns containing numbers.
|
|
315
|
-
*
|
|
316
|
-
* @param matrix - The value to validate as a 2x2 matrix
|
|
317
|
-
* @param exception - Custom exception details if validation fails
|
|
318
|
-
* @throws {IAssertException} When the matrix is not a valid 2x2 matrix
|
|
319
|
-
*
|
|
320
|
-
* @example
|
|
321
|
-
* ```typescript
|
|
322
|
-
* // Validate a 2x2 matrix
|
|
323
|
-
* AssertMatrix2([[1, 2], [3, 4]]);
|
|
324
|
-
*
|
|
325
|
-
* // This would throw an exception
|
|
326
|
-
* AssertMatrix2([[1, 2, 3], [4, 5, 6]]); // Too many columns
|
|
327
|
-
* ```
|
|
328
|
-
*/
|
|
329
|
-
export function AssertMatrix2(matrix, exception = {}) {
|
|
330
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
331
|
-
SetExceptionClass(exception, MatrixError);
|
|
332
|
-
// Delegate to the general matrix assertion with 2x2 square matrix constraints
|
|
333
|
-
// This ensures the matrix is exactly 2 rows by 2 columns
|
|
334
|
-
AssertMatrix(matrix, { square: true, size: 2 }, exception);
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Validates that an unknown value is a valid 3x3 matrix.
|
|
338
|
-
*
|
|
339
|
-
* This function ensures that the provided value is a proper 3x3 matrix,
|
|
340
|
-
* which should be a 2D array with exactly 3 rows and 3 columns containing numbers.
|
|
341
|
-
*
|
|
342
|
-
* @param matrix - The value to validate as a 3x3 matrix
|
|
343
|
-
* @param exception - Custom exception details if validation fails
|
|
344
|
-
* @throws {IAssertException} When the matrix is not a valid 3x3 matrix
|
|
345
|
-
*
|
|
346
|
-
* @example
|
|
347
|
-
* ```typescript
|
|
348
|
-
* // Validate a 3x3 matrix
|
|
349
|
-
* AssertMatrix3([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
|
|
350
|
-
*
|
|
351
|
-
* // This would throw an exception
|
|
352
|
-
* AssertMatrix3([[1, 2], [3, 4]]); // Too few rows and columns
|
|
353
|
-
* ```
|
|
354
|
-
*/
|
|
355
|
-
export function AssertMatrix3(matrix, exception = {}) {
|
|
356
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
357
|
-
SetExceptionClass(exception, MatrixError);
|
|
358
|
-
// Delegate to the general matrix assertion with 3x3 square matrix constraints
|
|
359
|
-
// This ensures the matrix is exactly 3 rows by 3 columns
|
|
360
|
-
AssertMatrix(matrix, { square: true, size: 3 }, exception);
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Validates that an unknown value is a valid 4x4 matrix.
|
|
364
|
-
*
|
|
365
|
-
* This function ensures that the provided value is a proper 4x4 matrix,
|
|
366
|
-
* which should be a 2D array with exactly 4 rows and 4 columns containing numbers.
|
|
367
|
-
*
|
|
368
|
-
* @param matrix - The value to validate as a 4x4 matrix
|
|
369
|
-
* @param exception - Custom exception details if validation fails
|
|
370
|
-
* @throws {IAssertException} When the matrix is not a valid 4x4 matrix
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
* ```typescript
|
|
374
|
-
* // Validate a 4x4 matrix
|
|
375
|
-
* AssertMatrix4([
|
|
376
|
-
* [1, 2, 3, 4],
|
|
377
|
-
* [5, 6, 7, 8],
|
|
378
|
-
* [9, 10, 11, 12],
|
|
379
|
-
* [13, 14, 15, 16]
|
|
380
|
-
* ]);
|
|
381
|
-
*
|
|
382
|
-
* // This would throw an exception
|
|
383
|
-
* AssertMatrix4([[1, 2], [3, 4]]); // Too few rows and columns
|
|
384
|
-
* ```
|
|
385
|
-
*/
|
|
386
|
-
export function AssertMatrix4(matrix, exception = {}) {
|
|
387
|
-
// Initialize the exception with the default MatrixError class if not provided
|
|
388
|
-
SetExceptionClass(exception, MatrixError);
|
|
389
|
-
// Delegate to the general matrix assertion with 4x4 square matrix constraints
|
|
390
|
-
// This ensures the matrix is exactly 4 rows by 4 columns
|
|
391
|
-
AssertMatrix(matrix, { square: true, size: 4 }, exception);
|
|
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
|
+
}
|
|
147
|
+
}
|
|
392
148
|
}
|
|
393
149
|
//# sourceMappingURL=asserts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asserts.js","sourceRoot":"","sources":["../../src/matrices/asserts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"asserts.js","sourceRoot":"","sources":["../../src/matrices/asserts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,oBAAoB,EAAkE,MAAM,YAAY,CAAC;AAEjM;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH;;;;;;;;GAQG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IACrB,IAAI,GAAW,cAAc,CAAC;IAE9C,YAAY,OAAe,EAAE,OAA6B;QACzD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,IAAI,OAAO,EAAE,KAAK,EAAE,CAAC;YACpB,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC5B,CAAC;IACF,CAAC;CACD;AAED;;;;GAIG;AACH,SAAS,YAAY,CACpB,MAA8C;IAE9C,OAAO,CAAC,KAAc,EAAc,EAAE;QACrC,IAAI,CAAC;YACJ,MAAM,CAAC,KAAK,CAAC,CAAC;YACd,OAAO,IAAI,CAAC;QACb,CAAC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