mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
1,544 lines (1,329 loc) • 1.87 MB
JavaScript
/**
* math.js
* https://github.com/josdejong/mathjs
*
* Math.js is an extensive math library for JavaScript and Node.js,
* It features real and complex numbers, units, matrices, a large set of
* mathematical functions, and a flexible expression parser.
*
* @version 6.2.2
* @date 2019-09-23
*
* @license
* Copyright (C) 2013-2019 Jos de Jong <wjosdejong@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["math"] = factory();
else
root["math"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 19);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return factory; });
/* unused harmony export sortFactories */
/* unused harmony export create */
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return isFactory; });
/* unused harmony export assertDependencies */
/* unused harmony export isOptionalDependency */
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return stripOptionalNotation; });
/* harmony import */ var _array__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
/* harmony import */ var _object__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3);
/**
* Create a factory function, which can be used to inject dependencies.
*
* The created functions are memoized, a consecutive call of the factory
* with the exact same inputs will return the same function instance.
* The memoized cache is exposed on `factory.cache` and can be cleared
* if needed.
*
* Example:
*
* const name = 'log'
* const dependencies = ['config', 'typed', 'divideScalar', 'Complex']
*
* export const createLog = factory(name, dependencies, ({ typed, config, divideScalar, Complex }) => {
* // ... create the function log here and return it
* }
*
* @param {string} name Name of the function to be created
* @param {string[]} dependencies The names of all required dependencies
* @param {function} create Callback function called with an object with all dependencies
* @param {Object} [meta] Optional object with meta information that will be attached
* to the created factory function as property `meta`.
* @returns {function}
*/
function factory(name, dependencies, create, meta) {
function assertAndCreate(scope) {
// we only pass the requested dependencies to the factory function
// to prevent functions to rely on dependencies that are not explicitly
// requested.
var deps = Object(_object__WEBPACK_IMPORTED_MODULE_1__[/* pickShallow */ "j"])(scope, dependencies.map(stripOptionalNotation));
assertDependencies(name, dependencies, scope);
return create(deps);
}
assertAndCreate.isFactory = true;
assertAndCreate.fn = name;
assertAndCreate.dependencies = dependencies.slice().sort();
if (meta) {
assertAndCreate.meta = meta;
}
return assertAndCreate;
}
/**
* Sort all factories such that when loading in order, the dependencies are resolved.
*
* @param {Array} factories
* @returns {Array} Returns a new array with the sorted factories.
*/
function sortFactories(factories) {
var factoriesByName = {};
factories.forEach(function (factory) {
factoriesByName[factory.fn] = factory;
});
function containsDependency(factory, dependency) {
// TODO: detect circular references
if (isFactory(factory)) {
if (Object(_array__WEBPACK_IMPORTED_MODULE_0__[/* contains */ "b"])(factory.dependencies, dependency.fn || dependency.name)) {
return true;
}
if (factory.dependencies.some(function (d) {
return containsDependency(factoriesByName[d], dependency);
})) {
return true;
}
}
return false;
}
var sorted = [];
function addFactory(factory) {
var index = 0;
while (index < sorted.length && !containsDependency(sorted[index], factory)) {
index++;
}
sorted.splice(index, 0, factory);
} // sort regular factory functions
factories.filter(isFactory).forEach(addFactory); // sort legacy factory functions AFTER the regular factory functions
factories.filter(function (factory) {
return !isFactory(factory);
}).forEach(addFactory);
return sorted;
} // TODO: comment or cleanup if unused in the end
function create(factories) {
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
sortFactories(factories).forEach(function (factory) {
return factory(scope);
});
return scope;
}
/**
* Test whether an object is a factory. This is the case when it has
* properties name, dependencies, and a function create.
* @param {*} obj
* @returns {boolean}
*/
function isFactory(obj) {
return typeof obj === 'function' && typeof obj.fn === 'string' && Array.isArray(obj.dependencies);
}
/**
* Assert that all dependencies of a list with dependencies are available in the provided scope.
*
* Will throw an exception when there are dependencies missing.
*
* @param {string} name Name for the function to be created. Used to generate a useful error message
* @param {string[]} dependencies
* @param {Object} scope
*/
function assertDependencies(name, dependencies, scope) {
var allDefined = dependencies.filter(function (dependency) {
return !isOptionalDependency(dependency);
}) // filter optionals
.every(function (dependency) {
return scope[dependency] !== undefined;
});
if (!allDefined) {
var missingDependencies = dependencies.filter(function (dependency) {
return scope[dependency] === undefined;
}); // TODO: create a custom error class for this, a MathjsError or something like that
throw new Error("Cannot create function \"".concat(name, "\", ") + "some dependencies are missing: ".concat(missingDependencies.map(function (d) {
return "\"".concat(d, "\"");
}).join(', '), "."));
}
}
function isOptionalDependency(dependency) {
return dependency && dependency[0] === '?';
}
function stripOptionalNotation(dependency) {
return dependency && dependency[0] === '?' ? dependency.slice(1) : dependency;
}
/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "y", function() { return isNumber; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function() { return isBigNumber; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "j", function() { return isComplex; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "o", function() { return isFraction; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "L", function() { return isUnit; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "I", function() { return isString; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return isArray; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "v", function() { return isMatrix; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "i", function() { return isCollection; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "n", function() { return isDenseMatrix; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "H", function() { return isSparseMatrix; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "D", function() { return isRange; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "t", function() { return isIndex; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "g", function() { return isBoolean; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "G", function() { return isResultSet; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "s", function() { return isHelp; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "p", function() { return isFunction; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "m", function() { return isDate; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "F", function() { return isRegExp; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "z", function() { return isObject; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "x", function() { return isNull; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "K", function() { return isUndefined; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return isAccessorNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return isArrayNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function() { return isAssignmentNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function() { return isBlockNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "k", function() { return isConditionalNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "l", function() { return isConstantNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "q", function() { return isFunctionAssignmentNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "r", function() { return isFunctionNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "u", function() { return isIndexNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "w", function() { return isNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "A", function() { return isObjectNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "B", function() { return isOperatorNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "C", function() { return isParenthesisNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "E", function() { return isRangeNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "J", function() { return isSymbolNode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "h", function() { return isChain; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "M", function() { return typeOf; });
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
// type checks for all known types
//
// note that:
//
// - check by duck-typing on a property like `isUnit`, instead of checking instanceof.
// instanceof cannot be used because that would not allow to pass data from
// one instance of math.js to another since each has it's own instance of Unit.
// - check the `isUnit` property via the constructor, so there will be no
// matches for "fake" instances like plain objects with a property `isUnit`.
// That is important for security reasons.
// - It must not be possible to override the type checks used internally,
// for security reasons, so these functions are not exposed in the expression
// parser.
function isNumber(x) {
return typeof x === 'number';
}
function isBigNumber(x) {
return x && x.constructor.prototype.isBigNumber === true || false;
}
function isComplex(x) {
return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isComplex === true || false;
}
function isFraction(x) {
return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isFraction === true || false;
}
function isUnit(x) {
return x && x.constructor.prototype.isUnit === true || false;
}
function isString(x) {
return typeof x === 'string';
}
var isArray = Array.isArray;
function isMatrix(x) {
return x && x.constructor.prototype.isMatrix === true || false;
}
/**
* Test whether a value is a collection: an Array or Matrix
* @param {*} x
* @returns {boolean} isCollection
*/
function isCollection(x) {
return Array.isArray(x) || isMatrix(x);
}
function isDenseMatrix(x) {
return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false;
}
function isSparseMatrix(x) {
return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false;
}
function isRange(x) {
return x && x.constructor.prototype.isRange === true || false;
}
function isIndex(x) {
return x && x.constructor.prototype.isIndex === true || false;
}
function isBoolean(x) {
return typeof x === 'boolean';
}
function isResultSet(x) {
return x && x.constructor.prototype.isResultSet === true || false;
}
function isHelp(x) {
return x && x.constructor.prototype.isHelp === true || false;
}
function isFunction(x) {
return typeof x === 'function';
}
function isDate(x) {
return x instanceof Date;
}
function isRegExp(x) {
return x instanceof RegExp;
}
function isObject(x) {
return !!(x && _typeof(x) === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x));
}
function isNull(x) {
return x === null;
}
function isUndefined(x) {
return x === undefined;
}
function isAccessorNode(x) {
return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false;
}
function isArrayNode(x) {
return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false;
}
function isAssignmentNode(x) {
return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false;
}
function isBlockNode(x) {
return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false;
}
function isConditionalNode(x) {
return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false;
}
function isConstantNode(x) {
return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false;
}
function isFunctionAssignmentNode(x) {
return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false;
}
function isFunctionNode(x) {
return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false;
}
function isIndexNode(x) {
return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false;
}
function isNode(x) {
return x && x.isNode === true && x.constructor.prototype.isNode === true || false;
}
function isObjectNode(x) {
return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false;
}
function isOperatorNode(x) {
return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false;
}
function isParenthesisNode(x) {
return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false;
}
function isRangeNode(x) {
return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false;
}
function isSymbolNode(x) {
return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false;
}
function isChain(x) {
return x && x.constructor.prototype.isChain === true || false;
}
function typeOf(x) {
var t = _typeof(x);
if (t === 'object') {
// JavaScript types
if (x === null) return 'null';
if (Array.isArray(x)) return 'Array';
if (x instanceof Date) return 'Date';
if (x instanceof RegExp) return 'RegExp'; // math.js types
if (isBigNumber(x)) return 'BigNumber';
if (isComplex(x)) return 'Complex';
if (isFraction(x)) return 'Fraction';
if (isMatrix(x)) return 'Matrix';
if (isUnit(x)) return 'Unit';
if (isIndex(x)) return 'Index';
if (isRange(x)) return 'Range';
if (isResultSet(x)) return 'ResultSet';
if (isNode(x)) return x.type;
if (isChain(x)) return 'Chain';
if (isHelp(x)) return 'Help';
return 'Object';
}
if (t === 'function') return 'Function';
return t; // can be 'string', 'number', 'boolean', ...
}
/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return arraySize; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "r", function() { return validate; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "s", function() { return validateIndex; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "o", function() { return resize; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "n", function() { return reshape; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "p", function() { return squeeze; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "q", function() { return unsqueeze; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function() { return flatten; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "m", function() { return map; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function() { return forEach; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return filter; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function() { return filterRegExp; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "k", function() { return join; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "i", function() { return identify; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "g", function() { return generalize; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "h", function() { return getArrayDataType; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "l", function() { return last; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "j", function() { return initial; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return contains; });
/* harmony import */ var _number__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4);
/* harmony import */ var _is__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
/* harmony import */ var _string__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5);
/* harmony import */ var _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(6);
/* harmony import */ var _error_IndexError__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(10);
/**
* Calculate the size of a multi dimensional array.
* This function checks the size of the first entry, it does not validate
* whether all dimensions match. (use function `validate` for that)
* @param {Array} x
* @Return {Number[]} size
*/
function arraySize(x) {
var s = [];
while (Array.isArray(x)) {
s.push(x.length);
x = x[0];
}
return s;
}
/**
* Recursively validate whether each element in a multi dimensional array
* has a size corresponding to the provided size array.
* @param {Array} array Array to be validated
* @param {number[]} size Array with the size of each dimension
* @param {number} dim Current dimension
* @throws DimensionError
* @private
*/
function _validate(array, size, dim) {
var i;
var len = array.length;
if (len !== size[dim]) {
throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](len, size[dim]);
}
if (dim < size.length - 1) {
// recursively validate each child array
var dimNext = dim + 1;
for (i = 0; i < len; i++) {
var child = array[i];
if (!Array.isArray(child)) {
throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](size.length - 1, size.length, '<');
}
_validate(array[i], size, dimNext);
}
} else {
// last dimension. none of the childs may be an array
for (i = 0; i < len; i++) {
if (Array.isArray(array[i])) {
throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](size.length + 1, size.length, '>');
}
}
}
}
/**
* Validate whether each element in a multi dimensional array has
* a size corresponding to the provided size array.
* @param {Array} array Array to be validated
* @param {number[]} size Array with the size of each dimension
* @throws DimensionError
*/
function validate(array, size) {
var isScalar = size.length === 0;
if (isScalar) {
// scalar
if (Array.isArray(array)) {
throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](array.length, 0);
}
} else {
// array
_validate(array, size, 0);
}
}
/**
* Test whether index is an integer number with index >= 0 and index < length
* when length is provided
* @param {number} index Zero-based index
* @param {number} [length] Length of the array
*/
function validateIndex(index, length) {
if (!Object(_is__WEBPACK_IMPORTED_MODULE_1__[/* isNumber */ "y"])(index) || !Object(_number__WEBPACK_IMPORTED_MODULE_0__[/* isInteger */ "i"])(index)) {
throw new TypeError('Index must be an integer (value: ' + index + ')');
}
if (index < 0 || typeof length === 'number' && index >= length) {
throw new _error_IndexError__WEBPACK_IMPORTED_MODULE_4__[/* IndexError */ "a"](index, length);
}
}
/**
* Resize a multi dimensional array. The resized array is returned.
* @param {Array} array Array to be resized
* @param {Array.<number>} size Array with the size of each dimension
* @param {*} [defaultValue=0] Value to be filled in in new entries,
* zero by default. Specify for example `null`,
* to clearly see entries that are not explicitly
* set.
* @return {Array} array The resized array
*/
function resize(array, size, defaultValue) {
// TODO: add support for scalars, having size=[] ?
// check the type of the arguments
if (!Array.isArray(array) || !Array.isArray(size)) {
throw new TypeError('Array expected');
}
if (size.length === 0) {
throw new Error('Resizing to scalar is not supported');
} // check whether size contains positive integers
size.forEach(function (value) {
if (!Object(_is__WEBPACK_IMPORTED_MODULE_1__[/* isNumber */ "y"])(value) || !Object(_number__WEBPACK_IMPORTED_MODULE_0__[/* isInteger */ "i"])(value) || value < 0) {
throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + Object(_string__WEBPACK_IMPORTED_MODULE_2__[/* format */ "d"])(size) + ')');
}
}); // recursively resize the array
var _defaultValue = defaultValue !== undefined ? defaultValue : 0;
_resize(array, size, 0, _defaultValue);
return array;
}
/**
* Recursively resize a multi dimensional array
* @param {Array} array Array to be resized
* @param {number[]} size Array with the size of each dimension
* @param {number} dim Current dimension
* @param {*} [defaultValue] Value to be filled in in new entries,
* undefined by default.
* @private
*/
function _resize(array, size, dim, defaultValue) {
var i;
var elem;
var oldLen = array.length;
var newLen = size[dim];
var minLen = Math.min(oldLen, newLen); // apply new length
array.length = newLen;
if (dim < size.length - 1) {
// non-last dimension
var dimNext = dim + 1; // resize existing child arrays
for (i = 0; i < minLen; i++) {
// resize child array
elem = array[i];
if (!Array.isArray(elem)) {
elem = [elem]; // add a dimension
array[i] = elem;
}
_resize(elem, size, dimNext, defaultValue);
} // create new child arrays
for (i = minLen; i < newLen; i++) {
// get child array
elem = [];
array[i] = elem; // resize new child array
_resize(elem, size, dimNext, defaultValue);
}
} else {
// last dimension
// remove dimensions of existing values
for (i = 0; i < minLen; i++) {
while (Array.isArray(array[i])) {
array[i] = array[i][0];
}
} // fill new elements with the default value
for (i = minLen; i < newLen; i++) {
array[i] = defaultValue;
}
}
}
/**
* Re-shape a multi dimensional array to fit the specified dimensions
* @param {Array} array Array to be reshaped
* @param {Array.<number>} sizes List of sizes for each dimension
* @returns {Array} Array whose data has been formatted to fit the
* specified dimensions
*
* @throws {DimensionError} If the product of the new dimension sizes does
* not equal that of the old ones
*/
function reshape(array, sizes) {
var flatArray = flatten(array);
var newArray;
function product(arr) {
return arr.reduce(function (prev, curr) {
return prev * curr;
});
}
if (!Array.isArray(array) || !Array.isArray(sizes)) {
throw new TypeError('Array expected');
}
if (sizes.length === 0) {
throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](0, product(arraySize(array)), '!=');
}
var totalSize = 1;
for (var sizeIndex = 0; sizeIndex < sizes.length; sizeIndex++) {
totalSize *= sizes[sizeIndex];
}
if (flatArray.length !== totalSize) {
throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](product(sizes), product(arraySize(array)), '!=');
}
try {
newArray = _reshape(flatArray, sizes);
} catch (e) {
if (e instanceof _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"]) {
throw new _error_DimensionError__WEBPACK_IMPORTED_MODULE_3__[/* DimensionError */ "a"](product(sizes), product(arraySize(array)), '!=');
}
throw e;
}
return newArray;
}
/**
* Iteratively re-shape a multi dimensional array to fit the specified dimensions
* @param {Array} array Array to be reshaped
* @param {Array.<number>} sizes List of sizes for each dimension
* @returns {Array} Array whose data has been formatted to fit the
* specified dimensions
*/
function _reshape(array, sizes) {
// testing if there are enough elements for the requested shape
var tmpArray = array;
var tmpArray2; // for each dimensions starting by the last one and ignoring the first one
for (var sizeIndex = sizes.length - 1; sizeIndex > 0; sizeIndex--) {
var size = sizes[sizeIndex];
tmpArray2 = []; // aggregate the elements of the current tmpArray in elements of the requested size
var length = tmpArray.length / size;
for (var i = 0; i < length; i++) {
tmpArray2.push(tmpArray.slice(i * size, (i + 1) * size));
} // set it as the new tmpArray for the next loop turn or for return
tmpArray = tmpArray2;
}
return tmpArray;
}
/**
* Squeeze a multi dimensional array
* @param {Array} array
* @param {Array} [size]
* @returns {Array} returns the array itself
*/
function squeeze(array, size) {
var s = size || arraySize(array); // squeeze outer dimensions
while (Array.isArray(array) && array.length === 1) {
array = array[0];
s.shift();
} // find the first dimension to be squeezed
var dims = s.length;
while (s[dims - 1] === 1) {
dims--;
} // squeeze inner dimensions
if (dims < s.length) {
array = _squeeze(array, dims, 0);
s.length = dims;
}
return array;
}
/**
* Recursively squeeze a multi dimensional array
* @param {Array} array
* @param {number} dims Required number of dimensions
* @param {number} dim Current dimension
* @returns {Array | *} Returns the squeezed array
* @private
*/
function _squeeze(array, dims, dim) {
var i, ii;
if (dim < dims) {
var next = dim + 1;
for (i = 0, ii = array.length; i < ii; i++) {
array[i] = _squeeze(array[i], dims, next);
}
} else {
while (Array.isArray(array)) {
array = array[0];
}
}
return array;
}
/**
* Unsqueeze a multi dimensional array: add dimensions when missing
*
* Paramter `size` will be mutated to match the new, unqueezed matrix size.
*
* @param {Array} array
* @param {number} dims Desired number of dimensions of the array
* @param {number} [outer] Number of outer dimensions to be added
* @param {Array} [size] Current size of array.
* @returns {Array} returns the array itself
* @private
*/
function unsqueeze(array, dims, outer, size) {
var s = size || arraySize(array); // unsqueeze outer dimensions
if (outer) {
for (var i = 0; i < outer; i++) {
array = [array];
s.unshift(1);
}
} // unsqueeze inner dimensions
array = _unsqueeze(array, dims, 0);
while (s.length < dims) {
s.push(1);
}
return array;
}
/**
* Recursively unsqueeze a multi dimensional array
* @param {Array} array
* @param {number} dims Required number of dimensions
* @param {number} dim Current dimension
* @returns {Array | *} Returns the squeezed array
* @private
*/
function _unsqueeze(array, dims, dim) {
var i, ii;
if (Array.isArray(array)) {
var next = dim + 1;
for (i = 0, ii = array.length; i < ii; i++) {
array[i] = _unsqueeze(array[i], dims, next);
}
} else {
for (var d = dim; d < dims; d++) {
array = [array];
}
}
return array;
}
/**
* Flatten a multi dimensional array, put all elements in a one dimensional
* array
* @param {Array} array A multi dimensional array
* @return {Array} The flattened array (1 dimensional)
*/
function flatten(array) {
if (!Array.isArray(array)) {
// if not an array, return as is
return array;
}
var flat = [];
array.forEach(function callback(value) {
if (Array.isArray(value)) {
value.forEach(callback); // traverse through sub-arrays recursively
} else {
flat.push(value);
}
});
return flat;
}
/**
* A safe map
* @param {Array} array
* @param {function} callback
*/
function map(array, callback) {
return Array.prototype.map.call(array, callback);
}
/**
* A safe forEach
* @param {Array} array
* @param {function} callback
*/
function forEach(array, callback) {
Array.prototype.forEach.call(array, callback);
}
/**
* A safe filter
* @param {Array} array
* @param {function} callback
*/
function filter(array, callback) {
if (arraySize(array).length !== 1) {
throw new Error('Only one dimensional matrices supported');
}
return Array.prototype.filter.call(array, callback);
}
/**
* Filter values in a callback given a regular expression
* @param {Array} array
* @param {RegExp} regexp
* @return {Array} Returns the filtered array
* @private
*/
function filterRegExp(array, regexp) {
if (arraySize(array).length !== 1) {
throw new Error('Only one dimensional matrices supported');
}
return Array.prototype.filter.call(array, function (entry) {
return regexp.test(entry);
});
}
/**
* A safe join
* @param {Array} array
* @param {string} separator
*/
function join(array, separator) {
return Array.prototype.join.call(array, separator);
}
/**
* Assign a numeric identifier to every element of a sorted array
* @param {Array} a An array
* @return {Array} An array of objects containing the original value and its identifier
*/
function identify(a) {
if (!Array.isArray(a)) {
throw new TypeError('Array input expected');
}
if (a.length === 0) {
return a;
}
var b = [];
var count = 0;
b[0] = {
value: a[0],
identifier: 0
};
for (var i = 1; i < a.length; i++) {
if (a[i] === a[i - 1]) {
count++;
} else {
count = 0;
}
b.push({
value: a[i],
identifier: count
});
}
return b;
}
/**
* Remove the numeric identifier from the elements
* @param {array} a An array
* @return {array} An array of values without identifiers
*/
function generalize(a) {
if (!Array.isArray(a)) {
throw new TypeError('Array input expected');
}
if (a.length === 0) {
return a;
}
var b = [];
for (var i = 0; i < a.length; i++) {
b.push(a[i].value);
}
return b;
}
/**
* Check the datatype of a given object
* This is a low level implementation that should only be used by
* parent Matrix classes such as SparseMatrix or DenseMatrix
* This method does not validate Array Matrix shape
* @param {Array} array
* @param {function} typeOf Callback function to use to determine the type of a value
* @return string
*/
function getArrayDataType(array, typeOf) {
var type; // to hold type info
var length = 0; // to hold length value to ensure it has consistent sizes
for (var i = 0; i < array.length; i++) {
var item = array[i];
var isArray = Array.isArray(item); // Saving the target matrix row size
if (i === 0 && isArray) {
length = item.length;
} // If the current item is an array but the length does not equal the targetVectorSize
if (isArray && item.length !== length) {
return undefined;
}
var itemType = isArray ? getArrayDataType(item, typeOf) // recurse into a nested array
: typeOf(item);
if (type === undefined) {
type = itemType; // first item
} else if (type !== itemType) {
return 'mixed';
} else {// we're good, everything has the same type so far
}
}
return type;
}
/**
* Return the last item from an array
* @param array
* @returns {*}
*/
function last(array) {
return array[array.length - 1];
}
/**
* Get all but the last element of array.
*/
function initial(array) {
return array.slice(0, array.length - 1);
}
/**
* Test whether an array or string contains an item
* @param {Array | string} array
* @param {*} item
* @return {boolean}
*/
function contains(array, item) {
return array.indexOf(item) !== -1;
}
/***/ }),
/* 3 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return clone; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "i", function() { return mapObject; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function() { return extend; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return deepExtend; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function() { return deepStrictEqual; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return deepFlatten; });
/* unused harmony export canDefineProperty */
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "h", function() { return lazy; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "k", function() { return traverse; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function() { return hasOwnProperty; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "g", function() { return isLegacyFactory; });
/* unused harmony export get */
/* unused harmony export set */
/* unused harmony export pick */
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "j", function() { return pickShallow; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "l", function() { return values; });
/* harmony import */ var _is__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* Clone an object
*
* clone(x)
*
* Can clone any primitive type, array, and object.
* If x has a function clone, this function will be invoked to clone the object.
*
* @param {*} x
* @return {*} clone
*/
function clone(x) {
var type = _typeof(x); // immutable primitive types
if (type === 'number' || type === 'string' || type === 'boolean' || x === null || x === undefined) {
return x;
} // use clone function of the object when available
if (typeof x.clone === 'function') {
return x.clone();
} // array
if (Array.isArray(x)) {
return x.map(function (value) {
return clone(value);
});
}
if (x instanceof Date) return new Date(x.valueOf());
if (Object(_is__WEBPACK_IMPORTED_MODULE_0__[/* isBigNumber */ "e"])(x)) return x; // bignumbers are immutable
if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x); // TODO: clone a RegExp
// object
return mapObject(x, clone);
}
/**
* Apply map to all properties of an object
* @param {Object} object
* @param {function} callback
* @return {Object} Returns a copy of the object with mapped properties
*/
function mapObject(object, callback) {
var clone = {};
for (var key in object) {
if (hasOwnProperty(object, key)) {
clone[key] = callback(object[key]);
}
}
return clone;
}
/**
* Extend object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @return {Object} a
*/
function extend(a, b) {
for (var prop in b) {
if (hasOwnProperty(b, prop)) {
a[prop] = b[prop];
}
}
return a;
}
/**
* Deep extend an object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @returns {Object}
*/
function deepExtend(a, b) {
// TODO: add support for Arrays to deepExtend
if (Array.isArray(b)) {
throw new TypeError('Arrays are not supported by deepExtend');
}
for (var prop in b) {
if (hasOwnProperty(b, prop)) {
if (b[prop] && b[prop].constructor === Object) {
if (a[prop] === undefined) {
a[prop] = {};
}
if (a[prop] && a[prop].constructor === Object) {
deepExtend(a[prop], b[prop]);
} else {
a[prop] = b[prop];
}
} else if (Array.isArray(b[prop])) {
throw new TypeError('Arrays are not supported by deepExtend');
} else {
a[prop] = b[prop];
}
}
}
return a;
}
/**
* Deep test equality of all fields in two pairs of arrays or objects.
* Compares values and functions strictly (ie. 2 is not the same as '2').
* @param {Array | Object} a
* @param {Array | Object} b
* @returns {boolean}
*/
function deepStrictEqual(a, b) {
var prop, i, len;
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
if (a.length !== b.length) {
return false;
}
for (i = 0, len = a.length; i < len; i++) {
if (!deepStrictEqual(a[i], b[i])) {
return false;
}
}
return true;
} else if (typeof a === 'function') {
return a === b;
} else if (a instanceof Object) {
if (Array.isArray(b) || !(b instanceof Object)) {
return false;
}
for (prop in a) {
// noinspection JSUnfilteredForInLoop
if (!(prop in b) || !deepStrictEqual(a[prop], b[prop])) {
return false;
}
}
for (prop in b) {
// noinspection JSUnfilteredForInLoop
if (!(prop in a) || !deepStrictEqual(a[prop], b[prop])) {
return false;
}
}
return true;
} else {
return a === b;
}
}
/**
* Recursively flatten a nested object.
* @param {Object} nestedObject
* @return {Object} Returns the flattened object
*/
function deepFlatten(nestedObject) {
var flattenedObject = {};
_deepFlatten(nestedObject, flattenedObject);
return flattenedObject;
} // helper function used by deepFlatten
function _deepFlatten(nestedObject, flattenedObject) {
for (var prop in nestedObject) {
if (hasOwnProperty(nestedObject, prop)) {
var value = nestedObject[prop];
if (_typeof(value) === 'object' && value !== null) {
_deepFlatten(value, flattenedObject);
} else {
flattenedObject[prop] = value;
}
}
}
}
/**
* Test whether the current JavaScript engine supports Object.defineProperty
* @returns {boolean} returns true if supported
*/
function canDefineProperty() {
// test needed for broken IE8 implementation
try {
if (Object.defineProperty) {
Object.defineProperty({}, 'x', {
get: function get() {}
});
return true;
}
} catch (e) {}
return false;
}
/**
* Attach a lazy loading property to a constant.
* The given function `fn` is called once when the property is first requested.
*
* @param {Object} object Object where to add the property
* @param {string} prop Property name
* @param {Function} valueResolver Function returning the property value. Called
* without arguments.
*/
function lazy(object, prop, valueResolver) {
var _uninitialized = true;
var _value;
Object.defineProperty(object, prop, {
get: function get() {
if (_uninitialized) {
_value = valueResolver();
_uninitialized = false;
}
return _value;
},
set: function set(value) {
_value = value;
_uninitialized = false;
},
configurable: true,
enumerable: true
});
}
/**
* Traverse a path into an object.
* When a namespace is missing, it will be created
* @param {Object} object
* @param {string | string[]} path A dot separated string like 'name.space'
* @return {Object} Returns the object at the end of the path
*/
function traverse(object, path) {
if (path && typeof path === 'string') {
return traverse(object, path.split('.'));
}
var obj = object;
if (path) {
for (var i = 0; i < path.length; i++) {
var key = path[i];
if (!(key in obj)) {
obj[key] = {};
}
obj = obj[key];
}
}
return obj;
}
/**
* A safe hasOwnProperty
* @param {Object} object
* @param {string} property
*/
function hasOwnProperty(object, property) {
return object && Object.hasOwnProperty.call(object, property);
}
/**
* Test whether an object is a factory. a factory has fields:
*
* - factory: function (type: Object, config: Object, load: function, typed: function [, math: Object]) (required)
* - name: string (optional)
* - path: string A dot separated path (optional)
* - math: boolean If true (false by default), the math namespace is passed
* as fifth argument of the factory function
*
* @param {*} object
* @returns {boolean}
*/
function isLegacyFactory(object) {
return object && typeof object.factory === 'function';
}
/**
* Get a nested property from an object
* @param {Object} object
* @param {string | string[]} path
* @returns {Object}
*/
function get(object, path) {
if (typeof path === 'string') {
if (isPath(path)) {
return get(object, path.split('.'));
} else {
return object[path];
}
}
var child = object;
for (var i = 0; i < path.length; i++) {
var key = path[i];
child = child ? child[key] : undefined;
}
return child;
}
/**
* Set a nested property in an object
* Mutates the object itself
* If the path doesn't exist, it will be created
* @param {Object} object
* @param {string | string[]} path
* @param {*} value
* @returns {Object}
*/
function set(object, path, value) {
if (typeof path === 'string') {
if (isPath(path)) {
return set(object, path.split('.'), value);
} else {
object[path] = value;
return object;
}
}
var child = object;
for (var i = 0; i < path.length - 1; i++) {
var key = path[i];
if (child[key] === undefined) {
child[key] = {};
}
child = child[key];
}
if (path.length > 0) {
var lastKey = path[path.length - 1];
child[lastKey] = value;
}
return object;
}
/**
* Create an object composed of the picked object properties
* @param {Object} object
* @param {string[]} properties
* @param {function} [transform] Optional value to transform a value when picking it
* @return {Object}
*/
function pick(object, properties, transform) {
var copy = {};
for (var i = 0; i < properties.length; i++) {
var key = properties[i];
var value = get(object, key);
if (value !== undefined) {
set(copy, key, transform ? transform(value, key) : value);
}
}
return copy;
}
/**
* Shallow version of pick, creating an object composed of the picked object properties
* but not for nested properties
* @param {Object} object
* @param {string[]} properties
* @return {Object}
*/
function pickShallow(object, properties) {
var copy = {};
for (var i = 0; i < properties.length; i++) {
var key = properties[i];
var value = object[key];
if (value !== undefined) {
copy[key] = va