1 | 'use strict'
|
2 |
|
3 | const isInteger = require('../../utils/number').isInteger
|
4 | const resize = require('../../utils/array').resize
|
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 |
|
40 | const zeros = typed('zeros', {
|
41 | '': function () {
|
42 | return (config.matrix === 'Array')
|
43 | ? _zeros([])
|
44 | : _zeros([], 'default')
|
45 | },
|
46 |
|
47 |
|
48 |
|
49 | '...number | BigNumber | string': function (size) {
|
50 | const last = size[size.length - 1]
|
51 | if (typeof last === 'string') {
|
52 | const format = size.pop()
|
53 | return _zeros(size, format)
|
54 | } else if (config.matrix === 'Array') {
|
55 | return _zeros(size)
|
56 | } else {
|
57 | return _zeros(size, 'default')
|
58 | }
|
59 | },
|
60 |
|
61 | 'Array': _zeros,
|
62 |
|
63 | 'Matrix': function (size) {
|
64 | const format = size.storage()
|
65 | return _zeros(size.valueOf(), format)
|
66 | },
|
67 |
|
68 | 'Array | Matrix, string': function (size, format) {
|
69 | return _zeros(size.valueOf(), format)
|
70 | }
|
71 | })
|
72 |
|
73 | zeros.toTex = undefined
|
74 |
|
75 | return zeros
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function _zeros (size, format) {
|
85 | const hasBigNumbers = _normalize(size)
|
86 | const defaultValue = hasBigNumbers ? new type.BigNumber(0) : 0
|
87 | _validate(size)
|
88 |
|
89 | if (format) {
|
90 |
|
91 | const m = matrix(format)
|
92 | if (size.length > 0) {
|
93 | return m.resize(size, defaultValue)
|
94 | }
|
95 | return m
|
96 | } else {
|
97 |
|
98 | const arr = []
|
99 | if (size.length > 0) {
|
100 | return resize(arr, size, defaultValue)
|
101 | }
|
102 | return arr
|
103 | }
|
104 | }
|
105 |
|
106 |
|
107 | function _normalize (size) {
|
108 | let hasBigNumbers = false
|
109 | size.forEach(function (value, index, arr) {
|
110 | if (type.isBigNumber(value)) {
|
111 | hasBigNumbers = true
|
112 | arr[index] = value.toNumber()
|
113 | }
|
114 | })
|
115 | return hasBigNumbers
|
116 | }
|
117 |
|
118 |
|
119 | function _validate (size) {
|
120 | size.forEach(function (value) {
|
121 | if (typeof value !== 'number' || !isInteger(value) || value < 0) {
|
122 | throw new Error('Parameters in function zeros must be positive integers')
|
123 | }
|
124 | })
|
125 | }
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 | exports.name = 'zeros'
|
131 | exports.factory = factory
|