@siteimprove/alfa-math 0.89.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +151 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/matrix.d.ts +57 -0
- package/dist/matrix.js +152 -0
- package/dist/real.d.ts +37 -0
- package/dist/real.js +72 -0
- package/dist/vector.d.ts +44 -0
- package/dist/vector.js +79 -0
- package/package.json +31 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# @siteimprove/alfa-math
|
|
2
|
+
|
|
3
|
+
## 0.89.3
|
|
4
|
+
|
|
5
|
+
## 0.89.2
|
|
6
|
+
|
|
7
|
+
### Patch Changes
|
|
8
|
+
|
|
9
|
+
- **Changed:** Trying to fix a problem in generating provenance statements ([#1674](https://github.com/Siteimprove/alfa/pull/1674))
|
|
10
|
+
|
|
11
|
+
## 0.89.1
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- **Added:** Trying to publish Alfa packages on the npm registry ([#1673](https://github.com/Siteimprove/alfa/pull/1673))
|
|
16
|
+
|
|
17
|
+
## 0.89.0
|
|
18
|
+
|
|
19
|
+
## 0.88.0
|
|
20
|
+
|
|
21
|
+
### Minor Changes
|
|
22
|
+
|
|
23
|
+
- **Fixed:** The publish flow was updated to a new version. ([`a2f19cf9a6c7c72b8bf085597e4f1a95ac3e4eb2`](https://github.com/Siteimprove/alfa/commit/a2f19cf9a6c7c72b8bf085597e4f1a95ac3e4eb2))
|
|
24
|
+
|
|
25
|
+
Some 0.87.\* versions were generating uninstallable package. This should be fixed now.
|
|
26
|
+
|
|
27
|
+
## 0.87.12
|
|
28
|
+
|
|
29
|
+
## 0.87.11
|
|
30
|
+
|
|
31
|
+
## 0.87.10
|
|
32
|
+
|
|
33
|
+
## 0.87.7
|
|
34
|
+
|
|
35
|
+
## 0.87.6
|
|
36
|
+
|
|
37
|
+
## 0.87.5
|
|
38
|
+
|
|
39
|
+
## 0.87.4
|
|
40
|
+
|
|
41
|
+
## 0.87.3
|
|
42
|
+
|
|
43
|
+
## 0.87.2
|
|
44
|
+
|
|
45
|
+
## 0.87.1
|
|
46
|
+
|
|
47
|
+
## 0.87.0
|
|
48
|
+
|
|
49
|
+
## 0.86.2
|
|
50
|
+
|
|
51
|
+
## 0.86.1
|
|
52
|
+
|
|
53
|
+
## 0.86.0
|
|
54
|
+
|
|
55
|
+
### Minor Changes
|
|
56
|
+
|
|
57
|
+
- **Breaking:** TS resolution has been changed to `Node16`, target to `es2022`. ([#1636](https://github.com/Siteimprove/alfa/pull/1636))
|
|
58
|
+
|
|
59
|
+
- **Breaking:** Alfa is now distributed as ESM rather than CJS modules; projects using it must be ESM or use dynamic `import()`. ([#1636](https://github.com/Siteimprove/alfa/pull/1636))
|
|
60
|
+
|
|
61
|
+
⚠️ This is the last of a series of changes on the internal structure and build process of distributed packages that was started with v0.85.0.
|
|
62
|
+
|
|
63
|
+
## 0.85.1
|
|
64
|
+
|
|
65
|
+
## 0.85.0
|
|
66
|
+
|
|
67
|
+
### Minor Changes
|
|
68
|
+
|
|
69
|
+
- **Breaking:** The .js files are now built in the `dist` folder rather than in `src`. ([#1628](https://github.com/Siteimprove/alfa/pull/1628))
|
|
70
|
+
|
|
71
|
+
⚠️ This is the first of a series of changes on the internal structure and build process of distributed packages. It is probably better to not use this version and wait until more of these internal changes have been done to jump directly to the final result. We are internally releasing these changes for validation purpose only.
|
|
72
|
+
|
|
73
|
+
This should not impact consumers, the `package.json` files should be set correctly to consume these files.
|
|
74
|
+
|
|
75
|
+
## 0.84.0
|
|
76
|
+
|
|
77
|
+
## 0.83.1
|
|
78
|
+
|
|
79
|
+
## 0.83.0
|
|
80
|
+
|
|
81
|
+
## 0.82.0
|
|
82
|
+
|
|
83
|
+
### Minor Changes
|
|
84
|
+
|
|
85
|
+
- **Breaking:** Node 18 is no longer supported. ([#1618](https://github.com/Siteimprove/alfa/pull/1618))
|
|
86
|
+
|
|
87
|
+
## 0.81.0
|
|
88
|
+
|
|
89
|
+
### Patch Changes
|
|
90
|
+
|
|
91
|
+
- **Added:** Each package now contains its internal dependency graph in its `docs` directory. ([#1610](https://github.com/Siteimprove/alfa/pull/1610))
|
|
92
|
+
|
|
93
|
+
## 0.80.0
|
|
94
|
+
|
|
95
|
+
## 0.79.1
|
|
96
|
+
|
|
97
|
+
## 0.79.0
|
|
98
|
+
|
|
99
|
+
## 0.78.2
|
|
100
|
+
|
|
101
|
+
## 0.78.1
|
|
102
|
+
|
|
103
|
+
## 0.78.0
|
|
104
|
+
|
|
105
|
+
## 0.77.0
|
|
106
|
+
|
|
107
|
+
## 0.76.0
|
|
108
|
+
|
|
109
|
+
## 0.75.2
|
|
110
|
+
|
|
111
|
+
## 0.75.1
|
|
112
|
+
|
|
113
|
+
## 0.75.0
|
|
114
|
+
|
|
115
|
+
## 0.74.0
|
|
116
|
+
|
|
117
|
+
## 0.73.0
|
|
118
|
+
|
|
119
|
+
## 0.72.0
|
|
120
|
+
|
|
121
|
+
## 0.71.1
|
|
122
|
+
|
|
123
|
+
## 0.71.0
|
|
124
|
+
|
|
125
|
+
## 0.70.0
|
|
126
|
+
|
|
127
|
+
## 0.69.0
|
|
128
|
+
|
|
129
|
+
## 0.68.0
|
|
130
|
+
|
|
131
|
+
## 0.67.0
|
|
132
|
+
|
|
133
|
+
## 0.66.0
|
|
134
|
+
|
|
135
|
+
## 0.65.1
|
|
136
|
+
|
|
137
|
+
## 0.65.0
|
|
138
|
+
|
|
139
|
+
## 0.64.0
|
|
140
|
+
|
|
141
|
+
## 0.63.3
|
|
142
|
+
|
|
143
|
+
## 0.63.2
|
|
144
|
+
|
|
145
|
+
## 0.63.1
|
|
146
|
+
|
|
147
|
+
## 0.63.0
|
|
148
|
+
|
|
149
|
+
## 0.62.2
|
|
150
|
+
|
|
151
|
+
## 0.62.1
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/matrix.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link https://en.wikipedia.org/wiki/Matrix_(mathematics)}
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type Matrix = Array<Array<number>>;
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export declare namespace Matrix {
|
|
11
|
+
function isMatrix(value: unknown): value is Matrix;
|
|
12
|
+
function isSquare(m: Matrix): boolean;
|
|
13
|
+
function clone(m: Matrix): Matrix;
|
|
14
|
+
function equals(m: Matrix, n: Matrix, e?: number): boolean;
|
|
15
|
+
function identity(n: number): Matrix;
|
|
16
|
+
function size(m: Matrix): [number, number];
|
|
17
|
+
function rows(m: Matrix): number;
|
|
18
|
+
function columns(m: Matrix): number;
|
|
19
|
+
function row(m: Matrix, i: number): Array<number>;
|
|
20
|
+
function column(m: Matrix, i: number): Array<number>;
|
|
21
|
+
function add(m: Matrix, n: Matrix): Matrix;
|
|
22
|
+
function subtract(m: Matrix, n: Matrix): Matrix;
|
|
23
|
+
/**
|
|
24
|
+
* {@link https://en.wikipedia.org/wiki/Matrix_multiplication}
|
|
25
|
+
*/
|
|
26
|
+
function multiply(m: Matrix, n: number | Matrix): Matrix;
|
|
27
|
+
/**
|
|
28
|
+
* Compute the transpose of a matrix.
|
|
29
|
+
*
|
|
30
|
+
* {@link https://en.wikipedia.org/wiki/Transpose}
|
|
31
|
+
*/
|
|
32
|
+
function transpose(m: Matrix): Matrix;
|
|
33
|
+
/**
|
|
34
|
+
* Compute the determinant of a non-empty, square matrix.
|
|
35
|
+
*
|
|
36
|
+
* {@link https://en.wikipedia.org/wiki/Determinant}
|
|
37
|
+
* {@link https://en.wikipedia.org/wiki/Laplace_expansion}
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* This function uses Laplace expansion for computing the determinant which
|
|
41
|
+
* has a time complexity of O(n!) and is therefore not practical for large
|
|
42
|
+
* matrices.
|
|
43
|
+
*/
|
|
44
|
+
function determinant(m: Matrix): number;
|
|
45
|
+
/**
|
|
46
|
+
* Compute the inverse of an invertible matrix.
|
|
47
|
+
*
|
|
48
|
+
* {@link https://en.wikipedia.org/wiki/Invertible_matrix}
|
|
49
|
+
* {@link https://en.wikipedia.org/wiki/Cramers_rule}
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* This function uses Cramer's rule for computing the inverse which has a time
|
|
53
|
+
* complexity of O(n!) and is therefore not practical for large matrices.
|
|
54
|
+
*/
|
|
55
|
+
function inverse(m: Matrix): Matrix;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=matrix.d.ts.map
|
package/dist/matrix.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Real } from "./real.js";
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export var Matrix;
|
|
6
|
+
(function (Matrix) {
|
|
7
|
+
function isMatrix(value) {
|
|
8
|
+
return (Array.isArray(value) &&
|
|
9
|
+
value.every((r) => Array.isArray(r) && r.every((v) => typeof v === "number")));
|
|
10
|
+
}
|
|
11
|
+
Matrix.isMatrix = isMatrix;
|
|
12
|
+
function isSquare(m) {
|
|
13
|
+
return rows(m) === columns(m);
|
|
14
|
+
}
|
|
15
|
+
Matrix.isSquare = isSquare;
|
|
16
|
+
function clone(m) {
|
|
17
|
+
return m.map((r) => r.slice(0));
|
|
18
|
+
}
|
|
19
|
+
Matrix.clone = clone;
|
|
20
|
+
function equals(m, n, e) {
|
|
21
|
+
return (m.length === n.length &&
|
|
22
|
+
m.every((r, i) => r.length === n[i].length &&
|
|
23
|
+
r.every((v, j) => Real.equals(v, n[i][j], e))));
|
|
24
|
+
}
|
|
25
|
+
Matrix.equals = equals;
|
|
26
|
+
function identity(n) {
|
|
27
|
+
return new Array(n).fill([]).map((_, i) => {
|
|
28
|
+
const r = new Array(n).fill(0);
|
|
29
|
+
r[i] = 1;
|
|
30
|
+
return r;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
Matrix.identity = identity;
|
|
34
|
+
function size(m) {
|
|
35
|
+
return [rows(m), columns(m)];
|
|
36
|
+
}
|
|
37
|
+
Matrix.size = size;
|
|
38
|
+
function rows(m) {
|
|
39
|
+
return m.length;
|
|
40
|
+
}
|
|
41
|
+
Matrix.rows = rows;
|
|
42
|
+
function columns(m) {
|
|
43
|
+
return m.length === 0 ? 0 : m[0].length;
|
|
44
|
+
}
|
|
45
|
+
Matrix.columns = columns;
|
|
46
|
+
function row(m, i) {
|
|
47
|
+
return m[i];
|
|
48
|
+
}
|
|
49
|
+
Matrix.row = row;
|
|
50
|
+
function column(m, i) {
|
|
51
|
+
return m.map((r) => r[i]);
|
|
52
|
+
}
|
|
53
|
+
Matrix.column = column;
|
|
54
|
+
function add(m, n) {
|
|
55
|
+
return m.map((r, i) => r.map((v, j) => v + n?.[i]?.[j]));
|
|
56
|
+
}
|
|
57
|
+
Matrix.add = add;
|
|
58
|
+
function subtract(m, n) {
|
|
59
|
+
return m.map((r, i) => r.map((v, j) => v - n?.[i]?.[j]));
|
|
60
|
+
}
|
|
61
|
+
Matrix.subtract = subtract;
|
|
62
|
+
/**
|
|
63
|
+
* {@link https://en.wikipedia.org/wiki/Matrix_multiplication}
|
|
64
|
+
*/
|
|
65
|
+
function multiply(m, n) {
|
|
66
|
+
return typeof n === "number"
|
|
67
|
+
? m.map((r) => r.map((v) => v * n))
|
|
68
|
+
: m.map((r, i) => n[0].map((_, j) => r.reduce((s, _, k) => s + m[i][k] * n?.[k]?.[j], 0)));
|
|
69
|
+
}
|
|
70
|
+
Matrix.multiply = multiply;
|
|
71
|
+
/**
|
|
72
|
+
* Compute the transpose of a matrix.
|
|
73
|
+
*
|
|
74
|
+
* {@link https://en.wikipedia.org/wiki/Transpose}
|
|
75
|
+
*/
|
|
76
|
+
function transpose(m) {
|
|
77
|
+
return m.length === 0 ? m : m[0].map((_, i) => m.map((row) => row[i]));
|
|
78
|
+
}
|
|
79
|
+
Matrix.transpose = transpose;
|
|
80
|
+
/**
|
|
81
|
+
* Compute the determinant of a non-empty, square matrix.
|
|
82
|
+
*
|
|
83
|
+
* {@link https://en.wikipedia.org/wiki/Determinant}
|
|
84
|
+
* {@link https://en.wikipedia.org/wiki/Laplace_expansion}
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* This function uses Laplace expansion for computing the determinant which
|
|
88
|
+
* has a time complexity of O(n!) and is therefore not practical for large
|
|
89
|
+
* matrices.
|
|
90
|
+
*/
|
|
91
|
+
function determinant(m) {
|
|
92
|
+
switch (m.length) {
|
|
93
|
+
// The determinant of a 1x1 matrix is the single value of the matrix.
|
|
94
|
+
case 1:
|
|
95
|
+
return m[0][0];
|
|
96
|
+
// The determinant of a 2x2 matrix is fairly short to write out and so
|
|
97
|
+
// this is done as an optimization.
|
|
98
|
+
case 2:
|
|
99
|
+
return m[0]?.[0] * m[1]?.[1] - m[0]?.[1] * m[1]?.[0];
|
|
100
|
+
default:
|
|
101
|
+
return m.reduce((d, _, i) => d + m[0]?.[i] * cofactor(m, 0, i), 0);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
Matrix.determinant = determinant;
|
|
105
|
+
/**
|
|
106
|
+
* Compute the inverse of an invertible matrix.
|
|
107
|
+
*
|
|
108
|
+
* {@link https://en.wikipedia.org/wiki/Invertible_matrix}
|
|
109
|
+
* {@link https://en.wikipedia.org/wiki/Cramers_rule}
|
|
110
|
+
*
|
|
111
|
+
* @remarks
|
|
112
|
+
* This function uses Cramer's rule for computing the inverse which has a time
|
|
113
|
+
* complexity of O(n!) and is therefore not practical for large matrices.
|
|
114
|
+
*/
|
|
115
|
+
function inverse(m) {
|
|
116
|
+
return multiply(adjugate(m), 1 / determinant(m));
|
|
117
|
+
}
|
|
118
|
+
Matrix.inverse = inverse;
|
|
119
|
+
/**
|
|
120
|
+
* Compute the (i, j) cofactor of a non-empty, square matrix.
|
|
121
|
+
*
|
|
122
|
+
* {@link https://en.wikipedia.org/wiki/Minor_(linear_algebra)}
|
|
123
|
+
* @internal
|
|
124
|
+
*/
|
|
125
|
+
function cofactor(m, i, j) {
|
|
126
|
+
return (-1) ** (i + j) * minor(m, i, j);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Compute the (i, j) minor of a non-empty, square matrix.
|
|
130
|
+
*
|
|
131
|
+
* {@link https://en.wikipedia.org/wiki/Minor_(linear_algebra)}
|
|
132
|
+
* @internal
|
|
133
|
+
*/
|
|
134
|
+
function minor(m, i, j) {
|
|
135
|
+
return determinant(m
|
|
136
|
+
// Remove the i-th row
|
|
137
|
+
.filter((_, k) => k !== i)
|
|
138
|
+
.map((r) => r
|
|
139
|
+
// Remove the j-th column
|
|
140
|
+
.filter((_, l) => l !== j)));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Compute the adjugate of a non-empty, square matrix.
|
|
144
|
+
*
|
|
145
|
+
* {@link https://en.wikipedia.org/wiki/Adjugate_matrix}
|
|
146
|
+
* @internal
|
|
147
|
+
*/
|
|
148
|
+
function adjugate(m) {
|
|
149
|
+
return transpose(m.map((r, i) => r.map((_, j) => cofactor(m, i, j))));
|
|
150
|
+
}
|
|
151
|
+
})(Matrix || (Matrix = {}));
|
|
152
|
+
//# sourceMappingURL=matrix.js.map
|
package/dist/real.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link https://en.wikipedia.org/wiki/Real_number}
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export declare namespace Real {
|
|
7
|
+
function isReal(value: number): boolean;
|
|
8
|
+
function isReal(value: unknown): value is number;
|
|
9
|
+
function add(p: number, q: number): number;
|
|
10
|
+
function subtract(p: number, q: number): number;
|
|
11
|
+
function multiply(p: number, q: number): number;
|
|
12
|
+
function divide(p: number, d: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* Compute the modulus of a division of two reals.
|
|
15
|
+
*
|
|
16
|
+
* {@link https://en.wikipedia.org/wiki/Modulo_operation}
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* The modulo operation is different from the remainder operation supported
|
|
20
|
+
* natively in JavaScript through the % operator.
|
|
21
|
+
*/
|
|
22
|
+
function modulo(p: number, d: number): number;
|
|
23
|
+
/**
|
|
24
|
+
* Clamp a real between a lower and an upper value.
|
|
25
|
+
*/
|
|
26
|
+
function clamp(p: number, l: number, u: number): number;
|
|
27
|
+
/**
|
|
28
|
+
* Round a real to a given number of decimals.
|
|
29
|
+
*/
|
|
30
|
+
function round(p: number, n?: number): number;
|
|
31
|
+
/**
|
|
32
|
+
* Check if two reals are equal, accounting for floating-point precision
|
|
33
|
+
* errors according to a given epsilon.
|
|
34
|
+
*/
|
|
35
|
+
function equals(a: number, b: number, e?: number): boolean;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=real.d.ts.map
|
package/dist/real.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const { abs, min, max } = Math;
|
|
2
|
+
/**
|
|
3
|
+
* {@link https://en.wikipedia.org/wiki/Real_number}
|
|
4
|
+
*
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export var Real;
|
|
8
|
+
(function (Real) {
|
|
9
|
+
function isReal(value) {
|
|
10
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
11
|
+
}
|
|
12
|
+
Real.isReal = isReal;
|
|
13
|
+
function add(p, q) {
|
|
14
|
+
return p + q;
|
|
15
|
+
}
|
|
16
|
+
Real.add = add;
|
|
17
|
+
function subtract(p, q) {
|
|
18
|
+
return p - q;
|
|
19
|
+
}
|
|
20
|
+
Real.subtract = subtract;
|
|
21
|
+
function multiply(p, q) {
|
|
22
|
+
return p * q;
|
|
23
|
+
}
|
|
24
|
+
Real.multiply = multiply;
|
|
25
|
+
function divide(p, d) {
|
|
26
|
+
return p / d;
|
|
27
|
+
}
|
|
28
|
+
Real.divide = divide;
|
|
29
|
+
/**
|
|
30
|
+
* Compute the modulus of a division of two reals.
|
|
31
|
+
*
|
|
32
|
+
* {@link https://en.wikipedia.org/wiki/Modulo_operation}
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* The modulo operation is different from the remainder operation supported
|
|
36
|
+
* natively in JavaScript through the % operator.
|
|
37
|
+
*/
|
|
38
|
+
function modulo(p, d) {
|
|
39
|
+
return ((p % d) + d) % d;
|
|
40
|
+
}
|
|
41
|
+
Real.modulo = modulo;
|
|
42
|
+
/**
|
|
43
|
+
* Clamp a real between a lower and an upper value.
|
|
44
|
+
*/
|
|
45
|
+
function clamp(p, l, u) {
|
|
46
|
+
return max(l, min(p, u));
|
|
47
|
+
}
|
|
48
|
+
Real.clamp = clamp;
|
|
49
|
+
/**
|
|
50
|
+
* Round a real to a given number of decimals.
|
|
51
|
+
*/
|
|
52
|
+
function round(p, n = 0) {
|
|
53
|
+
return n === 0 ? Math.round(p) : Math.round(p * 10 ** n) / 10 ** n;
|
|
54
|
+
}
|
|
55
|
+
Real.round = round;
|
|
56
|
+
/**
|
|
57
|
+
* Check if two reals are equal, accounting for floating-point precision
|
|
58
|
+
* errors according to a given epsilon.
|
|
59
|
+
*/
|
|
60
|
+
function equals(a, b, e = Number.EPSILON) {
|
|
61
|
+
if (a === b) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
const diff = abs(a - b);
|
|
65
|
+
if (a === 0 || b === 0) {
|
|
66
|
+
return diff < e;
|
|
67
|
+
}
|
|
68
|
+
return diff / min(abs(b) + abs(a), Number.MAX_VALUE) < e;
|
|
69
|
+
}
|
|
70
|
+
Real.equals = equals;
|
|
71
|
+
})(Real || (Real = {}));
|
|
72
|
+
//# sourceMappingURL=real.js.map
|
package/dist/vector.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)}
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type Vector = Array<number>;
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export declare namespace Vector {
|
|
11
|
+
function isVector(value: unknown): value is Vector;
|
|
12
|
+
function clone(v: Vector): Vector;
|
|
13
|
+
function equals(v: Vector, u: Vector, e: number): boolean;
|
|
14
|
+
function size(v: Vector): number;
|
|
15
|
+
function add(v: Vector, u: Vector): Vector;
|
|
16
|
+
function subtract(v: Vector, u: Vector): Vector;
|
|
17
|
+
function multiply(v: Vector, s: number): Vector;
|
|
18
|
+
function divide(v: Vector, d: number): Vector;
|
|
19
|
+
/**
|
|
20
|
+
* Compute the dot product of two non-empty, equal length vectors.
|
|
21
|
+
*
|
|
22
|
+
* {@link https://en.wikipedia.org/wiki/Dot_product}
|
|
23
|
+
*/
|
|
24
|
+
function dot(v: Vector, u: Vector): number;
|
|
25
|
+
/**
|
|
26
|
+
* Compute the cross product of two 3-dimensional vectors.
|
|
27
|
+
*
|
|
28
|
+
* {@link https://en.wikipedia.org/wiki/Cross_product}
|
|
29
|
+
*/
|
|
30
|
+
function cross(v: Vector, u: Vector): Vector;
|
|
31
|
+
/**
|
|
32
|
+
* Compute the norm of a vector.
|
|
33
|
+
*
|
|
34
|
+
* {@link https://en.wikipedia.org/wiki/Norm_(mathematics)}
|
|
35
|
+
*/
|
|
36
|
+
function norm(v: Vector): number;
|
|
37
|
+
/**
|
|
38
|
+
* Compute a unit vector corresponding to a vector.
|
|
39
|
+
*
|
|
40
|
+
* {@link https://en.wikipedia.org/wiki/Unit_vector}
|
|
41
|
+
*/
|
|
42
|
+
function normalize(v: Vector): Vector;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=vector.d.ts.map
|
package/dist/vector.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Real } from "./real.js";
|
|
2
|
+
const { sqrt } = Math;
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export var Vector;
|
|
7
|
+
(function (Vector) {
|
|
8
|
+
function isVector(value) {
|
|
9
|
+
return Array.isArray(value) && value.every((n) => typeof n === "number");
|
|
10
|
+
}
|
|
11
|
+
Vector.isVector = isVector;
|
|
12
|
+
function clone(v) {
|
|
13
|
+
return v.slice(0);
|
|
14
|
+
}
|
|
15
|
+
Vector.clone = clone;
|
|
16
|
+
function equals(v, u, e) {
|
|
17
|
+
return v.length === u.length && v.every((n, i) => Real.equals(n, u[i], e));
|
|
18
|
+
}
|
|
19
|
+
Vector.equals = equals;
|
|
20
|
+
function size(v) {
|
|
21
|
+
return v.length;
|
|
22
|
+
}
|
|
23
|
+
Vector.size = size;
|
|
24
|
+
function add(v, u) {
|
|
25
|
+
return v.map((n, i) => n + u?.[i]);
|
|
26
|
+
}
|
|
27
|
+
Vector.add = add;
|
|
28
|
+
function subtract(v, u) {
|
|
29
|
+
return v.map((n, i) => n - u?.[i]);
|
|
30
|
+
}
|
|
31
|
+
Vector.subtract = subtract;
|
|
32
|
+
function multiply(v, s) {
|
|
33
|
+
return v.map((n) => n * s);
|
|
34
|
+
}
|
|
35
|
+
Vector.multiply = multiply;
|
|
36
|
+
function divide(v, d) {
|
|
37
|
+
return v.map((n) => n / d);
|
|
38
|
+
}
|
|
39
|
+
Vector.divide = divide;
|
|
40
|
+
/**
|
|
41
|
+
* Compute the dot product of two non-empty, equal length vectors.
|
|
42
|
+
*
|
|
43
|
+
* {@link https://en.wikipedia.org/wiki/Dot_product}
|
|
44
|
+
*/
|
|
45
|
+
function dot(v, u) {
|
|
46
|
+
return v.reduce((s, n, i) => s + n * u?.[i], 0);
|
|
47
|
+
}
|
|
48
|
+
Vector.dot = dot;
|
|
49
|
+
/**
|
|
50
|
+
* Compute the cross product of two 3-dimensional vectors.
|
|
51
|
+
*
|
|
52
|
+
* {@link https://en.wikipedia.org/wiki/Cross_product}
|
|
53
|
+
*/
|
|
54
|
+
function cross(v, u) {
|
|
55
|
+
const [vx, vy, vz] = v;
|
|
56
|
+
const [ux, uy, uz] = u;
|
|
57
|
+
return [vy * uz - vz * uy, vz * ux - vx * uz, vx * uy - vy * ux];
|
|
58
|
+
}
|
|
59
|
+
Vector.cross = cross;
|
|
60
|
+
/**
|
|
61
|
+
* Compute the norm of a vector.
|
|
62
|
+
*
|
|
63
|
+
* {@link https://en.wikipedia.org/wiki/Norm_(mathematics)}
|
|
64
|
+
*/
|
|
65
|
+
function norm(v) {
|
|
66
|
+
return sqrt(v.reduce((s, n) => s + n ** 2, 0));
|
|
67
|
+
}
|
|
68
|
+
Vector.norm = norm;
|
|
69
|
+
/**
|
|
70
|
+
* Compute a unit vector corresponding to a vector.
|
|
71
|
+
*
|
|
72
|
+
* {@link https://en.wikipedia.org/wiki/Unit_vector}
|
|
73
|
+
*/
|
|
74
|
+
function normalize(v) {
|
|
75
|
+
return divide(v, norm(v));
|
|
76
|
+
}
|
|
77
|
+
Vector.normalize = normalize;
|
|
78
|
+
})(Vector || (Vector = {}));
|
|
79
|
+
//# sourceMappingURL=vector.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json.schemastore.org/package",
|
|
3
|
+
"name": "@siteimprove/alfa-math",
|
|
4
|
+
"homepage": "https://alfa.siteimprove.com",
|
|
5
|
+
"version": "0.89.5",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"description": "Functionality for working with numbers and mathematical structures, such as vectors and matrices",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "github:Siteimprove/alfa",
|
|
11
|
+
"directory": "packages/alfa-math"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/siteimprove/alfa/issues",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20.0.0"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "dist/index.js",
|
|
19
|
+
"types": "dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/**/*.js",
|
|
22
|
+
"dist/**/*.d.ts"
|
|
23
|
+
],
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@siteimprove/alfa-test": "^0.89.5"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"registry": "https://npm.pkg.github.com/"
|
|
30
|
+
}
|
|
31
|
+
}
|