@smockle/matrix 3.0.5 → 4.0.0
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/.github/CODEOWNERS +1 -0
- package/.github/workflows/publish.yml +33 -0
- package/.github/workflows/test.yml +30 -0
- package/LICENSE +21 -13
- package/README.md +97 -82
- package/package.json +28 -39
- package/src/__tests__/index.test.ts +540 -0
- package/src/index.ts +290 -0
- package/tsconfig.json +17 -0
- package/.editorconfig +0 -7
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -6
- package/.flowconfig +0 -27
- package/.prettierignore +0 -1
- package/.travis.yml +0 -19
- package/appveyor.yml +0 -21
- package/flow-typed/npm/@std/esm_vx.x.x.js +0 -33
- package/flow-typed/npm/codecov_vx.x.x.js +0 -276
- package/flow-typed/npm/colortape_vx.x.x.js +0 -80
- package/flow-typed/npm/eslint-config-standard_vx.x.x.js +0 -45
- package/flow-typed/npm/eslint-plugin-import_vx.x.x.js +0 -333
- package/flow-typed/npm/eslint-plugin-node_vx.x.x.js +0 -249
- package/flow-typed/npm/eslint-plugin-promise_vx.x.x.js +0 -150
- package/flow-typed/npm/eslint-plugin-standard_vx.x.x.js +0 -87
- package/flow-typed/npm/eslint_vx.x.x.js +0 -2363
- package/flow-typed/npm/flow-bin_v0.x.x.js +0 -6
- package/flow-typed/npm/flow-typed_vx.x.x.js +0 -193
- package/flow-typed/npm/husky_vx.x.x.js +0 -88
- package/flow-typed/npm/jsdoc-to-markdown_vx.x.x.js +0 -95
- package/flow-typed/npm/lint-staged_vx.x.x.js +0 -143
- package/flow-typed/npm/lodash_v4.x.x.js +0 -530
- package/flow-typed/npm/mathjs_vx.x.x.js +0 -5561
- package/flow-typed/npm/nyc_vx.x.x.js +0 -108
- package/flow-typed/npm/prettier-eslint-cli_vx.x.x.js +0 -74
- package/jsdoc.json +0 -6
- package/jsdoc2md/README.hbs +0 -41
- package/lib/matrix.js +0 -4
- package/lib/matrix.mjs +0 -312
- package/test/matrix.mjs +0 -243
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
// flow-typed signature: f6e603c55941b36c9d8577c99c4196b5
|
|
2
|
-
// flow-typed version: <<STUB>>/nyc_v^11.0.2/flow_v0.53.1
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* This is an autogenerated libdef stub for:
|
|
6
|
-
*
|
|
7
|
-
* 'nyc'
|
|
8
|
-
*
|
|
9
|
-
* Fill this stub out by replacing all the `any` types.
|
|
10
|
-
*
|
|
11
|
-
* Once filled out, we encourage you to share your work with the
|
|
12
|
-
* community by sending a pull request to:
|
|
13
|
-
* https://github.com/flowtype/flow-typed
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
declare module 'nyc' {
|
|
17
|
-
declare module.exports: any;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* We include stubs for each file inside this npm package in case you need to
|
|
22
|
-
* require those files directly. Feel free to delete any files that aren't
|
|
23
|
-
* needed.
|
|
24
|
-
*/
|
|
25
|
-
declare module 'nyc/bin/nyc' {
|
|
26
|
-
declare module.exports: any;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
declare module 'nyc/bin/wrap' {
|
|
30
|
-
declare module.exports: any;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
declare module 'nyc/lib/commands/instrument' {
|
|
34
|
-
declare module.exports: any;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
declare module 'nyc/lib/config-util' {
|
|
38
|
-
declare module.exports: any;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
declare module 'nyc/lib/hash' {
|
|
42
|
-
declare module.exports: any;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
declare module 'nyc/lib/instrumenters/istanbul' {
|
|
46
|
-
declare module.exports: any;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
declare module 'nyc/lib/instrumenters/noop' {
|
|
50
|
-
declare module.exports: any;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
declare module 'nyc/lib/process-args' {
|
|
54
|
-
declare module.exports: any;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
declare module 'nyc/lib/process' {
|
|
58
|
-
declare module.exports: any;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
declare module 'nyc/lib/self-coverage-helper' {
|
|
62
|
-
declare module.exports: any;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
declare module 'nyc/lib/source-maps' {
|
|
66
|
-
declare module.exports: any;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Filename aliases
|
|
70
|
-
declare module 'nyc/bin/nyc.js' {
|
|
71
|
-
declare module.exports: $Exports<'nyc/bin/nyc'>;
|
|
72
|
-
}
|
|
73
|
-
declare module 'nyc/bin/wrap.js' {
|
|
74
|
-
declare module.exports: $Exports<'nyc/bin/wrap'>;
|
|
75
|
-
}
|
|
76
|
-
declare module 'nyc/index' {
|
|
77
|
-
declare module.exports: $Exports<'nyc'>;
|
|
78
|
-
}
|
|
79
|
-
declare module 'nyc/index.js' {
|
|
80
|
-
declare module.exports: $Exports<'nyc'>;
|
|
81
|
-
}
|
|
82
|
-
declare module 'nyc/lib/commands/instrument.js' {
|
|
83
|
-
declare module.exports: $Exports<'nyc/lib/commands/instrument'>;
|
|
84
|
-
}
|
|
85
|
-
declare module 'nyc/lib/config-util.js' {
|
|
86
|
-
declare module.exports: $Exports<'nyc/lib/config-util'>;
|
|
87
|
-
}
|
|
88
|
-
declare module 'nyc/lib/hash.js' {
|
|
89
|
-
declare module.exports: $Exports<'nyc/lib/hash'>;
|
|
90
|
-
}
|
|
91
|
-
declare module 'nyc/lib/instrumenters/istanbul.js' {
|
|
92
|
-
declare module.exports: $Exports<'nyc/lib/instrumenters/istanbul'>;
|
|
93
|
-
}
|
|
94
|
-
declare module 'nyc/lib/instrumenters/noop.js' {
|
|
95
|
-
declare module.exports: $Exports<'nyc/lib/instrumenters/noop'>;
|
|
96
|
-
}
|
|
97
|
-
declare module 'nyc/lib/process-args.js' {
|
|
98
|
-
declare module.exports: $Exports<'nyc/lib/process-args'>;
|
|
99
|
-
}
|
|
100
|
-
declare module 'nyc/lib/process.js' {
|
|
101
|
-
declare module.exports: $Exports<'nyc/lib/process'>;
|
|
102
|
-
}
|
|
103
|
-
declare module 'nyc/lib/self-coverage-helper.js' {
|
|
104
|
-
declare module.exports: $Exports<'nyc/lib/self-coverage-helper'>;
|
|
105
|
-
}
|
|
106
|
-
declare module 'nyc/lib/source-maps.js' {
|
|
107
|
-
declare module.exports: $Exports<'nyc/lib/source-maps'>;
|
|
108
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
// flow-typed signature: 56ebe15a26b55410ff353983dbf8c878
|
|
2
|
-
// flow-typed version: <<STUB>>/prettier-eslint-cli_v^4.0.0/flow_v0.53.1
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* This is an autogenerated libdef stub for:
|
|
6
|
-
*
|
|
7
|
-
* 'prettier-eslint-cli'
|
|
8
|
-
*
|
|
9
|
-
* Fill this stub out by replacing all the `any` types.
|
|
10
|
-
*
|
|
11
|
-
* Once filled out, we encourage you to share your work with the
|
|
12
|
-
* community by sending a pull request to:
|
|
13
|
-
* https://github.com/flowtype/flow-typed
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
declare module 'prettier-eslint-cli' {
|
|
17
|
-
declare module.exports: any;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* We include stubs for each file inside this npm package in case you need to
|
|
22
|
-
* require those files directly. Feel free to delete any files that aren't
|
|
23
|
-
* needed.
|
|
24
|
-
*/
|
|
25
|
-
declare module 'prettier-eslint-cli/dist/add-exception-handler' {
|
|
26
|
-
declare module.exports: any;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
declare module 'prettier-eslint-cli/dist/format-files' {
|
|
30
|
-
declare module.exports: any;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
declare module 'prettier-eslint-cli/dist/index' {
|
|
34
|
-
declare module.exports: any;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
declare module 'prettier-eslint-cli/dist/messages' {
|
|
38
|
-
declare module.exports: any;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
declare module 'prettier-eslint-cli/dist/no-main' {
|
|
42
|
-
declare module.exports: any;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
declare module 'prettier-eslint-cli/dist/parser' {
|
|
46
|
-
declare module.exports: any;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
declare module 'prettier-eslint-cli/dist/uncaught-exception-handler' {
|
|
50
|
-
declare module.exports: any;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Filename aliases
|
|
54
|
-
declare module 'prettier-eslint-cli/dist/add-exception-handler.js' {
|
|
55
|
-
declare module.exports: $Exports<'prettier-eslint-cli/dist/add-exception-handler'>;
|
|
56
|
-
}
|
|
57
|
-
declare module 'prettier-eslint-cli/dist/format-files.js' {
|
|
58
|
-
declare module.exports: $Exports<'prettier-eslint-cli/dist/format-files'>;
|
|
59
|
-
}
|
|
60
|
-
declare module 'prettier-eslint-cli/dist/index.js' {
|
|
61
|
-
declare module.exports: $Exports<'prettier-eslint-cli/dist/index'>;
|
|
62
|
-
}
|
|
63
|
-
declare module 'prettier-eslint-cli/dist/messages.js' {
|
|
64
|
-
declare module.exports: $Exports<'prettier-eslint-cli/dist/messages'>;
|
|
65
|
-
}
|
|
66
|
-
declare module 'prettier-eslint-cli/dist/no-main.js' {
|
|
67
|
-
declare module.exports: $Exports<'prettier-eslint-cli/dist/no-main'>;
|
|
68
|
-
}
|
|
69
|
-
declare module 'prettier-eslint-cli/dist/parser.js' {
|
|
70
|
-
declare module.exports: $Exports<'prettier-eslint-cli/dist/parser'>;
|
|
71
|
-
}
|
|
72
|
-
declare module 'prettier-eslint-cli/dist/uncaught-exception-handler.js' {
|
|
73
|
-
declare module.exports: $Exports<'prettier-eslint-cli/dist/uncaught-exception-handler'>;
|
|
74
|
-
}
|
package/jsdoc.json
DELETED
package/jsdoc2md/README.hbs
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
[](https://www.npmjs.com/package/@smockle/matrix)
|
|
2
|
-
[](https://travis-ci.org/smockle/matrix)
|
|
3
|
-
[](https://ci.appveyor.com/project/smockle/matrix)
|
|
4
|
-
[](https://codecov.io/gh/smockle/matrix)
|
|
5
|
-
[](https://snyk.io/test/github/smockle/matrix)
|
|
6
|
-
[](https://greenkeeper.io/)
|
|
7
|
-
|
|
8
|
-
# matrix
|
|
9
|
-
|
|
10
|
-
Single or multi dimensional matrices and matrix functions.
|
|
11
|
-
|
|
12
|
-
## Installation
|
|
13
|
-
|
|
14
|
-
Run `npm install --save @smockle/matrix` to add `matrix` to your project.
|
|
15
|
-
|
|
16
|
-
## Usage
|
|
17
|
-
|
|
18
|
-
```JavaScript
|
|
19
|
-
const Matrix = require('@smockle/matrix')
|
|
20
|
-
|
|
21
|
-
// 1x1 Matrix
|
|
22
|
-
const m11 = Matrix(3)
|
|
23
|
-
|
|
24
|
-
// 1x3 Matrix
|
|
25
|
-
const m13 = Matrix([1, 2, 3])
|
|
26
|
-
|
|
27
|
-
// 3x1 Matrix
|
|
28
|
-
const m31 = Matrix([[1], [2], [3]])
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## API Reference
|
|
32
|
-
{{#module name="matrix"}}
|
|
33
|
-
{{>body~}}
|
|
34
|
-
{{>member-index~}}
|
|
35
|
-
{{>separator~}}
|
|
36
|
-
{{>members~}}
|
|
37
|
-
{{/module}}
|
|
38
|
-
|
|
39
|
-
## Testing
|
|
40
|
-
|
|
41
|
-
`matrix` includes several unit tests. After cloning the `matrix` repo locally, run `npm install` in the project folder to install dependencies. Run `npm test` to execute the tests.
|
package/lib/matrix.js
DELETED
package/lib/matrix.mjs
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
/** @module matrix */
|
|
4
|
-
|
|
5
|
-
import fill from 'lodash/fill'
|
|
6
|
-
import flattenDeep from 'lodash/flattenDeep'
|
|
7
|
-
import padStart from 'lodash/padStart'
|
|
8
|
-
import unzip from 'lodash/unzip'
|
|
9
|
-
import mathjs from 'mathjs'
|
|
10
|
-
const { inv } = mathjs
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Creates a Matrix
|
|
14
|
-
* @constructor
|
|
15
|
-
* @alias module:matrix
|
|
16
|
-
* @param {number|Array<number>} x - Values to store in matrix
|
|
17
|
-
* @throws {TypeError} Argument x must be a number or number array
|
|
18
|
-
* @return {Matrix} Single or multi dimensional matrix
|
|
19
|
-
*/
|
|
20
|
-
function Matrix (
|
|
21
|
-
x /* : number | Array<number | Array<number>> */
|
|
22
|
-
) /* : Matrix */ {
|
|
23
|
-
if (
|
|
24
|
-
(typeof x !== 'number' && !(x instanceof Array)) ||
|
|
25
|
-
// maltyped array contents
|
|
26
|
-
flattenDeep([].concat(x)).some(
|
|
27
|
-
(x /* : number */) => typeof x !== 'number'
|
|
28
|
-
) ||
|
|
29
|
-
/* @flow disable */
|
|
30
|
-
(x[0] instanceof Array &&
|
|
31
|
-
// extra nesting
|
|
32
|
-
(x.length === 1 ||
|
|
33
|
-
// uneven rows
|
|
34
|
-
/* @flow disable */
|
|
35
|
-
x.some(row => row.length !== x[0].length)))
|
|
36
|
-
) {
|
|
37
|
-
throw new TypeError('Matrix must be a number or array of numbers')
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Single or multi dimensional matrix
|
|
42
|
-
* @constant {Matrix}
|
|
43
|
-
*/
|
|
44
|
-
const matrix = Object.create(Matrix.prototype)
|
|
45
|
-
matrix.__value = x
|
|
46
|
-
return matrix
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Determines whether two matrices can be summed
|
|
51
|
-
* @alias module:matrix.addable
|
|
52
|
-
* @param {Matrix} x - Matrix to check
|
|
53
|
-
* @param {Matrix} y - Matrix to check
|
|
54
|
-
* @return {boolean} Whether two matrices can be summed (using matrix addition)
|
|
55
|
-
*/
|
|
56
|
-
Matrix.addable = function (x /* : Matrix */, y /* : Matrix */) /* : boolean */ {
|
|
57
|
-
return (
|
|
58
|
-
x instanceof Matrix &&
|
|
59
|
-
y instanceof Matrix &&
|
|
60
|
-
x.countRows() === y.countRows() &&
|
|
61
|
-
x.countColumns() === y.countColumns()
|
|
62
|
-
)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Adds two matrices using matrix addition
|
|
67
|
-
* @alias module:matrix.add
|
|
68
|
-
* @param {Matrix} x - Matrix to add
|
|
69
|
-
* @param {Matrix} y - Matrix to add
|
|
70
|
-
* @throws {TypeError} Matrices are not addable
|
|
71
|
-
* @return {Matrix} New matrix with the summation
|
|
72
|
-
*/
|
|
73
|
-
Matrix.add = function (x /* : Matrix */, y /* : Matrix */) /* : Matrix */ {
|
|
74
|
-
if (!Matrix.addable(x, y)) throw new TypeError('Matrices are not addable')
|
|
75
|
-
return x.map(
|
|
76
|
-
// prettier-ignore
|
|
77
|
-
(row /* : Array<number> */, i /* : number */)/* : Array<number> */ =>
|
|
78
|
-
row.map(
|
|
79
|
-
// prettier-ignore
|
|
80
|
-
(column /* : number */, j /* : number */)/* : number */ =>
|
|
81
|
-
column + y.__value[i][j]
|
|
82
|
-
)
|
|
83
|
-
)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Determines whether two matrices can be multiplied
|
|
88
|
-
* @alias module:matrix.multipliable
|
|
89
|
-
* @param {Matrix} x - Matrix to check
|
|
90
|
-
* @param {Matrix} y - Matrix to check
|
|
91
|
-
* @return {boolean} Whether two matrices can be summed (using matrix multiplication)
|
|
92
|
-
*/
|
|
93
|
-
Matrix.multipliable = function (
|
|
94
|
-
x /* : Matrix */,
|
|
95
|
-
y /* : Matrix */
|
|
96
|
-
) /* : boolean */ {
|
|
97
|
-
return (
|
|
98
|
-
x instanceof Matrix &&
|
|
99
|
-
y instanceof Matrix &&
|
|
100
|
-
x.countColumns() === y.countRows()
|
|
101
|
-
)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Calculates the inner product of two matrices
|
|
106
|
-
* @param {Matrix} x - Matrix to multiply
|
|
107
|
-
* @param {Matrix} y - Matrix to multiply
|
|
108
|
-
* @param {number} i - Column in matrix y to multiply
|
|
109
|
-
* @return {number} Inner product of matrices
|
|
110
|
-
*/
|
|
111
|
-
function innerproduct (
|
|
112
|
-
x /* : Matrix */,
|
|
113
|
-
y /* : Matrix */,
|
|
114
|
-
i /* : number */
|
|
115
|
-
) /* : number */ {
|
|
116
|
-
const _x /* : Array<number> */ = x.__value
|
|
117
|
-
const _y /* : Array<number> */ = unzip(y.__value) === []
|
|
118
|
-
? unzip([y.__value])[i]
|
|
119
|
-
: unzip(y.__value)[i]
|
|
120
|
-
return [].concat(_x).reduce(
|
|
121
|
-
// prettier-ignore
|
|
122
|
-
(z /* : number */, _z /* : number */, j /* : number */)/* : number */ =>
|
|
123
|
-
z + _z * _y[j],
|
|
124
|
-
0
|
|
125
|
-
)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Calculates the dot product of two matrices
|
|
130
|
-
* @alias module:matrix.multiply
|
|
131
|
-
* @param {Matrix} x - Matrix to multiply
|
|
132
|
-
* @param {Matrix} y - Matrix to multiply
|
|
133
|
-
* @return {Matrix} New matrix with the dot product
|
|
134
|
-
*/
|
|
135
|
-
Matrix.multiply = function (x /* : Matrix */, y /* : Matrix */) /* : Matrix */ {
|
|
136
|
-
if (!Matrix.multipliable(x, y)) {
|
|
137
|
-
throw new TypeError('Matrices are not multipliable')
|
|
138
|
-
}
|
|
139
|
-
/* @flow disable */
|
|
140
|
-
if (x.countColumns() === 0 && y.countRows() === 0) return Matrix(x * y)
|
|
141
|
-
/**
|
|
142
|
-
* New matrix with the dot product
|
|
143
|
-
* @const {Matrix}
|
|
144
|
-
*/
|
|
145
|
-
const z /* : Matrix */ = Matrix(
|
|
146
|
-
fill(
|
|
147
|
-
Array(x.countRows()),
|
|
148
|
-
x.countRows() !== 1 ? fill(Array(y.countColumns()), 0) : 0
|
|
149
|
-
)
|
|
150
|
-
)
|
|
151
|
-
// prettier-ignore
|
|
152
|
-
return z.map(
|
|
153
|
-
(
|
|
154
|
-
_z /* : number | Array<number> */,
|
|
155
|
-
i /* : number */
|
|
156
|
-
)/* : number | Array<number> */ => {
|
|
157
|
-
if (typeof _z === 'number') return innerproduct(x, y, i)
|
|
158
|
-
return _z.map((_, j) => innerproduct({ __value: x.__value[i] }, y, j))
|
|
159
|
-
}
|
|
160
|
-
)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Inverts a matrix
|
|
165
|
-
* @alias module:matrix.invert
|
|
166
|
-
* @param {x} Matrix to invert
|
|
167
|
-
* @return {Matrix} Matrix inverse
|
|
168
|
-
*/
|
|
169
|
-
Matrix.invert = function (x /* : Matrix */) /* : Matrix */ {
|
|
170
|
-
return Matrix(inv(x instanceof Matrix ? x.__value : x))
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Counts rows in this matrix
|
|
175
|
-
* @alias module:matrix#countRows
|
|
176
|
-
* @return {number} Number of rows
|
|
177
|
-
*/
|
|
178
|
-
Matrix.prototype.countRows = function () /* : number */ {
|
|
179
|
-
if (typeof this.__value === 'number') return 0
|
|
180
|
-
if (typeof this.__value[0] === 'number') return 1
|
|
181
|
-
return this.__value.length
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Counts columns in this matrix
|
|
186
|
-
* @alias module:matrix#countColumns
|
|
187
|
-
* @return {number} Number of columns
|
|
188
|
-
*/
|
|
189
|
-
Matrix.prototype.countColumns = function () /* : number */ {
|
|
190
|
-
if (typeof this.__value === 'number') return 0
|
|
191
|
-
if (typeof this.__value[0] === 'number') return this.__value.length
|
|
192
|
-
return this.__value[0].length
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Determines whether this matrix can be summed
|
|
197
|
-
* @alias module:matrix#addable
|
|
198
|
-
* @param {Matrix} y - Matrix to check
|
|
199
|
-
* @return {boolean} Whether this matrix can be summed (using matrix addition)
|
|
200
|
-
*/
|
|
201
|
-
Matrix.prototype.addable = function (y /* : Matrix */) /* : boolean */ {
|
|
202
|
-
return Matrix.addable(this, y)
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Adds this matrix using matrix addition
|
|
207
|
-
* @alias module:matrix#add
|
|
208
|
-
* @param {Matrix} y - Matrix to add
|
|
209
|
-
* @return {Matrix} New matrix with the summation
|
|
210
|
-
*/
|
|
211
|
-
Matrix.prototype.add = function (y /* : Matrix */) /* : Matrix */ {
|
|
212
|
-
return Matrix.add(this, y)
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Determines whether this matrix can be multiplied
|
|
217
|
-
* @alias module:matrix#multipliable
|
|
218
|
-
* @param {Matrix} y - Matrix to check
|
|
219
|
-
* @return {boolean} Whether two matrices can be summed (using matrix multiplication)
|
|
220
|
-
*/
|
|
221
|
-
Matrix.prototype.multipliable = function (y /* : Matrix */) /* : boolean */ {
|
|
222
|
-
return Matrix.multipliable(this, y)
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Calculates the dot product of this matrix
|
|
227
|
-
* @alias module:matrix#multiply
|
|
228
|
-
* @param {Matrix} y - Matrix to multiply
|
|
229
|
-
* @return {Matrix} New matrix with the dot product
|
|
230
|
-
*/
|
|
231
|
-
Matrix.prototype.multiply = function (y /* : Matrix */) /* : Matrix */ {
|
|
232
|
-
return Matrix.multiply(this, y)
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Calculates the transpose of this matrix
|
|
237
|
-
* @alias module:matrix#transpose
|
|
238
|
-
* @return {Matrix} New matrix with the transpose
|
|
239
|
-
*/
|
|
240
|
-
Matrix.prototype.transpose = function () /* : Matrix */ {
|
|
241
|
-
switch (this.countRows()) {
|
|
242
|
-
case 0:
|
|
243
|
-
return Matrix(this.__value)
|
|
244
|
-
case 1:
|
|
245
|
-
return Matrix(unzip([this.__value]))
|
|
246
|
-
default:
|
|
247
|
-
return Matrix(unzip(this.__value))
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Inverts this matrix
|
|
253
|
-
* @alias module:matrix#invert
|
|
254
|
-
* @return {Matrix} Matrix inverse
|
|
255
|
-
*/
|
|
256
|
-
Matrix.prototype.invert = function () /* : Matrix */ {
|
|
257
|
-
return Matrix.invert(this)
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Maps over this matrix
|
|
262
|
-
* @alias module:matrix#map
|
|
263
|
-
* @return {Matrix} Matrix inverse
|
|
264
|
-
*/
|
|
265
|
-
Matrix.prototype.map = function (x /* : Function */) /* : Matrix */ {
|
|
266
|
-
return Matrix(this.__value.map(x))
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Returns the number or number array value
|
|
271
|
-
* @alias module:matrix#valueOf
|
|
272
|
-
* @return {number|Array<number>} Number of number array value
|
|
273
|
-
*/
|
|
274
|
-
/* @flow disable */
|
|
275
|
-
Matrix.prototype.valueOf = function () /* : number | Array<number> */ {
|
|
276
|
-
return this.__value
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Formats and prints the matrix value
|
|
281
|
-
* @alias module:matrix#inspect
|
|
282
|
-
* @return {string} Formatted matrix value
|
|
283
|
-
*/
|
|
284
|
-
Matrix.prototype.inspect = function () /* : string */ {
|
|
285
|
-
switch (this.countRows()) {
|
|
286
|
-
case 0:
|
|
287
|
-
return `${this.__value}`
|
|
288
|
-
case 1:
|
|
289
|
-
return `[ ${this.__value.join(' ')} ]`
|
|
290
|
-
default:
|
|
291
|
-
/**
|
|
292
|
-
* Output array filled with zeroes
|
|
293
|
-
* @constant {string}
|
|
294
|
-
*/
|
|
295
|
-
const padding /* : Array<number> */ = unzip(
|
|
296
|
-
this.__value
|
|
297
|
-
).map((column /* : Array<number> */) =>
|
|
298
|
-
column.reduce((length, x) => Math.max(`${x}`.length, length), 0)
|
|
299
|
-
)
|
|
300
|
-
return this.__value
|
|
301
|
-
.reduce(
|
|
302
|
-
(output, row) =>
|
|
303
|
-
`${output}[ ${row
|
|
304
|
-
.map((x, i) => padStart(x, padding[i]))
|
|
305
|
-
.join(' ')} ]`,
|
|
306
|
-
''
|
|
307
|
-
)
|
|
308
|
-
.replace(/]\[/g, ']\n[')
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
export default Matrix
|