1 | 'use strict'
|
2 |
|
3 | const array = require('../../utils/array')
|
4 | const isInteger = require('../../utils/number').isInteger
|
5 |
|
6 | function factory (type, config, load, typed) {
|
7 | const matrix = load(require('../../type/matrix/function/matrix'))
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const identity = typed('identity', {
|
40 | '': function () {
|
41 | return (config.matrix === 'Matrix') ? matrix([]) : []
|
42 | },
|
43 |
|
44 | 'string': function (format) {
|
45 | return matrix(format)
|
46 | },
|
47 |
|
48 | 'number | BigNumber': function (rows) {
|
49 | return _identity(rows, rows, config.matrix === 'Matrix' ? 'default' : undefined)
|
50 | },
|
51 |
|
52 | 'number | BigNumber, string': function (rows, format) {
|
53 | return _identity(rows, rows, format)
|
54 | },
|
55 |
|
56 | 'number | BigNumber, number | BigNumber': function (rows, cols) {
|
57 | return _identity(rows, cols, config.matrix === 'Matrix' ? 'default' : undefined)
|
58 | },
|
59 |
|
60 | 'number | BigNumber, number | BigNumber, string': function (rows, cols, format) {
|
61 | return _identity(rows, cols, format)
|
62 | },
|
63 |
|
64 | 'Array': function (size) {
|
65 | return _identityVector(size)
|
66 | },
|
67 |
|
68 | 'Array, string': function (size, format) {
|
69 | return _identityVector(size, format)
|
70 | },
|
71 |
|
72 | 'Matrix': function (size) {
|
73 | return _identityVector(size.valueOf(), size.storage())
|
74 | },
|
75 |
|
76 | 'Matrix, string': function (size, format) {
|
77 | return _identityVector(size.valueOf(), format)
|
78 | }
|
79 | })
|
80 |
|
81 | identity.toTex = undefined
|
82 |
|
83 | return identity
|
84 |
|
85 | function _identityVector (size, format) {
|
86 | switch (size.length) {
|
87 | case 0: return format ? matrix(format) : []
|
88 | case 1: return _identity(size[0], size[0], format)
|
89 | case 2: return _identity(size[0], size[1], format)
|
90 | default: throw new Error('Vector containing two values expected')
|
91 | }
|
92 | }
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | function _identity (rows, cols, format) {
|
103 |
|
104 | const Big = (type.isBigNumber(rows) || type.isBigNumber(cols))
|
105 | ? type.BigNumber
|
106 | : null
|
107 |
|
108 | if (type.isBigNumber(rows)) rows = rows.toNumber()
|
109 | if (type.isBigNumber(cols)) cols = cols.toNumber()
|
110 |
|
111 | if (!isInteger(rows) || rows < 1) {
|
112 | throw new Error('Parameters in function identity must be positive integers')
|
113 | }
|
114 | if (!isInteger(cols) || cols < 1) {
|
115 | throw new Error('Parameters in function identity must be positive integers')
|
116 | }
|
117 |
|
118 | const one = Big ? new type.BigNumber(1) : 1
|
119 | const defaultValue = Big ? new Big(0) : 0
|
120 | const size = [rows, cols]
|
121 |
|
122 |
|
123 | if (format) {
|
124 |
|
125 | const F = type.Matrix.storage(format)
|
126 |
|
127 | return F.diagonal(size, one, 0, defaultValue)
|
128 | }
|
129 |
|
130 |
|
131 | const res = array.resize([], size, defaultValue)
|
132 |
|
133 | const minimum = rows < cols ? rows : cols
|
134 |
|
135 | for (let d = 0; d < minimum; d++) {
|
136 | res[d][d] = one
|
137 | }
|
138 | return res
|
139 | }
|
140 | }
|
141 |
|
142 | exports.name = 'identity'
|
143 | exports.factory = factory
|