@wemap/maths 12.10.0 → 12.10.8-alpha.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 (51) hide show
  1. package/{index.ts → dist/index.d.ts} +0 -2
  2. package/dist/index.js +1 -1155
  3. package/dist/index.js.map +1 -1
  4. package/{src/EulerAngles.ts → dist/src/EulerAngles.d.ts} +2 -7
  5. package/dist/src/Geometry.d.ts +10 -0
  6. package/dist/src/Matrix.d.ts +13 -0
  7. package/dist/src/Matrix.spec.d.ts +1 -0
  8. package/dist/src/Matrix3.d.ts +20 -0
  9. package/dist/src/Matrix3.spec.d.ts +2 -0
  10. package/dist/src/Matrix4.d.ts +35 -0
  11. package/dist/src/Matrix4.spec.d.ts +1 -0
  12. package/dist/src/Point2.d.ts +17 -0
  13. package/dist/src/Point2.spec.d.ts +1 -0
  14. package/dist/src/Polygon.d.ts +7 -0
  15. package/dist/src/Polygon.spec.d.ts +1 -0
  16. package/dist/src/Quaternion.d.ts +71 -0
  17. package/dist/src/Quaternion.spec.d.ts +1 -0
  18. package/dist/src/Rotations.d.ts +13 -0
  19. package/dist/src/Rotations.spec.d.ts +1 -0
  20. package/dist/src/Utils.d.ts +38 -0
  21. package/dist/src/Utils.spec.d.ts +1 -0
  22. package/dist/src/Vector.d.ts +5 -0
  23. package/dist/src/Vector.spec.d.ts +1 -0
  24. package/dist/src/Vector3.d.ts +13 -0
  25. package/dist/src/Vector3.spec.d.ts +1 -0
  26. package/{src/types.ts → dist/src/types.d.ts} +37 -8
  27. package/dist/vite.config.d.ts +19 -0
  28. package/package.json +7 -4
  29. package/src/Geometry.ts +0 -39
  30. package/src/Matrix.spec.ts +0 -130
  31. package/src/Matrix.ts +0 -242
  32. package/src/Matrix3.spec.ts +0 -138
  33. package/src/Matrix3.ts +0 -165
  34. package/src/Matrix4.spec.ts +0 -187
  35. package/src/Matrix4.ts +0 -309
  36. package/src/Point2.spec.ts +0 -19
  37. package/src/Point2.ts +0 -54
  38. package/src/Polygon.spec.ts +0 -48
  39. package/src/Polygon.ts +0 -63
  40. package/src/Quaternion.spec.ts +0 -126
  41. package/src/Quaternion.ts +0 -292
  42. package/src/Rotations.spec.ts +0 -67
  43. package/src/Rotations.ts +0 -223
  44. package/src/Utils.spec.ts +0 -83
  45. package/src/Utils.ts +0 -77
  46. package/src/Vector.spec.ts +0 -15
  47. package/src/Vector.ts +0 -10
  48. package/src/Vector3.spec.ts +0 -52
  49. package/src/Vector3.ts +0 -51
  50. package/tsconfig.json +0 -3
  51. package/vite.config.ts +0 -4
