@lakuna/umath 0.0.2 → 0.0.4

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 (147) hide show
  1. package/README.md +41 -1
  2. package/dist/algorithms/combinations.d.ts +2 -0
  3. package/dist/algorithms/combinations.d.ts.map +1 -0
  4. package/dist/algorithms/combinations.js +5 -0
  5. package/dist/algorithms/combinations.js.map +1 -0
  6. package/dist/algorithms/degreesToRadians.d.ts +2 -0
  7. package/dist/algorithms/degreesToRadians.d.ts.map +1 -0
  8. package/dist/algorithms/degreesToRadians.js +4 -0
  9. package/dist/algorithms/degreesToRadians.js.map +1 -0
  10. package/dist/algorithms/factorial.d.ts +2 -0
  11. package/dist/algorithms/factorial.d.ts.map +1 -0
  12. package/dist/algorithms/factorial.js +4 -0
  13. package/dist/algorithms/factorial.js.map +1 -0
  14. package/dist/algorithms/fibonacci.d.ts +2 -0
  15. package/dist/algorithms/fibonacci.d.ts.map +1 -0
  16. package/dist/algorithms/fibonacci.js +9 -0
  17. package/dist/algorithms/fibonacci.js.map +1 -0
  18. package/dist/algorithms/greatestCommonDivisor.d.ts +3 -0
  19. package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -0
  20. package/dist/algorithms/greatestCommonDivisor.js +13 -0
  21. package/dist/algorithms/greatestCommonDivisor.js.map +1 -0
  22. package/dist/algorithms/isPrime.d.ts +2 -0
  23. package/dist/algorithms/isPrime.d.ts.map +1 -0
  24. package/dist/algorithms/isPrime.js +12 -0
  25. package/dist/algorithms/isPrime.js.map +1 -0
  26. package/dist/algorithms/permutations.d.ts +2 -0
  27. package/dist/algorithms/permutations.d.ts.map +1 -0
  28. package/dist/algorithms/permutations.js +5 -0
  29. package/dist/algorithms/permutations.js.map +1 -0
  30. package/dist/algorithms/primeFactorization.d.ts +2 -0
  31. package/dist/algorithms/primeFactorization.d.ts.map +1 -0
  32. package/dist/algorithms/primeFactorization.js +15 -0
  33. package/dist/algorithms/primeFactorization.js.map +1 -0
  34. package/dist/algorithms/radiansToDegrees.d.ts +2 -0
  35. package/dist/algorithms/radiansToDegrees.d.ts.map +1 -0
  36. package/dist/algorithms/radiansToDegrees.js +4 -0
  37. package/dist/algorithms/radiansToDegrees.js.map +1 -0
  38. package/dist/algorithms/summation.d.ts +2 -0
  39. package/dist/algorithms/summation.d.ts.map +1 -0
  40. package/dist/algorithms/summation.js +7 -0
  41. package/dist/algorithms/summation.js.map +1 -0
  42. package/dist/index.d.ts +28 -1
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +22 -1
  45. package/dist/index.js.map +1 -1
  46. package/dist/linear/DualQuaternion.d.ts +89 -0
  47. package/dist/linear/DualQuaternion.d.ts.map +1 -0
  48. package/dist/linear/DualQuaternion.js +530 -0
  49. package/dist/linear/DualQuaternion.js.map +1 -0
  50. package/dist/linear/Matrix.d.ts +18 -0
  51. package/dist/linear/Matrix.d.ts.map +1 -0
  52. package/dist/linear/Matrix.js +2 -0
  53. package/dist/linear/Matrix.js.map +1 -0
  54. package/dist/linear/Matrix2.d.ts +66 -0
  55. package/dist/linear/Matrix2.d.ts.map +1 -0
  56. package/dist/linear/Matrix2.js +240 -0
  57. package/dist/linear/Matrix2.js.map +1 -0
  58. package/dist/linear/Matrix3.d.ts +91 -0
  59. package/dist/linear/Matrix3.d.ts.map +1 -0
  60. package/dist/linear/Matrix3.js +533 -0
  61. package/dist/linear/Matrix3.js.map +1 -0
  62. package/dist/linear/Matrix4.d.ts +140 -0
  63. package/dist/linear/Matrix4.d.ts.map +1 -0
  64. package/dist/linear/Matrix4.js +1363 -0
  65. package/dist/linear/Matrix4.js.map +1 -0
  66. package/dist/linear/Quaternion.d.ts +80 -0
  67. package/dist/linear/Quaternion.d.ts.map +1 -0
  68. package/dist/linear/Quaternion.js +371 -0
  69. package/dist/linear/Quaternion.js.map +1 -0
  70. package/dist/linear/SquareMatrix.d.ts +7 -0
  71. package/dist/linear/SquareMatrix.d.ts.map +1 -0
  72. package/dist/linear/SquareMatrix.js +2 -0
  73. package/dist/linear/SquareMatrix.js.map +1 -0
  74. package/dist/linear/Vector.d.ts +30 -0
  75. package/dist/linear/Vector.d.ts.map +1 -0
  76. package/dist/linear/Vector.js +2 -0
  77. package/dist/linear/Vector.js.map +1 -0
  78. package/dist/linear/Vector2.d.ts +96 -0
  79. package/dist/linear/Vector2.d.ts.map +1 -0
  80. package/dist/linear/Vector2.js +293 -0
  81. package/dist/linear/Vector2.js.map +1 -0
  82. package/dist/linear/Vector3.d.ts +111 -0
  83. package/dist/linear/Vector3.d.ts.map +1 -0
  84. package/dist/linear/Vector3.js +466 -0
  85. package/dist/linear/Vector3.js.map +1 -0
  86. package/dist/linear/Vector4.d.ts +86 -0
  87. package/dist/linear/Vector4.d.ts.map +1 -0
  88. package/dist/linear/Vector4.js +348 -0
  89. package/dist/linear/Vector4.js.map +1 -0
  90. package/dist/types/AxisAngle.d.ts +6 -0
  91. package/dist/types/AxisAngle.d.ts.map +1 -0
  92. package/dist/types/AxisAngle.js +2 -0
  93. package/dist/types/AxisAngle.js.map +1 -0
  94. package/dist/types/FieldOfView.d.ts +7 -0
  95. package/dist/types/FieldOfView.d.ts.map +1 -0
  96. package/dist/types/FieldOfView.js +2 -0
  97. package/dist/types/FieldOfView.js.map +1 -0
  98. package/dist/types/IntegerRepresentation.d.ts +2 -0
  99. package/dist/types/IntegerRepresentation.d.ts.map +1 -0
  100. package/dist/types/IntegerRepresentation.js +2 -0
  101. package/dist/types/IntegerRepresentation.js.map +1 -0
  102. package/dist/utility/BigNumber.d.ts +15 -0
  103. package/dist/utility/BigNumber.d.ts.map +1 -0
  104. package/dist/utility/BigNumber.js +75 -0
  105. package/dist/utility/BigNumber.js.map +1 -0
  106. package/dist/utility/MagnitudeError.d.ts +4 -0
  107. package/dist/utility/MagnitudeError.d.ts.map +1 -0
  108. package/dist/utility/MagnitudeError.js +6 -0
  109. package/dist/utility/MagnitudeError.js.map +1 -0
  110. package/dist/utility/SingularMatrixError.d.ts +4 -0
  111. package/dist/utility/SingularMatrixError.d.ts.map +1 -0
  112. package/dist/utility/SingularMatrixError.js +6 -0
  113. package/dist/utility/SingularMatrixError.js.map +1 -0
  114. package/dist/utility/epsilon.d.ts +3 -0
  115. package/dist/utility/epsilon.d.ts.map +1 -0
  116. package/dist/utility/epsilon.js +2 -0
  117. package/dist/utility/epsilon.js.map +1 -0
  118. package/package.json +17 -9
  119. package/src/algorithms/combinations.ts +12 -0
  120. package/src/algorithms/degreesToRadians.ts +10 -0
  121. package/src/algorithms/factorial.ts +9 -0
  122. package/src/algorithms/fibonacci.ts +14 -0
  123. package/src/algorithms/greatestCommonDivisor.ts +30 -0
  124. package/src/algorithms/isPrime.ts +20 -0
  125. package/src/algorithms/permutations.ts +12 -0
  126. package/src/algorithms/primeFactorization.ts +21 -0
  127. package/src/algorithms/radiansToDegrees.ts +10 -0
  128. package/src/algorithms/summation.ts +23 -0
  129. package/src/index.ts +28 -1
  130. package/src/linear/DualQuaternion.ts +1213 -0
  131. package/src/linear/Matrix.ts +106 -0
  132. package/src/linear/Matrix2.ts +709 -0
  133. package/src/linear/Matrix3.ts +1194 -0
  134. package/src/linear/Matrix4.ts +2569 -0
  135. package/src/linear/Quaternion.ts +967 -0
  136. package/src/linear/SquareMatrix.ts +27 -0
  137. package/src/linear/Vector.ts +178 -0
  138. package/src/linear/Vector2.ts +979 -0
  139. package/src/linear/Vector3.ts +1320 -0
  140. package/src/linear/Vector4.ts +975 -0
  141. package/src/types/AxisAngle.ts +10 -0
  142. package/src/types/FieldOfView.ts +14 -0
  143. package/src/types/IntegerRepresentation.ts +2 -0
  144. package/src/utility/BigNumber.ts +130 -0
  145. package/src/utility/MagnitudeError.ts +10 -0
  146. package/src/utility/SingularMatrixError.ts +13 -0
  147. package/src/utility/epsilon.ts +2 -0
