@lakuna/umath 1.3.9 → 1.4.2

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 (162) hide show
  1. package/README.md +1 -1
  2. package/dist/algorithms/approx.d.ts +9 -0
  3. package/dist/algorithms/approx.d.ts.map +1 -0
  4. package/dist/algorithms/approx.js +12 -0
  5. package/dist/algorithms/approx.js.map +1 -0
  6. package/dist/algorithms/approxRelative.d.ts +9 -0
  7. package/dist/algorithms/approxRelative.d.ts.map +1 -0
  8. package/dist/algorithms/approxRelative.js +12 -0
  9. package/dist/algorithms/approxRelative.js.map +1 -0
  10. package/dist/algorithms/combinations.d.ts +2 -1
  11. package/dist/algorithms/combinations.d.ts.map +1 -1
  12. package/dist/algorithms/combinations.js +2 -1
  13. package/dist/algorithms/combinations.js.map +1 -1
  14. package/dist/algorithms/degreesToRadians.d.ts +3 -2
  15. package/dist/algorithms/degreesToRadians.d.ts.map +1 -1
  16. package/dist/algorithms/degreesToRadians.js +5 -3
  17. package/dist/algorithms/degreesToRadians.js.map +1 -1
  18. package/dist/algorithms/factorial.d.ts +2 -1
  19. package/dist/algorithms/factorial.d.ts.map +1 -1
  20. package/dist/algorithms/factorial.js +2 -1
  21. package/dist/algorithms/factorial.js.map +1 -1
  22. package/dist/algorithms/fibonacci.d.ts +2 -1
  23. package/dist/algorithms/fibonacci.d.ts.map +1 -1
  24. package/dist/algorithms/fibonacci.js +6 -2
  25. package/dist/algorithms/fibonacci.js.map +1 -1
  26. package/dist/algorithms/greatestCommonDivisor.d.ts +4 -2
  27. package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
  28. package/dist/algorithms/greatestCommonDivisor.js +5 -2
  29. package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
  30. package/dist/algorithms/hypergeometricPmf.d.ts +3 -2
  31. package/dist/algorithms/hypergeometricPmf.d.ts.map +1 -1
  32. package/dist/algorithms/hypergeometricPmf.js +3 -2
  33. package/dist/algorithms/hypergeometricPmf.js.map +1 -1
  34. package/dist/algorithms/isPrime.d.ts +2 -1
  35. package/dist/algorithms/isPrime.d.ts.map +1 -1
  36. package/dist/algorithms/isPrime.js +2 -1
  37. package/dist/algorithms/isPrime.js.map +1 -1
  38. package/dist/algorithms/permutations.d.ts +2 -1
  39. package/dist/algorithms/permutations.d.ts.map +1 -1
  40. package/dist/algorithms/permutations.js +2 -1
  41. package/dist/algorithms/permutations.js.map +1 -1
  42. package/dist/algorithms/primeFactorization.d.ts +2 -1
  43. package/dist/algorithms/primeFactorization.d.ts.map +1 -1
  44. package/dist/algorithms/primeFactorization.js +2 -1
  45. package/dist/algorithms/primeFactorization.js.map +1 -1
  46. package/dist/algorithms/radiansToDegrees.d.ts +3 -2
  47. package/dist/algorithms/radiansToDegrees.d.ts.map +1 -1
  48. package/dist/algorithms/radiansToDegrees.js +5 -3
  49. package/dist/algorithms/radiansToDegrees.js.map +1 -1
  50. package/dist/algorithms/summation.d.ts +2 -1
  51. package/dist/algorithms/summation.d.ts.map +1 -1
  52. package/dist/algorithms/summation.js +2 -1
  53. package/dist/algorithms/summation.js.map +1 -1
  54. package/dist/linalg/DualQuaternion.d.ts +102 -32
  55. package/dist/linalg/DualQuaternion.d.ts.map +1 -1
  56. package/dist/linalg/DualQuaternion.js +243 -272
  57. package/dist/linalg/DualQuaternion.js.map +1 -1
  58. package/dist/linalg/Matrix.d.ts +14 -10
  59. package/dist/linalg/Matrix.d.ts.map +1 -1
  60. package/dist/linalg/Matrix2.d.ts +74 -51
  61. package/dist/linalg/Matrix2.d.ts.map +1 -1
  62. package/dist/linalg/Matrix2.js +95 -171
  63. package/dist/linalg/Matrix2.js.map +1 -1
  64. package/dist/linalg/Matrix3.d.ts +122 -72
  65. package/dist/linalg/Matrix3.d.ts.map +1 -1
  66. package/dist/linalg/Matrix3.js +186 -355
  67. package/dist/linalg/Matrix3.js.map +1 -1
  68. package/dist/linalg/Matrix4.d.ts +266 -149
  69. package/dist/linalg/Matrix4.d.ts.map +1 -1
  70. package/dist/linalg/Matrix4.js +512 -852
  71. package/dist/linalg/Matrix4.js.map +1 -1
  72. package/dist/linalg/Quaternion.d.ts +252 -34
  73. package/dist/linalg/Quaternion.d.ts.map +1 -1
  74. package/dist/linalg/Quaternion.js +436 -166
  75. package/dist/linalg/Quaternion.js.map +1 -1
  76. package/dist/linalg/SlowMatrix.d.ts +10 -9
  77. package/dist/linalg/SlowMatrix.d.ts.map +1 -1
  78. package/dist/linalg/SlowMatrix.js +10 -9
  79. package/dist/linalg/SlowMatrix.js.map +1 -1
  80. package/dist/linalg/SlowSquareMatrix.d.ts +10 -9
  81. package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
  82. package/dist/linalg/SlowSquareMatrix.js +10 -9
  83. package/dist/linalg/SlowSquareMatrix.js.map +1 -1
  84. package/dist/linalg/SquareMatrix.d.ts +6 -5
  85. package/dist/linalg/SquareMatrix.d.ts.map +1 -1
  86. package/dist/linalg/Vector.d.ts +8 -4
  87. package/dist/linalg/Vector.d.ts.map +1 -1
  88. package/dist/linalg/Vector2.d.ts +82 -31
  89. package/dist/linalg/Vector2.d.ts.map +1 -1
  90. package/dist/linalg/Vector2.js +112 -154
  91. package/dist/linalg/Vector2.js.map +1 -1
  92. package/dist/linalg/Vector3.d.ts +93 -41
  93. package/dist/linalg/Vector3.d.ts.map +1 -1
  94. package/dist/linalg/Vector3.js +171 -282
  95. package/dist/linalg/Vector3.js.map +1 -1
  96. package/dist/linalg/Vector4.d.ts +71 -21
  97. package/dist/linalg/Vector4.d.ts.map +1 -1
  98. package/dist/linalg/Vector4.js +121 -195
  99. package/dist/linalg/Vector4.js.map +1 -1
  100. package/dist/types/AxisAngle.d.ts +4 -1
  101. package/dist/types/AxisAngle.d.ts.map +1 -1
  102. package/dist/types/FieldOfView.d.ts +4 -1
  103. package/dist/types/FieldOfView.d.ts.map +1 -1
  104. package/dist/utility/BigNumber.d.ts +4 -1
  105. package/dist/utility/BigNumber.d.ts.map +1 -1
  106. package/dist/utility/BigNumber.js +4 -1
  107. package/dist/utility/BigNumber.js.map +1 -1
  108. package/dist/utility/MagnitudeError.d.ts +4 -1
  109. package/dist/utility/MagnitudeError.d.ts.map +1 -1
  110. package/dist/utility/MagnitudeError.js +4 -1
  111. package/dist/utility/MagnitudeError.js.map +1 -1
  112. package/dist/utility/MatrixSizeError.d.ts +4 -1
  113. package/dist/utility/MatrixSizeError.d.ts.map +1 -1
  114. package/dist/utility/MatrixSizeError.js +4 -1
  115. package/dist/utility/MatrixSizeError.js.map +1 -1
  116. package/dist/utility/PartialMatrixError.d.ts +4 -1
  117. package/dist/utility/PartialMatrixError.d.ts.map +1 -1
  118. package/dist/utility/PartialMatrixError.js +4 -1
  119. package/dist/utility/PartialMatrixError.js.map +1 -1
  120. package/dist/utility/SingularMatrixError.d.ts +2 -1
  121. package/dist/utility/SingularMatrixError.d.ts.map +1 -1
  122. package/dist/utility/SingularMatrixError.js +2 -1
  123. package/dist/utility/SingularMatrixError.js.map +1 -1
  124. package/dist/utility/epsilon.d.ts +4 -1
  125. package/dist/utility/epsilon.d.ts.map +1 -1
  126. package/dist/utility/epsilon.js +4 -1
  127. package/dist/utility/epsilon.js.map +1 -1
  128. package/package.json +11 -11
  129. package/src/algorithms/approx.ts +12 -0
  130. package/src/algorithms/approxRelative.ts +12 -0
  131. package/src/algorithms/combinations.ts +2 -1
  132. package/src/algorithms/degreesToRadians.ts +6 -3
  133. package/src/algorithms/factorial.ts +3 -1
  134. package/src/algorithms/fibonacci.ts +7 -2
  135. package/src/algorithms/greatestCommonDivisor.ts +9 -4
  136. package/src/algorithms/hypergeometricPmf.ts +3 -2
  137. package/src/algorithms/isPrime.ts +2 -1
  138. package/src/algorithms/permutations.ts +2 -1
  139. package/src/algorithms/primeFactorization.ts +2 -1
  140. package/src/algorithms/radiansToDegrees.ts +6 -3
  141. package/src/algorithms/summation.ts +2 -1
  142. package/src/linalg/DualQuaternion.ts +424 -289
  143. package/src/linalg/Matrix.ts +14 -10
  144. package/src/linalg/Matrix2.ts +141 -188
  145. package/src/linalg/Matrix3.ts +400 -375
  146. package/src/linalg/Matrix4.ts +1083 -905
  147. package/src/linalg/Quaternion.ts +706 -188
  148. package/src/linalg/SlowMatrix.ts +10 -9
  149. package/src/linalg/SlowSquareMatrix.ts +10 -9
  150. package/src/linalg/SquareMatrix.ts +6 -5
  151. package/src/linalg/Vector.ts +8 -4
  152. package/src/linalg/Vector2.ts +146 -173
  153. package/src/linalg/Vector3.ts +293 -326
  154. package/src/linalg/Vector4.ts +227 -215
  155. package/src/types/AxisAngle.ts +4 -1
  156. package/src/types/FieldOfView.ts +4 -1
  157. package/src/utility/BigNumber.ts +6 -3
  158. package/src/utility/MagnitudeError.ts +4 -1
  159. package/src/utility/MatrixSizeError.ts +4 -1
  160. package/src/utility/PartialMatrixError.ts +4 -1
  161. package/src/utility/SingularMatrixError.ts +2 -1
  162. package/src/utility/epsilon.ts +4 -1