@@ -0,0 +1,71 @@
1
+ import { Matrix3_t, Quaternion_t, Vector3_t } from './types.js';
2
+ declare class Quaternion {
3
+ static ROTX_PI2: Quaternion_t;
4
+ static ROTX_PI: Quaternion_t;
5
+ static ROTX_MPI2: Quaternion_t;
6
+ static ROTY_PI2: Quaternion_t;
7
+ static ROTY_PI: Quaternion_t;
8
+ static ROTY_MPI2: Quaternion_t;
9
+ static ROTZ_PI2: Quaternion_t;
10
+ static ROTZ_PI: Quaternion_t;
11
+ static ROTZ_MPI2: Quaternion_t;
12
+ static get identity(): Quaternion_t;
13
+ static sum(q1: Quaternion_t, q2: Quaternion_t): Quaternion_t;
14
+ static rotateMatlab(q: Quaternion_t, v: Vector3_t): Vector3_t;
15
+ static rotate(q: Quaternion_t, v: Vector3_t): Vector3_t;
16
+ /**
17
+ * Unit Quaternion inverse
18
+ * @param {Array.<Number>} quaternion must be a unit quaternion.
19
+ */
20
+ static inverse(q: Quaternion_t): Quaternion_t;
21
+ /**
22
+ * Multiply quaternions
23
+ * @param {...Number[]} quaternions
24
+ * @returns {Number[]}
25
+ */
26
+ static multiply(...quaternions: Quaternion_t[]): Quaternion_t;
27
+ /**
28
+ * Axis-angle to quaternion
29
+ * @param {Array[Number]} axis unit vector
30
+ * @param {Number} angle in radians
31
+ */
32
+ static fromAxisAngle(axis: Vector3_t, angle: number): Quaternion_t;
33
+ /**
34
+ * Rotation around x-axis
35
+ * @param {Number} angle in radians
36
+ */
37
+ static rotX(angle: number): Quaternion_t;
38
+ /**
39
+ * Rotation around x-axis
40
+ * @param {Number} angle in radians
41
+ */
42
+ static rotY(angle: number): Quaternion_t;
43
+ /**
44
+ * Rotation around x-axis
45
+ * @param {Number} angle in radians
46
+ */
47
+ static rotZ(angle: number): Quaternion_t;
48
+ static fromMatrix3(m: Matrix3_t): Quaternion_t;
49
+ static fromMatrix3Matlab(m: Matrix3_t): Quaternion_t;
50
+ static toMatrix3(q: Quaternion_t): Matrix3_t;
51
+ static wxyz2xyzw(wxyz: Quaternion_t): Quaternion_t;
52
+ static xyzw2wxyz(xyzw: Quaternion_t): Quaternion_t;
53
+ static distance(q1: Quaternion_t, q2: Quaternion_t): number;
54
+ static equals(q1: Quaternion_t, q2: Quaternion_t): boolean;
55
+ static normalize(quaternion: Quaternion_t): Quaternion_t;
56
+ static norm(quaternion: Quaternion_t): number;
57
+ static dot(q1: Quaternion_t, q2: Quaternion_t): number;
58
+ static getRotationBetweenTwoVectors(from: Vector3_t, to: Vector3_t): Quaternion_t;
59
+ /**
60
+ * Performs a spherical linear interpolation between two quat
61
+ *
62
+ * @param {number[]} a the first operand
63
+ * @param {number[]} b the second operand
64
+ * @param {number} t interpolation amount between the two inputs
65
+ * @returns {number[]} out
66
+ */
67
+ static slerp(a: Quaternion_t, b: Quaternion_t, t: number): Quaternion_t;
68
+ static toString(q: Quaternion_t): string;
69
+ static fromUnitVectors(vFrom: Vector3_t, vTo: Vector3_t): Quaternion_t;
70
+ }
71
+ export default Quaternion;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { EulerAnglesDegrees_t, EulerAngles_t, Quaternion_t } from './types.js';
2
+ declare class Rotations {
3
+ static eulerToQuaternionZXYDegrees(eulerAnglesDegrees: EulerAnglesDegrees_t): Quaternion_t;
4
+ static eulerToQuaternionZXY(eulerAngles: EulerAngles_t): Quaternion_t;
5
+ static quaternionToEulerZXYDegrees(quaternion: Quaternion_t): EulerAnglesDegrees_t;
6
+ static quaternionToEulerZXY(quaternion: Quaternion_t): EulerAngles_t;
7
+ static quaternionToEulerDegrees(quaternion: Quaternion_t, order: string): EulerAnglesDegrees_t;
8
+ static quaternionToEuler(quaternion: Quaternion_t, order: string): EulerAngles_t;
9
+ private static _threeaxisrot;
10
+ private static _twoaxisrot;
11
+ static getHeadingFromQuaternion(quaternion: Quaternion_t): number;
12
+ }
13
+ export default Rotations;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Transform an angle in degre to radian
3
+ * @param {Number} angle
4
+ * @return {Number}
5
+ */
6
+ export declare function deg2rad(angle: number): number;
7
+ export declare function rad2deg(rad: number): number;
8
+ /**
9
+ * Difference between two angles
10
+ * @param {Number} angle1 in radians
11
+ * @param {Number} angle2 in radians
12
+ */
13
+ export declare function diffAngle(angle1: number, angle2: number): number;
14
+ /**
15
+ * Difference between two angles in degrees
16
+ * @param {Number} angle1 in degrees
17
+ * @param {Number} angle2 in degrees
18
+ */
19
+ export declare function diffAngleDegrees(angle1: number, angle2: number): number;
20
+ /**
21
+ * Difference angle between two lines
22
+ * @param {Number} angle1 line1 angle in radians
23
+ * @param {Number} angle2 line2 angle in radians
24
+ */
25
+ export declare function diffAngleLines(angle1: number, angle2: number): number;
26
+ export declare function positiveMod(n: number, m: number): number;
27
+ /**
28
+ * constrain n to the given range, excluding the minimum, via modular arithmetic
29
+ *
30
+ * @param {number} n value
31
+ * @param {number} min the minimum value to be returned, exclusive
32
+ * @param {number} max the maximum value to be returned, inclusive
33
+ * @returns {number} constrained number
34
+ */
35
+ export declare function wrap(n: number, min: number, max: number): number;
36
+ export declare function average(arr: number[]): number;
37
+ export declare function std(arr: number[]): number;
38
+ export declare function roundFactor(num: number, factor: number): number;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Vector_t } from './types';
2
+ declare class Vector {
3
+ static concat(v1: Vector_t, v2: Vector_t): Vector_t;
4
+ }
5
+ export default Vector;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { Vector3_t } from './types.js';
2
+ import { default as Vector } from './Vector.js';
3
+ declare class Vector3 extends Vector {
4
+ static norm(vector: Vector3_t): number;
5
+ static normalize(vector: Vector3_t): Vector3_t;
6
+ static sum(v1: Vector3_t, v2: Vector3_t): Vector3_t;
7
+ static subtract(v1: Vector3_t, v2: Vector3_t): Vector3_t;
8
+ static distance(v1: Vector3_t, v2: Vector3_t): number;
9
+ static cross(v1: Vector3_t, v2: Vector3_t): Vector3_t;
10
+ static dot(v1: Vector3_t, v2: Vector3_t): number;
11
+ static multiplyScalar(vector: Vector3_t, scalar: number): Vector3_t;
12
+ }
13
+ export default Vector3;
@@ -0,0 +1 @@
1
+ export {};
@@ -4,18 +4,47 @@ export type Polygon_t = Point2_t[];
4
4
  export type Quaternion_t = [number, number, number, number];
