@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.
Files changed (88) hide show
  1. package/LICENSE +1 -1
  2. package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
  3. package/dist/algorithms/greatestCommonDivisor.js +14 -4
  4. package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
  5. package/dist/index.d.ts +11 -11
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/linalg/DualQuaternion.d.ts +124 -124
  9. package/dist/linalg/DualQuaternion.d.ts.map +1 -1
  10. package/dist/linalg/DualQuaternion.js +173 -165
  11. package/dist/linalg/DualQuaternion.js.map +1 -1
  12. package/dist/linalg/Matrix.d.ts +24 -24
  13. package/dist/linalg/Matrix.d.ts.map +1 -1
  14. package/dist/linalg/Matrix2.d.ts +63 -63
  15. package/dist/linalg/Matrix2.d.ts.map +1 -1
  16. package/dist/linalg/Matrix2.js +92 -88
  17. package/dist/linalg/Matrix2.js.map +1 -1
  18. package/dist/linalg/Matrix3.d.ts +107 -107
  19. package/dist/linalg/Matrix3.d.ts.map +1 -1
  20. package/dist/linalg/Matrix3.js +147 -138
  21. package/dist/linalg/Matrix3.js.map +1 -1
  22. package/dist/linalg/Matrix4.d.ts +200 -201
  23. package/dist/linalg/Matrix4.d.ts.map +1 -1
  24. package/dist/linalg/Matrix4.js +272 -251
  25. package/dist/linalg/Matrix4.js.map +1 -1
  26. package/dist/linalg/Quaternion.d.ts +116 -116
  27. package/dist/linalg/Quaternion.d.ts.map +1 -1
  28. package/dist/linalg/Quaternion.js +175 -161
  29. package/dist/linalg/Quaternion.js.map +1 -1
  30. package/dist/linalg/SlowMatrix.d.ts +5 -5
  31. package/dist/linalg/SlowMatrix.d.ts.map +1 -1
  32. package/dist/linalg/SlowMatrix.js +12 -12
  33. package/dist/linalg/SlowMatrix.js.map +1 -1
  34. package/dist/linalg/SlowSquareMatrix.d.ts +6 -6
  35. package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
  36. package/dist/linalg/SlowSquareMatrix.js +15 -15
  37. package/dist/linalg/SlowSquareMatrix.js.map +1 -1
  38. package/dist/linalg/SlowVector.d.ts +70 -70
  39. package/dist/linalg/SlowVector.d.ts.map +1 -1
  40. package/dist/linalg/SlowVector.js +153 -153
  41. package/dist/linalg/SlowVector.js.map +1 -1
  42. package/dist/linalg/SquareMatrix.d.ts +8 -8
  43. package/dist/linalg/SquareMatrix.d.ts.map +1 -1
  44. package/dist/linalg/Vector.d.ts +85 -84
  45. package/dist/linalg/Vector.d.ts.map +1 -1
  46. package/dist/linalg/Vector2.d.ts +104 -104
  47. package/dist/linalg/Vector2.d.ts.map +1 -1
  48. package/dist/linalg/Vector2.js +145 -143
  49. package/dist/linalg/Vector2.js.map +1 -1
  50. package/dist/linalg/Vector3.d.ts +130 -130
  51. package/dist/linalg/Vector3.d.ts.map +1 -1
  52. package/dist/linalg/Vector3.js +176 -173
  53. package/dist/linalg/Vector3.js.map +1 -1
  54. package/dist/linalg/Vector4.d.ts +101 -101
  55. package/dist/linalg/Vector4.d.ts.map +1 -1
  56. package/dist/linalg/Vector4.js +144 -140
  57. package/dist/linalg/Vector4.js.map +1 -1
  58. package/dist/types/AxisAngle.d.ts +2 -2
  59. package/dist/types/AxisAngle.d.ts.map +1 -1
  60. package/dist/types/FieldOfView.d.ts +2 -2
  61. package/dist/types/FieldOfView.d.ts.map +1 -1
  62. package/dist/utility/BigNumber.d.ts +21 -11
  63. package/dist/utility/BigNumber.d.ts.map +1 -1
  64. package/dist/utility/BigNumber.js +42 -31
  65. package/dist/utility/BigNumber.js.map +1 -1
  66. package/dist/utility/createAxisAngleLike.d.ts.map +1 -1
  67. package/dist/utility/createAxisAngleLike.js.map +1 -1
  68. package/package.json +11 -9
  69. package/src/algorithms/greatestCommonDivisor.ts +19 -7
  70. package/src/index.ts +17 -13
  71. package/src/linalg/DualQuaternion.ts +208 -193
  72. package/src/linalg/Matrix.ts +28 -28
  73. package/src/linalg/Matrix2.ts +117 -108
  74. package/src/linalg/Matrix3.ts +177 -158
  75. package/src/linalg/Matrix4.ts +386 -348
  76. package/src/linalg/Quaternion.ts +207 -189
  77. package/src/linalg/SlowMatrix.ts +19 -18
  78. package/src/linalg/SlowSquareMatrix.ts +19 -18
  79. package/src/linalg/SlowVector.ts +170 -169
  80. package/src/linalg/SquareMatrix.ts +9 -9
  81. package/src/linalg/Vector.ts +91 -89
  82. package/src/linalg/Vector2.ts +160 -155
  83. package/src/linalg/Vector3.ts +206 -199
  84. package/src/linalg/Vector4.ts +165 -156
  85. package/src/types/AxisAngle.ts +3 -3
  86. package/src/types/FieldOfView.ts +3 -3
  87. package/src/utility/BigNumber.ts +63 -41
  88. 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.