@@ -1,9 +1,12 @@
1
1
  import type { default as Vector, VectorLike } from "./Vector.js";
2
2
  import type { Matrix4Like } from "./Matrix4.js";
3
3
  import type { QuaternionLike } from "./Quaternion.js";
4
- import epsilon from "../utility/epsilon.js";
4
+ import approxRelative from "../algorithms/approxRelative.js";
5
5
 
6
- /** A quantity with magnitude and direction in four dimensions. */
6
+ /**
7
+ * A quantity with magnitude and direction in four dimensions.
8
+ * @public
9
+ */
7
10
  export interface Vector4Like extends VectorLike {
8
11
  /** The first component of this vector. */
9
12
  0: number;
@@ -21,8 +24,9 @@ export interface Vector4Like extends VectorLike {
21
24
  /**
22
25
  * Creates a 4x1 vector-like object.
23
26
  * @returns A 4x1 vector-like object.
27
+ * @public
24
28
  */
25
- export const createVector4Like = () => {
29
+ export const createVector4Like = (): Float32Array & Vector4Like => {
26
30
  return new Float32Array(4) as Float32Array & Vector4Like;
27
31
  };
28
32
 
@@ -34,6 +38,7 @@ export const createVector4Like = () => {
34
38
  * @param w - The fourth component.
35
39
  * @param out - The vector to store the result in.
36
40
  * @returns A new vector.
41
+ * @public
37
42
  */
38
43
  export const fromValues = <T extends Vector4Like>(
39
44
  x: number,
@@ -54,35 +59,23 @@ export const fromValues = <T extends Vector4Like>(
54
59
  * @param a - The first vector.
55
60
  * @param b - The second vector.
56
61
  * @returns Whether or not the vectors are equivalent.
62
+ * @public
57
63
  */
58
- export const equals = (a: Vector4Like, b: Vector4Like): boolean => {
59
- const a0 = a[0];
60
- const a1 = a[1];
61
- const a2 = a[2];
62
- const a3 = a[3];
63
-
64
- const b0 = b[0];
65
- const b1 = b[1];
66
- const b2 = b[2];
67
- const b3 = b[3];
68
-
69
- return (
70
- Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0)) &&
71
- Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1)) &&
72
- Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2)) &&
73
- Math.abs(a3 - b3) <= epsilon * Math.max(1, Math.abs(a3), Math.abs(b3))
74
- );
75
- };
64
+ export const equals = (a: Vector4Like, b: Vector4Like): boolean =>
65
+ approxRelative(a[0], b[0]) &&
66
+ approxRelative(a[1], b[1]) &&
67
+ approxRelative(a[2], b[2]) &&
68
+ approxRelative(a[3], b[3]);
76
69
 