5
5
  export type Vector3_t = [number, number, number];
6
6
  export type Vector4_t = [number, number, number, number];
7
- export type Vector16_t = [number, number, number, number,
8
- number, number, number, number,
9
- number, number, number, number,
10
- number, number, number, number
7
+ export type Vector16_t = [
8
+ number,
9
+ number,
10
+ number,
11
+ number,
12
+ number,
13
+ number,
14
+ number,
15
+ number,
16
+ number,
17
+ number,
18
+ number,
19
+ number,
20
+ number,
21
+ number,
22
+ number,
23
+ number
11
24
  ];
12
25
  export type Vector_t = number[];
13
26
  export type Matrix3_t = [[number, number, number], [number, number, number], [number, number, number]];
14
- export type Matrix4_t = [[number, number, number, number],
27
+ export type Matrix4_t = [
15
28
  [number, number, number, number],
16
- [number, number, number, number],
17
- [number, number, number, number]];
18
-
29
+ [
30
+ number,
31
+ number,
32
+ number,
33
+ number
34
+ ],
35
+ [
36
+ number,
37
+ number,
38
+ number,
39
+ number
40
+ ],
41
+ [
42
+ number,
43
+ number,
44
+ number,
45
+ number
46
+ ]
47
+ ];
19
48
  export type Matrix_t = number[][];
20
49
  export type EulerAngles_t = Vector3_t;
21
50
  export type EulerAnglesDegrees_t = Vector3_t;
@@ -0,0 +1,19 @@
1
+ declare const _default: {
2
+ build: {
3
+ minify: string;
4
+ lib: {
5
+ entry: string;
6
+ name: string;
7
+ formats: string[];
8
+ fileName: (format: string) => "index.mjs" | "index.js";
9
+ };
10
+ sourcemap: boolean;
11
+ };
12
+ plugins: import('vite').Plugin[];
13
+ optimizeDeps: {
14
+ esbuildOptions: {
15
+ jsx: string;
16
+ };
17
+ };
18
+ };
19
+ export default _default;
package/package.json CHANGED
@@ -6,14 +6,14 @@
6
6
  ],
