@lakuna/umath 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/LICENSE +1 -1
- package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.js +14 -4
- package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/linalg/DualQuaternion.d.ts +124 -124
- package/dist/linalg/DualQuaternion.d.ts.map +1 -1
- package/dist/linalg/DualQuaternion.js +173 -165
- package/dist/linalg/DualQuaternion.js.map +1 -1
- package/dist/linalg/Matrix.d.ts +24 -24
- package/dist/linalg/Matrix.d.ts.map +1 -1
- package/dist/linalg/Matrix2.d.ts +63 -63
- package/dist/linalg/Matrix2.d.ts.map +1 -1
- package/dist/linalg/Matrix2.js +92 -88
- package/dist/linalg/Matrix2.js.map +1 -1
- package/dist/linalg/Matrix3.d.ts +107 -107
- package/dist/linalg/Matrix3.d.ts.map +1 -1
- package/dist/linalg/Matrix3.js +147 -138
- package/dist/linalg/Matrix3.js.map +1 -1
- package/dist/linalg/Matrix4.d.ts +200 -201
- package/dist/linalg/Matrix4.d.ts.map +1 -1
- package/dist/linalg/Matrix4.js +272 -251
- package/dist/linalg/Matrix4.js.map +1 -1
- package/dist/linalg/Quaternion.d.ts +116 -116
- package/dist/linalg/Quaternion.d.ts.map +1 -1
- package/dist/linalg/Quaternion.js +175 -161
- package/dist/linalg/Quaternion.js.map +1 -1
- package/dist/linalg/SlowMatrix.d.ts +5 -5
- package/dist/linalg/SlowMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowMatrix.js +12 -12
- package/dist/linalg/SlowMatrix.js.map +1 -1
- package/dist/linalg/SlowSquareMatrix.d.ts +6 -6
- package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowSquareMatrix.js +15 -15
- package/dist/linalg/SlowSquareMatrix.js.map +1 -1
- package/dist/linalg/SlowVector.d.ts +70 -70
- package/dist/linalg/SlowVector.d.ts.map +1 -1
- package/dist/linalg/SlowVector.js +153 -153
- package/dist/linalg/SlowVector.js.map +1 -1
- package/dist/linalg/SquareMatrix.d.ts +8 -8
- package/dist/linalg/SquareMatrix.d.ts.map +1 -1
- package/dist/linalg/Vector.d.ts +85 -84
- package/dist/linalg/Vector.d.ts.map +1 -1
- package/dist/linalg/Vector2.d.ts +104 -104
- package/dist/linalg/Vector2.d.ts.map +1 -1
- package/dist/linalg/Vector2.js +145 -143
- package/dist/linalg/Vector2.js.map +1 -1
- package/dist/linalg/Vector3.d.ts +130 -130
- package/dist/linalg/Vector3.d.ts.map +1 -1
- package/dist/linalg/Vector3.js +176 -173
- package/dist/linalg/Vector3.js.map +1 -1
- package/dist/linalg/Vector4.d.ts +101 -101
- package/dist/linalg/Vector4.d.ts.map +1 -1
- package/dist/linalg/Vector4.js +144 -140
- package/dist/linalg/Vector4.js.map +1 -1
- package/dist/types/AxisAngle.d.ts +2 -2
- package/dist/types/AxisAngle.d.ts.map +1 -1
- package/dist/types/FieldOfView.d.ts +2 -2
- package/dist/types/FieldOfView.d.ts.map +1 -1
- package/dist/utility/BigNumber.d.ts +21 -11
- package/dist/utility/BigNumber.d.ts.map +1 -1
- package/dist/utility/BigNumber.js +42 -31
- package/dist/utility/BigNumber.js.map +1 -1
- package/dist/utility/createAxisAngleLike.d.ts.map +1 -1
- package/dist/utility/createAxisAngleLike.js.map +1 -1
- package/package.json +11 -9
- package/src/algorithms/greatestCommonDivisor.ts +19 -7
- package/src/index.ts +17 -13
- package/src/linalg/DualQuaternion.ts +208 -193
- package/src/linalg/Matrix.ts +28 -28
- package/src/linalg/Matrix2.ts +117 -108
- package/src/linalg/Matrix3.ts +177 -158
- package/src/linalg/Matrix4.ts +386 -348
- package/src/linalg/Quaternion.ts +207 -189
- package/src/linalg/SlowMatrix.ts +19 -18
- package/src/linalg/SlowSquareMatrix.ts +19 -18
- package/src/linalg/SlowVector.ts +170 -169
- package/src/linalg/SquareMatrix.ts +9 -9
- package/src/linalg/Vector.ts +91 -89
- package/src/linalg/Vector2.ts +160 -155
- package/src/linalg/Vector3.ts +206 -199
- package/src/linalg/Vector4.ts +165 -156
- package/src/types/AxisAngle.ts +3 -3
- package/src/types/FieldOfView.ts +3 -3
- package/src/utility/BigNumber.ts +63 -41
- package/src/utility/createAxisAngleLike.ts +1 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type SquareMatrix from "./SquareMatrix.js";
|
|
2
|
+
|
|
1
3
|
import MatrixSizeError from "../utility/MatrixSizeError.js";
|
|
2
4
|
import SingularMatrixError from "../utility/SingularMatrixError.js";
|
|
3
5
|
import SlowMatrix from "./SlowMatrix.js";
|
|
4
|
-
import type SquareMatrix from "./SquareMatrix.js";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* A variable-size matrix with the same number of rows and columns.
|
|
@@ -12,23 +13,6 @@ export default class SlowSquareMatrix
|
|
|
12
13
|
extends SlowMatrix
|
|
13
14
|
implements SquareMatrix
|
|
14
15
|
{
|
|
15
|
-
/**
|
|
16
|
-
* Create a variable-size matrix with the same number of rows and columns from the given columns.
|
|
17
|
-
* @param cols - The columns in the matrix.
|
|
18
|
-
* @see {@link https://en.wikipedia.org/wiki/Square_matrix | Square matrix}
|
|
19
|
-
*/
|
|
20
|
-
public constructor(...cols: number[][]) {
|
|
21
|
-
super(...cols);
|
|
22
|
-
|
|
23
|
-
// Ensure that every column is the same height and that the height is equal to the width.
|
|
24
|
-
for (let i = 0; i < this.width; i++) {
|
|
25
|
-
const col = cols[i];
|
|
26
|
-
if (col?.length !== this.height) {
|
|
27
|
-
throw new MatrixSizeError();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
16
|
/**
|
|
33
17
|
* Get the determinant of this matrix.
|
|
34
18
|
* @see {@link https://en.wikipedia.org/wiki/Determinant | Determinant}
|
|
@@ -54,6 +38,23 @@ export default class SlowSquareMatrix
|
|
|
54
38
|
return out;
|
|
55
39
|
}
|
|
56
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Create a variable-size matrix with the same number of rows and columns from the given columns.
|
|
43
|
+
* @param cols - The columns in the matrix.
|
|
44
|
+
* @see {@link https://en.wikipedia.org/wiki/Square_matrix | Square matrix}
|
|
45
|
+
*/
|
|
46
|
+
public constructor(...cols: number[][]) {
|
|
47
|
+
super(...cols);
|
|
48
|
+
|
|
49
|
+
// Ensure that every column is the same height and that the height is equal to the width.
|
|
50
|
+
for (let i = 0; i < this.width; i++) {
|
|
51
|
+
const col = cols[i];
|
|
52
|
+
if (col?.length !== this.height) {
|
|
53
|
+
throw new MatrixSizeError();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
57
58
|
/**
|
|
58
59
|
* Calculate the adjugate of this matrix.
|
|
59
60
|
* @returns The adjugate of this matrix.
|
package/src/linalg/SlowVector.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type Vector from "./Vector.js";
|
|
2
2
|
import type { VectorLike } from "./Vector.js";
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
import approx from "../algorithms/approx.js";
|
|
5
|
+
import VectorSizeError from "../utility/VectorSizeError.js";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* A vector with size information.
|
|
@@ -25,56 +26,27 @@ const isSized = (vector: VectorLike): vector is SizedVectorLike =>
|
|
|
25
26
|
* @public
|
|
26
27
|
*/
|
|
27
28
|
export default class SlowVector extends Float32Array implements Vector {
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*/
|
|
32
|
-
public constructor(...values: number[]) {
|
|
33
|
-
super(values);
|
|
29
|
+
/** Get the magnitude (length) of this vector. */
|
|
30
|
+
public get magnitude(): number {
|
|
31
|
+
return Math.hypot(...this);
|
|
34
32
|
}
|
|
35
33
|
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* @returns Whether the vectors are equivalent.
|
|
40
|
-
*/
|
|
41
|
-
public equals(vector: VectorLike): boolean {
|
|
42
|
-
if (!isSized(vector) || this.length !== vector.length) {
|
|
43
|
-
throw new VectorSizeError();
|
|
44
|
-
}
|
|
45
|
-
|
|
34
|
+
/** Get the squared magnitude (length) of this vector. */
|
|
35
|
+
public get squaredMagnitude(): number {
|
|
36
|
+
let out = 0;
|
|
46
37
|
for (let i = 0; i < this.length; i++) {
|
|
47
|
-
|
|
48
|
-
const b = vector[i];
|
|
49
|
-
if (
|
|
50
|
-
typeof a === "undefined" ||
|
|
51
|
-
typeof b === "undefined" ||
|
|
52
|
-
!approx(a, b)
|
|
53
|
-
) {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
38
|
+
out += (this[i] ?? 0) ** 2;
|
|
56
39
|
}
|
|
57
40
|
|
|
58
|
-
return
|
|
41
|
+
return out;
|
|
59
42
|
}
|
|
60
43
|
|
|
61
44
|
/**
|
|
62
|
-
*
|
|
63
|
-
* @param
|
|
64
|
-
* @returns Whether the vectors are equivalent.
|
|
45
|
+
* Create a variable-size vector from the given values.
|
|
46
|
+
* @param values - The values in the vector.
|
|
65
47
|
*/
|
|
66
|
-
public
|
|
67
|
-
|
|
68
|
-
throw new VectorSizeError();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
for (let i = 0; i < this.length; i++) {
|
|
72
|
-
if (this[i] !== vector[i]) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return true;
|
|
48
|
+
public constructor(...values: number[]) {
|
|
49
|
+
super(values);
|
|
78
50
|
}
|
|
79
51
|
|
|
80
52
|
/**
|
|
@@ -95,6 +67,19 @@ export default class SlowVector extends Float32Array implements Vector {
|
|
|
95
67
|
return new SlowVector(...out);
|
|
96
68
|
}
|
|
97
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Round up the components of this vector.
|
|
72
|
+
* @returns The rounded vector.
|
|
73
|
+
*/
|
|
74
|
+
public ceil(): SlowVector {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (let i = 0; i < this.length; i++) {
|
|
77
|
+
out[i] = Math.ceil(this[i] ?? 0);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return new SlowVector(...out);
|
|
81
|
+
}
|
|
82
|
+
|
|
98
83
|
/**
|
|
99
84
|
* Create a copy of this vector.
|
|
100
85
|
* @returns A copy of this vector.
|
|
@@ -121,21 +106,21 @@ export default class SlowVector extends Float32Array implements Vector {
|
|
|
121
106
|
}
|
|
122
107
|
|
|
123
108
|
/**
|
|
124
|
-
*
|
|
109
|
+
* Calculate the Euclidean distance between this vector and another.
|
|
125
110
|
* @param vector - The other vector.
|
|
126
|
-
* @returns The
|
|
111
|
+
* @returns The distance.
|
|
127
112
|
*/
|
|
128
|
-
public
|
|
113
|
+
public distance(vector: VectorLike): number {
|
|
129
114
|
if (!isSized(vector) || this.length !== vector.length) {
|
|
130
115
|
throw new VectorSizeError();
|
|
131
116
|
}
|
|
132
117
|
|
|
133
|
-
const
|
|
118
|
+
const temp = [];
|
|
134
119
|
for (let i = 0; i < this.length; i++) {
|
|
135
|
-
|
|
120
|
+
temp[i] = (vector[i] ?? 0) - (this[i] ?? 0);
|
|
136
121
|
}
|
|
137
122
|
|
|
138
|
-
return
|
|
123
|
+
return Math.hypot(...temp);
|
|
139
124
|
}
|
|
140
125
|
|
|
141
126
|
/**
|
|
@@ -157,34 +142,66 @@ export default class SlowVector extends Float32Array implements Vector {
|
|
|
157
142
|
}
|
|
158
143
|
|
|
159
144
|
/**
|
|
160
|
-
*
|
|
145
|
+
* Calculate the dot product of this and another vector.
|
|
161
146
|
* @param vector - The other vector.
|
|
162
|
-
* @returns The
|
|
147
|
+
* @returns The dot product.
|
|
148
|
+
* @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
|
|
163
149
|
*/
|
|
164
|
-
public
|
|
150
|
+
public dot(vector: VectorLike): number {
|
|
165
151
|
if (!isSized(vector) || this.length !== vector.length) {
|
|
166
152
|
throw new VectorSizeError();
|
|
167
153
|
}
|
|
168
154
|
|
|
169
|
-
|
|
155
|
+
let out = 0;
|
|
170
156
|
for (let i = 0; i < this.length; i++) {
|
|
171
|
-
out
|
|
157
|
+
out += (this[i] ?? 0) * (vector[i] ?? 0);
|
|
172
158
|
}
|
|
173
159
|
|
|
174
|
-
return
|
|
160
|
+
return out;
|
|
175
161
|
}
|
|
176
162
|
|
|
177
163
|
/**
|
|
178
|
-
*
|
|
179
|
-
* @
|
|
164
|
+
* Determine whether this vector is roughly equivalent to another.
|
|
165
|
+
* @param vector - The other vector.
|
|
166
|
+
* @returns Whether the vectors are equivalent.
|
|
180
167
|
*/
|
|
181
|
-
public
|
|
182
|
-
|
|
168
|
+
public equals(vector: VectorLike): boolean {
|
|
169
|
+
if (!isSized(vector) || this.length !== vector.length) {
|
|
170
|
+
throw new VectorSizeError();
|
|
171
|
+
}
|
|
172
|
+
|
|
183
173
|
for (let i = 0; i < this.length; i++) {
|
|
184
|
-
|
|
174
|
+
const a = this[i];
|
|
175
|
+
const b = vector[i];
|
|
176
|
+
if (
|
|
177
|
+
typeof a === "undefined" ||
|
|
178
|
+
typeof b === "undefined" ||
|
|
179
|
+
!approx(a, b)
|
|
180
|
+
) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
185
183
|
}
|
|
186
184
|
|
|
187
|
-
return
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Determine whether this vector is exactly equivalent to another.
|
|
190
|
+
* @param vector - The other vector.
|
|
191
|
+
* @returns Whether the vectors are equivalent.
|
|
192
|
+
*/
|
|
193
|
+
public exactEquals(vector: VectorLike): boolean {
|
|
194
|
+
if (!isSized(vector) || this.length !== vector.length) {
|
|
195
|
+
throw new VectorSizeError();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
for (let i = 0; i < this.length; i++) {
|
|
199
|
+
if (this[i] !== vector[i]) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return true;
|
|
188
205
|
}
|
|
189
206
|
|
|
190
207
|
/**
|
|
@@ -201,31 +218,33 @@ export default class SlowVector extends Float32Array implements Vector {
|
|
|
201
218
|
}
|
|
202
219
|
|
|
203
220
|
/**
|
|
204
|
-
*
|
|
205
|
-
* @returns The
|
|
221
|
+
* Calculate the multiplicative inverse of the components of this vector.
|
|
222
|
+
* @returns The inverted vector.
|
|
206
223
|
*/
|
|
207
|
-
public
|
|
224
|
+
public invert(): SlowVector {
|
|
208
225
|
const out = [];
|
|
209
226
|
for (let i = 0; i < this.length; i++) {
|
|
210
|
-
out[i] =
|
|
227
|
+
out[i] = 1 / (this[i] ?? 0);
|
|
211
228
|
}
|
|
212
229
|
|
|
213
230
|
return new SlowVector(...out);
|
|
214
231
|
}
|
|
215
232
|
|
|
216
233
|
/**
|
|
217
|
-
*
|
|
234
|
+
* Perform a linear interpolation between this and another vector.
|
|
218
235
|
* @param vector - The other vector.
|
|
219
|
-
* @
|
|
236
|
+
* @param t - The interpolation amount (in `[0,1]`).
|
|
237
|
+
* @returns The interpolated vector.
|
|
220
238
|
*/
|
|
221
|
-
public
|
|
239
|
+
public lerp(vector: VectorLike, t: number): SlowVector {
|
|
222
240
|
if (!isSized(vector) || this.length !== vector.length) {
|
|
223
241
|
throw new VectorSizeError();
|
|
224
242
|
}
|
|
225
243
|
|
|
226
244
|
const out = [];
|
|
227
245
|
for (let i = 0; i < this.length; i++) {
|
|
228
|
-
|
|
246
|
+
const ti = this[i] ?? 0;
|
|
247
|
+
out[i] = ti + t * ((vector[i] ?? 0) - ti);
|
|
229
248
|
}
|
|
230
249
|
|
|
231
250
|
return new SlowVector(...out);
|
|
@@ -250,191 +269,173 @@ export default class SlowVector extends Float32Array implements Vector {
|
|
|
250
269
|
}
|
|
251
270
|
|
|
252
271
|
/**
|
|
253
|
-
*
|
|
254
|
-
* @param
|
|
255
|
-
* @returns The
|
|
272
|
+
* Return the minimum of this and another vector.
|
|
273
|
+
* @param vector - The other vector.
|
|
274
|
+
* @returns The minimum.
|
|
256
275
|
*/
|
|
257
|
-
public
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
out[i] = (this[i] ?? 0) ** exponent;
|
|
276
|
+
public min(vector: VectorLike): SlowVector {
|
|
277
|
+
if (!isSized(vector) || this.length !== vector.length) {
|
|
278
|
+
throw new VectorSizeError();
|
|
261
279
|
}
|
|
262
280
|
|
|
263
|
-
return new SlowVector(...out);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Scale this vector by a scalar.
|
|
268
|
-
* @param scalar - The scalar.
|
|
269
|
-
* @returns The scaled vector.
|
|
270
|
-
*/
|
|
271
|
-
public scale(scalar: number): SlowVector {
|
|
272
281
|
const out = [];
|
|
273
282
|
for (let i = 0; i < this.length; i++) {
|
|
274
|
-
out[i] = (this[i] ?? 0
|
|
283
|
+
out[i] = Math.min(this[i] ?? 0, vector[i] ?? 0);
|
|
275
284
|
}
|
|
276
285
|
|
|
277
286
|
return new SlowVector(...out);
|
|
278
287
|
}
|
|
279
288
|
|
|
280
289
|
/**
|
|
281
|
-
*
|
|
290
|
+
* Multiply this vector by another.
|
|
282
291
|
* @param vector - The other vector.
|
|
283
|
-
* @
|
|
284
|
-
* @returns The sum.
|
|
292
|
+
* @returns The product of the vectors.
|
|
285
293
|
*/
|
|
286
|
-
public
|
|
287
|
-
if (!isSized(vector)) {
|
|
294
|
+
public multiply(vector: VectorLike): SlowVector {
|
|
295
|
+
if (!isSized(vector) || this.length !== vector.length) {
|
|
288
296
|
throw new VectorSizeError();
|
|
289
297
|
}
|
|
290
298
|
|
|
291
299
|
const out = [];
|
|
292
|
-
for (let i = 0; i <
|
|
293
|
-
out[i] = (
|
|
300
|
+
for (let i = 0; i < this.length; i++) {
|
|
301
|
+
out[i] = (this[i] ?? 0) * (vector[i] ?? 0);
|
|
294
302
|
}
|
|
295
303
|
|
|
296
|
-
return
|
|
304
|
+
return new SlowVector(...out);
|
|
297
305
|
}
|
|
298
306
|
|
|
299
307
|
/**
|
|
300
|
-
*
|
|
301
|
-
* @
|
|
302
|
-
* @returns The distance.
|
|
308
|
+
* Negate this vector.
|
|
309
|
+
* @returns The negated vector.
|
|
303
310
|
*/
|
|
304
|
-
public
|
|
305
|
-
|
|
306
|
-
throw new VectorSizeError();
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
const temp = [];
|
|
310
|
-
for (let i = 0; i < this.length; i++) {
|
|
311
|
-
temp[i] = (vector[i] ?? 0) - (this[i] ?? 0);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
return Math.hypot(...temp);
|
|
311
|
+
public negate(): SlowVector {
|
|
312
|
+
return this.scale(-1);
|
|
315
313
|
}
|
|
316
314
|
|
|
317
315
|
/**
|
|
318
|
-
*
|
|
319
|
-
* @
|
|
320
|
-
* @
|
|
316
|
+
* Normalize this vector.
|
|
317
|
+
* @returns The normalized vector.
|
|
318
|
+
* @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
|
|
321
319
|
*/
|
|
322
|
-
public
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}
|
|
320
|
+
public normalize(): SlowVector {
|
|
321
|
+
return this.scale(1 / this.magnitude);
|
|
322
|
+
}
|
|
326
323
|
|
|
327
|
-
|
|
324
|
+
/**
|
|
325
|
+
* Raise each component of this vector to the given power.
|
|
326
|
+
* @param exponent - The exponent (power) to raise each component to.
|
|
327
|
+
* @returns The power (result of the exponentiation).
|
|
328
|
+
*/
|
|
329
|
+
public pow(exponent: number): SlowVector {
|
|
330
|
+
const out = [];
|
|
328
331
|
for (let i = 0; i < this.length; i++) {
|
|
329
|
-
out
|
|
332
|
+
out[i] = (this[i] ?? 0) ** exponent;
|
|
330
333
|
}
|
|
331
334
|
|
|
332
|
-
return out;
|
|
335
|
+
return new SlowVector(...out);
|
|
333
336
|
}
|
|
334
337
|
|
|
335
|
-
/**
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
338
|
+
/**
|
|
339
|
+
* Set this vector to a random value with the given magnitude.
|
|
340
|
+
* @param magnitude - The magnitude.
|
|
341
|
+
* @returns This vector.
|
|
342
|
+
*/
|
|
343
|
+
public random(magnitude: number): this {
|
|
344
|
+
const temp = [];
|
|
345
|
+
for (let i = 0; i < this.length; i++) {
|
|
346
|
+
temp[i] = Math.random();
|
|
347
|
+
}
|
|
339
348
|
|
|
340
|
-
|
|
341
|
-
public get squaredMagnitude(): number {
|
|
342
|
-
let out = 0;
|
|
349
|
+
const randomized = new SlowVector(...temp).normalize().scale(magnitude);
|
|
343
350
|
for (let i = 0; i < this.length; i++) {
|
|
344
|
-
|
|
351
|
+
this[i] = randomized[i] ?? 0;
|
|
345
352
|
}
|
|
346
353
|
|
|
347
|
-
return
|
|
354
|
+
return this;
|
|
348
355
|
}
|
|
349
356
|
|
|
350
357
|
/**
|
|
351
|
-
*
|
|
352
|
-
* @returns The
|
|
358
|
+
* Round the components of this vector.
|
|
359
|
+
* @returns The rounded vector.
|
|
353
360
|
*/
|
|
354
|
-
public
|
|
355
|
-
|
|
361
|
+
public round(): SlowVector {
|
|
362
|
+
const out = [];
|
|
363
|
+
for (let i = 0; i < this.length; i++) {
|
|
364
|
+
out[i] = Math.round(this[i] ?? 0);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return new SlowVector(...out);
|
|
356
368
|
}
|
|
357
369
|
|
|
358
370
|
/**
|
|
359
|
-
*
|
|
360
|
-
* @
|
|
371
|
+
* Scale this vector by a scalar.
|
|
372
|
+
* @param scalar - The scalar.
|
|
373
|
+
* @returns The scaled vector.
|
|
361
374
|
*/
|
|
362
|
-
public
|
|
375
|
+
public scale(scalar: number): SlowVector {
|
|
363
376
|
const out = [];
|
|
364
377
|
for (let i = 0; i < this.length; i++) {
|
|
365
|
-
out[i] =
|
|
378
|
+
out[i] = (this[i] ?? 0) * scalar;
|
|
366
379
|
}
|
|
367
380
|
|
|
368
381
|
return new SlowVector(...out);
|
|
369
382
|
}
|
|
370
383
|
|
|
371
384
|
/**
|
|
372
|
-
*
|
|
373
|
-
* @
|
|
374
|
-
* @
|
|
385
|
+
* Add another vector to this one after scaling the other by a scalar.
|
|
386
|
+
* @param vector - The other vector.
|
|
387
|
+
* @param scalar - The scalar.
|
|
388
|
+
* @returns The sum.
|
|
375
389
|
*/
|
|
376
|
-
public
|
|
377
|
-
|
|
390
|
+
public scaleAndAdd(vector: VectorLike, scalar: number): SlowVector {
|
|
391
|
+
if (!isSized(vector)) {
|
|
392
|
+
throw new VectorSizeError();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const out = [];
|
|
396
|
+
for (let i = 0; i < vector.length; i++) {
|
|
397
|
+
out[i] = (vector[i] ?? 0) * scalar;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return this.add(out);
|
|
378
401
|
}
|
|
379
402
|
|
|
380
403
|
/**
|
|
381
|
-
* Calculate the
|
|
404
|
+
* Calculate the squared Euclidean distance between this vector and another.
|
|
382
405
|
* @param vector - The other vector.
|
|
383
|
-
* @returns The
|
|
384
|
-
* @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
|
|
406
|
+
* @returns The squared distance.
|
|
385
407
|
*/
|
|
386
|
-
public
|
|
408
|
+
public squaredDistance(vector: VectorLike): number {
|
|
387
409
|
if (!isSized(vector) || this.length !== vector.length) {
|
|
388
410
|
throw new VectorSizeError();
|
|
389
411
|
}
|
|
390
412
|
|
|
391
413
|
let out = 0;
|
|
392
414
|
for (let i = 0; i < this.length; i++) {
|
|
393
|
-
out += (
|
|
415
|
+
out += ((vector[i] ?? 0) - (this[i] ?? 0)) ** 2;
|
|
394
416
|
}
|
|
395
417
|
|
|
396
418
|
return out;
|
|
397
419
|
}
|
|
398
420
|
|
|
399
421
|
/**
|
|
400
|
-
*
|
|
422
|
+
* Subtract another vector from this one.
|
|
401
423
|
* @param vector - The other vector.
|
|
402
|
-
* @
|
|
403
|
-
* @returns The interpolated vector.
|
|
424
|
+
* @returns The difference between the vectors.
|
|
404
425
|
*/
|
|
405
|
-
public
|
|
426
|
+
public subtract(vector: VectorLike): SlowVector {
|
|
406
427
|
if (!isSized(vector) || this.length !== vector.length) {
|
|
407
428
|
throw new VectorSizeError();
|
|
408
429
|
}
|
|
409
430
|
|
|
410
431
|
const out = [];
|
|
411
432
|
for (let i = 0; i < this.length; i++) {
|
|
412
|
-
|
|
413
|
-
out[i] = ti + t * ((vector[i] ?? 0) - ti);
|
|
433
|
+
out[i] = (this[i] ?? 0) - (vector[i] ?? 0);
|
|
414
434
|
}
|
|
415
435
|
|
|
416
436
|
return new SlowVector(...out);
|
|
417
437
|
}
|
|
418
438
|
|
|
419
|
-
/**
|
|
420
|
-
* Set this vector to a random value with the given magnitude.
|
|
421
|
-
* @param magnitude - The magnitude.
|
|
422
|
-
* @returns This vector.
|
|
423
|
-
*/
|
|
424
|
-
public random(magnitude: number): this {
|
|
425
|
-
const temp = [];
|
|
426
|
-
for (let i = 0; i < this.length; i++) {
|
|
427
|
-
temp[i] = Math.random();
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
const randomized = new SlowVector(...temp).normalize().scale(magnitude);
|
|
431
|
-
for (let i = 0; i < this.length; i++) {
|
|
432
|
-
this[i] = randomized[i] ?? 0;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return this;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
439
|
/**
|
|
439
440
|
* Set this to the zero vector.
|
|
440
441
|
* @returns This vector.
|
|
@@ -6,30 +6,30 @@ import type { default as Matrix, MatrixLike } from "./Matrix.js";
|
|
|
6
6
|
* @public
|
|
7
7
|
*/
|
|
8
8
|
export default interface SquareMatrix extends Matrix {
|
|
9
|
-
/**
|
|
10
|
-
* Get the determinant of this matrix.
|
|
11
|
-
* @see {@link https://en.wikipedia.org/wiki/Determinant | Determinant}
|
|
12
|
-
*/
|
|
13
|
-
get determinant(): number;
|
|
14
|
-
|
|
15
9
|
/**
|
|
16
10
|
* Calculate the adjugate of this matrix.
|
|
17
11
|
* @returns The adjugate of this matrix.
|
|
18
12
|
* @see {@link https://en.wikipedia.org/wiki/Adjugate_matrix | Adjugate matrix}
|
|
19
13
|
*/
|
|
20
|
-
adjoint()
|
|
14
|
+
adjoint: () => MatrixLike;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the determinant of this matrix.
|
|
18
|
+
* @see {@link https://en.wikipedia.org/wiki/Determinant | Determinant}
|
|
19
|
+
*/
|
|
20
|
+
get determinant(): number;
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Reset this matrix to identity.
|
|
24
24
|
* @returns This matrix.
|
|
25
25
|
* @see {@link https://en.wikipedia.org/wiki/Identity_matrix | Identity matrix}
|
|
26
26
|
*/
|
|
27
|
-
identity()
|
|
27
|
+
identity: () => this;
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Invert this matrix.
|
|
31
31
|
* @returns The inverted matrix.
|
|
32
32
|
* @see {@link https://en.wikipedia.org/wiki/Invertible_matrix | Invertible matrix}
|
|
33
33
|
*/
|
|
34
|
-
invert()
|
|
34
|
+
invert: () => MatrixLike;
|
|
35
35
|
}
|