UNPKG

3.79 kBJavaScriptView Raw
1'use strict'
2
3const DimensionError = require('../../error/DimensionError')
4const ArgumentsError = require('../../error/ArgumentsError')
5
6const isInteger = require('../../utils/number').isInteger
7const format = require('../../utils/string').format
8const clone = require('../../utils/object').clone
9const array = require('../../utils/array')
10
11function factory (type, config, load, typed) {
12 const matrix = load(require('../../type/matrix/function/matrix'))
13
14 /**
15 * Resize a matrix
16 *
17 * Syntax:
18 *
19 * math.resize(x, size)
20 * math.resize(x, size, defaultValue)
21 *
22 * Examples:
23 *
24 * math.resize([1, 2, 3, 4, 5], [3]) // returns Array [1, 2, 3]
25 * math.resize([1, 2, 3], [5], 0) // returns Array [1, 2, 3, 0, 0]
26 * math.resize(2, [2, 3], 0) // returns Matrix [[2, 0, 0], [0, 0, 0]]
27 * math.resize("hello", [8], "!") // returns string 'hello!!!'
28 *
29 * See also:
30 *
31 * size, squeeze, subset, reshape
32 *
33 * @param {Array | Matrix | *} x Matrix to be resized
34 * @param {Array | Matrix} size One dimensional array with numbers
35 * @param {number | string} [defaultValue=0] Zero by default, except in
36 * case of a string, in that case
37 * defaultValue = ' '
38 * @return {* | Array | Matrix} A resized clone of matrix `x`
39 */
40 // TODO: rework resize to a typed-function
41 const resize = function resize (x, size, defaultValue) {
42 if (arguments.length !== 2 && arguments.length !== 3) {
43 throw new ArgumentsError('resize', arguments.length, 2, 3)
44 }
45
46 if (type.isMatrix(size)) {
47 size = size.valueOf() // get Array
48 }
49
50 if (type.isBigNumber(size[0])) {
51 // convert bignumbers to numbers
52 size = size.map(function (value) {
53 return type.isBigNumber(value) ? value.toNumber() : value
54 })
55 }
56
57 // check x is a Matrix
58 if (type.isMatrix(x)) {
59 // use optimized matrix implementation, return copy
60 return x.resize(size, defaultValue, true)
61 }
62
63 if (typeof x === 'string') {
64 // resize string
65 return _resizeString(x, size, defaultValue)
66 }
67
68 // check result should be a matrix
69 const asMatrix = Array.isArray(x) ? false : (config.matrix !== 'Array')
70
71 if (size.length === 0) {
72 // output a scalar
73 while (Array.isArray(x)) {
74 x = x[0]
75 }
76
77 return clone(x)
78 } else {
79 // output an array/matrix
80 if (!Array.isArray(x)) {
81 x = [x]
82 }
83 x = clone(x)
84
85 const res = array.resize(x, size, defaultValue)
86 return asMatrix ? matrix(res) : res
87 }
88 }
89
90 resize.toTex = undefined // use default template
91
92 return resize
93
94 /**
95 * Resize a string
96 * @param {string} str
97 * @param {number[]} size
98 * @param {string} [defaultChar=' ']
99 * @private
100 */
101 function _resizeString (str, size, defaultChar) {
102 if (defaultChar !== undefined) {
103 if (typeof defaultChar !== 'string' || defaultChar.length !== 1) {
104 throw new TypeError('Single character expected as defaultValue')
105 }
106 } else {
107 defaultChar = ' '
108 }
109
110 if (size.length !== 1) {
111 throw new DimensionError(size.length, 1)
112 }
113 const len = size[0]
114 if (typeof len !== 'number' || !isInteger(len)) {
115 throw new TypeError('Invalid size, must contain positive integers ' +
116 '(size: ' + format(size) + ')')
117 }
118
119 if (str.length > len) {
120 return str.substring(0, len)
121 } else if (str.length < len) {
122 let res = str
123 for (let i = 0, ii = len - str.length; i < ii; i++) {
124 res += defaultChar
125 }
126 return res
127 } else {
128 return str
129 }
130 }
131}
132
133exports.name = 'resize'
134exports.factory = factory