7
7
  "description": "Wemap maths utils package",
8
8
  "main": "dist/index.js",
9
- "types": "index.ts",
9
+ "types": "./dist/index.d.ts",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "git+https://github.com/wemap/wemap-modules-js.git",
13
13
  "directory": "packages/maths"
14
14
  },
15
15
  "name": "@wemap/maths",
16
- "version": "12.10.0",
16
+ "version": "12.10.8-alpha.2",
17
17
  "bugs": {
18
18
  "url": "https://github.com/wemap/wemap-modules-js/issues"
19
19
  },
@@ -29,12 +29,15 @@
29
29
  "wemap"
30
30
  ],
31
31
  "license": "ISC",
32
+ "files": [
33
+ "dist"
34
+ ],
32
35
  "exports": {
33
36
  ".": {
34
- "types": "./index.ts",
37
+ "types": "./dist/index.d.ts",
35
38
  "import": "./dist/index.mjs",
36
39
  "require": "./dist/index.js"
37
40
  }
38
41
  },
39
- "gitHead": "e5293ab739c4cf3121f56e4fdb31510ab431dc45"
42
+ "gitHead": "76c13880fb0d409c142cfc464dd48abc298ae58e"
40
43
  }
package/src/Geometry.ts DELETED
@@ -1,39 +0,0 @@
1
- import { Line_t, Polygon_t } from './types';
2
-
3
- /**
4
- * @param {Number[][]} line1
5
- * @param {Number[][]} line2
6
- * @param {Boolean} detectIntersectionAtBounds should detect intersection if a
7
- * line extremity intersect the other line
8
- * @returns {Boolean}
9
- */
10
- export function linesIntersect(line1: Line_t, line2: Line_t, detectIntersectionAtBounds = true): boolean {
11
-
12
- const [[p0x, p0y], [p1x, p1y]] = line1;
13
- const [[p2x, p2y], [p3x, p3y]] = line2;
14
-
15
- const s1x = p1x - p0x;
16
- const s1y = p1y - p0y;
17
- const s2x = p3x - p2x;
18
- const s2y = p3y - p2y;
19
-
20
- const s = (-s1y * (p0x - p2x) + s1x * (p0y - p2y)) / (-s2x * s1y + s1x * s2y);
21
- const t = (s2x * (p0y - p2y) - s2y * (p0x - p2x)) / (-s2x * s1y + s1x * s2y);
22
-
23
- if (!detectIntersectionAtBounds) {
24
- return s > 0 && s < 1 && t > 0 && t < 1;
25
- }
26
- return s >= 0 && s <= 1 && t >= 0 && t <= 1;
27
- }
28
-
29
- export function isPolygonClockwise(polygon: Polygon_t): boolean {
30
-
31
- let sum = 0;
32
- for (let i = 0; i < polygon.length; i++) {
33
- const p1 = polygon[i];
34
- const p2 = polygon[(i + 1) % polygon.length];
35
- const edge = (p2[0] - p1[0]) * (p2[1] + p1[1]);
36
- sum += edge;
37
- }
38
- return sum > 0;
39
- }
@@ -1,130 +0,0 @@
1
- import chai from 'chai';
2
-
3
- import Matrix from './Matrix.js';
4
-
5
- const { expect } = chai;
6
-
7
- const m1 = [
8
- [1, 2, 3, 4],
9
- [5, 6, 7, 8],
10
- [9, 0, 1, 2]
11
- ];
12
-
13
- const m2 = [
14
- [1, 2],
15
- [3, 4],
16
- [5, 6],
17
- [7, 8]
18
- ];
19
-
20
- const m3 = [[3, 4, 5, 6]];
21
-
22
- const m4 = [[5, 6], [9, 0], [3, 4]];
23
-
24
- const m5 = [[1, 2], [3, 4]];
25
-
26
- const v1 = [1, 3, 5, 7];
27
-
28
- describe('Matrix', () => {
29
-
30
- it('sum', () => {
31
- const result = Matrix.sum(m5, m5);
32
- expect(result).deep.equal([
33
- [2, 4],
34
- [6, 8]
35
- ]);
36
- });
37
-
38
- it('concatRow', () => {
39
- const result = Matrix.concatRow(m1, m3);
40
- expect(result).deep.equal([
41
- [1, 2, 3, 4],
42
- [5, 6, 7, 8],
43
- [9, 0, 1, 2],
44
- [3, 4, 5, 6]
45
- ]);
46
- });
47
-
48
- it('concatLine', () => {
49
- const result = Matrix.concatLine(m1, m4);
50
- expect(result).deep.equal([
51
- [1, 2, 3, 4, 5, 6],
52
- [5, 6, 7, 8, 9, 0],
53
- [9, 0, 1, 2, 3, 4]
54
- ]);
55
- });
56
-
57
- it('diag', () => {
58
- const result = Matrix.diag([1, 2, 3, 4]);
59
- expect(result).deep.equal([
60
- [1, 0, 0, 0],
61
- [0, 2, 0, 0],
62
- [0, 0, 3, 0],
63
- [0, 0, 0, 4]
64
- ]);
65
- });
66
-
67
- it('transpose', () => {
68
- const result = Matrix.transpose(m1);
69
- expect(result).deep.equal([
70
- [1, 5, 9],
71
- [2, 6, 0],
72
- [3, 7, 1],
73
- [4, 8, 2]
74
- ]);
75
- });
76
-
77
- it('multiply', () => {
78
- const result = Matrix.multiply(m1, m2);
79
- expect(result).deep.equal([
80
- [50, 60],
81
- [114, 140],
82
- [28, 40]
83
- ]);
84
- });
85
-
86
- it('multiplyScalar', () => {
87
- const result = Matrix.multiplyScalar(m2, 3);
88
- expect(result).deep.equal([
89
- [3, 6],
90
- [9, 12],
91
- [15, 18],
92
- [21, 24]
93
- ]);
94
- });
95
-
96
- it('multiplyVector', () => {
97
- const result = Matrix.multiplyVector(m1, v1);
98
- expect(result).deep.equal([50, 114, 28]);
99
- });
100
-
101
- it('inverse', () => {
102
- let result;
103
-
104
- result = Matrix.inverse([[1, 2]]);
105
- expect(result).is.null;
106
-
107
- result = Matrix.inverse([[1, 2], [3, 4]]);
108
- expect(result).deep.equal([
109
- [-2, 1],
110
- [1.5, -0.5]
111
- ]);
112
-
113
- result = Matrix.inverse([[0, 2], [1, 9]]);
114
- expect(result).deep.equal([
115
- [-4.5, 1],
116
- [0.5, 0]
117
- ]);
118
-
119
- result = Matrix.inverse([[0, 2, 3], [0, 1, 2], [1, 2, 1]]);
120
- expect(result).deep.equal([
121
- [-3, 4, 1],
122
- [2, -3, 0],
123
- [-1, 2, -0]
124
- ]);
125
-
126
- result = Matrix.inverse([[1, 2], [1, 2]]);
127
- expect(result).is.null;
128
-
129
- });
130
- });
package/src/Matrix.ts DELETED
@@ -1,242 +0,0 @@
1
- /* eslint-disable max-statements */
2
- /* eslint max-depth: 0 */
3
-
4
- import { Matrix_t, Vector3_t, Vector_t } from './types';
5
-
6
- class Matrix {
7
-
8
- static sum(a: Matrix_t, b: Matrix_t): Matrix_t {
9
- const aNumRows = a.length;
10
- const aNumCols = a[0].length;
11
-
12
- const m = new Array(aNumRows);
13
- for (let r = 0; r < aNumRows; ++r) {
14
- m[r] = new Array(aNumCols);
15
- for (let c = 0; c < aNumCols; ++c) {
16
- m[r][c] = a[r][c] + b[r][c];
17
- }
18
- }
19
- return m;
20
- }
21
-
22
- static concatRow(a: Matrix_t, b: Matrix_t): Matrix_t {
23
- const aNumRows = a.length;
24
- const bNumRows = b.length;
25
-
26
- const m = new Array(aNumRows + bNumRows);
27
-
28
- for (let r = 0; r < aNumRows; ++r) {
29
- m[r] = a[r].slice();
30
- }
31
- for (let r = 0; r < bNumRows; ++r) {
32
- m[aNumRows + r] = b[r].slice();
33
- }
34
-
35
- return m;
36
- }
37
-
38
- static concatLine(a: Matrix_t, b: Matrix_t): Matrix_t {
39
- const aNumRows = a.length;
40
- const m = new Array(aNumRows);
41
- for (let r = 0; r < aNumRows; ++r) {
42
- m[r] = a[r].concat(b[r]);
43
- }
44
- return m;
45
- }
46
-
47
- static diag(arr: Vector_t): Matrix_t {
48
- const m = Array(arr.length);
49
- for (let i = 0; i < arr.length; i++) {
50
- m[i] = Array(arr.length);
51
- for (let j = 0; j < arr.length; j++) {
52
- m[i][j] = i === j ? arr[i] : 0;
53
- }
54
- }
55
- return m;
56
- }
57
-
58
- static transpose(a: Matrix_t): Matrix_t {
59
-
60
- const numRows = a.length;
61
- const numCols = a[0].length;
62
-
63
- const m = Array(numCols);
64
- for (let i = 0; i < numCols; i++) {
65
- m[i] = Array(numRows);
66
- for (let j = 0; j < numRows; j++) {
67
- m[i][j] = a[j][i];
68
- }
69
- }
70
-
71
- return m;
72
- }
73
-
74
-
75
- static multiply(a: Matrix_t, b: Matrix_t): Matrix_t {
76
- const aNumRows = a.length;
77
- const aNumCols = a[0].length;
78
- const bNumCols = b[0].length;
79
-
80
- const m = new Array(aNumRows);
81
- for (let r = 0; r < aNumRows; ++r) {
82
-
83
- m[r] = new Array(bNumCols);
84
- for (let c = 0; c < bNumCols; ++c) {
85
-
86
- m[r][c] = 0;
87
- for (let i = 0; i < aNumCols; ++i) {
88
- m[r][c] += a[r][i] * b[i][c];
89
- }
90
- }
91
- }
92
- return m;
93
- }
94
-
95
- static multiplyScalar(a: Matrix_t, s: number): Matrix_t {
96
- const aNumRows = a.length;
97
- const aNumCols = a[0].length;
98
-
99
- const m = new Array(aNumRows);
100
- for (let r = 0; r < aNumRows; ++r) {
101
- m[r] = new Array(aNumCols);
102
- for (let c = 0; c < aNumCols; ++c) {
103
- m[r][c] = a[r][c] * s;
104
- }
105
- }
106
- return m;
107
- }
108
-
109
- static multiplyVector(a: Matrix_t, v: Vector_t): Vector_t {
110
- const aNumRows = a.length;
111
- const aNumCols = a[0].length;
112
-
113
- const output = new Array(aNumRows) as Vector3_t;
114
- for (let r = 0; r < aNumRows; ++r) {
115
- output[r] = 0;
116
- for (let c = 0; c < aNumCols; ++c) {
117
- output[r] += a[r][c] * v[c];
118
- }
119
- }
120
- return output;
121
- }
122
-
123
- // Returns the inverse of matrix M
124
- // Helped from http://blog.acipo.com/matrix-inversion-in-javascript/
125
- static inverse(M: Matrix_t): Matrix_t | null {
126
- // I use Guassian Elimination to calculate the inverse:
127
- // (1) 'augment' the matrix (left) by the identity (on the right)
128
- // (2) Turn the matrix on the left into the identity by elemetry row ops
129
- // (3) The matrix on the right is the inverse (was the identity matrix)
130
- // There are 3 elemtary row ops: (I combine b and c in my code)
131
- // (a) Swap 2 rows
132
- // (b) Multiply a row by a scalar
133
- // (c) Add 2 rows
134
-
135
- // if the matrix isn't square: exit (error)
136
- if (M.length !== M[0].length) {
137
- return null;
138
- }
139
-
140
- // create the identity matrix (I), and a copy (C) of the original
141
- let i = 0, ii = 0, j = 0, e = 0;
142
- const dim = M.length;
143
- const I: Matrix_t = [], C: Matrix_t = [];
144
- for (i = 0; i < dim; i += 1) {
145
- // Create the row
146
- I[I.length] = [];
147
- C[C.length] = [];
148
- for (j = 0; j < dim; j += 1) {
149
-
150
- // if we're on the diagonal, put a 1 (for identity)
151
- if (i === j) {
152
- I[i][j] = 1;
153
- } else {
154
- I[i][j] = 0;
155
- }
156
-
157
- // Also, make the copy of the original
158
- C[i][j] = M[i][j];
159
- }
160
- }
161
-
162
- // Perform elementary row operations
163
- for (i = 0; i < dim; i += 1) {
164
- // get the element e on the diagonal
165
- e = C[i][i];
166
-
167
- // if we have a 0 on the diagonal (we'll need to swap with a lower row)
168
- if (e === 0) {
169
- // look through every row below the i'th row
170
- for (ii = i + 1; ii < dim; ii += 1) {
171
- // if the ii'th row has a non-0 in the i'th col
172
- if (C[ii][i] !== 0) {
173
- // it would make the diagonal have a non-0 so swap it
174
- for (j = 0; j < dim; j++) {
175
- // temp store i'th row
176
- e = C[i][j];
177
- // replace i'th row by ii'th
178
- C[i][j] = C[ii][j];
179
- // repace ii'th by temp
180
- C[ii][j] = e;
181
- // temp store i'th row
182
- e = I[i][j];
183
- // replace i'th row by ii'th
184
- I[i][j] = I[ii][j];
185
- // repace ii'th by temp
186
- I[ii][j] = e;
187
- }
188
- // don't bother checking other rows since we've swapped
189
- break;
190
- }
191
- }
192
- // get the new diagonal
193
- e = C[i][i];
194
- // if it's still 0, not invertable (error)
195
- if (e === 0) {
196
- return null;
197
- }
198
- }
199
-
200
- // Scale this row down by e (so we have a 1 on the diagonal)
201
- for (j = 0; j < dim; j++) {
202
- // apply to original matrix
203
- C[i][j] = C[i][j] / e;
204
-
205
- // apply to identity
206
- I[i][j] = I[i][j] / e;
207
- }
208
-
209
- // Subtract this row (scaled appropriately for each row) from ALL of
210
- // the other rows so that there will be 0's in this column in the
211
- // rows above and below this one
212
- for (ii = 0; ii < dim; ii++) {
213
- // Only apply to other rows (we want a 1 on the diagonal)
214
- if (ii === i) {
215
- continue;
216
- }
217
-
218
- // We want to change this element to 0
219
- e = C[ii][i];
220
-
221
- // Subtract (the row above(or below) scaled by e) from (the
222
- // current row) but start at the i'th column and assume all the
223
- // stuff left of diagonal is 0 (which it should be if we made this
224
- // algorithm correctly)
225
- for (j = 0; j < dim; j++) {
226
- // apply to original matrix
227
- C[ii][j] -= e * C[i][j];
228
-
229
- // apply to identity
230
- I[ii][j] -= e * I[i][j];
231
- }
232
- }
233
- }
234
-
235
- // we've done all operations, C should be the identity
236
- // matrix I should be the inverse:
237
- return I;
238
- }
239
-
240
- }
241
-
242
- export default Matrix;