77
70
  /**
78
71
  * Determine whether or not two vectors are exactly equivalent.
79
72
  * @param a - The first vector.
80
73
  * @param b - The second vector.
81
74
  * @returns Whether or not the vectors are equivalent.
75
+ * @public
82
76
  */
83
- export const exactEquals = (a: Vector4Like, b: Vector4Like): boolean => {
84
- return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
85
- };
77
+ export const exactEquals = (a: Vector4Like, b: Vector4Like): boolean =>
78
+ a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
86
79
 
87
80
  /**
88
81
  * Add two vectors.
@@ -90,32 +83,23 @@ export const exactEquals = (a: Vector4Like, b: Vector4Like): boolean => {
90
83
  * @param b - The addend.
91
84
  * @param out - The vector to store the result in.
92
85
  * @returns The sum.
86
+ * @public
93
87
  */
94
88
  export const add = <T extends Vector4Like>(
95
89
  a: Vector4Like,
96
90
  b: Vector4Like,
97
91
  out: T
98
- ): T => {
99
- out[0] = a[0] + b[0];
100
- out[1] = a[1] + b[1];
101
- out[2] = a[2] + b[2];
102
- out[3] = a[3] + b[3];
103
- return out;
104
- };
92
+ ): T => fromValues(a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3], out);
105
93
 
106
94
  /**
107
95
  * Copy the values from one vector to another.
108
96
  * @param vector - The vector to copy.
109
97
  * @param out - The vector to store the result in.
110
98
  * @returns The copy.
99
+ * @public
111
100
  */
112
- export const copy = <T extends Vector4Like>(vector: Vector4Like, out: T): T => {
113
- out[0] = vector[0];
114
- out[1] = vector[1];
115
- out[2] = vector[2];
116
- out[3] = vector[3];
117
- return out;
118
- };
101
+ export const copy = <T extends Vector4Like>(vector: Vector4Like, out: T): T =>
102
+ fromValues(vector[0], vector[1], vector[2], vector[3], out);
119
103
 
120
104
  /**
121
105
  * Multiply two vectors.
@@ -123,18 +107,13 @@ export const copy = <T extends Vector4Like>(vector: Vector4Like, out: T): T => {
123
107
  * @param b - The multiplicand.
124
108
  * @param out - The vector to store the result in.
125
109
  * @returns The product.
110
+ * @public
126
111
  */
127
112
  export const multiply = <T extends Vector4Like>(
128
113
  a: Vector4Like,
129
114
  b: Vector4Like,
130
115
  out: T
131
- ): T => {
132
- out[0] = a[0] * b[0];
133
- out[1] = a[1] * b[1];
134
- out[2] = a[2] * b[2];
135
- out[3] = a[3] * b[3];
136
- return out;
137
- };
116
+ ): T => fromValues(a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3], out);
138
117
 
