@smockle/matrix 5.0.0 → 5.0.1
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/dist/index.d.ts +30 -8
- package/dist/index.js +14 -43
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,22 +2,25 @@ interface IMatrix {
|
|
|
2
2
|
countRows: () => number;
|
|
3
3
|
countColumns: () => number;
|
|
4
4
|
addable: (y: Matrix) => boolean;
|
|
5
|
-
add: (y: Matrix) => Matrix;
|
|
6
5
|
multipliable: (y: Matrix) => boolean;
|
|
7
6
|
multiply: (y: Matrix) => Matrix;
|
|
8
|
-
transpose: () =>
|
|
9
|
-
invert: () => Matrix;
|
|
7
|
+
transpose: () => Matrix2D;
|
|
10
8
|
map: (x: any) => Matrix;
|
|
11
9
|
}
|
|
12
|
-
interface Matrix1D extends IMatrix {
|
|
10
|
+
export interface Matrix1D extends IMatrix {
|
|
13
11
|
__value: number[];
|
|
12
|
+
add: (y: Matrix1D) => Matrix1D;
|
|
13
|
+
invert: () => Matrix1D;
|
|
14
14
|
valueOf: () => number[];
|
|
15
15
|
}
|
|
16
|
-
interface Matrix2D extends IMatrix {
|
|
16
|
+
export interface Matrix2D extends IMatrix {
|
|
17
17
|
__value: number[][];
|
|
18
|
+
add: (y: Matrix2D) => Matrix2D;
|
|
19
|
+
invert: () => Matrix2D;
|
|
18
20
|
valueOf: () => number[][];
|
|
19
21
|
}
|
|
20
22
|
type Matrix = Matrix1D | Matrix2D;
|
|
23
|
+
export declare function isMatrix1D(matrix: Matrix): matrix is Matrix1D;
|
|
21
24
|
/**
|
|
22
25
|
* Creates a Matrix
|
|
23
26
|
* @constructor
|
|
@@ -26,12 +29,31 @@ type Matrix = Matrix1D | Matrix2D;
|
|
|
26
29
|
* @throws {TypeError} Argument x must be a number or number array
|
|
27
30
|
* @return {Matrix} Single or multi dimensional matrix
|
|
28
31
|
*/
|
|
29
|
-
declare function Matrix(x: number[]
|
|
32
|
+
declare function Matrix(x: number[]): Matrix1D;
|
|
33
|
+
declare function Matrix(x: number[][]): Matrix2D;
|
|
30
34
|
declare namespace Matrix {
|
|
31
35
|
var addable: (x: Matrix, y: Matrix) => boolean;
|
|
32
|
-
var add:
|
|
36
|
+
var add: typeof Add;
|
|
33
37
|
var multipliable: (x: Matrix, y: Matrix) => boolean;
|
|
34
38
|
var multiply: (x: Matrix, y: Matrix) => Matrix;
|
|
35
|
-
var invert:
|
|
39
|
+
var invert: typeof Invert;
|
|
36
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Adds two matrices using matrix addition
|
|
43
|
+
* @alias module:matrix.add
|
|
44
|
+
* @param {Matrix} x - Matrix to add
|
|
45
|
+
* @param {Matrix} y - Matrix to add
|
|
46
|
+
* @throws {TypeError} Matrices are not addable
|
|
47
|
+
* @return {Matrix} New matrix with the summation
|
|
48
|
+
*/
|
|
49
|
+
declare function Add(x: Matrix1D, y: Matrix1D): Matrix1D;
|
|
50
|
+
declare function Add(x: Matrix2D, y: Matrix2D): Matrix2D;
|
|
51
|
+
/**
|
|
52
|
+
* Inverts a matrix. Matrix must be a square (e.g. 1x1 or 2x2)
|
|
53
|
+
* @alias module:matrix.invert
|
|
54
|
+
* @param {x} Matrix to invert
|
|
55
|
+
* @return {Matrix} Matrix inverse
|
|
56
|
+
*/
|
|
57
|
+
declare function Invert(x: Matrix1D): Matrix1D;
|
|
58
|
+
declare function Invert(x: Matrix2D): Matrix2D;
|
|
37
59
|
export default Matrix;
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
import { fill, padStart, unzip } from "lodash-es";
|
|
2
2
|
import { inv } from "mathjs";
|
|
3
|
-
function isMatrix1D(matrix) {
|
|
3
|
+
export function isMatrix1D(matrix) {
|
|
4
4
|
return matrix.countRows() === 1;
|
|
5
5
|
}
|
|
6
|
-
/**
|
|
7
|
-
* Creates a Matrix
|
|
8
|
-
* @constructor
|
|
9
|
-
* @alias module:matrix
|
|
10
|
-
* @param {number[] | number[][]} x - Values to store in matrix
|
|
11
|
-
* @throws {TypeError} Argument x must be a number or number array
|
|
12
|
-
* @return {Matrix} Single or multi dimensional matrix
|
|
13
|
-
*/
|
|
14
6
|
function Matrix(x) {
|
|
15
7
|
// extra nesting
|
|
16
8
|
if (Array.isArray(x[0]) && x.length === 1) {
|
|
@@ -37,21 +29,14 @@ function Matrix(x) {
|
|
|
37
29
|
Matrix.addable = function (x, y) {
|
|
38
30
|
return (x.countRows() === y.countRows() && x.countColumns() === y.countColumns());
|
|
39
31
|
};
|
|
40
|
-
|
|
41
|
-
* Adds two matrices using matrix addition
|
|
42
|
-
* @alias module:matrix.add
|
|
43
|
-
* @param {Matrix} x - Matrix to add
|
|
44
|
-
* @param {Matrix} y - Matrix to add
|
|
45
|
-
* @throws {TypeError} Matrices are not addable
|
|
46
|
-
* @return {Matrix} New matrix with the summation
|
|
47
|
-
*/
|
|
48
|
-
Matrix.add = function (x, y) {
|
|
32
|
+
function Add(x, y) {
|
|
49
33
|
if (!Matrix.addable(x, y))
|
|
50
34
|
throw new TypeError("Matrices are not addable");
|
|
51
35
|
return x.map((row, i) => row.map((column, j) => {
|
|
52
36
|
return column + (Array.isArray(y.__value[i]) ? y.__value[i][j] : 0);
|
|
53
37
|
}));
|
|
54
|
-
}
|
|
38
|
+
}
|
|
39
|
+
Matrix.add = Add;
|
|
55
40
|
/**
|
|
56
41
|
* Determines whether two matrices can be multiplied
|
|
57
42
|
* @alias module:matrix.multipliable
|
|
@@ -104,15 +89,10 @@ Matrix.multiply = function (x, y) {
|
|
|
104
89
|
});
|
|
105
90
|
}
|
|
106
91
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
* @return {Matrix} Matrix inverse
|
|
112
|
-
*/
|
|
113
|
-
Matrix.invert = function (x) {
|
|
114
|
-
return Matrix(inv(x instanceof Matrix ? x.__value : x));
|
|
115
|
-
};
|
|
92
|
+
function Invert(x) {
|
|
93
|
+
return Matrix(inv(x.__value));
|
|
94
|
+
}
|
|
95
|
+
Matrix.invert = Invert;
|
|
116
96
|
/**
|
|
117
97
|
* Counts rows in this matrix
|
|
118
98
|
* @alias module:matrix#countRows
|
|
@@ -142,15 +122,10 @@ Matrix.prototype.countColumns = function () {
|
|
|
142
122
|
Matrix.prototype.addable = function (y) {
|
|
143
123
|
return Matrix.addable(this, y);
|
|
144
124
|
};
|
|
145
|
-
|
|
146
|
-
* Adds this matrix using matrix addition
|
|
147
|
-
* @alias module:matrix#add
|
|
148
|
-
* @param {Matrix} y - Matrix to add
|
|
149
|
-
* @return {Matrix} New matrix with the summation
|
|
150
|
-
*/
|
|
151
|
-
Matrix.prototype.add = function (y) {
|
|
125
|
+
function add(y) {
|
|
152
126
|
return Matrix.add(this, y);
|
|
153
|
-
}
|
|
127
|
+
}
|
|
128
|
+
Matrix.prototype.add = add;
|
|
154
129
|
/**
|
|
155
130
|
* Determines whether this matrix can be multiplied
|
|
156
131
|
* @alias module:matrix#multipliable
|
|
@@ -182,14 +157,10 @@ Matrix.prototype.transpose = function () {
|
|
|
182
157
|
return Matrix(unzip(this.__value));
|
|
183
158
|
}
|
|
184
159
|
};
|
|
185
|
-
|
|
186
|
-
* Inverts this matrix
|
|
187
|
-
* @alias module:matrix#invert
|
|
188
|
-
* @return {Matrix} Matrix inverse
|
|
189
|
-
*/
|
|
190
|
-
Matrix.prototype.invert = function () {
|
|
160
|
+
function invert() {
|
|
191
161
|
return Matrix.invert(this);
|
|
192
|
-
}
|
|
162
|
+
}
|
|
163
|
+
Matrix.prototype.invert = invert;
|
|
193
164
|
/**
|
|
194
165
|
* Maps over this matrix
|
|
195
166
|
* @alias module:matrix#map
|