@smockle/matrix 5.0.0 → 5.0.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.
- package/dist/index.d.ts +40 -9
- package/dist/index.js +20 -60
- 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,40 @@ 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
|
-
var multiply:
|
|
35
|
-
var invert:
|
|
38
|
+
var multiply: typeof Multiply;
|
|
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
|
+
* Calculates the dot product of two matrices
|
|
53
|
+
* @alias module:matrix.multiply
|
|
54
|
+
* @param {Matrix} x - Matrix to multiply
|
|
55
|
+
* @param {Matrix} y - Matrix to multiply
|
|
56
|
+
* @return {Matrix} New matrix with the dot product
|
|
57
|
+
*/
|
|
58
|
+
declare function Multiply(x: Matrix1D, y: Matrix): Matrix1D;
|
|
59
|
+
declare function Multiply(x: Matrix2D, y: Matrix): Matrix2D;
|
|
60
|
+
/**
|
|
61
|
+
* Inverts a matrix. Matrix must be a square (e.g. 1x1 or 2x2)
|
|
62
|
+
* @alias module:matrix.invert
|
|
63
|
+
* @param {x} Matrix to invert
|
|
64
|
+
* @return {Matrix} Matrix inverse
|
|
65
|
+
*/
|
|
66
|
+
declare function Invert(x: Matrix1D): Matrix1D;
|
|
67
|
+
declare function Invert(x: Matrix2D): Matrix2D;
|
|
37
68
|
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
|
|
@@ -80,14 +65,7 @@ function innerproduct(x, y, i) {
|
|
|
80
65
|
}
|
|
81
66
|
return [..._x].reduce((z, _z, j) => z + _z * _y[j], 0);
|
|
82
67
|
}
|
|
83
|
-
|
|
84
|
-
* Calculates the dot product of two matrices
|
|
85
|
-
* @alias module:matrix.multiply
|
|
86
|
-
* @param {Matrix} x - Matrix to multiply
|
|
87
|
-
* @param {Matrix} y - Matrix to multiply
|
|
88
|
-
* @return {Matrix} New matrix with the dot product
|
|
89
|
-
*/
|
|
90
|
-
Matrix.multiply = function (x, y) {
|
|
68
|
+
function Multiply(x, y) {
|
|
91
69
|
if (!Matrix.multipliable(x, y)) {
|
|
92
70
|
throw new TypeError("Matrices are not multipliable");
|
|
93
71
|
}
|
|
@@ -103,16 +81,12 @@ Matrix.multiply = function (x, y) {
|
|
|
103
81
|
}
|
|
104
82
|
});
|
|
105
83
|
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
*/
|
|
113
|
-
Matrix.invert = function (x) {
|
|
114
|
-
return Matrix(inv(x instanceof Matrix ? x.__value : x));
|
|
115
|
-
};
|
|
84
|
+
}
|
|
85
|
+
Matrix.multiply = Multiply;
|
|
86
|
+
function Invert(x) {
|
|
87
|
+
return Matrix(inv(x.__value));
|
|
88
|
+
}
|
|
89
|
+
Matrix.invert = Invert;
|
|
116
90
|
/**
|
|
117
91
|
* Counts rows in this matrix
|
|
118
92
|
* @alias module:matrix#countRows
|
|
@@ -142,15 +116,10 @@ Matrix.prototype.countColumns = function () {
|
|
|
142
116
|
Matrix.prototype.addable = function (y) {
|
|
143
117
|
return Matrix.addable(this, y);
|
|
144
118
|
};
|
|
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) {
|
|
119
|
+
function add(y) {
|
|
152
120
|
return Matrix.add(this, y);
|
|
153
|
-
}
|
|
121
|
+
}
|
|
122
|
+
Matrix.prototype.add = add;
|
|
154
123
|
/**
|
|
155
124
|
* Determines whether this matrix can be multiplied
|
|
156
125
|
* @alias module:matrix#multipliable
|
|
@@ -160,15 +129,10 @@ Matrix.prototype.add = function (y) {
|
|
|
160
129
|
Matrix.prototype.multipliable = function (y) {
|
|
161
130
|
return Matrix.multipliable(this, y);
|
|
162
131
|
};
|
|
163
|
-
|
|
164
|
-
* Calculates the dot product of this matrix
|
|
165
|
-
* @alias module:matrix#multiply
|
|
166
|
-
* @param {Matrix} y - Matrix to multiply
|
|
167
|
-
* @return {Matrix} New matrix with the dot product
|
|
168
|
-
*/
|
|
169
|
-
Matrix.prototype.multiply = function (y) {
|
|
132
|
+
function multiply(y) {
|
|
170
133
|
return Matrix.multiply(this, y);
|
|
171
|
-
}
|
|
134
|
+
}
|
|
135
|
+
Matrix.prototype.multiply = multiply;
|
|
172
136
|
/**
|
|
173
137
|
* Calculates the transpose of this matrix
|
|
174
138
|
* @alias module:matrix#transpose
|
|
@@ -182,14 +146,10 @@ Matrix.prototype.transpose = function () {
|
|
|
182
146
|
return Matrix(unzip(this.__value));
|
|
183
147
|
}
|
|
184
148
|
};
|
|
185
|
-
|
|
186
|
-
* Inverts this matrix
|
|
187
|
-
* @alias module:matrix#invert
|
|
188
|
-
* @return {Matrix} Matrix inverse
|
|
189
|
-
*/
|
|
190
|
-
Matrix.prototype.invert = function () {
|
|
149
|
+
function invert() {
|
|
191
150
|
return Matrix.invert(this);
|
|
192
|
-
}
|
|
151
|
+
}
|
|
152
|
+
Matrix.prototype.invert = invert;
|
|
193
153
|
/**
|
|
194
154
|
* Maps over this matrix
|
|
195
155
|
* @alias module:matrix#map
|