139
118
  /**
140
119
  * Divide two vectors.
@@ -142,18 +121,13 @@ export const multiply = <T extends Vector4Like>(
142
121
  * @param b - The divisor.
143
122
  * @param out - The vector to store the result in.
144
123
  * @returns The quotient.
124
+ * @public
145
125
  */
146
126
  export const divide = <T extends Vector4Like>(
147
127
  a: Vector4Like,
148
128
  b: Vector4Like,
149
129
  out: T
150
- ): T => {
151
- out[0] = a[0] / b[0];
152
- out[1] = a[1] / b[1];
153
- out[2] = a[2] / b[2];
154
- out[3] = a[3] / b[3];
155
- return out;
156
- };
130
+ ): T => fromValues(a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3], out);
157
131
 
158
132
  /**
159
133
  * Subtract two vectors.
@@ -161,66 +135,77 @@ export const divide = <T extends Vector4Like>(
161
135
  * @param b - The subtrahend.
162
136
  * @param out - The vector to store the result in.
163
137
  * @returns The difference.
138
+ * @public
164
139
  */
165
140
  export const subtract = <T extends Vector4Like>(
166
141
  a: Vector4Like,
167
142
  b: Vector4Like,
168
143
  out: T
169
- ): T => {
170
- out[0] = a[0] - b[0];
171
- out[1] = a[1] - b[1];
172
- out[2] = a[2] - b[2];
173
- out[3] = a[3] - b[3];
174
- return out;
175
- };
144
+ ): T => fromValues(a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3], out);
145
+
146
+ /**
147
+ * Absolutize the components of a vector.
148
+ * @param vector - The vector.
149
+ * @param out - The vector to store the result in.
150
+ * @returns The absolutized vector.
151
+ * @public
152
+ */
153
+ export const abs = <T extends Vector4Like>(vector: Vector4Like, out: T): T =>
154
+ fromValues(
155
+ Math.abs(vector[0]),
156
+ Math.abs(vector[1]),
157
+ Math.abs(vector[2]),
158
+ Math.abs(vector[3]),
159
+ out
160
+ );
176
161
 
177
162
  /**
178
163
  * Round up the components of a vector.
179
164
  * @param vector - The vector.
180
165
  * @param out - The vector to store the result in.
181
166
  * @returns The rounded vector.
167
+ * @public
182
168
  */
183
- export const ceil = <T extends Vector4Like>(vector: Vector4Like, out: T): T => {
184
- out[0] = Math.ceil(vector[0]);
185
- out[1] = Math.ceil(vector[1]);
186
- out[2] = Math.ceil(vector[2]);
187
- out[3] = Math.ceil(vector[3]);
188
- return out;
189
- };
169
+ export const ceil = <T extends Vector4Like>(vector: Vector4Like, out: T): T =>
170
+ fromValues(
171
+ Math.ceil(vector[0]),
172
+ Math.ceil(vector[1]),
173
+ Math.ceil(vector[2]),
174
+ Math.ceil(vector[3]),
175
+ out
176
+ );
190
177
 
191
178
  /**
192
179
  * Round down the components of a vector.
193
180
  * @param vector - The vector.
194
181
  * @param out - The vector to store the result in.
195
182
  * @returns The rounded vector.
183
+ * @public
196
184
  */
197
- export const floor = <T extends Vector4Like>(
198
- vector: Vector4Like,
199
- out: T
200
- ): T => {
201
- out[0] = Math.floor(vector[0]);
202
- out[1] = Math.floor(vector[1]);
203
- out[2] = Math.floor(vector[2]);
204
- out[3] = Math.floor(vector[3]);
205
- return out;
206
- };
185
+ export const floor = <T extends Vector4Like>(vector: Vector4Like, out: T): T =>
186
+ fromValues(
187
+ Math.floor(vector[0]),
188
+ Math.floor(vector[1]),
189
+ Math.floor(vector[2]),
190
+ Math.floor(vector[3]),
191
+ out
192
+ );
207
193
 
208
194
  /**
209
195
  * Round the components of a vector.
210
196
  * @param vector - The vector.
211
197
  * @param out - The vector to store the result in.
212
198
  * @returns The rounded vector.
199
+ * @public
213
200
  */
214
- export const round = <T extends Vector4Like>(
215
- vector: Vector4Like,
216
- out: T
217
- ): T => {
218
- out[0] = Math.round(vector[0]);
219
- out[1] = Math.round(vector[1]);
220
- out[2] = Math.round(vector[2]);
221
- out[3] = Math.round(vector[3]);
222
- return out;
223
- };
201
+ export const round = <T extends Vector4Like>(vector: Vector4Like, out: T): T =>
202
+ fromValues(
203
+ Math.round(vector[0]),
204
+ Math.round(vector[1]),
205
+ Math.round(vector[2]),
206
+ Math.round(vector[3]),
207
+ out
208
+ );
224
209
 
225
210
  /**
226
211
  * Return the minimum of two vectors.
@@ -228,18 +213,20 @@ export const round = <T extends Vector4Like>(
228
213
  * @param b - The second vector.
229
214
  * @param out - The vector to store the result in.
230
215
  * @returns The minimum.
216
+ * @public
231
217
  */
