@lakuna/umath 1.3.8 → 1.4.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/LICENSE +1 -1
- package/README.md +2 -0
- package/dist/algorithms/approx.d.ts +9 -0
- package/dist/algorithms/approx.d.ts.map +1 -0
- package/dist/algorithms/approx.js +12 -0
- package/dist/algorithms/approx.js.map +1 -0
- package/dist/algorithms/approxRelative.d.ts +9 -0
- package/dist/algorithms/approxRelative.d.ts.map +1 -0
- package/dist/algorithms/approxRelative.js +12 -0
- package/dist/algorithms/approxRelative.js.map +1 -0
- package/dist/algorithms/combinations.d.ts +2 -1
- package/dist/algorithms/combinations.d.ts.map +1 -1
- package/dist/algorithms/combinations.js +2 -1
- package/dist/algorithms/combinations.js.map +1 -1
- package/dist/algorithms/degreesToRadians.d.ts +3 -2
- package/dist/algorithms/degreesToRadians.d.ts.map +1 -1
- package/dist/algorithms/degreesToRadians.js +5 -3
- package/dist/algorithms/degreesToRadians.js.map +1 -1
- package/dist/algorithms/factorial.d.ts +2 -1
- package/dist/algorithms/factorial.d.ts.map +1 -1
- package/dist/algorithms/factorial.js +2 -1
- package/dist/algorithms/factorial.js.map +1 -1
- package/dist/algorithms/fibonacci.d.ts +2 -1
- package/dist/algorithms/fibonacci.d.ts.map +1 -1
- package/dist/algorithms/fibonacci.js +6 -2
- package/dist/algorithms/fibonacci.js.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.d.ts +4 -2
- package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.js +5 -2
- package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
- package/dist/algorithms/hypergeometricPmf.d.ts +3 -2
- package/dist/algorithms/hypergeometricPmf.d.ts.map +1 -1
- package/dist/algorithms/hypergeometricPmf.js +3 -2
- package/dist/algorithms/hypergeometricPmf.js.map +1 -1
- package/dist/algorithms/isPrime.d.ts +2 -1
- package/dist/algorithms/isPrime.d.ts.map +1 -1
- package/dist/algorithms/isPrime.js +2 -1
- package/dist/algorithms/isPrime.js.map +1 -1
- package/dist/algorithms/permutations.d.ts +2 -1
- package/dist/algorithms/permutations.d.ts.map +1 -1
- package/dist/algorithms/permutations.js +2 -1
- package/dist/algorithms/permutations.js.map +1 -1
- package/dist/algorithms/primeFactorization.d.ts +2 -1
- package/dist/algorithms/primeFactorization.d.ts.map +1 -1
- package/dist/algorithms/primeFactorization.js +4 -4
- package/dist/algorithms/primeFactorization.js.map +1 -1
- package/dist/algorithms/radiansToDegrees.d.ts +3 -2
- package/dist/algorithms/radiansToDegrees.d.ts.map +1 -1
- package/dist/algorithms/radiansToDegrees.js +5 -3
- package/dist/algorithms/radiansToDegrees.js.map +1 -1
- package/dist/algorithms/summation.d.ts +2 -1
- package/dist/algorithms/summation.d.ts.map +1 -1
- package/dist/algorithms/summation.js +2 -1
- package/dist/algorithms/summation.js.map +1 -1
- package/dist/linalg/DualQuaternion.d.ts +102 -32
- package/dist/linalg/DualQuaternion.d.ts.map +1 -1
- package/dist/linalg/DualQuaternion.js +249 -275
- package/dist/linalg/DualQuaternion.js.map +1 -1
- package/dist/linalg/Matrix.d.ts +14 -10
- package/dist/linalg/Matrix.d.ts.map +1 -1
- package/dist/linalg/Matrix2.d.ts +74 -51
- package/dist/linalg/Matrix2.d.ts.map +1 -1
- package/dist/linalg/Matrix2.js +96 -169
- package/dist/linalg/Matrix2.js.map +1 -1
- package/dist/linalg/Matrix3.d.ts +122 -72
- package/dist/linalg/Matrix3.d.ts.map +1 -1
- package/dist/linalg/Matrix3.js +205 -358
- package/dist/linalg/Matrix3.js.map +1 -1
- package/dist/linalg/Matrix4.d.ts +267 -150
- package/dist/linalg/Matrix4.d.ts.map +1 -1
- package/dist/linalg/Matrix4.js +553 -864
- package/dist/linalg/Matrix4.js.map +1 -1
- package/dist/linalg/Quaternion.d.ts +252 -34
- package/dist/linalg/Quaternion.d.ts.map +1 -1
- package/dist/linalg/Quaternion.js +436 -166
- package/dist/linalg/Quaternion.js.map +1 -1
- package/dist/linalg/SlowMatrix.d.ts +10 -9
- package/dist/linalg/SlowMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowMatrix.js +10 -9
- package/dist/linalg/SlowMatrix.js.map +1 -1
- package/dist/linalg/SlowSquareMatrix.d.ts +10 -9
- package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowSquareMatrix.js +10 -9
- package/dist/linalg/SlowSquareMatrix.js.map +1 -1
- package/dist/linalg/SquareMatrix.d.ts +6 -5
- package/dist/linalg/SquareMatrix.d.ts.map +1 -1
- package/dist/linalg/Vector.d.ts +8 -4
- package/dist/linalg/Vector.d.ts.map +1 -1
- package/dist/linalg/Vector2.d.ts +82 -31
- package/dist/linalg/Vector2.d.ts.map +1 -1
- package/dist/linalg/Vector2.js +112 -154
- package/dist/linalg/Vector2.js.map +1 -1
- package/dist/linalg/Vector3.d.ts +93 -41
- package/dist/linalg/Vector3.d.ts.map +1 -1
- package/dist/linalg/Vector3.js +171 -282
- package/dist/linalg/Vector3.js.map +1 -1
- package/dist/linalg/Vector4.d.ts +71 -21
- package/dist/linalg/Vector4.d.ts.map +1 -1
- package/dist/linalg/Vector4.js +121 -195
- package/dist/linalg/Vector4.js.map +1 -1
- package/dist/types/AxisAngle.d.ts +4 -1
- package/dist/types/AxisAngle.d.ts.map +1 -1
- package/dist/types/FieldOfView.d.ts +4 -1
- package/dist/types/FieldOfView.d.ts.map +1 -1
- package/dist/utility/BigNumber.d.ts +4 -1
- package/dist/utility/BigNumber.d.ts.map +1 -1
- package/dist/utility/BigNumber.js +4 -1
- package/dist/utility/BigNumber.js.map +1 -1
- package/dist/utility/MagnitudeError.d.ts +4 -1
- package/dist/utility/MagnitudeError.d.ts.map +1 -1
- package/dist/utility/MagnitudeError.js +4 -1
- package/dist/utility/MagnitudeError.js.map +1 -1
- package/dist/utility/MatrixSizeError.d.ts +4 -1
- package/dist/utility/MatrixSizeError.d.ts.map +1 -1
- package/dist/utility/MatrixSizeError.js +4 -1
- package/dist/utility/MatrixSizeError.js.map +1 -1
- package/dist/utility/PartialMatrixError.d.ts +4 -1
- package/dist/utility/PartialMatrixError.d.ts.map +1 -1
- package/dist/utility/PartialMatrixError.js +4 -1
- package/dist/utility/PartialMatrixError.js.map +1 -1
- package/dist/utility/SingularMatrixError.d.ts +2 -1
- package/dist/utility/SingularMatrixError.d.ts.map +1 -1
- package/dist/utility/SingularMatrixError.js +2 -1
- package/dist/utility/SingularMatrixError.js.map +1 -1
- package/dist/utility/epsilon.d.ts +4 -1
- package/dist/utility/epsilon.d.ts.map +1 -1
- package/dist/utility/epsilon.js +4 -1
- package/dist/utility/epsilon.js.map +1 -1
- package/package.json +14 -18
- package/src/algorithms/approx.ts +12 -0
- package/src/algorithms/approxRelative.ts +12 -0
- package/src/algorithms/combinations.ts +2 -1
- package/src/algorithms/degreesToRadians.ts +6 -3
- package/src/algorithms/factorial.ts +3 -1
- package/src/algorithms/fibonacci.ts +7 -2
- package/src/algorithms/greatestCommonDivisor.ts +9 -4
- package/src/algorithms/hypergeometricPmf.ts +3 -2
- package/src/algorithms/isPrime.ts +2 -1
- package/src/algorithms/permutations.ts +2 -1
- package/src/algorithms/primeFactorization.ts +5 -3
- package/src/algorithms/radiansToDegrees.ts +6 -3
- package/src/algorithms/summation.ts +2 -1
- package/src/linalg/DualQuaternion.ts +430 -292
- package/src/linalg/Matrix.ts +14 -10
- package/src/linalg/Matrix2.ts +143 -185
- package/src/linalg/Matrix3.ts +421 -385
- package/src/linalg/Matrix4.ts +1147 -952
- package/src/linalg/Quaternion.ts +706 -188
- package/src/linalg/SlowMatrix.ts +10 -9
- package/src/linalg/SlowSquareMatrix.ts +10 -9
- package/src/linalg/SquareMatrix.ts +6 -5
- package/src/linalg/Vector.ts +8 -4
- package/src/linalg/Vector2.ts +146 -173
- package/src/linalg/Vector3.ts +293 -326
- package/src/linalg/Vector4.ts +227 -215
- package/src/types/AxisAngle.ts +4 -1
- package/src/types/FieldOfView.ts +4 -1
- package/src/utility/BigNumber.ts +6 -3
- package/src/utility/MagnitudeError.ts +4 -1
- package/src/utility/MatrixSizeError.ts +4 -1
- package/src/utility/PartialMatrixError.ts +4 -1
- package/src/utility/SingularMatrixError.ts +2 -1
- package/src/utility/epsilon.ts +4 -1
package/src/linalg/Matrix2.ts
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
add as vector4Add,
|
|
3
|
+
copy as vector4Copy,
|
|
4
|
+
exactEquals as vector4ExactEquals,
|
|
5
|
+
fromValues as vector4FromValues,
|
|
6
|
+
getMagnitude as vector4GetMagnitude,
|
|
7
|
+
scale as vector4Scale,
|
|
8
|
+
scaleAndAdd as vector4ScaleAndAdd,
|
|
9
|
+
subtract as vector4Subtract
|
|
10
|
+
} from "./Vector4.js";
|
|
1
11
|
import type { MatrixLike } from "./Matrix.js";
|
|
2
12
|
import SingularMatrixError from "../utility/SingularMatrixError.js";
|
|
3
13
|
import type SquareMatrix from "./SquareMatrix.js";
|
|
4
14
|
import type { Vector2Like } from "./Vector2.js";
|
|
5
|
-
import
|
|
15
|
+
import approxRelative from "../algorithms/approxRelative.js";
|
|
6
16
|
|
|
7
17
|
/**
|
|
8
18
|
* Numbers arranged into two columns and two rows.
|
|
9
|
-
* @see
|
|
19
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_(mathematics) | Matrix}
|
|
20
|
+
* @public
|
|
10
21
|
*/
|
|
11
22
|
export interface Matrix2Like extends MatrixLike {
|
|
12
23
|
/** The value in the first column and first row. */
|
|
@@ -23,29 +34,45 @@ export interface Matrix2Like extends MatrixLike {
|
|
|
23
34
|
}
|
|
24
35
|
|
|
25
36
|
/**
|
|
26
|
-
* Create a
|
|
27
|
-
* @returns A
|
|
37
|
+
* Create a two-by-two matrix-like object.
|
|
38
|
+
* @returns A two-by-two matrix-like object.
|
|
39
|
+
* @public
|
|
28
40
|
*/
|
|
29
|
-
export const createMatrix2Like = () => {
|
|
41
|
+
export const createMatrix2Like = (): Float32Array & Matrix2Like => {
|
|
30
42
|
return new Float32Array(4) as Float32Array & Matrix2Like;
|
|
31
43
|
};
|
|
32
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Create a two-by-two matrix with the given values.
|
|
47
|
+
* @param c0r0 - The value in the first column and first row.
|
|
48
|
+
* @param c0r1 - The value in the first column and second row.
|
|
49
|
+
* @param c1r0 - The value in the second column and first row.
|
|
50
|
+
* @param c1r1 - The value in the second column and second row.
|
|
51
|
+
* @param out - The matrix to store the result in.
|
|
52
|
+
* @returns The matrix.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export const fromValues: <T extends Matrix2Like>(
|
|
56
|
+
c0r0: number,
|
|
57
|
+
c0r1: number,
|
|
58
|
+
c1r0: number,
|
|
59
|
+
c1r1: number,
|
|
60
|
+
out: T
|
|
61
|
+
) => T = vector4FromValues;
|
|
62
|
+
|
|
33
63
|
/**
|
|
34
64
|
* Create a transformation matrix that represents a rotation by the given angle around the Z-axis.
|
|
35
65
|
* @param r - The angle in radians.
|
|
36
66
|
* @param out - The matrix to store the result in.
|
|
37
67
|
* @returns The transformation matrix.
|
|
38
|
-
* @see
|
|
68
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
69
|
+
* @public
|
|
39
70
|
*/
|
|
40
71
|
export const fromRotation = <T extends Matrix2Like>(r: number, out: T): T => {
|
|
41
72
|
const s = Math.sin(r);
|
|
42
73
|
const c = Math.cos(r);
|
|
43
74
|
|
|
44
|
-
|
|
45
|
-
out[1] = s;
|
|
46
|
-
out[2] = -s;
|
|
47
|
-
out[3] = c;
|
|
48
|
-
return out;
|
|
75
|
+
return fromValues(c, s, -s, c, out);
|
|
49
76
|
};
|
|
50
77
|
|
|
51
78
|
/**
|
|
@@ -53,76 +80,36 @@ export const fromRotation = <T extends Matrix2Like>(r: number, out: T): T => {
|
|
|
53
80
|
* @param v - The scaling vector.
|
|
54
81
|
* @param out - The matrix to store the result in.
|
|
55
82
|
* @returns The transformation matrix.
|
|
56
|
-
* @see
|
|
83
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
84
|
+
* @public
|
|
57
85
|
*/
|
|
58
86
|
export const fromScaling = <T extends Matrix2Like>(
|
|
59
87
|
vector: Vector2Like,
|
|
60
88
|
out: T
|
|
61
|
-
): T =>
|
|
62
|
-
out[0] = vector[0];
|
|
63
|
-
out[1] = 0;
|
|
64
|
-
out[2] = 0;
|
|
65
|
-
out[3] = vector[1];
|
|
66
|
-
return out;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Create a two-by-two matrix with the given values.
|
|
71
|
-
* @param c0r0 - The value in the first column and first row.
|
|
72
|
-
* @param c0r1 - The value in the first column and second row.
|
|
73
|
-
* @param c1r0 - The value in the second column and first row.
|
|
74
|
-
* @param c1r1 - The value in the second column and second row.
|
|
75
|
-
* @param out - The matrix to store the result in.
|
|
76
|
-
* @returns The matrix.
|
|
77
|
-
*/
|
|
78
|
-
export const fromValues = <T extends Matrix2Like>(
|
|
79
|
-
c0r0: number,
|
|
80
|
-
c0r1: number,
|
|
81
|
-
c1r0: number,
|
|
82
|
-
c1r1: number,
|
|
83
|
-
out: T
|
|
84
|
-
): T => {
|
|
85
|
-
out[0] = c0r0;
|
|
86
|
-
out[1] = c0r1;
|
|
87
|
-
out[2] = c1r0;
|
|
88
|
-
out[3] = c1r1;
|
|
89
|
-
return out;
|
|
90
|
-
};
|
|
89
|
+
): T => fromValues(vector[0], 0, 0, vector[1], out);
|
|
91
90
|
|
|
92
91
|
/**
|
|
93
92
|
* Determine whether or not two matrices are roughly equivalent.
|
|
94
93
|
* @param a - The first matrix.
|
|
95
94
|
* @param b - The second matrix.
|
|
96
95
|
* @returns Whether or not the matrices are equivalent.
|
|
96
|
+
* @public
|
|
97
97
|
*/
|
|
98
|
-
export const equals = (a: Matrix2Like, b: Matrix2Like): boolean =>
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const b0 = b[0];
|
|
105
|
-
const b1 = b[1];
|
|
106
|
-
const b2 = b[2];
|
|
107
|
-
const b3 = b[3];
|
|
108
|
-
|
|
109
|
-
return (
|
|
110
|
-
Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0)) &&
|
|
111
|
-
Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1)) &&
|
|
112
|
-
Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2)) &&
|
|
113
|
-
Math.abs(a3 - b3) <= epsilon * Math.max(1, Math.abs(a3), Math.abs(b3))
|
|
114
|
-
);
|
|
115
|
-
};
|
|
98
|
+
export const equals = (a: Matrix2Like, b: Matrix2Like): boolean =>
|
|
99
|
+
approxRelative(a[0], b[0]) &&
|
|
100
|
+
approxRelative(a[1], b[1]) &&
|
|
101
|
+
approxRelative(a[2], b[2]) &&
|
|
102
|
+
approxRelative(a[3], b[3]);
|
|
116
103
|
|
|
117
104
|
/**
|
|
118
105
|
* Determine whether or not two matrices are exactly equivalent.
|
|
119
106
|
* @param a - The first matrix.
|
|
120
107
|
* @param b - The second matrix.
|
|
121
108
|
* @returns Whether the matrices are equivalent.
|
|
109
|
+
* @public
|
|
122
110
|
*/
|
|
123
|
-
export const exactEquals
|
|
124
|
-
|
|
125
|
-
};
|
|
111
|
+
export const exactEquals: (a: Matrix2Like, b: Matrix2Like) => boolean =
|
|
112
|
+
vector4ExactEquals;
|
|
126
113
|
|
|
127
114
|
/**
|
|
128
115
|
* Add two matrices.
|
|
@@ -130,70 +117,55 @@ export const exactEquals = (a: Matrix2Like, b: Matrix2Like): boolean => {
|
|
|
130
117
|
* @param b - The addend.
|
|
131
118
|
* @param out - The matrix to store the result in.
|
|
132
119
|
* @returns The sum.
|
|
133
|
-
* @see
|
|
120
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
121
|
+
* @public
|
|
134
122
|
*/
|
|
135
|
-
export const add
|
|
123
|
+
export const add: <T extends Matrix2Like>(
|
|
136
124
|
a: Matrix2Like,
|
|
137
125
|
b: Matrix2Like,
|
|
138
126
|
out: T
|
|
139
|
-
)
|
|
140
|
-
out[0] = a[0] + b[0];
|
|
141
|
-
out[1] = a[1] + b[1];
|
|
142
|
-
out[2] = a[2] + b[2];
|
|
143
|
-
out[3] = a[3] + b[3];
|
|
144
|
-
return out;
|
|
145
|
-
};
|
|
127
|
+
) => T = vector4Add;
|
|
146
128
|
|
|
147
129
|
/**
|
|
148
130
|
* Calculate the adjugate of a matrix.
|
|
149
131
|
* @param matrix - The matrix.
|
|
150
132
|
* @param out - The matrix to store the result in.
|
|
151
133
|
* @returns The adjugate of the matrix.
|
|
152
|
-
* @see
|
|
134
|
+
* @see {@link https://en.wikipedia.org/wiki/Adjugate_matrix | Adjugate matrix}
|
|
135
|
+
* @public
|
|
153
136
|
*/
|
|
154
137
|
export const adjoint = <T extends Matrix2Like>(
|
|
155
138
|
matrix: Matrix2Like,
|
|
156
139
|
out: T
|
|
157
|
-
): T =>
|
|
158
|
-
const a0 = matrix[0];
|
|
159
|
-
out[0] = matrix[3];
|
|
160
|
-
out[1] = -matrix[1];
|
|
161
|
-
out[2] = -matrix[2];
|
|
162
|
-
out[3] = a0;
|
|
163
|
-
return out;
|
|
164
|
-
};
|
|
140
|
+
): T => fromValues(matrix[3], -matrix[1], -matrix[2], matrix[0], out);
|
|
165
141
|
|
|
166
142
|
/**
|
|
167
143
|
* Copy the values from one matrix into another.
|
|
168
144
|
* @param matrix - The matrix to copy.
|
|
169
145
|
* @param out - The matrix to store the result in.
|
|
170
146
|
* @returns The copy matrix.
|
|
147
|
+
* @public
|
|
171
148
|
*/
|
|
172
|
-
export const copy
|
|
173
|
-
|
|
174
|
-
out[1] = matrix[1];
|
|
175
|
-
out[2] = matrix[2];
|
|
176
|
-
out[3] = matrix[3];
|
|
177
|
-
return out;
|
|
178
|
-
};
|
|
149
|
+
export const copy: <T extends Matrix2Like>(matrix: Matrix2Like, out: T) => T =
|
|
150
|
+
vector4Copy;
|
|
179
151
|
|
|
180
152
|
/**
|
|
181
153
|
* Calculate the Frobenius norm of a matrix.
|
|
182
154
|
* @param matrix - The matrix.
|
|
183
155
|
* @returns The Frobenius norm.
|
|
184
|
-
* @see
|
|
156
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_norm | Matrix norm}
|
|
157
|
+
* @public
|
|
185
158
|
*/
|
|
186
|
-
export const frob
|
|
187
|
-
return Math.hypot(matrix[0], matrix[1], matrix[2], matrix[3]);
|
|
188
|
-
};
|
|
159
|
+
export const frob: (matrix: Matrix2Like) => number = vector4GetMagnitude;
|
|
189
160
|
|
|
190
161
|
/**
|
|
191
162
|
* Multiply one matrix by another.
|
|
192
|
-
* @param a - The
|
|
193
|
-
* @param b - The
|
|
163
|
+
* @param a - The multiplier.
|
|
164
|
+
* @param b - The multiplicand.
|
|
194
165
|
* @param out - The matrix to store the result in.
|
|
195
166
|
* @returns The product.
|
|
196
|
-
* @see
|
|
167
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
168
|
+
* @public
|
|
197
169
|
*/
|
|
198
170
|
export const multiply = <T extends Matrix2Like>(
|
|
199
171
|
a: Matrix2Like,
|
|
@@ -210,32 +182,29 @@ export const multiply = <T extends Matrix2Like>(
|
|
|
210
182
|
const b2 = b[2];
|
|
211
183
|
const b3 = b[3];
|
|
212
184
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
185
|
+
return fromValues(
|
|
186
|
+
a0 * b0 + a2 * b1,
|
|
187
|
+
a1 * b0 + a3 * b1,
|
|
188
|
+
a0 * b2 + a2 * b3,
|
|
189
|
+
a1 * b2 + a3 * b3,
|
|
190
|
+
out
|
|
191
|
+
);
|
|
218
192
|
};
|
|
219
193
|
|
|
220
194
|
/**
|
|
221
195
|
* Multiply a matrix by a scalar value.
|
|
222
|
-
* @param matrix - The
|
|
223
|
-
* @param scalar - The
|
|
196
|
+
* @param matrix - The multiplier.
|
|
197
|
+
* @param scalar - The multiplicand.
|
|
224
198
|
* @param out - The matrix to store the result in.
|
|
225
199
|
* @returns The product.
|
|
226
|
-
* @see
|
|
200
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
201
|
+
* @public
|
|
227
202
|
*/
|
|
228
|
-
export const multiplyScalar
|
|
203
|
+
export const multiplyScalar: <T extends Matrix2Like>(
|
|
229
204
|
matrix: Matrix2Like,
|
|
230
205
|
scalar: number,
|
|
231
206
|
out: T
|
|
232
|
-
)
|
|
233
|
-
out[0] = matrix[0] * scalar;
|
|
234
|
-
out[1] = matrix[1] * scalar;
|
|
235
|
-
out[2] = matrix[2] * scalar;
|
|
236
|
-
out[3] = matrix[3] * scalar;
|
|
237
|
-
return out;
|
|
238
|
-
};
|
|
207
|
+
) => T = vector4Scale;
|
|
239
208
|
|
|
240
209
|
/**
|
|
241
210
|
* Add a matrix to another after multiplying the other by a scalar.
|
|
@@ -244,21 +213,16 @@ export const multiplyScalar = <T extends Matrix2Like>(
|
|
|
244
213
|
* @param scalar - The multiplier.
|
|
245
214
|
* @param out - The matrix to store the result in.
|
|
246
215
|
* @returns The sum.
|
|
247
|
-
* @see
|
|
248
|
-
* @see
|
|
216
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
217
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
218
|
+
* @public
|
|
249
219
|
*/
|
|
250
|
-
export const multiplyScalarAndAdd
|
|
220
|
+
export const multiplyScalarAndAdd: <T extends Matrix2Like>(
|
|
251
221
|
a: Matrix2Like,
|
|
252
222
|
b: Matrix2Like,
|
|
253
223
|
scalar: number,
|
|
254
224
|
out: T
|
|
255
|
-
)
|
|
256
|
-
out[0] = a[0] + b[0] * scalar;
|
|
257
|
-
out[1] = a[1] + b[1] * scalar;
|
|
258
|
-
out[2] = a[2] + b[2] * scalar;
|
|
259
|
-
out[3] = a[3] + b[3] * scalar;
|
|
260
|
-
return out;
|
|
261
|
-
};
|
|
225
|
+
) => T = vector4ScaleAndAdd;
|
|
262
226
|
|
|
263
227
|
/**
|
|
264
228
|
* Subtract one matrix from another.
|
|
@@ -266,26 +230,22 @@ export const multiplyScalarAndAdd = <T extends Matrix2Like>(
|
|
|
266
230
|
* @param b - The subtrahend.
|
|
267
231
|
* @param out - The matrix to store the result in.
|
|
268
232
|
* @returns The difference.
|
|
269
|
-
* @see
|
|
233
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
234
|
+
* @public
|
|
270
235
|
*/
|
|
271
|
-
export const subtract
|
|
236
|
+
export const subtract: <T extends Matrix2Like>(
|
|
272
237
|
a: Matrix2Like,
|
|
273
238
|
b: Matrix2Like,
|
|
274
239
|
out: T
|
|
275
|
-
)
|
|
276
|
-
out[0] = a[0] - b[0];
|
|
277
|
-
out[1] = a[1] - b[1];
|
|
278
|
-
out[2] = a[2] - b[2];
|
|
279
|
-
out[3] = a[3] - b[3];
|
|
280
|
-
return out;
|
|
281
|
-
};
|
|
240
|
+
) => T = vector4Subtract;
|
|
282
241
|
|
|
283
242
|
/**
|
|
284
243
|
* Transpose a matrix.
|
|
285
244
|
* @param matrix - The matrix.
|
|
286
245
|
* @param out - The matrix to store the result in.
|
|
287
246
|
* @returns The transpose of the matrix.
|
|
288
|
-
* @see
|
|
247
|
+
* @see {@link https://en.wikipedia.org/wiki/Transpose | Transpose}
|
|
248
|
+
* @public
|
|
289
249
|
*/
|
|
290
250
|
export const transpose = <T extends Matrix2Like>(
|
|
291
251
|
matrix: Matrix2Like,
|
|
@@ -295,45 +255,39 @@ export const transpose = <T extends Matrix2Like>(
|
|
|
295
255
|
const a1 = matrix[1];
|
|
296
256
|
out[1] = matrix[2];
|
|
297
257
|
out[2] = a1;
|
|
298
|
-
|
|
299
|
-
out[0] = matrix[0];
|
|
300
|
-
out[1] = matrix[2];
|
|
301
|
-
out[2] = matrix[1];
|
|
302
|
-
out[3] = matrix[3];
|
|
258
|
+
return out;
|
|
303
259
|
}
|
|
304
|
-
|
|
260
|
+
|
|
261
|
+
return fromValues(matrix[0], matrix[2], matrix[1], matrix[3], out);
|
|
305
262
|
};
|
|
306
263
|
|
|
307
264
|
/**
|
|
308
265
|
* Calculate the determinant of a matrix.
|
|
309
266
|
* @param matrix- The matrix.
|
|
310
267
|
* @returns The determinant.
|
|
311
|
-
* @see
|
|
268
|
+
* @see {@link https://en.wikipedia.org/wiki/Determinant | Determinant}
|
|
269
|
+
* @public
|
|
312
270
|
*/
|
|
313
|
-
export const determinant = (matrix: Matrix2Like): number =>
|
|
314
|
-
|
|
315
|
-
};
|
|
271
|
+
export const determinant = (matrix: Matrix2Like): number =>
|
|
272
|
+
matrix[0] * matrix[3] - matrix[2] * matrix[1];
|
|
316
273
|
|
|
317
274
|
/**
|
|
318
275
|
* Reset a matrix to identity.
|
|
319
276
|
* @param out - The matrix to store the result in.
|
|
320
277
|
* @returns The matrix.
|
|
321
|
-
* @see
|
|
278
|
+
* @see {@link https://en.wikipedia.org/wiki/Identity_matrix | Identity matrix}
|
|
279
|
+
* @public
|
|
322
280
|
*/
|
|
323
|
-
export const identity = <T extends Matrix2Like>(out: T): T =>
|
|
324
|
-
|
|
325
|
-
out[1] = 0;
|
|
326
|
-
out[2] = 0;
|
|
327
|
-
out[3] = 1;
|
|
328
|
-
return out;
|
|
329
|
-
};
|
|
281
|
+
export const identity = <T extends Matrix2Like>(out: T): T =>
|
|
282
|
+
fromValues(1, 0, 0, 1, out);
|
|
330
283
|
|
|
331
284
|
/**
|
|
332
285
|
* Invert a matrix.
|
|
333
286
|
* @param matrix - The matrix.
|
|
334
287
|
* @param out - The matrix to store the result in.
|
|
335
288
|
* @returns The inverted matrix.
|
|
336
|
-
* @see
|
|
289
|
+
* @see {@link https://en.wikipedia.org/wiki/Invertible_matrix | Invertible matrix}
|
|
290
|
+
* @public
|
|
337
291
|
*/
|
|
338
292
|
export const invert = <T extends Matrix2Like>(
|
|
339
293
|
matrix: Matrix2Like,
|
|
@@ -350,11 +304,7 @@ export const invert = <T extends Matrix2Like>(
|
|
|
350
304
|
}
|
|
351
305
|
det = 1 / det;
|
|
352
306
|
|
|
353
|
-
|
|
354
|
-
out[1] = -a1 * det;
|
|
355
|
-
out[2] = -a2 * det;
|
|
356
|
-
out[3] = a0 * det;
|
|
357
|
-
return out;
|
|
307
|
+
return fromValues(a3 * det, -a1 * det, -a2 * det, a0 * det, out);
|
|
358
308
|
};
|
|
359
309
|
|
|
360
310
|
/**
|
|
@@ -363,7 +313,8 @@ export const invert = <T extends Matrix2Like>(
|
|
|
363
313
|
* @param r - The angle in radians.
|
|
364
314
|
* @param out - The matrix to store the result in.
|
|
365
315
|
* @returns The rotated matrix.
|
|
366
|
-
* @see
|
|
316
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
317
|
+
* @public
|
|
367
318
|
*/
|
|
368
319
|
export const rotate = <T extends Matrix2Like>(
|
|
369
320
|
matrix: Matrix2Like,
|
|
@@ -378,11 +329,13 @@ export const rotate = <T extends Matrix2Like>(
|
|
|
378
329
|
const s = Math.sin(r);
|
|
379
330
|
const c = Math.cos(r);
|
|
380
331
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
332
|
+
return fromValues(
|
|
333
|
+
a0 * c + a2 * s,
|
|
334
|
+
a1 * c + a3 * s,
|
|
335
|
+
a0 * -s + a2 * c,
|
|
336
|
+
a1 * -s + a3 * c,
|
|
337
|
+
out
|
|
338
|
+
);
|
|
386
339
|
};
|
|
387
340
|
|
|
388
341
|
/**
|
|
@@ -391,7 +344,8 @@ export const rotate = <T extends Matrix2Like>(
|
|
|
391
344
|
* @param vector - The scaling vector.
|
|
392
345
|
* @param out - The matrix to store the result in.
|
|
393
346
|
* @returns The scaled matrix.
|
|
394
|
-
* @see
|
|
347
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
348
|
+
* @public
|
|
395
349
|
*/
|
|
396
350
|
export const scale = <T extends Matrix2Like>(
|
|
397
351
|
matrix: Matrix2Like,
|
|
@@ -401,16 +355,19 @@ export const scale = <T extends Matrix2Like>(
|
|
|
401
355
|
const v0 = vector[0];
|
|
402
356
|
const v1 = vector[1];
|
|
403
357
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
358
|
+
return fromValues(
|
|
359
|
+
matrix[0] * v0,
|
|
360
|
+
matrix[1] * v0,
|
|
361
|
+
matrix[2] * v1,
|
|
362
|
+
matrix[3] * v1,
|
|
363
|
+
out
|
|
364
|
+
);
|
|
409
365
|
};
|
|
410
366
|
|
|
411
367
|
/**
|
|
412
368
|
* A two-by-two matrix.
|
|
413
|
-
* @see
|
|
369
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_(mathematics) | Matrix}
|
|
370
|
+
* @public
|
|
414
371
|
*/
|
|
415
372
|
export default class Matrix2
|
|
416
373
|
extends Float32Array
|
|
@@ -421,7 +378,7 @@ export default class Matrix2
|
|
|
421
378
|
* @param r - The angle in radians.
|
|
422
379
|
* @param out - The matrix to store the result in.
|
|
423
380
|
* @returns The transformation matrix.
|
|
424
|
-
* @see
|
|
381
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
425
382
|
*/
|
|
426
383
|
public static fromRotation<T extends Matrix2Like = Matrix2>(
|
|
427
384
|
r: number,
|
|
@@ -435,7 +392,7 @@ export default class Matrix2
|
|
|
435
392
|
* @param vector - The scaling vector.
|
|
436
393
|
* @param out - The matrix to store the result in.
|
|
437
394
|
* @returns The transformation matrix.
|
|
438
|
-
* @see
|
|
395
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
439
396
|
*/
|
|
440
397
|
public static fromScaling<T extends Matrix2Like = Matrix2>(
|
|
441
398
|
vector: Vector2Like,
|
|
@@ -465,7 +422,7 @@ export default class Matrix2
|
|
|
465
422
|
|
|
466
423
|
/**
|
|
467
424
|
* Create a two-by-two identity matrix.
|
|
468
|
-
* @see
|
|
425
|
+
* @see {@link https://en.wikipedia.org/wiki/Identity_matrix | Identity matrix}
|
|
469
426
|
*/
|
|
470
427
|
public constructor() {
|
|
471
428
|
super(4);
|
|
@@ -518,7 +475,7 @@ export default class Matrix2
|
|
|
518
475
|
* @param matrix - The other matrix.
|
|
519
476
|
* @param out - The matrix to store the result in.
|
|
520
477
|
* @returns The sum of the matrices.
|
|
521
|
-
* @see
|
|
478
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
522
479
|
*/
|
|
523
480
|
public add<T extends Matrix2Like = Matrix2>(
|
|
524
481
|
matrix: Matrix2Like,
|
|
@@ -531,7 +488,7 @@ export default class Matrix2
|
|
|
531
488
|
* Calculate the adjugate of this matrix.
|
|
532
489
|
* @param out - The matrix to store the result in.
|
|
533
490
|
* @returns The adjugate of this matrix.
|
|
534
|
-
* @see
|
|
491
|
+
* @see {@link https://en.wikipedia.org/wiki/Adjugate_matrix | Adjugate matrix}
|
|
535
492
|
*/
|
|
536
493
|
public adjoint<T extends Matrix2Like = Matrix2>(
|
|
537
494
|
out: T = new Matrix2() as Matrix2 & T
|
|
@@ -561,7 +518,7 @@ export default class Matrix2
|
|
|
561
518
|
|
|
562
519
|
/**
|
|
563
520
|
* Get the Frobenius norm of this matrix.
|
|
564
|
-
* @see
|
|
521
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_norm | Matrix norm}
|
|
565
522
|
*/
|
|
566
523
|
public get frob(): number {
|
|
567
524
|
return frob(this);
|
|
@@ -572,7 +529,8 @@ export default class Matrix2
|
|
|
572
529
|
* @param matrix - The other matrix.
|
|
573
530
|
* @param out - The matrix to store the result in.
|
|
574
531
|
* @returns The product of the matrices.
|
|
575
|
-
* @see
|
|
532
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
533
|
+
* @public
|
|
576
534
|
*/
|
|
577
535
|
public multiply<T extends Matrix2Like = Matrix2>(
|
|
578
536
|
matrix: Matrix2Like,
|
|
@@ -586,7 +544,7 @@ export default class Matrix2
|
|
|
586
544
|
* @param scalar - The scalar value.
|
|
587
545
|
* @param out - The matrix to store the result in.
|
|
588
546
|
* @returns The product of the matrix and the scalar value.
|
|
589
|
-
* @see
|
|
547
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
590
548
|
*/
|
|
591
549
|
public multiplyScalar<T extends Matrix2Like = Matrix2>(
|
|
592
550
|
scalar: number,
|
|
@@ -601,8 +559,8 @@ export default class Matrix2
|
|
|
601
559
|
* @param scalar - The scalar.
|
|
602
560
|
* @param out - The matrix to store the result in.
|
|
603
561
|
* @returns The sum.
|
|
604
|
-
* @see
|
|
605
|
-
* @see
|
|
562
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
563
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
606
564
|
*/
|
|
607
565
|
public multiplyScalarAndAdd<T extends Matrix2Like = Matrix2>(
|
|
608
566
|
matrix: Matrix2Like,
|
|
@@ -617,7 +575,7 @@ export default class Matrix2
|
|
|
617
575
|
* @param matrix - The other matrix.
|
|
618
576
|
* @param out - The matrix to store the result in.
|
|
619
577
|
* @returns The difference between the matrices.
|
|
620
|
-
* @see
|
|
578
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
621
579
|
*/
|
|
622
580
|
public subtract<T extends Matrix2Like = Matrix2>(
|
|
623
581
|
matrix: Matrix2Like,
|
|
@@ -630,7 +588,7 @@ export default class Matrix2
|
|
|
630
588
|
* Transpose this matrix.
|
|
631
589
|
* @param out - The matrix to store the result in.
|
|
632
590
|
* @returns The transpose of this matrix.
|
|
633
|
-
* @see
|
|
591
|
+
* @see {@link https://en.wikipedia.org/wiki/Transpose | Transpose}
|
|
634
592
|
*/
|
|
635
593
|
public transpose<T extends Matrix2Like = Matrix2>(
|
|
636
594
|
out: T = new Matrix2() as Matrix2 & T
|
|
@@ -640,7 +598,7 @@ export default class Matrix2
|
|
|
640
598
|
|
|
641
599
|
/**
|
|
642
600
|
* Get the determinant of this matrix.
|
|
643
|
-
* @see
|
|
601
|
+
* @see {@link https://en.wikipedia.org/wiki/Determinant | Determinant}
|
|
644
602
|
*/
|
|
645
603
|
public get determinant(): number {
|
|
646
604
|
return determinant(this);
|
|
@@ -649,7 +607,7 @@ export default class Matrix2
|
|
|
649
607
|
/**
|
|
650
608
|
* Reset this matrix to identity.
|
|
651
609
|
* @returns This matrix.
|
|
652
|
-
* @see
|
|
610
|
+
* @see {@link https://en.wikipedia.org/wiki/Identity_matrix | Identity matrix}
|
|
653
611
|
*/
|
|
654
612
|
public identity(): this {
|
|
655
613
|
return identity(this);
|
|
@@ -659,7 +617,7 @@ export default class Matrix2
|
|
|
659
617
|
* Invert this matrix.
|
|
660
618
|
* @param out - The matrix to store the result in.
|
|
661
619
|
* @returns The inverted matrix.
|
|
662
|
-
* @see
|
|
620
|
+
* @see {@link https://en.wikipedia.org/wiki/Invertible_matrix | Invertible matrix}
|
|
663
621
|
*/
|
|
664
622
|
public invert<T extends Matrix2Like = Matrix2>(
|
|
665
623
|
out: T = new Matrix2() as Matrix2 & T
|
|
@@ -672,7 +630,7 @@ export default class Matrix2
|
|
|
672
630
|
* @param r - The angle in radians.
|
|
673
631
|
* @param out - The matrix to store the result in.
|
|
674
632
|
* @returns The rotated matrix.
|
|
675
|
-
* @see
|
|
633
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
676
634
|
*/
|
|
677
635
|
public rotate<T extends Matrix2Like = Matrix2>(
|
|
678
636
|
r: number,
|
|
@@ -686,7 +644,7 @@ export default class Matrix2
|
|
|
686
644
|
* @param vector - The scaling vector.
|
|
687
645
|
* @param out - The matrix to store the result in.
|
|
688
646
|
* @returns The scaled matrix.
|
|
689
|
-
* @see
|
|
647
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
690
648
|
*/
|
|
691
649
|
public scale<T extends Matrix2Like = Matrix2>(
|
|
692
650
|
vector: Vector2Like,
|