mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.
51 lines (41 loc) • 1.34 kB
JavaScript
module.exports = function (math, config) {
var util = require('../../util/index'),
BigNumber = math.type.BigNumber,
Complex = require('../../type/Complex'),
Unit = require('../../type/Unit'),
Matrix = require('../../type/Matrix'),
array = util.array,
isNumber = util.number.isNumber,
isBoolean = util['boolean'].isBoolean,
isString = util.string.isString,
isComplex = Complex.isComplex,
isUnit = Unit.isUnit;
/**
* Calculate the size of a matrix or scalar
*
* size(x)
*
* @param {Boolean | Number | Complex | Unit | String | Array | Matrix} x
* @return {Array | Matrix} res
*/
math.size = function size (x) {
if (arguments.length != 1) {
throw new math.error.ArgumentsError('size', arguments.length, 1);
}
var asArray = (config.matrix === 'array');
if (isNumber(x) || isComplex(x) || isUnit(x) || isBoolean(x) ||
x == null || x instanceof BigNumber) {
return asArray ? [] : new Matrix([]);
}
if (isString(x)) {
return asArray ? [x.length] : new Matrix([x.length]);
}
if (Array.isArray(x)) {
return array.size(x);
}
if (x instanceof Matrix) {
return new Matrix(x.size());
}
throw new math.error.UnsupportedTypeError('size', math['typeof'](x));
};
};