232
218
  export const min = <T extends Vector4Like>(
233
219
  a: Vector4Like,
234
220
  b: Vector4Like,
235
221
  out: T
236
- ): T => {
237
- out[0] = Math.min(a[0], b[0]);
238
- out[1] = Math.min(a[1], b[1]);
239
- out[2] = Math.min(a[2], b[2]);
240
- out[3] = Math.min(a[3], b[3]);
241
- return out;
242
- };
222
+ ): T =>
223
+ fromValues(
224
+ Math.min(a[0], b[0]),
225
+ Math.min(a[1], b[1]),
226
+ Math.min(a[2], b[2]),
227
+ Math.min(a[3], b[3]),
228
+ out
229
+ );
243
230
 
244
231
  /**
245
232
  * Return the maximum of two vectors.
@@ -247,18 +234,20 @@ export const min = <T extends Vector4Like>(
247
234
  * @param b - The second vector.
248
235
  * @param out - The vector to store the result in.
249
236
  * @returns The maximum.
237
+ * @public
250
238
  */
251
239
  export const max = <T extends Vector4Like>(
252
240
  a: Vector4Like,
253
241
  b: Vector4Like,
254
242
  out: T
255
- ): T => {
256
- out[0] = Math.max(a[0], b[0]);
257
- out[1] = Math.max(a[1], b[1]);
258
- out[2] = Math.max(a[2], b[2]);
259
- out[3] = Math.max(a[3], b[3]);
260
- return out;
261
- };
243
+ ): T =>
244
+ fromValues(
245
+ Math.max(a[0], b[0]),
246
+ Math.max(a[1], b[1]),
247
+ Math.max(a[2], b[2]),
248
+ Math.max(a[3], b[3]),
249
+ out
250
+ );
262
251
 
263
252
  /**
264
253
  * Scale a vector by a scalar.
@@ -266,18 +255,20 @@ export const max = <T extends Vector4Like>(
266
255
  * @param scalar - The multiplicand.
267
256
  * @param out - The vector to store the result in.
268
257
  * @returns The product.
258
+ * @public
269
259
  */
270
260
  export const scale = <T extends Vector4Like>(
271
261
  vector: Vector4Like,
272
262
  scalar: number,
273
263
  out: T
274
- ): T => {
275
- out[0] = vector[0] * scalar;
276
- out[1] = vector[1] * scalar;
277
- out[2] = vector[2] * scalar;
278
- out[3] = vector[3] * scalar;
279
- return out;
280
- };
264
+ ): T =>
265
+ fromValues(
266
+ vector[0] * scalar,
267
+ vector[1] * scalar,
268
+ vector[2] * scalar,
269
+ vector[3] * scalar,
270
+ out
271
+ );
281
272
 
282
273
  /**
283
274
  * Add two vectors after scaling the second by a scalar.
@@ -286,33 +277,36 @@ export const scale = <T extends Vector4Like>(
286
277
  * @param scalar - The multiplicand.
287
278
  * @param out - The vector to store the result in.
288
279
  * @returns The sum.
280
+ * @public
289
281
  */
290
282
  export const scaleAndAdd = <T extends Vector4Like>(
291
283
  a: Vector4Like,
292
284
  b: Vector4Like,
293
285
  scalar: number,
294
286
  out: T
295
- ): T => {
296
- out[0] = a[0] + b[0] * scalar;
297
- out[1] = a[1] + b[1] * scalar;
298
- out[2] = a[2] + b[2] * scalar;
299
- out[3] = a[3] + b[3] * scalar;
300
- return out;
301
- };
287
+ ): T =>
288
+ fromValues(
289
+ a[0] + b[0] * scalar,
290
+ a[1] + b[1] * scalar,
291
+ a[2] + b[2] * scalar,
292
+ a[3] + b[3] * scalar,
293
+ out
294
+ );
302
295
 
303
296
  /**
304
297
  * Calculate the Euclidean distance between two vectors.
305
298
  * @param a - The first vector
306
299
  * @param b - The second vector.
307
300
  * @returns The distance.
308
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
301
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
302
+ * @public
309
303
  */
310
304
  export const distance = (a: Vector4Like, b: Vector4Like): number => {
311
305
  const x = a[0] - b[0];
312
306
  const y = a[1] - b[1];
313
307
  const z = a[2] - b[2];
314
308
  const w = a[3] - b[3];
315
- return Math.sqrt(x * x + y * y + z * z + w * w);
309
+ return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
316
310
  };
317
311
 
318
312
  /**
@@ -320,7 +314,8 @@ export const distance = (a: Vector4Like, b: Vector4Like): number => {
320
314
  * @param a - The first vector.
321
315
  * @param b - The second vector.
322
316
  * @returns The squared distance.
323
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
317
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
318
+ * @public
324
319
  */