@@ -1,7 +1,8 @@
1
1
  import type Vector from "./Vector.js";
2
2
  import type { VectorLike } from "./Vector.js";
3
- import VectorSizeError from "../utility/VectorSizeError.js";
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
- * Create a variable-size vector from the given values.
30
- * @param values - The values in the vector.
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
- * Determine whether this vector is roughly equivalent to another.
38
- * @param vector - The other vector.
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
- const a = this[i];
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 true;
41
+ return out;
59
42
  }
60
43
 
61
44
  /**
62
- * Determine whether this vector is exactly equivalent to another.
63
- * @param vector - The other vector.
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 exactEquals(vector: VectorLike): boolean {
67
- if (!isSized(vector) || this.length !== vector.length) {
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
- * Multiply this vector by another.
109
+ * Calculate the Euclidean distance between this vector and another.
125
110
  * @param vector - The other vector.
126
- * @returns The product of the vectors.
111
+ * @returns The distance.
127
112
  */
128
- public multiply(vector: VectorLike): SlowVector {
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 out = [];
118
+ const temp = [];
134
119
  for (let i = 0; i < this.length; i++) {
135
- out[i] = (this[i] ?? 0) * (vector[i] ?? 0);
120
+ temp[i] = (vector[i] ?? 0) - (this[i] ?? 0);
136
121
  }
137
122
 
138
- return new SlowVector(...out);
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
- * Subtract another vector from this one.
145
+ * Calculate the dot product of this and another vector.
161
146
  * @param vector - The other vector.
162
- * @returns The difference between the vectors.
147
+ * @returns The dot product.
148
+ * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
163
149
  */
164
- public subtract(vector: VectorLike): SlowVector {
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
- const out = [];
155
+ let out = 0;
170
156
  for (let i = 0; i < this.length; i++) {
171
- out[i] = (this[i] ?? 0) - (vector[i] ?? 0);
157
+ out += (this[i] ?? 0) * (vector[i] ?? 0);
172
158
  }
173
159
 
174
- return new SlowVector(...out);
160
+ return out;
175
161
  }
176
162
 
177
163
  /**
178
- * Round up the components of this vector.
179
- * @returns The rounded vector.
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 ceil(): SlowVector {
182
- const out = [];
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
- out[i] = Math.ceil(this[i] ?? 0);
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 new SlowVector(...out);
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
- * Round the components of this vector.
205
- * @returns The rounded vector.
221
+ * Calculate the multiplicative inverse of the components of this vector.
222
+ * @returns The inverted vector.
206
223
  */
207
- public round(): SlowVector {
224
+ public invert(): SlowVector {
208
225
  const out = [];
209
226
  for (let i = 0; i < this.length; i++) {
210
- out[i] = Math.round(this[i] ?? 0);
227
+ out[i] = 1 / (this[i] ?? 0);
211
228
  }
212
229
 
213
230
  return new SlowVector(...out);
214
231
  }
215
232
 
216
233
  /**
217
- * Return the minimum of this and another vector.
234
+ * Perform a linear interpolation between this and another vector.
218
235
  * @param vector - The other vector.
219
- * @returns The minimum.
236
+ * @param t - The interpolation amount (in `[0,1]`).
237
+ * @returns The interpolated vector.
220
238
  */
221
- public min(vector: VectorLike): SlowVector {
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
- out[i] = Math.min(this[i] ?? 0, vector[i] ?? 0);
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
- * Raise each component of this vector to the given power.
254
- * @param exponent - The exponent (power) to raise each component to.
255
- * @returns The power (result of the exponentiation).
272
+ * Return the minimum of this and another vector.
273
+ * @param vector - The other vector.
274
+ * @returns The minimum.
256
275
  */
257
- public pow(exponent: number): SlowVector {
258
- const out = [];
259
- for (let i = 0; i < this.length; i++) {
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) * scalar;
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
- * Add another vector to this one after scaling the other by a scalar.
290
+ * Multiply this vector by another.
282
291
  * @param vector - The other vector.
283
- * @param scalar - The scalar.
284
- * @returns The sum.
292
+ * @returns The product of the vectors.
285
293
  */
286
- public scaleAndAdd(vector: VectorLike, scalar: number): SlowVector {
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 < vector.length; i++) {
293
- out[i] = (vector[i] ?? 0) * scalar;
300
+ for (let i = 0; i < this.length; i++) {
301
+ out[i] = (this[i] ?? 0) * (vector[i] ?? 0);
294
302
  }
295
303
 
296
- return this.add(out);
304
+ return new SlowVector(...out);
297
305
  }
298
306
 
299
307
  /**
300
- * Calculate the Euclidean distance between this vector and another.
301
- * @param vector - The other vector.
302
- * @returns The distance.
308
+ * Negate this vector.
309
+ * @returns The negated vector.
303
310
  */
304
- public distance(vector: VectorLike): number {
305
- if (!isSized(vector) || this.length !== vector.length) {
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
- * Calculate the squared Euclidean distance between this vector and another.
319
- * @param vector - The other vector.
320
- * @returns The squared distance.
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 squaredDistance(vector: VectorLike): number {
323
- if (!isSized(vector) || this.length !== vector.length) {
324
- throw new VectorSizeError();
325
- }
320
+ public normalize(): SlowVector {
321
+ return this.scale(1 / this.magnitude);
322
+ }
326
323
 
327
- let out = 0;
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 += ((vector[i] ?? 0) - (this[i] ?? 0)) ** 2;
332
+ out[i] = (this[i] ?? 0) ** exponent;
330
333
  }
331
334
 
332
- return out;
335
+ return new SlowVector(...out);
333
336
  }
334
337
 
335
- /** Get the magnitude (length) of this vector. */
336
- public get magnitude(): number {
337
- return Math.hypot(...this);
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
- /** Get the squared magnitude (length) of this vector. */
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
- out += (this[i] ?? 0) ** 2;
351
+ this[i] = randomized[i] ?? 0;
345
352
  }
346
353
 
347
- return out;
354
+ return this;
348
355
  }
349
356
 
350
357
  /**
351
- * Negate this vector.
352
- * @returns The negated vector.
358
+ * Round the components of this vector.
359
+ * @returns The rounded vector.
353
360
  */
354
- public negate(): SlowVector {
355
- return this.scale(-1);
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
- * Calculate the multiplicative inverse of the components of this vector.
360
- * @returns The inverted vector.
371
+ * Scale this vector by a scalar.
372
+ * @param scalar - The scalar.
373
+ * @returns The scaled vector.
361
374
  */
362
- public invert(): SlowVector {
375
+ public scale(scalar: number): SlowVector {
363
376
  const out = [];
364
377
  for (let i = 0; i < this.length; i++) {
365
- out[i] = 1 / (this[i] ?? 0);
378
+ out[i] = (this[i] ?? 0) * scalar;
366
379
  }
367
380
 
368
381
  return new SlowVector(...out);
369
382
  }
370
383
 
371
384
  /**
372
- * Normalize this vector.
373
- * @returns The normalized vector.
374
- * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
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 normalize(): SlowVector {
377
- return this.scale(1 / this.magnitude);
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 dot product of this and another vector.
404
+ * Calculate the squared Euclidean distance between this vector and another.
382
405
  * @param vector - The other vector.
383
- * @returns The dot product.
384
- * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
406
+ * @returns The squared distance.
385
407
  */
386
- public dot(vector: VectorLike): number {
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 += (this[i] ?? 0) * (vector[i] ?? 0);
415
+ out += ((vector[i] ?? 0) - (this[i] ?? 0)) ** 2;
394
416
  }
395
417
 
396
418
  return out;
397
419
  }
398
420
 
399
421
  /**
400
- * Perform a linear interpolation between this and another vector.
422
+ * Subtract another vector from this one.
401
423
  * @param vector - The other vector.
402
- * @param t - The interpolation amount (in `[0,1]`).
403
- * @returns The interpolated vector.
424
+ * @returns The difference between the vectors.
404
425
  */
405
- public lerp(vector: VectorLike, t: number): SlowVector {
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
- const ti = this[i] ?? 0;
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(): MatrixLike;
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(): this;
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(): MatrixLike;
34
+ invert: () => MatrixLike;
35
35
  }