UNPKG

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.

47 lines (42 loc) 1.23 kB
module.exports = function (math) { var util = require('../../util/index'), BigNumber = math.type.BigNumber, Index = require('../../type/Index'); /** * Create an index. An Index can store ranges having start, step, and end * for multiple dimensions. * Matrix.get, Matrix.set, and math.subset accept an Index as input. * * Usage: * var index = math.index(range1, range2, ...); * * Where each range can be any of: * An array [start, end] * An array [start, end, step] * A number * null, this will create select the whole dimension * * The parameters start, end, and step must be integer numbers. * * @param {...*} ranges */ math.index = function matrix(ranges) { var i = new Index(); // downgrade BigNumber to Number var args = Array.prototype.slice.apply(arguments).map(function (arg) { if (arg instanceof BigNumber) { return arg.toNumber(); } else if (Array.isArray(arg)) { return arg.map(function (elem) { return (elem instanceof BigNumber) ? elem.toNumber() : elem; }); } else { return arg; } }); Index.apply(i, args); return i; }; };