325
320
  export const squaredDistance = (a: Vector4Like, b: Vector4Like): number => {
326
321
  const x = a[0] - b[0];
@@ -334,19 +329,21 @@ export const squaredDistance = (a: Vector4Like, b: Vector4Like): number => {
334
329
  * Calculate the magnitude (length) of a vector.
335
330
  * @param vector - The vector.
336
331
  * @returns The magnitude.
332
+ * @public
337
333
  */
338
334
  export const getMagnitude = (vector: Vector4Like): number => {
339
335
  const x = vector[0];
340
336
  const y = vector[1];
341
337
  const z = vector[2];
342
338
  const w = vector[3];
343
- return Math.sqrt(x * x + y * y + z * z + w * w);
339
+ return Math.sqrt(x * x + y * y + z * z + w * w); // `Math.hypot` is slower.
344
340
  };
345
341
 
346
342
  /**
347
343
  * Calculate the squared magnitude (length) of a vector.
348
344
  * @param vector - The vector.
349
345
  * @returns The magnitude.
346
+ * @public
350
347
  */
351
348
  export const getSquaredMagnitude = (vector: Vector4Like): number => {
352
349
  const x = vector[0];
@@ -361,41 +358,28 @@ export const getSquaredMagnitude = (vector: Vector4Like): number => {
361
358
  * @param vector - The vector.
362
359
  * @param out - The vector to store the result in.
363
360
  * @returns The negated vector.
361
+ * @public
364
362
  */
365
- export const negate = <T extends Vector4Like>(
366
- vector: Vector4Like,
367
- out: T
368
- ): T => {
369
- out[0] = -vector[0];
370
- out[1] = -vector[1];
371
- out[2] = -vector[2];
372
- out[3] = -vector[3];
373
- return out;
374
- };
363
+ export const negate = <T extends Vector4Like>(vector: Vector4Like, out: T): T =>
364
+ fromValues(-vector[0], -vector[1], -vector[2], -vector[3], out);
375
365
 
376
366
  /**
377
367
  * Calculate the multiplicative inverse of the components of a vector.
378
368
  * @param vector - The vector.
379
369
  * @param out - The vector to store the result in.
380
370
  * @returns The inverted vector.
371
+ * @public
381
372
  */
382
- export const invert = <T extends Vector4Like>(
383
- vector: Vector4Like,
384
- out: T
385
- ): T => {
386
- out[0] = 1 / vector[0];
387
- out[1] = 1 / vector[1];
388
- out[2] = 1 / vector[2];
389
- out[3] = 1 / vector[3];
390
- return out;
391
- };
373
+ export const invert = <T extends Vector4Like>(vector: Vector4Like, out: T): T =>
374
+ fromValues(1 / vector[0], 1 / vector[1], 1 / vector[2], 1 / vector[3], out);
392
375
 
393
376
  /**
394
377
  * Normalize a vector.
395
378
  * @param vector - The vector.
396
379
  * @param out - The vector to store the result in.
397
380
  * @returns The normalized vector.
398
- * @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
381
+ * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
382
+ * @public
399
383
  */
400
384
  export const normalize = <T extends Vector4Like>(
401
385
  vector: Vector4Like,
@@ -411,11 +395,7 @@ export const normalize = <T extends Vector4Like>(
411
395
  len = 1 / Math.sqrt(len);
412
396
  }
413
397
 
414
- out[0] = x * len;
415
- out[1] = y * len;
416
- out[2] = z * len;
417
- out[3] = w * len;
418
- return out;
398
+ return fromValues(x * len, y * len, z * len, w * len, out);
419
399
  };
420
400
 
421
401
  /**
@@ -423,11 +403,11 @@ export const normalize = <T extends Vector4Like>(
423
403
  * @param a - The multiplier.
424
404
  * @param b - The multiplicand.
425
405
  * @returns The dot product.
426
- * @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
406
+ * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
407
+ * @public
427
408
  */
428
- export const dot = (a: Vector4Like, b: Vector4Like): number => {
429
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
430
- };
409
+ export const dot = (a: Vector4Like, b: Vector4Like): number =>
410
+ a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
431
411
 
432
412
  /**
433
413
  * Calculate the cross product of three vectors in a four-dimensional space.
@@ -436,7 +416,8 @@ export const dot = (a: Vector4Like, b: Vector4Like): number => {
436
416
  * @param c - The third vector.
437
417
  * @param out - The vector to store the result in.
438
418
  * @returns The cross product.
439
- * @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
419
+ * @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
420
+ * @public
440
421
  */
441
422
  export const cross = <T extends Vector4Like>(
442
423
  a: Vector4Like,
@@ -444,23 +425,35 @@ export const cross = <T extends Vector4Like>(
444
425
  c: Vector4Like,
445
426
  out: T
446
427
  ): T => {
447
- const d = b[0] * c[1] - b[1] * c[0];
448
- const e = b[0] * c[2] - b[2] * c[0];
449
- const f = b[0] * c[3] - b[3] * c[0];
450
- const g = b[1] * c[2] - b[2] * c[1];
451
- const h = b[1] * c[3] - b[3] * c[1];
452
- const i = b[2] * c[3] - b[3] * c[2];
453
-
454
- const j = a[0];
455
- const k = a[1];
456
- const l = a[2];
457
- const m = a[3];
458
-
459
- out[0] = k * i - l * h + m * g;
460
- out[1] = -(j * i) + l * f - m * e;
461
- out[2] = j * h - k * f + m * d;
462
- out[3] = -(j * g) + k * e - l * d;
463
- return out;
428
+ const a0 = a[0];
429
+ const a1 = a[1];
430
+ const a2 = a[2];
431
+ const a3 = a[3];
432
+
433
+ const b0 = b[0];
434
+ const b1 = b[1];
435
+ const b2 = b[2];
436
+ const b3 = b[3];
437
+
438
+ const c0 = c[0];
439
+ const c1 = c[1];
440
+ const c2 = c[2];
441
+ const c3 = c[3];
442
+
443
+ const d = b0 * c1 - b1 * c0;
444
+ const e = b0 * c2 - b2 * c0;
445
+ const f = b0 * c3 - b3 * c0;
446
+ const g = b1 * c2 - b2 * c1;
447
+ const h = b1 * c3 - b3 * c1;
448
+ const i = b2 * c3 - b3 * c2;
449
+
450
+ return fromValues(
451
+ a1 * i - a2 * h + a3 * g,
452
+ -(a0 * i) + a2 * f - a3 * e,
453
+ a0 * h - a1 * f + a3 * d,
454
+ -(a0 * g) + a1 * e - a2 * d,
455
+ out
456
+ );
464
457
  };
465
458
 
466
459
  /**
@@ -470,7 +463,8 @@ export const cross = <T extends Vector4Like>(
470
463
  * @param t - The interpolation amount (in `[0,1]`).
471
464
  * @param out - The vector to store the result in.
472
465
  * @returns The interpolated vector.
473
- * @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
466
+ * @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
467
+ * @public
474
468
  */
475
469
  export const lerp = <T extends Vector4Like>(
476
470
  a: Vector4Like,
@@ -483,11 +477,13 @@ export const lerp = <T extends Vector4Like>(
483
477
  const az = a[2];
484
478
  const aw = a[3];
485
479
 
486
- out[0] = ax + t * (b[0] - ax);
487
- out[1] = ay + t * (b[1] - ay);
488
- out[2] = az + t * (b[2] - az);
489
- out[3] = aw + t * (b[3] - aw);
490
- return out;
480
+ return fromValues(
481
+ ax + t * (b[0] - ax),
482
+ ay + t * (b[1] - ay),
483
+ az + t * (b[2] - az),
484
+ aw + t * (b[3] - aw),
485
+ out
486
+ );
491
487
  };
492
488
 
493
489
  /**
@@ -495,25 +491,27 @@ export const lerp = <T extends Vector4Like>(
495
491
  * @param magnitude - The magnitude.
496
492
  * @param out - The vector to store the result in.
497
493
  * @returns This vector.
494
+ * @see {@link https://github.com/toji/gl-matrix/pull/458 | toji/gl-matrix#458}
495
+ * @public
498
496
  */
499
497
  export const random = <T extends Vector4Like>(magnitude: number, out: T): T => {
500
498
  const a = Math.random();
501
499
  const v1 = a * 2 - 1;
502
500
  const v2 = (4 * Math.random() - 2) * Math.sqrt(a * -a + a);
503
- const s1 = v1 * v1 + v2 * v2;
504
501
 
505
502
  const b = Math.random();
506
503
  const v3 = b * 2 - 1;
507
504
  const v4 = (4 * Math.random() - 2) * Math.sqrt(b * -b + b);
508
- const s2 = v3 * v3 + v4 * v4;
509
505
 
510
- const d = Math.sqrt((1 - s1) / s2);
506
+ const d = Math.sqrt((1 - (v1 * v1 + v2 * v2)) / (v3 * v3 + v4 * v4));
511
507
 
512
- out[0] = magnitude * v1;
513
- out[1] = magnitude * v2;
514
- out[2] = magnitude * v3 * d;
515
- out[3] = magnitude * v4 * d;
516
- return out;
508
+ return fromValues(
509
+ magnitude * v1,
510
+ magnitude * v2,
511
+ magnitude * v3 * d,
512
+ magnitude * v4 * d,
513
+ out
514
+ );
517
515
  };
518
516
 
519
517
  /**
@@ -522,7 +520,8 @@ export const random = <T extends Vector4Like>(magnitude: number, out: T): T => {
522
520
  * @param matrix - The matrix (multiplicand).
523
521
  * @param out - The vector to store the result in.
524
522
  * @returns The transformed vector.
525
- * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
523
+ * @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
524
+ * @public
526
525
  */
527
526
  export const transformMatrix4 = <T extends Vector4Like>(
528
527
  vector: Vector4Like,
@@ -534,33 +533,33 @@ export const transformMatrix4 = <T extends Vector4Like>(
534
533
  const z = vector[2];
535
534
  const w = vector[3];
536
535
 
537
- out[0] = matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12] * w;
538
- out[1] = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13] * w;
539
- out[2] = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14] * w;
540
- out[3] = matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15] * w;
541
- return out;
536
+ return fromValues(
537
+ matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12] * w,
538
+ matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13] * w,
539
+ matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14] * w,
540
+ matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15] * w,
541
+ out
542
+ );
542
543
  };
