@@ -5,27 +5,29 @@ interface IMatrix {
5
5
countRows : ( ) => number ;
6
6
countColumns : ( ) => number ;
7
7
addable : ( y : Matrix ) => boolean ;
8
- add : ( y : Matrix ) => Matrix ;
9
8
multipliable : ( y : Matrix ) => boolean ;
10
9
multiply : ( y : Matrix ) => Matrix ;
11
- transpose : ( ) => Matrix ;
12
- invert : ( ) => Matrix ;
10
+ transpose : ( ) => Matrix2D ;
13
11
map : ( x : any ) => Matrix ;
14
12
}
15
13
16
- interface Matrix1D extends IMatrix {
14
+ export interface Matrix1D extends IMatrix {
17
15
__value : number [ ] ;
16
+ add : ( y : Matrix1D ) => Matrix1D ;
17
+ invert : ( ) => Matrix1D ;
18
18
valueOf : ( ) => number [ ] ;
19
19
}
20
20
21
- interface Matrix2D extends IMatrix {
21
+ export interface Matrix2D extends IMatrix {
22
22
__value : number [ ] [ ] ;
23
+ add : ( y : Matrix2D ) => Matrix2D ;
24
+ invert : ( ) => Matrix2D ;
23
25
valueOf : ( ) => number [ ] [ ] ;
24
26
}
25
27
26
28
type Matrix = Matrix1D | Matrix2D ;
27
29
28
- function isMatrix1D ( matrix : Matrix ) : matrix is Matrix1D {
30
+ export function isMatrix1D ( matrix : Matrix ) : matrix is Matrix1D {
29
31
return matrix . countRows ( ) === 1 ;
30
32
}
31
33
@@ -37,7 +39,9 @@ function isMatrix1D(matrix: Matrix): matrix is Matrix1D {
37
39
* @throws {TypeError } Argument x must be a number or number array
38
40
* @return {Matrix } Single or multi dimensional matrix
39
41
*/
40
- function Matrix ( x : number [ ] | number [ ] [ ] ) : Matrix {
42
+ function Matrix ( x : number [ ] ) : Matrix1D ;
43
+ function Matrix ( x : number [ ] [ ] ) : Matrix2D ;
44
+ function Matrix < T extends number [ ] | number [ ] [ ] > ( x : T ) : Matrix1D | Matrix2D {
41
45
// extra nesting
42
46
if ( Array . isArray ( x [ 0 ] ) && x . length === 1 ) {
43
47
throw new TypeError ( "Matrix must be a number or array of numbers" ) ;
@@ -79,14 +83,17 @@ Matrix.addable = function (x: Matrix, y: Matrix): boolean {
79
83
* @throws {TypeError } Matrices are not addable
80
84
* @return {Matrix } New matrix with the summation
81
85
*/
82
- Matrix . add = function ( x : Matrix , y : Matrix ) : Matrix {
86
+ function Add ( x : Matrix1D , y : Matrix1D ) : Matrix1D ;
87
+ function Add ( x : Matrix2D , y : Matrix2D ) : Matrix2D ;
88
+ function Add < T extends Matrix1D & Matrix2D > ( x : T , y : T ) : Matrix1D | Matrix2D {
83
89
if ( ! Matrix . addable ( x , y ) ) throw new TypeError ( "Matrices are not addable" ) ;
84
90
return x . map ( ( row : number [ ] , i : number ) : number [ ] =>
85
91
row . map ( ( column : number , j : number ) : number => {
86
92
return column + ( Array . isArray ( y . __value [ i ] ) ? y . __value [ i ] [ j ] : 0 ) ;
87
93
} )
88
94
) ;
89
- } ;
95
+ }
96
+ Matrix . add = Add ;
90
97
91
98
/**
92
99
* Determines whether two matrices can be multiplied
@@ -150,14 +157,17 @@ Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
150
157
} ;
151
158
152
159
/**
153
- * Inverts a matrix
160
+ * Inverts a matrix. Matrix must be a square (e.g. 1x1 or 2x2)
154
161
* @alias module:matrix.invert
155
162
* @param {x } Matrix to invert
156
163
* @return {Matrix } Matrix inverse
157
164
*/
158
- Matrix . invert = function ( x : Matrix ) : Matrix {
159
- return Matrix ( inv < any > ( x instanceof Matrix ? x . __value : x ) ) ;
160
- } ;
165
+ function Invert ( x : Matrix1D ) : Matrix1D ;
166
+ function Invert ( x : Matrix2D ) : Matrix2D ;
167
+ function Invert < T extends Matrix1D & Matrix2D > ( x : T ) : Matrix1D | Matrix2D {
168
+ return Matrix ( inv < any > ( x . __value ) ) ;
169
+ }
170
+ Matrix . invert = Invert ;
161
171
162
172
/**
163
173
* Counts rows in this matrix
@@ -195,9 +205,15 @@ Matrix.prototype.addable = function (this: Matrix, y: Matrix): boolean {
195
205
* @param {Matrix } y - Matrix to add
196
206
* @return {Matrix } New matrix with the summation
197
207
*/
198
- Matrix . prototype . add = function ( this : Matrix , y : Matrix ) : Matrix {
208
+ function add ( this : Matrix1D , y : Matrix1D ) : Matrix1D ;
209
+ function add ( this : Matrix2D , y : Matrix2D ) : Matrix2D ;
210
+ function add < T extends Matrix1D & Matrix2D > (
211
+ this : T ,
212
+ y : T
213
+ ) : Matrix1D | Matrix2D {
199
214
return Matrix . add ( this , y ) ;
200
- } ;
215
+ }
216
+ Matrix . prototype . add = add ;
201
217
202
218
/**
203
219
* Determines whether this matrix can be multiplied
@@ -224,7 +240,7 @@ Matrix.prototype.multiply = function (this: Matrix, y: Matrix): Matrix {
224
240
* @alias module:matrix#transpose
225
241
* @return {Matrix } New matrix with the transpose
226
242
*/
227
- Matrix . prototype . transpose = function ( this : Matrix ) : Matrix {
243
+ Matrix . prototype . transpose = function ( this : Matrix ) : Matrix2D {
228
244
if ( isMatrix1D ( this ) ) {
229
245
return Matrix ( unzip ( [ this . __value ] ) ) ;
230
246
} else {
@@ -237,9 +253,12 @@ Matrix.prototype.transpose = function (this: Matrix): Matrix {
237
253
* @alias module:matrix#invert
238
254
* @return {Matrix } Matrix inverse
239
255
*/
240
- Matrix . prototype . invert = function ( this : Matrix ) : Matrix {
256
+ function invert ( this : Matrix1D ) : Matrix1D ;
257
+ function invert ( this : Matrix2D ) : Matrix2D ;
258
+ function invert < T extends Matrix1D & Matrix2D > ( this : T ) : Matrix1D | Matrix2D {
241
259
return Matrix . invert ( this ) ;
242
- } ;
260
+ }
261
+ Matrix . prototype . invert = invert ;
243
262
244
263
/**
245
264
* Maps over this matrix
0 commit comments