@@ -0,0 +1,709 @@
1
+ import { SingularMatrixError, epsilon, type SquareMatrix } from "@lakuna/umath";
2
+ import type { Vector2Like } from "@lakuna/umath/Vector2";
3
+
4
+ /**
5
+ * Numbers arranged into two columns and two rows.
6
+ * @see [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics))
7
+ */
8
+ export type Matrix2Like = Matrix2 | [
9
+ number, number,
10
+ number, number
11
+ ];
12
+
13
+ /**
14
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
15
+ * @param radians The angle in radians.
16
+ * @param out The matrix to store the result in.
17
+ * @returns The transformation matrix.
18
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
19
+ */
20
+ export function fromRotation<T extends Matrix2Like>(radians: number, out: T): T {
21
+ const s: number = Math.sin(radians);
22
+ const c: number = Math.cos(radians);
23
+
24
+ out[0] = c;
25
+ out[1] = s;
26
+ out[2] = -s;
27
+ out[3] = c;
28
+ return out;
29
+ }
30
+
31
+ /**
32
+ * Creates a transformation matrix that represents a scaling by the given vector.
33
+ * @param vector The scaling vector.
34
+ * @param out The matrix to store the result in.
35
+ * @returns The transformation matrix.
36
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
37
+ */
38
+ export function fromScaling<T extends Matrix2Like>(vector: Vector2Like, out: T): T {
39
+ out[0] = vector[0];
40
+ out[1] = 0;
41
+ out[2] = 0;
42
+ out[3] = vector[1];
43
+ return out;
44
+ }
45
+
46
+ /**
47
+ * Creates a two-by-two matrix with the given values.
48
+ * @param c0r0 The value in the first column and first row.
49
+ * @param c0r1 The value in the first column and second row.
50
+ * @param c1r0 The value in the second column and first row.
51
+ * @param c1r1 The value in the second column and second row.
52
+ * @param out The matrix to store the result in.
53
+ * @returns The matrix.
54
+ */
55
+ export function fromValues<T extends Matrix2Like>(c0r0: number, c0r1: number, c1r0: number, c1r1: number, out: T): T {
56
+ out[0] = c0r0;
57
+ out[1] = c0r1;
58
+ out[2] = c1r0;
59
+ out[3] = c1r1;
60
+ return out;
61
+ }
62
+
63
+ /**
64
+ * Determines whether two matrices are roughly equivalent.
65
+ * @param a The first matrix.
66
+ * @param b The second matrix.
67
+ * @returns Whether the matrices are equivalent.
68
+ */
69
+ export function equals(a: Matrix2Like, b: Matrix2Like): boolean {
70
+ const a0: number = a[0];
71
+ const a1: number = a[1];
72
+ const a2: number = a[2];
73
+ const a3: number = a[3];
74
+
75
+ const b0: number = b[0];
76
+ const b1: number = b[1];
77
+ const b2: number = b[2];
78
+ const b3: number = b[3];
79
+
80
+ return (Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0))
81
+ && Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1))
82
+ && Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2))
83
+ && Math.abs(a3 - b3) <= epsilon * Math.max(1, Math.abs(a3), Math.abs(b3)));
84
+ }
85
+
86
+ /**
87
+ * Determines whether two matrices are exactly equivalent.
88
+ * @param a The first matrix.
89
+ * @param b The second matrix.
90
+ * @returns Whether the matrices are equivalent.
91
+ */
92
+ export function exactEquals(a: Matrix2Like, b: Matrix2Like): boolean {
93
+ return a[0] == b[0]
94
+ && a[1] == b[1]
95
+ && a[2] == b[2]
96
+ && a[3] == b[3];
97
+ }
98
+
99
+ /**
100
+ * Adds two matrices.
101
+ * @param a The augend.
102
+ * @param b The addend.
103
+ * @param out The matrix to store the result in.
104
+ * @returns The sum.
105
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
106
+ */
107
+ export function add<T extends Matrix2Like>(a: Matrix2Like, b: Matrix2Like, out: T): T {
108
+ out[0] = a[0] + b[0];
109
+ out[1] = a[1] + b[1];
110
+ out[2] = a[2] + b[2];
111
+ out[3] = a[3] + b[3];
112
+ return out;
113
+ }
114
+
115
+ /**
116
+ * Calculates the adjugate of a matrix.
117
+ * @param matrix The matrix.
118
+ * @param out The matrix to store the result in.
119
+ * @returns The adjugate of the matrix.
120
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
121
+ */
122
+ export function adjoint<T extends Matrix2Like>(matrix: Matrix2Like, out: T): T {
123
+ const a0: number = matrix[0];
124
+ out[0] = matrix[3];
125
+ out[1] = -matrix[1];
126
+ out[2] = -matrix[2];
127
+ out[3] = a0;
128
+ return out;
129
+ }
130
+
131
+ /**
132
+ * Copies the values of one matrix to another.
133
+ * @param matrix The matrix to copy.
134
+ * @param out The matrix to store the result in.
135
+ * @returns The copy matrix.
136
+ */
137
+ export function copy<T extends Matrix2Like>(matrix: Matrix2Like, out: T): T {
138
+ out[0] = matrix[0];
139
+ out[1] = matrix[1];
140
+ out[2] = matrix[2];
141
+ out[3] = matrix[3];
142
+ return out;
143
+ }
144
+
145
+ /**
146
+ * Calculates the Frobenius norm of a matrix.
147
+ * @param matrix The matrix.
148
+ * @returns The Frobenius norm.
149
+ * @see [Matrix norm](https://en.wikipedia.org/wiki/Matrix_norm)
150
+ */
151
+ export function frob(matrix: Matrix2Like): number {
152
+ return Math.hypot(
153
+ matrix[0] as number, matrix[1] as number,
154
+ matrix[2] as number, matrix[3] as number
155
+ );
156
+ }
157
+
158
+ /**
159
+ * Multiplies one matrix by another.
160
+ * @param a The multiplicand.
161
+ * @param b The multiplier.
162
+ * @param out The matrix to store the result in.
163
+ * @returns The product.
164
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
165
+ */
166
+ export function multiply<T extends Matrix2Like>(a: Matrix2Like, b: Matrix2Like, out: T): T {
167
+ const a0: number = a[0];
168
+ const a1: number = a[1];
169
+ const a2: number = a[2];
170
+ const a3: number = a[3];
171
+
172
+ const b0: number = b[0];
173
+ const b1: number = b[1];
174
+ const b2: number = b[2];
175
+ const b3: number = b[3];
176
+
177
+ out[0] = a0 * b0 + a2 * b1;
178
+ out[1] = a1 * b0 + a3 * b1;
179
+ out[2] = a0 * b2 + a2 * b3;
180
+ out[3] = a1 * b2 + a3 * b3;
181
+ return out;
182
+ }
183
+
184
+ /**
185
+ * Multiplies a matrix by a scalar value.
186
+ * @param matrix The multiplicand.
187
+ * @param scalar The multiplier.
188
+ * @param out The matrix to store the result in.
189
+ * @returns The product.
190
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
191
+ */
192
+ export function multiplyScalar<T extends Matrix2Like>(matrix: Matrix2Like, scalar: number, out: T): T {
193
+ out[0] = matrix[0] * scalar;
194
+ out[1] = matrix[1] * scalar;
195
+ out[2] = matrix[2] * scalar;
196
+ out[3] = matrix[3] * scalar;
197
+ return out;
198
+ }
199
+
200
+ /**
201
+ * Adds a matrix to another after multiplying the other by a scalar.
202
+ * @param a The augend.
203
+ * @param b The addend.
204
+ * @param scalar The multiplier.
205
+ * @param out The matrix to store the result in.
206
+ * @returns The sum.
207
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
208
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
209
+ */
210
+ export function multiplyScalarAndAdd<T extends Matrix2Like>(a: Matrix2Like, b: Matrix2Like, scalar: number, out: T): T {
211
+ out[0] = a[0] + b[0] * scalar;
212
+ out[1] = a[1] + b[1] * scalar;
213
+ out[2] = a[2] + b[2] * scalar;
214
+ out[3] = a[3] + b[3] * scalar;
215
+ return out;
216
+ }
217
+
218
+ /**
219
+ * Subtracts one matrix from another.
220
+ * @param a The minuend.
221
+ * @param b The subtrahend.
222
+ * @param out The matrix to store the result in.
223
+ * @returns The difference.
224
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
225
+ */
226
+ export function subtract<T extends Matrix2Like>(a: Matrix2Like, b: Matrix2Like, out: T): T {
227
+ out[0] = a[0] - b[0];
228
+ out[1] = a[1] - b[1];
229
+ out[2] = a[2] - b[2];
230
+ out[3] = a[3] - b[3];
231
+ return out;
232
+ }
233
+
234
+ /**
235
+ * Transposes a matrix.
236
+ * @param matrix The matrix.
237
+ * @param out The matrix to store the result in.
238
+ * @returns The transpose of the matrix.
239
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
240
+ */
241
+ export function transpose<T extends Matrix2Like>(matrix: Matrix2Like, out: T): T {
242
+ if (out == matrix) {
243
+ const a1 = matrix[1];
244
+ out[1] = matrix[2];
245
+ out[2] = a1;
246
+ } else {
247
+ out[0] = matrix[0];
248
+ out[1] = matrix[2];
249
+ out[2] = matrix[1];
250
+ out[3] = matrix[3];
251
+ }
252
+ return out;
253
+ }
254
+
255
+ /**
256
+ * Calculates the determinant of a matrix.
257
+ * @param matrix The matrix.
258
+ * @returns The determinant.
259
+ * @see [Determinant](https://en.wikipedia.org/wiki/Determinant)
260
+ */
261
+ export function determinant(matrix: Matrix2Like): number {
262
+ return (matrix[0] as number) * (matrix[3] as number)
263
+ - (matrix[2] as number) * (matrix[1] as number);
264
+ }
265
+
266
+ /**
267
+ * Resets a matrix to identity.
268
+ * @param out The matrix to store the result in.
269
+ * @returns The matrix.
270
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
271
+ */
272
+ export function identity<T extends Matrix2Like>(out: T): T {
273
+ out[0] = 1;
274
+ out[1] = 0;
275
+ out[2] = 0;
276
+ out[3] = 1;
277
+ return out;
278
+ }
279
+
280
+ /**
281
+ * Inverts a matrix.
282
+ * @param matrix The matrix.
283
+ * @param out The matrix to store the result in.
284
+ * @returns The inverted matrix.
285
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
286
+ */
287
+ export function invert<T extends Matrix2Like>(matrix: Matrix2Like, out: T): T {
288
+ const a0: number = matrix[0];
289
+ const a1: number = matrix[1];
290
+ const a2: number = matrix[2];
291
+ const a3: number = matrix[3];
292
+
293
+ let determinant: number = a0 * a3 - a2 * a1;
294
+ if (!determinant) {
295
+ throw new SingularMatrixError();
296
+ }
297
+ determinant = 1 / determinant;
298
+
299
+ out[0] = a3 * determinant;
300
+ out[1] = -a1 * determinant;
301
+ out[2] = -a2 * determinant;
302
+ out[3] = a0 * determinant;
303
+ return out;
304
+ }
305
+
306
+ /**
307
+ * Rotates a matrix by the given angle.
308
+ * @param matrix The matrix.
309
+ * @param radians The angle in radians.
310
+ * @param out The matrix to store the result in.
311
+ * @returns The rotated matrix.
312
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
313
+ */
314
+ export function rotate<T extends Matrix2Like>(matrix: Matrix2Like, radians: number, out: T): T {
315
+ const a0: number = matrix[0];
316
+ const a1: number = matrix[1];
317
+ const a2: number = matrix[2];
318
+ const a3: number = matrix[3];
319
+
320
+ const s: number = Math.sin(radians);
321
+ const c: number = Math.cos(radians);
322
+
323
+ out[0] = a0 * c + a2 * s;
324
+ out[1] = a1 * c + a3 * s;
325
+ out[2] = a0 * -s + a2 * c;
326
+ out[3] = a1 * -s + a3 * c;
327
+ return out;
328
+ }
329
+
330
+ /**
331
+ * Scales a matrix by the given vector.
332
+ * @param matrix The matrix.
333
+ * @param vector The scaling vector.
334
+ * @param out The matrix to store the result in.
335
+ * @returns The scaled matrix.
336
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
337
+ */
338
+ export function scale<T extends Matrix2Like>(matrix: Matrix2Like, vector: Vector2Like, out: T): T {
339
+ const v0: number = vector[0];
340
+ const v1: number = vector[1];
341
+
342
+ out[0] = matrix[0] * v0;
343
+ out[1] = matrix[1] * v0;
344
+ out[2] = matrix[2] * v1;
345
+ out[3] = matrix[3] * v1;
346
+ return out;
347
+ }
348
+
349
+ /**
350
+ * A two-by-two matrix.
351
+ * @see [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics))
352
+ */
353
+ export default class Matrix2 extends Float32Array implements SquareMatrix {
354
+ /**
355
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
356
+ * @param radians The angle in radians.
357
+ * @returns The transformation matrix.
358
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
359
+ */
360
+ public static fromRotation(radians: number): Matrix2;
361
+
362
+ /**
363
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
364
+ * @param radians The angle in radians.
365
+ * @param out The matrix to store the result in.
366
+ * @returns The transformation matrix.
367
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
368
+ */
369
+ public static fromRotation<T extends Matrix2Like>(radians: number, out: T): T;
370
+
371
+ public static fromRotation<T extends Matrix2Like>(radians: number, out: T = new Matrix2() as T): T {
372
+ return fromRotation(radians, out);
373
+ }
374
+
375
+ /**
376
+ * Creates a transformation matrix that represents a scaling by the given vector.
377
+ * @param vector The scaling vector.
378
+ * @returns The transformation matrix.
379
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
380
+ */
381
+ public static fromScaling(vector: Vector2Like): Matrix2;
382
+
383
+ /**
384
+ * Creates a transformation matrix that represents a scaling by the given vector.
385
+ * @param vector The scaling vector.
386
+ * @param out The matrix to store the result in.
387
+ * @returns The transformation matrix.
388
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
389
+ */
390
+ public static fromScaling<T extends Matrix2Like>(vector: Vector2Like, out: T): T;
391
+
392
+ public static fromScaling<T extends Matrix2Like>(vector: Vector2Like, out: T = new Matrix2() as T): T {
393
+ return fromScaling(vector, out);
394
+ }
395
+
396
+ /**
397
+ * Creates a two-by-two matrix with the given values.
398
+ * @param c0r0 The value in the first column and first row.
399
+ * @param c0r1 The value in the first column and second row.
400
+ * @param c1r0 The value in the second column and first row.
401
+ * @param c1r1 The value in the second column and second row.
402
+ * @returns The matrix.
403
+ */
404
+ public static fromValues(c0r0: number, c0r1: number, c1r0: number, c1r1: number): Matrix2;
405
+
406
+ /**
407
+ * Creates a two-by-two matrix with the given values.
408
+ * @param c0r0 The value in the first column and first row.
409
+ * @param c0r1 The value in the first column and second row.
410
+ * @param c1r0 The value in the second column and first row.
411
+ * @param c1r1 The value in the second column and second row.
412
+ * @param out The matrix to store the result in.
413
+ * @returns The matrix.
414
+ */
415
+ public static fromValues<T extends Matrix2Like>(c0r0: number, c0r1: number, c1r0: number, c1r1: number, out: T): T;
416
+
417
+ public static fromValues<T extends Matrix2Like>(c0r0: number, c0r1: number, c1r0: number, c1r1: number, out: T = new Matrix2() as T): T {
418
+ return fromValues(c0r0, c0r1, c1r0, c1r1, out);
419
+ }
420
+
421
+ /**
422
+ * Creates a two-by-two identity matrix.
423
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
424
+ */
425
+ public constructor() {
426
+ super(4);
427
+
428
+ this[0] = 1;
429
+ this[3] = 1;
430
+
431
+ this.width = 2;
432
+ this.height = 2;
433
+ }
434
+
435
+ /** The number of columns in this matrix. */
436
+ public readonly width: 2;
437
+
438
+ /** The number of rows in this matrix. */
439
+ public readonly height: 2;
440
+
441
+ /**
442
+ * Determines whether this matrix is roughly equivalent to another.
443
+ * @param matrix The other matrix.
444
+ * @returns Whether the matrices are equivalent.
445
+ */
446
+ public equals(matrix: Matrix2Like): boolean {
447
+ return equals(this, matrix);
448
+ }
449
+
450
+ /**
451
+ * Determines whether this matrix is exactly equivalent to another.
452
+ * @param matrix The other matrix.
453
+ * @returns Whether the matrices are equivalent.
454
+ */
455
+ public exactEquals(matrix: Matrix2Like): boolean {
456
+ return exactEquals(this, matrix);
457
+ }
458
+
459
+ /**
460
+ * Adds another matrix to this one.
461
+ * @param matrix The other matrix.
462
+ * @returns The sum of the matrices.
463
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
464
+ */
465
+ public add(matrix: Matrix2Like): Matrix2;
466
+
467
+ /**
468
+ * Adds another matrix to this one.
469
+ * @param matrix The other matrix.
470
+ * @param out The matrix to store the result in.
471
+ * @returns The sum of the matrices.
472
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
473
+ */
474
+ public add<T extends Matrix2Like>(matrix: Matrix2Like, out: T): T;
475
+
476
+ public add<T extends Matrix2Like>(matrix: Matrix2Like, out: T = new Matrix2() as T): T {
477
+ return add(this, matrix, out);
478
+ }
479
+
480
+ /**
481
+ * Calculates the adjugate of this matrix.
482
+ * @returns The adjugate of this matrix.
483
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
484
+ */
485
+ public adjoint(): Matrix2;
486
+
487
+ /**
488
+ * Calculates the adjugate of this matrix.
489
+ * @param out The matrix to store the result in.
490
+ * @returns The adjugate of this matrix.
491
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
492
+ */
493
+ public adjoint<T extends Matrix2Like>(out: T): T;
494
+
495
+ public adjoint<T extends Matrix2Like>(out: T = new Matrix2() as T): T {
496
+ return adjoint(this, out);
497
+ }
498
+
499
+ /**
500
+ * Creates a copy of this matrix.
501
+ * @returns A copy of this matrix.
502
+ */
503
+ public clone(): Matrix2 {
504
+ // TODO: `out` parameter.
505
+ return copy(this, new Matrix2());
506
+ }
507
+
508
+ /**
509
+ * Copies the values of another matrix into this one.
510
+ * @param matrix The matrix to copy.
511
+ * @returns This matrix.
512
+ */
513
+ public copy(matrix: Matrix2Like): this {
514
+ return copy(matrix, this);
515
+ }
516
+
517
+ /**
518
+ * The Frobenius norm of this matrix.
519
+ * @see [Matrix norm](https://en.wikipedia.org/wiki/Matrix_norm)
520
+ */
521
+ public get frob(): number {
522
+ return frob(this);
523
+ }
524
+
525
+ /**
526
+ * Multiplies this matrix by another.
527
+ * @param matrix The other matrix.
528
+ * @returns The product of the matrices.
529
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
530
+ */
531
+ public multiply(matrix: Matrix2Like): Matrix2;
532
+
533
+ /**
534
+ * Multiplies this matrix by another.
535
+ * @param matrix The other matrix.
536
+ * @param out The matrix to store the result in.
537
+ * @returns The product of the matrices.
538
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
539
+ */
540
+ public multiply<T extends Matrix2Like>(matrix: Matrix2Like, out: T): T;
541
+
542
+ public multiply<T extends Matrix2Like>(matrix: Matrix2Like, out: T = new Matrix2() as T): T {
543
+ return multiply(this, matrix, out);
544
+ }
545
+
546
+ /**
547
+ * Multiplies this matrix by a scalar value.
548
+ * @param scalar The scalar value.
549
+ * @returns The product of the matrix and the scalar value.
550
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
551
+ */
552
+ public multiplyScalar(scalar: number): Matrix2;
553
+
554
+ /**
555
+ * Multiplies this matrix by a scalar value.
556
+ * @param scalar The scalar value.
557
+ * @param out The matrix to store the result in.
558
+ * @returns The product of the matrix and the scalar value.
559
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
560
+ */
561
+ public multiplyScalar<T extends Matrix2Like>(scalar: number, out: T): T;
562
+
563
+ public multiplyScalar<T extends Matrix2Like>(scalar: number, out: T = new Matrix2() as T): T {
564
+ return multiplyScalar(this, scalar, out);
565
+ }
566
+
567
+ /**
568
+ * Adds this matrix to another after multiplying the other by a scalar.
569
+ * @param matrix The other matrix.
570
+ * @param scalar The scalar.
571
+ * @returns The sum.
572
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
573
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
574
+ */
575
+ public multiplyScalarAndAdd(matrix: Matrix2Like, scalar: number): Matrix2;
576
+
577
+ /**
578
+ * Adds this matrix to another after multiplying the other by a scalar.
579
+ * @param matrix The other matrix.
580
+ * @param scalar The scalar.
581
+ * @param out The matrix to store the result in.
582
+ * @returns The sum.
583
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
584
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
585
+ */
586
+ public multiplyScalarAndAdd<T extends Matrix2Like>(matrix: Matrix2Like, scalar: number, out: T): T;
587
+
588
+ public multiplyScalarAndAdd<T extends Matrix2Like>(matrix: Matrix2Like, scalar: number, out: T = new Matrix2() as T): T {
589
+ return multiplyScalarAndAdd(this, matrix, scalar, out);
590
+ }
591
+
592
+ /**
593
+ * Subtracts another matrix from this one.
594
+ * @param matrix The other matrix.
595
+ * @returns The difference between the matrices.
596
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
597
+ */
598
+ public subtract(matrix: Matrix2Like): Matrix2;
599
+
600
+ /**
601
+ * Subtracts another matrix from this one.
602
+ * @param matrix The other matrix.
603
+ * @param out The matrix to store the result in.
604
+ * @returns The difference between the matrices.
605
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
606
+ */
607
+ public subtract<T extends Matrix2Like>(matrix: Matrix2Like, out: T): T;
608
+
609
+ public subtract<T extends Matrix2Like>(matrix: Matrix2Like, out: T = new Matrix2() as T): T {
610
+ return subtract(this, matrix, out);
611
+ }
612
+
613
+ /**
614
+ * Transposes this matrix.
615
+ * @returns The transpose of this matrix.
616
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
617
+ */
618
+ public transpose(): Matrix2;
619
+
620
+ /**
621
+ * Transposes this matrix.
622
+ * @param out The matrix to store the result in.
623
+ * @returns The transpose of this matrix.
624
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
625
+ */
626
+ public transpose<T extends Matrix2Like>(out: T): T;
627
+
628
+ public transpose<T extends Matrix2Like>(out: T = new Matrix2() as T): T {
629
+ return transpose(this, out);
630
+ }
631
+
632
+ /**
633
+ * The determinant of this matrix.
634
+ * @see [Determinant](https://en.wikipedia.org/wiki/Determinant)
635
+ */
636
+ public get determinant(): number {
637
+ return determinant(this);
638
+ }
639
+
640
+ /**
641
+ * Resets this matrix to identity.
642
+ * @returns This matrix.
643
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
644
+ */
645
+ public identity(): this {
646
+ return identity(this);
647
+ }
648
+
649
+ /**
650
+ * Inverts this matrix.
651
+ * @returns The inverted matrix.
652
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
653
+ */
654
+ public invert(): Matrix2;
655
+
656
+ /**
657
+ * Inverts this matrix.
658
+ * @param out The matrix to store the result in.
659
+ * @returns The inverted matrix.
660
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
661
+ */
662
+ public invert<T extends Matrix2Like>(out: T): T;
663
+
664
+ public invert<T extends Matrix2Like>(out: T = new Matrix2() as T): T {
665
+ return invert(this, out);
666
+ }
667
+
668
+ /**
669
+ * Rotates this matrix by the given angle.
670
+ * @param radians The angle in radians.
671
+ * @returns The rotated matrix.
672
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
673
+ */
674
+ public rotate(radians: number): Matrix2;
675
+
676
+ /**
677
+ * Rotates this matrix by the given angle.
678
+ * @param radians The angle in radians.
679
+ * @param out The matrix to store the result in.
680
+ * @returns The rotated matrix.
681
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
682
+ */
683
+ public rotate<T extends Matrix2Like>(radians: number, out: T): T;
684
+
685
+ public rotate<T extends Matrix2Like>(radians: number, out: T = new Matrix2() as T): T {
686
+ return rotate(this, radians, out);
687
+ }
688
+
689
+ /**
690
+ * Scales this matrix by the given vector.
691
+ * @param v The scaling vector.
692
+ * @returns The scaled matrix.
693
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
694
+ */
695
+ public scale(v: Vector2Like): Matrix2;
696
+
697
+ /**
698
+ * Scales this matrix by the given vector.
699
+ * @param vector The scaling vector.
700
+ * @param out The matrix to store the result in.
701
+ * @returns The scaled matrix.
702
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
703
+ */
704
+ public scale<T extends Matrix2Like>(vector: Vector2Like, out: T): T;
705
+
706
+ public scale<T extends Matrix2Like>(vector: Vector2Like, out: T = new Matrix2() as T): T {
707
+ return scale(this, vector, out);
708
+ }
709
+ }