543
544
 
544
545
  /**
545
546
  * Set a vector to the zero vector.
546
547
  * @param out - The vector to store the result in.
547
548
  * @returns This vector.
549
+ * @public
548
550
  */
549
- export const zero = <T extends Vector4Like>(out: T): T => {
550
- out[0] = 0;
551
- out[1] = 0;
552
- out[2] = 0;
553
- out[3] = 0;
554
- return out;
555
- };
551
+ export const zero = <T extends Vector4Like>(out: T): T =>
552
+ fromValues(0, 0, 0, 0, out);
556
553
 
557
554
  /**
558
- * Transform a vector by a quaternion.
555
+ * Transform a vector by a unit quaternion.
559
556
  * @param vector - The vector.
560
- * @param quaternion - The quaternion.
557
+ * @param quaternion - The unit quaternion.
561
558
  * @param out - The vector to store the result in.
562
559
  * @returns The transformed vector.
563
- * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
560
+ * @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
561
+ * @see {@link https://raw.org/proof/vector-rotation-using-quaternions/ | Fast Vector Rotation using Quaternions}
562
+ * @public
564
563
  */
565
564
  export const transformQuaternion = <T extends Vector4Like>(
566
565
  vector: Vector4Like,
@@ -576,21 +575,23 @@ export const transformQuaternion = <T extends Vector4Like>(
576
575
  const qz = quaternion[2];
577
576
  const qw = quaternion[3];
578
577
 
579
- const ix = qw * x + qy * z - qz * y;
580
- const iy = qw * y + qz * x - qx * z;
581
- const iz = qw * z + qx * y - qy * x;
582
- const iw = -qx * x - qy * y - qz * z;
578
+ const tx = (qy * z - qz * y) * 2;
579
+ const ty = (qz * x - qx * z) * 2;
580
+ const tz = (qx * y - qy * x) * 2;
583
581
 
584
- out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
585
- out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
586
- out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
587
- out[3] = vector[3];
588
- return out;
582
+ return fromValues(
583
+ x + qw * tx + qy * tz - qz * ty,
584
+ y + qw * ty + qz * tx - qx * tz,
585
+ z + qw * tz + qx * ty - qy * tx,
586
+ vector[3],
587
+ out
588
+ );
589
589
  };
590
590
 
591
591
  /**
592
592
  * A quantity with magnitude and direction in four dimensions.
593
- * @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
593
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_vector | Euclidean vector}
594
+ * @public
594
595
  */
595
596
  export default class Vector4
596
597
  extends Float32Array
@@ -617,7 +618,7 @@ export default class Vector4
617
618
 
618
619
  /**
619
620
  * Create a four-dimensional zero vector.
620
- * @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
621
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_vector | Euclidean vector}
621
622
  */
622
623
  public constructor() {
623
624
  super(4);
@@ -725,6 +726,17 @@ export default class Vector4
725
726
  return subtract(this, vector, out);
726
727
  }
727
728
 
729
+ /**
730
+ * Absolutize the components of this vector.
731
+ * @param out - The vector to store the result in.
732
+ * @returns The absolutized vector.
733
+ */
734
+ public abs<T extends Vector4Like = Vector4>(
735
+ out: T = new Vector4() as Vector4 & T
736
+ ): T {
737
+ return abs(this, out);
738
+ }
739
+
728
740
  /**
729
741
  * Round up the components of this vector.
730
742
  * @param out - The vector to store the result in.
@@ -816,7 +828,7 @@ export default class Vector4
816
828
  * Calculate the Euclidean distance between this vector and another.
817
829
  * @param vector - The other vector.
818
830
  * @returns The distance.
819
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
831
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
820
832
  */
821
833
  public distance(vector: Vector4Like): number {
822
834
  return distance(this, vector);
@@ -826,7 +838,7 @@ export default class Vector4
826
838
  * Calculate the squared Euclidean distance between this vector and another.
827
839
  * @param vector - The other vector.
828
840
  * @returns The squared distance.
829
- * @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
841
+ * @see {@link https://en.wikipedia.org/wiki/Euclidean_distance | Euclidean distance}
830
842
  */
831
843
  public squaredDistance(vector: Vector4Like): number {
832
844
  return squaredDistance(this, vector);
@@ -868,7 +880,7 @@ export default class Vector4
868
880
  * Normalize this vector.
869
881
  * @param out - The vector to store the result in.
870
882
  * @returns The normalized vector.
871
- * @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
883
+ * @see {@link https://en.wikipedia.org/wiki/Unit_vector | Unit vector}
872
884
  */
873
885
  public normalize<T extends Vector4Like = Vector4>(
874
886
  out: T = new Vector4() as Vector4 & T
@@ -880,7 +892,7 @@ export default class Vector4
880
892
  * Calculate the dot product of this and another vector.
881
893
  * @param vector - The other vector.
882
894
  * @returns The dot product.
883
- * @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
895
+ * @see {@link https://en.wikipedia.org/wiki/Dot_product | Dot product}
884
896
  */
885
897
  public dot(vector: Vector4Like): number {
886
898
  return dot(this, vector);
@@ -892,7 +904,7 @@ export default class Vector4
892
904
  * @param b - The other other vector.
893
905
  * @param out - The vector to store the result in.
894
906
  * @returns The cross product.
895
- * @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
907
+ * @see {@link https://en.wikipedia.org/wiki/Cross_product | Cross product}
896
908
  */
897
909
  public cross<T extends Vector4Like = Vector4>(
898
910
  a: Vector4Like,
@@ -908,7 +920,7 @@ export default class Vector4
908
920
  * @param t - The interpolation amount (in `[0,1]`).
909
921
  * @param out - The vector to store the result in.
910
922
  * @returns The interpolated vector.
911
- * @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
923
+ * @see {@link https://en.wikipedia.org/wiki/Linear_interpolation | Linear interpolation}
912
924
  */
913
925
  public lerp<T extends Vector4Like = Vector4>(
914
926
  vector: Vector4Like,
@@ -932,7 +944,7 @@ export default class Vector4
932
944
  * @param matrix - The matrix.
933
945
  * @param out - The vector to store the result in.
934
946
  * @returns The transformed vector.
935
- * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
947
+ * @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
936
948
  */
937
949
  public transformMatrix4<T extends Vector4Like = Vector4>(
938
950
  matrix: Matrix4Like,
@@ -954,7 +966,7 @@ export default class Vector4
954
966
  * @param quaternion - The quaternion.
955
967
  * @param out - The vector to store the result in.
956
968
  * @returns The transformed vector.
957
- * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
969
+ * @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
958
970
  */
959
971
  public transformQuaternion<T extends Vector4Like = Vector4>(
960
972
  quaternion: QuaternionLike,