/** * 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 3.9.3 * @date 2017-02-22 * * @license * Copyright (C) 2013-2017 Jos de Jong * * 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 * * http://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; /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ // 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 = 509); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Execute the callback function element wise for each element in array and any * nested array * Returns an array with the results * @param {Array | Matrix} array * @param {Function} callback The callback is called with two parameters: * value1 and value2, which contain the current * element of both arrays. * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. * * @return {Array | Matrix} res */ module.exports = function deepMap(array, callback, skipZeros) { if (array && (typeof array.map === 'function')) { // TODO: replace array.map with a for loop to improve performance return array.map(function (x) { return deepMap(x, callback, skipZeros); }); } else { return callback(array); } }; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { /** * Create a Matrix. The function creates a new `math.type.Matrix` object from * an `Array`. A Matrix has utility functions to manipulate the data in the * matrix, like getting the size and getting or setting values in the matrix. * Supported storage formats are 'dense' and 'sparse'. * * Syntax: * * math.matrix() // creates an empty matrix using default storage format (dense). * math.matrix(data) // creates a matrix with initial data using default storage format (dense). * math.matrix('dense') // creates an empty matrix using the given storage format. * math.matrix(data, 'dense') // creates a matrix with initial data using the given storage format. * math.matrix(data, 'sparse') // creates a sparse matrix with initial data. * math.matrix(data, 'sparse', 'number') // creates a sparse matrix with initial data, number data type. * * Examples: * * var m = math.matrix([[1, 2], [3, 4]]); * m.size(); // Array [2, 2] * m.resize([3, 2], 5); * m.valueOf(); // Array [[1, 2], [3, 4], [5, 5]] * m.get([1, 0]) // number 3 * * See also: * * bignumber, boolean, complex, index, number, string, unit, sparse * * @param {Array | Matrix} [data] A multi dimensional array * @param {string} [format] The Matrix storage format * * @return {Matrix} The created matrix */ var matrix = typed('matrix', { '': function () { return _create([]); }, 'string': function (format) { return _create([], format); }, 'string, string': function (format, datatype) { return _create([], format, datatype); }, 'Array': function (data) { return _create(data); }, 'Matrix': function (data) { return _create(data, data.storage()); }, 'Array | Matrix, string': _create, 'Array | Matrix, string, string': _create }); matrix.toTex = { 0: '\\begin{bmatrix}\\end{bmatrix}', 1: '\\left(${args[0]}\\right)', 2: '\\left(${args[0]}\\right)' }; return matrix; /** * Create a new Matrix with given storage format * @param {Array} data * @param {string} [format] * @param {string} [datatype] * @returns {Matrix} Returns a new Matrix * @private */ function _create(data, format, datatype) { // get storage format constructor var M = type.Matrix.storage(format || 'default'); // create instance return new M(data, datatype); } } exports.name = 'matrix'; exports.factory = factory; /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * @typedef {{sign: '+' | '-' | '', coefficients: number[], exponent: number}} SplitValue */ /** * Test whether value is a number * @param {*} value * @return {boolean} isNumber */ exports.isNumber = function(value) { return typeof value === 'number'; }; /** * Check if a number is integer * @param {number | boolean} value * @return {boolean} isInteger */ exports.isInteger = function(value) { return isFinite(value) ? (value == Math.round(value)) : false; // Note: we use ==, not ===, as we can have Booleans as well }; /** * Calculate the sign of a number * @param {number} x * @returns {*} */ exports.sign = Math.sign || function(x) { if (x > 0) { return 1; } else if (x < 0) { return -1; } else { return 0; } }; /** * Convert a number to a formatted string representation. * * Syntax: * * format(value) * format(value, options) * format(value, precision) * format(value, fn) * * Where: * * {number} value The value to be formatted * {Object} options An object with formatting options. Available options: * {string} notation * Number notation. Choose from: * 'fixed' Always use regular number notation. * For example '123.40' and '14000000' * 'exponential' Always use exponential notation. * For example '1.234e+2' and '1.4e+7' * 'engineering' Always use engineering notation. * For example '123.4e+0' and '14.0e+6' * 'auto' (default) Regular number notation for numbers * having an absolute value between * `lower` and `upper` bounds, and uses * exponential notation elsewhere. * Lower bound is included, upper bound * is excluded. * For example '123.4' and '1.4e7'. * {number} precision A number between 0 and 16 to round * the digits of the number. * In case of notations 'exponential' and * 'auto', `precision` defines the total * number of significant digits returned * and is undefined by default. * In case of notation 'fixed', * `precision` defines the number of * significant digits after the decimal * point, and is 0 by default. * {Object} exponential An object containing two parameters, * {number} lower and {number} upper, * used by notation 'auto' to determine * when to return exponential notation. * Default values are `lower=1e-3` and * `upper=1e5`. * Only applicable for notation `auto`. * {Function} fn A custom formatting function. Can be used to override the * built-in notations. Function `fn` is called with `value` as * parameter and must return a string. Is useful for example to * format all values inside a matrix in a particular way. * * Examples: * * format(6.4); // '6.4' * format(1240000); // '1.24e6' * format(1/3); // '0.3333333333333333' * format(1/3, 3); // '0.333' * format(21385, 2); // '21000' * format(12.071, {notation: 'fixed'}); // '12' * format(2.3, {notation: 'fixed', precision: 2}); // '2.30' * format(52.8, {notation: 'exponential'}); // '5.28e+1' * format(12345678, {notation: 'engineering'}); // '12.345678e+6' * * @param {number} value * @param {Object | Function | number} [options] * @return {string} str The formatted value */ exports.format = function(value, options) { if (typeof options === 'function') { // handle format(value, fn) return options(value); } // handle special cases if (value === Infinity) { return 'Infinity'; } else if (value === -Infinity) { return '-Infinity'; } else if (isNaN(value)) { return 'NaN'; } // default values for options var notation = 'auto'; var precision = undefined; if (options) { // determine notation from options if (options.notation) { notation = options.notation; } // determine precision from options if (exports.isNumber(options)) { precision = options; } else if (options.precision) { precision = options.precision; } } // handle the various notations switch (notation) { case 'fixed': return exports.toFixed(value, precision); case 'exponential': return exports.toExponential(value, precision); case 'engineering': return exports.toEngineering(value, precision); case 'auto': return exports .toPrecision(value, precision, options && options.exponential) // remove trailing zeros after the decimal point .replace(/((\.\d*?)(0+))($|e)/, function () { var digits = arguments[2]; var e = arguments[4]; return (digits !== '.') ? digits + e : e; }); default: throw new Error('Unknown notation "' + notation + '". ' + 'Choose "auto", "exponential", or "fixed".'); } }; /** * Split a number into sign, coefficients, and exponent * @param {number | string} value * @return {SplitValue} * Returns an object containing sign, coefficients, and exponent */ exports.splitNumber = function (value) { // parse the input value var match = String(value).toLowerCase().match(/^0*?(-?)(\d+\.?\d*)(e([+-]?\d+))?$/); if (!match) { throw new SyntaxError('Invalid number ' + value); } var sign = match[1]; var digits = match[2]; var exponent = parseFloat(match[4] || '0'); var dot = digits.indexOf('.'); exponent += (dot !== -1) ? (dot - 1) : (digits.length - 1); var coefficients = digits .replace('.', '') // remove the dot (must be removed before removing leading zeros) .replace(/^0*/, function (zeros) { // remove leading zeros, add their count to the exponent exponent -= zeros.length; return ''; }) .replace(/0*$/, '') // remove trailing zeros .split('') .map(function (d) { return parseInt(d); }); if (coefficients.length === 0) { coefficients.push(0); exponent++; } return { sign: sign, coefficients: coefficients, exponent: exponent }; }; /** * Format a number in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3' * @param {number | string} value * @param {number} [precision=0] Optional number of decimals after the * decimal point. Zero by default. */ exports.toEngineering = function (value, precision) { if (isNaN(value) || !isFinite(value)) { return String(value); } var rounded = exports.roundDigits(exports.splitNumber(value), precision); var e = rounded.exponent; var c = rounded.coefficients; // find nearest lower multiple of 3 for exponent var newExp = e % 3 === 0 ? e : (e < 0 ? (e - 3) - (e % 3) : e - (e % 3)); // concatenate coefficients with necessary zeros var significandsDiff = e >= 0 ? e : Math.abs(newExp); // add zeros if necessary (for ex: 1e+8) if (c.length - 1 < significandsDiff) c = c.concat(zeros(significandsDiff - (c.length - 1))); // find difference in exponents var expDiff = Math.abs(e - newExp); var decimalIdx = 1; // push decimal index over by expDiff times while (--expDiff >= 0) decimalIdx++; // if all coefficient values are zero after the decimal point, don't add a decimal value. // otherwise concat with the rest of the coefficients var decimals = c.slice(decimalIdx).join(''); var decimalVal = decimals.match(/[1-9]/) ? ('.' + decimals) : ''; var str = c.slice(0, decimalIdx).join('') + decimalVal + 'e' + (e >= 0 ? '+' : '') + newExp.toString(); return rounded.sign + str; }; /** * Format a number with fixed notation. * @param {number | string} value * @param {number} [precision=0] Optional number of decimals after the * decimal point. Zero by default. */ exports.toFixed = function (value, precision) { if (isNaN(value) || !isFinite(value)) { return String(value); } var splitValue = exports.splitNumber(value) var rounded = exports.roundDigits(splitValue, splitValue.exponent + 1 + (precision || 0)); var c = rounded.coefficients; var p = rounded.exponent + 1; // exponent may have changed // append zeros if needed var pp = p + (precision || 0); if (c.length < pp) { c = c.concat(zeros(pp - c.length)); } // prepend zeros if needed if (p < 0) { c = zeros(-p + 1).concat(c); p = 1; } // insert a dot if needed if (precision) { c.splice(p, 0, (p === 0) ? '0.' : '.'); } return rounded.sign + c.join(''); }; /** * Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3' * @param {number | string} value * @param {number} [precision] Number of digits in formatted output. * If not provided, the maximum available digits * is used. */ exports.toExponential = function (value, precision) { if (isNaN(value) || !isFinite(value)) { return String(value); } // round if needed, else create a clone var split = exports.splitNumber(value) var rounded = precision ? exports.roundDigits(split, precision) : split; var c = rounded.coefficients; var e = rounded.exponent; // append zeros if needed if (c.length < precision) { c = c.concat(zeros(precision - c.length)); } // format as `C.CCCe+EEE` or `C.CCCe-EEE` var first = c.shift(); return rounded.sign + first + (c.length > 0 ? ('.' + c.join('')) : '') + 'e' + (e >= 0 ? '+' : '') + e; } /** * Format a number with a certain precision * @param {number | string} value * @param {number} [precision=undefined] Optional number of digits. * @param {{lower: number | undefined, upper: number | undefined}} [options] * By default: * lower = 1e-3 (excl) * upper = 1e+5 (incl) * @return {string} */ exports.toPrecision = function (value, precision, options) { if (isNaN(value) || !isFinite(value)) { return String(value); } // determine lower and upper bound for exponential notation. var lower = (options && options.lower !== undefined) ? options.lower : 1e-3; var upper = (options && options.upper !== undefined) ? options.upper : 1e+5; var split = exports.splitNumber(value) var abs = Math.abs(Math.pow(10, split.exponent)); if (abs < lower || abs >= upper) { // exponential notation return exports.toExponential(value, precision); } else { var rounded = precision ? exports.roundDigits(split, precision) : split; var c = rounded.coefficients; var e = rounded.exponent; // append trailing zeros if (c.length < precision) { c = c.concat(zeros(precision - c.length)); } // append trailing zeros // TODO: simplify the next statement c = c.concat(zeros(e - c.length + 1 + (c.length < precision ? precision - c.length : 0))); // prepend zeros c = zeros(-e).concat(c); var dot = e > 0 ? e : 0; if (dot < c.length - 1) { c.splice(dot + 1, 0, '.'); } return rounded.sign + c.join(''); } } /** * Round the number of digits of a number * * @param {SplitValue} split A value split with .splitNumber(value) * @param {number} precision A positive integer * @return {SplitValue} * Returns an object containing sign, coefficients, and exponent * with rounded digits */ exports.roundDigits = function (split, precision) { // create a clone var rounded = { sign: split.sign, coefficients: split.coefficients, exponent: split.exponent } var c = rounded.coefficients; // prepend zeros if needed while (precision <= 0) { c.unshift(0); rounded.exponent++; precision++; } if (c.length > precision) { var removed = c.splice(precision, c.length - precision); if (removed[0] >= 5) { var i = precision - 1; c[i]++; while (c[i] === 10) { c.pop(); if (i === 0) { c.unshift(0); rounded.exponent++; i++; } i--; c[i]++; } } } return rounded; }; /** * Create an array filled with zeros. * @param {number} length * @return {Array} */ function zeros(length) { var arr = []; for (var i = 0; i < length; i++) { arr.push(0); } return arr; } /** * Count the number of significant digits of a number. * * For example: * 2.34 returns 3 * 0.0034 returns 2 * 120.5e+30 returns 4 * * @param {number} value * @return {number} digits Number of significant digits */ exports.digits = function(value) { return value .toExponential() .replace(/e.*$/, '') // remove exponential notation .replace( /^0\.?0*|\./, '') // remove decimal point and leading zeros .length }; /** * Minimum number added to one that makes the result different than one */ exports.DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16; /** * Compares two floating point numbers. * @param {number} x First value to compare * @param {number} y Second value to compare * @param {number} [epsilon] The maximum relative difference between x and y * If epsilon is undefined or null, the function will * test whether x and y are exactly equal. * @return {boolean} whether the two numbers are nearly equal */ exports.nearlyEqual = function(x, y, epsilon) { // if epsilon is null or undefined, test whether x and y are exactly equal if (epsilon == null) { return x == y; } // use "==" operator, handles infinities if (x == y) { return true; } // NaN if (isNaN(x) || isNaN(y)) { return false; } // at this point x and y should be finite if(isFinite(x) && isFinite(y)) { // check numbers are very close, needed when comparing numbers near zero var diff = Math.abs(x - y); if (diff < exports.DBL_EPSILON) { return true; } else { // use relative error return diff <= Math.max(Math.abs(x), Math.abs(y)) * epsilon; } } // Infinite and Number or negative Infinite and positive Infinite cases return false; }; /***/ }), /* 3 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; exports.symbols = { // GREEK LETTERS Alpha: 'A', alpha: '\\alpha', Beta: 'B', beta: '\\beta', Gamma: '\\Gamma', gamma: '\\gamma', Delta: '\\Delta', delta: '\\delta', Epsilon: 'E', epsilon: '\\epsilon', varepsilon: '\\varepsilon', Zeta: 'Z', zeta: '\\zeta', Eta: 'H', eta: '\\eta', Theta: '\\Theta', theta: '\\theta', vartheta: '\\vartheta', Iota: 'I', iota: '\\iota', Kappa: 'K', kappa: '\\kappa', varkappa: '\\varkappa', Lambda: '\\Lambda', lambda: '\\lambda', Mu: 'M', mu: '\\mu', Nu: 'N', nu: '\\nu', Xi: '\\Xi', xi: '\\xi', Omicron: 'O', omicron: 'o', Pi: '\\Pi', pi: '\\pi', varpi: '\\varpi', Rho: 'P', rho: '\\rho', varrho: '\\varrho', Sigma: '\\Sigma', sigma: '\\sigma', varsigma: '\\varsigma', Tau: 'T', tau: '\\tau', Upsilon: '\\Upsilon', upsilon: '\\upsilon', Phi: '\\Phi', phi: '\\phi', varphi: '\\varphi', Chi: 'X', chi: '\\chi', Psi: '\\Psi', psi: '\\psi', Omega: '\\Omega', omega: '\\omega', //logic 'true': '\\mathrm{True}', 'false': '\\mathrm{False}', //other i: 'i', //TODO use \i ?? inf: '\\infty', Inf: '\\infty', infinity: '\\infty', Infinity: '\\infty', oo: '\\infty', lim: '\\lim', 'undefined': '\\mathbf{?}' }; exports.operators = { 'transpose': '^\\top', 'factorial': '!', 'pow': '^', 'dotPow': '.^\\wedge', //TODO find ideal solution 'unaryPlus': '+', 'unaryMinus': '-', 'bitNot': '~', //TODO find ideal solution 'not': '\\neg', 'multiply': '\\cdot', 'divide': '\\frac', //TODO how to handle that properly? 'dotMultiply': '.\\cdot', //TODO find ideal solution 'dotDivide': '.:', //TODO find ideal solution 'mod': '\\mod', 'add': '+', 'subtract': '-', 'to': '\\rightarrow', 'leftShift': '<<', 'rightArithShift': '>>', 'rightLogShift': '>>>', 'equal': '=', 'unequal': '\\neq', 'smaller': '<', 'larger': '>', 'smallerEq': '\\leq', 'largerEq': '\\geq', 'bitAnd': '\\&', 'bitXor': '\\underline{|}', 'bitOr': '|', 'and': '\\wedge', 'xor': '\\veebar', 'or': '\\vee' }; exports.defaultTemplate = '\\mathrm{${name}}\\left(${args}\\right)'; var units = { deg: '^\\circ' }; //@param {string} name //@param {boolean} isUnit exports.toSymbol = function (name, isUnit) { isUnit = typeof isUnit === 'undefined' ? false : isUnit; if (isUnit) { if (units.hasOwnProperty(name)) { return units[name]; } return '\\mathrm{' + name + '}'; } if (exports.symbols.hasOwnProperty(name)) { return exports.symbols[name]; } else if (name.indexOf('_') !== -1) { //symbol with index (eg. alpha_1) var index = name.indexOf('_'); return exports.toSymbol(name.substring(0, index)) + '_{' + exports.toSymbol(name.substring(index + 1)) + '}'; } return name; }; /***/ }), /* 4 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * 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 */ exports.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 Number) return new Number(x.valueOf()); if (x instanceof String) return new String(x.valueOf()); if (x instanceof Boolean) return new Boolean(x.valueOf()); if (x instanceof Date) return new Date(x.valueOf()); if (x && x.isBigNumber === true) return x; // bignumbers are immutable if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x); // TODO: clone a RegExp // object var m = {}; for (var key in x) { if (x.hasOwnProperty(key)) { m[key] = clone(x[key]); } } return m; }; /** * Extend object a with the properties of object b * @param {Object} a * @param {Object} b * @return {Object} a */ exports.extend = function(a, b) { for (var prop in b) { if (b.hasOwnProperty(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} */ exports.deepExtend = 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 (b.hasOwnProperty(prop)) { if (b[prop] && b[prop].constructor === Object) { if (a[prop] === undefined) { a[prop] = {}; } if (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. * @param {Array | Object} a * @param {Array | Object} b * @returns {boolean} */ exports.deepEqual = function deepEqual (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 (!exports.deepEqual(a[i], b[i])) { return false; } } return true; } else if (a instanceof Object) { if (Array.isArray(b) || !(b instanceof Object)) { return false; } for (prop in a) { //noinspection JSUnfilteredForInLoop if (!exports.deepEqual(a[prop], b[prop])) { return false; } } for (prop in b) { //noinspection JSUnfilteredForInLoop if (!exports.deepEqual(a[prop], b[prop])) { return false; } } return true; } else { return (typeof a === typeof b) && (a == b); } }; /** * Test whether the current JavaScript engine supports Object.defineProperty * @returns {boolean} returns true if supported */ exports.canDefineProperty = function () { // test needed for broken IE8 implementation try { if (Object.defineProperty) { Object.defineProperty({}, 'x', { get: function () {} }); 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. * On older browsers ( 0 ? _iterate(cf, 0, asize, asize[0], adata, b, inverse) : []; // c matrix return new DenseMatrix({ data: cdata, size: clone(asize), datatype: dt }); }; // recursive function var _iterate = function (f, level, s, n, av, bv, inverse) { // initialize array for this level var cv = []; // check we reach the last level if (level === s.length - 1) { // loop arrays in last level for (var i = 0; i < n; i++) { // invoke callback and store value cv[i] = inverse ? f(bv, av[i]) : f(av[i], bv); } } else { // iterate current level for (var j = 0; j < n; j++) { // iterate next level cv[j] = _iterate(f, level + 1, s, s[level + 1], av[j], bv, inverse); } } return cv; }; return algorithm14; } exports.name = 'algorithm14'; exports.factory = factory; /***/ }), /* 6 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var DimensionError = __webpack_require__(9); var string = util.string, isString = string.isString; function factory (type, config, load, typed) { var DenseMatrix = type.DenseMatrix; /** * Iterates over DenseMatrix items and invokes the callback function f(Aij..z, Bij..z). * Callback function invoked MxN times. * * C(i,j,...z) = f(Aij..z, Bij..z) * * @param {Matrix} a The DenseMatrix instance (A) * @param {Matrix} b The DenseMatrix instance (B) * @param {Function} callback The f(Aij..z,Bij..z) operation to invoke * * @return {Matrix} DenseMatrix (C) * * https://github.com/josdejong/mathjs/pull/346#issuecomment-97658658 */ var algorithm13 = function (a, b, callback) { // a arrays var adata = a._data; var asize = a._size; var adt = a._datatype; // b arrays var bdata = b._data; var bsize = b._size; var bdt = b._datatype; // c arrays var csize = []; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // validate each one of the dimension sizes for (var s = 0; s < asize.length; s++) { // must match if (asize[s] !== bsize[s]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // update dimension in c csize[s] = asize[s]; } // datatype var dt; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // convert b to the same datatype b = typed.convert(b, dt); // callback cf = typed.find(callback, [dt, dt]); } // populate cdata, iterate through dimensions var cdata = csize.length > 0 ? _iterate(cf, 0, csize, csize[0], adata, bdata) : []; // c matrix return new DenseMatrix({ data: cdata, size: csize, datatype: dt }); }; // recursive function var _iterate = function (f, level, s, n, av, bv) { // initialize array for this level var cv = []; // check we reach the last level if (level === s.length - 1) { // loop arrays in last level for (var i = 0; i < n; i++) { // invoke callback and store value cv[i] = f(av[i], bv[i]); } } else { // iterate current level for (var j = 0; j < n; j++) { // iterate next level cv[j] = _iterate(f, level + 1, s, s[level + 1], av[j], bv[j]); } } return cv; }; return algorithm13; } exports.name = 'algorithm13'; exports.factory = factory; /***/ }), /* 7 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var number = __webpack_require__(2); var string = __webpack_require__(19); var object = __webpack_require__(4); var types = __webpack_require__(60); var DimensionError = __webpack_require__(9); var IndexError = __webpack_require__(42); /** * 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 */ exports.size = function (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 DimensionError(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 DimensionError(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 DimensionError(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 */ exports.validate = function(array, size) { var isScalar = (size.length == 0); if (isScalar) { // scalar if (Array.isArray(array)) { throw new DimensionError(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 */ exports.validateIndex = function(index, length) { if (!number.isNumber(index) || !number.isInteger(index)) { throw new TypeError('Index must be an integer (value: ' + index + ')'); } if (index < 0 || (typeof length === 'number' && index >= length)) { throw new IndexError(index, length); } }; // a constant used to specify an undefined defaultValue exports.UNINITIALIZED = {}; /** * Resize a multi dimensional array. The resized array is returned. * @param {Array} array Array to be resized * @param {Array.} size Array with the size of each dimension * @param {*} [defaultValue=0] Value to be filled in in new entries, * zero by default. To leave new entries undefined, * specify array.UNINITIALIZED as defaultValue * @return {Array} array The resized array */ exports.resize = function(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 (!number.isNumber(value) || !number.isInteger(value) || value < 0) { throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + string.format(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]; } } if(defaultValue !== exports.UNINITIALIZED) { // fill new elements with the default value for (i = minLen; i < newLen; i++) { array[i] = defaultValue; } } } } /** * Squeeze a multi dimensional array * @param {Array} array * @param {Array} [size] * @returns {Array} returns the array itself */ exports.squeeze = function(array, size) { var s = size || exports.size(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 */ exports.unsqueeze = function(array, dims, outer, size) { var s = size || exports.size(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) */ exports.flatten = function(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; }; /** * Test whether an object is an array * @param {*} value * @return {boolean} isArray */ exports.isArray = Array.isArray; /***/ }), /* 8 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var nearlyEqual = __webpack_require__(2).nearlyEqual; var bigNearlyEqual = __webpack_require__(31); function factory (type, config, load, typed) { /** * Test whether two values are equal. * * @param {number | BigNumber | Fraction | boolean | Complex | Unit} x First value to compare * @param {number | BigNumber | Fraction | boolean | Complex} y Second value to compare * @return {boolean} Returns true when the compared values are equal, else returns false * @private */ var equalScalar = typed('equalScalar', { 'boolean, boolean': function (x, y) { return x === y; }, 'number, number': function (x, y) { return x === y || nearlyEqual(x, y, config.epsilon); }, 'BigNumber, BigNumber': function (x, y) { return x.eq(y) || bigNearlyEqual(x, y, config.epsilon); }, 'Fraction, Fraction': function (x, y) { return x.equals(y); }, 'Complex, Complex': function (x, y) { return x.equals(y); }, 'Unit, Unit': function (x, y) { if (!x.equalBase(y)) { throw new Error('Cannot compare units with different base'); } return equalScalar(x.value, y.value); }, 'string, string': function (x, y) { return x === y; } }); return equalScalar; } exports.factory = factory; /***/ }), /* 9 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Create a range error with the message: * 'Dimension mismatch ( != )' * @param {number | number[]} actual The actual size * @param {number | number[]} expected The expected size * @param {string} [relation='!='] Optional relation between actual * and expected size: '!=', '<', etc. * @extends RangeError */ function DimensionError(actual, expected, relation) { if (!(this instanceof DimensionError)) { throw new SyntaxError('Constructor must be called with the new operator'); } this.actual = actual; this.expected = expected; this.relation = relation; this.message = 'Dimension mismatch (' + (Array.isArray(actual) ? ('[' + actual.join(', ') + ']') : actual) + ' ' + (this.relation || '!=') + ' ' + (Array.isArray(expected) ? ('[' + expected.join(', ') + ']') : expected) + ')'; this.stack = (new Error()).stack; } DimensionError.prototype = new RangeError(); DimensionError.prototype.constructor = RangeError; DimensionError.prototype.name = 'DimensionError'; DimensionError.prototype.isDimensionError = true; module.exports = DimensionError; /***/ }), /* 10 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var extend = __webpack_require__(4).extend; var array = __webpack_require__(7); function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var addScalar = load(__webpack_require__(17)); var multiplyScalar = load(__webpack_require__(20)); var equalScalar = load(__webpack_require__(8)); var algorithm11 = load(__webpack_require__(13)); var algorithm14 = load(__webpack_require__(5)); var DenseMatrix = type.DenseMatrix; var SparseMatrix = type.SparseMatrix; /** * Multiply two or more values, `x * y`. * For matrices, the matrix product is calculated. * * Syntax: * * math.multiply(x, y) * math.multiply(x, y, z, ...) * * Examples: * * math.multiply(4, 5.2); // returns number 20.8 * math.multiply(2, 3, 4); // returns number 24 * * var a = math.complex(2, 3); * var b = math.complex(4, 1); * math.multiply(a, b); // returns Complex 5 + 14i * * var c = [[1, 2], [4, 3]]; * var d = [[1, 2, 3], [3, -4, 7]]; * math.multiply(c, d); // returns Array [[7, -6, 17], [13, -4, 33]] * * var e = math.unit('2.1 km'); * math.multiply(3, e); // returns Unit 6.3 km * * See also: * * divide, prod, cross, dot * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to multiply * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to multiply * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Multiplication of `x` and `y` */ var multiply = typed('multiply', extend({ // we extend the signatures of multiplyScalar with signatures dealing with matrices 'Array, Array': function (x, y) { // check dimensions _validateMatrixDimensions(array.size(x), array.size(y)); // use dense matrix implementation var m = multiply(matrix(x), matrix(y)); // return array or scalar return (m && m.isMatrix === true) ? m.valueOf() : m; }, 'Matrix, Matrix': function (x, y) { // dimensions var xsize = x.size(); var ysize = y.size(); // check dimensions _validateMatrixDimensions(xsize, ysize); // process dimensions if (xsize.length === 1) { // process y dimensions if (ysize.length === 1) { // Vector * Vector return _multiplyVectorVector(x, y, xsize[0]); } // Vector * Matrix return _multiplyVectorMatrix(x, y); } // process y dimensions if (ysize.length === 1) { // Matrix * Vector return _multiplyMatrixVector(x, y); } // Matrix * Matrix return _multiplyMatrixMatrix(x, y); }, 'Matrix, Array': function (x, y) { // use Matrix * Matrix implementation return multiply(x, matrix(y)); }, 'Array, Matrix': function (x, y) { // use Matrix * Matrix implementation return multiply(matrix(x, y.storage()), y); }, 'Matrix, any': function (x, y) { // result var c; // process storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, multiplyScalar, false); break; case 'dense': c = algorithm14(x, y, multiplyScalar, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm11(y, x, multiplyScalar, true); break; case 'dense': c = algorithm14(y, x, multiplyScalar, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, multiplyScalar, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, multiplyScalar, true).valueOf(); }, 'any, any': multiplyScalar, 'any, any, ...any': function (x, y, rest) { var result = multiply(x, y); for (var i = 0; i < rest.length; i++) { result = multiply(result, rest[i]); } return result; } }, multiplyScalar.signatures)); var _validateMatrixDimensions = function (size1, size2) { // check left operand dimensions switch (size1.length) { case 1: // check size2 switch (size2.length) { case 1: // Vector x Vector if (size1[0] !== size2[0]) { // throw error throw new RangeError('Dimension mismatch in multiplication. Vectors must have the same length'); } break; case 2: // Vector x Matrix if (size1[0] !== size2[0]) { // throw error throw new RangeError('Dimension mismatch in multiplication. Vector length (' + size1[0] + ') must match Matrix rows (' + size2[0] + ')'); } break; default: throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix B has ' + size2.length + ' dimensions)'); } break; case 2: // check size2 switch (size2.length) { case 1: // Matrix x Vector if (size1[1] !== size2[0]) { // throw error throw new RangeError('Dimension mismatch in multiplication. Matrix columns (' + size1[1] + ') must match Vector length (' + size2[0] + ')'); } break; case 2: // Matrix x Matrix if (size1[1] !== size2[0]) { // throw error throw new RangeError('Dimension mismatch in multiplication. Matrix A columns (' + size1[1] + ') must match Matrix B rows (' + size2[0] + ')'); } break; default: throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix B has ' + size2.length + ' dimensions)'); } break; default: throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix A has ' + size1.length + ' dimensions)'); } }; /** * C = A * B * * @param {Matrix} a Dense Vector (N) * @param {Matrix} b Dense Vector (N) * * @return {number} Scalar value */ var _multiplyVectorVector = function (a, b, n) { // check empty vector if (n === 0) throw new Error('Cannot multiply two empty vectors'); // a dense var adata = a._data; var adt = a._datatype; // b dense var bdata = b._data; var bdt = b._datatype; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); } // result (do not initialize it with zero) var c = mf(adata[0], bdata[0]); // loop data for (var i = 1; i < n; i++) { // multiply and accumulate c = af(c, mf(adata[i], bdata[i])); } return c; }; /** * C = A * B * * @param {Matrix} a Dense Vector (M) * @param {Matrix} b Matrix (MxN) * * @return {Matrix} Dense Vector (N) */ var _multiplyVectorMatrix = function (a, b) { // process storage switch (b.storage()) { case 'dense': return _multiplyVectorDenseMatrix(a, b); } throw new Error('Not implemented'); }; /** * C = A * B * * @param {Matrix} a Dense Vector (M) * @param {Matrix} b Dense Matrix (MxN) * * @return {Matrix} Dense Vector (N) */ var _multiplyVectorDenseMatrix = function (a, b) { // a dense var adata = a._data; var asize = a._size; var adt = a._datatype; // b dense var bdata = b._data; var bsize = b._size; var bdt = b._datatype; // rows & columns var alength = asize[0]; var bcolumns = bsize[1]; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); } // result var c = []; // loop matrix columns for (var j = 0; j < bcolumns; j++) { // sum (do not initialize it with zero) var sum = mf(adata[0], bdata[0][j]); // loop vector for (var i = 1; i < alength; i++) { // multiply & accumulate sum = af(sum, mf(adata[i], bdata[i][j])); } c[j] = sum; } // return matrix return new DenseMatrix({ data: c, size: [bcolumns], datatype: dt }); }; /** * C = A * B * * @param {Matrix} a Matrix (MxN) * @param {Matrix} b Dense Vector (N) * * @return {Matrix} Dense Vector (M) */ var _multiplyMatrixVector = function (a, b) { // process storage switch (a.storage()) { case 'dense': return _multiplyDenseMatrixVector(a, b); case 'sparse': return _multiplySparseMatrixVector(a, b); } }; /** * C = A * B * * @param {Matrix} a Matrix (MxN) * @param {Matrix} b Matrix (NxC) * * @return {Matrix} Matrix (MxC) */ var _multiplyMatrixMatrix = function (a, b) { // process storage switch (a.storage()) { case 'dense': // process storage switch (b.storage()) { case 'dense': return _multiplyDenseMatrixDenseMatrix(a, b); case 'sparse': return _multiplyDenseMatrixSparseMatrix(a, b); } break; case 'sparse': // process storage switch (b.storage()) { case 'dense': return _multiplySparseMatrixDenseMatrix(a, b); case 'sparse': return _multiplySparseMatrixSparseMatrix(a, b); } break; } }; /** * C = A * B * * @param {Matrix} a DenseMatrix (MxN) * @param {Matrix} b Dense Vector (N) * * @return {Matrix} Dense Vector (M) */ var _multiplyDenseMatrixVector = function (a, b) { // a dense var adata = a._data; var asize = a._size; var adt = a._datatype; // b dense var bdata = b._data; var bdt = b._datatype; // rows & columns var arows = asize[0]; var acolumns = asize[1]; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); } // result var c = []; // loop matrix a rows for (var i = 0; i < arows; i++) { // current row var row = adata[i]; // sum (do not initialize it with zero) var sum = mf(row[0], bdata[0]); // loop matrix a columns for (var j = 1; j < acolumns; j++) { // multiply & accumulate sum = af(sum, mf(row[j], bdata[j])); } c[i] = sum; } // return matrix return new DenseMatrix({ data: c, size: [arows], datatype: dt }); }; /** * C = A * B * * @param {Matrix} a DenseMatrix (MxN) * @param {Matrix} b DenseMatrix (NxC) * * @return {Matrix} DenseMatrix (MxC) */ var _multiplyDenseMatrixDenseMatrix = function (a, b) { // a dense var adata = a._data; var asize = a._size; var adt = a._datatype; // b dense var bdata = b._data; var bsize = b._size; var bdt = b._datatype; // rows & columns var arows = asize[0]; var acolumns = asize[1]; var bcolumns = bsize[1]; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); } // result var c = []; // loop matrix a rows for (var i = 0; i < arows; i++) { // current row var row = adata[i]; // initialize row array c[i] = []; // loop matrix b columns for (var j = 0; j < bcolumns; j++) { // sum (avoid initializing sum to zero) var sum = mf(row[0], bdata[0][j]); // loop matrix a columns for (var x = 1; x < acolumns; x++) { // multiply & accumulate sum = af(sum, mf(row[x], bdata[x][j])); } c[i][j] = sum; } } // return matrix return new DenseMatrix({ data: c, size: [arows, bcolumns], datatype: dt }); }; /** * C = A * B * * @param {Matrix} a DenseMatrix (MxN) * @param {Matrix} b SparseMatrix (NxC) * * @return {Matrix} SparseMatrix (MxC) */ var _multiplyDenseMatrixSparseMatrix = function (a, b) { // a dense var adata = a._data; var asize = a._size; var adt = a._datatype; // b sparse var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; var bsize = b._size; var bdt = b._datatype; // validate b matrix if (!bvalues) throw new Error('Cannot multiply Dense Matrix times Pattern only Matrix'); // rows & columns var arows = asize[0]; var bcolumns = bsize[1]; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // equalScalar signature to use var eq = equalScalar; // zero value var zero = 0; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); } // result var cvalues = []; var cindex = []; var cptr = []; // c matrix var c = new SparseMatrix({ values : cvalues, index: cindex, ptr: cptr, size: [arows, bcolumns], datatype: dt }); // loop b columns for (var jb = 0; jb < bcolumns; jb++) { // update ptr cptr[jb] = cindex.length; // indeces in column jb var kb0 = bptr[jb]; var kb1 = bptr[jb + 1]; // do not process column jb if no data exists if (kb1 > kb0) { // last row mark processed var last = 0; // loop a rows for (var i = 0; i < arows; i++) { // column mark var mark = i + 1; // C[i, jb] var cij; // values in b column j for (var kb = kb0; kb < kb1; kb++) { // row var ib = bindex[kb]; // check value has been initialized if (last !== mark) { // first value in column jb cij = mf(adata[i][ib], bvalues[kb]); // update mark last = mark; } else { // accumulate value cij = af(cij, mf(adata[i][ib], bvalues[kb])); } } // check column has been processed and value != 0 if (last === mark && !eq(cij, zero)) { // push row & value cindex.push(i); cvalues.push(cij); } } } } // update ptr cptr[bcolumns] = cindex.length; // return sparse matrix return c; }; /** * C = A * B * * @param {Matrix} a SparseMatrix (MxN) * @param {Matrix} b Dense Vector (N) * * @return {Matrix} SparseMatrix (M, 1) */ var _multiplySparseMatrixVector = function (a, b) { // a sparse var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var adt = a._datatype; // validate a matrix if (!avalues) throw new Error('Cannot multiply Pattern only Matrix times Dense Matrix'); // b dense var bdata = b._data; var bdt = b._datatype; // rows & columns var arows = a._size[0]; var brows = b._size[0]; // result var cvalues = []; var cindex = []; var cptr = []; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // equalScalar signature to use var eq = equalScalar; // zero value var zero = 0; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); } // workspace var x = []; // vector with marks indicating a value x[i] exists in a given column var w = []; // update ptr cptr[0] = 0; // rows in b for (var ib = 0; ib < brows; ib++) { // b[ib] var vbi = bdata[ib]; // check b[ib] != 0, avoid loops if (!eq(vbi, zero)) { // A values & index in ib column for (var ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { // a row var ia = aindex[ka]; // check value exists in current j if (!w[ia]) { // ia is new entry in j w[ia] = true; // add i to pattern of C cindex.push(ia); // x(ia) = A x[ia] = mf(vbi, avalues[ka]); } else { // i exists in C already x[ia] = af(x[ia], mf(vbi, avalues[ka])); } } } } // copy values from x to column jb of c for (var p1 = cindex.length, p = 0; p < p1; p++) { // row var ic = cindex[p]; // copy value cvalues[p] = x[ic]; } // update ptr cptr[1] = cindex.length; // return sparse matrix return new SparseMatrix({ values : cvalues, index: cindex, ptr: cptr, size: [arows, 1], datatype: dt }); }; /** * C = A * B * * @param {Matrix} a SparseMatrix (MxN) * @param {Matrix} b DenseMatrix (NxC) * * @return {Matrix} SparseMatrix (MxC) */ var _multiplySparseMatrixDenseMatrix = function (a, b) { // a sparse var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var adt = a._datatype; // validate a matrix if (!avalues) throw new Error('Cannot multiply Pattern only Matrix times Dense Matrix'); // b dense var bdata = b._data; var bdt = b._datatype; // rows & columns var arows = a._size[0]; var brows = b._size[0]; var bcolumns = b._size[1]; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // equalScalar signature to use var eq = equalScalar; // zero value var zero = 0; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); } // result var cvalues = []; var cindex = []; var cptr = []; // c matrix var c = new SparseMatrix({ values : cvalues, index: cindex, ptr: cptr, size: [arows, bcolumns], datatype: dt }); // workspace var x = []; // vector with marks indicating a value x[i] exists in a given column var w = []; // loop b columns for (var jb = 0; jb < bcolumns; jb++) { // update ptr cptr[jb] = cindex.length; // mark in workspace for current column var mark = jb + 1; // rows in jb for (var ib = 0; ib < brows; ib++) { // b[ib, jb] var vbij = bdata[ib][jb]; // check b[ib, jb] != 0, avoid loops if (!eq(vbij, zero)) { // A values & index in ib column for (var ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { // a row var ia = aindex[ka]; // check value exists in current j if (w[ia] !== mark) { // ia is new entry in j w[ia] = mark; // add i to pattern of C cindex.push(ia); // x(ia) = A x[ia] = mf(vbij, avalues[ka]); } else { // i exists in C already x[ia] = af(x[ia], mf(vbij, avalues[ka])); } } } } // copy values from x to column jb of c for (var p0 = cptr[jb], p1 = cindex.length, p = p0; p < p1; p++) { // row var ic = cindex[p]; // copy value cvalues[p] = x[ic]; } } // update ptr cptr[bcolumns] = cindex.length; // return sparse matrix return c; }; /** * C = A * B * * @param {Matrix} a SparseMatrix (MxN) * @param {Matrix} b SparseMatrix (NxC) * * @return {Matrix} SparseMatrix (MxC) */ var _multiplySparseMatrixSparseMatrix = function (a, b) { // a sparse var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var adt = a._datatype; // b sparse var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; var bdt = b._datatype; // rows & columns var arows = a._size[0]; var bcolumns = b._size[1]; // flag indicating both matrices (a & b) contain data var values = avalues && bvalues; // datatype var dt; // addScalar signature to use var af = addScalar; // multiplyScalar signature to use var mf = multiplyScalar; // process data types if (adt && bdt && adt === bdt && typeof adt === 'string') { // datatype dt = adt; // find signatures that matches (dt, dt) af = typed.find(addScalar, [dt, dt]); mf = typed.find(multiplyScalar, [dt, dt]); } // result var cvalues = values ? [] : undefined; var cindex = []; var cptr = []; // c matrix var c = new SparseMatrix({ values : cvalues, index: cindex, ptr: cptr, size: [arows, bcolumns], datatype: dt }); // workspace var x = values ? [] : undefined; // vector with marks indicating a value x[i] exists in a given column var w = []; // variables var ka, ka0, ka1, kb, kb0, kb1, ia, ib; // loop b columns for (var jb = 0; jb < bcolumns; jb++) { // update ptr cptr[jb] = cindex.length; // mark in workspace for current column var mark = jb + 1; // B values & index in j for (kb0 = bptr[jb], kb1 = bptr[jb + 1], kb = kb0; kb < kb1; kb++) { // b row ib = bindex[kb]; // check we need to process values if (values) { // loop values in a[:,ib] for (ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { // row ia = aindex[ka]; // check value exists in current j if (w[ia] !== mark) { // ia is new entry in j w[ia] = mark; // add i to pattern of C cindex.push(ia); // x(ia) = A x[ia] = mf(bvalues[kb], avalues[ka]); } else { // i exists in C already x[ia] = af(x[ia], mf(bvalues[kb], avalues[ka])); } } } else { // loop values in a[:,ib] for (ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { // row ia = aindex[ka]; // check value exists in current j if (w[ia] !== mark) { // ia is new entry in j w[ia] = mark; // add i to pattern of C cindex.push(ia); } } } } // check we need to process matrix values (pattern matrix) if (values) { // copy values from x to column jb of c for (var p0 = cptr[jb], p1 = cindex.length, p = p0; p < p1; p++) { // row var ic = cindex[p]; // copy value cvalues[p] = x[ic]; } } } // update ptr cptr[bcolumns] = cindex.length; // return sparse matrix return c; }; multiply.toTex = { 2: '\\left(${args[0]}' + latex.operators['multiply'] + '${args[1]}\\right)' }; return multiply; } exports.name = 'multiply'; exports.factory = factory; /***/ }), /* 11 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var keywords = __webpack_require__(61); var extend = __webpack_require__(4).extend; var deepEqual= __webpack_require__(4).deepEqual; function factory (type, config, load, typed, math) { /** * Node */ function Node() { if (!(this instanceof Node)) { throw new SyntaxError('Constructor must be called with the new operator'); } } /** * Evaluate the node * @param {Object} [scope] Scope to read/write variables * @return {*} Returns the result */ Node.prototype.eval = function(scope) { return this.compile().eval(scope); }; Node.prototype.type = 'Node'; Node.prototype.isNode = true; Node.prototype.comment = ''; /** * Compile the node to javascript code * @return {{eval: function}} expr Returns an object with a function 'eval', * which can be invoked as expr.eval([scope]), * where scope is an optional object with * variables. */ Node.prototype.compile = function () { // TODO: calling compile(math) is deprecated since version 2.0.0. Remove this warning some day if (arguments.length > 0) { throw new Error('Calling compile(math) is deprecated. Call the function as compile() instead.'); } // definitions globally available inside the closure of the compiled expressions var defs = { math: math.expression.transform, args: {}, // can be filled with names of FunctionAssignment arguments _validateScope: _validateScope }; // will be used to put local function arguments var args = {}; var code = this._compile(defs, args); var defsCode = Object.keys(defs).map(function (name) { return ' var ' + name + ' = defs["' + name + '"];'; }); var factoryCode = defsCode.join(' ') + 'return {' + ' "eval": function (scope) {' + ' if (scope) _validateScope(scope);' + ' scope = scope || {};' + ' return ' + code + ';' + ' }' + '};'; var factory = new Function('defs', factoryCode); return factory(defs); }; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * and constants globally available inside the closure * of the compiled expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ Node.prototype._compile = function (defs, args) { // must be implemented by each of the Node implementations throw new Error('Cannot compile a Node interface'); }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ Node.prototype.forEach = function (callback) { // must be implemented by each of the Node implementations throw new Error('Cannot run forEach on a Node interface'); }; /** * Create a new Node having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {OperatorNode} Returns a transformed copy of the node */ Node.prototype.map = function (callback) { // must be implemented by each of the Node implementations throw new Error('Cannot run map on a Node interface'); }; /** * Validate whether an object is a Node, for use with map * @param {Node} node * @returns {Node} Returns the input if it's a node, else throws an Error * @protected */ Node.prototype._ifNode = function (node) { if (!(node && node.isNode)) { throw new TypeError('Callback function must return a Node'); } return node; }; /** * Recursively traverse all nodes in a node tree. Executes given callback for * this node and each of its child nodes. * @param {function(node: Node, path: string, parent: Node)} callback * A callback called for every node in the node tree. */ Node.prototype.traverse = function (callback) { // execute callback for itself callback(this, null, null); // recursively traverse over all childs of a node function _traverse(node, callback) { node.forEach(function (child, path, parent) { callback(child, path, parent); _traverse(child, callback); }); } _traverse(this, callback); }; /** * Recursively transform a node tree via a transform function. * * For example, to replace all nodes of type SymbolNode having name 'x' with a * ConstantNode with value 2: * * var res = Node.transform(function (node, path, parent) { * if (node && node.isSymbolNode) && (node.name == 'x')) { * return new ConstantNode(2); * } * else { * return node; * } * }); * * @param {function(node: Node, path: string, parent: Node) : Node} callback * A mapping function accepting a node, and returning * a replacement for the node or the original node. * Signature: callback(node: Node, index: string, parent: Node) : Node * @return {Node} Returns the original node or its replacement */ Node.prototype.transform = function (callback) { // traverse over all childs function _transform (node, callback) { return node.map(function(child, path, parent) { var replacement = callback(child, path, parent); return _transform(replacement, callback); }); } var replacement = callback(this, null, null); return _transform(replacement, callback); }; /** * Find any node in the node tree matching given filter function. For example, to * find all nodes of type SymbolNode having name 'x': * * var results = Node.filter(function (node) { * return (node && node.isSymbolNode) && (node.name == 'x'); * }); * * @param {function(node: Node, path: string, parent: Node) : Node} callback * A test function returning true when a node matches, and false * otherwise. Function signature: * callback(node: Node, index: string, parent: Node) : boolean * @return {Node[]} nodes An array with nodes matching given filter criteria */ Node.prototype.filter = function (callback) { var nodes = []; this.traverse(function (node, path, parent) { if (callback(node, path, parent)) { nodes.push(node); } }); return nodes; }; // TODO: deprecated since version 1.1.0, remove this some day Node.prototype.find = function () { throw new Error('Function Node.find is deprecated. Use Node.filter instead.'); }; // TODO: deprecated since version 1.1.0, remove this some day Node.prototype.match = function () { throw new Error('Function Node.match is deprecated. See functions Node.filter, Node.transform, Node.traverse.'); }; /** * Create a shallow clone of this node * @return {Node} */ Node.prototype.clone = function () { // must be implemented by each of the Node implementations throw new Error('Cannot clone a Node interface'); }; /** * Create a deep clone of this node * @return {Node} */ Node.prototype.cloneDeep = function () { return this.map(function (node) { return node.cloneDeep(); }); }; /** * Deep compare this node with another node. * @param {Node} other * @return {boolean} Returns true when both nodes are of the same type and * contain the same values (as do their childs) */ Node.prototype.equals = function (other) { return other ? deepEqual(this, other) : false }; /** * Get string representation. (wrapper function) * * This function can get an object of the following form: * { * handler: //This can be a callback function of the form * // "function callback(node, options)"or * // a map that maps function names (used in FunctionNodes) * // to callbacks * parenthesis: "keep" //the parenthesis option (This is optional) * } * * @param {Object} [options] * @return {string} */ Node.prototype.toString = function (options) { var customString; if (options && typeof options == "object") { switch (typeof options.handler) { case 'object': case 'undefined': break; case 'function': customString = options.handler(this, options); break; default: throw new TypeError('Object or function expected as callback'); } } if (typeof customString !== 'undefined') { return customString; } return this._toString(options); }; /** * Internal function to generate the string output. * This has to be implemented by every Node * * @throws {Error} */ Node.prototype._toString = function () { //must be implemented by each of the Node implementations throw new Error('_toString not implemented for ' + this.type); }; /** * Get LaTeX representation. (wrapper function) * * This function can get an object of the following form: * { * handler: //This can be a callback function of the form * // "function callback(node, options)"or * // a map that maps function names (used in FunctionNodes) * // to callbacks * parenthesis: "keep" //the parenthesis option (This is optional) * } * * @param {Object} [options] * @return {string} */ Node.prototype.toTex = function (options) { var customTex; if (options && typeof options == 'object') { switch (typeof options.handler) { case 'object': case 'undefined': break; case 'function': customTex = options.handler(this, options); break; default: throw new TypeError('Object or function expected as callback'); } } if (typeof customTex !== 'undefined') { return customTex; } return this._toTex(options); }; /** * Internal function to generate the LaTeX output. * This has to be implemented by every Node * * @param {Object} [options] * @throws {Error} */ Node.prototype._toTex = function (options) { //must be implemented by each of the Node implementations throw new Error('_toTex not implemented for ' + this.type); }; /** * Get identifier. * @return {string} */ Node.prototype.getIdentifier = function () { return this.type; }; /** * Get the content of the current Node. * @return {Node} node **/ Node.prototype.getContent = function () { return this; }; /** * Validate the symbol names of a scope. * Throws an error when the scope contains an illegal symbol. * @param {Object} scope */ function _validateScope(scope) { for (var symbol in scope) { if (scope.hasOwnProperty(symbol)) { if (symbol in keywords) { throw new Error('Scope contains an illegal symbol, "' + symbol + '" is a reserved keyword'); } } } } return Node; } exports.name = 'Node'; exports.path = 'expression.node'; exports.math = true; // request access to the math namespace as 5th argument of the factory function exports.factory = factory; /***/ }), /* 12 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var DenseMatrix = type.DenseMatrix; /** * Iterates over SparseMatrix items and invokes the callback function f(Dij, Sij). * Callback function invoked M*N times. * * * ┌ f(Dij, Sij) ; S(i,j) !== 0 * C(i,j) = ┤ * └ f(Dij, 0) ; otherwise * * * @param {Matrix} denseMatrix The DenseMatrix instance (D) * @param {Matrix} sparseMatrix The SparseMatrix instance (C) * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) * * @return {Matrix} DenseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 */ var algorithm03 = function (denseMatrix, sparseMatrix, callback, inverse) { // dense matrix arrays var adata = denseMatrix._data; var asize = denseMatrix._size; var adt = denseMatrix._datatype; // sparse matrix arrays var bvalues = sparseMatrix._values; var bindex = sparseMatrix._index; var bptr = sparseMatrix._ptr; var bsize = sparseMatrix._size; var bdt = sparseMatrix._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // sparse matrix cannot be a Pattern matrix if (!bvalues) throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // result (DenseMatrix) var cdata = []; // initialize dense matrix for (var z = 0; z < rows; z++) { // initialize row cdata[z] = []; } // workspace var x = []; // marks indicating we have a value in x for a given column var w = []; // loop columns in b for (var j = 0; j < columns; j++) { // column mark var mark = j + 1; // values in column j for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { // row var i = bindex[k]; // update workspace x[i] = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); w[i] = mark; } // process workspace for (var y = 0; y < rows; y++) { // check we have a calculated value for current row if (w[y] === mark) { // use calculated value cdata[y][j] = x[y]; } else { // calculate value cdata[y][j] = inverse ? cf(zero, adata[y][j]) : cf(adata[y][j], zero); } } } // return dense matrix return new DenseMatrix({ data: cdata, size: [rows, columns], datatype: dt }); }; return algorithm03; } exports.name = 'algorithm03'; exports.factory = factory; /***/ }), /* 13 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var equalScalar = load(__webpack_require__(8)); var SparseMatrix = type.SparseMatrix; /** * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). * Callback function invoked NZ times (number of nonzero items in S). * * * ┌ f(Sij, b) ; S(i,j) !== 0 * C(i,j) = ┤ * └ 0 ; otherwise * * * @param {Matrix} s The SparseMatrix instance (S) * @param {Scalar} b The Scalar value * @param {Function} callback The f(Aij,b) operation to invoke * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) * * @return {Matrix} SparseMatrix (C) * * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 */ var algorithm11 = function (s, b, callback, inverse) { // sparse matrix arrays var avalues = s._values; var aindex = s._index; var aptr = s._ptr; var asize = s._size; var adt = s._datatype; // sparse matrix cannot be a Pattern matrix if (!avalues) throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // equal signature to use var eq = equalScalar; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string') { // datatype dt = adt; // find signature that matches (dt, dt) eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); // convert b to the same datatype b = typed.convert(b, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cvalues = []; var cindex = []; var cptr = []; // matrix var c = new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: dt }); // loop columns for (var j = 0; j < columns; j++) { // initialize ptr cptr[j] = cindex.length; // values in j for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row var i = aindex[k]; // invoke callback var v = inverse ? cf(b, avalues[k]) : cf(avalues[k], b); // check value is zero if (!eq(v, zero)) { // push index & value cindex.push(i); cvalues.push(v); } } } // update ptr cptr[columns] = cindex.length; // return sparse matrix return c; }; return algorithm11; } exports.name = 'algorithm11'; exports.factory = factory; /***/ }), /* 14 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var DenseMatrix = type.DenseMatrix; /** * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). * Callback function invoked MxN times. * * * ┌ f(Sij, b) ; S(i,j) !== 0 * C(i,j) = ┤ * └ f(0, b) ; otherwise * * * @param {Matrix} s The SparseMatrix instance (S) * @param {Scalar} b The Scalar value * @param {Function} callback The f(Aij,b) operation to invoke * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) * * @return {Matrix} DenseMatrix (C) * * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 */ var algorithm12 = function (s, b, callback, inverse) { // sparse matrix arrays var avalues = s._values; var aindex = s._index; var aptr = s._ptr; var asize = s._size; var adt = s._datatype; // sparse matrix cannot be a Pattern matrix if (!avalues) throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string') { // datatype dt = adt; // convert b to the same datatype b = typed.convert(b, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cdata = []; // matrix var c = new DenseMatrix({ data: cdata, size: [rows, columns], datatype: dt }); // workspaces var x = []; // marks indicating we have a value in x for a given column var w = []; // loop columns for (var j = 0; j < columns; j++) { // columns mark var mark = j + 1; // values in j for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row var r = aindex[k]; // update workspace x[r] = avalues[k]; w[r] = mark; } // loop rows for (var i = 0; i < rows; i++) { // initialize C on first column if (j === 0) { // create row array cdata[i] = []; } // check sparse matrix has a value @ i,j if (w[i] === mark) { // invoke callback, update C cdata[i][j] = inverse ? cf(b, x[i]) : cf(x[i], b); } else { // dense matrix value @ i, j cdata[i][j] = inverse ? cf(b, 0) : cf(0, b); } } } // return sparse matrix return c; }; return algorithm12; } exports.name = 'algorithm12'; exports.factory = factory; /***/ }), /* 15 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var extend = __webpack_require__(4).extend; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var addScalar = load(__webpack_require__(17)); var latex = __webpack_require__(3); var algorithm01 = load(__webpack_require__(27)); var algorithm04 = load(__webpack_require__(73)); var algorithm10 = load(__webpack_require__(30)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Add two or more values, `x + y`. * For matrices, the function is evaluated element wise. * * Syntax: * * math.add(x, y) * math.add(x, y, z, ...) * * Examples: * * math.add(2, 3); // returns number 5 * math.add(2, 3, 4); // returns number 9 * * var a = math.complex(2, 3); * var b = math.complex(-4, 1); * math.add(a, b); // returns Complex -2 + 4i * * math.add([1, 2, 3], 4); // returns Array [5, 6, 7] * * var c = math.unit('5 cm'); * var d = math.unit('2.1 mm'); * math.add(c, d); // returns Unit 52.1 mm * * math.add("2.3", "4"); // returns number 6.3 * * See also: * * subtract, sum * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to add * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to add * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Sum of `x` and `y` */ var add = typed('add', extend({ // we extend the signatures of addScalar with signatures dealing with matrices 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm04(x, y, addScalar); break; default: // sparse + dense c = algorithm01(y, x, addScalar, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm01(x, y, addScalar, false); break; default: // dense + dense c = algorithm13(x, y, addScalar); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return add(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return add(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return add(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm10(x, y, addScalar, false); break; default: c = algorithm14(x, y, addScalar, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm10(y, x, addScalar, true); break; default: c = algorithm14(y, x, addScalar, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, addScalar, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, addScalar, true).valueOf(); }, 'any, any': addScalar, 'any, any, ...any': function (x, y, rest) { var result = add(x, y); for (var i = 0; i < rest.length; i++) { result = add(result, rest[i]); } return result; } }, addScalar.signatures)); add.toTex = { 2: '\\left(${args[0]}' + latex.operators['add'] + '${args[1]}\\right)' }; return add; } exports.name = 'add'; exports.factory = factory; /***/ }), /* 16 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory(type, config, load, typed) { var multiplyScalar = load(__webpack_require__(20)); /** * Divide two scalar values, `x / y`. * This function is meant for internal use: it is used by the public functions * `divide` and `inv`. * * This function does not support collections (Array or Matrix), and does * not validate the number of of inputs. * * @param {number | BigNumber | Fraction | Complex | Unit} x Numerator * @param {number | BigNumber | Fraction | Complex} y Denominator * @return {number | BigNumber | Fraction | Complex | Unit} Quotient, `x / y` * @private */ var divideScalar = typed('divide', { 'number, number': function (x, y) { return x / y; }, 'Complex, Complex': function (x, y) { return x.div(y); }, 'BigNumber, BigNumber': function (x, y) { return x.div(y); }, 'Fraction, Fraction': function (x, y) { return x.div(y); }, 'Unit, number | Fraction | BigNumber': function (x, y) { var res = x.clone(); // TODO: move the divide function to Unit.js, it uses internals of Unit res.value = divideScalar(((res.value === null) ? res._normalize(1) : res.value), y); return res; }, 'number | Fraction | BigNumber, Unit': function (x, y) { var res = y.pow(-1); // TODO: move the divide function to Unit.js, it uses internals of Unit res.value = multiplyScalar(((res.value === null) ? res._normalize(1) : res.value), x); return res; }, 'Unit, Unit': function (x, y) { return x.divide(y); } }); return divideScalar; } exports.factory = factory; /***/ }), /* 17 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory(type, config, load, typed) { /** * Add two scalar values, `x + y`. * This function is meant for internal use: it is used by the public function * `add` * * This function does not support collections (Array or Matrix), and does * not validate the number of of inputs. * * @param {number | BigNumber | Fraction | Complex | Unit} x First value to add * @param {number | BigNumber | Fraction | Complex} y Second value to add * @return {number | BigNumber | Fraction | Complex | Unit} Sum of `x` and `y` * @private */ var add = typed('add', { 'number, number': function (x, y) { return x + y; }, 'Complex, Complex': function (x, y) { return x.add(y); }, 'BigNumber, BigNumber': function (x, y) { return x.plus(y); }, 'Fraction, Fraction': function (x, y) { return x.add(y); }, 'Unit, Unit': function (x, y) { if (x.value == null) throw new Error('Parameter x contains a unit with undefined value'); if (y.value == null) throw new Error('Parameter y contains a unit with undefined value'); if (!x.equalBase(y)) throw new Error('Units do not match'); var res = x.clone(); res.value = add(res.value, y.value); res.fixPrefix = false; return res; } }); return add; } exports.factory = factory; /***/ }), /* 18 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var addScalar = load(__webpack_require__(17)); var unaryMinus = load(__webpack_require__(36)); var algorithm01 = load(__webpack_require__(27)); var algorithm03 = load(__webpack_require__(12)); var algorithm05 = load(__webpack_require__(56)); var algorithm10 = load(__webpack_require__(30)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); // TODO: split function subtract in two: subtract and subtractScalar /** * Subtract two values, `x - y`. * For matrices, the function is evaluated element wise. * * Syntax: * * math.subtract(x, y) * * Examples: * * math.subtract(5.3, 2); // returns number 3.3 * * var a = math.complex(2, 3); * var b = math.complex(4, 1); * math.subtract(a, b); // returns Complex -2 + 2i * * math.subtract([5, 7, 4], 4); // returns Array [1, 3, 0] * * var c = math.unit('2.1 km'); * var d = math.unit('500m'); * math.subtract(c, d); // returns Unit 1.6 km * * See also: * * add * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x * Initial value * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y * Value to subtract from `x` * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} * Subtraction of `x` and `y` */ var subtract = typed('subtract', { 'number, number': function (x, y) { return x - y; }, 'Complex, Complex': function (x, y) { return x.sub(y); }, 'BigNumber, BigNumber': function (x, y) { return x.minus(y); }, 'Fraction, Fraction': function (x, y) { return x.sub(y); }, 'Unit, Unit': function (x, y) { if (x.value == null) { throw new Error('Parameter x contains a unit with undefined value'); } if (y.value == null) { throw new Error('Parameter y contains a unit with undefined value'); } if (!x.equalBase(y)) { throw new Error('Units do not match'); } var res = x.clone(); res.value = subtract(res.value, y.value); res.fixPrefix = false; return res; }, 'Matrix, Matrix': function (x, y) { // matrix sizes var xsize = x.size(); var ysize = y.size(); // check dimensions if (xsize.length !== ysize.length) throw new DimensionError(xsize.length, ysize.length); // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse - sparse c = algorithm05(x, y, subtract); break; default: // sparse - dense c = algorithm03(y, x, subtract, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense - sparse c = algorithm01(x, y, subtract, false); break; default: // dense - dense c = algorithm13(x, y, subtract); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return subtract(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return subtract(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return subtract(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': // algorithm 7 is faster than 9 since it calls f() for nonzero items only! c = algorithm10(x, unaryMinus(y), addScalar); break; default: c = algorithm14(x, y, subtract); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm10(y, x, subtract, true); break; default: c = algorithm14(y, x, subtract, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, subtract, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, subtract, true).valueOf(); } }); subtract.toTex = { 2: '\\left(${args[0]}' + latex.operators['subtract'] + '${args[1]}\\right)' }; return subtract; } exports.name = 'subtract'; exports.factory = factory; /***/ }), /* 19 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var formatNumber = __webpack_require__(2).format; var formatBigNumber = __webpack_require__(498).format; /** * Test whether value is a string * @param {*} value * @return {boolean} isString */ exports.isString = function(value) { return typeof value === 'string'; }; /** * Check if a text ends with a certain string. * @param {string} text * @param {string} search */ exports.endsWith = function(text, search) { var start = text.length - search.length; var end = text.length; return (text.substring(start, end) === search); }; /** * Format a value of any type into a string. * * Usage: * math.format(value) * math.format(value, precision) * * When value is a function: * * - When the function has a property `syntax`, it returns this * syntax description. * - In other cases, a string `'function'` is returned. * * When `value` is an Object: * * - When the object contains a property `format` being a function, this * function is invoked as `value.format(options)` and the result is returned. * - When the object has its own `toString` method, this method is invoked * and the result is returned. * - In other cases the function will loop over all object properties and * return JSON object notation like '{"a": 2, "b": 3}'. * * Example usage: * math.format(2/7); // '0.2857142857142857' * math.format(math.pi, 3); // '3.14' * math.format(new Complex(2, 3)); // '2 + 3i' * math.format('hello'); // '"hello"' * * @param {*} value Value to be stringified * @param {Object | number | Function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @return {string} str */ exports.format = function(value, options) { if (typeof value === 'number') { return formatNumber(value, options); } if (value && value.isBigNumber === true) { return formatBigNumber(value, options); } if (value && value.isFraction === true) { if (!options || options.fraction !== 'decimal') { // output as ratio, like '1/3' return (value.s * value.n) + '/' + value.d; } else { // output as decimal, like '0.(3)' return value.toString(); } } if (Array.isArray(value)) { return formatArray(value, options); } if (exports.isString(value)) { return '"' + value + '"'; } if (typeof value === 'function') { return value.syntax ? String(value.syntax) : 'function'; } if (value && typeof value === 'object') { if (typeof value.format === 'function') { return value.format(options); } else if (value && value.toString() !== {}.toString()) { // this object has a non-native toString method, use that one return value.toString(); } else { var entries = []; for (var key in value) { if (value.hasOwnProperty(key)) { entries.push('"' + key + '": ' + exports.format(value[key], options)); } } return '{' + entries.join(', ') + '}'; } } return String(value); }; /** * Recursively format an n-dimensional matrix * Example output: "[[1, 2], [3, 4]]" * @param {Array} array * @param {Object | number | Function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @returns {string} str */ function formatArray (array, options) { if (Array.isArray(array)) { var str = '['; var len = array.length; for (var i = 0; i < len; i++) { if (i != 0) { str += ', '; } str += formatArray(array[i], options); } str += ']'; return str; } else { return exports.format(array, options); } } /***/ }), /* 20 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory(type, config, load, typed) { /** * Multiply two scalar values, `x * y`. * This function is meant for internal use: it is used by the public function * `multiply` * * This function does not support collections (Array or Matrix), and does * not validate the number of of inputs. * * @param {number | BigNumber | Fraction | Complex | Unit} x First value to multiply * @param {number | BigNumber | Fraction | Complex} y Second value to multiply * @return {number | BigNumber | Fraction | Complex | Unit} Multiplication of `x` and `y` * @private */ var multiplyScalar = typed('multiplyScalar', { 'number, number': function (x, y) { return x * y; }, 'Complex, Complex': function (x, y) { return x.mul(y); }, 'BigNumber, BigNumber': function (x, y) { return x.times(y); }, 'Fraction, Fraction': function (x, y) { return x.mul(y); }, 'number | Fraction | BigNumber | Complex, Unit': function (x, y) { var res = y.clone(); res.value = (res.value === null) ? res._normalize(x) : multiplyScalar(res.value, x); return res; }, 'Unit, number | Fraction | BigNumber | Complex': function (x, y) { var res = x.clone(); res.value = (res.value === null) ? res._normalize(y) : multiplyScalar(res.value, y); return res; }, 'Unit, Unit': function (x, y) { return x.multiply(y); } }); return multiplyScalar; } exports.factory = factory; /***/ }), /* 21 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var equalScalar = load(__webpack_require__(8)); var SparseMatrix = type.SparseMatrix; /** * Iterates over SparseMatrix nonzero items and invokes the callback function f(Dij, Sij). * Callback function invoked NNZ times (number of nonzero items in SparseMatrix). * * * ┌ f(Dij, Sij) ; S(i,j) !== 0 * C(i,j) = ┤ * └ 0 ; otherwise * * * @param {Matrix} denseMatrix The DenseMatrix instance (D) * @param {Matrix} sparseMatrix The SparseMatrix instance (S) * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) * * @return {Matrix} SparseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 */ var algorithm02 = function (denseMatrix, sparseMatrix, callback, inverse) { // dense matrix arrays var adata = denseMatrix._data; var asize = denseMatrix._size; var adt = denseMatrix._datatype; // sparse matrix arrays var bvalues = sparseMatrix._values; var bindex = sparseMatrix._index; var bptr = sparseMatrix._ptr; var bsize = sparseMatrix._size; var bdt = sparseMatrix._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // sparse matrix cannot be a Pattern matrix if (!bvalues) throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // equal signature to use var eq = equalScalar; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // find signature that matches (dt, dt) eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // result (SparseMatrix) var cvalues = []; var cindex = []; var cptr = []; // loop columns in b for (var j = 0; j < columns; j++) { // update cptr cptr[j] = cindex.length; // values in column j for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { // row var i = bindex[k]; // update C(i,j) var cij = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); // check for nonzero if (!eq(cij, zero)) { // push i & v cindex.push(i); cvalues.push(cij); } } } // update cptr cptr[columns] = cindex.length; // return sparse matrix return new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: dt }); }; return algorithm02; } exports.name = 'algorithm02'; exports.factory = factory; /***/ }), /* 22 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var DenseMatrix = type.DenseMatrix; /** * Iterates over SparseMatrix A and SparseMatrix B items (zero and nonzero) and invokes the callback function f(Aij, Bij). * Callback function invoked MxN times. * * C(i,j) = f(Aij, Bij) * * @param {Matrix} a The SparseMatrix instance (A) * @param {Matrix} b The SparseMatrix instance (B) * @param {Function} callback The f(Aij,Bij) operation to invoke * * @return {Matrix} DenseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 */ var algorithm07 = function (a, b, callback) { // sparse matrix arrays var asize = a._size; var adt = a._datatype; // sparse matrix arrays var bsize = b._size; var bdt = b._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // vars var i, j; // result arrays var cdata = []; // initialize c for (i = 0; i < rows; i++) cdata[i] = []; // matrix var c = new DenseMatrix({ data: cdata, size: [rows, columns], datatype: dt }); // workspaces var xa = []; var xb = []; // marks indicating we have a value in x for a given column var wa = []; var wb = []; // loop columns for (j = 0; j < columns; j++) { // columns mark var mark = j + 1; // scatter the values of A(:,j) into workspace _scatter(a, j, wa, xa, mark); // scatter the values of B(:,j) into workspace _scatter(b, j, wb, xb, mark); // loop rows for (i = 0; i < rows; i++) { // matrix values @ i,j var va = wa[i] === mark ? xa[i] : zero; var vb = wb[i] === mark ? xb[i] : zero; // invoke callback cdata[i][j] = cf(va, vb); } } // return sparse matrix return c; }; var _scatter = function (m, j, w, x, mark) { // a arrays var values = m._values; var index = m._index; var ptr = m._ptr; // loop values in column j for (var k = ptr[j], k1 = ptr[j + 1]; k < k1; k++) { // row var i = index[k]; // update workspace w[i] = mark; x[i] = values[k]; } }; return algorithm07; } exports.name = 'algorithm07'; exports.factory = factory; /***/ }), /* 23 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; exports.array = __webpack_require__(7); exports['boolean'] = __webpack_require__(501); exports['function'] = __webpack_require__(33); exports.number = __webpack_require__(2); exports.object = __webpack_require__(4); exports.string = __webpack_require__(19); exports.types = __webpack_require__(60); exports.emitter = __webpack_require__(132); /***/ }), /* 24 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var latex = __webpack_require__(3); function factory (type, config, load, typed, math) { var Node = load(__webpack_require__(11)); var Unit = load(__webpack_require__(130)); /** * @constructor SymbolNode * @extends {Node} * A symbol node can hold and resolve a symbol * @param {string} name * @extends {Node} */ function SymbolNode(name) { if (!(this instanceof SymbolNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } // validate input if (typeof name !== 'string') throw new TypeError('String expected for parameter "name"'); this.name = name; } SymbolNode.prototype = new Node(); SymbolNode.prototype.type = 'SymbolNode'; SymbolNode.prototype.isSymbolNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ SymbolNode.prototype._compile = function (defs, args) { // add a function to the definitions defs['undef'] = undef; defs['Unit'] = Unit; if (args[this.name]) { // this is a FunctionAssignment argument // (like an x when inside the expression of a function assignment `f(x) = ...`) return this.name; } else if (this.name in defs.math) { return '("' + this.name + '" in scope ? scope["' + this.name + '"] : math["' + this.name + '"])'; } else { return '(' + '"' + this.name + '" in scope ? scope["' + this.name + '"] : ' + (Unit.isValuelessUnit(this.name) ? 'new Unit(null, "' + this.name + '")' : 'undef("' + this.name + '")') + ')'; } }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ SymbolNode.prototype.forEach = function (callback) { // nothing to do, we don't have childs }; /** * Create a new SymbolNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node) : Node} callback * @returns {SymbolNode} Returns a clone of the node */ SymbolNode.prototype.map = function (callback) { return this.clone(); }; /** * Throws an error 'Undefined symbol {name}' * @param {string} name */ function undef (name) { throw new Error('Undefined symbol ' + name); } /** * Create a clone of this node, a shallow copy * @return {SymbolNode} */ SymbolNode.prototype.clone = function() { return new SymbolNode(this.name); }; /** * Get string representation * @param {Object} options * @return {string} str * @override */ SymbolNode.prototype._toString = function(options) { return this.name; }; /** * Get LaTeX representation * @param {Object} options * @return {string} str * @override */ SymbolNode.prototype._toTex = function(options) { var isUnit = false; if ((typeof math[this.name] === 'undefined') && Unit.isValuelessUnit(this.name)) { isUnit = true; } var symbol = latex.toSymbol(this.name, isUnit); if (symbol[0] === '\\') { //no space needed if the symbol starts with '\' return symbol; } //the space prevents symbols from breaking stuff like '\cdot' if it's written right before the symbol return ' ' + symbol; }; return SymbolNode; } exports.name = 'SymbolNode'; exports.path = 'expression.node'; exports.math = true; // request access to the math namespace as 5th argument of the factory function exports.factory = factory; /***/ }), /* 25 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the absolute value of a number. For matrices, the function is * evaluated element wise. * * Syntax: * * math.abs(x) * * Examples: * * math.abs(3.5); // returns number 3.5 * math.abs(-4.2); // returns number 4.2 * * math.abs([3, -5, -1, 0, 2]); // returns Array [3, 5, 1, 0, 2] * * See also: * * sign * * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x * A number or matrix for which to get the absolute value * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} * Absolute value of `x` */ var abs = typed('abs', { 'number': Math.abs, 'Complex': function (x) { return x.abs(); }, 'BigNumber': function (x) { return x.abs(); }, 'Fraction': function (x) { return x.abs(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since abs(0) = 0 return deepMap(x, abs, true); }, 'Unit': function(x) { return x.abs(); } }); abs.toTex = {1: '\\left|${args[0]}\\right|'}; return abs; } exports.name = 'abs'; exports.factory = factory; /***/ }), /* 26 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var nearlyEqual = __webpack_require__(2).nearlyEqual; var bigNearlyEqual = __webpack_require__(31); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); var latex = __webpack_require__(3); /** * Test whether value x is larger than y. * * The function returns true when x is larger than y and the relative * difference between x and y is larger than the configured epsilon. The * function cannot be used to compare values smaller than approximately 2.22e-16. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.larger(x, y) * * Examples: * * math.larger(2, 3); // returns false * math.larger(5, 2 + 2); // returns true * * var a = math.unit('5 cm'); * var b = math.unit('2 inch'); * math.larger(a, b); // returns false * * See also: * * equal, unequal, smaller, smallerEq, largerEq, compare * * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare * @return {boolean | Array | Matrix} Returns true when the x is larger than y, else returns false */ var larger = typed('larger', { 'boolean, boolean': function (x, y) { return x > y; }, 'number, number': function (x, y) { return x > y && !nearlyEqual(x, y, config.epsilon); }, 'BigNumber, BigNumber': function (x, y) { return x.gt(y) && !bigNearlyEqual(x, y, config.epsilon); }, 'Fraction, Fraction': function (x, y) { return x.compare(y) === 1; }, 'Complex, Complex': function () { throw new TypeError('No ordering relation is defined for complex numbers'); }, 'Unit, Unit': function (x, y) { if (!x.equalBase(y)) { throw new Error('Cannot compare units with different base'); } return larger(x.value, y.value); }, 'string, string': function (x, y) { return x > y; }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, larger); break; default: // sparse + dense c = algorithm03(y, x, larger, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, larger, false); break; default: // dense + dense c = algorithm13(x, y, larger); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return larger(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return larger(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return larger(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, larger, false); break; default: c = algorithm14(x, y, larger, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, larger, true); break; default: c = algorithm14(y, x, larger, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, larger, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, larger, true).valueOf(); } }); larger.toTex = { 2: '\\left(${args[0]}' + latex.operators['larger'] + '${args[1]}\\right)' }; return larger; } exports.name = 'larger'; exports.factory = factory; /***/ }), /* 27 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var DenseMatrix = type.DenseMatrix; /** * Iterates over SparseMatrix nonzero items and invokes the callback function f(Dij, Sij). * Callback function invoked NNZ times (number of nonzero items in SparseMatrix). * * * ┌ f(Dij, Sij) ; S(i,j) !== 0 * C(i,j) = ┤ * └ Dij ; otherwise * * * @param {Matrix} denseMatrix The DenseMatrix instance (D) * @param {Matrix} sparseMatrix The SparseMatrix instance (S) * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) * * @return {Matrix} DenseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 */ var algorithm01 = function (denseMatrix, sparseMatrix, callback, inverse) { // dense matrix arrays var adata = denseMatrix._data; var asize = denseMatrix._size; var adt = denseMatrix._datatype; // sparse matrix arrays var bvalues = sparseMatrix._values; var bindex = sparseMatrix._index; var bptr = sparseMatrix._ptr; var bsize = sparseMatrix._size; var bdt = sparseMatrix._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // sparse matrix cannot be a Pattern matrix if (!bvalues) throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); // rows & columns var rows = asize[0]; var columns = asize[1]; // process data types var dt = typeof adt === 'string' && adt === bdt ? adt : undefined; // callback function var cf = dt ? typed.find(callback, [dt, dt]) : callback; // vars var i, j; // result (DenseMatrix) var cdata = []; // initialize c for (i = 0; i < rows; i++) cdata[i] = []; // workspace var x = []; // marks indicating we have a value in x for a given column var w = []; // loop columns in b for (j = 0; j < columns; j++) { // column mark var mark = j + 1; // values in column j for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { // row i = bindex[k]; // update workspace x[i] = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); // mark i as updated w[i] = mark; } // loop rows for (i = 0; i < rows; i++) { // check row is in workspace if (w[i] === mark) { // c[i][j] was already calculated cdata[i][j] = x[i]; } else { // item does not exist in S cdata[i][j] = adata[i][j]; } } } // return dense matrix return new DenseMatrix({ data: cdata, size: [rows, columns], datatype: dt }); }; return algorithm01; } exports.name = 'algorithm01'; exports.factory = factory; /***/ }), /* 28 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var ArgumentsError = __webpack_require__(41); var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var AccessorNode = load(__webpack_require__(83)); var ArrayNode = load(__webpack_require__(62)); var AssignmentNode = load(__webpack_require__(84)); var BlockNode = load(__webpack_require__(85)); var ConditionalNode = load(__webpack_require__(86)); var ConstantNode = load(__webpack_require__(43)); var FunctionAssignmentNode = load(__webpack_require__(87)); var IndexNode = load(__webpack_require__(88)); var ObjectNode = load(__webpack_require__(89)); var OperatorNode = load(__webpack_require__(48)); var ParenthesisNode = load(__webpack_require__(49)); var FunctionNode = load(__webpack_require__(44)); var RangeNode = load(__webpack_require__(63)); var SymbolNode = load(__webpack_require__(24)); /** * Parse an expression. Returns a node tree, which can be evaluated by * invoking node.eval(); * * Syntax: * * parse(expr) * parse(expr, options) * parse([expr1, expr2, expr3, ...]) * parse([expr1, expr2, expr3, ...], options) * * Example: * * var node = parse('sqrt(3^2 + 4^2)'); * node.compile(math).eval(); // 5 * * var scope = {a:3, b:4} * var node = parse('a * b'); // 12 * var code = node.compile(math); * code.eval(scope); // 12 * scope.a = 5; * code.eval(scope); // 20 * * var nodes = math.parse(['a = 3', 'b = 4', 'a * b']); * nodes[2].compile(math).eval(); // 12 * * @param {string | string[] | Matrix} expr * @param {{nodes: Object}} [options] Available options: * - `nodes` a set of custom nodes * @return {Node | Node[]} node * @throws {Error} */ function parse (expr, options) { if (arguments.length != 1 && arguments.length != 2) { throw new ArgumentsError('parse', arguments.length, 1, 2); } // pass extra nodes extra_nodes = (options && options.nodes) ? options.nodes : {}; if (typeof expr === 'string') { // parse a single expression expression = expr; return parseStart(); } else if (Array.isArray(expr) || expr instanceof type.Matrix) { // parse an array or matrix with expressions return deepMap(expr, function (elem) { if (typeof elem !== 'string') throw new TypeError('String expected'); expression = elem; return parseStart(); }); } else { // oops throw new TypeError('String or matrix expected'); } } // token types enumeration var TOKENTYPE = { NULL : 0, DELIMITER : 1, NUMBER : 2, SYMBOL : 3, UNKNOWN : 4 }; // map with all delimiters var DELIMITERS = { ',': true, '(': true, ')': true, '[': true, ']': true, '{': true, '}': true, '\"': true, ';': true, '+': true, '-': true, '*': true, '.*': true, '/': true, './': true, '%': true, '^': true, '.^': true, '~': true, '!': true, '&': true, '|': true, '^|': true, '\'': true, '=': true, ':': true, '?': true, '==': true, '!=': true, '<': true, '>': true, '<=': true, '>=': true, '<<': true, '>>': true, '>>>': true }; // map with all named delimiters var NAMED_DELIMITERS = { 'mod': true, 'to': true, 'in': true, 'and': true, 'xor': true, 'or': true, 'not': true }; var extra_nodes = {}; // current extra nodes var expression = ''; // current expression var comment = ''; // last parsed comment var index = 0; // current index in expr var c = ''; // current token character in expr var token = ''; // current token var token_type = TOKENTYPE.NULL; // type of the token var nesting_level = 0; // level of nesting inside parameters, used to ignore newline characters var conditional_level = null; // when a conditional is being parsed, the level of the conditional is stored here /** * Get the first character from the expression. * The character is stored into the char c. If the end of the expression is * reached, the function puts an empty string in c. * @private */ function first() { index = 0; c = expression.charAt(0); nesting_level = 0; conditional_level = null; } /** * Get the next character from the expression. * The character is stored into the char c. If the end of the expression is * reached, the function puts an empty string in c. * @private */ function next() { index++; c = expression.charAt(index); } /** * Preview the previous character from the expression. * @return {string} cNext * @private */ function prevPreview() { return expression.charAt(index - 1); } /** * Preview the next character from the expression. * @return {string} cNext * @private */ function nextPreview() { return expression.charAt(index + 1); } /** * Preview the second next character from the expression. * @return {string} cNext * @private */ function nextNextPreview() { return expression.charAt(index + 2); } /** * Get next token in the current string expr. * The token and token type are available as token and token_type * @private */ function getToken() { token_type = TOKENTYPE.NULL; token = ''; comment = ''; // skip over whitespaces // space, tab, and newline when inside parameters while (parse.isWhitespace(c, nesting_level)) { next(); } // skip comment if (c == '#') { while (c != '\n' && c != '') { comment += c; next(); } } // check for end of expression if (c == '') { // token is still empty token_type = TOKENTYPE.DELIMITER; return; } // check for new line character if (c == '\n' && !nesting_level) { token_type = TOKENTYPE.DELIMITER; token = c; next(); return; } // check for delimiters consisting of 3 characters var c2 = c + nextPreview(); var c3 = c2 + nextNextPreview(); if (c3.length == 3 && DELIMITERS[c3]) { token_type = TOKENTYPE.DELIMITER; token = c3; next(); next(); next(); return; } // check for delimiters consisting of 2 characters if (c2.length == 2 && DELIMITERS[c2]) { token_type = TOKENTYPE.DELIMITER; token = c2; next(); next(); return; } // check for delimiters consisting of 1 character if (DELIMITERS[c]) { token_type = TOKENTYPE.DELIMITER; token = c; next(); return; } // check for a number if (parse.isDigitDot(c)) { token_type = TOKENTYPE.NUMBER; // get number, can have a single dot if (c == '.') { token += c; next(); if (!parse.isDigit(c)) { // this is no number, it is just a dot (can be dot notation) token_type = TOKENTYPE.DELIMITER; } } else { while (parse.isDigit(c)) { token += c; next(); } if (parse.isDecimalMark(c, nextPreview())) { token += c; next(); } } while (parse.isDigit(c)) { token += c; next(); } // check for exponential notation like "2.3e-4", "1.23e50" or "2e+4" c2 = nextPreview(); if (c == 'E' || c == 'e') { if (parse.isDigit(c2) || c2 == '-' || c2 == '+') { token += c; next(); if (c == '+' || c == '-') { token += c; next(); } // Scientific notation MUST be followed by an exponent if (!parse.isDigit(c)) { throw createSyntaxError('Digit expected, got "' + c + '"'); } while (parse.isDigit(c)) { token += c; next(); } if (parse.isDecimalMark(c, nextPreview())) { throw createSyntaxError('Digit expected, got "' + c + '"'); } } else if (c2 == '.') { next(); throw createSyntaxError('Digit expected, got "' + c + '"'); } } return; } // check for variables, functions, named operators if (parse.isAlpha(c, prevPreview(), nextPreview())) { while (parse.isAlpha(c, prevPreview(), nextPreview()) || parse.isDigit(c)) { token += c; next(); } if (NAMED_DELIMITERS.hasOwnProperty(token)) { token_type = TOKENTYPE.DELIMITER; } else { token_type = TOKENTYPE.SYMBOL; } return; } // something unknown is found, wrong characters -> a syntax error token_type = TOKENTYPE.UNKNOWN; while (c != '') { token += c; next(); } throw createSyntaxError('Syntax error in part "' + token + '"'); } /** * Get next token and skip newline tokens */ function getTokenSkipNewline () { do { getToken(); } while (token == '\n'); } /** * Open parameters. * New line characters will be ignored until closeParams() is called */ function openParams() { nesting_level++; } /** * Close parameters. * New line characters will no longer be ignored */ function closeParams() { nesting_level--; } /** * Checks whether the current character `c` is a valid alpha character: * * - A latin letter (upper or lower case) Ascii: a-z, A-Z * - An underscore Ascii: _ * - A latin letter with accents Unicode: \u00C0 - \u02AF * - A greek letter Unicode: \u0370 - \u03FF * - A mathematical alphanumeric symbol Unicode: \u{1D400} - \u{1D7FF} excluding invalid code points * * The previous and next characters are needed to determine whether * this character is part of a unicode surrogate pair. * * @param {string} c Current character in the expression * @param {string} cPrev Previous character * @param {string} cNext Next character * @return {boolean} */ parse.isAlpha = function isAlpha (c, cPrev, cNext) { return parse.isValidLatinOrGreek(c) || parse.isValidMathSymbol(c, cNext) || parse.isValidMathSymbol(cPrev, c); }; /** * Test whether a character is a valid latin, greek, or letter-like character * @param {string} c * @return {boolean} */ parse.isValidLatinOrGreek = function isValidLatinOrGreek (c) { return /^[a-zA-Z_\u00C0-\u02AF\u0370-\u03FF\u2100-\u214F]$/.test(c); }; /** * Test whether two given 16 bit characters form a surrogate pair of a * unicode math symbol. * * http://unicode-table.com/en/ * http://www.wikiwand.com/en/Mathematical_operators_and_symbols_in_Unicode * * Note: In ES6 will be unicode aware: * http://stackoverflow.com/questions/280712/javascript-unicode-regexes * https://mathiasbynens.be/notes/es6-unicode-regex * * @param {string} high * @param {string} low * @return {boolean} */ parse.isValidMathSymbol = function isValidMathSymbol (high, low) { return /^[\uD835]$/.test(high) && /^[\uDC00-\uDFFF]$/.test(low) && /^[^\uDC55\uDC9D\uDCA0\uDCA1\uDCA3\uDCA4\uDCA7\uDCA8\uDCAD\uDCBA\uDCBC\uDCC4\uDD06\uDD0B\uDD0C\uDD15\uDD1D\uDD3A\uDD3F\uDD45\uDD47-\uDD49\uDD51\uDEA6\uDEA7\uDFCC\uDFCD]$/.test(low); }; /** * Check whether given character c is a white space character: space, tab, or enter * @param {string} c * @param {number} nestingLevel * @return {boolean} */ parse.isWhitespace = function isWhitespace (c, nestingLevel) { // TODO: also take '\r' carriage return as newline? Or does that give problems on mac? return c == ' ' || c == '\t' || (c == '\n' && nestingLevel > 0); }; /** * Test whether the character c is a decimal mark (dot). * This is the case when it's not the start of a delimiter '.*', './', or '.^' * @param {string} c * @param {string} cNext * @return {boolean} */ parse.isDecimalMark = function isDecimalMark (c, cNext) { return c == '.' && cNext !== '/' && cNext !== '*' && cNext !== '^'; }; /** * checks if the given char c is a digit or dot * @param {string} c a string with one character * @return {boolean} */ parse.isDigitDot = function isDigitDot (c) { return ((c >= '0' && c <= '9') || c == '.'); }; /** * checks if the given char c is a digit * @param {string} c a string with one character * @return {boolean} */ parse.isDigit = function isDigit (c) { return (c >= '0' && c <= '9'); }; /** * Start of the parse levels below, in order of precedence * @return {Node} node * @private */ function parseStart () { // get the first character in expression first(); getToken(); var node = parseBlock(); // check for garbage at the end of the expression // an expression ends with a empty character '' and token_type DELIMITER if (token != '') { if (token_type == TOKENTYPE.DELIMITER) { // user entered a not existing operator like "//" // TODO: give hints for aliases, for example with "<>" give as hint " did you mean != ?" throw createError('Unexpected operator ' + token); } else { throw createSyntaxError('Unexpected part "' + token + '"'); } } return node; } /** * Parse a block with expressions. Expressions can be separated by a newline * character '\n', or by a semicolon ';'. In case of a semicolon, no output * of the preceding line is returned. * @return {Node} node * @private */ function parseBlock () { var node; var blocks = []; var visible; if (token != '' && token != '\n' && token != ';') { node = parseAssignment(); node.comment = comment; } // TODO: simplify this loop while (token == '\n' || token == ';') { if (blocks.length == 0 && node) { visible = (token != ';'); blocks.push({ node: node, visible: visible }); } getToken(); if (token != '\n' && token != ';' && token != '') { node = parseAssignment(); node.comment = comment; visible = (token != ';'); blocks.push({ node: node, visible: visible }); } } if (blocks.length > 0) { return new BlockNode(blocks); } else { if (!node) { node = new ConstantNode('undefined', 'undefined'); node.comment = comment; } return node } } /** * Assignment of a function or variable, * - can be a variable like 'a=2.3' * - or a updating an existing variable like 'matrix(2,3:5)=[6,7,8]' * - defining a function like 'f(x) = x^2' * @return {Node} node * @private */ function parseAssignment () { var name, args, value, valid; var node = parseConditional(); if (token == '=') { if (node && node.isSymbolNode) { // parse a variable assignment like 'a = 2/3' name = node.name; getTokenSkipNewline(); value = parseAssignment(); return new AssignmentNode(new SymbolNode(name), value); } else if (node && node.isAccessorNode) { // parse a matrix subset assignment like 'A[1,2] = 4' getTokenSkipNewline(); value = parseAssignment(); return new AssignmentNode(node.object, node.index, value); } else if (node && node.isFunctionNode) { // parse function assignment like 'f(x) = x^2' valid = true; args = []; name = node.name; node.args.forEach(function (arg, index) { if (arg && arg.isSymbolNode) { args[index] = arg.name; } else { valid = false; } }); if (valid) { getTokenSkipNewline(); value = parseAssignment(); return new FunctionAssignmentNode(name, args, value); } } throw createSyntaxError('Invalid left hand side of assignment operator ='); } return node; } /** * conditional operation * * condition ? truePart : falsePart * * Note: conditional operator is right-associative * * @return {Node} node * @private */ function parseConditional () { var node = parseLogicalOr(); while (token == '?') { // set a conditional level, the range operator will be ignored as long // as conditional_level == nesting_level. var prev = conditional_level; conditional_level = nesting_level; getTokenSkipNewline(); var condition = node; var trueExpr = parseAssignment(); if (token != ':') throw createSyntaxError('False part of conditional expression expected'); conditional_level = null; getTokenSkipNewline(); var falseExpr = parseAssignment(); // Note: check for conditional operator again, right associativity node = new ConditionalNode(condition, trueExpr, falseExpr); // restore the previous conditional level conditional_level = prev; } return node; } /** * logical or, 'x or y' * @return {Node} node * @private */ function parseLogicalOr() { var node = parseLogicalXor(); while (token == 'or') { getTokenSkipNewline(); node = new OperatorNode('or', 'or', [node, parseLogicalXor()]); } return node; } /** * logical exclusive or, 'x xor y' * @return {Node} node * @private */ function parseLogicalXor() { var node = parseLogicalAnd(); while (token == 'xor') { getTokenSkipNewline(); node = new OperatorNode('xor', 'xor', [node, parseLogicalAnd()]); } return node; } /** * logical and, 'x and y' * @return {Node} node * @private */ function parseLogicalAnd() { var node = parseBitwiseOr(); while (token == 'and') { getTokenSkipNewline(); node = new OperatorNode('and', 'and', [node, parseBitwiseOr()]); } return node; } /** * bitwise or, 'x | y' * @return {Node} node * @private */ function parseBitwiseOr() { var node = parseBitwiseXor(); while (token == '|') { getTokenSkipNewline(); node = new OperatorNode('|', 'bitOr', [node, parseBitwiseXor()]); } return node; } /** * bitwise exclusive or (xor), 'x ^| y' * @return {Node} node * @private */ function parseBitwiseXor() { var node = parseBitwiseAnd(); while (token == '^|') { getTokenSkipNewline(); node = new OperatorNode('^|', 'bitXor', [node, parseBitwiseAnd()]); } return node; } /** * bitwise and, 'x & y' * @return {Node} node * @private */ function parseBitwiseAnd () { var node = parseRelational(); while (token == '&') { getTokenSkipNewline(); node = new OperatorNode('&', 'bitAnd', [node, parseRelational()]); } return node; } /** * relational operators * @return {Node} node * @private */ function parseRelational () { var node, operators, name, fn, params; node = parseShift(); operators = { '==': 'equal', '!=': 'unequal', '<': 'smaller', '>': 'larger', '<=': 'smallerEq', '>=': 'largerEq' }; while (token in operators) { name = token; fn = operators[name]; getTokenSkipNewline(); params = [node, parseShift()]; node = new OperatorNode(name, fn, params); } return node; } /** * Bitwise left shift, bitwise right arithmetic shift, bitwise right logical shift * @return {Node} node * @private */ function parseShift () { var node, operators, name, fn, params; node = parseConversion(); operators = { '<<' : 'leftShift', '>>' : 'rightArithShift', '>>>' : 'rightLogShift' }; while (token in operators) { name = token; fn = operators[name]; getTokenSkipNewline(); params = [node, parseConversion()]; node = new OperatorNode(name, fn, params); } return node; } /** * conversion operators 'to' and 'in' * @return {Node} node * @private */ function parseConversion () { var node, operators, name, fn, params; node = parseRange(); operators = { 'to' : 'to', 'in' : 'to' // alias of 'to' }; while (token in operators) { name = token; fn = operators[name]; getTokenSkipNewline(); if (name === 'in' && token === '') { // end of expression -> this is the unit 'in' ('inch') node = new OperatorNode('*', 'multiply', [node, new SymbolNode('in')], true); } else { // operator 'a to b' or 'a in b' params = [node, parseRange()]; node = new OperatorNode(name, fn, params); } } return node; } /** * parse range, "start:end", "start:step:end", ":", "start:", ":end", etc * @return {Node} node * @private */ function parseRange () { var node, params = []; if (token == ':') { // implicit start=1 (one-based) node = new ConstantNode('1', 'number'); } else { // explicit start node = parseAddSubtract(); } if (token == ':' && (conditional_level !== nesting_level)) { // we ignore the range operator when a conditional operator is being processed on the same level params.push(node); // parse step and end while (token == ':' && params.length < 3) { getTokenSkipNewline(); if (token == ')' || token == ']' || token == ',' || token == '') { // implicit end params.push(new SymbolNode('end')); } else { // explicit end params.push(parseAddSubtract()); } } if (params.length == 3) { // params = [start, step, end] node = new RangeNode(params[0], params[2], params[1]); // start, end, step } else { // length == 2 // params = [start, end] node = new RangeNode(params[0], params[1]); // start, end } } return node; } /** * add or subtract * @return {Node} node * @private */ function parseAddSubtract () { var node, operators, name, fn, params; node = parseMultiplyDivide(); operators = { '+': 'add', '-': 'subtract' }; while (token in operators) { name = token; fn = operators[name]; getTokenSkipNewline(); params = [node, parseMultiplyDivide()]; node = new OperatorNode(name, fn, params); } return node; } /** * multiply, divide, modulus * @return {Node} node * @private */ function parseMultiplyDivide () { var node, last, operators, name, fn; node = parseUnary(); last = node; operators = { '*': 'multiply', '.*': 'dotMultiply', '/': 'divide', './': 'dotDivide', '%': 'mod', 'mod': 'mod' }; while (true) { if (token in operators) { // explicit operators name = token; fn = operators[name]; getTokenSkipNewline(); last = parseUnary(); node = new OperatorNode(name, fn, [node, last]); } else if ((token_type == TOKENTYPE.SYMBOL) || (token == 'in' && (node && node.isConstantNode)) || (token_type == TOKENTYPE.NUMBER && !last.isConstantNode && (!last.isOperatorNode || last.op === '!')) || (token == '(')) { // parse implicit multiplication // // symbol: implicit multiplication like '2a', '(2+3)a', 'a b' // number: implicit multiplication like '(2+3)2' // parenthesis: implicit multiplication like '2(3+4)', '(3+4)(1+2)' last = parseUnary(); node = new OperatorNode('*', 'multiply', [node, last], true /*implicit*/); } else { break; } } return node; } /** * Unary plus and minus, and logical and bitwise not * @return {Node} node * @private */ function parseUnary () { var name, params; var fn = { '-': 'unaryMinus', '+': 'unaryPlus', '~': 'bitNot', 'not': 'not' }[token]; if (fn) { name = token; getTokenSkipNewline(); params = [parseUnary()]; return new OperatorNode(name, fn, params); } return parsePow(); } /** * power * Note: power operator is right associative * @return {Node} node * @private */ function parsePow () { var node, name, fn, params; node = parseLeftHandOperators(); if (token == '^' || token == '.^') { name = token; fn = (name == '^') ? 'pow' : 'dotPow'; getTokenSkipNewline(); params = [node, parseUnary()]; // Go back to unary, we can have '2^-3' node = new OperatorNode(name, fn, params); } return node; } /** * Left hand operators: factorial x!, transpose x' * @return {Node} node * @private */ function parseLeftHandOperators () { var node, operators, name, fn, params; node = parseCustomNodes(); operators = { '!': 'factorial', '\'': 'transpose' }; while (token in operators) { name = token; fn = operators[name]; getToken(); params = [node]; node = new OperatorNode(name, fn, params); node = parseAccessors(node); } return node; } /** * Parse a custom node handler. A node handler can be used to process * nodes in a custom way, for example for handling a plot. * * A handler must be passed as second argument of the parse function. * - must extend math.expression.node.Node * - must contain a function _compile(defs: Object) : string * - must contain a function find(filter: Object) : Node[] * - must contain a function toString() : string * - the constructor is called with a single argument containing all parameters * * For example: * * nodes = { * 'plot': PlotHandler * }; * * The constructor of the handler is called as: * * node = new PlotHandler(params); * * The handler will be invoked when evaluating an expression like: * * node = math.parse('plot(sin(x), x)', nodes); * * @return {Node} node * @private */ function parseCustomNodes () { var params = [], handler; if (token_type == TOKENTYPE.SYMBOL && extra_nodes[token]) { handler = extra_nodes[token]; getToken(); // parse parameters if (token == '(') { params = []; openParams(); getToken(); if (token != ')') { params.push(parseAssignment()); // parse a list with parameters while (token == ',') { getToken(); params.push(parseAssignment()); } } if (token != ')') { throw createSyntaxError('Parenthesis ) expected'); } closeParams(); getToken(); } // create a new node handler //noinspection JSValidateTypes return new handler(params); } return parseSymbol(); } /** * parse symbols: functions, variables, constants, units * @return {Node} node * @private */ function parseSymbol () { var node, name; if (token_type == TOKENTYPE.SYMBOL || (token_type == TOKENTYPE.DELIMITER && token in NAMED_DELIMITERS)) { name = token; getToken(); // parse function parameters and matrix index node = new SymbolNode(name); node = parseAccessors(node); return node; } return parseString(); } /** * parse accessors: * - function invocation in round brackets (...), for example sqrt(2) * - index enclosed in square brackets [...], for example A[2,3] * - dot notation for properties, like foo.bar * @param {Node} node Node on which to apply the parameters. If there * are no parameters in the expression, the node * itself is returned * @param {string[]} [types] Filter the types of notations * can be ['(', '[', '.'] * @return {Node} node * @private */ function parseAccessors (node, types) { var params; while ((token == '(' || token == '[' || token == '.') && (!types || types.indexOf(token) !== -1)) { params = []; if (token == '(') { if (node.isSymbolNode || node.isAccessorNode || node.isFunctionNode) { // function invocation like fn(2, 3) openParams(); getToken(); if (token != ')') { params.push(parseAssignment()); // parse a list with parameters while (token == ',') { getToken(); params.push(parseAssignment()); } } if (token != ')') { throw createSyntaxError('Parenthesis ) expected'); } closeParams(); getToken(); node = new FunctionNode(node, params); } else { // implicit multiplication like (2+3)(4+5) // don't parse it here but let it be handled by parseMultiplyDivide // with correct precedence return node; } } else if (token == '[') { // index notation like variable[2, 3] openParams(); getToken(); if (token != ']') { params.push(parseAssignment()); // parse a list with parameters while (token == ',') { getToken(); params.push(parseAssignment()); } } if (token != ']') { throw createSyntaxError('Parenthesis ] expected'); } closeParams(); getToken(); node = new AccessorNode(node, new IndexNode(params)); } else { // dot notation like variable.prop getToken(); if (token_type != TOKENTYPE.SYMBOL) { throw createSyntaxError('Property name expected after dot'); } params.push(new ConstantNode(token)); getToken(); var dotNotation = true; node = new AccessorNode(node, new IndexNode(params, dotNotation)); } } return node; } /** * parse a string. * A string is enclosed by double quotes * @return {Node} node * @private */ function parseString () { var node, str; if (token == '"') { str = parseStringToken(); // create constant node = new ConstantNode(str, 'string'); // parse index parameters node = parseAccessors(node); return node; } return parseMatrix(); } /** * Parse a string surrounded by double quotes "..." * @return {string} */ function parseStringToken () { var str = ''; while (c != '' && c != '\"') { if (c == '\\') { // escape character str += c; next(); } str += c; next(); } getToken(); if (token != '"') { throw createSyntaxError('End of string " expected'); } getToken(); return str; } /** * parse the matrix * @return {Node} node * @private */ function parseMatrix () { var array, params, rows, cols; if (token == '[') { // matrix [...] openParams(); getToken(); if (token != ']') { // this is a non-empty matrix var row = parseRow(); if (token == ';') { // 2 dimensional array rows = 1; params = [row]; // the rows of the matrix are separated by dot-comma's while (token == ';') { getToken(); params[rows] = parseRow(); rows++; } if (token != ']') { throw createSyntaxError('End of matrix ] expected'); } closeParams(); getToken(); // check if the number of columns matches in all rows cols = params[0].items.length; for (var r = 1; r < rows; r++) { if (params[r].items.length != cols) { throw createError('Column dimensions mismatch ' + '(' + params[r].items.length + ' != ' + cols + ')'); } } array = new ArrayNode(params); } else { // 1 dimensional vector if (token != ']') { throw createSyntaxError('End of matrix ] expected'); } closeParams(); getToken(); array = row; } } else { // this is an empty matrix "[ ]" closeParams(); getToken(); array = new ArrayNode([]); } return parseAccessors(array); } return parseObject(); } /** * Parse a single comma-separated row from a matrix, like 'a, b, c' * @return {ArrayNode} node */ function parseRow () { var params = [parseAssignment()]; var len = 1; while (token == ',') { getToken(); // parse expression params[len] = parseAssignment(); len++; } return new ArrayNode(params); } /** * parse an object, enclosed in angle brackets{...}, for example {value: 2} * @return {Node} node * @private */ function parseObject () { if (token == '{') { var key; var properties = {}; do { getToken(); if (token != '}') { // parse key if (token == '"') { key = parseStringToken(); } else if (token_type == TOKENTYPE.SYMBOL) { key = token; getToken(); } else { throw createSyntaxError('Symbol or string expected as object key'); } // parse key/value separator if (token != ':') { throw createSyntaxError('Colon : expected after object key'); } getToken(); // parse key properties[key] = parseAssignment(); } } while (token == ','); if (token != '}') { throw createSyntaxError('Comma , or bracket } expected after object value'); } getToken(); var node = new ObjectNode(properties); // parse index parameters node = parseAccessors(node); return node; } return parseNumber(); } /** * parse a number * @return {Node} node * @private */ function parseNumber () { var number; if (token_type == TOKENTYPE.NUMBER) { // this is a number number = token; getToken(); return new ConstantNode(number, 'number'); } return parseParentheses(); } /** * parentheses * @return {Node} node * @private */ function parseParentheses () { var node; // check if it is a parenthesized expression if (token == '(') { // parentheses (...) openParams(); getToken(); node = parseAssignment(); // start again if (token != ')') { throw createSyntaxError('Parenthesis ) expected'); } closeParams(); getToken(); node = new ParenthesisNode(node); node = parseAccessors(node); return node; } return parseEnd(); } /** * Evaluated when the expression is not yet ended but expected to end * @return {Node} res * @private */ function parseEnd () { if (token == '') { // syntax error or unexpected end of expression throw createSyntaxError('Unexpected end of expression'); } else { throw createSyntaxError('Value expected'); } } /** * Shortcut for getting the current row value (one based) * Returns the line of the currently handled expression * @private */ /* TODO: implement keeping track on the row number function row () { return null; } */ /** * Shortcut for getting the current col value (one based) * Returns the column (position) where the last token starts * @private */ function col () { return index - token.length + 1; } /** * Create an error * @param {string} message * @return {SyntaxError} instantiated error * @private */ function createSyntaxError (message) { var c = col(); var error = new SyntaxError(message + ' (char ' + c + ')'); error['char'] = c; return error; } /** * Create an error * @param {string} message * @return {Error} instantiated error * @private */ function createError (message) { var c = col(); var error = new SyntaxError(message + ' (char ' + c + ')'); error['char'] = c; return error; } return parse; } exports.name = 'parse'; exports.path = 'expression'; exports.factory = factory; /***/ }), /* 29 */ /***/ (function(module, exports, __webpack_require__) { var IndexError = __webpack_require__(42); /** * Transform zero-based indices to one-based indices in errors * @param {Error} err * @returns {Error} Returns the transformed error */ exports.transform = function (err) { if (err && err.isIndexError) { return new IndexError( err.index + 1, err.min + 1, err.max !== undefined ? err.max + 1 : undefined); } return err; }; /***/ }), /* 30 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var DenseMatrix = type.DenseMatrix; /** * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). * Callback function invoked NZ times (number of nonzero items in S). * * * ┌ f(Sij, b) ; S(i,j) !== 0 * C(i,j) = ┤ * └ b ; otherwise * * * @param {Matrix} s The SparseMatrix instance (S) * @param {Scalar} b The Scalar value * @param {Function} callback The f(Aij,b) operation to invoke * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) * * @return {Matrix} DenseMatrix (C) * * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 */ var algorithm10 = function (s, b, callback, inverse) { // sparse matrix arrays var avalues = s._values; var aindex = s._index; var aptr = s._ptr; var asize = s._size; var adt = s._datatype; // sparse matrix cannot be a Pattern matrix if (!avalues) throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string') { // datatype dt = adt; // convert b to the same datatype b = typed.convert(b, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cdata = []; // matrix var c = new DenseMatrix({ data: cdata, size: [rows, columns], datatype: dt }); // workspaces var x = []; // marks indicating we have a value in x for a given column var w = []; // loop columns for (var j = 0; j < columns; j++) { // columns mark var mark = j + 1; // values in j for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row var r = aindex[k]; // update workspace x[r] = avalues[k]; w[r] = mark; } // loop rows for (var i = 0; i < rows; i++) { // initialize C on first column if (j === 0) { // create row array cdata[i] = []; } // check sparse matrix has a value @ i,j if (w[i] === mark) { // invoke callback, update C cdata[i][j] = inverse ? cf(b, x[i]) : cf(x[i], b); } else { // dense matrix value @ i, j cdata[i][j] = b; } } } // return sparse matrix return c; }; return algorithm10; } exports.name = 'algorithm10'; exports.factory = factory; /***/ }), /* 31 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Compares two BigNumbers. * @param {BigNumber} x First value to compare * @param {BigNumber} y Second value to compare * @param {number} [epsilon] The maximum relative difference between x and y * If epsilon is undefined or null, the function will * test whether x and y are exactly equal. * @return {boolean} whether the two numbers are nearly equal */ module.exports = function nearlyEqual(x, y, epsilon) { // if epsilon is null or undefined, test whether x and y are exactly equal if (epsilon == null) { return x.eq(y); } // use "==" operator, handles infinities if (x.eq(y)) { return true; } // NaN if (x.isNaN() || y.isNaN()) { return false; } // at this point x and y should be finite if(x.isFinite() && y.isFinite()) { // check numbers are very close, needed when comparing numbers near zero var diff = x.minus(y).abs(); if (diff.isZero()) { return true; } else { // use relative error var max = x.constructor.max(x.abs(), y.abs()); return diff.lte(max.times(epsilon)); } } // Infinite and Number or negative Infinite and positive Infinite cases return false; }; /***/ }), /* 32 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Recursively loop over all elements in a given multi dimensional array * and invoke the callback on each of the elements. * @param {Array | Matrix} array * @param {Function} callback The callback method is invoked with one * parameter: the current element in the array */ module.exports = function deepForEach (array, callback) { if (array && array.isMatrix === true) { array = array.valueOf(); } for (var i = 0, ii = array.length; i < ii; i++) { var value = array[i]; if (Array.isArray(value)) { deepForEach(value, callback); } else { callback(value); } } }; /***/ }), /* 33 */ /***/ (function(module, exports) { // function utils /* * Memoize a given function by caching the computed result. * The cache of a memoized function can be cleared by deleting the `cache` * property of the function. * * @param {function} fn The function to be memoized. * Must be a pure function. * @param {function(args: Array)} [hasher] A custom hash builder. * Is JSON.stringify by default. * @return {function} Returns the memoized function */ exports.memoize = function(fn, hasher) { return function memoize() { if (typeof memoize.cache !== 'object') { memoize.cache = {}; } var args = []; for (var i = 0; i < arguments.length; i++) { args[i] = arguments[i]; } var hash = hasher ? hasher(args) : JSON.stringify(args); if (!(hash in memoize.cache)) { return memoize.cache[hash] = fn.apply(fn, args); } return memoize.cache[hash]; }; }; /** * Find the maximum number of arguments expected by a typed function. * @param {function} fn A typed function * @return {number} Returns the maximum number of expected arguments. * Returns -1 when no signatures where found on the function. */ exports.maxArgumentCount = function (fn) { return Object.keys(fn.signatures || {}) .reduce(function (args, signature) { var count = (signature.match(/,/g) || []).length + 1; return Math.max(args, count); }, -1); }; /***/ }), /* 34 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var extend = __webpack_require__(4).extend; function factory (type, config, load, typed) { var divideScalar = load(__webpack_require__(16)); var multiply = load(__webpack_require__(10)); var inv = load(__webpack_require__(109)); var matrix = load(__webpack_require__(1)); var algorithm11 = load(__webpack_require__(13)); var algorithm14 = load(__webpack_require__(5)); /** * Divide two values, `x / y`. * To divide matrices, `x` is multiplied with the inverse of `y`: `x * inv(y)`. * * Syntax: * * math.divide(x, y) * * Examples: * * math.divide(2, 3); // returns number 0.6666666666666666 * * var a = math.complex(5, 14); * var b = math.complex(4, 1); * math.divide(a, b); // returns Complex 2 + 3i * * var c = [[7, -6], [13, -4]]; * var d = [[1, 2], [4, 3]]; * math.divide(c, d); // returns Array [[-9, 4], [-11, 6]] * * var e = math.unit('18 km'); * math.divide(e, 4.5); // returns Unit 4 km * * See also: * * multiply * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Numerator * @param {number | BigNumber | Fraction | Complex | Array | Matrix} y Denominator * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Quotient, `x / y` */ var divide = typed('divide', extend({ // we extend the signatures of divideScalar with signatures dealing with matrices 'Array | Matrix, Array | Matrix': function (x, y) { // TODO: implement matrix right division using pseudo inverse // http://www.mathworks.nl/help/matlab/ref/mrdivide.html // http://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html // http://stackoverflow.com/questions/12263932/how-does-gnu-octave-matrix-division-work-getting-unexpected-behaviour return multiply(x, inv(y)); }, 'Matrix, any': function (x, y) { // result var c; // process storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, divideScalar, false); break; case 'dense': c = algorithm14(x, y, divideScalar, false); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, divideScalar, false).valueOf(); }, 'any, Array | Matrix': function (x, y) { return multiply(x, inv(y)); } }, divideScalar.signatures)); divide.toTex = {2: '\\frac{${args[0]}}{${args[1]}}'}; return divide; } exports.name = 'divide'; exports.factory = factory; /***/ }), /* 35 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var size = __webpack_require__(7).size; function factory (type, config, load, typed) { var latex = __webpack_require__(3); var eye = load(__webpack_require__(66)); var multiply = load(__webpack_require__(10)); var matrix = load(__webpack_require__(1)); var fraction = load(__webpack_require__(125)); var number = load(__webpack_require__(75)); /** * Calculates the power of x to y, `x ^ y`. * Matrix exponentiation is supported for square matrices `x`, and positive * integer exponents `y`. * * For cubic roots of negative numbers, the function returns the principal * root by default. In order to let the function return the real root, * math.js can be configured with `math.config({predictable: true})`. * To retrieve all cubic roots of a value, use `math.cbrt(x, true)`. * * Syntax: * * math.pow(x, y) * * Examples: * * math.pow(2, 3); // returns number 8 * * var a = math.complex(2, 3); * math.pow(a, 2) // returns Complex -5 + 12i * * var b = [[1, 2], [4, 3]]; * math.pow(b, 2); // returns Array [[9, 8], [16, 17]] * * See also: * * multiply, sqrt, cbrt, nthRoot * * @param {number | BigNumber | Complex | Array | Matrix} x The base * @param {number | BigNumber | Complex} y The exponent * @return {number | BigNumber | Complex | Array | Matrix} The value of `x` to the power `y` */ var pow = typed('pow', { 'number, number': _pow, 'Complex, Complex': function (x, y) { return x.pow(y); }, 'BigNumber, BigNumber': function (x, y) { if (y.isInteger() || x >= 0 || config.predictable) { return x.pow(y); } else { return new type.Complex(x.toNumber(), 0).pow(y.toNumber(), 0); } }, 'Fraction, Fraction': function (x, y) { if (y.d !== 1) { if (config.predictable) { throw new Error('Function pow does not support non-integer exponents for fractions.'); } else { return _pow(x.valueOf(), y.valueOf()); } } else { return x.pow(y); } }, 'Array, number': _powArray, 'Array, BigNumber': function (x, y) { return _powArray(x, y.toNumber()); }, 'Matrix, number': _powMatrix, 'Matrix, BigNumber': function (x, y) { return _powMatrix(x, y.toNumber()); }, 'Unit, number': function (x, y) { return x.pow(y); } }); /** * Calculates the power of x to y, x^y, for two numbers. * @param {number} x * @param {number} y * @return {number | Complex} res * @private */ function _pow(x, y) { // Alternatively could define a 'realmode' config option or something, but // 'predictable' will work for now if (config.predictable && !isInteger(y) && x < 0) { // Check to see if y can be represented as a fraction try { var yFrac = fraction(y); var yNum = number(yFrac); if(y === yNum || Math.abs((y - yNum) / y) < 1e-14) { if(yFrac.d % 2 === 1) { return (yFrac.n % 2 === 0 ? 1 : -1) * Math.pow(-x, y); } } } catch (ex) { // fraction() throws an error if y is Infinity, etc. } // Unable to express y as a fraction, so continue on } if (isInteger(y) || x >= 0 || config.predictable) { return Math.pow(x, y); } else { return new type.Complex(x, 0).pow(y, 0); } } /** * Calculate the power of a 2d array * @param {Array} x must be a 2 dimensional, square matrix * @param {number} y a positive, integer value * @returns {Array} * @private */ function _powArray(x, y) { if (!isInteger(y) || y < 0) { throw new TypeError('For A^b, b must be a positive integer (value is ' + y + ')'); } // verify that A is a 2 dimensional square matrix var s = size(x); if (s.length != 2) { throw new Error('For A^b, A must be 2 dimensional (A has ' + s.length + ' dimensions)'); } if (s[0] != s[1]) { throw new Error('For A^b, A must be square (size is ' + s[0] + 'x' + s[1] + ')'); } var res = eye(s[0]).valueOf(); var px = x; while (y >= 1) { if ((y & 1) == 1) { res = multiply(px, res); } y >>= 1; px = multiply(px, px); } return res; } /** * Calculate the power of a 2d matrix * @param {Matrix} x must be a 2 dimensional, square matrix * @param {number} y a positive, integer value * @returns {Matrix} * @private */ function _powMatrix (x, y) { return matrix(_powArray(x.valueOf(), y)); } pow.toTex = { 2: '\\left(${args[0]}\\right)' + latex.operators['pow'] + '{${args[1]}}' }; return pow; } exports.name = 'pow'; exports.factory = factory; /***/ }), /* 36 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var latex = __webpack_require__(3); /** * Inverse the sign of a value, apply a unary minus operation. * * For matrices, the function is evaluated element wise. Boolean values and * strings will be converted to a number. For complex numbers, both real and * complex value are inverted. * * Syntax: * * math.unaryMinus(x) * * Examples: * * math.unaryMinus(3.5); // returns -3.5 * math.unaryMinus(-4.2); // returns 4.2 * * See also: * * add, subtract, unaryPlus * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Number to be inverted. * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Returns the value with inverted sign. */ var unaryMinus = typed('unaryMinus', { 'number': function (x) { return -x; }, 'Complex': function (x) { return x.neg(); }, 'BigNumber': function (x) { return x.neg(); }, 'Fraction': function (x) { return x.neg(); }, 'Unit': function (x) { var res = x.clone(); res.value = unaryMinus(x.value); return res; }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since unaryMinus(0) = 0 return deepMap(x, unaryMinus, true); } // TODO: add support for string }); unaryMinus.toTex = { 1: latex.operators['unaryMinus'] + '\\left(${args[0]}\\right)' }; return unaryMinus; } exports.name = 'unaryMinus'; exports.factory = factory; /***/ }), /* 37 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var resize = __webpack_require__(7).resize; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Create a matrix filled with zeros. The created matrix can have one or * multiple dimensions. * * Syntax: * * math.zeros(m) * math.zeros(m, format) * math.zeros(m, n) * math.zeros(m, n, format) * math.zeros([m, n]) * math.zeros([m, n], format) * * Examples: * * math.zeros(3); // returns [0, 0, 0] * math.zeros(3, 2); // returns [[0, 0], [0, 0], [0, 0]] * math.zeros(3, 'dense'); // returns [0, 0, 0] * * var A = [[1, 2, 3], [4, 5, 6]]; * math.zeros(math.size(A)); // returns [[0, 0, 0], [0, 0, 0]] * * See also: * * ones, eye, size, range * * @param {...number | Array} size The size of each dimension of the matrix * @param {string} [format] The Matrix storage format * * @return {Array | Matrix} A matrix filled with zeros */ var zeros = typed('zeros', { '': function () { return (config.matrix === 'Array') ? _zeros([]) : _zeros([], 'default'); }, // math.zeros(m, n, p, ..., format) // TODO: more accurate signature '...number | BigNumber, string' as soon as typed-function supports this '...number | BigNumber | string': function (size) { var last = size[size.length - 1]; if (typeof last === 'string') { var format = size.pop(); return _zeros(size, format); } else if (config.matrix === 'Array') { return _zeros(size); } else { return _zeros(size, 'default'); } }, 'Array': _zeros, 'Matrix': function (size) { var format = size.storage(); return _zeros(size.valueOf(), format); }, 'Array | Matrix, string': function (size, format) { return _zeros (size.valueOf(), format); } }); zeros.toTex = undefined; // use default template return zeros; /** * Create an Array or Matrix with zeros * @param {Array} size * @param {string} [format='default'] * @return {Array | Matrix} * @private */ function _zeros(size, format) { var hasBigNumbers = _normalize(size); var defaultValue = hasBigNumbers ? new type.BigNumber(0) : 0; _validate(size); if (format) { // return a matrix var m = matrix(format); if (size.length > 0) { return m.resize(size, defaultValue); } return m; } else { // return an Array var arr = []; if (size.length > 0) { return resize(arr, size, defaultValue); } return arr; } } // replace BigNumbers with numbers, returns true if size contained BigNumbers function _normalize(size) { var hasBigNumbers = false; size.forEach(function (value, index, arr) { if (value && value.isBigNumber === true) { hasBigNumbers = true; arr[index] = value.toNumber(); } }); return hasBigNumbers; } // validate arguments function _validate (size) { size.forEach(function (value) { if (typeof value !== 'number' || !isInteger(value) || value < 0) { throw new Error('Parameters in function zeros must be positive integers'); } }); } } // TODO: zeros contains almost the same code as ones. Reuse this? exports.name = 'zeros'; exports.factory = factory; /***/ }), /* 38 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var nearlyEqual = __webpack_require__(2).nearlyEqual; var bigNearlyEqual = __webpack_require__(31); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); var latex = __webpack_require__(3); /** * Test whether value x is smaller than y. * * The function returns true when x is smaller than y and the relative * difference between x and y is smaller than the configured epsilon. The * function cannot be used to compare values smaller than approximately 2.22e-16. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.smaller(x, y) * * Examples: * * math.smaller(2, 3); // returns true * math.smaller(5, 2 * 2); // returns false * * var a = math.unit('5 cm'); * var b = math.unit('2 inch'); * math.smaller(a, b); // returns true * * See also: * * equal, unequal, smallerEq, smaller, smallerEq, compare * * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare * @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false */ var smaller = typed('smaller', { 'boolean, boolean': function (x, y) { return x < y; }, 'number, number': function (x, y) { return x < y && !nearlyEqual(x, y, config.epsilon); }, 'BigNumber, BigNumber': function (x, y) { return x.lt(y) && !bigNearlyEqual(x, y, config.epsilon); }, 'Fraction, Fraction': function (x, y) { return x.compare(y) === -1; }, 'Complex, Complex': function (x, y) { throw new TypeError('No ordering relation is defined for complex numbers'); }, 'Unit, Unit': function (x, y) { if (!x.equalBase(y)) { throw new Error('Cannot compare units with different base'); } return smaller(x.value, y.value); }, 'string, string': function (x, y) { return x < y; }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, smaller); break; default: // sparse + dense c = algorithm03(y, x, smaller, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, smaller, false); break; default: // dense + dense c = algorithm13(x, y, smaller); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return smaller(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return smaller(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return smaller(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, smaller, false); break; default: c = algorithm14(x, y, smaller, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, smaller, true); break; default: c = algorithm14(y, x, smaller, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, smaller, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, smaller, true).valueOf(); } }); smaller.toTex = { 2: '\\left(${args[0]}' + latex.operators['smaller'] + '${args[1]}\\right)' }; return smaller; } exports.name = 'smaller'; exports.factory = factory; /***/ }), /* 39 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Test whether a value is an integer number. * The function supports `number`, `BigNumber`, and `Fraction`. * * The function is evaluated element-wise in case of Array or Matrix input. * * Syntax: * * math.isInteger(x) * * Examples: * * math.isInteger(2); // returns true * math.isInteger(0); // returns true * math.isInteger(0.5); // returns false * math.isInteger(math.bignumber(500)); // returns true * math.isInteger(math.fraction(4)); // returns true * math.isInteger('3'); // returns true * math.isInteger([3, 0.5, -2]); // returns [true, false, true] * math.isInteger(math.complex('2-4i'); // throws an error * * See also: * * isNumeric, isPositive, isNegative, isZero * * @param {number | BigNumber | Fraction | Array | Matrix} x Value to be tested * @return {boolean} Returns true when `x` contains a numeric, integer value. * Throws an error in case of an unknown data type. */ var isInteger = typed('isInteger', { 'number': number.isInteger, // TODO: what to do with isInteger(add(0.1, 0.2)) ? 'BigNumber': function (x) { return x.isInt(); }, 'Fraction': function (x) { return x.d === 1 && isFinite(x.n); }, 'Array | Matrix': function (x) { return deepMap(x, isInteger); } }); return isInteger; } exports.name = 'isInteger'; exports.factory = factory; /***/ }), /* 40 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Test whether a value is a collection: an Array or Matrix * @param {*} x * @returns {boolean} isCollection */ module.exports = function isCollection (x) { return (Array.isArray(x) || (x && x.isMatrix === true)); }; /***/ }), /* 41 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Create a syntax error with the message: * 'Wrong number of arguments in function ( provided, - expected)' * @param {string} fn Function name * @param {number} count Actual argument count * @param {number} min Minimum required argument count * @param {number} [max] Maximum required argument count * @extends Error */ function ArgumentsError(fn, count, min, max) { if (!(this instanceof ArgumentsError)) { throw new SyntaxError('Constructor must be called with the new operator'); } this.fn = fn; this.count = count; this.min = min; this.max = max; this.message = 'Wrong number of arguments in function ' + fn + ' (' + count + ' provided, ' + min + ((max != undefined) ? ('-' + max) : '') + ' expected)'; this.stack = (new Error()).stack; } ArgumentsError.prototype = new Error(); ArgumentsError.prototype.constructor = Error; ArgumentsError.prototype.name = 'ArgumentsError'; ArgumentsError.prototype.isArgumentsError = true; module.exports = ArgumentsError; /***/ }), /* 42 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Create a range error with the message: * 'Index out of range (index < min)' * 'Index out of range (index < max)' * * @param {number} index The actual index * @param {number} [min=0] Minimum index (included) * @param {number} [max] Maximum index (excluded) * @extends RangeError */ function IndexError(index, min, max) { if (!(this instanceof IndexError)) { throw new SyntaxError('Constructor must be called with the new operator'); } this.index = index; if (arguments.length < 3) { this.min = 0; this.max = min; } else { this.min = min; this.max = max; } if (this.min !== undefined && this.index < this.min) { this.message = 'Index out of range (' + this.index + ' < ' + this.min + ')'; } else if (this.max !== undefined && this.index >= this.max) { this.message = 'Index out of range (' + this.index + ' > ' + (this.max - 1) + ')'; } else { this.message = 'Index out of range (' + this.index + ')'; } this.stack = (new Error()).stack; } IndexError.prototype = new RangeError(); IndexError.prototype.constructor = RangeError; IndexError.prototype.name = 'IndexError'; IndexError.prototype.isIndexError = true; module.exports = IndexError; /***/ }), /* 43 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var getType = __webpack_require__(60).type; function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); /** * A ConstantNode holds a constant value like a number or string. A ConstantNode * stores a stringified version of the value and uses this to compile to * JavaScript. * * In case of a stringified number as input, this may be compiled to a BigNumber * when the math instance is configured for BigNumbers. * * Usage: * * // stringified values with type * new ConstantNode('2.3', 'number'); * new ConstantNode('true', 'boolean'); * new ConstantNode('hello', 'string'); * * // non-stringified values, type will be automatically detected * new ConstantNode(2.3); * new ConstantNode('hello'); * * @param {string | number | boolean | null | undefined} value * When valueType is provided, value must contain * an uninterpreted string representing the value. * When valueType is undefined, value can be a * number, string, boolean, null, or undefined, and * the type will be determined automatically. * @param {string} [valueType] The type of value. Choose from 'number', 'string', * 'boolean', 'undefined', 'null' * @constructor ConstantNode * @extends {Node} */ function ConstantNode(value, valueType) { if (!(this instanceof ConstantNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (valueType) { if (typeof valueType !== 'string') { throw new TypeError('String expected for parameter "valueType"'); } if (typeof value !== 'string') { throw new TypeError('String expected for parameter "value"'); } this.value = value; this.valueType = valueType; } else { // stringify the value and determine the type this.value = value + ''; this.valueType = getType(value); } if (!SUPPORTED_TYPES[this.valueType]) { throw new TypeError('Unsupported type of value "' + this.valueType + '"'); } } var SUPPORTED_TYPES = { 'number': true, 'string': true, 'boolean': true, 'undefined': true, 'null': true }; ConstantNode.prototype = new Node(); ConstantNode.prototype.type = 'ConstantNode'; ConstantNode.prototype.isConstantNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ ConstantNode.prototype._compile = function (defs, args) { switch (this.valueType) { case 'number': // TODO: replace this with using config.number var numConfig = defs.math.config().number; if (numConfig === 'BigNumber') { return 'math.bignumber("' + this.value + '")'; } else if (numConfig === 'Fraction') { return 'math.fraction("' + this.value + '")'; } else { // remove leading zeros like '003.2' which are not allowed by JavaScript return this.value.replace(/^(0*)[0-9]/, function (match, zeros) { return match.substring(zeros.length); }); } case 'string': return '"' + this.value + '"'; case 'boolean': return this.value; case 'undefined': return this.value; case 'null': return this.value; default: // TODO: move this error to the constructor? throw new TypeError('Unsupported type of constant "' + this.valueType + '"'); } }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ ConstantNode.prototype.forEach = function (callback) { // nothing to do, we don't have childs }; /** * Create a new ConstantNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node) : Node} callback * @returns {ConstantNode} Returns a clone of the node */ ConstantNode.prototype.map = function (callback) { return this.clone(); }; /** * Create a clone of this node, a shallow copy * @return {ConstantNode} */ ConstantNode.prototype.clone = function () { return new ConstantNode(this.value, this.valueType); }; /** * Get string representation * @param {Object} options * @return {string} str */ ConstantNode.prototype._toString = function (options) { switch (this.valueType) { case 'string': return '"' + this.value + '"'; default: return this.value; } }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ ConstantNode.prototype._toTex = function (options) { var value = this.value, index; switch (this.valueType) { case 'string': return '\\mathtt{"' + value + '"}'; case 'number': index = value.toLowerCase().indexOf('e'); if (index !== -1) { return value.substring(0, index) + '\\cdot10^{' + value.substring(index + 1) + '}'; } return value; default: return value; } }; return ConstantNode; } exports.name = 'ConstantNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 44 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var latex = __webpack_require__(3); var extend = __webpack_require__(4).extend; function factory (type, config, load, typed, math) { var Node = load(__webpack_require__(11)); var SymbolNode = load(__webpack_require__(24)); /** * @constructor FunctionNode * @extends {./Node} * invoke a list with arguments on a node * @param {./Node | string} fn Node resolving with a function on which to invoke * the arguments, typically a SymboNode or AccessorNode * @param {./Node[]} args */ function FunctionNode(fn, args) { if (!(this instanceof FunctionNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (typeof fn === 'string') { fn = new SymbolNode(fn); } // validate input if (!fn || !fn.isNode) throw new TypeError('Node expected as parameter "fn"'); if (!Array.isArray(args) || !args.every(function (arg) {return arg && arg.isNode;})) { throw new TypeError('Array containing Nodes expected for parameter "args"'); } this.fn = fn; this.args = args || []; // readonly property name Object.defineProperty(this, 'name', { get: function () { return this.fn.name || ''; }.bind(this), set: function () { throw new Error('Cannot assign a new name, name is read-only'); } }); // TODO: deprecated since v3, remove some day var deprecated = function () { throw new Error('Property `FunctionNode.object` is deprecated, use `FunctionNode.fn` instead'); }; Object.defineProperty(this, 'object', { get: deprecated, set: deprecated }); } FunctionNode.prototype = new Node(); FunctionNode.prototype.type = 'FunctionNode'; FunctionNode.prototype.isFunctionNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ FunctionNode.prototype._compile = function (defs, args) { // compile fn and arguments var jsFn = this.fn._compile(defs, args); var jsArgs = this.args.map(function (arg) { return arg._compile(defs, args); }); var jsScope = this._compileScope(defs, args); var argsName; if (this.fn.isSymbolNode) { // we can statically determine whether the function has an rawArgs property var name = this.fn.name; var fn = defs.math[name]; var isRaw = (typeof fn === 'function') && (fn.rawArgs == true); if (isRaw) { // pass unevaluated parameters (nodes) to the function argsName = this._getUniqueArgumentsName(defs); defs[argsName] = this.args; return jsFn + '(' + argsName + ', math, ' + jsScope + ')'; } else { // "regular" evaluation return jsFn + '(' + jsArgs.join(', ') + ')'; } } else if (this.fn.isAccessorNode && this.fn.index.isObjectProperty()) { // execute the function with the right context: the object of the AccessorNode argsName = this._getUniqueArgumentsName(defs); defs[argsName] = this.args; var jsObject = this.fn.object._compile(defs, args); var prop = this.fn.index.getObjectProperty(); return '(function () {' + 'var object = ' + jsObject + ';' + 'return (object["' + prop + '"] && object["' + prop + '"].rawArgs) ' + ' ? object["' + prop + '"](' + argsName + ', math, ' + jsScope + ')' + ' : object["' + prop + '"](' + jsArgs.join(', ') + ')' + '})()'; } else { // this.fn.isAccessorNode && !this.fn.index.isObjectProperty() // we have to dynamically determine whether the function has a rawArgs property argsName = this._getUniqueArgumentsName(defs); defs[argsName] = this.args; return '(function () {' + 'var fn = ' + jsFn + ';' + 'return (fn && fn.rawArgs) ' + ' ? fn(' + argsName + ', math, ' + jsScope + ')' + ' : fn(' + jsArgs.join(', ') + ')' + '})()'; } }; /** * Merge function arguments into scope before passing to the actual function. * This is needed when the function has `rawArgs=true`. In that case we have * to pass the `scope` as third argument, including any variables of * enclosing functions. * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ FunctionNode.prototype._compileScope = function (defs, args) { var names = Object.keys(args); if (names.length === 0) { return 'scope'; } else { // merge arguments into scope defs.extend = extend; var jsArgs = names .map(function (arg) { return '"' + arg + '": ' + arg; }) .join(', '); return 'extend(extend({}, scope), {' + jsArgs + '})'; } }; /** * Get a unique name for a arguments to store in defs * @param {Object} defs * @return {string} A string like 'args1', 'args2', ... * @private */ FunctionNode.prototype._getUniqueArgumentsName = function (defs) { var argsName; var i = 0; do { argsName = 'args' + i; i++; } while (argsName in defs); return argsName; }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ FunctionNode.prototype.forEach = function (callback) { for (var i = 0; i < this.args.length; i++) { callback(this.args[i], 'args[' + i + ']', this); } }; /** * Create a new FunctionNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {FunctionNode} Returns a transformed copy of the node */ FunctionNode.prototype.map = function (callback) { var fn = this.fn.map(callback); var args = []; for (var i = 0; i < this.args.length; i++) { args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this)); } return new FunctionNode(fn, args); }; /** * Create a clone of this node, a shallow copy * @return {FunctionNode} */ FunctionNode.prototype.clone = function () { return new FunctionNode(this.fn, this.args.slice(0)); }; //backup Node's toString function //@private var nodeToString = FunctionNode.prototype.toString; /** * Get string representation. (wrapper function) * This overrides parts of Node's toString function. * If callback is an object containing callbacks, it * calls the correct callback for the current node, * otherwise it falls back to calling Node's toString * function. * * @param {Object} options * @return {string} str * @override */ FunctionNode.prototype.toString = function (options) { var customString; var name = this.fn.toString(options); if (options && (typeof options.handler === 'object') && options.handler.hasOwnProperty(name)) { //callback is a map of callback functions customString = options.handler[name](this, options); } if (typeof customString !== 'undefined') { return customString; } //fall back to Node's toString return nodeToString.call(this, options); }; /** * Get string representation * @param {Object} options * @return {string} str */ FunctionNode.prototype._toString = function (options) { var args = this.args.map(function (arg) { return arg.toString(options); }); // format the arguments like "add(2, 4.2)" return this.fn.toString(options) + '(' + args.join(', ') + ')'; }; /* * Expand a LaTeX template * * @param {string} template * @param {Node} node * @param {Object} options * @private **/ function expandTemplate(template, node, options) { var latex = ''; // Match everything of the form ${identifier} or ${identifier[2]} or $$ // while submatching identifier and 2 (in the second case) var regex = new RegExp('\\$(?:\\{([a-z_][a-z_0-9]*)(?:\\[([0-9]+)\\])?\\}|\\$)', 'ig'); var inputPos = 0; //position in the input string var match; while ((match = regex.exec(template)) !== null) { //go through all matches // add everything in front of the match to the LaTeX string latex += template.substring(inputPos, match.index); inputPos = match.index; if (match[0] === '$$') { // escaped dollar sign latex += '$'; inputPos++; } else { // template parameter inputPos += match[0].length; var property = node[match[1]]; if (!property) { throw new ReferenceError('Template: Property ' + match[1] + ' does not exist.'); } if (match[2] === undefined) { //no square brackets switch (typeof property) { case 'string': latex += property; break; case 'object': if (property.isNode) { latex += property.toTex(options); } else if (Array.isArray(property)) { //make array of Nodes into comma separated list latex += property.map(function (arg, index) { if (arg && arg.isNode) { return arg.toTex(options); } throw new TypeError('Template: ' + match[1] + '[' + index + '] is not a Node.'); }).join(','); } else { throw new TypeError('Template: ' + match[1] + ' has to be a Node, String or array of Nodes'); } break; default: throw new TypeError('Template: ' + match[1] + ' has to be a Node, String or array of Nodes'); } } else { //with square brackets if (property[match[2]] && property[match[2]].isNode) { latex += property[match[2]].toTex(options); } else { throw new TypeError('Template: ' + match[1] + '[' + match[2] + '] is not a Node.'); } } } } latex += template.slice(inputPos); //append rest of the template return latex; } //backup Node's toTex function //@private var nodeToTex = FunctionNode.prototype.toTex; /** * Get LaTeX representation. (wrapper function) * This overrides parts of Node's toTex function. * If callback is an object containing callbacks, it * calls the correct callback for the current node, * otherwise it falls back to calling Node's toTex * function. * * @param {Object} options * @return {string} */ FunctionNode.prototype.toTex = function (options) { var customTex; if (options && (typeof options.handler === 'object') && options.handler.hasOwnProperty(this.name)) { //callback is a map of callback functions customTex = options.handler[this.name](this, options); } if (typeof customTex !== 'undefined') { return customTex; } //fall back to Node's toTex return nodeToTex.call(this, options); }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ FunctionNode.prototype._toTex = function (options) { var args = this.args.map(function (arg) { //get LaTeX of the arguments return arg.toTex(options); }); var latexConverter; if (math[this.name] && ((typeof math[this.name].toTex === 'function') || (typeof math[this.name].toTex === 'object') || (typeof math[this.name].toTex === 'string'))) { //.toTex is a callback function latexConverter = math[this.name].toTex; } var customToTex; switch (typeof latexConverter) { case 'function': //a callback function customToTex = latexConverter(this, options); break; case 'string': //a template string customToTex = expandTemplate(latexConverter, this, options); break; case 'object': //an object with different "converters" for different numbers of arguments switch (typeof latexConverter[args.length]) { case 'function': customToTex = latexConverter[args.length](this, options); break; case 'string': customToTex = expandTemplate(latexConverter[args.length], this, options); break; } } if (typeof customToTex !== 'undefined') { return customToTex; } return expandTemplate(latex.defaultTemplate, this, options); }; /** * Get identifier. * @return {string} */ FunctionNode.prototype.getIdentifier = function () { return this.type + ':' + this.name; }; return FunctionNode; } exports.name = 'FunctionNode'; exports.path = 'expression.node'; exports.math = true; // request access to the math namespace as 5th argument of the factory function exports.factory = factory; /***/ }), /* 45 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; //list of identifiers of nodes in order of their precedence //also contains information about left/right associativity //and which other operator the operator is associative with //Example: // addition is associative with addition and subtraction, because: // (a+b)+c=a+(b+c) // (a+b)-c=a+(b-c) // // postfix operators are left associative, prefix operators // are right associative // //It's also possible to set the following properties: // latexParens: if set to false, this node doesn't need to be enclosed // in parentheses when using LaTeX // latexLeftParens: if set to false, this !OperatorNode's! // left argument doesn't need to be enclosed // in parentheses // latexRightParens: the same for the right argument var properties = [ { //assignment 'AssignmentNode': {}, 'FunctionAssignmentNode': {} }, { //conditional expression 'ConditionalNode': { latexLeftParens: false, latexRightParens: false, latexParens: false //conditionals don't need parentheses in LaTeX because //they are 2 dimensional } }, { //logical or 'OperatorNode:or': { associativity: 'left', associativeWith: [] } }, { //logical xor 'OperatorNode:xor': { associativity: 'left', associativeWith: [] } }, { //logical and 'OperatorNode:and': { associativity: 'left', associativeWith: [] } }, { //bitwise or 'OperatorNode:bitOr': { associativity: 'left', associativeWith: [] } }, { //bitwise xor 'OperatorNode:bitXor': { associativity: 'left', associativeWith: [] } }, { //bitwise and 'OperatorNode:bitAnd': { associativity: 'left', associativeWith: [] } }, { //relational operators 'OperatorNode:equal': { associativity: 'left', associativeWith: [] }, 'OperatorNode:unequal': { associativity: 'left', associativeWith: [] }, 'OperatorNode:smaller': { associativity: 'left', associativeWith: [] }, 'OperatorNode:larger': { associativity: 'left', associativeWith: [] }, 'OperatorNode:smallerEq': { associativity: 'left', associativeWith: [] }, 'OperatorNode:largerEq': { associativity: 'left', associativeWith: [] } }, { //bitshift operators 'OperatorNode:leftShift': { associativity: 'left', associativeWith: [] }, 'OperatorNode:rightArithShift': { associativity: 'left', associativeWith: [] }, 'OperatorNode:rightLogShift': { associativity: 'left', associativeWith: [] } }, { //unit conversion 'OperatorNode:to': { associativity: 'left', associativeWith: [] } }, { //range 'RangeNode': {} }, { //addition, subtraction 'OperatorNode:add': { associativity: 'left', associativeWith: ['OperatorNode:add', 'OperatorNode:subtract'] }, 'OperatorNode:subtract': { associativity: 'left', associativeWith: [] } }, { //multiply, divide, modulus 'OperatorNode:multiply': { associativity: 'left', associativeWith: [ 'OperatorNode:multiply', 'OperatorNode:divide', 'Operator:dotMultiply', 'Operator:dotDivide' ] }, 'OperatorNode:divide': { associativity: 'left', associativeWith: [], latexLeftParens: false, latexRightParens: false, latexParens: false //fractions don't require parentheses because //they're 2 dimensional, so parens aren't needed //in LaTeX }, 'OperatorNode:dotMultiply': { associativity: 'left', associativeWith: [ 'OperatorNode:multiply', 'OperatorNode:divide', 'OperatorNode:dotMultiply', 'OperatorNode:doDivide' ] }, 'OperatorNode:dotDivide': { associativity: 'left', associativeWith: [] }, 'OperatorNode:mod': { associativity: 'left', associativeWith: [] } }, { //unary prefix operators 'OperatorNode:unaryPlus': { associativity: 'right' }, 'OperatorNode:unaryMinus': { associativity: 'right' }, 'OperatorNode:bitNot': { associativity: 'right' }, 'OperatorNode:not': { associativity: 'right' } }, { //exponentiation 'OperatorNode:pow': { associativity: 'right', associativeWith: [], latexRightParens: false //the exponent doesn't need parentheses in //LaTeX because it's 2 dimensional //(it's on top) }, 'OperatorNode:dotPow': { associativity: 'right', associativeWith: [] } }, { //factorial 'OperatorNode:factorial': { associativity: 'left' } }, { //matrix transpose 'OperatorNode:transpose': { associativity: 'left' } } ]; /** * Get the precedence of a Node. * Higher number for higher precedence, starting with 0. * Returns null if the precedence is undefined. * * @param {Node} * @param {string} parenthesis * @return {number|null} */ function getPrecedence (_node, parenthesis) { var node = _node; if (parenthesis !== 'keep') { //ParenthesisNodes are only ignored when not in 'keep' mode node = _node.getContent(); } var identifier = node.getIdentifier(); for (var i = 0; i < properties.length; i++) { if (identifier in properties[i]) { return i; } } return null; } /** * Get the associativity of an operator (left or right). * Returns a string containing 'left' or 'right' or null if * the associativity is not defined. * * @param {Node} * @param {string} parenthesis * @return {string|null} * @throws {Error} */ function getAssociativity (_node, parenthesis) { var node = _node; if (parenthesis !== 'keep') { //ParenthesisNodes are only ignored when not in 'keep' mode node = _node.getContent(); } var identifier = node.getIdentifier(); var index = getPrecedence(node, parenthesis); if (index === null) { //node isn't in the list return null; } var property = properties[index][identifier]; if (property.hasOwnProperty('associativity')) { if (property.associativity === 'left') { return 'left'; } if (property.associativity === 'right') { return 'right'; } //associativity is invalid throw Error('\'' + identifier + '\' has the invalid associativity \'' + property.associativity + '\'.'); } //associativity is undefined return null; } /** * Check if an operator is associative with another operator. * Returns either true or false or null if not defined. * * @param {Node} nodeA * @param {Node} nodeB * @param {string} parenthesis * @return {bool|null} */ function isAssociativeWith (nodeA, nodeB, parenthesis) { var a = nodeA; var b = nodeB; if (parenthesis !== 'keep') { //ParenthesisNodes are only ignored when not in 'keep' mode var a = nodeA.getContent(); var b = nodeB.getContent(); } var identifierA = a.getIdentifier(); var identifierB = b.getIdentifier(); var index = getPrecedence(a, parenthesis); if (index === null) { //node isn't in the list return null; } var property = properties[index][identifierA]; if (property.hasOwnProperty('associativeWith') && (property.associativeWith instanceof Array)) { for (var i = 0; i < property.associativeWith.length; i++) { if (property.associativeWith[i] === identifierB) { return true; } } return false; } //associativeWith is not defined return null; } module.exports.properties = properties; module.exports.getPrecedence = getPrecedence; module.exports.getAssociativity = getAssociativity; module.exports.isAssociativeWith = isAssociativeWith; /***/ }), /* 46 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var nearlyEqual = __webpack_require__(2).nearlyEqual; var bigNearlyEqual = __webpack_require__(31); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm05 = load(__webpack_require__(56)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y. * * x and y are considered equal when the relative difference between x and y * is smaller than the configured epsilon. The function cannot be used to * compare values smaller than approximately 2.22e-16. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.compare(x, y) * * Examples: * * math.compare(6, 1); // returns 1 * math.compare(2, 3); // returns -1 * math.compare(7, 7); // returns 0 * * var a = math.unit('5 cm'); * var b = math.unit('40 mm'); * math.compare(a, b); // returns 1 * * math.compare(2, [1, 2, 3]); // returns [1, 0, -1] * * See also: * * equal, unequal, smaller, smallerEq, larger, largerEq * * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} x First value to compare * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} y Second value to compare * @return {number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison: 1, 0 or -1. */ var compare = typed('compare', { 'boolean, boolean': function (x, y) { return x === y ? 0 : (x > y ? 1 : -1); }, 'number, number': function (x, y) { return (x === y || nearlyEqual(x, y, config.epsilon)) ? 0 : (x > y ? 1 : -1); }, 'BigNumber, BigNumber': function (x, y) { return (x.eq(y) || bigNearlyEqual(x, y, config.epsilon)) ? new type.BigNumber(0) : new type.BigNumber(x.cmp(y)); }, 'Fraction, Fraction': function (x, y) { return new type.Fraction(x.compare(y)); }, 'Complex, Complex': function () { throw new TypeError('No ordering relation is defined for complex numbers'); }, 'Unit, Unit': function (x, y) { if (!x.equalBase(y)) { throw new Error('Cannot compare units with different base'); } return compare(x.value, y.value); }, 'string, string': function (x, y) { return x === y ? 0 : (x > y ? 1 : -1); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm05(x, y, compare); break; default: // sparse + dense c = algorithm03(y, x, compare, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, compare, false); break; default: // dense + dense c = algorithm13(x, y, compare); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return compare(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return compare(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return compare(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, compare, false); break; default: c = algorithm14(x, y, compare, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, compare, true); break; default: c = algorithm14(y, x, compare, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, compare, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, compare, true).valueOf(); } }); compare.toTex = undefined; // use default template return compare; } exports.name = 'compare'; exports.factory = factory; /***/ }), /* 47 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Test whether a value is negative: smaller than zero. * The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`. * * The function is evaluated element-wise in case of Array or Matrix input. * * Syntax: * * math.isNegative(x) * * Examples: * * math.isNegative(3); // returns false * math.isNegative(-2); // returns true * math.isNegative(0); // returns false * math.isNegative(-0); // returns false * math.isNegative(math.bignumber(2)); // returns false * math.isNegative(math.fraction(-2, 5)); // returns true * math.isNegative('-2'); // returns true * math.isNegative([2, 0, -3]'); // returns [false, false, true] * * See also: * * isNumeric, isPositive, isZero, isInteger * * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested * @return {boolean} Returns true when `x` is larger than zero. * Throws an error in case of an unknown data type. */ var isNegative = typed('isNegative', { 'number': function (x) { return x < 0; }, 'BigNumber': function (x) { return x.isNeg() && !x.isZero() && !x.isNaN(); }, 'Fraction': function (x) { return x.s < 0; // It's enough to decide on the sign }, 'Unit': function (x) { return isNegative(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, isNegative); } }); return isNegative; } exports.name = 'isNegative'; exports.factory = factory; /***/ }), /* 48 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var latex = __webpack_require__(3); var operators = __webpack_require__(45); function factory (type, config, load, typed, math) { var Node = load(__webpack_require__(11)); var ConstantNode = load(__webpack_require__(43)); var SymbolNode = load(__webpack_require__(24)); var FunctionNode = load(__webpack_require__(44)); /** * @constructor OperatorNode * @extends {Node} * An operator with two arguments, like 2+3 * * @param {string} op Operator name, for example '+' * @param {string} fn Function name, for example 'add' * @param {Node[]} args Operator arguments * @param {boolean} [implicit] Is this an implicit multiplication? */ function OperatorNode(op, fn, args, implicit) { if (!(this instanceof OperatorNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } //validate input if (typeof op !== 'string') { throw new TypeError('string expected for parameter "op"'); } if (typeof fn !== 'string') { throw new TypeError('string expected for parameter "fn"'); } if (!Array.isArray(args) || !args.every(function (node) {return node && node.isNode;})) { throw new TypeError('Array containing Nodes expected for parameter "args"'); } this.implicit = (implicit === true); this.op = op; this.fn = fn; this.args = args || []; } OperatorNode.prototype = new Node(); OperatorNode.prototype.type = 'OperatorNode'; OperatorNode.prototype.isOperatorNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ OperatorNode.prototype._compile = function (defs, args) { if (!defs.math[this.fn]) { throw new Error('Function ' + this.fn + ' missing in provided namespace "math"'); } var jsArgs = this.args.map(function (arg) { return arg._compile(defs, args); }); return 'math.' + this.fn + '(' + jsArgs.join(', ') + ')'; }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ OperatorNode.prototype.forEach = function (callback) { for (var i = 0; i < this.args.length; i++) { callback(this.args[i], 'args[' + i + ']', this); } }; /** * Create a new OperatorNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {OperatorNode} Returns a transformed copy of the node */ OperatorNode.prototype.map = function (callback) { var args = []; for (var i = 0; i < this.args.length; i++) { args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this)); } return new OperatorNode(this.op, this.fn, args); }; /** * Create a clone of this node, a shallow copy * @return {OperatorNode} */ OperatorNode.prototype.clone = function () { return new OperatorNode(this.op, this.fn, this.args.slice(0), this.implicit); }; /** * Calculate which parentheses are necessary. Gets an OperatorNode * (which is the root of the tree) and an Array of Nodes * (this.args) and returns an array where 'true' means that an argument * has to be enclosed in parentheses whereas 'false' means the opposite. * * @param {OperatorNode} root * @param {string} parenthesis * @param {Node[]} args * @param {boolean} latex * @return {boolean[]} * @private */ function calculateNecessaryParentheses(root, parenthesis, args, latex) { //precedence of the root OperatorNode var precedence = operators.getPrecedence(root, parenthesis); var associativity = operators.getAssociativity(root, parenthesis); if ((parenthesis === 'all') || ((args.length > 2) && (root.getIdentifier() !== 'OperatorNode:add') && (root.getIdentifier() !== 'OperatorNode:multiply'))) { var parens = args.map(function (arg) { switch (arg.getContent().type) { //Nodes that don't need extra parentheses case 'ArrayNode': case 'ConstantNode': case 'SymbolNode': case 'ParenthesisNode': return false; break; default: return true; } }); return parens; } if (args.length === 0) { return []; } else if (args.length === 1) { //unary operators //precedence of the operand var operandPrecedence = operators.getPrecedence(args[0], parenthesis); //handle special cases for LaTeX, where some of the parentheses aren't needed if (latex && (operandPrecedence !== null)) { var operandIdentifier; var rootIdentifier; if (parenthesis === 'keep') { operandIdentifier = args[0].getIdentifier(); rootIdentifier = root.getIdentifier(); } else { //Ignore Parenthesis Nodes when not in 'keep' mode operandIdentifier = args[0].getContent().getIdentifier(); rootIdentifier = root.getContent().getIdentifier(); } if (operators.properties[precedence][rootIdentifier].latexLeftParens === false) { return [false]; } if (operators.properties[operandPrecedence][operandIdentifier].latexParens === false) { return [false]; } } if (operandPrecedence === null) { //if the operand has no defined precedence, no parens are needed return [false]; } if (operandPrecedence <= precedence) { //if the operands precedence is lower, parens are needed return [true]; } //otherwise, no parens needed return [false]; } else if (args.length === 2) { //binary operators var lhsParens; //left hand side needs parenthesis? //precedence of the left hand side var lhsPrecedence = operators.getPrecedence(args[0], parenthesis); //is the root node associative with the left hand side var assocWithLhs = operators.isAssociativeWith(root, args[0], parenthesis); if (lhsPrecedence === null) { //if the left hand side has no defined precedence, no parens are needed //FunctionNode for example lhsParens = false; } else if ((lhsPrecedence === precedence) && (associativity === 'right') && !assocWithLhs) { //In case of equal precedence, if the root node is left associative // parens are **never** necessary for the left hand side. //If it is right associative however, parens are necessary //if the root node isn't associative with the left hand side lhsParens = true; } else if (lhsPrecedence < precedence) { lhsParens = true; } else { lhsParens = false; } var rhsParens; //right hand side needs parenthesis? //precedence of the right hand side var rhsPrecedence = operators.getPrecedence(args[1], parenthesis); //is the root node associative with the right hand side? var assocWithRhs = operators.isAssociativeWith(root, args[1], parenthesis); if (rhsPrecedence === null) { //if the right hand side has no defined precedence, no parens are needed //FunctionNode for example rhsParens = false; } else if ((rhsPrecedence === precedence) && (associativity === 'left') && !assocWithRhs) { //In case of equal precedence, if the root node is right associative // parens are **never** necessary for the right hand side. //If it is left associative however, parens are necessary //if the root node isn't associative with the right hand side rhsParens = true; } else if (rhsPrecedence < precedence) { rhsParens = true; } else { rhsParens = false; } //handle special cases for LaTeX, where some of the parentheses aren't needed if (latex) { var rootIdentifier; var lhsIdentifier; var rhsIdentifier; if (parenthesis === 'keep') { rootIdentifier = root.getIdentifier(); lhsIdentifier = root.args[0].getIdentifier(); rhsIdentifier = root.args[1].getIdentifier(); } else { //Ignore ParenthesisNodes when not in 'keep' mode rootIdentifier = root.getContent().getIdentifier(); lhsIdentifier = root.args[0].getContent().getIdentifier(); rhsIdentifier = root.args[1].getContent().getIdentifier(); } if (lhsPrecedence !== null) { if (operators.properties[precedence][rootIdentifier].latexLeftParens === false) { lhsParens = false; } if (operators.properties[lhsPrecedence][lhsIdentifier].latexParens === false) { lhsParens = false; } } if (rhsPrecedence !== null) { if (operators.properties[precedence][rootIdentifier].latexRightParens === false) { rhsParens = false; } if (operators.properties[rhsPrecedence][rhsIdentifier].latexParens === false) { rhsParens = false; } } } return [lhsParens, rhsParens]; } else if ((args.length > 2) && ((root.getIdentifier() === 'OperatorNode:add') || (root.getIdentifier() === 'OperatorNode:multiply'))) { var parensArray = args.map(function (arg) { var argPrecedence = operators.getPrecedence(arg, parenthesis); var assocWithArg = operators.isAssociativeWith(root, arg, parenthesis); var argAssociativity = operators.getAssociativity(arg, parenthesis); if (argPrecedence === null) { //if the argument has no defined precedence, no parens are needed return false; } else if ((precedence === argPrecedence) && (associativity === argAssociativity) && !assocWithArg) { return true; } else if (argPrecedence < precedence) { return true; } return false; }); return parensArray; } } /** * Get string representation. * @param {Object} options * @return {string} str */ OperatorNode.prototype._toString = function (options) { var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'; var implicit = (options && options.implicit) ? options.implicit : 'hide'; var args = this.args; var parens = calculateNecessaryParentheses(this, parenthesis, args, false); if (args.length === 1) { //unary operators var assoc = operators.getAssociativity(this, parenthesis); var operand = args[0].toString(options); if (parens[0]) { operand = '(' + operand + ')'; } if (assoc === 'right') { //prefix operator return this.op + operand; } else if (assoc === 'left') { //postfix return operand + this.op; } //fall back to postfix return operand + this.op; } else if (args.length == 2) { var lhs = args[0].toString(options); //left hand side var rhs = args[1].toString(options); //right hand side if (parens[0]) { //left hand side in parenthesis? lhs = '(' + lhs + ')'; } if (parens[1]) { //right hand side in parenthesis? rhs = '(' + rhs + ')'; } if (this.implicit && (this.getIdentifier() === 'OperatorNode:multiply') && (implicit == 'hide')) { return lhs + ' ' + rhs; } return lhs + ' ' + this.op + ' ' + rhs; } else if ((args.length > 2) && ((this.getIdentifier() === 'OperatorNode:add') || (this.getIdentifier() === 'OperatorNode:multiply'))) { var stringifiedArgs = args.map(function (arg, index) { arg = arg.toString(options); if (parens[index]) { //put in parenthesis? arg = '(' + arg + ')'; } return arg; }); if (this.implicit && (this.getIdentifier() === 'OperatorNode:multiply') && (implicit === 'hide')) { return stringifiedArgs.join(' '); } return stringifiedArgs.join(' ' + this.op + ' '); } else { //fallback to formatting as a function call return this.fn + '(' + this.args.join(', ') + ')'; } }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ OperatorNode.prototype._toTex = function (options) { var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'; var implicit = (options && options.implicit) ? options.implicit : 'hide'; var args = this.args; var parens = calculateNecessaryParentheses(this, parenthesis, args, true); var op = latex.operators[this.fn]; op = typeof op === 'undefined' ? this.op : op; //fall back to using this.op if (args.length === 1) { //unary operators var assoc = operators.getAssociativity(this, parenthesis); var operand = args[0].toTex(options); if (parens[0]) { operand = '\\left(' + operand + '\\right)'; } if (assoc === 'right') { //prefix operator return op + operand; } else if (assoc === 'left') { //postfix operator return operand + op; } //fall back to postfix return operand + op; } else if (args.length === 2) { //binary operators var lhs = args[0]; //left hand side var lhsTex = lhs.toTex(options); if (parens[0]) { lhsTex = '\\left(' + lhsTex + '\\right)'; } var rhs = args[1]; //right hand side var rhsTex = rhs.toTex(options); if (parens[1]) { rhsTex = '\\left(' + rhsTex + '\\right)'; } //handle some exceptions (due to the way LaTeX works) var lhsIdentifier; if (parenthesis === 'keep') { lhsIdentifier = lhs.getIdentifier(); } else { //Ignore ParenthesisNodes if in 'keep' mode lhsIdentifier = lhs.getContent().getIdentifier(); } switch (this.getIdentifier()) { case 'OperatorNode:divide': //op contains '\\frac' at this point return op + '{' + lhsTex + '}' + '{' + rhsTex + '}'; case 'OperatorNode:pow': lhsTex = '{' + lhsTex + '}'; rhsTex = '{' + rhsTex + '}'; switch (lhsIdentifier) { case 'ConditionalNode': // case 'OperatorNode:divide': lhsTex = '\\left(' + lhsTex + '\\right)'; } case 'OperatorNode:multiply': if (this.implicit && (implicit === 'hide')) { return lhsTex + '~' + rhsTex; } } return lhsTex + op + rhsTex; } else if ((args.length > 2) && ((this.getIdentifier() === 'OperatorNode:add') || (this.getIdentifier() === 'OperatorNode:multiply'))) { var texifiedArgs = args.map(function (arg, index) { arg = arg.toTex(options); if (parens[index]) { arg = '\\left(' + arg + '\\right)'; } return arg; }); if ((this.getIdentifier() === 'OperatorNode:multiply') && this.implicit) { return texifiedArgs.join('~'); } return texifiedArgs.join(op) } else { //fall back to formatting as a function call //as this is a fallback, it doesn't use //fancy function names return '\\mathrm{' + this.fn + '}\\left(' + args.map(function (arg) { return arg.toTex(options); }).join(',') + '\\right)'; } }; /** * Get identifier. * @return {string} */ OperatorNode.prototype.getIdentifier = function () { return this.type + ':' + this.fn; }; return OperatorNode; } exports.name = 'OperatorNode'; exports.path = 'expression.node'; exports.math = true; // request access to the math namespace as 5th argument of the factory function exports.factory = factory; /***/ }), /* 49 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); /** * @constructor ParenthesisNode * @extends {Node} * A parenthesis node describes manual parenthesis from the user input * @param {Node} content * @extends {Node} */ function ParenthesisNode(content) { if (!(this instanceof ParenthesisNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } // validate input if (!(content && content.isNode)) { throw new TypeError('Node expected for parameter "content"'); } this.content = content; } ParenthesisNode.prototype = new Node(); ParenthesisNode.prototype.type = 'ParenthesisNode'; ParenthesisNode.prototype.isParenthesisNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ ParenthesisNode.prototype._compile = function (defs, args) { return this.content._compile(defs, args); }; /** * Get the content of the current Node. * @return {Node} content * @override **/ ParenthesisNode.prototype.getContent = function () { return this.content.getContent(); }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ ParenthesisNode.prototype.forEach = function (callback) { callback(this.content, 'content', this); }; /** * Create a new ParenthesisNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node) : Node} callback * @returns {ParenthesisNode} Returns a clone of the node */ ParenthesisNode.prototype.map = function (callback) { var content = callback(this.content, 'content', this); return new ParenthesisNode(content); }; /** * Create a clone of this node, a shallow copy * @return {ParenthesisNode} */ ParenthesisNode.prototype.clone = function() { return new ParenthesisNode(this.content); }; /** * Get string representation * @param {Object} options * @return {string} str * @override */ ParenthesisNode.prototype._toString = function(options) { if ((!options) || (options && !options.parenthesis) || (options && options.parenthesis === 'keep')) { return '(' + this.content.toString(options) + ')'; } return this.content.toString(options); }; /** * Get LaTeX representation * @param {Object} options * @return {string} str * @override */ ParenthesisNode.prototype._toTex = function(options) { if ((!options) || (options && !options.parenthesis) || (options && options.parenthesis === 'keep')) { return '\\left(' + this.content.toTex(options) + '\\right)'; } return this.content.toTex(options); }; return ParenthesisNode; } exports.name = 'ParenthesisNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 50 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the square root of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.sqrt(x) * * Examples: * * math.sqrt(25); // returns 5 * math.square(5); // returns 25 * math.sqrt(-4); // returns Complex 2i * * See also: * * square, multiply, cube, cbrt * * @param {number | BigNumber | Complex | Array | Matrix | Unit} x * Value for which to calculate the square root. * @return {number | BigNumber | Complex | Array | Matrix | Unit} * Returns the square root of `x` */ var sqrt = typed('sqrt', { 'number': _sqrtNumber, 'Complex': function (x) { return x.sqrt(); }, 'BigNumber': function (x) { if (!x.isNegative() || config.predictable) { return x.sqrt(); } else { // negative value -> downgrade to number to do complex value computation return _sqrtNumber(x.toNumber()); } }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since sqrt(0) = 0 return deepMap(x, sqrt, true); }, 'Unit': function (x) { // Someday will work for complex units when they are implemented return x.pow(0.5); } }); /** * Calculate sqrt for a number * @param {number} x * @returns {number | Complex} Returns the square root of x * @private */ function _sqrtNumber(x) { if (x >= 0 || config.predictable) { return Math.sqrt(x); } else { return new type.Complex(x, 0).sqrt(); } } sqrt.toTex = {1: '\\sqrt{${args[0]}}'}; return sqrt; } exports.name = 'sqrt'; exports.factory = factory; /***/ }), /* 51 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var clone = __webpack_require__(4).clone; var validateIndex = __webpack_require__(7).validateIndex; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Get or set a subset of a matrix or string. * * Syntax: * math.subset(value, index) // retrieve a subset * math.subset(value, index, replacement [, defaultValue]) // replace a subset * * Examples: * * // get a subset * var d = [[1, 2], [3, 4]]; * math.subset(d, math.index(1, 0)); // returns 3 * math.subset(d, math.index([0, 2], 1)); // returns [[2], [4]] * * // replace a subset * var e = []; * var f = math.subset(e, math.index(0, [0, 2]), [5, 6]); // f = [[5, 6]] * var g = math.subset(f, math.index(1, 1), 7, 0); // g = [[5, 6], [0, 7]] * * See also: * * size, resize, squeeze, index * * @param {Array | Matrix | string} matrix An array, matrix, or string * @param {Index} index An index containing ranges for each * dimension * @param {*} [replacement] An array, matrix, or scalar. * If provided, the subset is replaced with replacement. * If not provided, the subset is returned * @param {*} [defaultValue=undefined] Default value, filled in on new entries when * the matrix is resized. If not provided, * math.matrix elements will be left undefined. * @return {Array | Matrix | string} Either the retrieved subset or the updated matrix. */ var subset = typed('subset', { // get subset 'Array, Index': function (value, index) { var m = matrix(value); var subset = m.subset(index); // returns a Matrix return subset && subset.valueOf(); // return an Array (like the input) }, 'Matrix, Index': function (value, index) { return value.subset(index); }, 'Object, Index': _getObjectProperty, 'string, Index': _getSubstring, // set subset 'Array, Index, any': function (value, index, replacement) { return matrix(clone(value)) .subset(index, replacement, undefined) .valueOf(); }, 'Array, Index, any, any': function (value, index, replacement, defaultValue) { return matrix(clone(value)) .subset(index, replacement, defaultValue) .valueOf(); }, 'Matrix, Index, any': function (value, index, replacement) { return value.clone().subset(index, replacement); }, 'Matrix, Index, any, any': function (value, index, replacement, defaultValue) { return value.clone().subset(index, replacement, defaultValue); }, 'string, Index, string': _setSubstring, 'string, Index, string, string': _setSubstring, 'Object, Index, any': _setObjectProperty }); subset.toTex = undefined; // use default template return subset; /** * Retrieve a subset of a string * @param {string} str string from which to get a substring * @param {Index} index An index containing ranges for each dimension * @returns {string} substring * @private */ function _getSubstring(str, index) { if (!index || index.isIndex !== true) { // TODO: better error message throw new TypeError('Index expected'); } if (index.size().length != 1) { throw new DimensionError(index.size().length, 1); } // validate whether the range is out of range var strLen = str.length; validateIndex(index.min()[0], strLen); validateIndex(index.max()[0], strLen); var range = index.dimension(0); var substr = ''; range.forEach(function (v) { substr += str.charAt(v); }); return substr; } /** * Replace a substring in a string * @param {string} str string to be replaced * @param {Index} index An index containing ranges for each dimension * @param {string} replacement Replacement string * @param {string} [defaultValue] Default value to be uses when resizing * the string. is ' ' by default * @returns {string} result * @private */ function _setSubstring(str, index, replacement, defaultValue) { if (!index || index.isIndex !== true) { // TODO: better error message throw new TypeError('Index expected'); } if (index.size().length != 1) { throw new DimensionError(index.size().length, 1); } if (defaultValue !== undefined) { if (typeof defaultValue !== 'string' || defaultValue.length !== 1) { throw new TypeError('Single character expected as defaultValue'); } } else { defaultValue = ' '; } var range = index.dimension(0); var len = range.size()[0]; if (len != replacement.length) { throw new DimensionError(range.size()[0], replacement.length); } // validate whether the range is out of range var strLen = str.length; validateIndex(index.min()[0]); validateIndex(index.max()[0]); // copy the string into an array with characters var chars = []; for (var i = 0; i < strLen; i++) { chars[i] = str.charAt(i); } range.forEach(function (v, i) { chars[v] = replacement.charAt(i[0]); }); // initialize undefined characters with a space if (chars.length > strLen) { for (i = strLen - 1, len = chars.length; i < len; i++) { if (!chars[i]) { chars[i] = defaultValue; } } } return chars.join(''); } } /** * Retrieve a property from an object * @param {Object} object * @param {Index} index * @return {*} Returns the value of the property * @private */ function _getObjectProperty (object, index) { if (index.size().length !== 1) { throw new DimensionError(index.size(), 1); } var key = index.dimension(0); if (typeof key !== 'string') { throw new TypeError('String expected as index to retrieve an object property'); } return object[key]; } /** * Set a property on an object * @param {Object} object * @param {Index} index * @param {*} replacement * @return {*} Returns the updated object * @private */ function _setObjectProperty (object, index, replacement) { if (index.size().length !== 1) { throw new DimensionError(index.size(), 1); } var key = index.dimension(0); if (typeof key !== 'string') { throw new TypeError('String expected as index to retrieve an object property'); } // clone the object, and apply the property to the clone var updated = clone(object); updated[key] = replacement; return updated; } exports.name = 'subset'; exports.factory = factory; /***/ }), /* 52 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var clone = __webpack_require__(4).clone; var format = __webpack_require__(19).format; function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var DenseMatrix = type.DenseMatrix, SparseMatrix = type.SparseMatrix; /** * Transpose a matrix. All values of the matrix are reflected over its * main diagonal. Only applicable to two dimensional matrices containing * a vector (i.e. having size `[1,n]` or `[n,1]`). One dimensional * vectors and scalars return the input unchanged. * * Syntax: * * math.transpose(x) * * Examples: * * var A = [[1, 2, 3], [4, 5, 6]]; * math.transpose(A); // returns [[1, 4], [2, 5], [3, 6]] * * See also: * * diag, inv, subset, squeeze * * @param {Array | Matrix} x Matrix to be transposed * @return {Array | Matrix} The transposed matrix */ var transpose = typed('transpose', { 'Array': function (x) { // use dense matrix implementation return transpose(matrix(x)).valueOf(); }, 'Matrix': function (x) { // matrix size var size = x.size(); // result var c; // process dimensions switch (size.length) { case 1: // vector c = x.clone(); break; case 2: // rows and columns var rows = size[0]; var columns = size[1]; // check columns if (columns === 0) { // throw exception throw new RangeError('Cannot transpose a 2D matrix with no columns (size: ' + format(size) + ')'); } // process storage format switch (x.storage()) { case 'dense': c = _denseTranspose(x, rows, columns); break; case 'sparse': c = _sparseTranspose(x, rows, columns); break; } break; default: // multi dimensional throw new RangeError('Matrix must be a vector or two dimensional (size: ' + format(this._size) + ')'); } return c; }, // scalars 'any': function (x) { return clone(x); } }); var _denseTranspose = function (m, rows, columns) { // matrix array var data = m._data; // transposed matrix data var transposed = []; var transposedRow; // loop columns for (var j = 0; j < columns; j++) { // initialize row transposedRow = transposed[j] = []; // loop rows for (var i = 0; i < rows; i++) { // set data transposedRow[i] = clone(data[i][j]); } } // return matrix return new DenseMatrix({ data: transposed, size: [columns, rows], datatype: m._datatype }); }; var _sparseTranspose = function (m, rows, columns) { // matrix arrays var values = m._values; var index = m._index; var ptr = m._ptr; // result matrices var cvalues = values ? [] : undefined; var cindex = []; var cptr = []; // row counts var w = []; for (var x = 0; x < rows; x++) w[x] = 0; // vars var p, l, j; // loop values in matrix for (p = 0, l = index.length; p < l; p++) { // number of values in row w[index[p]]++; } // cumulative sum var sum = 0; // initialize cptr with the cummulative sum of row counts for (var i = 0; i < rows; i++) { // update cptr cptr.push(sum); // update sum sum += w[i]; // update w w[i] = cptr[i]; } // update cptr cptr.push(sum); // loop columns for (j = 0; j < columns; j++) { // values & index in column for (var k0 = ptr[j], k1 = ptr[j + 1], k = k0; k < k1; k++) { // C values & index var q = w[index[k]]++; // C[j, i] = A[i, j] cindex[q] = j; // check we need to process values (pattern matrix) if (values) cvalues[q] = clone(values[k]); } } // return matrix return new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [columns, rows], datatype: m._datatype }); }; transpose.toTex = {1: '\\left(${args[0]}\\right)' + latex.operators['transpose']}; return transpose; } exports.name = 'transpose'; exports.factory = factory; /***/ }), /* 53 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { /** * Compute the number of ways of picking `k` unordered outcomes from `n` * possibilities. * * Combinations only takes integer arguments. * The following condition must be enforced: k <= n. * * Syntax: * * math.combinations(n, k) * * Examples: * * math.combinations(7, 5); // returns 21 * * See also: * * permutations, factorial * * @param {number | BigNumber} n Total number of objects in the set * @param {number | BigNumber} k Number of objects in the subset * @return {number | BigNumber} Number of possible combinations. */ var combinations = typed('combinations', { 'number, number': function (n, k) { var max, result, i; if (!isInteger(n) || n < 0) { throw new TypeError('Positive integer value expected in function combinations'); } if (!isInteger(k) || k < 0) { throw new TypeError('Positive integer value expected in function combinations'); } if (k > n) { throw new TypeError('k must be less than or equal to n'); } max = Math.max(k, n - k); result = 1; for (i = 1; i <= n - max; i++) { result = result * (max + i) / i; } return result; }, 'BigNumber, BigNumber': function (n, k) { var max, result, i, ii; var one = new type.BigNumber(1); if (!isPositiveInteger(n) || !isPositiveInteger(k)) { throw new TypeError('Positive integer value expected in function combinations'); } if (k.gt(n)) { throw new TypeError('k must be less than n in function combinations'); } max = n.minus(k); if (k.lt(max)) max = k; result = one; for (i = one, ii = n.minus(max); i.lte(ii); i = i.plus(1)) { result = result.times(max.plus(i)).dividedBy(i); } return result; } // TODO: implement support for collection in combinations }); combinations.toTex = {2: '\\binom{${args[0]}}{${args[1]}}'}; return combinations; } /** * Test whether BigNumber n is a positive integer * @param {BigNumber} n * @returns {boolean} isPositiveInteger */ function isPositiveInteger(n) { return n.isInteger() && n.gte(0); } exports.name = 'combinations'; exports.factory = factory; /***/ }), /* 54 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var gamma = load(__webpack_require__(112)); var latex = __webpack_require__(3); /** * Compute the factorial of a value * * Factorial only supports an integer value as argument. * For matrices, the function is evaluated element wise. * * Syntax: * * math.factorial(n) * * Examples: * * math.factorial(5); // returns 120 * math.factorial(3); // returns 6 * * See also: * * combinations, gamma, permutations * * @param {number | BigNumber | Array | Matrix} n An integer number * @return {number | BigNumber | Array | Matrix} The factorial of `n` */ var factorial = typed('factorial', { 'number': function (n) { if (n < 0) { throw new Error('Value must be non-negative'); } return gamma(n + 1); }, 'BigNumber': function (n) { if (n.isNegative()) { throw new Error('Value must be non-negative'); } return gamma(n.plus(1)); }, 'Array | Matrix': function (n) { return deepMap(n, factorial); } }); factorial.toTex = { 1: '\\left(${args[0]}\\right)' + latex.operators['factorial'] }; return factorial; } exports.name = 'factorial'; exports.factory = factory; /***/ }), /* 55 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Test whether a value is positive: larger than zero. * The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`. * * The function is evaluated element-wise in case of Array or Matrix input. * * Syntax: * * math.isPositive(x) * * Examples: * * math.isPositive(3); // returns true * math.isPositive(-2); // returns false * math.isPositive(0); // returns false * math.isPositive(-0); // returns false * math.isPositive(0.5); // returns true * math.isPositive(math.bignumber(2)); // returns true * math.isPositive(math.fraction(-2, 5)); // returns false * math.isPositive(math.fraction(1,3)); // returns false * math.isPositive('2'); // returns true * math.isPositive([2, 0, -3]'); // returns [true, false, false] * * See also: * * isNumeric, isZero, isNegative, isInteger * * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested * @return {boolean} Returns true when `x` is larger than zero. * Throws an error in case of an unknown data type. */ var isPositive = typed('isPositive', { 'number': function (x) { return x > 0; }, 'BigNumber': function (x) { return !x.isNeg() && !x.isZero() && !x.isNaN(); }, 'Fraction': function (x) { return x.s > 0 && x.n > 0; }, 'Unit': function (x) { return isPositive(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, isPositive); } }); return isPositive; } exports.name = 'isPositive'; exports.factory = factory; /***/ }), /* 56 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var equalScalar = load(__webpack_require__(8)); var SparseMatrix = type.SparseMatrix; /** * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). * Callback function invoked MAX(NNZA, NNZB) times * * * ┌ f(Aij, Bij) ; A(i,j) !== 0 || B(i,j) !== 0 * C(i,j) = ┤ * └ 0 ; otherwise * * * @param {Matrix} a The SparseMatrix instance (A) * @param {Matrix} b The SparseMatrix instance (B) * @param {Function} callback The f(Aij,Bij) operation to invoke * * @return {Matrix} SparseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 */ var algorithm05 = function (a, b, callback) { // sparse matrix arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var asize = a._size; var adt = a._datatype; // sparse matrix arrays var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; var bsize = b._size; var bdt = b._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // equal signature to use var eq = equalScalar; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // find signature that matches (dt, dt) eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cvalues = avalues && bvalues ? [] : undefined; var cindex = []; var cptr = []; // matrix var c = new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: dt }); // workspaces var xa = cvalues ? [] : undefined; var xb = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column var wa = []; var wb = []; // vars var i, j, k, k1; // loop columns for (j = 0; j < columns; j++) { // update cptr cptr[j] = cindex.length; // columns mark var mark = j + 1; // loop values A(:,j) for (k = aptr[j], k1 = aptr[j + 1]; k < k1; k++) { // row i = aindex[k]; // push index cindex.push(i); // update workspace wa[i] = mark; // check we need to process values if (xa) xa[i] = avalues[k]; } // loop values B(:,j) for (k = bptr[j], k1 = bptr[j + 1]; k < k1; k++) { // row i = bindex[k]; // check row existed in A if (wa[i] !== mark) { // push index cindex.push(i); } // update workspace wb[i] = mark; // check we need to process values if (xb) xb[i] = bvalues[k]; } // check we need to process values (non pattern matrix) if (cvalues) { // initialize first index in j k = cptr[j]; // loop index in j while (k < cindex.length) { // row i = cindex[k]; // marks var wai = wa[i]; var wbi = wb[i]; // check Aij or Bij are nonzero if (wai === mark || wbi === mark) { // matrix values @ i,j var va = wai === mark ? xa[i] : zero; var vb = wbi === mark ? xb[i] : zero; // Cij var vc = cf(va, vb); // check for zero if (!eq(vc, zero)) { // push value cvalues.push(vc); // increment pointer k++; } else { // remove value @ i, do not increment pointer cindex.splice(k, 1); } } } } } // update cptr cptr[columns] = cindex.length; // return sparse matrix return c; }; return algorithm05; } exports.name = 'algorithm05'; exports.factory = factory; /***/ }), /* 57 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var scatter = __webpack_require__(502); var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var equalScalar = load(__webpack_require__(8)); var SparseMatrix = type.SparseMatrix; /** * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). * Callback function invoked (Anz U Bnz) times, where Anz and Bnz are the nonzero elements in both matrices. * * * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0 * C(i,j) = ┤ * └ 0 ; otherwise * * * @param {Matrix} a The SparseMatrix instance (A) * @param {Matrix} b The SparseMatrix instance (B) * @param {Function} callback The f(Aij,Bij) operation to invoke * * @return {Matrix} SparseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 */ var algorithm06 = function (a, b, callback) { // sparse matrix arrays var avalues = a._values; var asize = a._size; var adt = a._datatype; // sparse matrix arrays var bvalues = b._values; var bsize = b._size; var bdt = b._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // equal signature to use var eq = equalScalar; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // find signature that matches (dt, dt) eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cvalues = avalues && bvalues ? [] : undefined; var cindex = []; var cptr = []; // matrix var c = new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: dt }); // workspaces var x = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column var w = []; // marks indicating value in a given row has been updated var u = []; // loop columns for (var j = 0; j < columns; j++) { // update cptr cptr[j] = cindex.length; // columns mark var mark = j + 1; // scatter the values of A(:,j) into workspace scatter(a, j, w, x, u, mark, c, cf); // scatter the values of B(:,j) into workspace scatter(b, j, w, x, u, mark, c, cf); // check we need to process values (non pattern matrix) if (x) { // initialize first index in j var k = cptr[j]; // loop index in j while (k < cindex.length) { // row var i = cindex[k]; // check function was invoked on current row (Aij !=0 && Bij != 0) if (u[i] === mark) { // value @ i var v = x[i]; // check for zero value if (!eq(v, zero)) { // push value cvalues.push(v); // increment pointer k++; } else { // remove value @ i, do not increment pointer cindex.splice(k, 1); } } else { // remove value @ i, do not increment pointer cindex.splice(k, 1); } } } else { // initialize first index in j var p = cptr[j]; // loop index in j while (p < cindex.length) { // row var r = cindex[p]; // check function was invoked on current row (Aij !=0 && Bij != 0) if (u[r] !== mark) { // remove value @ i, do not increment pointer cindex.splice(p, 1); } else { // increment pointer p++; } } } } // update cptr cptr[columns] = cindex.length; // return sparse matrix return c; }; return algorithm06; } exports.name = 'algorithm06'; exports.factory = factory; /***/ }), /* 58 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isCollection = __webpack_require__(40); /** * Test whether an array contains collections * @param {Array} array * @returns {boolean} Returns true when the array contains one or multiple * collections (Arrays or Matrices). Returns false otherwise. */ module.exports = function containsCollections (array) { for (var i = 0; i < array.length; i++) { if (isCollection(array[i])) { return true; } } return false; }; /***/ }), /* 59 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var arraySize = __webpack_require__(7).size; var IndexError = __webpack_require__(42); /** * Reduce a given matrix or array to a new matrix or * array with one less dimension, applying the given * callback in the selected dimension. * @param {Array | Matrix} mat * @param {number} dim * @param {Function} callback * @return {Array | Matrix} res */ module.exports = function(mat, dim, callback) { var size = Array.isArray(mat) ? arraySize(mat) : mat.size(); if (dim < 0 || (dim >= size.length)) { // TODO: would be more clear when throwing a DimensionError here throw new IndexError(dim, size.length); } if (mat && mat.isMatrix === true) { return mat.create(_reduce(mat.valueOf(), dim, callback)); }else { return _reduce(mat, dim, callback); } }; /** * Recursively reduce a matrix * @param {Array} mat * @param {number} dim * @param {Function} callback * @returns {Array} ret * @private */ function _reduce(mat, dim, callback){ var i, ret, val, tran; if(dim<=0){ if( !Array.isArray(mat[0]) ){ val = mat[0]; for(i=1; i 3) throw new Error('Too many arguments'); this.start = start; // included lower-bound this.end = end; // included upper-bound this.step = step || null; // optional step } RangeNode.prototype = new Node(); RangeNode.prototype.type = 'RangeNode'; RangeNode.prototype.isRangeNode = true; /** * Check whether the RangeNode needs the `end` symbol to be defined. * This end is the size of the Matrix in current dimension. * @return {boolean} */ RangeNode.prototype.needsEnd = function () { // find all `end` symbols in this RangeNode var endSymbols = this.filter(function (node) { return (node && node.isSymbolNode) && (node.name == 'end'); }); return endSymbols.length > 0; }; /** * Compile the node to javascript code * * When the range has a symbol `end` defined, the RangeNode requires * a variable `end` to be defined in the current closure, which must contain * the length of the of the matrix that's being handled in the range's * dimension. To check whether the `end` variable is needed, call * RangeNode.needsEnd(). * * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ RangeNode.prototype._compile = function (defs, args) { return 'math.range(' + this.start._compile(defs, args) + ', ' + this.end._compile(defs, args) + (this.step ? (', ' + this.step._compile(defs, args)) : '') + ')'; }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ RangeNode.prototype.forEach = function (callback) { callback(this.start, 'start', this); callback(this.end, 'end', this); if (this.step) { callback(this.step, 'step', this); } }; /** * Create a new RangeNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {RangeNode} Returns a transformed copy of the node */ RangeNode.prototype.map = function (callback) { return new RangeNode( this._ifNode(callback(this.start, 'start', this)), this._ifNode(callback(this.end, 'end', this)), this.step && this._ifNode(callback(this.step, 'step', this)) ); }; /** * Create a clone of this node, a shallow copy * @return {RangeNode} */ RangeNode.prototype.clone = function () { return new RangeNode(this.start, this.end, this.step && this.step); }; /** * Calculate the necessary parentheses * @param {Node} node * @param {string} parenthesis * @return {Object} parentheses * @private */ function calculateNecessaryParentheses(node, parenthesis) { var precedence = operators.getPrecedence(node, parenthesis); var parens = {}; var startPrecedence = operators.getPrecedence(node.start, parenthesis); parens.start = ((startPrecedence !== null) && (startPrecedence <= precedence)) || (parenthesis === 'all'); if (node.step) { var stepPrecedence = operators.getPrecedence(node.step, parenthesis); parens.step = ((stepPrecedence !== null) && (stepPrecedence <= precedence)) || (parenthesis === 'all'); } var endPrecedence = operators.getPrecedence(node.end, parenthesis); parens.end = ((endPrecedence !== null) && (endPrecedence <= precedence)) || (parenthesis === 'all'); return parens; } /** * Get string representation * @param {Object} options * @return {string} str */ RangeNode.prototype._toString = function (options) { var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'; var parens = calculateNecessaryParentheses(this, parenthesis); //format string as start:step:stop var str; var start = this.start.toString(options); if (parens.start) { start = '(' + start + ')'; } str = start; if (this.step) { var step = this.step.toString(options); if (parens.step) { step = '(' + step + ')'; } str += ':' + step; } var end = this.end.toString(options); if (parens.end) { end = '(' + end + ')'; } str += ':' + end; return str; }; /** * Get LaTeX representation * @params {Object} options * @return {string} str */ RangeNode.prototype._toTex = function (options) { var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'; var parens = calculateNecessaryParentheses(this, parenthesis); var str = this.start.toTex(options); if (parens.start) { str = '\\left(' + str + '\\right)'; } if (this.step) { var step = this.step.toTex(options); if (parens.step) { step = '\\left(' + step + '\\right)'; } str += ':' + step; } var end = this.end.toTex(options); if (parens.end) { end = '\\left(' + end + '\\right)'; } str += ':' + end; return str; }; return RangeNode; } exports.name = 'RangeNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 64 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var string = util.string; var array = util.array; var isArray = Array.isArray; function factory (type) { var DenseMatrix = type.DenseMatrix; /** * Validates matrix and column vector b for backward/forward substitution algorithms. * * @param {Matrix} m An N x N matrix * @param {Array | Matrix} b A column vector * @param {Boolean} copy Return a copy of vector b * * @return {DenseMatrix} Dense column vector b */ var solveValidation = function (m, b, copy) { // matrix size var size = m.size(); // validate matrix dimensions if (size.length !== 2) throw new RangeError('Matrix must be two dimensional (size: ' + string.format(size) + ')'); // rows & columns var rows = size[0]; var columns = size[1]; // validate rows & columns if (rows !== columns) throw new RangeError('Matrix must be square (size: ' + string.format(size) + ')'); // vars var data, i, bdata; // check b is matrix if (b && b.isMatrix === true) { // matrix size var msize = b.size(); // vector if (msize.length === 1) { // check vector length if (msize[0] !== rows) throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); // create data array data = []; // matrix data (DenseMatrix) bdata = b._data; // loop b data for (i = 0; i < rows; i++) { // row array data[i] = [bdata[i]]; } // return Dense Matrix return new DenseMatrix({ data: data, size: [rows, 1], datatype: b._datatype }); } // two dimensions if (msize.length === 2) { // array must be a column vector if (msize[0] !== rows || msize[1] !== 1) throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); // check matrix type if (b.isDenseMatrix === true) { // check a copy is needed if (copy) { // create data array data = []; // matrix data (DenseMatrix) bdata = b._data; // loop b data for (i = 0; i < rows; i++) { // row array data[i] = [bdata[i][0]]; } // return Dense Matrix return new DenseMatrix({ data: data, size: [rows, 1], datatype: b._datatype }); } // b is already a column vector return b; } // create data array data = []; for (i = 0; i < rows; i++) data[i] = [0]; // sparse matrix arrays var values = b._values; var index = b._index; var ptr = b._ptr; // loop values in column 0 for (var k1 = ptr[1], k = ptr[0]; k < k1; k++) { // row i = index[k]; // add to data data[i][0] = values[k]; } // return Dense Matrix return new DenseMatrix({ data: data, size: [rows, 1], datatype: b._datatype }); } // throw error throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); } // check b is array if (isArray(b)) { // size var asize = array.size(b); // check matrix dimensions, vector if (asize.length === 1) { // check vector length if (asize[0] !== rows) throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); // create data array data = []; // loop b for (i = 0; i < rows; i++) { // row array data[i] = [b[i]]; } // return Dense Matrix return new DenseMatrix({ data: data, size: [rows, 1] }); } if (asize.length === 2) { // array must be a column vector if (asize[0] !== rows || asize[1] !== 1) throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); // create data array data = []; // loop b data for (i = 0; i < rows; i++) { // row array data[i] = [b[i][0]]; } // return Dense Matrix return new DenseMatrix({ data: data, size: [rows, 1] }); } // throw error throw new RangeError('Dimension mismatch. Matrix columns must match vector length.'); } }; return solveValidation; } exports.factory = factory; /***/ }), /* 65 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory () { /** * This function "flips" its input about the integer -1. * * @param {Number} i The value to flip * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_flip = function (i) { // flip the value return -i - 2; }; return cs_flip; } exports.name = 'cs_flip'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 66 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var array = __webpack_require__(7); var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Create a 2-dimensional identity matrix with size m x n or n x n. * The matrix has ones on the diagonal and zeros elsewhere. * * Syntax: * * math.eye(n) * math.eye(n, format) * math.eye(m, n) * math.eye(m, n, format) * math.eye([m, n]) * math.eye([m, n], format) * * Examples: * * math.eye(3); // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]] * math.eye(3, 2); // returns [[1, 0], [0, 1], [0, 0]] * * var A = [[1, 2, 3], [4, 5, 6]]; * math.eye(math.size(A)); // returns [[1, 0, 0], [0, 1, 0]] * * See also: * * diag, ones, zeros, size, range * * @param {...number | Matrix | Array} size The size for the matrix * @param {string} [format] The Matrix storage format * * @return {Matrix | Array | number} A matrix with ones on the diagonal. */ var eye = typed('eye', { '': function () { return (config.matrix === 'Matrix') ? matrix([]) : []; }, 'string': function (format) { return matrix(format); }, 'number | BigNumber': function (rows) { return _eye(rows, rows, config.matrix === 'Matrix' ? 'default' : undefined); }, 'number | BigNumber, string': function (rows, format) { return _eye(rows, rows, format); }, 'number | BigNumber, number | BigNumber': function (rows, cols) { return _eye(rows, cols, config.matrix === 'Matrix' ? 'default' : undefined); }, 'number | BigNumber, number | BigNumber, string': function (rows, cols, format) { return _eye(rows, cols, format); }, 'Array': function (size) { return _eyeVector(size); }, 'Array, string': function (size, format) { return _eyeVector(size, format); }, 'Matrix': function (size) { return _eyeVector(size.valueOf(), size.storage()); }, 'Matrix, string': function (size, format) { return _eyeVector(size.valueOf(), format); } }); eye.toTex = undefined; // use default template return eye; function _eyeVector (size, format) { switch (size.length) { case 0: return format ? matrix(format) : []; case 1: return _eye(size[0], size[0], format); case 2: return _eye(size[0], size[1], format); default: throw new Error('Vector containing two values expected'); } } /** * Create an identity matrix * @param {number | BigNumber} rows * @param {number | BigNumber} cols * @param {string} [format] * @returns {Matrix} * @private */ function _eye (rows, cols, format) { // BigNumber constructor with the right precision var Big = (rows && rows.isBigNumber === true) ? type.BigNumber : (cols && cols.isBigNumber === true) ? type.BigNumber : null; if (rows && rows.isBigNumber === true) rows = rows.toNumber(); if (cols && cols.isBigNumber === true) cols = cols.toNumber(); if (!isInteger(rows) || rows < 1) { throw new Error('Parameters in function eye must be positive integers'); } if (!isInteger(cols) || cols < 1) { throw new Error('Parameters in function eye must be positive integers'); } var one = Big ? new type.BigNumber(1) : 1; var defaultValue = Big ? new Big(0) : 0; var size = [rows, cols]; // check we need to return a matrix if (format) { // get matrix storage constructor var F = type.Matrix.storage(format); // create diagonal matrix (use optimized implementation for storage format) return F.diagonal(size, one, 0, defaultValue); } // create and resize array var res = array.resize([], size, defaultValue); // fill in ones on the diagonal var minimum = rows < cols ? rows : cols; // fill diagonal for (var d = 0; d < minimum; d++) { res[d][d] = one; } return res; } } exports.name = 'eye'; exports.factory = factory; /***/ }), /* 67 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var maxArgumentCount = __webpack_require__(33).maxArgumentCount; function factory (type, config, load, typed) { /** * Create a new matrix or array with the results of the callback function executed on * each entry of the matrix/array. * * Syntax: * * math.map(x, callback) * * Examples: * * math.map([1, 2, 3], function(value) { * return value * value; * }); // returns [1, 4, 9] * * See also: * * filter, forEach, sort * * @param {Matrix | Array} x The matrix to iterate on. * @param {Function} callback The callback method is invoked with three * parameters: the value of the element, the index * of the element, and the matrix being traversed. * @return {Matrix | array} Transformed map of x */ var map = typed('map', { 'Array, function': _map, 'Matrix, function': function (x, callback) { return x.map(callback); } }); map.toTex = undefined; // use default template return map; } /** * Map for a multi dimensional array * @param {Array} array * @param {Function} callback * @return {Array} * @private */ function _map (array, callback) { // figure out what number of arguments the callback function expects var args = maxArgumentCount(callback); var recurse = function (value, index) { if (Array.isArray(value)) { return value.map(function (child, i) { // we create a copy of the index array and append the new index value return recurse(child, index.concat(i)); }); } else { // invoke the callback function with the right number of arguments if (args === 1) { return callback(value); } else if (args === 2) { return callback(value, index); } else { // 3 or -1 return callback(value, index, array); } } }; return recurse(array, []); } exports.name = 'map'; exports.factory = factory; /***/ }), /* 68 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var asc = load(__webpack_require__(46)); function desc(a, b) { return -asc(a, b); } /** * Partition-based selection of an array or 1D matrix. * Will find the kth smallest value, and mutates the input array. * Uses Quickselect. * * Syntax: * * math.partitionSelect(x, k) * math.partitionSelect(x, k, compare) * * Examples: * * math.partitionSelect([5, 10, 1], 2); // returns 10 * math.partitionSelect(['C', 'B', 'A', 'D'], 1); // returns 'B' * * function sortByLength (a, b) { * return a.length - b.length; * } * math.partitionSelect(['Langdon', 'Tom', 'Sara'], 2, sortByLength); // returns 'Langdon' * * See also: * * sort * * @param {Matrix | Array} x A one dimensional matrix or array to sort * @param {Number} k The kth smallest value to be retrieved; zero-based index * @param {Function | 'asc' | 'desc'} [compare='asc'] * An optional comparator function. The function is called as * `compare(a, b)`, and must return 1 when a > b, -1 when a < b, * and 0 when a == b. * @return {*} Returns the kth lowest value. */ return typed('partitionSelect', { 'Array | Matrix, number': function (x, k) { return _partitionSelect(x, k, asc); }, 'Array | Matrix, number, string': function (x, k, compare) { if (compare === 'asc') { return _partitionSelect(x, k, asc); } else if (compare === 'desc') { return _partitionSelect(x, k, desc); } else { throw new Error('Compare string must be "asc" or "desc"'); } }, 'Array | Matrix, number, function': _partitionSelect }); function _partitionSelect(x, k, compare) { if (!isInteger(k) || k < 0) { throw new Error('k must be a non-negative integer'); } if (x && x.isMatrix) { var size = x.size(); if (size.length > 1) { throw new Error('Only one dimensional matrices supported'); } return quickSelect(x.valueOf(), k, compare); } if (Array.isArray(x)) { return quickSelect(x, k, compare); } } /** * Quickselect algorithm. * Code adapted from: * http://blog.teamleadnet.com/2012/07/quick-select-algorithm-find-kth-element.html * * @param {Array} arr * @param {Number} k * @param {Function} compare * @private */ function quickSelect(arr, k, compare) { if (k >= arr.length) { throw new Error('k out of bounds'); } var from = 0; var to = arr.length - 1; // if from == to we reached the kth element while (from < to) { var r = from; var w = to; var pivot = arr[Math.floor(Math.random() * (to - from + 1)) + from]; // stop if the reader and writer meets while (r < w) { // arr[r] >= pivot if (compare(arr[r], pivot) >= 0) { // put the large values at the end var tmp = arr[w]; arr[w] = arr[r]; arr[r] = tmp; --w; } else { // the value is smaller than the pivot, skip ++r; } } // if we stepped up (r++) we need to step one down (arr[r] > pivot) if (compare(arr[r], pivot) > 0) { --r; } // the r pointer is on the end of the first k elements if (k <= r) { to = r; } else { from = r + 1; } } return arr[k]; } } exports.name = 'partitionSelect'; exports.factory = factory; /***/ }), /* 69 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var ArgumentsError = __webpack_require__(41); var isCollection = __webpack_require__(40); var isNumber = __webpack_require__(2).isNumber; // TODO: rethink math.distribution // TODO: rework to a typed function function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var array = __webpack_require__(7); /** * Create a distribution object with a set of random functions for given * random distribution. * * Syntax: * * math.distribution(name) * * Examples: * * var normalDist = math.distribution('normal'); // create a normal distribution * normalDist.random(0, 10); // get a random value between 0 and 10 * * See also: * * random, randomInt, pickRandom * * @param {string} name Name of a distribution. Choose from 'uniform', 'normal'. * @return {Object} Returns a distribution object containing functions: * `random([size] [, min] [, max])`, * `randomInt([min] [, max])`, * `pickRandom(array)` */ function distribution(name) { if (!distributions.hasOwnProperty(name)) throw new Error('Unknown distribution ' + name); var args = Array.prototype.slice.call(arguments, 1), distribution = distributions[name].apply(this, args); return (function(distribution) { // This is the public API for all distributions var randFunctions = { random: function(arg1, arg2, arg3) { var size, min, max; if (arguments.length > 3) { throw new ArgumentsError('random', arguments.length, 0, 3); } else if (arguments.length === 1) { // `random(max)` or `random(size)` if (isCollection(arg1)) { size = arg1; } else { max = arg1; } } else if (arguments.length === 2) { // `random(min, max)` or `random(size, max)` if (isCollection(arg1)) { size = arg1; max = arg2; } else { min = arg1; max = arg2; } } else { // `random(size, min, max)` size = arg1; min = arg2; max = arg3; } // TODO: validate type of size if ((min !== undefined && !isNumber(min)) || (max !== undefined && !isNumber(max))) { throw new TypeError('Invalid argument in function random'); } if (max === undefined) max = 1; if (min === undefined) min = 0; if (size !== undefined) { var res = _randomDataForMatrix(size.valueOf(), min, max, _random); return (size && size.isMatrix === true) ? matrix(res) : res; } return _random(min, max); }, randomInt: typed({ 'number | Array': function(arg) { var min = 0; if (isCollection(arg)) { var size = arg; var max = 1; var res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt); return (size && size.isMatrix === true) ? matrix(res) : res; } else { var max = arg; return _randomInt(min, max); } }, 'number | Array, number': function(arg1, arg2) { if (isCollection(arg1)) { var size = arg1; var max = arg2; var min = 0; var res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt); return (size && size.isMatrix === true) ? matrix(res) : res; } else { var min = arg1; var max = arg2; return _randomInt(min, max); } }, 'Array, number, number': function(size, min, max) { var res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt); return (size && size.isMatrix === true) ? matrix(res) : res; } }), pickRandom: typed({ 'Array': function(possibles) { return _pickRandom(possibles); }, 'Array, number | Array': function(possibles, arg2) { var number, weights; if (Array.isArray(arg2)) { weights = arg2; } else if (isNumber(arg2)) { number = arg2; } else { throw new TypeError('Invalid argument in function pickRandom') } return _pickRandom(possibles, number, weights); }, 'Array, number | Array, Array | number': function(possibles, arg2, arg3) { var number, weights; if (Array.isArray(arg2)) { weights = arg2; number = arg3; } else { weights = arg3; number = arg2; } if (!Array.isArray(weights) || !isNumber(number)) { throw new TypeError('Invalid argument in function pickRandom'); } return _pickRandom(possibles, number, weights); } }) } var _pickRandom = function(possibles, number, weights) { var single = (typeof number === 'undefined'); if (single) { number = 1; } if (possibles && possibles.isMatrix === true) { possibles = possibles.valueOf(); // get Array } else if (!Array.isArray(possibles)) { throw new TypeError('Unsupported type of value in function pickRandom'); } if (array.size(possibles).length > 1) { throw new Error('Only one dimensional vectors supported'); } if (typeof weights !== 'undefined') { if (weights.length != possibles.length) { throw new Error('Weights must have the same length as possibles'); } var totalWeights = 0; for (var i = 0, len = weights.length; i < len; i++) { if (!isNumber(weights[i]) || weights[i] < 0) { throw new Error('Weights must be an array of positive numbers'); } totalWeights += weights[i]; } } var length = possibles.length; if (length == 0) { return []; } else if (number >= length) { return possibles; } var result = []; var pick; while (result.length < number) { if (typeof weights === 'undefined') { pick = possibles[Math.floor(Math.random() * length)]; } else { var randKey = Math.random() * totalWeights; for (var i = 0, len = possibles.length; i < len; i++) { randKey -= weights[i]; if (randKey < 0) { pick = possibles[i]; break; } } } if (result.indexOf(pick) == -1) { result.push(pick); } } return single ? result[0] : result; // TODO: add support for multi dimensional matrices } var _random = function(min, max) { return min + distribution() * (max - min); }; var _randomInt = function(min, max) { return Math.floor(min + distribution() * (max - min)); }; // This is a function for generating a random matrix recursively. var _randomDataForMatrix = function(size, min, max, randFunc) { var data = [], length, i; size = size.slice(0); if (size.length > 1) { for (var i = 0, length = size.shift(); i < length; i++) { data.push(_randomDataForMatrix(size, min, max, randFunc)); } } else { for (var i = 0, length = size.shift(); i < length; i++) { data.push(randFunc(min, max)); } } return data; }; return randFunctions; })(distribution); } // Each distribution is a function that takes no argument and when called returns // a number between 0 and 1. var distributions = { uniform: function() { return Math.random; }, // Implementation of normal distribution using Box-Muller transform // ref : http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform // We take : mean = 0.5, standard deviation = 1/6 // so that 99.7% values are in [0, 1]. normal: function() { return function() { var u1, u2, picked = -1; // We reject values outside of the interval [0, 1] // TODO: check if it is ok to do that? while (picked < 0 || picked > 1) { u1 = Math.random(); u2 = Math.random(); picked = 1/6 * Math.pow(-2 * Math.log(u1), 0.5) * Math.cos(2 * Math.PI * u2) + 0.5; } return picked; } } }; distribution.toTex = undefined; // use default template return distribution; } exports.name = 'distribution'; exports.factory = factory; /***/ }), /* 70 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var equalScalar = load(__webpack_require__(8)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); var latex = __webpack_require__(3); /** * Test whether two values are equal. * * The function tests whether the relative difference between x and y is * smaller than the configured epsilon. The function cannot be used to * compare values smaller than approximately 2.22e-16. * * For matrices, the function is evaluated element wise. * In case of complex numbers, x.re must equal y.re, and x.im must equal y.im. * * Values `null` and `undefined` are compared strictly, thus `null` is only * equal to `null` and nothing else, and `undefined` is only equal to * `undefined` and nothing else. * * Syntax: * * math.equal(x, y) * * Examples: * * math.equal(2 + 2, 3); // returns false * math.equal(2 + 2, 4); // returns true * * var a = math.unit('50 cm'); * var b = math.unit('5 m'); * math.equal(a, b); // returns true * * var c = [2, 5, 1]; * var d = [2, 7, 1]; * * math.equal(c, d); // returns [true, false, true] * math.deepEqual(c, d); // returns false * * math.equal(0, null); // returns false * * See also: * * unequal, smaller, smallerEq, larger, largerEq, compare, deepEqual * * @param {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} x First value to compare * @param {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} y Second value to compare * @return {boolean | Array | Matrix} Returns true when the compared values are equal, else returns false */ var equal = typed('equal', { 'any, any': function (x, y) { // strict equality for null and undefined? if (x === null) { return y === null; } if (y === null) { return x === null; } if (x === undefined) { return y === undefined; } if (y === undefined) { return x === undefined; } return equalScalar(x, y); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, equalScalar); break; default: // sparse + dense c = algorithm03(y, x, equalScalar, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, equalScalar, false); break; default: // dense + dense c = algorithm13(x, y, equalScalar); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return equal(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return equal(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return equal(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, equalScalar, false); break; default: c = algorithm14(x, y, equalScalar, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, equalScalar, true); break; default: c = algorithm14(y, x, equalScalar, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, equalScalar, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, equalScalar, true).valueOf(); } }); equal.toTex = { 2: '\\left(${args[0]}' + latex.operators['equal'] + '${args[1]}\\right)' }; return equal; } exports.name = 'equal'; exports.factory = factory; /***/ }), /* 71 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Test whether a value is an numeric value. * * The function is evaluated element-wise in case of Array or Matrix input. * * Syntax: * * math.isNumeric(x) * * Examples: * * math.isNumeric(2); // returns true * math.isNumeric(0); // returns true * math.isNumeric(math.bignumber(500)); // returns true * math.isNumeric(math.fraction(4)); // returns true * math.isNumeric(math.complex('2-4i'); // returns false * math.isNumeric('3'); // returns false * math.isNumeric([2.3, 'foo', false]); // returns [true, false, true] * * See also: * * isZero, isPositive, isNegative, isInteger * * @param {*} x Value to be tested * @return {boolean} Returns true when `x` is a `number`, `BigNumber`, * `Fraction`, or `boolean`. Returns false for other types. * Throws an error in case of unknown types. */ var isNumeric = typed('isNumeric', { 'number | BigNumber | Fraction | boolean': function () { return true; }, 'Complex | Unit | string': function () { return false; }, 'Array | Matrix': function (x) { return deepMap(x, isNumeric); } }); return isNumeric; } exports.name = 'isNumeric'; exports.factory = factory; /***/ }), /* 72 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var string = util.string; var isString = string.isString; function factory (type, config, load, typed) { /** * @constructor Matrix * * A Matrix is a wrapper around an Array. A matrix can hold a multi dimensional * array. A matrix can be constructed as: * var matrix = math.matrix(data) * * Matrix contains the functions to resize, get and set values, get the size, * clone the matrix and to convert the matrix to a vector, array, or scalar. * Furthermore, one can iterate over the matrix using map and forEach. * The internal Array of the Matrix can be accessed using the function valueOf. * * Example usage: * var matrix = math.matrix([[1, 2], [3, 4]]); * matix.size(); // [2, 2] * matrix.resize([3, 2], 5); * matrix.valueOf(); // [[1, 2], [3, 4], [5, 5]] * matrix.subset([1,2]) // 3 (indexes are zero-based) * */ function Matrix() { if (!(this instanceof Matrix)) { throw new SyntaxError('Constructor must be called with the new operator'); } } /** * Attach type information */ Matrix.prototype.type = 'Matrix'; Matrix.prototype.isMatrix = true; /** * Get the Matrix storage constructor for the given format. * * @param {string} format The Matrix storage format. * * @return {Function} The Matrix storage constructor. */ Matrix.storage = function (format) { // check storage format is a string if (!isString(format)) { throw new TypeError('format must be a string value'); } // get storage format constructor var constructor = Matrix._storage[format]; if (!constructor) { throw new SyntaxError('Unsupported matrix storage format: ' + format); } // return storage constructor return constructor; }; // a map with all constructors for all storage types Matrix._storage = {}; /** * Get the storage format used by the matrix. * * Usage: * var format = matrix.storage() // retrieve storage format * * @return {string} The storage format. */ Matrix.prototype.storage = function () { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke storage on a Matrix interface'); }; /** * Get the datatype of the data stored in the matrix. * * Usage: * var format = matrix.datatype() // retrieve matrix datatype * * @return {string} The datatype. */ Matrix.prototype.datatype = function () { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke datatype on a Matrix interface'); }; /** * Create a new Matrix With the type of the current matrix instance * @param {Array | Object} data * @param {string} [datatype] */ Matrix.prototype.create = function (data, datatype) { throw new Error('Cannot invoke create on a Matrix interface'); }; /** * Get a subset of the matrix, or replace a subset of the matrix. * * Usage: * var subset = matrix.subset(index) // retrieve subset * var value = matrix.subset(index, replacement) // replace subset * * @param {Index} index * @param {Array | Matrix | *} [replacement] * @param {*} [defaultValue=0] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be filled with zeros. */ Matrix.prototype.subset = function (index, replacement, defaultValue) { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke subset on a Matrix interface'); }; /** * Get a single element from the matrix. * @param {number[]} index Zero-based index * @return {*} value */ Matrix.prototype.get = function (index) { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke get on a Matrix interface'); }; /** * Replace a single element in the matrix. * @param {number[]} index Zero-based index * @param {*} value * @param {*} [defaultValue] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be left undefined. * @return {Matrix} self */ Matrix.prototype.set = function (index, value, defaultValue) { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke set on a Matrix interface'); }; /** * Resize the matrix to the given size. Returns a copy of the matrix when * `copy=true`, otherwise return the matrix itself (resize in place). * * @param {number[]} size The new size the matrix should have. * @param {*} [defaultValue=0] Default value, filled in on new entries. * If not provided, the matrix elements will * be filled with zeros. * @param {boolean} [copy] Return a resized copy of the matrix * * @return {Matrix} The resized matrix */ Matrix.prototype.resize = function (size, defaultValue) { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke resize on a Matrix interface'); }; /** * Create a clone of the matrix * @return {Matrix} clone */ Matrix.prototype.clone = function () { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke clone on a Matrix interface'); }; /** * Retrieve the size of the matrix. * @returns {number[]} size */ Matrix.prototype.size = function() { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke size on a Matrix interface'); }; /** * Create a new matrix with the results of the callback function executed on * each entry of the matrix. * @param {Function} callback The callback function is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix being traversed. * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. * * @return {Matrix} matrix */ Matrix.prototype.map = function (callback, skipZeros) { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke map on a Matrix interface'); }; /** * Execute a callback function on each entry of the matrix. * @param {Function} callback The callback function is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix being traversed. */ Matrix.prototype.forEach = function (callback) { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke forEach on a Matrix interface'); }; /** * Create an Array with a copy of the data of the Matrix * @returns {Array} array */ Matrix.prototype.toArray = function () { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke toArray on a Matrix interface'); }; /** * Get the primitive value of the Matrix: a multidimensional array * @returns {Array} array */ Matrix.prototype.valueOf = function () { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke valueOf on a Matrix interface'); }; /** * Get a string representation of the matrix, with optional formatting options. * @param {Object | number | Function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @returns {string} str */ Matrix.prototype.format = function (options) { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke format on a Matrix interface'); }; /** * Get a string representation of the matrix * @returns {string} str */ Matrix.prototype.toString = function () { // must be implemented by each of the Matrix implementations throw new Error('Cannot invoke toString on a Matrix interface'); }; // exports return Matrix; } exports.name = 'Matrix'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 73 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var equalScalar = load(__webpack_require__(8)); var SparseMatrix = type.SparseMatrix; /** * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). * Callback function invoked MAX(NNZA, NNZB) times * * * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0 * C(i,j) = ┤ A(i,j) ; A(i,j) !== 0 * └ B(i,j) ; B(i,j) !== 0 * * * @param {Matrix} a The SparseMatrix instance (A) * @param {Matrix} b The SparseMatrix instance (B) * @param {Function} callback The f(Aij,Bij) operation to invoke * * @return {Matrix} SparseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 */ var algorithm04 = function (a, b, callback) { // sparse matrix arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var asize = a._size; var adt = a._datatype; // sparse matrix arrays var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; var bsize = b._size; var bdt = b._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // equal signature to use var eq = equalScalar; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // find signature that matches (dt, dt) eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cvalues = avalues && bvalues ? [] : undefined; var cindex = []; var cptr = []; // matrix var c = new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: dt }); // workspace var xa = avalues && bvalues ? [] : undefined; var xb = avalues && bvalues ? [] : undefined; // marks indicating we have a value in x for a given column var wa = []; var wb = []; // vars var i, j, k, k0, k1; // loop columns for (j = 0; j < columns; j++) { // update cptr cptr[j] = cindex.length; // columns mark var mark = j + 1; // loop A(:,j) for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row i = aindex[k]; // update c cindex.push(i); // update workspace wa[i] = mark; // check we need to process values if (xa) xa[i] = avalues[k]; } // loop B(:,j) for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { // row i = bindex[k]; // check row exists in A if (wa[i] === mark) { // update record in xa @ i if (xa) { // invoke callback var v = cf(xa[i], bvalues[k]); // check for zero if (!eq(v, zero)) { // update workspace xa[i] = v; } else { // remove mark (index will be removed later) wa[i] = null; } } } else { // update c cindex.push(i); // update workspace wb[i] = mark; // check we need to process values if (xb) xb[i] = bvalues[k]; } } // check we need to process values (non pattern matrix) if (xa && xb) { // initialize first index in j k = cptr[j]; // loop index in j while (k < cindex.length) { // row i = cindex[k]; // check workspace has value @ i if (wa[i] === mark) { // push value (Aij != 0 || (Aij != 0 && Bij != 0)) cvalues[k] = xa[i]; // increment pointer k++; } else if (wb[i] === mark) { // push value (bij != 0) cvalues[k] = xb[i]; // increment pointer k++; } else { // remove index @ k cindex.splice(k, 1); } } } } // update cptr cptr[columns] = cindex.length; // return sparse matrix return c; }; return algorithm04; } exports.name = 'algorithm04'; exports.factory = factory; /***/ }), /* 74 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var equalScalar = load(__webpack_require__(8)); var SparseMatrix = type.SparseMatrix; /** * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). * Callback function invoked MAX(NNZA, NNZB) times * * * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0 * C(i,j) = ┤ A(i,j) ; A(i,j) !== 0 * └ 0 ; otherwise * * * @param {Matrix} a The SparseMatrix instance (A) * @param {Matrix} b The SparseMatrix instance (B) * @param {Function} callback The f(Aij,Bij) operation to invoke * * @return {Matrix} SparseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 */ var algorithm08 = function (a, b, callback) { // sparse matrix arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var asize = a._size; var adt = a._datatype; // sparse matrix arrays var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; var bsize = b._size; var bdt = b._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // sparse matrix cannot be a Pattern matrix if (!avalues || !bvalues) throw new Error('Cannot perform operation on Pattern Sparse Matrices'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // equal signature to use var eq = equalScalar; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // find signature that matches (dt, dt) eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cvalues = []; var cindex = []; var cptr = []; // matrix var c = new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: dt }); // workspace var x = []; // marks indicating we have a value in x for a given column var w = []; // vars var k, k0, k1, i; // loop columns for (var j = 0; j < columns; j++) { // update cptr cptr[j] = cindex.length; // columns mark var mark = j + 1; // loop values in a for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row i = aindex[k]; // mark workspace w[i] = mark; // set value x[i] = avalues[k]; // add index cindex.push(i); } // loop values in b for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { // row i = bindex[k]; // check value exists in workspace if (w[i] === mark) { // evaluate callback x[i] = cf(x[i], bvalues[k]); } } // initialize first index in j k = cptr[j]; // loop index in j while (k < cindex.length) { // row i = cindex[k]; // value @ i var v = x[i]; // check for zero value if (!eq(v, zero)) { // push value cvalues.push(v); // increment pointer k++; } else { // remove value @ i, do not increment pointer cindex.splice(k, 1); } } } // update cptr cptr[columns] = cindex.length; // return sparse matrix return c; }; return algorithm08; } exports.name = 'algorithm08'; exports.factory = factory; /***/ }), /* 75 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Create a number or convert a string, boolean, or unit to a number. * When value is a matrix, all elements will be converted to number. * * Syntax: * * math.number(value) * math.number(unit, valuelessUnit) * * Examples: * * math.number(2); // returns number 2 * math.number('7.2'); // returns number 7.2 * math.number(true); // returns number 1 * math.number([true, false, true, true]); // returns [1, 0, 1, 1] * math.number(math.unit('52cm'), 'm'); // returns 0.52 * * See also: * * bignumber, boolean, complex, index, matrix, string, unit * * @param {string | number | BigNumber | Fraction | boolean | Array | Matrix | Unit | null} [value] Value to be converted * @param {Unit | string} [valuelessUnit] A valueless unit, used to convert a unit to a number * @return {number | Array | Matrix} The created number */ var number = typed('number', { '': function () { return 0; }, 'number': function (x) { return x; }, 'string': function (x) { var num = Number(x); if (isNaN(num)) { throw new SyntaxError('String "' + x + '" is no valid number'); } return num; }, 'BigNumber': function (x) { return x.toNumber(); }, 'Fraction': function (x) { return x.valueOf(); }, 'Unit': function (x) { throw new Error('Second argument with valueless unit expected'); }, 'Unit, string | Unit': function (unit, valuelessUnit) { return unit.toNumber(valuelessUnit); }, 'Array | Matrix': function (x) { return deepMap(x, number); } }); number.toTex = { 0: '0', 1: '\\left(${args[0]}\\right)', 2: '\\left(\\left(${args[0]}\\right)${args[1]}\\right)' }; return number; } exports.name = 'number'; exports.factory = factory; /***/ }), /* 76 */ /***/ (function(module, exports) { /** * Bitwise not * @param {BigNumber} value * @return {BigNumber} Result of ~`x`, fully precise * */ module.exports = function bitNot (x) { if (x.isFinite() && !x.isInteger()) { throw new Error('Integer expected in function bitNot'); } var BigNumber = x.constructor; var prevPrec = BigNumber.precision; BigNumber.config({precision: 1E9}); var x = x.plus(new BigNumber(1)); x.s = -x.s || null; BigNumber.config({precision: prevPrec}); return x; }; /***/ }), /* 77 */ /***/ (function(module, exports, __webpack_require__) { var bitNot = __webpack_require__(76); /** * Applies bitwise function to numbers * @param {BigNumber} x * @param {BigNumber} y * @param {function (a, b)} func * @return {BigNumber} */ module.exports = function bitwise(x, y, func) { var BigNumber = x.constructor; var xBits, yBits; var xSign = +(x.s < 0); var ySign = +(y.s < 0); if (xSign) { xBits = decCoefficientToBinaryString(bitNot(x)); for (var i = 0; i < xBits.length; ++i) { xBits[i] ^= 1; } } else { xBits = decCoefficientToBinaryString(x); } if (ySign) { yBits = decCoefficientToBinaryString(bitNot(y)); for (var i = 0; i < yBits.length; ++i) { yBits[i] ^= 1; } } else { yBits = decCoefficientToBinaryString(y); } var minBits, maxBits, minSign; if (xBits.length <= yBits.length) { minBits = xBits; maxBits = yBits; minSign = xSign; } else { minBits = yBits; maxBits = xBits; minSign = ySign; } var shortLen = minBits.length; var longLen = maxBits.length; var expFuncVal = func(xSign, ySign) ^ 1; var outVal = new BigNumber(expFuncVal ^ 1); var twoPower = new BigNumber(1); var two = new BigNumber(2); var prevPrec = BigNumber.precision; BigNumber.config({precision: 1E9}); while (shortLen > 0) { if (func(minBits[--shortLen], maxBits[--longLen]) == expFuncVal) { outVal = outVal.plus(twoPower); } twoPower = twoPower.times(two); } while (longLen > 0) { if (func(minSign, maxBits[--longLen]) == expFuncVal) { outVal = outVal.plus(twoPower); } twoPower = twoPower.times(two); } BigNumber.config({precision: prevPrec}); if (expFuncVal == 0) { outVal.s = -outVal.s; } return outVal; }; /* Extracted from decimal.js, and edited to specialize. */ function decCoefficientToBinaryString (x) { // Convert to string var a = x.d; // array with digits var r = a[0] + ''; for (var i = 1; i < a.length; ++i) { var s = a[i] + ''; for (var z = 7 - s.length; z--; ) { s = '0' + s; } r += s; } var j; for (j = r.length - 1; r.charAt(j) == '0'; --j); var xe = x.e; var str = r.slice(0, j + 1 || 1); var strL = str.length; if (xe > 0) { if (++xe > strL) { // Append zeros. for (xe -= strL; xe--; str += '0'); } else if (xe < strL) { str = str.slice(0, xe) + '.' + str.slice(xe); } } // Convert from base 10 (decimal) to base 2 var arr = [0]; for (var i = 0; i < str.length; ) { for (var arrL = arr.length; arrL--; arr[arrL] *= 10); arr[0] += str.charAt(i++) << 0; // convert to int for (var j = 0; j < arr.length; ++j) { if (arr[j] > 1) { if (arr[j + 1] == null) { arr[j + 1] = 0; } arr[j + 1] += arr[j] >> 1; arr[j] &= 1; } } } return arr.reverse(); } /***/ }), /* 78 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var extend = __webpack_require__(4).extend; function factory (type, config, load, typed, math) { var _parse = load(__webpack_require__(28)); /** * @constructor Parser * Parser contains methods to evaluate or parse expressions, and has a number * of convenience methods to get, set, and remove variables from memory. Parser * keeps a scope containing variables in memory, which is used for all * evaluations. * * Methods: * var result = parser.eval(expr); // evaluate an expression * var value = parser.get(name); // retrieve a variable from the parser * var values = parser.getAll(); // retrieve all defined variables * parser.set(name, value); // set a variable in the parser * parser.remove(name); // clear a variable from the * // parsers scope * parser.clear(); // clear the parsers scope * * Example usage: * var parser = new Parser(); * // Note: there is a convenience method which can be used instead: * // var parser = new math.parser(); * * // evaluate expressions * parser.eval('sqrt(3^2 + 4^2)'); // 5 * parser.eval('sqrt(-4)'); // 2i * parser.eval('2 inch in cm'); // 5.08 cm * parser.eval('cos(45 deg)'); // 0.7071067811865476 * * // define variables and functions * parser.eval('x = 7 / 2'); // 3.5 * parser.eval('x + 3'); // 6.5 * parser.eval('function f(x, y) = x^y'); // f(x, y) * parser.eval('f(2, 3)'); // 8 * * // get and set variables and functions * var x = parser.get('x'); // 7 * var f = parser.get('f'); // function * var g = f(3, 2); // 9 * parser.set('h', 500); * var i = parser.eval('h / 2'); // 250 * parser.set('hello', function (name) { * return 'hello, ' + name + '!'; * }); * parser.eval('hello("user")'); // "hello, user!" * * // clear defined functions and variables * parser.clear(); * */ function Parser() { if (!(this instanceof Parser)) { throw new SyntaxError( 'Constructor must be called with the new operator'); } this.scope = {}; } /** * Attach type information */ Parser.prototype.type = 'Parser'; Parser.prototype.isParser = true; /** * Parse an expression and return the parsed function node. * The node tree can be compiled via `code = node.compile(math)`, * and the compiled code can be executed as `code.eval([scope])` * @param {string} expr * @return {Node} node * @throws {Error} */ Parser.prototype.parse = function (expr) { throw new Error('Parser.parse is deprecated. Use math.parse instead.'); }; /** * Parse and compile an expression, return the compiled javascript code. * The node can be evaluated via code.eval([scope]) * @param {string} expr * @return {{eval: function}} code * @throws {Error} */ Parser.prototype.compile = function (expr) { throw new Error('Parser.compile is deprecated. Use math.compile instead.'); }; /** * Parse and evaluate the given expression * @param {string} expr A string containing an expression, for example "2+3" * @return {*} result The result, or undefined when the expression was empty * @throws {Error} */ Parser.prototype.eval = function (expr) { // TODO: validate arguments return _parse(expr) .compile() .eval(this.scope); }; /** * Get a variable (a function or variable) by name from the parsers scope. * Returns undefined when not found * @param {string} name * @return {* | undefined} value */ Parser.prototype.get = function (name) { // TODO: validate arguments return this.scope[name]; }; /** * Get a map with all defined variables * @return {Object} values */ Parser.prototype.getAll = function () { return extend({}, this.scope); }; /** * Set a symbol (a function or variable) by name from the parsers scope. * @param {string} name * @param {* | undefined} value */ Parser.prototype.set = function (name, value) { // TODO: validate arguments return this.scope[name] = value; }; /** * Remove a variable from the parsers scope * @param {string} name */ Parser.prototype.remove = function (name) { // TODO: validate arguments delete this.scope[name]; }; /** * Clear the scope with variables and functions */ Parser.prototype.clear = function () { for (var name in this.scope) { if (this.scope.hasOwnProperty(name)) { delete this.scope[name]; } } }; return Parser; } exports.name = 'Parser'; exports.path = 'expression'; exports.factory = factory; exports.math = true; // requires the math namespace as 5th argument /***/ }), /* 79 */ /***/ (function(module, exports) { module.exports = { 'name': 'e', 'category': 'Constants', 'syntax': [ 'e' ], 'description': 'Euler\'s number, the base of the natural logarithm. Approximately equal to 2.71828', 'examples': [ 'e', 'e ^ 2', 'exp(2)', 'log(e)' ], 'seealso': ['exp'] }; /***/ }), /* 80 */ /***/ (function(module, exports) { module.exports = { 'name': 'pi', 'category': 'Constants', 'syntax': [ 'pi' ], 'description': 'The number pi is a mathematical constant that is the ratio of a circle\'s circumference to its diameter, and is approximately equal to 3.14159', 'examples': [ 'pi', 'sin(pi/2)' ], 'seealso': ['tau'] }; /***/ }), /* 81 */ /***/ (function(module, exports, __webpack_require__) { function factory (construction, config, load, typed) { var docs = {}; // construction functions docs.bignumber = __webpack_require__(157); docs['boolean'] = __webpack_require__(158); docs.complex = __webpack_require__(159); docs.createUnit = __webpack_require__(160); docs.fraction = __webpack_require__(161); docs.index = __webpack_require__(162); docs.matrix = __webpack_require__(163); docs.number = __webpack_require__(164); docs.sparse = __webpack_require__(165); docs.splitUnit = __webpack_require__(166); docs.string = __webpack_require__(167); docs.unit = __webpack_require__(168); // constants docs.e = __webpack_require__(79); docs.E = __webpack_require__(79); docs['false'] = __webpack_require__(150); docs.i = __webpack_require__(151); docs['Infinity'] = __webpack_require__(142); docs.LN2 = __webpack_require__(144); docs.LN10 = __webpack_require__(143); docs.LOG2E = __webpack_require__(146); docs.LOG10E = __webpack_require__(145); docs.NaN = __webpack_require__(147); docs['null'] = __webpack_require__(152); docs.pi = __webpack_require__(80); docs.PI = __webpack_require__(80); docs.phi = __webpack_require__(153); docs.SQRT1_2 = __webpack_require__(148); docs.SQRT2 = __webpack_require__(149); docs.tau = __webpack_require__(154); docs['true'] = __webpack_require__(155); docs.version = __webpack_require__(156); // physical constants // TODO: more detailed docs for physical constants docs.speedOfLight = {description: 'Speed of light in vacuum', examples: ['speedOfLight']}; docs.gravitationConstant = {description: 'Newtonian constant of gravitation', examples: ['gravitationConstant']}; docs.planckConstant = {description: 'Planck constant', examples: ['planckConstant']}; docs.reducedPlanckConstant = {description: 'Reduced Planck constant', examples: ['reducedPlanckConstant']}; docs.magneticConstant = {description: 'Magnetic constant (vacuum permeability)', examples: ['magneticConstant']}; docs.electricConstant = {description: 'Electric constant (vacuum permeability)', examples: ['electricConstant']}; docs.vacuumImpedance = {description: 'Characteristic impedance of vacuum', examples: ['vacuumImpedance']}; docs.coulomb = {description: 'Coulomb\'s constant', examples: ['coulomb']}; docs.elementaryCharge = {description: 'Elementary charge', examples: ['elementaryCharge']}; docs.bohrMagneton = {description: 'Borh magneton', examples: ['bohrMagneton']}; docs.conductanceQuantum = {description: 'Conductance quantum', examples: ['conductanceQuantum']}; docs.inverseConductanceQuantum = {description: 'Inverse conductance quantum', examples: ['inverseConductanceQuantum']}; //docs.josephson = {description: 'Josephson constant', examples: ['josephson']}; docs.magneticFluxQuantum = {description: 'Magnetic flux quantum', examples: ['magneticFluxQuantum']}; docs.nuclearMagneton = {description: 'Nuclear magneton', examples: ['nuclearMagneton']}; docs.klitzing = {description: 'Von Klitzing constant', examples: ['klitzing']}; docs.bohrRadius = {description: 'Borh radius', examples: ['bohrRadius']}; docs.classicalElectronRadius = {description: 'Classical electron radius', examples: ['classicalElectronRadius']}; docs.electronMass = {description: 'Electron mass', examples: ['electronMass']}; docs.fermiCoupling = {description: 'Fermi coupling constant', examples: ['fermiCoupling']}; docs.fineStructure = {description: 'Fine-structure constant', examples: ['fineStructure']}; docs.hartreeEnergy = {description: 'Hartree energy', examples: ['hartreeEnergy']}; docs.protonMass = {description: 'Proton mass', examples: ['protonMass']}; docs.deuteronMass = {description: 'Deuteron Mass', examples: ['deuteronMass']}; docs.neutronMass = {description: 'Neutron mass', examples: ['neutronMass']}; docs.quantumOfCirculation = {description: 'Quantum of circulation', examples: ['quantumOfCirculation']}; docs.rydberg = {description: 'Rydberg constant', examples: ['rydberg']}; docs.thomsonCrossSection = {description: 'Thomson cross section', examples: ['thomsonCrossSection']}; docs.weakMixingAngle = {description: 'Weak mixing angle', examples: ['weakMixingAngle']}; docs.efimovFactor = {description: 'Efimov factor', examples: ['efimovFactor']}; docs.atomicMass = {description: 'Atomic mass constant', examples: ['atomicMass']}; docs.avogadro = {description: 'Avogadro\'s number', examples: ['avogadro']}; docs.boltzmann = {description: 'Boltzmann constant', examples: ['boltzmann']}; docs.faraday = {description: 'Faraday constant', examples: ['faraday']}; docs.firstRadiation = {description: 'First radiation constant', examples: ['firstRadiation']}; docs.loschmidt = {description: 'Loschmidt constant at T=273.15 K and p=101.325 kPa', examples: ['loschmidt']}; docs.gasConstant = {description: 'Gas constant', examples: ['gasConstant']}; docs.molarPlanckConstant = {description: 'Molar Planck constant', examples: ['molarPlanckConstant']}; docs.molarVolume = {description: 'Molar volume of an ideal gas at T=273.15 K and p=101.325 kPa', examples: ['molarVolume']}; docs.sackurTetrode = {description: 'Sackur-Tetrode constant at T=1 K and p=101.325 kPa', examples: ['sackurTetrode']}; docs.secondRadiation = {description: 'Second radiation constant', examples: ['secondRadiation']}; docs.stefanBoltzmann = {description: 'Stefan-Boltzmann constant', examples: ['stefanBoltzmann']}; docs.wienDisplacement = {description: 'Wien displacement law constant', examples: ['wienDisplacement']}; //docs.spectralRadiance = {description: 'First radiation constant for spectral radiance', examples: ['spectralRadiance']}; docs.molarMass = {description: 'Molar mass constant', examples: ['molarMass']}; docs.molarMassC12 = {description: 'Molar mass constant of carbon-12', examples: ['molarMassC12']}; docs.gravity = {description: 'Standard acceleration of gravity (standard acceleration of free-fall on Earth)', examples: ['gravity']}; docs.planckLength = {description: 'Planck length', examples: ['planckLength']}; docs.planckMass = {description: 'Planck mass', examples: ['planckMass']}; docs.planckTime = {description: 'Planck time', examples: ['planckTime']}; docs.planckCharge = {description: 'Planck charge', examples: ['planckCharge']}; docs.planckTemperature = {description: 'Planck temperature', examples: ['planckTemperature']}; // functions - algebra docs.derivative = __webpack_require__(172); docs.lsolve = __webpack_require__(173); docs.lup = __webpack_require__(174); docs.lusolve = __webpack_require__(175); docs.simplify = __webpack_require__(176); docs.slu = __webpack_require__(177); docs.usolve = __webpack_require__(178); // functions - arithmetic docs.abs = __webpack_require__(179); docs.add = __webpack_require__(180); docs.cbrt = __webpack_require__(181); docs.ceil = __webpack_require__(182); docs.cube = __webpack_require__(183); docs.divide = __webpack_require__(184); docs.dotDivide = __webpack_require__(185); docs.dotMultiply = __webpack_require__(186); docs.dotPow = __webpack_require__(187); docs.exp = __webpack_require__(188); docs.fix = __webpack_require__(189); docs.floor = __webpack_require__(190); docs.gcd = __webpack_require__(191); docs.hypot = __webpack_require__(192); docs.lcm = __webpack_require__(193); docs.log = __webpack_require__(194); docs.log10 = __webpack_require__(195); docs.mod = __webpack_require__(196); docs.multiply = __webpack_require__(197); docs.norm = __webpack_require__(198); docs.nthRoot = __webpack_require__(199); docs.pow = __webpack_require__(200); docs.round = __webpack_require__(201); docs.sign = __webpack_require__(202); docs.sqrt = __webpack_require__(203); docs.square = __webpack_require__(204); docs.subtract = __webpack_require__(205); docs.unaryMinus = __webpack_require__(206); docs.unaryPlus = __webpack_require__(207); docs.xgcd = __webpack_require__(208); // functions - bitwise docs.bitAnd = __webpack_require__(209); docs.bitNot = __webpack_require__(210); docs.bitOr = __webpack_require__(211); docs.bitXor = __webpack_require__(212); docs.leftShift = __webpack_require__(213); docs.rightArithShift = __webpack_require__(214); docs.rightLogShift = __webpack_require__(215); // functions - combinatorics docs.bellNumbers = __webpack_require__(216); docs.catalan = __webpack_require__(217); docs.composition = __webpack_require__(218); docs.stirlingS2 = __webpack_require__(219); // functions - core docs['config'] = __webpack_require__(169); docs['import'] = __webpack_require__(170); docs['typed'] = __webpack_require__(171); // functions - complex docs.arg = __webpack_require__(220); docs.conj = __webpack_require__(221); docs.re = __webpack_require__(223); docs.im = __webpack_require__(222); // functions - expression docs['eval'] = __webpack_require__(224); docs.help = __webpack_require__(225); // functions - geometry docs.distance = __webpack_require__(226); docs.intersect = __webpack_require__(227); // functions - logical docs['and'] = __webpack_require__(228); docs['not'] = __webpack_require__(229); docs['or'] = __webpack_require__(230); docs['xor'] = __webpack_require__(231); // functions - matrix docs['concat'] = __webpack_require__(232); docs.cross = __webpack_require__(233); docs.det = __webpack_require__(234); docs.diag = __webpack_require__(235); docs.dot = __webpack_require__(236); docs.eye = __webpack_require__(237); docs.filter = __webpack_require__(238); docs.flatten = __webpack_require__(239); docs.forEach = __webpack_require__(240); docs.inv = __webpack_require__(241); docs.kron = __webpack_require__(242); docs.map = __webpack_require__(243); docs.ones = __webpack_require__(244); docs.partitionSelect = __webpack_require__(245); docs.range = __webpack_require__(246); docs.resize = __webpack_require__(247); docs.size = __webpack_require__(248); docs.sort = __webpack_require__(249); docs.squeeze = __webpack_require__(250); docs.subset = __webpack_require__(251); docs.trace = __webpack_require__(252); docs.transpose = __webpack_require__(253); docs.zeros = __webpack_require__(254); // functions - probability docs.combinations = __webpack_require__(255); //docs.distribution = require('./function/probability/distribution'); docs.factorial = __webpack_require__(256); docs.gamma = __webpack_require__(257); docs.kldivergence = __webpack_require__(258); docs.multinomial = __webpack_require__(259); docs.permutations = __webpack_require__(260); docs.pickRandom = __webpack_require__(261); docs.random = __webpack_require__(262); docs.randomInt = __webpack_require__(263); // functions - relational docs.compare = __webpack_require__(264); docs.deepEqual = __webpack_require__(265); docs['equal'] = __webpack_require__(266); docs.larger = __webpack_require__(267); docs.largerEq = __webpack_require__(268); docs.smaller = __webpack_require__(269); docs.smallerEq = __webpack_require__(270); docs.unequal = __webpack_require__(271); // functions - special docs.erf = __webpack_require__(272); // functions - statistics docs.mad = __webpack_require__(273); docs.max = __webpack_require__(274); docs.mean = __webpack_require__(275); docs.median = __webpack_require__(276); docs.min = __webpack_require__(277); docs.mode = __webpack_require__(278); docs.prod = __webpack_require__(279); docs.quantileSeq = __webpack_require__(280); docs.std = __webpack_require__(281); docs.sum = __webpack_require__(282); docs['var'] = __webpack_require__(283); // functions - trigonometry docs.acos = __webpack_require__(284); docs.acosh = __webpack_require__(285); docs.acot = __webpack_require__(286); docs.acoth = __webpack_require__(287); docs.acsc = __webpack_require__(288); docs.acsch = __webpack_require__(289); docs.asec = __webpack_require__(290); docs.asech = __webpack_require__(291); docs.asin = __webpack_require__(292); docs.asinh = __webpack_require__(293); docs.atan = __webpack_require__(294); docs.atanh = __webpack_require__(296); docs.atan2 = __webpack_require__(295); docs.cos = __webpack_require__(297); docs.cosh = __webpack_require__(298); docs.cot = __webpack_require__(299); docs.coth = __webpack_require__(300); docs.csc = __webpack_require__(301); docs.csch = __webpack_require__(302); docs.sec = __webpack_require__(303); docs.sech = __webpack_require__(304); docs.sin = __webpack_require__(305); docs.sinh = __webpack_require__(306); docs.tan = __webpack_require__(307); docs.tanh = __webpack_require__(308); // functions - units docs.to = __webpack_require__(309); // functions - utils docs.clone = __webpack_require__(310); docs.format = __webpack_require__(311); docs.isNaN = __webpack_require__(313); docs.isInteger = __webpack_require__(312); docs.isNegative = __webpack_require__(314); docs.isNumeric = __webpack_require__(315); docs.isPositive = __webpack_require__(316); docs.isPrime = __webpack_require__(317); docs.isZero = __webpack_require__(318); // docs.print = require('./function/utils/print'); // TODO: add documentation for print as soon as the parser supports objects. docs['typeof'] = __webpack_require__(319); return docs; } exports.name = 'docs'; exports.path = 'expression'; exports.factory = factory; /***/ }), /* 82 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed, math) { var Parser = load(__webpack_require__(78)); /** * Create a parser. The function creates a new `math.expression.Parser` object. * * Syntax: * * math.parser() * * Examples: * * var parser = new math.parser(); * * // evaluate expressions * var a = parser.eval('sqrt(3^2 + 4^2)'); // 5 * var b = parser.eval('sqrt(-4)'); // 2i * var c = parser.eval('2 inch in cm'); // 5.08 cm * var d = parser.eval('cos(45 deg)'); // 0.7071067811865476 * * // define variables and functions * parser.eval('x = 7 / 2'); // 3.5 * parser.eval('x + 3'); // 6.5 * parser.eval('function f(x, y) = x^y'); // f(x, y) * parser.eval('f(2, 3)'); // 8 * * // get and set variables and functions * var x = parser.get('x'); // 7 * var f = parser.get('f'); // function * var g = f(3, 2); // 9 * parser.set('h', 500); * var i = parser.eval('h / 2'); // 250 * parser.set('hello', function (name) { * return 'hello, ' + name + '!'; * }); * parser.eval('hello("user")'); // "hello, user!" * * // clear defined functions and variables * parser.clear(); * * See also: * * eval, compile, parse * * @return {Parser} Parser */ return typed('parser', { '': function () { return new Parser(math); } }); } exports.name = 'parser'; exports.factory = factory; exports.math = true; // requires the math namespace as 5th argument /***/ }), /* 83 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); var access = load(__webpack_require__(90)); /** * @constructor AccessorNode * @extends {Node} * Access an object property or get a matrix subset * * @param {Node} object The object from which to retrieve * a property or subset. * @param {IndexNode} index IndexNode containing ranges */ function AccessorNode(object, index) { if (!(this instanceof AccessorNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (!(object && object.isNode)) { throw new TypeError('Node expected for parameter "object"'); } if (!(index && index.isIndexNode)) { throw new TypeError('IndexNode expected for parameter "index"'); } this.object = object || null; this.index = index; // readonly property name Object.defineProperty(this, 'name', { get: function () { if (this.index) { return (this.index.isObjectProperty()) ? this.index.getObjectProperty() : ''; } else { return this.object.name || ''; } }.bind(this), set: function () { throw new Error('Cannot assign a new name, name is read-only'); } }); } AccessorNode.prototype = new Node(); AccessorNode.prototype.type = 'AccessorNode'; AccessorNode.prototype.isAccessorNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ AccessorNode.prototype._compile = function (defs, args) { defs.access = access; var object = this.object._compile(defs, args); var index = this.index._compile(defs, args); if (this.index.isObjectProperty()) { return object + '["' + this.index.getObjectProperty() + '"]'; } else if (this.index.needsSize()) { // if some parameters use the 'end' parameter, we need to calculate the size return '(function () {' + ' var object = ' + object + ';' + ' var size = math.size(object).valueOf();' + ' return access(object, ' + index + ');' + '})()'; } else { return 'access(' + object + ', ' + index + ')'; } }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ AccessorNode.prototype.forEach = function (callback) { callback(this.object, 'object', this); callback(this.index, 'index', this); }; /** * Create a new AccessorNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {AccessorNode} Returns a transformed copy of the node */ AccessorNode.prototype.map = function (callback) { return new AccessorNode( this._ifNode(callback(this.object, 'object', this)), this._ifNode(callback(this.index, 'index', this)) ); }; /** * Create a clone of this node, a shallow copy * @return {AccessorNode} */ AccessorNode.prototype.clone = function () { return new AccessorNode(this.object, this.index); }; /** * Get string representation * @param {Object} options * @return {string} */ AccessorNode.prototype._toString = function (options) { var object = this.object.toString(options); if (needParenthesis(this.object)) { object = '(' + object + ')'; } return object + this.index.toString(options); }; /** * Get LaTeX representation * @param {Object} options * @return {string} */ AccessorNode.prototype._toTex = function (options) { var object = this.object.toTex(options); if (needParenthesis(this.object)) { object = '\\left(' + object + '\\right)'; } return object + this.index.toTex(options); }; /** * Are parenthesis needed? * @private */ function needParenthesis(node) { // TODO: maybe make a method on the nodes which tells whether they need parenthesis? return !(node.isAccessorNode || node.isArrayNode || node.isConstantNode || node.isFunctionNode || node.isObjectNode || node.isParenthesisNode || node.isSymbolNode); } return AccessorNode; } exports.name = 'AccessorNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 84 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var latex = __webpack_require__(3); function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); var ArrayNode = load(__webpack_require__(62)); var matrix = load(__webpack_require__(1)); var assign = load(__webpack_require__(328)); var access = load(__webpack_require__(90)); var keywords = __webpack_require__(61); var operators = __webpack_require__(45); /** * @constructor AssignmentNode * @extends {Node} * * Define a symbol, like `a=3.2`, update a property like `a.b=3.2`, or * replace a subset of a matrix like `A[2,2]=42`. * * Syntax: * * new AssignmentNode(symbol, value) * new AssignmentNode(object, index, value) * * Usage: * * new AssignmentNode(new SymbolNode('a'), new ConstantNode(2)); // a=2 * new AssignmentNode(new SymbolNode('a'), new IndexNode('b'), new ConstantNode(2)) // a.b=2 * new AssignmentNode(new SymbolNode('a'), new IndexNode(1, 2), new ConstantNode(3)) // a[1,2]=3 * * @param {SymbolNode | AccessorNode} object Object on which to assign a value * @param {IndexNode} [index=null] Index, property name or matrix * index. Optional. If not provided * and `object` is a SymbolNode, * the property is assigned to the * global scope. * @param {Node} value The value to be assigned */ function AssignmentNode(object, index, value) { if (!(this instanceof AssignmentNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } this.object = object; this.index = value ? index : null; this.value = value ? value : index; // validate input if (!object || !(object.isSymbolNode || object.isAccessorNode)) { throw new TypeError('SymbolNode or AccessorNode expected as "object"'); } if (object && object.isSymbolNode && object.name === 'end') { throw new Error('Cannot assign to symbol "end"'); } if (this.index && !this.index.isIndexNode) { throw new TypeError('IndexNode expected as "index"'); } if (!this.value || !this.value.isNode) { throw new TypeError('Node expected as "value"'); } // readonly property name Object.defineProperty(this, 'name', { get: function () { if (this.index) { return (this.index.isObjectProperty()) ? this.index.getObjectProperty() : ''; } else { return this.object.name || ''; } }.bind(this), set: function () { throw new Error('Cannot assign a new name, name is read-only'); } }); } AssignmentNode.prototype = new Node(); AssignmentNode.prototype.type = 'AssignmentNode'; AssignmentNode.prototype.isAssignmentNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @private */ AssignmentNode.prototype._compile = function (defs, args) { defs.assign = assign; defs.access = access; var size; var object = this.object._compile(defs, args); var index = this.index ? this.index._compile(defs, args) : null; var value = this.value._compile(defs, args); if (!this.index) { // apply a variable to the scope, for example `a=2` if (!this.object.isSymbolNode) { throw new TypeError('SymbolNode expected as object'); } return 'scope["' + this.object.name + '"] = ' + value; } else if (this.index.isObjectProperty()) { // apply an object property for example `a.b=2` return object + '["' + this.index.getObjectProperty() + '"] = ' + value; } else if (this.object.isSymbolNode) { // update a matrix subset, for example `a[2]=3` size = this.index.needsSize() ? 'var size = math.size(object).valueOf();' : ''; // apply updated object to scope return '(function () {' + ' var object = ' + object + ';' + ' var value = ' + value + ';' + ' ' + size + ' scope["' + this.object.name + '"] = assign(object, ' + index + ', value);' + ' return value;' + '})()'; } else { // this.object.isAccessorNode === true // update a matrix subset, for example `a.b[2]=3` size = this.index.needsSize() ? 'var size = math.size(object).valueOf();' : ''; // we will not use the _compile of the AccessorNode, but compile it // ourselves here as we need the parent object of the AccessorNode: // wee need to apply the updated object to parent object var parentObject = this.object.object._compile(defs, args); if (this.object.index.isObjectProperty()) { var parentProperty = '["' + this.object.index.getObjectProperty() + '"]'; return '(function () {' + ' var parent = ' + parentObject + ';' + ' var object = parent' + parentProperty + ';' + // parentIndex is a property ' var value = ' + value + ';' + size + ' parent' + parentProperty + ' = assign(object, ' + index + ', value);' + ' return value;' + '})()'; } else { // if some parameters use the 'end' parameter, we need to calculate the size var parentSize = this.object.index.needsSize() ? 'var size = math.size(parent).valueOf();' : ''; var parentIndex = this.object.index._compile(defs, args); return '(function () {' + ' var parent = ' + parentObject + ';' + ' ' + parentSize + ' var parentIndex = ' + parentIndex + ';' + ' var object = access(parent, parentIndex);' + ' var value = ' + value + ';' + ' ' + size + ' assign(parent, parentIndex, assign(object, ' + index + ', value));' + ' return value;' + '})()'; } } }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ AssignmentNode.prototype.forEach = function (callback) { callback(this.object, 'object', this); if (this.index) { callback(this.index, 'index', this); } callback(this.value, 'value', this); }; /** * Create a new AssignmentNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {AssignmentNode} Returns a transformed copy of the node */ AssignmentNode.prototype.map = function (callback) { var object = this._ifNode(callback(this.object, 'object', this)); var index = this.index ? this._ifNode(callback(this.index, 'index', this)) : null; var value = this._ifNode(callback(this.value, 'value', this)); return new AssignmentNode(object, index, value); }; /** * Create a clone of this node, a shallow copy * @return {AssignmentNode} */ AssignmentNode.prototype.clone = function() { return new AssignmentNode(this.object, this.index, this.value); }; /* * Is parenthesis needed? * @param {node} node * @param {string} [parenthesis='keep'] * @private */ function needParenthesis(node, parenthesis) { if (!parenthesis) { parenthesis = 'keep'; } var precedence = operators.getPrecedence(node, parenthesis); var exprPrecedence = operators.getPrecedence(node.value, parenthesis); return (parenthesis === 'all') || ((exprPrecedence !== null) && (exprPrecedence <= precedence)); } /** * Get string representation * @param {Object} options * @return {string} */ AssignmentNode.prototype._toString = function(options) { var object = this.object.toString(options); var index = this.index ? this.index.toString(options) : ''; var value = this.value.toString(options); if (needParenthesis(this, options && options.parenthesis)) { value = '(' + value + ')'; } return object + index + ' = ' + value; }; /** * Get LaTeX representation * @param {Object} options * @return {string} */ AssignmentNode.prototype._toTex = function(options) { var object = this.object.toTex(options); var index = this.index ? this.index.toTex(options) : ''; var value = this.value.toTex(options); if (needParenthesis(this, options && options.parenthesis)) { value = '\\left(' + value + '\\right)'; } return object + index + ':=' + value; }; return AssignmentNode; } exports.name = 'AssignmentNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 85 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); var ResultSet = load(__webpack_require__(129)); /** * @constructor BlockNode * @extends {Node} * Holds a set with blocks * @param {Array.<{node: Node} | {node: Node, visible: boolean}>} blocks * An array with blocks, where a block is constructed as an Object * with properties block, which is a Node, and visible, which is * a boolean. The property visible is optional and is true by default */ function BlockNode(blocks) { if (!(this instanceof BlockNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } // validate input, copy blocks if (!Array.isArray(blocks)) throw new Error('Array expected'); this.blocks = blocks.map(function (block) { var node = block && block.node; var visible = block && block.visible !== undefined ? block.visible : true; if (!(node && node.isNode)) throw new TypeError('Property "node" must be a Node'); if (typeof visible !== 'boolean') throw new TypeError('Property "visible" must be a boolean'); return { node: node, visible: visible } }); } BlockNode.prototype = new Node(); BlockNode.prototype.type = 'BlockNode'; BlockNode.prototype.isBlockNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ BlockNode.prototype._compile = function (defs, args) { defs.ResultSet = ResultSet; var blocks = this.blocks.map(function (param) { var js = param.node._compile(defs, args); if (param.visible) { return 'results.push(' + js + ');'; } else { return js + ';'; } }); return '(function () {' + 'var results = [];' + blocks.join('') + 'return new ResultSet(results);' + '})()'; }; /** * Execute a callback for each of the child blocks of this node * @param {function(child: Node, path: string, parent: Node)} callback */ BlockNode.prototype.forEach = function (callback) { for (var i = 0; i < this.blocks.length; i++) { callback(this.blocks[i].node, 'blocks[' + i + '].node', this); } }; /** * Create a new BlockNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {BlockNode} Returns a transformed copy of the node */ BlockNode.prototype.map = function (callback) { var blocks = []; for (var i = 0; i < this.blocks.length; i++) { var block = this.blocks[i]; var node = this._ifNode(callback(block.node, 'blocks[' + i + '].node', this)); blocks[i] = { node: node, visible: block.visible }; } return new BlockNode(blocks); }; /** * Create a clone of this node, a shallow copy * @return {BlockNode} */ BlockNode.prototype.clone = function () { var blocks = this.blocks.map(function (block) { return { node: block.node, visible: block.visible }; }); return new BlockNode(blocks); }; /** * Get string representation * @param {Object} options * @return {string} str * @override */ BlockNode.prototype._toString = function (options) { return this.blocks.map(function (param) { return param.node.toString(options) + (param.visible ? '' : ';'); }).join('\n'); }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ BlockNode.prototype._toTex = function (options) { return this.blocks.map(function (param) { return param.node.toTex(options) + (param.visible ? '' : ';'); }).join('\\;\\;\n'); }; return BlockNode; } exports.name = 'BlockNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 86 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var latex = __webpack_require__(3); var operators = __webpack_require__(45); function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); /** * A lazy evaluating conditional operator: 'condition ? trueExpr : falseExpr' * * @param {Node} condition Condition, must result in a boolean * @param {Node} trueExpr Expression evaluated when condition is true * @param {Node} falseExpr Expression evaluated when condition is true * * @constructor ConditionalNode * @extends {Node} */ function ConditionalNode(condition, trueExpr, falseExpr) { if (!(this instanceof ConditionalNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (!(condition && condition.isNode)) throw new TypeError('Parameter condition must be a Node'); if (!(trueExpr && trueExpr.isNode)) throw new TypeError('Parameter trueExpr must be a Node'); if (!(falseExpr && falseExpr.isNode)) throw new TypeError('Parameter falseExpr must be a Node'); this.condition = condition; this.trueExpr = trueExpr; this.falseExpr = falseExpr; } ConditionalNode.prototype = new Node(); ConditionalNode.prototype.type = 'ConditionalNode'; ConditionalNode.prototype.isConditionalNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ ConditionalNode.prototype._compile = function (defs, args) { /** * Test whether a condition is met * @param {*} condition * @returns {boolean} true if condition is true or non-zero, else false */ defs.testCondition = function (condition) { if (typeof condition === 'number' || typeof condition === 'boolean' || typeof condition === 'string') { return condition ? true : false; } if (condition) { if (condition.isBigNumber === true) { return condition.isZero() ? false : true; } if (condition.isComplex === true) { return (condition.re || condition.im) ? true : false; } if (condition.isUnit === true) { return condition.value ? true : false; } } if (condition === null || condition === undefined) { return false; } throw new TypeError('Unsupported type of condition "' + defs.math['typeof'](condition) + '"'); }; return ( 'testCondition(' + this.condition._compile(defs, args) + ') ? ' + '( ' + this.trueExpr._compile(defs, args) + ') : ' + '( ' + this.falseExpr._compile(defs, args) + ')' ); }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ ConditionalNode.prototype.forEach = function (callback) { callback(this.condition, 'condition', this); callback(this.trueExpr, 'trueExpr', this); callback(this.falseExpr, 'falseExpr', this); }; /** * Create a new ConditionalNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {ConditionalNode} Returns a transformed copy of the node */ ConditionalNode.prototype.map = function (callback) { return new ConditionalNode( this._ifNode(callback(this.condition, 'condition', this)), this._ifNode(callback(this.trueExpr, 'trueExpr', this)), this._ifNode(callback(this.falseExpr, 'falseExpr', this)) ); }; /** * Create a clone of this node, a shallow copy * @return {ConditionalNode} */ ConditionalNode.prototype.clone = function () { return new ConditionalNode(this.condition, this.trueExpr, this.falseExpr); }; /** * Get string representation * @param {Object} options * @return {string} str */ ConditionalNode.prototype._toString = function (options) { var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'; var precedence = operators.getPrecedence(this, parenthesis); //Enclose Arguments in parentheses if they are an OperatorNode //or have lower or equal precedence //NOTE: enclosing all OperatorNodes in parentheses is a decision //purely based on aesthetics and readability var condition = this.condition.toString(options); var conditionPrecedence = operators.getPrecedence(this.condition, parenthesis); if ((parenthesis === 'all') || (this.condition.type === 'OperatorNode') || ((conditionPrecedence !== null) && (conditionPrecedence <= precedence))) { condition = '(' + condition + ')'; } var trueExpr = this.trueExpr.toString(options); var truePrecedence = operators.getPrecedence(this.trueExpr, parenthesis); if ((parenthesis === 'all') || (this.trueExpr.type === 'OperatorNode') || ((truePrecedence !== null) && (truePrecedence <= precedence))) { trueExpr = '(' + trueExpr + ')'; } var falseExpr = this.falseExpr.toString(options); var falsePrecedence = operators.getPrecedence(this.falseExpr, parenthesis); if ((parenthesis === 'all') || (this.falseExpr.type === 'OperatorNode') || ((falsePrecedence !== null) && (falsePrecedence <= precedence))) { falseExpr = '(' + falseExpr + ')'; } return condition + ' ? ' + trueExpr + ' : ' + falseExpr; }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ ConditionalNode.prototype._toTex = function (options) { return '\\begin{cases} {' + this.trueExpr.toTex(options) + '}, &\\quad{\\text{if }\\;' + this.condition.toTex(options) + '}\\\\{' + this.falseExpr.toTex(options) + '}, &\\quad{\\text{otherwise}}\\end{cases}'; }; return ConditionalNode; } exports.name = 'ConditionalNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 87 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var keywords = __webpack_require__(61); var latex = __webpack_require__(3); var operators = __webpack_require__(45); function isString (x) { return typeof x === 'string'; } function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); /** * @constructor FunctionAssignmentNode * @extends {Node} * Function assignment * * @param {string} name Function name * @param {string[] | Array.<{name: string, type: string}>} params * Array with function parameter names, or an * array with objects containing the name * and type of the parameter * @param {Node} expr The function expression */ function FunctionAssignmentNode(name, params, expr) { if (!(this instanceof FunctionAssignmentNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } // validate input if (typeof name !== 'string') throw new TypeError('String expected for parameter "name"'); if (!Array.isArray(params)) throw new TypeError('Array containing strings or objects expected for parameter "params"'); if (!(expr && expr.isNode)) throw new TypeError('Node expected for parameter "expr"'); if (name in keywords) throw new Error('Illegal function name, "' + name + '" is a reserved keyword'); this.name = name; this.params = params.map(function (param) { return param && param.name || param; }); this.types = params.map(function (param) { return param && param.type || 'any' }); this.expr = expr; } FunctionAssignmentNode.prototype = new Node(); FunctionAssignmentNode.prototype.type = 'FunctionAssignmentNode'; FunctionAssignmentNode.prototype.isFunctionAssignmentNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} js * @private */ FunctionAssignmentNode.prototype._compile = function (defs, args) { defs.typed = typed; // we extend the original args and add the args to the child object var childArgs = Object.create(args); this.params.forEach(function (variable) { childArgs[variable] = true; }); // compile the function expression with the child args var jsExpr = this.expr._compile(defs, childArgs); return 'scope["' + this.name + '"] = ' + ' (function () {' + ' var fn = typed("' + this.name + '", {' + ' "' + this.types.join(',') + '": function (' + this.params.join(',') + ') {' + ' return ' + jsExpr + '' + ' }' + ' });' + ' fn.syntax = "' + this.name + '(' + this.params.join(', ') + ')";' + ' return fn;' + ' })()'; }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ FunctionAssignmentNode.prototype.forEach = function (callback) { callback(this.expr, 'expr', this); }; /** * Create a new FunctionAssignmentNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {FunctionAssignmentNode} Returns a transformed copy of the node */ FunctionAssignmentNode.prototype.map = function (callback) { var expr = this._ifNode(callback(this.expr, 'expr', this)); return new FunctionAssignmentNode(this.name, this.params.slice(0), expr); }; /** * Create a clone of this node, a shallow copy * @return {FunctionAssignmentNode} */ FunctionAssignmentNode.prototype.clone = function () { return new FunctionAssignmentNode(this.name, this.params.slice(0), this.expr); }; /** * Is parenthesis needed? * @param {Node} node * @param {Object} parenthesis * @private */ function needParenthesis(node, parenthesis) { var precedence = operators.getPrecedence(node, parenthesis); var exprPrecedence = operators.getPrecedence(node.expr, parenthesis); return (parenthesis === 'all') || ((exprPrecedence !== null) && (exprPrecedence <= precedence)); } /** * get string representation * @param {Object} options * @return {string} str */ FunctionAssignmentNode.prototype._toString = function (options) { var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'; var expr = this.expr.toString(options); if (needParenthesis(this, parenthesis)) { expr = '(' + expr + ')'; } return this.name + '(' + this.params.join(', ') + ') = ' + expr; }; /** * get LaTeX representation * @param {Object} options * @return {string} str */ FunctionAssignmentNode.prototype._toTex = function (options) { var parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'; var expr = this.expr.toTex(options); if (needParenthesis(this, parenthesis)) { expr = '\\left(' + expr + '\\right)'; } return '\\mathrm{' + this.name + '}\\left(' + this.params.map(latex.toSymbol).join(',') + '\\right):=' + expr; }; return FunctionAssignmentNode; } exports.name = 'FunctionAssignmentNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 88 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); var RangeNode = load(__webpack_require__(63)); var SymbolNode = load(__webpack_require__(24)); var Range = load(__webpack_require__(127)); var isArray = Array.isArray; /** * @constructor IndexNode * @extends Node * * Describes a subset of a matrix or an object property. * Cannot be used on its own, needs to be used within an AccessorNode or * AssignmentNode. * * @param {Node[]} dimensions * @param {boolean} [dotNotation=false] Optional property describing whether * this index was written using dot * notation like `a.b`, or using bracket * notation like `a["b"]` (default). * Used to stringify an IndexNode. */ function IndexNode(dimensions, dotNotation) { if (!(this instanceof IndexNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } this.dimensions = dimensions; this.dotNotation = dotNotation || false; // validate input if (!isArray(dimensions) || !dimensions.every(function (range) {return range && range.isNode;})) { throw new TypeError('Array containing Nodes expected for parameter "dimensions"'); } if (this.dotNotation && !this.isObjectProperty()) { throw new Error('dotNotation only applicable for object properties'); } // TODO: deprecated since v3, remove some day var deprecated = function () { throw new Error('Property `IndexNode.object` is deprecated, use `IndexNode.fn` instead'); }; Object.defineProperty(this, 'object', { get: deprecated, set: deprecated }); } IndexNode.prototype = new Node(); IndexNode.prototype.type = 'IndexNode'; IndexNode.prototype.isIndexNode = true; /** * Compile all range nodes * * When some of the dimensions has `end` defined, the IndexNode requires * a variable `size` to be defined in the current closure, and must contain * the size of the matrix that's being handled. To check whether the `size` * variable is needed, call IndexNode.needsSize(). * * @param {Object} defs Object which can be used to define functions * or constants globally available for the * compiled expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} code */ IndexNode.prototype._compile = function (defs, args) { // args can be mutated by IndexNode, when dimensions use `end` var childArgs = Object.create(args); // helper function to create a Range from start, step and end defs.range = function (start, end, step) { return new Range( (start && start.isBigNumber === true) ? start.toNumber() : start, (end && end.isBigNumber === true) ? end.toNumber() : end, (step && step.isBigNumber === true) ? step.toNumber() : step ); }; // TODO: implement support for bignumber (currently bignumbers are silently // reduced to numbers when changing the value to zero-based) // TODO: Optimization: when the range values are ConstantNodes, // we can beforehand resolve the zero-based value // optimization for a simple object property var dimensions = this.dimensions.map(function (range, i) { if (range && range.isRangeNode) { if (range.needsEnd()) { childArgs.end = true; // resolve end and create range return '(function () {' + 'var end = size[' + i + ']; ' + 'return range(' + range.start._compile(defs, childArgs) + ', ' + range.end._compile(defs, childArgs) + ', ' + (range.step ? range.step._compile(defs, childArgs) : '1') + '); ' + '})()'; } else { // create range return 'range(' + range.start._compile(defs, childArgs) + ', ' + range.end._compile(defs, childArgs) + ', ' + (range.step ? range.step._compile(defs, childArgs) : '1') + ')'; } } if (range.isSymbolNode && range.name === 'end') { childArgs.end = true; // resolve the parameter 'end' return '(function () {' + 'var end = size[' + i + ']; ' + 'return ' + range._compile(defs, childArgs) + '; ' + '})()' } else { // ConstantNode return range._compile(defs, childArgs); } }); return 'math.index(' + dimensions.join(', ') + ')'; }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ IndexNode.prototype.forEach = function (callback) { for (var i = 0; i < this.dimensions.length; i++) { callback(this.dimensions[i], 'dimensions[' + i + ']', this); } }; /** * Create a new IndexNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {IndexNode} Returns a transformed copy of the node */ IndexNode.prototype.map = function (callback) { var dimensions = []; for (var i = 0; i < this.dimensions.length; i++) { dimensions[i] = this._ifNode(callback(this.dimensions[i], 'dimensions[' + i + ']', this)); } return new IndexNode(dimensions); }; /** * Create a clone of this node, a shallow copy * @return {IndexNode} */ IndexNode.prototype.clone = function () { return new IndexNode(this.dimensions.slice(0)); }; /** * Test whether this IndexNode contains a single property name * @return {boolean} */ IndexNode.prototype.isObjectProperty = function () { return this.dimensions.length === 1 && this.dimensions[0].isConstantNode && this.dimensions[0].valueType === 'string'; }; /** * Returns the property name if IndexNode contains a property. * If not, returns null. * @return {string | null} */ IndexNode.prototype.getObjectProperty = function () { return this.isObjectProperty() ? this.dimensions[0].value : null; }; /** * Get string representation * @param {Object} options * @return {string} str */ IndexNode.prototype._toString = function (options) { // format the parameters like "[1, 0:5]" return this.dotNotation ? ('.' + this.getObjectProperty()) : ('[' + this.dimensions.join(', ') + ']'); }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ IndexNode.prototype._toTex = function (options) { var dimensions = this.dimensions.map(function (range) { return range.toTex(options); }); return this.dotNotation ? ('.' + this.getObjectProperty() + '') : ('_{' + dimensions.join(',') + '}'); }; /** * Test whether this IndexNode needs the object size, size of the Matrix * @return {boolean} */ IndexNode.prototype.needsSize = function () { return this.dimensions.some(function (range) { return (range.isRangeNode && range.needsEnd()) || (range.isSymbolNode && range.name === 'end'); }); }; return IndexNode; } exports.name = 'IndexNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 89 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var string = __webpack_require__(19); function factory (type, config, load, typed) { var Node = load(__webpack_require__(11)); /** * @constructor ObjectNode * @extends {Node} * Holds an object with keys/values * @param {Object.} [properties] array with key/value pairs */ function ObjectNode(properties) { if (!(this instanceof ObjectNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } this.properties = properties || {}; // validate input if (properties) { if (!(typeof properties === 'object') || Object.keys(properties).some(function (key) { return !properties[key] || !properties[key].isNode; })) { throw new TypeError('Object containing Nodes expected'); } } } ObjectNode.prototype = new Node(); ObjectNode.prototype.type = 'ObjectNode'; ObjectNode.prototype.isObjectNode = true; /** * Compile the node to javascript code * @param {Object} defs Object which can be used to define functions * or constants globally available for the compiled * expression * @param {Object} args Object with local function arguments, the key is * the name of the argument, and the value is `true`. * The object may not be mutated, but must be * extended instead. * @return {string} code * @private */ ObjectNode.prototype._compile = function (defs, args) { var entries = []; for (var key in this.properties) { if (this.properties.hasOwnProperty(key)) { entries.push('"' + key + '": ' + this.properties[key]._compile(defs, args)); } } return '{' + entries.join(', ') + '}'; }; /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ ObjectNode.prototype.forEach = function (callback) { for (var key in this.properties) { if (this.properties.hasOwnProperty(key)) { callback(this.properties[key], 'properties["' + key + '"]', this); } } }; /** * Create a new ObjectNode having it's childs be the results of calling * the provided callback function for each of the childs of the original node. * @param {function(child: Node, path: string, parent: Node): Node} callback * @returns {ObjectNode} Returns a transformed copy of the node */ ObjectNode.prototype.map = function (callback) { var properties = {}; for (var key in this.properties) { if (this.properties.hasOwnProperty(key)) { properties[key] = this._ifNode(callback(this.properties[key], 'properties["' + key + '"]', this)); } } return new ObjectNode(properties); }; /** * Create a clone of this node, a shallow copy * @return {ObjectNode} */ ObjectNode.prototype.clone = function() { var properties = {}; for (var key in this.properties) { if (this.properties.hasOwnProperty(key)) { properties[key] = this.properties[key]; } } return new ObjectNode(properties); }; /** * Get string representation * @param {Object} options * @return {string} str * @override */ ObjectNode.prototype._toString = function(options) { var entries = []; for (var key in this.properties) { if (this.properties.hasOwnProperty(key)) { entries.push('"' + key + '": ' + this.properties[key].toString(options)); } } return '{' + entries.join(', ') + '}'; }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ ObjectNode.prototype._toTex = function(options) { var entries = []; for (var key in this.properties) { if (this.properties.hasOwnProperty(key)) { entries.push("\\mathbf{" + key + ':} & ' + this.properties[key].toTex(options) + "\\\\"); } } return '\\left\\{\\begin{array}{ll}' + entries.join('\n') + '\\end{array}\\right\\}'; }; return ObjectNode; } exports.name = 'ObjectNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 90 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var errorTransform = __webpack_require__(29).transform; function factory (type, config, load, typed) { var subset = load(__webpack_require__(51)); var matrix = load(__webpack_require__(1)); /** * Retrieve part of an object: * * - Retrieve a property from an object * - Retrieve a part of a string * - Retrieve a matrix subset * * @param {Object | Array | Matrix | string} object * @param {Index} index * @return {Object | Array | Matrix | string} Returns the subset */ return function access(object, index) { try { if (Array.isArray(object)) { return matrix(object).subset(index).valueOf(); } else if (object && typeof object.subset === 'function') { // Matrix return object.subset(index); } else if (typeof object === 'string') { // TODO: move getStringSubset into a separate util file, use that return subset(object, index); } else if (typeof object === 'object') { if (!index.isObjectProperty()) { throw TypeError('Cannot apply a numeric index as object property'); } return object[index.getObjectProperty()]; } else { throw new TypeError('Cannot apply index: unsupported type of object'); } } catch (err) { throw errorTransform(err); } } } exports.factory = factory; /***/ }), /* 91 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var object = util.object; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var abs = load(__webpack_require__(25)); var addScalar = load(__webpack_require__(17)); var divideScalar = load(__webpack_require__(16)); var multiplyScalar = load(__webpack_require__(20)); var subtract = load(__webpack_require__(18)); var larger = load(__webpack_require__(26)); var equalScalar = load(__webpack_require__(8)); var unaryMinus = load(__webpack_require__(36)); var SparseMatrix = type.SparseMatrix; var DenseMatrix = type.DenseMatrix; var Spa = type.Spa; /** * Calculate the Matrix LU decomposition with partial pivoting. Matrix `A` is decomposed in two matrices (`L`, `U`) and a * row permutation vector `p` where `A[p,:] = L * U` * * Syntax: * * math.lup(A); * * Example: * * var m = [[2, 1], [1, 4]]; * var r = math.lup(); * // r = { * // L: [[1, 0], [0.5, 1]], * // U: [[2, 1], [0, 3.5]], * // P: [0, 1] * // } * * See also: * * slu, lsolve, lusolve, usolve * * @param {Matrix | Array} A A two dimensional matrix or array for which to get the LUP decomposition. * * @return {Array} The lower triangular matrix, the upper triangular matrix and the permutation matrix. */ var lup = typed('lup', { 'DenseMatrix': function (m) { return _denseLUP(m); }, 'SparseMatrix': function (m) { return _sparseLUP(m); }, 'Array': function (a) { // create dense matrix from array var m = matrix(a); // lup, use matrix implementation var r = _denseLUP(m); // result return { L: r.L.valueOf(), U: r.U.valueOf(), p: r.p }; } }); var _denseLUP = function (m) { // rows & columns var rows = m._size[0]; var columns = m._size[1]; // minimum rows and columns var n = Math.min(rows, columns); // matrix array, clone original data var data = object.clone(m._data); // l matrix arrays var ldata = []; var lsize = [rows, n]; // u matrix arrays var udata = []; var usize = [n, columns]; // vars var i, j, k; // permutation vector var p = []; for (i = 0; i < rows; i++) p[i] = i; // loop columns for (j = 0; j < columns; j++) { // skip first column in upper triangular matrix if (j > 0) { // loop rows for (i = 0; i < rows; i++) { // min i,j var min = Math.min(i, j); // v[i, j] var s = 0; // loop up to min for (k = 0; k < min; k++) { // s = l[i, k] - data[k, j] s = addScalar(s, multiplyScalar(data[i][k], data[k][j])); } data[i][j] = subtract(data[i][j], s); } } // row with larger value in cvector, row >= j var pi = j; var pabsv = 0; var vjj = 0; // loop rows for (i = j; i < rows; i++) { // data @ i, j var v = data[i][j]; // absolute value var absv = abs(v); // value is greater than pivote value if (larger(absv, pabsv)) { // store row pi = i; // update max value pabsv = absv; // value @ [j, j] vjj = v; } } // swap rows (j <-> pi) if (j !== pi) { // swap values j <-> pi in p p[j] = [p[pi], p[pi] = p[j]][0]; // swap j <-> pi in data DenseMatrix._swapRows(j, pi, data); } // check column is in lower triangular matrix if (j < rows) { // loop rows (lower triangular matrix) for (i = j + 1; i < rows; i++) { // value @ i, j var vij = data[i][j]; if (!equalScalar(vij, 0)) { // update data data[i][j] = divideScalar(data[i][j], vjj); } } } } // loop columns for (j = 0; j < columns; j++) { // loop rows for (i = 0; i < rows; i++) { // initialize row in arrays if (j === 0) { // check row exists in upper triangular matrix if (i < columns) { // U udata[i] = []; } // L ldata[i] = []; } // check we are in the upper triangular matrix if (i < j) { // check row exists in upper triangular matrix if (i < columns) { // U udata[i][j] = data[i][j]; } // check column exists in lower triangular matrix if (j < rows) { // L ldata[i][j] = 0; } continue; } // diagonal value if (i === j) { // check row exists in upper triangular matrix if (i < columns) { // U udata[i][j] = data[i][j]; } // check column exists in lower triangular matrix if (j < rows) { // L ldata[i][j] = 1; } continue; } // check row exists in upper triangular matrix if (i < columns) { // U udata[i][j] = 0; } // check column exists in lower triangular matrix if (j < rows) { // L ldata[i][j] = data[i][j]; } } } // l matrix var l = new DenseMatrix({ data: ldata, size: lsize }); // u matrix var u = new DenseMatrix({ data: udata, size: usize }); // p vector var pv = []; for (i = 0, n = p.length; i < n; i++) pv[p[i]] = i; // return matrices return { L: l, U: u, p: pv, toString: function () { return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p; } }; }; var _sparseLUP = function (m) { // rows & columns var rows = m._size[0]; var columns = m._size[1]; // minimum rows and columns var n = Math.min(rows, columns); // matrix arrays (will not be modified, thanks to permutation vector) var values = m._values; var index = m._index; var ptr = m._ptr; // l matrix arrays var lvalues = []; var lindex = []; var lptr = []; var lsize = [rows, n]; // u matrix arrays var uvalues = []; var uindex = []; var uptr = []; var usize = [n, columns]; // vars var i, j, k; // permutation vectors, (current index -> original index) and (original index -> current index) var pv_co = []; var pv_oc = []; for (i = 0; i < rows; i++) { pv_co[i] = i; pv_oc[i] = i; } // swap indices in permutation vectors (condition x < y)! var swapIndeces = function (x, y) { // find pv indeces getting data from x and y var kx = pv_oc[x]; var ky = pv_oc[y]; // update permutation vector current -> original pv_co[kx] = y; pv_co[ky] = x; // update permutation vector original -> current pv_oc[x] = ky; pv_oc[y] = kx; }; // loop columns for (j = 0; j < columns; j++) { // sparse accumulator var spa = new Spa(); // check lower triangular matrix has a value @ column j if (j < rows) { // update ptr lptr.push(lvalues.length); // first value in j column for lower triangular matrix lvalues.push(1); lindex.push(j); } // update ptr uptr.push(uvalues.length); // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = ptr[j]; var k1 = ptr[j + 1]; // copy column j into sparse accumulator for (k = k0; k < k1; k++) { // row i = index[k]; // copy column values into sparse accumulator (use permutation vector) spa.set(pv_co[i], values[k]); } // skip first column in upper triangular matrix if (j > 0) { // loop rows in column j (above diagonal) spa.forEach(0, j - 1, function (k, vkj) { // loop rows in column k (L) SparseMatrix._forEachRow(k, lvalues, lindex, lptr, function (i, vik) { // check row is below k if (i > k) { // update spa value spa.accumulate(i, unaryMinus(multiplyScalar(vik, vkj))); } }); }); } // row with larger value in spa, row >= j var pi = j; var vjj = spa.get(j); var pabsv = abs(vjj); // loop values in spa (order by row, below diagonal) spa.forEach(j + 1, rows - 1, function (x, v) { // absolute value var absv = abs(v); // value is greater than pivote value if (larger(absv, pabsv)) { // store row pi = x; // update max value pabsv = absv; // value @ [j, j] vjj = v; } }); // swap rows (j <-> pi) if (j !== pi) { // swap values j <-> pi in L SparseMatrix._swapRows(j, pi, lsize[1], lvalues, lindex, lptr); // swap values j <-> pi in U SparseMatrix._swapRows(j, pi, usize[1], uvalues, uindex, uptr); // swap values in spa spa.swap(j, pi); // update permutation vector (swap values @ j, pi) swapIndeces(j, pi); } // loop values in spa (order by row) spa.forEach(0, rows - 1, function (x, v) { // check we are above diagonal if (x <= j) { // update upper triangular matrix uvalues.push(v); uindex.push(x); } else { // update value v = divideScalar(v, vjj); // check value is non zero if (!equalScalar(v, 0)) { // update lower triangular matrix lvalues.push(v); lindex.push(x); } } }); } // update ptrs uptr.push(uvalues.length); lptr.push(lvalues.length); // return matrices return { L: new SparseMatrix({ values: lvalues, index: lindex, ptr: lptr, size: lsize }), U: new SparseMatrix({ values: uvalues, index: uindex, ptr: uptr, size: usize }), p: pv_co, toString: function () { return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p; } }; }; return lup; } exports.name = 'lup'; exports.factory = factory; /***/ }), /* 92 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var number = util.number, isInteger = number.isInteger; function factory (type, config, load, typed) { var cs_sqr = load(__webpack_require__(356)); var cs_lu = load(__webpack_require__(351)); /** * Calculate the Sparse Matrix LU decomposition with full pivoting. Sparse Matrix `A` is decomposed in two matrices (`L`, `U`) and two permutation vectors (`pinv`, `q`) where * * `P * A * Q = L * U` * * Syntax: * * math.slu(A, order, threshold); * * See also: * * lup, lsolve, usolve, lusolve * * @param {SparseMatrix} A A two dimensional sparse matrix for which to get the LU decomposition. * @param {Number} order The Symbolic Ordering and Analysis order: * 0 - Natural ordering, no permutation vector q is returned * 1 - Matrix must be square, symbolic ordering and analisis is performed on M = A + A' * 2 - Symbolic ordering and analisis is performed on M = A' * A. Dense columns from A' are dropped, A recreated from A'. * This is appropriatefor LU factorization of unsymmetric matrices. * 3 - Symbolic ordering and analisis is performed on M = A' * A. This is best used for LU factorization is matrix M has no dense rows. * A dense row is a row with more than 10*sqr(columns) entries. * @param {Number} threshold Partial pivoting threshold (1 for partial pivoting) * * @return {Object} The lower triangular matrix, the upper triangular matrix and the permutation vectors. */ var slu = typed('slu', { 'SparseMatrix, number, number': function (a, order, threshold) { // verify order if (!isInteger(order) || order < 0 || order > 3) throw new Error('Symbolic Ordering and Analysis order must be an integer number in the interval [0, 3]'); // verify threshold if (threshold < 0 || threshold > 1) throw new Error('Partial pivoting threshold must be a number from 0 to 1'); // perform symbolic ordering and analysis var s = cs_sqr(order, a, false); // perform lu decomposition var f = cs_lu(a, s, threshold); // return decomposition return { L: f.L, U: f.U, p: f.pinv, q: s.q, toString: function () { return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\np: ' + this.p.toString() + (this.q ? '\nq: ' + this.q.toString() : '') + '\n'; } }; } }); return slu; } exports.name = 'slu'; exports.factory = factory; /***/ }), /* 93 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var parse = load(__webpack_require__(28)); var ConstantNode = load(__webpack_require__(43)); var FunctionNode = load(__webpack_require__(44)); var OperatorNode = load(__webpack_require__(48)); var ParenthesisNode = load(__webpack_require__(49)); var SymbolNode = load(__webpack_require__(24)); var Node = load(__webpack_require__(11)); var simplifyConstant = load(__webpack_require__(342)); var util = load(__webpack_require__(94)); var isCommutative = util.isCommutative; var isAssociative = util.isAssociative; var flatten = util.flatten; var unflattenr = util.unflattenr; var unflattenl = util.unflattenl; var createMakeNodeFunction = util.createMakeNodeFunction; /** * Simplify an expression tree. * * It's possible to pass a custom set of rules to the function as second * argument. A rule can be specified as an object, string, or function: * * var rules = [ * { l: 'n1*n3 + n2*n3', r: '(n1+n2)*n3' }, * 'n1*n3 + n2*n3 -> (n1+n2)*n3', * function (node) { * // ... return a new node or return the node unchanged * return node * } * ] * * * The default list with rules is exposed on the function as `simplify.rules` * and can be used as a basis to built a set of custom rules. * * For more details on the theory, see: * * - [Strategies for simplifying math expressions (Stackoverflow)](http://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions) * - [Symbolic computation - Simplification (Wikipedia)](https://en.wikipedia.org/wiki/Symbolic_computation#Simplification) * * Syntax: * * simplify(expr) * simplify(expr, rules) * * Examples: * * math.simplify('2 * 1 * x ^ (2 - 1)'); // Node {2 * x} * var f = math.parse('2 * 1 * x ^ (2 - 1)'); * math.simplify(f); // Node {2 * x} * * See also: * * derivative, parse, eval * * @param {Node | string} expr * The expression to be simplified * @param {Array<{l:string, r: string} | string | function>} [rules] * Optional list with custom rules * @return {Node} Returns the simplified form of `expr` */ var simplify = typed('simplify', { 'string': function (expr) { return simplify(parse(expr), simplify.rules); }, 'string, Array': function (expr, rules) { return simplify(parse(expr), rules); }, 'Node': function (expr) { return simplify(expr, simplify.rules); }, 'Node, Array': function (expr, rules) { rules = _buildRules(rules); var res = removeParens(expr); var after = res.toString({parenthesis: 'all'}); var before = null; while(before != after) { lastsym = 0; before = after; for (var i=0; i c1 * n1' */ function _buildRules(rules) { // Array of rules to be used to simplify expressions var ruleSet = []; for(var i=0; i'); if (lr.length !== 2) { throw SyntaxError('Could not parse rule: ' + rule); } rule = {l: lr[0], r: lr[1]}; /* falls through */ case 'object': newRule = { l: removeParens(parse(rule.l)), r: removeParens(parse(rule.r)), } if(rule.context) { newRule.evaluate = rule.context; } if(rule.evaluate) { newRule.evaluate = parse(rule.evaluate); } if (newRule.l.isOperatorNode && isAssociative(newRule.l)) { var makeNode = createMakeNodeFunction(newRule.l); var expandsym = _getExpandPlaceholderSymbol(); newRule.expanded = {}; newRule.expanded.l = makeNode([newRule.l.clone(), expandsym]); // Push the expandsym into the deepest possible branch. // This helps to match the newRule against nodes returned from getSplits() later on. flatten(newRule.expanded.l); unflattenr(newRule.expanded.l); newRule.expanded.r = makeNode([newRule.r, expandsym]); } break; case 'function': newRule = rule; break; default: throw TypeError('Unsupported type of rule: ' + ruleType); } // console.log('Adding rule: ' + rules[i]); // console.log(newRule); ruleSet.push(newRule); } return ruleSet; } var lastsym = 0; function _getExpandPlaceholderSymbol() { return new SymbolNode('_p'+lastsym++); } /** * Returns a simplfied form of node, or the original node if no simplification was possible. * * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node * @return {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} The simplified form of `expr`, or the original node if no simplification was possible. */ var applyRule = typed('applyRule', { 'Node, Object': function (node, rule) { //console.log('Entering applyRule(' + node.toString() + ')'); // Do not clone node unless we find a match var res = node; // First replace our child nodes with their simplified versions // If a child could not be simplified, the assignments will have // no effect since the node is returned unchanged if (res instanceof OperatorNode || res instanceof FunctionNode) { if (res.args) { for(var i=0; i [ * +(node1, +(node2, node3)), * +(node2, +(node1, node3)), * +(node3, +(node1, node2))] * */ function getSplits(node, context) { var res = []; var right, rightArgs; var makeNode = createMakeNodeFunction(node); if (isCommutative(node, context)) { for (var i=0; i= 2 && rule.args.length === 2) { // node is flattened, rule is not // Associative operators/functions can be split in different ways so we check if the rule matches each // them and return their union. var splits = getSplits(node, rule.context); var splitMatches = []; for(var i = 0; i < splits.length; i++) { var matchSet = _ruleMatch(rule, splits[i], true); // recursing at the same tree depth here splitMatches = splitMatches.concat(matchSet); } return splitMatches; } else if (rule.args.length > 2) { throw Error('Unexpected non-binary associative function: ' + rule.toString()); } else { // Incorrect number of arguments in rule and node, so no match return []; } } else if (rule instanceof SymbolNode) { // If the rule is a SymbolNode, then it carries a special meaning // according to the first character of the symbol node name. // c.* matches a ConstantNode // n.* matches any node if (rule.name.length === 0) { throw new Error('Symbol in rule has 0 length...!?'); } if (rule.name[0] == 'n' || rule.name.substring(0,2) == '_p') { // rule matches _anything_, so assign this node to the rule.name placeholder // Assign node to the rule.name placeholder. // Our parent will check for matches among placeholders. res[0].placeholders[rule.name] = node; } else if (rule.name[0] == 'v') { // rule matches any variable thing (not a ConstantNode) if(!node.isConstantNode) { res[0].placeholders[rule.name] = node; } else { // Mis-match: rule was expecting something other than a ConstantNode return []; } } else if (rule.name[0] == 'C') { // rule matches anything but a ConstantNode if(node instanceof ConstantNode) { // Mis-match: rule was expecting not a ConstantNode return []; } else { res[0].placeholders[rule.name] = node; } } else if (rule.name[0] == 'c') { // rule matches any ConstantNode if(node instanceof ConstantNode) { res[0].placeholders[rule.name] = node; } else { // Mis-match: rule was expecting a ConstantNode return []; } } else { throw new Error('Invalid symbol in rule: ' + rule.name); } } else if (rule instanceof ConstantNode) { // Literal constant in our rule, so much match node exactly if(rule.value === node.value) { // The constants match } else { return []; } } else { // Some other node was encountered which we aren't prepared for, so no match return []; } // It's a match! // console.log('_ruleMatch(' + rule.toString() + ', ' + node.toString() + ') found a match'); return res; } /** * Determines whether p and q (and all their children nodes) are identical. * * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} p * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} q * @return {Object} Information about the match, if it exists. */ function _exactMatch(p, q) { if(p instanceof ConstantNode && q instanceof ConstantNode) { if(p.value !== q.value) { return false; } } else if(p instanceof SymbolNode && q instanceof SymbolNode) { if(p.name !== q.name) { return false; } } else if(p instanceof OperatorNode && q instanceof OperatorNode || p instanceof FunctionNode && q instanceof FunctionNode) { if (p instanceof OperatorNode) { if (p.op !== q.op || p.fn !== q.fn) { return false; } } else if (p instanceof FunctionNode) { if (p.name !== q.name) { return false; } } if(p.args.length !== q.args.length) { return false; } for(var i=0; i 2 && isAssociative(node)) { var curnode = node.args.pop(); while (node.args.length > 0) { curnode = makeNode([node.args.pop(), curnode]); } node.args = curnode.args; } } /** * Unflatten all flattened operators to a left-heavy binary tree. */ function unflattenl(node) { if (!node.args || node.args.length === 0) { return; } var makeNode = createMakeNodeFunction(node); var l = node.args.length; for (var i = 0; i < l; i++) { unflattenl(node.args[i]) } if (l > 2 && isAssociative(node)) { var curnode = node.args.shift(); while (node.args.length > 0) { curnode = makeNode([curnode, node.args.shift()]); } node.args = curnode.args; } } function createMakeNodeFunction(node) { if (node.isOperatorNode) { return function(args){ try{ return new OperatorNode(node.op, node.fn, args); } catch(err){ console.error(err); return []; } }; } else { return function(args){ return new FunctionNode(new SymbolNode(node.name), args); }; } } return { createMakeNodeFunction: createMakeNodeFunction, isCommutative: isCommutative, isAssociative: isAssociative, flatten: flatten, allChildren: allChildren, unflattenr: unflattenr, unflattenl: unflattenl }; } exports.factory = factory; exports.math = true; /***/ }), /* 95 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var divideScalar = load(__webpack_require__(16)); var multiplyScalar = load(__webpack_require__(20)); var subtract = load(__webpack_require__(18)); var equalScalar = load(__webpack_require__(8)); var solveValidation = load(__webpack_require__(64)); var DenseMatrix = type.DenseMatrix; /** * Solves the linear equation system by forwards substitution. Matrix must be a lower triangular matrix. * * `L * x = b` * * Syntax: * * math.lsolve(L, b); * * Examples: * * var a = [[-2, 3], [2, 1]]; * var b = [11, 9]; * var x = lsolve(a, b); // [[-5.5], [20]] * * See also: * * lup, slu, usolve, lusolve * * @param {Matrix, Array} L A N x N matrix or array (L) * @param {Matrix, Array} b A column vector with the b values * * @return {DenseMatrix | Array} A column vector with the linear system solution (x) */ var lsolve = typed('lsolve', { 'SparseMatrix, Array | Matrix': function (m, b) { // process matrix return _sparseForwardSubstitution(m, b); }, 'DenseMatrix, Array | Matrix': function (m, b) { // process matrix return _denseForwardSubstitution(m, b); }, 'Array, Array | Matrix': function (a, b) { // create dense matrix from array var m = matrix(a); // use matrix implementation var r = _denseForwardSubstitution(m, b); // result return r.valueOf(); } }); var _denseForwardSubstitution = function (m, b) { // validate matrix and vector, return copy of column vector b b = solveValidation(m, b, true); // column vector data var bdata = b._data; // rows & columns var rows = m._size[0]; var columns = m._size[1]; // result var x = []; // data var data = m._data; // forward solve m * x = b, loop columns for (var j = 0; j < columns; j++) { // b[j] var bj = bdata[j][0] || 0; // x[j] var xj; // forward substitution (outer product) avoids inner looping when bj == 0 if (!equalScalar(bj, 0)) { // value @ [j, j] var vjj = data[j][j]; // check vjj if (equalScalar(vjj, 0)) { // system cannot be solved throw new Error('Linear system cannot be solved since matrix is singular'); } // calculate xj xj = divideScalar(bj, vjj); // loop rows for (var i = j + 1; i < rows; i++) { // update copy of b bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, data[i][j]))]; } } else { // zero @ j xj = 0; } // update x x[j] = [xj]; } // return vector return new DenseMatrix({ data: x, size: [rows, 1] }); }; var _sparseForwardSubstitution = function (m, b) { // validate matrix and vector, return copy of column vector b b = solveValidation(m, b, true); // column vector data var bdata = b._data; // rows & columns var rows = m._size[0]; var columns = m._size[1]; // matrix arrays var values = m._values; var index = m._index; var ptr = m._ptr; // vars var i, k; // result var x = []; // forward solve m * x = b, loop columns for (var j = 0; j < columns; j++) { // b[j] var bj = bdata[j][0] || 0; // forward substitution (outer product) avoids inner looping when bj == 0 if (!equalScalar(bj, 0)) { // value @ [j, j] var vjj = 0; // lower triangular matrix values & index (column j) var jvalues = []; var jindex = []; // last index in column var l = ptr[j + 1]; // values in column, find value @ [j, j] for (k = ptr[j]; k < l; k++) { // row i = index[k]; // check row (rows are not sorted!) if (i === j) { // update vjj vjj = values[k]; } else if (i > j) { // store lower triangular jvalues.push(values[k]); jindex.push(i); } } // at this point we must have a value @ [j, j] if (equalScalar(vjj, 0)) { // system cannot be solved, there is no value @ [j, j] throw new Error('Linear system cannot be solved since matrix is singular'); } // calculate xj var xj = divideScalar(bj, vjj); // loop lower triangular for (k = 0, l = jindex.length; k < l; k++) { // row i = jindex[k]; // update copy of b bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, jvalues[k]))]; } // update x x[j] = [xj]; } else { // update x x[j] = [0]; } } // return vector return new DenseMatrix({ data: x, size: [rows, 1] }); }; return lsolve; } exports.name = 'lsolve'; exports.factory = factory; /***/ }), /* 96 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var divideScalar = load(__webpack_require__(16)); var multiplyScalar = load(__webpack_require__(20)); var subtract = load(__webpack_require__(18)); var equalScalar = load(__webpack_require__(8)); var solveValidation = load(__webpack_require__(64)); var DenseMatrix = type.DenseMatrix; /** * Solves the linear equation system by backward substitution. Matrix must be an upper triangular matrix. * * `U * x = b` * * Syntax: * * math.usolve(U, b); * * Examples: * * var a = [[-2, 3], [2, 1]]; * var b = [11, 9]; * var x = usolve(a, b); // [[8], [9]] * * See also: * * lup, slu, usolve, lusolve * * @param {Matrix, Array} U A N x N matrix or array (U) * @param {Matrix, Array} b A column vector with the b values * * @return {DenseMatrix | Array} A column vector with the linear system solution (x) */ var usolve = typed('usolve', { 'SparseMatrix, Array | Matrix': function (m, b) { // process matrix return _sparseBackwardSubstitution(m, b); }, 'DenseMatrix, Array | Matrix': function (m, b) { // process matrix return _denseBackwardSubstitution(m, b); }, 'Array, Array | Matrix': function (a, b) { // create dense matrix from array var m = matrix(a); // use matrix implementation var r = _denseBackwardSubstitution(m, b); // result return r.valueOf(); } }); var _denseBackwardSubstitution = function (m, b) { // validate matrix and vector, return copy of column vector b b = solveValidation(m, b, true); // column vector data var bdata = b._data; // rows & columns var rows = m._size[0]; var columns = m._size[1]; // result var x = []; // arrays var data = m._data; // backward solve m * x = b, loop columns (backwards) for (var j = columns - 1; j >= 0 ; j--) { // b[j] var bj = bdata[j][0] || 0; // x[j] var xj; // backward substitution (outer product) avoids inner looping when bj == 0 if (!equalScalar(bj, 0)) { // value @ [j, j] var vjj = data[j][j]; // check vjj if (equalScalar(vjj, 0)) { // system cannot be solved throw new Error('Linear system cannot be solved since matrix is singular'); } // calculate xj xj = divideScalar(bj, vjj); // loop rows for (var i = j - 1; i >= 0; i--) { // update copy of b bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, data[i][j]))]; } } else { // zero value @ j xj = 0; } // update x x[j] = [xj]; } // return column vector return new DenseMatrix({ data: x, size: [rows, 1] }); }; var _sparseBackwardSubstitution = function (m, b) { // validate matrix and vector, return copy of column vector b b = solveValidation(m, b, true); // column vector data var bdata = b._data; // rows & columns var rows = m._size[0]; var columns = m._size[1]; // matrix arrays var values = m._values; var index = m._index; var ptr = m._ptr; // vars var i, k; // result var x = []; // backward solve m * x = b, loop columns (backwards) for (var j = columns - 1; j >= 0 ; j--) { // b[j] var bj = bdata[j][0] || 0; // backward substitution (outer product) avoids inner looping when bj == 0 if (!equalScalar(bj, 0)) { // value @ [j, j] var vjj = 0; // upper triangular matrix values & index (column j) var jvalues = []; var jindex = []; // first & last indeces in column var f = ptr[j]; var l = ptr[j + 1]; // values in column, find value @ [j, j], loop backwards for (k = l - 1; k >= f; k--) { // row i = index[k]; // check row if (i === j) { // update vjj vjj = values[k]; } else if (i < j) { // store upper triangular jvalues.push(values[k]); jindex.push(i); } } // at this point we must have a value @ [j, j] if (equalScalar(vjj, 0)) { // system cannot be solved, there is no value @ [j, j] throw new Error('Linear system cannot be solved since matrix is singular'); } // calculate xj var xj = divideScalar(bj, vjj); // loop upper triangular for (k = 0, l = jindex.length; k < l; k++) { // row i = jindex[k]; // update copy of b bdata[i] = [subtract(bdata[i][0], multiplyScalar(xj, jvalues[k]))]; } // update x x[j] = [xj]; } else { // update x x[j] = [0]; } } // return vector return new DenseMatrix({ data: x, size: [rows, 1] }); }; return usolve; } exports.name = 'usolve'; exports.factory = factory; /***/ }), /* 97 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var cs_flip = load(__webpack_require__(65)); /** * Marks the node at w[j] * * @param {Array} w The array * @param {Number} j The array index * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_mark = function (w, j) { // mark w[j] w[j] = cs_flip(w [j]); }; return cs_mark; } exports.name = 'cs_mark'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 98 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory () { /** * Checks if the node at w[j] is marked * * @param {Array} w The array * @param {Number} j The array index * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_marked = function (w, j) { // check node is marked return w[j] < 0; }; return cs_marked; } exports.name = 'cs_marked'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 99 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory () { /** * Depth-first search and postorder of a tree rooted at node j * * @param {Number} j The tree node * @param {Number} k * @param {Array} w The workspace array * @param {Number} head The index offset within the workspace for the head array * @param {Number} next The index offset within the workspace for the next array * @param {Array} post The post ordering array * @param {Number} stack The index offset within the workspace for the stack array * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_tdfs = function (j, k, w, head, next, post, stack) { // variables var top = 0; // place j on the stack w[stack] = j; // while (stack is not empty) while (top >= 0) { // p = top of stack var p = w[stack + top]; // i = youngest child of p var i = w[head + p]; if (i == -1) { // p has no unordered children left top--; // node p is the kth postordered node post[k++] = p; } else { // remove i from children of p w[head + p] = w[next + i]; // increment top ++top; // start dfs on child node i w[stack + top] = i; } } return k; }; return cs_tdfs; } exports.name = 'cs_tdfs'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 100 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var divideScalar = load(__webpack_require__(16)); var latex = __webpack_require__(3); var algorithm02 = load(__webpack_require__(21)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm11 = load(__webpack_require__(13)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Divide two matrices element wise. The function accepts both matrices and * scalar values. * * Syntax: * * math.dotDivide(x, y) * * Examples: * * math.dotDivide(2, 4); // returns 0.5 * * a = [[9, 5], [6, 1]]; * b = [[3, 2], [5, 2]]; * * math.dotDivide(a, b); // returns [[3, 2.5], [1.2, 0.5]] * math.divide(a, b); // returns [[1.75, 0.75], [-1.75, 2.25]] * * See also: * * divide, multiply, dotMultiply * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Numerator * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Denominator * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Quotient, `x ./ y` */ var dotDivide = typed('dotDivide', { 'any, any': divideScalar, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse ./ sparse c = algorithm07(x, y, divideScalar, false); break; default: // sparse ./ dense c = algorithm02(y, x, divideScalar, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense ./ sparse c = algorithm03(x, y, divideScalar, false); break; default: // dense ./ dense c = algorithm13(x, y, divideScalar); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return dotDivide(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return dotDivide(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return dotDivide(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, divideScalar, false); break; default: c = algorithm14(x, y, divideScalar, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, divideScalar, true); break; default: c = algorithm14(y, x, divideScalar, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, divideScalar, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, divideScalar, true).valueOf(); } }); dotDivide.toTex = { 2: '\\left(${args[0]}' + latex.operators['dotDivide'] + '${args[1]}\\right)' }; return dotDivide; } exports.name = 'dotDivide'; exports.factory = factory; /***/ }), /* 101 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Round a value towards zero. * For matrices, the function is evaluated element wise. * * Syntax: * * math.fix(x) * * Examples: * * math.fix(3.2); // returns number 3 * math.fix(3.8); // returns number 3 * math.fix(-4.2); // returns number -4 * math.fix(-4.7); // returns number -4 * * var c = math.complex(3.2, -2.7); * math.fix(c); // returns Complex 3 - 2i * * math.fix([3.2, 3.8, -4.7]); // returns Array [3, 3, -4] * * See also: * * ceil, floor, round * * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value */ var fix = typed('fix', { 'number': function (x) { return (x > 0) ? Math.floor(x) : Math.ceil(x); }, 'Complex': function (x) { return new type.Complex( (x.re > 0) ? Math.floor(x.re) : Math.ceil(x.re), (x.im > 0) ? Math.floor(x.im) : Math.ceil(x.im) ); }, 'BigNumber': function (x) { return x.isNegative() ? x.ceil() : x.floor(); }, 'Fraction': function (x) { return x.s < 0 ? x.ceil() : x.floor(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since fix(0) = 0 return deepMap(x, fix, true); } }); fix.toTex = {1: '\\mathrm{${name}}\\left(${args[0]}\\right)'}; return fix; } exports.name = 'fix'; exports.factory = factory; /***/ }), /* 102 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var divideScalar = load(__webpack_require__(16)); /** * Calculate the logarithm of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.log(x) * math.log(x, base) * * Examples: * * math.log(3.5); // returns 1.252762968495368 * math.exp(math.log(2.4)); // returns 2.4 * * math.pow(10, 4); // returns 10000 * math.log(10000, 10); // returns 4 * math.log(10000) / math.log(10); // returns 4 * * math.log(1024, 2); // returns 10 * math.pow(2, 10); // returns 1024 * * See also: * * exp, log10 * * @param {number | BigNumber | Complex | Array | Matrix} x * Value for which to calculate the logarithm. * @param {number | BigNumber | Complex} [base=e] * Optional base for the logarithm. If not provided, the natural * logarithm of `x` is calculated. * @return {number | BigNumber | Complex | Array | Matrix} * Returns the logarithm of `x` */ var log = typed('log', { 'number': function (x) { if (x >= 0 || config.predictable) { return Math.log(x); } else { // negative value -> complex value computation return new type.Complex(x, 0).log(); } }, 'Complex': function (x) { return x.log(); }, 'BigNumber': function (x) { if (!x.isNegative() || config.predictable) { return x.ln(); } else { // downgrade to number, return Complex valued result return new type.Complex(x.toNumber(), 0).log(); } }, 'Array | Matrix': function (x) { return deepMap(x, log); }, 'any, any': function (x, base) { // calculate logarithm for a specified base, log(x, base) return divideScalar(log(x), log(base)); } }); log.toTex = { 1: '\\ln\\left(${args[0]}\\right)', 2: '\\log_{${args[1]}}\\left(${args[0]}\\right)' }; return log; } exports.name = 'log'; exports.factory = factory; /***/ }), /* 103 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var add = load(__webpack_require__(15)); var subtract = load(__webpack_require__(18)); var multiply = load(__webpack_require__(10)); var divide = load(__webpack_require__(34)); var pow = load(__webpack_require__(35)); var factorial = load(__webpack_require__(54)); var combinations = load(__webpack_require__(53)); var isNegative = load(__webpack_require__(47)); var isInteger = load(__webpack_require__(39)); var larger = load(__webpack_require__(26)); /** * The Stirling numbers of the second kind, counts the number of ways to partition * a set of n labelled objects into k nonempty unlabelled subsets. * stirlingS2 only takes integer arguments. * The following condition must be enforced: k <= n. * * If n = k or k = 1, then s(n,k) = 1 * * Syntax: * * math.stirlingS2(n, k) * * Examples: * * math.stirlingS2(5, 3); //returns 25 * * See also: * * Bell numbers * * @param {Number | BigNumber} n Total number of objects in the set * @param {Number | BigNumber} k Number of objects in the subset * @return {Number | BigNumber} S(n,k) */ var stirlingS2 = typed('stirlingS2', { 'number | BigNumber, number | BigNumber': function (n, k) { if (!isInteger(n) || isNegative(n) || !isInteger(k) || isNegative(k)) { throw new TypeError('Non-negative integer value expected in function stirlingS2'); } else if (larger(k, n)) { throw new TypeError('k must be less than or equal to n in function stirlingS2'); } // 1/k! Sum(i=0 -> k) [(-1)^(k-i)*C(k,j)* i^n] var kFactorial = factorial(k); var result = 0; for(var i = 0; i <= k; i++) { var negativeOne = pow(-1, subtract(k,i)); var kChooseI = combinations(k,i); var iPower = pow(i,n); result = add(result, multiply(multiply(kChooseI, iPower), negativeOne)); } return divide(result, kFactorial); } }); stirlingS2.toTex = {2: '\\mathrm{S}\\left(${args}\\right)'}; return stirlingS2; } exports.name = 'stirlingS2'; exports.factory = factory; /***/ }), /* 104 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var latex = __webpack_require__(3); /** * Logical `not`. Flips boolean value of a given parameter. * For matrices, the function is evaluated element wise. * * Syntax: * * math.not(x) * * Examples: * * math.not(2); // returns false * math.not(0); // returns true * math.not(true); // returns false * * a = [2, -7, 0]; * math.not(a); // returns [false, false, true] * * See also: * * and, or, xor * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check * @return {boolean | Array | Matrix} * Returns true when input is a zero or empty value. */ var not = typed('not', { 'number': function (x) { return !x; }, 'Complex': function (x) { return x.re === 0 && x.im === 0; }, 'BigNumber': function (x) { return x.isZero() || x.isNaN(); }, 'Unit': function (x) { return not(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, not); } }); not.toTex = { 1: latex.operators['not'] + '\\left(${args[0]}\\right)' }; return not; } exports.name = 'not'; exports.factory = factory; /***/ }), /* 105 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var clone = __webpack_require__(4).clone; var isInteger = __webpack_require__(2).isInteger; var array = __webpack_require__(7); var IndexError = __webpack_require__(42); var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Concatenate two or more matrices. * * Syntax: * * math.concat(A, B, C, ...) * math.concat(A, B, C, ..., dim) * * Where: * * - `dim: number` is a zero-based dimension over which to concatenate the matrices. * By default the last dimension of the matrices. * * Examples: * * var A = [[1, 2], [5, 6]]; * var B = [[3, 4], [7, 8]]; * * math.concat(A, B); // returns [[1, 2, 3, 4], [5, 6, 7, 8]] * math.concat(A, B, 0); // returns [[1, 2], [5, 6], [3, 4], [7, 8]] * math.concat('hello', ' ', 'world'); // returns 'hello world' * * See also: * * size, squeeze, subset, transpose * * @param {... Array | Matrix} args Two or more matrices * @return {Array | Matrix} Concatenated matrix */ var concat = typed('concat', { // TODO: change signature to '...Array | Matrix, dim?' when supported '...Array | Matrix | number | BigNumber': function (args) { var i; var len = args.length; var dim = -1; // zero-based dimension var prevDim; var asMatrix = false; var matrices = []; // contains multi dimensional arrays for (i = 0; i < len; i++) { var arg = args[i]; // test whether we need to return a Matrix (if not we return an Array) if (arg && arg.isMatrix === true) { asMatrix = true; } if (typeof arg === 'number' || (arg && arg.isBigNumber === true)) { if (i !== len - 1) { throw new Error('Dimension must be specified as last argument'); } // last argument contains the dimension on which to concatenate prevDim = dim; dim = arg.valueOf(); // change BigNumber to number if (!isInteger(dim)) { throw new TypeError('Integer number expected for dimension'); } if (dim < 0 || (i > 0 && dim > prevDim)) { // TODO: would be more clear when throwing a DimensionError here throw new IndexError(dim, prevDim + 1); } } else { // this is a matrix or array var m = clone(arg).valueOf(); var size = array.size(m); matrices[i] = m; prevDim = dim; dim = size.length - 1; // verify whether each of the matrices has the same number of dimensions if (i > 0 && dim != prevDim) { throw new DimensionError(prevDim + 1, dim + 1); } } } if (matrices.length == 0) { throw new SyntaxError('At least one matrix expected'); } var res = matrices.shift(); while (matrices.length) { res = _concat(res, matrices.shift(), dim, 0); } return asMatrix ? matrix(res) : res; }, '...string': function (args) { return args.join(''); } }); concat.toTex = undefined; // use default template return concat; } /** * Recursively concatenate two matrices. * The contents of the matrices is not cloned. * @param {Array} a Multi dimensional array * @param {Array} b Multi dimensional array * @param {number} concatDim The dimension on which to concatenate (zero-based) * @param {number} dim The current dim (zero-based) * @return {Array} c The concatenated matrix * @private */ function _concat(a, b, concatDim, dim) { if (dim < concatDim) { // recurse into next dimension if (a.length != b.length) { throw new DimensionError(a.length, b.length); } var c = []; for (var i = 0; i < a.length; i++) { c[i] = _concat(a[i], b[i], concatDim, dim + 1); } return c; } else { // concatenate this dimension return a.concat(b); } } exports.name = 'concat'; exports.factory = factory; /***/ }), /* 106 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var object = util.object; var string = util.string; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var add = load(__webpack_require__(15)); var subtract = load(__webpack_require__(18)); var multiply = load(__webpack_require__(10)); var unaryMinus = load(__webpack_require__(36)); /** * Calculate the determinant of a matrix. * * Syntax: * * math.det(x) * * Examples: * * math.det([[1, 2], [3, 4]]); // returns -2 * * var A = [ * [-2, 2, 3], * [-1, 1, 3], * [2, 0, -1] * ] * math.det(A); // returns 6 * * See also: * * inv * * @param {Array | Matrix} x A matrix * @return {number} The determinant of `x` */ var det = typed('det', { 'any': function (x) { return object.clone(x); }, 'Array | Matrix': function det (x) { var size; if (x && x.isMatrix === true) { size = x.size(); } else if (Array.isArray(x)) { x = matrix(x); size = x.size(); } else { // a scalar size = []; } switch (size.length) { case 0: // scalar return object.clone(x); case 1: // vector if (size[0] == 1) { return object.clone(x.valueOf()[0]); } else { throw new RangeError('Matrix must be square ' + '(size: ' + string.format(size) + ')'); } case 2: // two dimensional array var rows = size[0]; var cols = size[1]; if (rows == cols) { return _det(x.clone().valueOf(), rows, cols); } else { throw new RangeError('Matrix must be square ' + '(size: ' + string.format(size) + ')'); } default: // multi dimensional array throw new RangeError('Matrix must be two dimensional ' + '(size: ' + string.format(size) + ')'); } } }); det.toTex = {1: '\\det\\left(${args[0]}\\right)'}; return det; /** * Calculate the determinant of a matrix * @param {Array[]} matrix A square, two dimensional matrix * @param {number} rows Number of rows of the matrix (zero-based) * @param {number} cols Number of columns of the matrix (zero-based) * @returns {number} det * @private */ function _det (matrix, rows, cols) { if (rows == 1) { // this is a 1 x 1 matrix return object.clone(matrix[0][0]); } else if (rows == 2) { // this is a 2 x 2 matrix // the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12 return subtract( multiply(matrix[0][0], matrix[1][1]), multiply(matrix[1][0], matrix[0][1]) ); } else { // this is an n x n matrix var compute_mu = function (matrix) { var i, j; // Compute the matrix with zero lower triangle, same upper triangle, // and diagonals given by the negated sum of the below diagonal // elements. var mu = new Array(matrix.length); var sum = 0; for (i = 1; i < matrix.length; i++) { sum = add(sum, matrix[i][i]); } for (i = 0; i < matrix.length; i++) { mu[i] = new Array(matrix.length); mu[i][i] = unaryMinus(sum); for (j = 0; j < i; j++) { mu[i][j] = 0; // TODO: make bignumber 0 in case of bignumber computation } for (j = i + 1; j < matrix.length; j++) { mu[i][j] = matrix[i][j]; } if (i+1 < matrix.length) { sum = subtract(sum, matrix[i + 1][i + 1]); } } return mu; }; var fa = matrix; for (var i = 0; i < rows - 1; i++) { fa = multiply(compute_mu(fa), matrix); } if (rows % 2 == 0) { return unaryMinus(fa[0][0]); } else { return fa[0][0]; } } } } exports.name = 'det'; exports.factory = factory; /***/ }), /* 107 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var size = __webpack_require__(7).size; var maxArgumentCount = __webpack_require__(33).maxArgumentCount; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Filter the items in an array or one dimensional matrix. * * Syntax: * * math.filter(x, test) * * Examples: * * function isPositive (x) { * return x > 0; * } * math.filter([6, -2, -1, 4, 3], isPositive); // returns [6, 4, 3] * * math.filter(["23", "foo", "100", "55", "bar"], /[0-9]+/); // returns ["23", "100", "55"] * * See also: * * forEach, map, sort * * @param {Matrix | Array} x A one dimensional matrix or array to filter * @param {Function | RegExp} test * A function or regular expression to test items. * All entries for which `test` returns true are returned. * When `test` is a function, it is invoked with three parameters: * the value of the element, the index of the element, and the * matrix/array being traversed. The function must return a boolean. * @return {Matrix | Array} Returns the filtered matrix. */ var filter = typed('filter', { 'Array, function': _filterCallback, 'Array, RegExp': _filterRegExp, 'Matrix, function': function (x, test) { return matrix(_filterCallback(x.toArray(), test)); }, 'Matrix, RegExp': function (x, test) { return matrix(_filterRegExp(x.toArray(), test)); } }); filter.toTex = undefined; // use default template return filter; } /** * Filter values in a callback given a callback function * @param {Array} x * @param {Function} callback * @return {Array} Returns the filtered array * @private */ function _filterCallback (x, callback) { if (size(x).length !== 1) { throw new Error('Only one dimensional matrices supported'); } // figure out what number of arguments the callback function expects var args = maxArgumentCount(callback); return x.filter(function (value, index, array) { // invoke the callback function with the right number of arguments if (args === 1) { return callback(value); } else if (args === 2) { return callback(value, [index]); } else { // 3 or -1 return callback(value, [index], array); } }); } /** * Filter values in a callback given a regular expression * @param {Array} x * @param {Function} regexp * @return {Array} Returns the filtered array * @private */ function _filterRegExp (x, regexp) { if (size(x).length !== 1) { throw new Error('Only one dimensional matrices supported'); } return x.filter(function (entry) { return regexp.test(entry); }); } exports.name = 'filter'; exports.factory = factory; /***/ }), /* 108 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var maxArgumentCount = __webpack_require__(33).maxArgumentCount; function factory (type, config, load, typed) { /** * Iterate over all elements of a matrix/array, and executes the given callback function. * * Syntax: * * math.forEach(x, callback) * * Examples: * * math.forEach([1, 2, 3], function(value) { * console.log(value); * }); * // outputs 1, 2, 3 * * See also: * * filter, map, sort * * @param {Matrix | Array} x The matrix to iterate on. * @param {Function} callback The callback function is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix/array being traversed. */ var forEach = typed('forEach', { 'Array, function': _forEach, 'Matrix, function': function (x, callback) { return x.forEach(callback); } }); forEach.toTex = undefined; // use default template return forEach; } /** * forEach for a multi dimensional array * @param {Array} array * @param {Function} callback * @private */ function _forEach (array, callback) { // figure out what number of arguments the callback function expects var args = maxArgumentCount(callback); var recurse = function (value, index) { if (Array.isArray(value)) { value.forEach(function (child, i) { // we create a copy of the index array and append the new index value recurse(child, index.concat(i)); }); } else { // invoke the callback function with the right number of arguments if (args === 1) { callback(value); } else if (args === 2) { callback(value, index); } else { // 3 or -1 callback(value, index, array); } } }; recurse(array, []); } exports.name = 'forEach'; exports.factory = factory; /***/ }), /* 109 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var divideScalar = load(__webpack_require__(16)); var addScalar = load(__webpack_require__(17)); var multiply = load(__webpack_require__(10)); var unaryMinus = load(__webpack_require__(36)); var det = load(__webpack_require__(106)); var eye = load(__webpack_require__(66)); /** * Calculate the inverse of a square matrix. * * Syntax: * * math.inv(x) * * Examples: * * math.inv([[1, 2], [3, 4]]); // returns [[-2, 1], [1.5, -0.5]] * math.inv(4); // returns 0.25 * 1 / 4; // returns 0.25 * * See also: * * det, transpose * * @param {number | Complex | Array | Matrix} x Matrix to be inversed * @return {number | Complex | Array | Matrix} The inverse of `x`. */ var inv = typed('inv', { 'Array | Matrix': function (x) { var size = (x.isMatrix === true) ? x.size() : util.array.size(x); switch (size.length) { case 1: // vector if (size[0] == 1) { if (x.isMatrix === true) { return matrix([ divideScalar(1, x.valueOf()[0]) ]); } else { return [ divideScalar(1, x[0]) ]; } } else { throw new RangeError('Matrix must be square ' + '(size: ' + util.string.format(size) + ')'); } case 2: // two dimensional array var rows = size[0]; var cols = size[1]; if (rows == cols) { if (x.isMatrix === true) { return matrix( _inv(x.valueOf(), rows, cols), x.storage() ); } else { // return an Array return _inv(x, rows, cols); } } else { throw new RangeError('Matrix must be square ' + '(size: ' + util.string.format(size) + ')'); } default: // multi dimensional array throw new RangeError('Matrix must be two dimensional ' + '(size: ' + util.string.format(size) + ')'); } }, 'any': function (x) { // scalar return divideScalar(1, x); // FIXME: create a BigNumber one when configured for bignumbers } }); /** * Calculate the inverse of a square matrix * @param {Array[]} mat A square matrix * @param {number} rows Number of rows * @param {number} cols Number of columns, must equal rows * @return {Array[]} inv Inverse matrix * @private */ function _inv (mat, rows, cols){ var r, s, f, value, temp; if (rows == 1) { // this is a 1 x 1 matrix value = mat[0][0]; if (value == 0) { throw Error('Cannot calculate inverse, determinant is zero'); } return [[ divideScalar(1, value) ]]; } else if (rows == 2) { // this is a 2 x 2 matrix var d = det(mat); if (d == 0) { throw Error('Cannot calculate inverse, determinant is zero'); } return [ [ divideScalar(mat[1][1], d), divideScalar(unaryMinus(mat[0][1]), d) ], [ divideScalar(unaryMinus(mat[1][0]), d), divideScalar(mat[0][0], d) ] ]; } else { // this is a matrix of 3 x 3 or larger // calculate inverse using gauss-jordan elimination // http://en.wikipedia.org/wiki/Gaussian_elimination // http://mathworld.wolfram.com/MatrixInverse.html // http://math.uww.edu/~mcfarlat/inverse.htm // make a copy of the matrix (only the arrays, not of the elements) var A = mat.concat(); for (r = 0; r < rows; r++) { A[r] = A[r].concat(); } // create an identity matrix which in the end will contain the // matrix inverse var B = eye(rows).valueOf(); // loop over all columns, and perform row reductions for (var c = 0; c < cols; c++) { // element Acc should be non zero. if not, swap content // with one of the lower rows r = c; while (r < rows && A[r][c] == 0) { r++; } if (r == rows || A[r][c] == 0) { // TODO: in case of zero det, just return a matrix wih Infinity values? (like octave) throw Error('Cannot calculate inverse, determinant is zero'); } if (r != c) { temp = A[c]; A[c] = A[r]; A[r] = temp; temp = B[c]; B[c] = B[r]; B[r] = temp; } // eliminate non-zero values on the other rows at column c var Ac = A[c], Bc = B[c]; for (r = 0; r < rows; r++) { var Ar = A[r], Br = B[r]; if(r != c) { // eliminate value at column c and row r if (Ar[c] != 0) { f = divideScalar(unaryMinus(Ar[c]), Ac[c]); // add (f * row c) to row r to eliminate the value // at column c for (s = c; s < cols; s++) { Ar[s] = addScalar(Ar[s], multiply(f, Ac[s])); } for (s = 0; s < cols; s++) { Br[s] = addScalar(Br[s], multiply(f, Bc[s])); } } } else { // normalize value at Acc to 1, // divide each value on row r with the value at Acc f = Ac[c]; for (s = c; s < cols; s++) { Ar[s] = divideScalar(Ar[s], f); } for (s = 0; s < cols; s++) { Br[s] = divideScalar(Br[s], f); } } } } return B; } } inv.toTex = {1: '\\left(${args[0]}\\right)^{-1}'}; return inv; } exports.name = 'inv'; exports.factory = factory; /***/ }), /* 110 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var ZERO = new type.BigNumber(0); var ONE = new type.BigNumber(1); /** * Create an array from a range. * By default, the range end is excluded. This can be customized by providing * an extra parameter `includeEnd`. * * Syntax: * * math.range(str [, includeEnd]) // Create a range from a string, * // where the string contains the * // start, optional step, and end, * // separated by a colon. * math.range(start, end [, includeEnd]) // Create a range with start and * // end and a step size of 1. * math.range(start, end, step [, includeEnd]) // Create a range with start, step, * // and end. * * Where: * * - `str: string` * A string 'start:end' or 'start:step:end' * - `start: {number | BigNumber}` * Start of the range * - `end: number | BigNumber` * End of the range, excluded by default, included when parameter includeEnd=true * - `step: number | BigNumber` * Step size. Default value is 1. * - `includeEnd: boolean` * Option to specify whether to include the end or not. False by default. * * Examples: * * math.range(2, 6); // [2, 3, 4, 5] * math.range(2, -3, -1); // [2, 1, 0, -1, -2] * math.range('2:1:6'); // [2, 3, 4, 5] * math.range(2, 6, true); // [2, 3, 4, 5, 6] * * See also: * * ones, zeros, size, subset * * @param {*} args Parameters describing the ranges `start`, `end`, and optional `step`. * @return {Array | Matrix} range */ var range = typed('range', { // TODO: simplify signatures when typed-function supports default values and optional arguments // TODO: a number or boolean should not be converted to string here 'string': _strRange, 'string, boolean': _strRange, 'number, number': function (start, end) { return _out(_rangeEx(start, end, 1)); }, 'number, number, number': function (start, end, step) { return _out(_rangeEx(start, end, step)); }, 'number, number, boolean': function (start, end, includeEnd) { return includeEnd ? _out(_rangeInc(start, end, 1)) : _out(_rangeEx(start, end, 1)); }, 'number, number, number, boolean': function (start, end, step, includeEnd) { return includeEnd ? _out(_rangeInc(start, end, step)) : _out(_rangeEx(start, end, step)); }, 'BigNumber, BigNumber': function (start, end) { return _out(_bigRangeEx(start, end, ONE)); }, 'BigNumber, BigNumber, BigNumber': function (start, end, step) { return _out(_bigRangeEx(start, end, step)); }, 'BigNumber, BigNumber, boolean': function (start, end, includeEnd) { return includeEnd ? _out(_bigRangeInc(start, end, ONE)) : _out(_bigRangeEx(start, end, ONE)); }, 'BigNumber, BigNumber, BigNumber, boolean': function (start, end, step, includeEnd) { return includeEnd ? _out(_bigRangeInc(start, end, step)) : _out(_bigRangeEx(start, end, step)); } }); range.toTex = undefined; // use default template return range; function _out(arr) { return config.matrix === 'Array' ? arr : matrix(arr); } function _strRange (str, includeEnd) { var r = _parse(str); if (!r){ throw new SyntaxError('String "' + str + '" is no valid range'); } var fn; if (config.number === 'BigNumber') { fn = includeEnd ? _bigRangeInc : _bigRangeEx; return _out(fn( new type.BigNumber(r.start), new type.BigNumber(r.end), new type.BigNumber(r.step))); } else { fn = includeEnd ? _rangeInc : _rangeEx; return _out(fn(r.start, r.end, r.step)); } } /** * Create a range with numbers. End is excluded * @param {number} start * @param {number} end * @param {number} step * @returns {Array} range * @private */ function _rangeEx (start, end, step) { var array = [], x = start; if (step > 0) { while (x < end) { array.push(x); x += step; } } else if (step < 0) { while (x > end) { array.push(x); x += step; } } return array; } /** * Create a range with numbers. End is included * @param {number} start * @param {number} end * @param {number} step * @returns {Array} range * @private */ function _rangeInc (start, end, step) { var array = [], x = start; if (step > 0) { while (x <= end) { array.push(x); x += step; } } else if (step < 0) { while (x >= end) { array.push(x); x += step; } } return array; } /** * Create a range with big numbers. End is excluded * @param {BigNumber} start * @param {BigNumber} end * @param {BigNumber} step * @returns {Array} range * @private */ function _bigRangeEx (start, end, step) { var array = [], x = start; if (step.gt(ZERO)) { while (x.lt(end)) { array.push(x); x = x.plus(step); } } else if (step.lt(ZERO)) { while (x.gt(end)) { array.push(x); x = x.plus(step); } } return array; } /** * Create a range with big numbers. End is included * @param {BigNumber} start * @param {BigNumber} end * @param {BigNumber} step * @returns {Array} range * @private */ function _bigRangeInc (start, end, step) { var array = [], x = start; if (step.gt(ZERO)) { while (x.lte(end)) { array.push(x); x = x.plus(step); } } else if (step.lt(ZERO)) { while (x.gte(end)) { array.push(x); x = x.plus(step); } } return array; } /** * Parse a string into a range, * The string contains the start, optional step, and end, separated by a colon. * If the string does not contain a valid range, null is returned. * For example str='0:2:11'. * @param {string} str * @return {{start: number, end: number, step: number} | null} range Object containing properties start, end, step * @private */ function _parse (str) { var args = str.split(':'); // number var nums = args.map(function (arg) { // use Number and not parseFloat as Number returns NaN on invalid garbage in the string return Number(arg); }); var invalid = nums.some(function (num) { return isNaN(num); }); if(invalid) { return null; } switch (nums.length) { case 2: return { start: nums[0], end: nums[1], step: 1 }; case 3: return { start: nums[0], end: nums[2], step: nums[1] }; default: return null; } } } exports.name = 'range'; exports.factory = factory; /***/ }), /* 111 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var clone = __webpack_require__(4).clone; var format = __webpack_require__(19).format; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var add = load(__webpack_require__(15)); /** * Calculate the trace of a matrix: the sum of the elements on the main * diagonal of a square matrix. * * Syntax: * * math.trace(x) * * Examples: * * math.trace([[1, 2], [3, 4]]); // returns 5 * * var A = [ * [1, 2, 3], * [-1, 2, 3], * [2, 0, 3] * ] * math.trace(A); // returns 6 * * See also: * * diag * * @param {Array | Matrix} x A matrix * * @return {number} The trace of `x` */ var trace = typed('trace', { 'Array': function (x) { // use dense matrix implementation return trace(matrix(x)); }, 'Matrix': function (x) { // result var c; // process storage format switch (x.storage()) { case 'dense': c = _denseTrace(x); break; case 'sparse': c = _sparseTrace(x); break; } return c; }, 'any': clone }); var _denseTrace = function (m) { // matrix size & data var size = m._size; var data = m._data; // process dimensions switch (size.length) { case 1: // vector if (size[0] == 1) { // return data[0] return clone(data[0]); } throw new RangeError('Matrix must be square (size: ' + format(size) + ')'); case 2: // two dimensional var rows = size[0]; var cols = size[1]; if (rows === cols) { // calulate sum var sum = 0; // loop diagonal for (var i = 0; i < rows; i++) sum = add(sum, data[i][i]); // return trace return sum; } throw new RangeError('Matrix must be square (size: ' + format(size) + ')'); default: // multi dimensional throw new RangeError('Matrix must be two dimensional (size: ' + format(size) + ')'); } }; var _sparseTrace = function (m) { // matrix arrays var values = m._values; var index = m._index; var ptr = m._ptr; var size = m._size; // check dimensions var rows = size[0]; var columns = size[1]; // matrix must be square if (rows === columns) { // calulate sum var sum = 0; // check we have data (avoid looping columns) if (values.length > 0) { // loop columns for (var j = 0; j < columns; j++) { // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = ptr[j]; var k1 = ptr[j + 1]; // loop k within [k0, k1[ for (var k = k0; k < k1; k++) { // row index var i = index[k]; // check row if (i === j) { // accumulate value sum = add(sum, values[k]); // exit loop break; } if (i > j) { // exit loop, no value on the diagonal for column j break; } } } } // return trace return sum; } throw new RangeError('Matrix must be square (size: ' + format(size) + ')'); }; trace.toTex = {1: '\\mathrm{tr}\\left(${args[0]}\\right)'}; return trace; } exports.name = 'trace'; exports.factory = factory; /***/ }), /* 112 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var multiply = load(__webpack_require__(10)); var pow = load(__webpack_require__(35)); /** * Compute the gamma function of a value using Lanczos approximation for * small values, and an extended Stirling approximation for large values. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.gamma(n) * * Examples: * * math.gamma(5); // returns 24 * math.gamma(-0.5); // returns -3.5449077018110335 * math.gamma(math.i); // returns -0.15494982830180973 - 0.49801566811835596i * * See also: * * combinations, factorial, permutations * * @param {number | Array | Matrix} n A real or complex number * @return {number | Array | Matrix} The gamma of `n` */ var gamma = typed('gamma', { 'number': function (n) { var t, x; if (isInteger(n)) { if (n <= 0) { return isFinite(n) ? Infinity : NaN; } if (n > 171) { return Infinity; // Will overflow } var value = n - 2; var res = n - 1; while (value > 1) { res *= value; value--; } if (res == 0) { res = 1; // 0! is per definition 1 } return res; } if (n < 0.5) { return Math.PI / (Math.sin(Math.PI * n) * gamma(1-n)); } if (n >= 171.35) { return Infinity; // will overflow } if (n > 85.0) { // Extended Stirling Approx var twoN = n*n; var threeN = twoN*n; var fourN = threeN*n; var fiveN = fourN*n; return Math.sqrt(2*Math.PI/n) * Math.pow((n/Math.E), n) * (1 + 1/(12*n) + 1/(288*twoN) - 139/(51840*threeN) - 571/(2488320*fourN) + 163879/(209018880*fiveN) + 5246819/(75246796800*fiveN*n)); } --n; x = p[0]; for (var i = 1; i < p.length; ++i) { x += p[i] / (n+i); } t = n + g + 0.5; return Math.sqrt(2*Math.PI) * Math.pow(t, n+0.5) * Math.exp(-t) * x; }, 'Complex': function (n) { var t, x; if (n.im == 0) { return gamma(n.re); } n = new type.Complex(n.re - 1, n.im); x = new type.Complex(p[0], 0); for (var i = 1; i < p.length; ++i) { var real = n.re + i; // x += p[i]/(n+i) var den = real*real + n.im*n.im; if (den != 0) { x.re += p[i] * real / den; x.im += -(p[i] * n.im) / den; } else { x.re = p[i] < 0 ? -Infinity : Infinity; } } t = new type.Complex(n.re + g + 0.5, n.im); var twoPiSqrt = Math.sqrt(2*Math.PI); n.re += 0.5; var result = pow(t, n); if (result.im == 0) { // sqrt(2*PI)*result result.re *= twoPiSqrt; } else if (result.re == 0) { result.im *= twoPiSqrt; } else { result.re *= twoPiSqrt; result.im *= twoPiSqrt; } var r = Math.exp(-t.re); // exp(-t) t.re = r * Math.cos(-t.im); t.im = r * Math.sin(-t.im); return multiply(multiply(result, t), x); }, 'BigNumber': function (n) { if (n.isInteger()) { return (n.isNegative() || n.isZero()) ? new type.BigNumber(Infinity) : bigFactorial(n.minus(1)); } if (!n.isFinite()) { return new type.BigNumber(n.isNegative() ? NaN : Infinity); } throw new Error('Integer BigNumber expected'); }, 'Array | Matrix': function (n) { return deepMap(n, gamma); } }); /** * Calculate factorial for a BigNumber * @param {BigNumber} n * @returns {BigNumber} Returns the factorial of n */ function bigFactorial(n) { if (n.isZero()) { return new type.BigNumber(1); // 0! is per definition 1 } var precision = config.precision + (Math.log(n.toNumber()) | 0); var Big = type.BigNumber.clone({precision: precision}); var res = new Big(n); var value = n.toNumber() - 1; // number while (value > 1) { res = res.times(value); value--; } return new type.BigNumber(res.toPrecision(type.BigNumber.precision)); } gamma.toTex = {1: '\\Gamma\\left(${args[0]}\\right)'}; return gamma; } // TODO: comment on the variables g and p var g = 4.7421875; var p = [ 0.99999999999999709182, 57.156235665862923517, -59.597960355475491248, 14.136097974741747174, -0.49191381609762019978, 0.33994649984811888699e-4, 0.46523628927048575665e-4, -0.98374475304879564677e-4, 0.15808870322491248884e-3, -0.21026444172410488319e-3, 0.21743961811521264320e-3, -0.16431810653676389022e-3, 0.84418223983852743293e-4, -0.26190838401581408670e-4, 0.36899182659531622704e-5 ]; exports.name = 'gamma'; exports.factory = factory; /***/ }), /* 113 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var nearlyEqual = __webpack_require__(2).nearlyEqual; var bigNearlyEqual = __webpack_require__(31); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); var latex = __webpack_require__(3); /** * Test whether value x is larger or equal to y. * * The function returns true when x is larger than y or the relative * difference between x and y is smaller than the configured epsilon. The * function cannot be used to compare values smaller than approximately 2.22e-16. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.largerEq(x, y) * * Examples: * * math.larger(2, 1 + 1); // returns false * math.largerEq(2, 1 + 1); // returns true * * See also: * * equal, unequal, smaller, smallerEq, larger, compare * * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare * @return {boolean | Array | Matrix} Returns true when the x is larger or equal to y, else returns false */ var largerEq = typed('largerEq', { 'boolean, boolean': function (x, y) { return x >= y; }, 'number, number': function (x, y) { return x >= y || nearlyEqual(x, y, config.epsilon); }, 'BigNumber, BigNumber': function (x, y) { return x.gte(y) || bigNearlyEqual(x, y, config.epsilon); }, 'Fraction, Fraction': function (x, y) { return x.compare(y) !== -1; }, 'Complex, Complex': function () { throw new TypeError('No ordering relation is defined for complex numbers'); }, 'Unit, Unit': function (x, y) { if (!x.equalBase(y)) { throw new Error('Cannot compare units with different base'); } return largerEq(x.value, y.value); }, 'string, string': function (x, y) { return x >= y; }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, largerEq); break; default: // sparse + dense c = algorithm03(y, x, largerEq, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, largerEq, false); break; default: // dense + dense c = algorithm13(x, y, largerEq); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return largerEq(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return largerEq(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return largerEq(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, largerEq, false); break; default: c = algorithm14(x, y, largerEq, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, largerEq, true); break; default: c = algorithm14(y, x, largerEq, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, largerEq, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, largerEq, true).valueOf(); } }); largerEq.toTex = { 2: '\\left(${args[0]}' + latex.operators['largerEq'] + '${args[1]}\\right)' }; return largerEq; } exports.name = 'largerEq'; exports.factory = factory; /***/ }), /* 114 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepForEach = __webpack_require__(32); var reduce = __webpack_require__(59); var containsCollections = __webpack_require__(58); function factory (type, config, load, typed) { var larger = load(__webpack_require__(26)); /** * Compute the maximum value of a matrix or a list with values. * In case of a multi dimensional array, the maximum of the flattened array * will be calculated. When `dim` is provided, the maximum over the selected * dimension will be calculated. Parameter `dim` is zero-based. * * Syntax: * * math.max(a, b, c, ...) * math.max(A) * math.max(A, dim) * * Examples: * * math.max(2, 1, 4, 3); // returns 4 * math.max([2, 1, 4, 3]); // returns 4 * * // maximum over a specified dimension (zero-based) * math.max([[2, 5], [4, 3], [1, 7]], 0); // returns [4, 7] * math.max([[2, 5], [4, 3]], [1, 7], 1); // returns [5, 4, 7] * * math.max(2.7, 7.1, -4.5, 2.0, 4.1); // returns 7.1 * math.min(2.7, 7.1, -4.5, 2.0, 4.1); // returns -4.5 * * See also: * * mean, median, min, prod, std, sum, var * * @param {... *} args A single matrix or or multiple scalar values * @return {*} The maximum value */ var max = typed('max', { // max([a, b, c, d, ...]) 'Array | Matrix': _max, // max([a, b, c, d, ...], dim) 'Array | Matrix, number | BigNumber': function (array, dim) { return reduce(array, dim.valueOf(), _largest); }, // max(a, b, c, d, ...) '...': function (args) { if (containsCollections(args)) { throw new TypeError('Scalar values expected in function max'); } return _max(args); } }); max.toTex = '\\max\\left(${args}\\right)'; return max; /** * Return the largest of two values * @param {*} x * @param {*} y * @returns {*} Returns x when x is largest, or y when y is largest * @private */ function _largest(x, y){ return larger(x, y) ? x : y; } /** * Recursively calculate the maximum value in an n-dimensional array * @param {Array} array * @return {number} max * @private */ function _max(array) { var max = undefined; deepForEach(array, function (value) { if (max === undefined || larger(value, max)) { max = value; } }); if (max === undefined) { throw new Error('Cannot calculate max of an empty array'); } return max; } } exports.name = 'max'; exports.factory = factory; /***/ }), /* 115 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var size = __webpack_require__(7).size; var deepForEach = __webpack_require__(32); var reduce = __webpack_require__(59); var containsCollections = __webpack_require__(58); function factory (type, config, load, typed) { var add = load(__webpack_require__(15)); var divide = load(__webpack_require__(34)); /** * Compute the mean value of matrix or a list with values. * In case of a multi dimensional array, the mean of the flattened array * will be calculated. When `dim` is provided, the maximum over the selected * dimension will be calculated. Parameter `dim` is zero-based. * * Syntax: * * math.mean(a, b, c, ...) * math.mean(A) * math.mean(A, dim) * * Examples: * * math.mean(2, 1, 4, 3); // returns 2.5 * math.mean([1, 2.7, 3.2, 4]); // returns 2.725 * * math.mean([[2, 5], [6, 3], [1, 7]], 0); // returns [3, 5] * math.mean([[2, 5], [6, 3], [1, 7]], 1); // returns [3.5, 4.5, 4] * * See also: * * median, min, max, sum, prod, std, var * * @param {... *} args A single matrix or or multiple scalar values * @return {*} The mean of all values */ var mean = typed('mean', { // mean([a, b, c, d, ...]) 'Array | Matrix': _mean, // mean([a, b, c, d, ...], dim) 'Array | Matrix, number | BigNumber': _nmean, // mean(a, b, c, d, ...) '...': function (args) { if (containsCollections(args)) { throw new TypeError('Scalar values expected in function mean'); } return _mean(args); } }); mean.toTex = undefined; // use default template return mean; /** * Calculate the mean value in an n-dimensional array, returning a * n-1 dimensional array * @param {Array} array * @param {number} dim * @return {number} mean * @private */ function _nmean(array, dim){ var sum = reduce(array, dim, add); var s = Array.isArray(array) ? size(array) : array.size(); return divide(sum, s[dim]); } /** * Recursively calculate the mean value in an n-dimensional array * @param {Array} array * @return {number} mean * @private */ function _mean(array) { var sum = 0; var num = 0; deepForEach(array, function (value) { sum = add(sum, value); num++; }); if (num === 0) { throw new Error('Cannot calculate mean of an empty array'); } return divide(sum, num); } } exports.name = 'mean'; exports.factory = factory; /***/ }), /* 116 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var flatten = __webpack_require__(7).flatten; var reduce = __webpack_require__(59); var containsCollections = __webpack_require__(58); function factory (type, config, load, typed) { var add = load(__webpack_require__(17)); var divide = load(__webpack_require__(16)); var compare = load(__webpack_require__(46)); var partitionSelect = load(__webpack_require__(68)); /** * Compute the median of a matrix or a list with values. The values are * sorted and the middle value is returned. In case of an even number of * values, the average of the two middle values is returned. * Supported types of values are: Number, BigNumber, Unit * * In case of a (multi dimensional) array or matrix, the median of all * elements will be calculated. * * Syntax: * * math.median(a, b, c, ...) * math.median(A) * * Examples: * * math.median(5, 2, 7); // returns 5 * math.median([3, -1, 5, 7]); // returns 4 * * See also: * * mean, min, max, sum, prod, std, var, quantileSeq * * @param {... *} args A single matrix or or multiple scalar values * @return {*} The median */ var median = typed('median', { // median([a, b, c, d, ...]) 'Array | Matrix': _median, // median([a, b, c, d, ...], dim) 'Array | Matrix, number | BigNumber': function (array, dim) { // TODO: implement median(A, dim) throw new Error('median(A, dim) is not yet supported'); //return reduce(arguments[0], arguments[1], ...); }, // median(a, b, c, d, ...) '...': function (args) { if (containsCollections(args)) { throw new TypeError('Scalar values expected in function median'); } return _median(args); } }); /** * Recursively calculate the median of an n-dimensional array * @param {Array} array * @return {Number} median * @private */ function _median(array) { array = flatten(array.valueOf()); var num = array.length; if (num == 0) { throw new Error('Cannot calculate median of an empty array'); } if (num % 2 == 0) { // even: return the average of the two middle values var mid = num / 2 - 1; var right = partitionSelect(array, mid + 1); // array now partitioned at mid + 1, take max of left part var left = array[mid]; for (var i = 0; i < mid; ++i) { if (compare(array[i], left) > 0) { left = array[i]; } } return middle2(left, right); } else { // odd: return the middle value var m = partitionSelect(array, (num - 1) / 2); return middle(m); } } // helper function to type check the middle value of the array var middle = typed({ 'number | BigNumber | Unit': function (value) { return value; } }); // helper function to type check the two middle value of the array var middle2 = typed({ 'number | BigNumber | Unit, number | BigNumber | Unit': function (left, right) { return divide(add(left, right), 2); } }); median.toTex = undefined; // use default template return median; } exports.name = 'median'; exports.factory = factory; /***/ }), /* 117 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepForEach = __webpack_require__(32); var reduce = __webpack_require__(59); var containsCollections = __webpack_require__(58); function factory (type, config, load, typed) { var smaller = load(__webpack_require__(38)); /** * Compute the maximum value of a matrix or a list of values. * In case of a multi dimensional array, the maximum of the flattened array * will be calculated. When `dim` is provided, the maximum over the selected * dimension will be calculated. Parameter `dim` is zero-based. * * Syntax: * * math.min(a, b, c, ...) * math.min(A) * math.min(A, dim) * * Examples: * * math.min(2, 1, 4, 3); // returns 1 * math.min([2, 1, 4, 3]); // returns 1 * * // maximum over a specified dimension (zero-based) * math.min([[2, 5], [4, 3], [1, 7]], 0); // returns [1, 3] * math.min([[2, 5], [4, 3], [1, 7]], 1); // returns [2, 3, 1] * * math.max(2.7, 7.1, -4.5, 2.0, 4.1); // returns 7.1 * math.min(2.7, 7.1, -4.5, 2.0, 4.1); // returns -4.5 * * See also: * * mean, median, max, prod, std, sum, var * * @param {... *} args A single matrix or or multiple scalar values * @return {*} The minimum value */ var min = typed('min', { // min([a, b, c, d, ...]) 'Array | Matrix': _min, // min([a, b, c, d, ...], dim) 'Array | Matrix, number | BigNumber': function (array, dim) { return reduce(array, dim.valueOf(), _smallest); }, // min(a, b, c, d, ...) '...': function (args) { if (containsCollections(args)) { throw new TypeError('Scalar values expected in function min'); } return _min(args); } }); min.toTex = '\\min\\left(${args}\\right)'; return min; /** * Return the smallest of two values * @param {*} x * @param {*} y * @returns {*} Returns x when x is smallest, or y when y is smallest * @private */ function _smallest(x, y) { return smaller(x, y) ? x : y; } /** * Recursively calculate the minimum value in an n-dimensional array * @param {Array} array * @return {number} min * @private */ function _min(array) { var min = undefined; deepForEach(array, function (value) { if (min === undefined || smaller(value, min)) { min = value; } }); if (min === undefined) { throw new Error('Cannot calculate min of an empty array'); } return min; } } exports.name = 'min'; exports.factory = factory; /***/ }), /* 118 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepForEach = __webpack_require__(32); function factory (type, config, load, typed) { var add = load(__webpack_require__(17)); /** * Compute the sum of a matrix or a list with values. * In case of a (multi dimensional) array or matrix, the sum of all * elements will be calculated. * * Syntax: * * math.sum(a, b, c, ...) * math.sum(A) * * Examples: * * math.sum(2, 1, 4, 3); // returns 10 * math.sum([2, 1, 4, 3]); // returns 10 * math.sum([[2, 5], [4, 3], [1, 7]]); // returns 22 * * See also: * * mean, median, min, max, prod, std, var * * @param {... *} args A single matrix or or multiple scalar values * @return {*} The sum of all values */ var sum = typed('sum', { 'Array | Matrix': function (args) { // sum([a, b, c, d, ...]) return _sum(args); }, 'Array | Matrix, number | BigNumber': function () { // sum([a, b, c, d, ...], dim) // TODO: implement sum(A, dim) throw new Error('sum(A, dim) is not yet supported'); }, '...': function (args) { // sum(a, b, c, d, ...) return _sum(args); } }); sum.toTex = undefined; // use default template return sum; /** * Recursively calculate the sum of an n-dimensional array * @param {Array} array * @return {number} sum * @private */ function _sum(array) { var sum = undefined; deepForEach(array, function (value) { sum = (sum === undefined) ? value : add(sum, value); }); if (sum === undefined) { switch (config.number) { case 'number': return 0; case 'BigNumber': return new type.BigNumber(0); case 'Fraction': return new type.Fraction(0); default: return 0; } } return sum; } } exports.name = 'sum'; exports.factory = factory; /***/ }), /* 119 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DEFAULT_NORMALIZATION = 'unbiased'; var deepForEach = __webpack_require__(32); function factory (type, config, load, typed) { var add = load(__webpack_require__(17)); var subtract = load(__webpack_require__(18)); var multiply = load(__webpack_require__(20)); var divide = load(__webpack_require__(16)); /** * Compute the variance of a matrix or a list with values. * In case of a (multi dimensional) array or matrix, the variance over all * elements will be calculated. * * Optionally, the type of normalization can be specified as second * parameter. The parameter `normalization` can be one of the following values: * * - 'unbiased' (default) The sum of squared errors is divided by (n - 1) * - 'uncorrected' The sum of squared errors is divided by n * - 'biased' The sum of squared errors is divided by (n + 1) * * Note that older browser may not like the variable name `var`. In that * case, the function can be called as `math['var'](...)` instead of * `math.var(...)`. * * Syntax: * * math.var(a, b, c, ...) * math.var(A) * math.var(A, normalization) * * Examples: * * math.var(2, 4, 6); // returns 4 * math.var([2, 4, 6, 8]); // returns 6.666666666666667 * math.var([2, 4, 6, 8], 'uncorrected'); // returns 5 * math.var([2, 4, 6, 8], 'biased'); // returns 4 * * math.var([[1, 2, 3], [4, 5, 6]]); // returns 3.5 * * See also: * * mean, median, max, min, prod, std, sum * * @param {Array | Matrix} array * A single matrix or or multiple scalar values * @param {string} [normalization='unbiased'] * Determines how to normalize the variance. * Choose 'unbiased' (default), 'uncorrected', or 'biased'. * @return {*} The variance */ var variance = typed('variance', { // var([a, b, c, d, ...]) 'Array | Matrix': function (array) { return _var(array, DEFAULT_NORMALIZATION); }, // var([a, b, c, d, ...], normalization) 'Array | Matrix, string': _var, // var(a, b, c, d, ...) '...': function (args) { return _var(args, DEFAULT_NORMALIZATION); } }); variance.toTex = '\\mathrm{Var}\\left(${args}\\right)'; return variance; /** * Recursively calculate the variance of an n-dimensional array * @param {Array} array * @param {string} normalization * Determines how to normalize the variance: * - 'unbiased' The sum of squared errors is divided by (n - 1) * - 'uncorrected' The sum of squared errors is divided by n * - 'biased' The sum of squared errors is divided by (n + 1) * @return {number | BigNumber} variance * @private */ function _var(array, normalization) { var sum = 0; var num = 0; if (array.length == 0) { throw new SyntaxError('Function var requires one or more parameters (0 provided)'); } // calculate the mean and number of elements deepForEach(array, function (value) { sum = add(sum, value); num++; }); if (num === 0) throw new Error('Cannot calculate var of an empty array'); var mean = divide(sum, num); // calculate the variance sum = 0; deepForEach(array, function (value) { var diff = subtract(value, mean); sum = add(sum, multiply(diff, diff)); }); switch (normalization) { case 'uncorrected': return divide(sum, num); case 'biased': return divide(sum, num + 1); case 'unbiased': var zero = (sum && sum.isBigNumber === true) ? new type.BigNumber(0) : 0; return (num == 1) ? zero : divide(sum, num - 1); default: throw new Error('Unknown normalization "' + normalization + '". ' + 'Choose "unbiased" (default), "uncorrected", or "biased".'); } } } exports.name = 'var'; exports.factory = factory; /***/ }), /* 120 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var string = __webpack_require__(19); function factory (type, config, load, typed) { /** * Format a value of any type into a string. * * Syntax: * * math.format(value) * math.format(value, options) * math.format(value, precision) * math.format(value, callback) * * Where: * * - `value: *` * The value to be formatted * - `options: Object` * An object with formatting options. Available options: * - `notation: string` * Number notation. Choose from: * - 'fixed' * Always use regular number notation. * For example '123.40' and '14000000' * - 'exponential' * Always use exponential notation. * For example '1.234e+2' and '1.4e+7' * - 'engineering' * Always use engineering notation. * For example '123.4e+0' and '14.0e+6' * - 'auto' (default) * Regular number notation for numbers having an absolute value between * `lower` and `upper` bounds, and uses exponential notation elsewhere. * Lower bound is included, upper bound is excluded. * For example '123.4' and '1.4e7'. * - `precision: number` * A number between 0 and 16 to round the digits of the number. In case * of notations 'exponential' and 'auto', `precision` defines the total * number of significant digits returned and is undefined by default. * In case of notation 'fixed', `precision` defines the number of * significant digits after the decimal point, and is 0 by default. * - `exponential: Object` * An object containing two parameters, {number} lower and {number} upper, * used by notation 'auto' to determine when to return exponential * notation. Default values are `lower=1e-3` and `upper=1e5`. Only * applicable for notation `auto`. * - `fraction: string`. Available values: 'ratio' (default) or 'decimal'. * For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is * configured, and will output `0.(3)` when 'decimal' is configured. * - `callback: function` * A custom formatting function, invoked for all numeric elements in `value`, * for example all elements of a matrix, or the real and imaginary * parts of a complex number. This callback can be used to override the * built-in numeric notation with any type of formatting. Function `callback` * is called with `value` as parameter and must return a string. * * When `value` is an Object: * * - When the object contains a property `format` being a function, this function * is invoked as `value.format(options)` and the result is returned. * - When the object has its own `toString` method, this method is invoked * and the result is returned. * - In other cases the function will loop over all object properties and * return JSON object notation like '{"a": 2, "b": 3}'. * * When value is a function: * * - When the function has a property `syntax`, it returns this * syntax description. * - In other cases, a string `'function'` is returned. * * Examples: * * math.format(6.4); // returns '6.4' * math.format(1240000); // returns '1.24e6' * math.format(1/3); // returns '0.3333333333333333' * math.format(1/3, 3); // returns '0.333' * math.format(21385, 2); // returns '21000' * math.format(12.071, {notation: 'fixed'}); // returns '12' * math.format(2.3, {notation: 'fixed', precision: 2}); // returns '2.30' * math.format(52.8, {notation: 'exponential'}); // returns '5.28e+1' * math.format(12400, {notation: 'engineering'}); // returns '12.400e+3' * * function formatCurrency(value) { * // return currency notation with two digits: * return '$' + value.toFixed(2); * * // you could also use math.format inside the callback: * // return '$' + math.format(value, {notation: 'fixed', precision: 2}); * } * math.format([2.1, 3, 0.016], formatCurrency}; // returns '[$2.10, $3.00, $0.02]' * * See also: * * print * * @param {*} value Value to be stringified * @param {Object | Function | number} [options] Formatting options * @return {string} The formatted value */ var format = typed('format', { 'any': string.format, 'any, Object | function | number': string.format }); format.toTex = undefined; // use default template return format; } exports.name = 'format'; exports.factory = factory; /***/ }), /* 121 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic arccos of a value, * defined as `acosh(x) = ln(sqrt(x^2 - 1) + x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.acosh(x) * * Examples: * * math.acosh(1.5); // returns 0.9624236501192069 * * See also: * * cosh, asinh, atanh * * @param {number | Complex | Unit | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic arccosine of x */ var acosh = typed('acosh', { 'number': function (x) { if (x >= 1 || config.predictable) { return _acosh(x); } if (x <= -1) { return new type.Complex(Math.log(Math.sqrt(x*x - 1) - x), Math.PI); } return new type.Complex(x, 0).acosh(); }, 'Complex': function (x) { return x.acosh(); }, 'BigNumber': function (x) { return x.acosh(); }, 'Array | Matrix': function (x) { return deepMap(x, acosh); } }); acosh.toTex = {1: '\\cosh^{-1}\\left(${args[0]}\\right)'}; return acosh; } /** * Calculate the hyperbolic arccos of a number * @param {number} x * @return {number} * @private */ var _acosh = Math.acosh || function (x) { return Math.log(Math.sqrt(x*x - 1) + x) }; exports.name = 'acosh'; exports.factory = factory; /***/ }), /* 122 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Test whether a value is zero. * The function can check for zero for types `number`, `BigNumber`, `Fraction`, * `Complex`, and `Unit`. * * The function is evaluated element-wise in case of Array or Matrix input. * * Syntax: * * math.isZero(x) * * Examples: * * math.isZero(0); // returns true * math.isZero(2); // returns false * math.isZero(0.5); // returns false * math.isZero(math.bignumber(0)); // returns true * math.isZero(math.fraction(0)); // returns true * math.isZero(math.fraction(1,3)); // returns false * math.isZero(math.complex('2 - 4i'); // returns false * math.isZero(math.complex('0i'); // returns true * math.isZero('0'); // returns true * math.isZero('2'); // returns false * math.isZero([2, 0, -3]'); // returns [false, true, false] * * See also: * * isNumeric, isPositive, isNegative, isInteger * * @param {number | BigNumber | Complex | Fraction | Unit | Array | Matrix} x Value to be tested * @return {boolean} Returns true when `x` is zero. * Throws an error in case of an unknown data type. */ var isZero = typed('isZero', { 'number': function (x) { return x === 0; }, 'BigNumber': function (x) { return x.isZero(); }, 'Complex': function (x) { return x.re === 0 && x.im === 0; }, 'Fraction': function (x) { return x.d === 1 && x.n === 0; }, 'Unit': function (x) { return isZero(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, isZero); } }); return isZero; } exports.name = 'isZero'; exports.factory = factory; /***/ }), /* 123 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var types = __webpack_require__(60); function factory (type, config, load, typed) { /** * Determine the type of a variable. * * Function `typeof` recognizes the following types of objects: * * Object | Returns | Example * ---------------------- | ------------- | ------------------------------------------ * null | `'null'` | `math.typeof(null)` * number | `'number'` | `math.typeof(3.5)` * boolean | `'boolean'` | `math.typeof (true)` * string | `'string'` | `math.typeof ('hello world')` * Array | `'Array'` | `math.typeof ([1, 2, 3])` * Date | `'Date'` | `math.typeof (new Date())` * Function | `'Function'` | `math.typeof (function () {})` * Object | `'Object'` | `math.typeof ({a: 2, b: 3})` * RegExp | `'RegExp'` | `math.typeof (/a regexp/)` * undefined | `'undefined'` | `math.typeof(undefined)` * math.type.BigNumber | `'BigNumber'` | `math.typeof (math.bignumber('2.3e500'))` * math.type.Chain | `'Chain'` | `math.typeof (math.chain(2))` * math.type.Complex | `'Complex'` | `math.typeof (math.complex(2, 3))` * math.type.Fraction | `'Fraction'` | `math.typeof (math.fraction(1, 3))` * math.type.Help | `'Help'` | `math.typeof (math.help('sqrt'))` * math.type.Index | `'Index'` | `math.typeof (math.index(1, 3))` * math.type.Matrix | `'Matrix'` | `math.typeof (math.matrix([[1,2], [3, 4]]))` * math.type.Range | `'Range'` | `math.typeof (math.range(0, 10))` * math.type.Unit | `'Unit'` | `math.typeof (math.unit('45 deg'))` * * Syntax: * * math.typeof(x) * * Examples: * * math.typeof(3.5); // returns 'number' * math.typeof(math.complex('2-4i')); // returns 'Complex' * math.typeof(math.unit('45 deg')); // returns 'Unit' * math.typeof('hello world'); // returns 'string' * * @param {*} x The variable for which to test the type. * @return {string} Returns the name of the type. Primitive types are lower case, * non-primitive types are upper-camel-case. * For example 'number', 'string', 'Array', 'Date'. */ var _typeof = typed('_typeof', { 'any': function (x) { // JavaScript types var t = types.type(x); // math.js types if (t === 'Object') { if (x.isBigNumber === true) return 'BigNumber'; if (x.isComplex === true) return 'Complex'; if (x.isFraction === true) return 'Fraction'; if (x.isMatrix === true) return 'Matrix'; if (x.isUnit === true) return 'Unit'; if (x.isIndex === true) return 'Index'; if (x.isRange === true) return 'Range'; if (x.isChain === true) return 'Chain'; if (x.isHelp === true) return 'Help'; } return t; } }); _typeof.toTex = undefined; // use default template return _typeof; } exports.name = 'typeof'; exports.factory = factory; /***/ }), /* 124 */ /***/ (function(module, exports, __webpack_require__) { var Complex = __webpack_require__(504); var format = __webpack_require__(2).format; var isNumber = __webpack_require__(2).isNumber; function factory (type, config, load, typed, math) { /** * Attach type information */ Complex.prototype.type = 'Complex'; Complex.prototype.isComplex = true; /** * Get a JSON representation of the complex number * @returns {Object} Returns a JSON object structured as: * `{"mathjs": "Complex", "re": 2, "im": 3}` */ Complex.prototype.toJSON = function () { return { mathjs: 'Complex', re: this.re, im: this.im }; }; /* * Return the value of the complex number in polar notation * The angle phi will be set in the interval of [-pi, pi]. * @return {{r: number, phi: number}} Returns and object with properties r and phi. */ Complex.prototype.toPolar = function () { return { r: this.abs(), phi: this.arg() }; }; /** * Get a string representation of the complex number, * with optional formatting options. * @param {Object | number | Function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @return {string} str */ Complex.prototype.format = function (options) { var str = ''; var im = this.im; var re = this.re; var strRe = format(this.re, options); var strIm = format(this.im, options); // round either re or im when smaller than the configured precision var precision = isNumber(options) ? options : options ? options.precision : null; if (precision !== null) { var epsilon = Math.pow(10, -precision); if (Math.abs(re / im) < epsilon) { re = 0; } if (Math.abs(im / re) < epsilon) { im = 0; } } if (im == 0) { // real value str = strRe; } else if (re == 0) { // purely complex value if (im == 1) { str = 'i'; } else if (im == -1) { str = '-i'; } else { str = strIm + 'i'; } } else { // complex value if (im > 0) { if (im == 1) { str = strRe + ' + i'; } else { str = strRe + ' + ' + strIm + 'i'; } } else { if (im == -1) { str = strRe + ' - i'; } else { str = strRe + ' - ' + strIm.substring(1) + 'i'; } } } return str; }; /** * Create a complex number from polar coordinates * * Usage: * * Complex.fromPolar(r: number, phi: number) : Complex * Complex.fromPolar({r: number, phi: number}) : Complex * * @param {*} args... * @return {Complex} */ Complex.fromPolar = function (args) { switch (arguments.length) { case 1: var arg = arguments[0]; if (typeof arg === 'object') { return Complex(arg); } throw new TypeError('Input has to be an object with r and phi keys.'); case 2: var r = arguments[0], phi = arguments[1]; if (isNumber(r)) { if (phi && phi.isUnit && phi.hasBase('ANGLE')) { // convert unit to a number in radians phi = phi.toNumber('rad'); } if (isNumber(phi)) { return new Complex({r: r, phi: phi}); } throw new TypeError('Phi is not a number nor an angle unit.'); } else { throw new TypeError('Radius r is not a number.'); } default: throw new SyntaxError('Wrong number of arguments in function fromPolar'); } }; Complex.prototype.valueOf = Complex.prototype.toString; /** * Create a Complex number from a JSON object * @param {Object} json A JSON Object structured as * {"mathjs": "Complex", "re": 2, "im": 3} * All properties are optional, default values * for `re` and `im` are 0. * @return {Complex} Returns a new Complex number */ Complex.fromJSON = function (json) { return new Complex(json); }; // apply the current epsilon Complex.EPSILON = config.epsilon; // listen for changed in the configuration, automatically apply changed epsilon math.on('config', function (curr, prev) { if (curr.epsilon !== prev.epsilon) { Complex.EPSILON = curr.epsilon; } }); return Complex; } exports.name = 'Complex'; exports.path = 'type'; exports.factory = factory; exports.math = true; // request access to the math namespace /***/ }), /* 125 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Create a fraction convert a value to a fraction. * * Syntax: * math.fraction(numerator, denominator) * math.fraction({n: numerator, d: denominator}) * math.fraction(matrix: Array | Matrix) Turn all matrix entries * into fractions * * Examples: * * math.fraction(1, 3); * math.fraction('2/3'); * math.fraction({n: 2, d: 3}); * math.fraction([0.2, 0.25, 1.25]); * * See also: * * bignumber, number, string, unit * * @param {number | string | Fraction | BigNumber | Array | Matrix} [args] * Arguments specifying the numerator and denominator of * the fraction * @return {Fraction | Array | Matrix} Returns a fraction */ var fraction = typed('fraction', { 'number': function (x) { if (!isFinite(x) || isNaN(x)) { throw new Error(x + ' cannot be represented as a fraction'); } return new type.Fraction(x); }, 'string': function (x) { return new type.Fraction(x); }, 'number, number': function (numerator, denominator) { return new type.Fraction(numerator, denominator); }, 'BigNumber': function (x) { return new type.Fraction(x.toString()); }, 'Fraction': function (x) { return x; // fractions are immutable }, 'Object': function (x) { return new type.Fraction(x); }, 'Array | Matrix': function (x) { return deepMap(x, fraction); } }); return fraction; } exports.name = 'fraction'; exports.factory = factory; /***/ }), /* 126 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var DimensionError = __webpack_require__(9); var string = util.string; var array = util.array; var object = util.object; var number = util.number; var isArray = Array.isArray; var isNumber = number.isNumber; var isInteger = number.isInteger; var isString = string.isString; var validateIndex = array.validateIndex; function factory (type, config, load, typed) { var Matrix = load(__webpack_require__(72)); // force loading Matrix (do not use via type.Matrix) /** * Dense Matrix implementation. A regular, dense matrix, supporting multi-dimensional matrices. This is the default matrix type. * @class DenseMatrix */ function DenseMatrix(data, datatype) { if (!(this instanceof DenseMatrix)) throw new SyntaxError('Constructor must be called with the new operator'); if (datatype && !isString(datatype)) throw new Error('Invalid datatype: ' + datatype); if (data && data.isMatrix === true) { // check data is a DenseMatrix if (data.type === 'DenseMatrix') { // clone data & size this._data = object.clone(data._data); this._size = object.clone(data._size); this._datatype = datatype || data._datatype; } else { // build data from existing matrix this._data = data.toArray(); this._size = data.size(); this._datatype = datatype || data._datatype; } } else if (data && isArray(data.data) && isArray(data.size)) { // initialize fields from JSON representation this._data = data.data; this._size = data.size; this._datatype = datatype || data.datatype; } else if (isArray(data)) { // replace nested Matrices with Arrays this._data = preprocess(data); // get the dimensions of the array this._size = array.size(this._data); // verify the dimensions of the array, TODO: compute size while processing array array.validate(this._data, this._size); // data type unknown this._datatype = datatype; } else if (data) { // unsupported type throw new TypeError('Unsupported type of data (' + util.types.type(data) + ')'); } else { // nothing provided this._data = []; this._size = [0]; this._datatype = datatype; } } DenseMatrix.prototype = new Matrix(); /** * Attach type information */ DenseMatrix.prototype.type = 'DenseMatrix'; DenseMatrix.prototype.isDenseMatrix = true; /** * Get the storage format used by the matrix. * * Usage: * var format = matrix.storage() // retrieve storage format * * @memberof DenseMatrix * @return {string} The storage format. */ DenseMatrix.prototype.storage = function () { return 'dense'; }; /** * Get the datatype of the data stored in the matrix. * * Usage: * var format = matrix.datatype() // retrieve matrix datatype * * @memberof DenseMatrix * @return {string} The datatype. */ DenseMatrix.prototype.datatype = function () { return this._datatype; }; /** * Create a new DenseMatrix * @memberof DenseMatrix * @param {Array} data * @param {string} [datatype] */ DenseMatrix.prototype.create = function (data, datatype) { return new DenseMatrix(data, datatype); }; /** * Get a subset of the matrix, or replace a subset of the matrix. * * Usage: * var subset = matrix.subset(index) // retrieve subset * var value = matrix.subset(index, replacement) // replace subset * * @memberof DenseMatrix * @param {Index} index * @param {Array | DenseMatrix | *} [replacement] * @param {*} [defaultValue=0] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be filled with zeros. */ DenseMatrix.prototype.subset = function (index, replacement, defaultValue) { switch (arguments.length) { case 1: return _get(this, index); // intentional fall through case 2: case 3: return _set(this, index, replacement, defaultValue); default: throw new SyntaxError('Wrong number of arguments'); } }; /** * Get a single element from the matrix. * @memberof DenseMatrix * @param {number[]} index Zero-based index * @return {*} value */ DenseMatrix.prototype.get = function (index) { if (!isArray(index)) throw new TypeError('Array expected'); if (index.length != this._size.length) throw new DimensionError(index.length, this._size.length); // check index for (var x = 0; x < index.length; x++) validateIndex(index[x], this._size[x]); var data = this._data; for (var i = 0, ii = index.length; i < ii; i++) { var index_i = index[i]; validateIndex(index_i, data.length); data = data[index_i]; } return data; }; /** * Replace a single element in the matrix. * @memberof DenseMatrix * @param {number[]} index Zero-based index * @param {*} value * @param {*} [defaultValue] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be left undefined. * @return {DenseMatrix} self */ DenseMatrix.prototype.set = function (index, value, defaultValue) { if (!isArray(index)) throw new TypeError('Array expected'); if (index.length < this._size.length) throw new DimensionError(index.length, this._size.length, '<'); var i, ii, index_i; // enlarge matrix when needed var size = index.map(function (i) { return i + 1; }); _fit(this, size, defaultValue); // traverse over the dimensions var data = this._data; for (i = 0, ii = index.length - 1; i < ii; i++) { index_i = index[i]; validateIndex(index_i, data.length); data = data[index_i]; } // set new value index_i = index[index.length - 1]; validateIndex(index_i, data.length); data[index_i] = value; return this; }; /** * Get a submatrix of this matrix * @memberof DenseMatrix * @param {DenseMatrix} matrix * @param {Index} index Zero-based index * @private */ function _get (matrix, index) { if (!index || index.isIndex !== true) { throw new TypeError('Invalid index'); } var isScalar = index.isScalar(); if (isScalar) { // return a scalar return matrix.get(index.min()); } else { // validate dimensions var size = index.size(); if (size.length != matrix._size.length) { throw new DimensionError(size.length, matrix._size.length); } // validate if any of the ranges in the index is out of range var min = index.min(); var max = index.max(); for (var i = 0, ii = matrix._size.length; i < ii; i++) { validateIndex(min[i], matrix._size[i]); validateIndex(max[i], matrix._size[i]); } // retrieve submatrix // TODO: more efficient when creating an empty matrix and setting _data and _size manually return new DenseMatrix(_getSubmatrix(matrix._data, index, size.length, 0), matrix._datatype); } } /** * Recursively get a submatrix of a multi dimensional matrix. * Index is not checked for correct number or length of dimensions. * @memberof DenseMatrix * @param {Array} data * @param {Index} index * @param {number} dims Total number of dimensions * @param {number} dim Current dimension * @return {Array} submatrix * @private */ function _getSubmatrix (data, index, dims, dim) { var last = (dim == dims - 1); var range = index.dimension(dim); if (last) { return range.map(function (i) { return data[i]; }).valueOf(); } else { return range.map(function (i) { var child = data[i]; return _getSubmatrix(child, index, dims, dim + 1); }).valueOf(); } } /** * Replace a submatrix in this matrix * Indexes are zero-based. * @memberof DenseMatrix * @param {DenseMatrix} matrix * @param {Index} index * @param {DenseMatrix | Array | *} submatrix * @param {*} defaultValue Default value, filled in on new entries when * the matrix is resized. * @return {DenseMatrix} matrix * @private */ function _set (matrix, index, submatrix, defaultValue) { if (!index || index.isIndex !== true) { throw new TypeError('Invalid index'); } // get index size and check whether the index contains a single value var iSize = index.size(), isScalar = index.isScalar(); // calculate the size of the submatrix, and convert it into an Array if needed var sSize; if (submatrix && submatrix.isMatrix === true) { sSize = submatrix.size(); submatrix = submatrix.valueOf(); } else { sSize = array.size(submatrix); } if (isScalar) { // set a scalar // check whether submatrix is a scalar if (sSize.length !== 0) { throw new TypeError('Scalar expected'); } matrix.set(index.min(), submatrix, defaultValue); } else { // set a submatrix // validate dimensions if (iSize.length < matrix._size.length) { throw new DimensionError(iSize.length, matrix._size.length, '<'); } if (sSize.length < iSize.length) { // calculate number of missing outer dimensions var i = 0; var outer = 0; while (iSize[i] === 1 && sSize[i] === 1) { i++; } while (iSize[i] === 1) { outer++; i++; } // unsqueeze both outer and inner dimensions submatrix = array.unsqueeze(submatrix, iSize.length, outer, sSize); } // check whether the size of the submatrix matches the index size if (!object.deepEqual(iSize, sSize)) { throw new DimensionError(iSize, sSize, '>'); } // enlarge matrix when needed var size = index.max().map(function (i) { return i + 1; }); _fit(matrix, size, defaultValue); // insert the sub matrix var dims = iSize.length, dim = 0; _setSubmatrix (matrix._data, index, submatrix, dims, dim); } return matrix; } /** * Replace a submatrix of a multi dimensional matrix. * @memberof DenseMatrix * @param {Array} data * @param {Index} index * @param {Array} submatrix * @param {number} dims Total number of dimensions * @param {number} dim * @private */ function _setSubmatrix (data, index, submatrix, dims, dim) { var last = (dim == dims - 1), range = index.dimension(dim); if (last) { range.forEach(function (dataIndex, subIndex) { validateIndex(dataIndex); data[dataIndex] = submatrix[subIndex[0]]; }); } else { range.forEach(function (dataIndex, subIndex) { validateIndex(dataIndex); _setSubmatrix(data[dataIndex], index, submatrix[subIndex[0]], dims, dim + 1); }); } } /** * Resize the matrix to the given size. Returns a copy of the matrix when * `copy=true`, otherwise return the matrix itself (resize in place). * * @memberof DenseMatrix * @param {number[]} size The new size the matrix should have. * @param {*} [defaultValue=0] Default value, filled in on new entries. * If not provided, the matrix elements will * be filled with zeros. * @param {boolean} [copy] Return a resized copy of the matrix * * @return {Matrix} The resized matrix */ DenseMatrix.prototype.resize = function (size, defaultValue, copy) { // validate arguments if (!isArray(size)) throw new TypeError('Array expected'); // matrix to resize var m = copy ? this.clone() : this; // resize matrix return _resize(m, size, defaultValue); }; var _resize = function (matrix, size, defaultValue) { // check size if (size.length === 0) { // first value in matrix var v = matrix._data; // go deep while (isArray(v)) { v = v[0]; } return v; } // resize matrix matrix._size = size.slice(0); // copy the array matrix._data = array.resize(matrix._data, matrix._size, defaultValue); // return matrix return matrix; }; /** * Enlarge the matrix when it is smaller than given size. * If the matrix is larger or equal sized, nothing is done. * @memberof DenseMatrix * @param {DenseMatrix} matrix The matrix to be resized * @param {number[]} size * @param {*} defaultValue Default value, filled in on new entries. * @private */ function _fit(matrix, size, defaultValue) { var newSize = matrix._size.slice(0), // copy the array changed = false; // add dimensions when needed while (newSize.length < size.length) { newSize.push(0); changed = true; } // enlarge size when needed for (var i = 0, ii = size.length; i < ii; i++) { if (size[i] > newSize[i]) { newSize[i] = size[i]; changed = true; } } if (changed) { // resize only when size is changed _resize(matrix, newSize, defaultValue); } } /** * Create a clone of the matrix * @memberof DenseMatrix * @return {DenseMatrix} clone */ DenseMatrix.prototype.clone = function () { var m = new DenseMatrix({ data: object.clone(this._data), size: object.clone(this._size), datatype: this._datatype }); return m; }; /** * Retrieve the size of the matrix. * @memberof DenseMatrix * @returns {number[]} size */ DenseMatrix.prototype.size = function() { return this._size.slice(0); // return a clone of _size }; /** * Create a new matrix with the results of the callback function executed on * each entry of the matrix. * @memberof DenseMatrix * @param {Function} callback The callback function is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix being traversed. * * @return {DenseMatrix} matrix */ DenseMatrix.prototype.map = function (callback) { // matrix instance var me = this; var recurse = function (value, index) { if (isArray(value)) { return value.map(function (child, i) { return recurse(child, index.concat(i)); }); } else { return callback(value, index, me); } }; // return dense format return new DenseMatrix({ data: recurse(this._data, []), size: object.clone(this._size), datatype: this._datatype }); }; /** * Execute a callback function on each entry of the matrix. * @memberof DenseMatrix * @param {Function} callback The callback function is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix being traversed. */ DenseMatrix.prototype.forEach = function (callback) { // matrix instance var me = this; var recurse = function (value, index) { if (isArray(value)) { value.forEach(function (child, i) { recurse(child, index.concat(i)); }); } else { callback(value, index, me); } }; recurse(this._data, []); }; /** * Create an Array with a copy of the data of the DenseMatrix * @memberof DenseMatrix * @returns {Array} array */ DenseMatrix.prototype.toArray = function () { return object.clone(this._data); }; /** * Get the primitive value of the DenseMatrix: a multidimensional array * @memberof DenseMatrix * @returns {Array} array */ DenseMatrix.prototype.valueOf = function () { return this._data; }; /** * Get a string representation of the matrix, with optional formatting options. * @memberof DenseMatrix * @param {Object | number | Function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @returns {string} str */ DenseMatrix.prototype.format = function (options) { return string.format(this._data, options); }; /** * Get a string representation of the matrix * @memberof DenseMatrix * @returns {string} str */ DenseMatrix.prototype.toString = function () { return string.format(this._data); }; /** * Get a JSON representation of the matrix * @memberof DenseMatrix * @returns {Object} */ DenseMatrix.prototype.toJSON = function () { return { mathjs: 'DenseMatrix', data: this._data, size: this._size, datatype: this._datatype }; }; /** * Get the kth Matrix diagonal. * * @memberof DenseMatrix * @param {number | BigNumber} [k=0] The kth diagonal where the vector will retrieved. * * @returns {Array} The array vector with the diagonal values. */ DenseMatrix.prototype.diagonal = function(k) { // validate k if any if (k) { // convert BigNumber to a number if (k.isBigNumber === true) k = k.toNumber(); // is must be an integer if (!isNumber(k) || !isInteger(k)) { throw new TypeError ('The parameter k must be an integer number'); } } else { // default value k = 0; } var kSuper = k > 0 ? k : 0; var kSub = k < 0 ? -k : 0; // rows & columns var rows = this._size[0]; var columns = this._size[1]; // number diagonal values var n = Math.min(rows - kSub, columns - kSuper); // x is a matrix get diagonal from matrix var data = []; // loop rows for (var i = 0; i < n; i++) { data[i] = this._data[i + kSub][i + kSuper]; } // create DenseMatrix return new DenseMatrix({ data: data, size: [n], datatype: this._datatype }); }; /** * Create a diagonal matrix. * * @memberof DenseMatrix * @param {Array} size The matrix size. * @param {number | Array} value The values for the diagonal. * @param {number | BigNumber} [k=0] The kth diagonal where the vector will be filled in. * @param {number} [defaultValue] The default value for non-diagonal * * @returns {DenseMatrix} */ DenseMatrix.diagonal = function (size, value, k, defaultValue, datatype) { if (!isArray(size)) throw new TypeError('Array expected, size parameter'); if (size.length !== 2) throw new Error('Only two dimensions matrix are supported'); // map size & validate size = size.map(function (s) { // check it is a big number if (s && s.isBigNumber === true) { // convert it s = s.toNumber(); } // validate arguments if (!isNumber(s) || !isInteger(s) || s < 1) { throw new Error('Size values must be positive integers'); } return s; }); // validate k if any if (k) { // convert BigNumber to a number if (k && k.isBigNumber === true) k = k.toNumber(); // is must be an integer if (!isNumber(k) || !isInteger(k)) { throw new TypeError ('The parameter k must be an integer number'); } } else { // default value k = 0; } if (defaultValue && isString(datatype)) { // convert defaultValue to the same datatype defaultValue = typed.convert(defaultValue, datatype); } var kSuper = k > 0 ? k : 0; var kSub = k < 0 ? -k : 0; // rows and columns var rows = size[0]; var columns = size[1]; // number of non-zero items var n = Math.min(rows - kSub, columns - kSuper); // value extraction function var _value; // check value if (isArray(value)) { // validate array if (value.length !== n) { // number of values in array must be n throw new Error('Invalid value array length'); } // define function _value = function (i) { // return value @ i return value[i]; }; } else if (value && value.isMatrix === true) { // matrix size var ms = value.size(); // validate matrix if (ms.length !== 1 || ms[0] !== n) { // number of values in array must be n throw new Error('Invalid matrix length'); } // define function _value = function (i) { // return value @ i return value.get([i]); }; } else { // define function _value = function () { // return value return value; }; } // discover default value if needed if (!defaultValue) { // check first value in array defaultValue = (_value(0) && _value(0).isBigNumber === true) ? new type.BigNumber(0) : 0; } // empty array var data = []; // check we need to resize array if (size.length > 0) { // resize array data = array.resize(data, size, defaultValue); // fill diagonal for (var d = 0; d < n; d++) { data[d + kSub][d + kSuper] = _value(d); } } // create DenseMatrix return new DenseMatrix({ data: data, size: [rows, columns] }); }; /** * Generate a matrix from a JSON object * @memberof DenseMatrix * @param {Object} json An object structured like * `{"mathjs": "DenseMatrix", data: [], size: []}`, * where mathjs is optional * @returns {DenseMatrix} */ DenseMatrix.fromJSON = function (json) { return new DenseMatrix(json); }; /** * Swap rows i and j in Matrix. * * @memberof DenseMatrix * @param {number} i Matrix row index 1 * @param {number} j Matrix row index 2 * * @return {Matrix} The matrix reference */ DenseMatrix.prototype.swapRows = function (i, j) { // check index if (!isNumber(i) || !isInteger(i) || !isNumber(j) || !isInteger(j)) { throw new Error('Row index must be positive integers'); } // check dimensions if (this._size.length !== 2) { throw new Error('Only two dimensional matrix is supported'); } // validate index validateIndex(i, this._size[0]); validateIndex(j, this._size[0]); // swap rows DenseMatrix._swapRows(i, j, this._data); // return current instance return this; }; /** * Swap rows i and j in Dense Matrix data structure. * * @param {number} i Matrix row index 1 * @param {number} j Matrix row index 2 */ DenseMatrix._swapRows = function (i, j, data) { // swap values i <-> j var vi = data[i]; data[i] = data[j]; data[j] = vi; }; /** * Preprocess data, which can be an Array or DenseMatrix with nested Arrays and * Matrices. Replaces all nested Matrices with Arrays * @memberof DenseMatrix * @param {Array} data * @return {Array} data */ function preprocess(data) { for (var i = 0, ii = data.length; i < ii; i++) { var elem = data[i]; if (isArray(elem)) { data[i] = preprocess(elem); } else if (elem && elem.isMatrix === true) { data[i] = preprocess(elem.valueOf()); } } return data; } // register this type in the base class Matrix type.Matrix._storage.dense = DenseMatrix; type.Matrix._storage['default'] = DenseMatrix; // exports return DenseMatrix; } exports.name = 'DenseMatrix'; exports.path = 'type'; exports.factory = factory; exports.lazy = false; // no lazy loading, as we alter type.Matrix._storage /***/ }), /* 127 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Create a range. A range has a start, step, and end, and contains functions * to iterate over the range. * * A range can be constructed as: * var range = new Range(start, end); * var range = new Range(start, end, step); * * To get the result of the range: * range.forEach(function (x) { * console.log(x); * }); * range.map(function (x) { * return math.sin(x); * }); * range.toArray(); * * Example usage: * var c = new Range(2, 6); // 2:1:5 * c.toArray(); // [2, 3, 4, 5] * var d = new Range(2, -3, -1); // 2:-1:-2 * d.toArray(); // [2, 1, 0, -1, -2] * * @class Range * @constructor Range * @param {number} start included lower bound * @param {number} end excluded upper bound * @param {number} [step] step size, default value is 1 */ function Range(start, end, step) { if (!(this instanceof Range)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (start != null) { if (start.isBigNumber === true) start = start.toNumber(); else if (typeof start !== 'number') throw new TypeError('Parameter start must be a number'); } if (end != null) { if (end.isBigNumber === true) end = end.toNumber(); else if (typeof end !== 'number') throw new TypeError('Parameter end must be a number'); } if (step != null) { if (step.isBigNumber === true) step = step.toNumber(); else if (typeof step !== 'number') throw new TypeError('Parameter step must be a number'); } this.start = (start != null) ? parseFloat(start) : 0; this.end = (end != null) ? parseFloat(end) : 0; this.step = (step != null) ? parseFloat(step) : 1; } /** * Attach type information */ Range.prototype.type = 'Range'; Range.prototype.isRange = true; /** * Parse a string into a range, * The string contains the start, optional step, and end, separated by a colon. * If the string does not contain a valid range, null is returned. * For example str='0:2:11'. * @memberof Range * @param {string} str * @return {Range | null} range */ Range.parse = function (str) { if (typeof str !== 'string') { return null; } var args = str.split(':'); var nums = args.map(function (arg) { return parseFloat(arg); }); var invalid = nums.some(function (num) { return isNaN(num); }); if (invalid) { return null; } switch (nums.length) { case 2: return new Range(nums[0], nums[1]); case 3: return new Range(nums[0], nums[2], nums[1]); default: return null; } }; /** * Create a clone of the range * @return {Range} clone */ Range.prototype.clone = function () { return new Range(this.start, this.end, this.step); }; /** * Retrieve the size of the range. * Returns an array containing one number, the number of elements in the range. * @memberof Range * @returns {number[]} size */ Range.prototype.size = function () { var len = 0, start = this.start, step = this.step, end = this.end, diff = end - start; if (number.sign(step) == number.sign(diff)) { len = Math.ceil((diff) / step); } else if (diff == 0) { len = 0; } if (isNaN(len)) { len = 0; } return [len]; }; /** * Calculate the minimum value in the range * @memberof Range * @return {number | undefined} min */ Range.prototype.min = function () { var size = this.size()[0]; if (size > 0) { if (this.step > 0) { // positive step return this.start; } else { // negative step return this.start + (size - 1) * this.step; } } else { return undefined; } }; /** * Calculate the maximum value in the range * @memberof Range * @return {number | undefined} max */ Range.prototype.max = function () { var size = this.size()[0]; if (size > 0) { if (this.step > 0) { // positive step return this.start + (size - 1) * this.step; } else { // negative step return this.start; } } else { return undefined; } }; /** * Execute a callback function for each value in the range. * @memberof Range * @param {function} callback The callback method is invoked with three * parameters: the value of the element, the index * of the element, and the Range being traversed. */ Range.prototype.forEach = function (callback) { var x = this.start; var step = this.step; var end = this.end; var i = 0; if (step > 0) { while (x < end) { callback(x, [i], this); x += step; i++; } } else if (step < 0) { while (x > end) { callback(x, [i], this); x += step; i++; } } }; /** * Execute a callback function for each value in the Range, and return the * results as an array * @memberof Range * @param {function} callback The callback method is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix being traversed. * @returns {Array} array */ Range.prototype.map = function (callback) { var array = []; this.forEach(function (value, index, obj) { array[index[0]] = callback(value, index, obj); }); return array; }; /** * Create an Array with a copy of the Ranges data * @memberof Range * @returns {Array} array */ Range.prototype.toArray = function () { var array = []; this.forEach(function (value, index) { array[index[0]] = value; }); return array; }; /** * Get the primitive value of the Range, a one dimensional array * @memberof Range * @returns {Array} array */ Range.prototype.valueOf = function () { // TODO: implement a caching mechanism for range.valueOf() return this.toArray(); }; /** * Get a string representation of the range, with optional formatting options. * Output is formatted as 'start:step:end', for example '2:6' or '0:0.2:11' * @memberof Range * @param {Object | number | function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @returns {string} str */ Range.prototype.format = function (options) { var str = number.format(this.start, options); if (this.step != 1) { str += ':' + number.format(this.step, options); } str += ':' + number.format(this.end, options); return str; }; /** * Get a string representation of the range. * @memberof Range * @returns {string} */ Range.prototype.toString = function () { return this.format(); }; /** * Get a JSON representation of the range * @memberof Range * @returns {Object} Returns a JSON object structured as: * `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}` */ Range.prototype.toJSON = function () { return { mathjs: 'Range', start: this.start, end: this.end, step: this.step }; }; /** * Instantiate a Range from a JSON object * @memberof Range * @param {Object} json A JSON object structured as: * `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}` * @return {Range} */ Range.fromJSON = function (json) { return new Range(json.start, json.end, json.step); }; return Range; } exports.name = 'Range'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 128 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); function factory (type, config, load, typed) { var equalScalar = load(__webpack_require__(8)); var SparseMatrix = type.SparseMatrix; /** * Iterates over SparseMatrix A and invokes the callback function f(Aij, Bij). * Callback function invoked NZA times, number of nonzero elements in A. * * * ┌ f(Aij, Bij) ; A(i,j) !== 0 * C(i,j) = ┤ * └ 0 ; otherwise * * * @param {Matrix} a The SparseMatrix instance (A) * @param {Matrix} b The SparseMatrix instance (B) * @param {Function} callback The f(Aij,Bij) operation to invoke * * @return {Matrix} SparseMatrix (C) * * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 */ var algorithm09 = function (a, b, callback) { // sparse matrix arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var asize = a._size; var adt = a._datatype; // sparse matrix arrays var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; var bsize = b._size; var bdt = b._datatype; // validate dimensions if (asize.length !== bsize.length) throw new DimensionError(asize.length, bsize.length); // check rows & columns if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); // rows & columns var rows = asize[0]; var columns = asize[1]; // datatype var dt; // equal signature to use var eq = equalScalar; // zero value var zero = 0; // callback signature to use var cf = callback; // process data types if (typeof adt === 'string' && adt === bdt) { // datatype dt = adt; // find signature that matches (dt, dt) eq = typed.find(equalScalar, [dt, dt]); // convert 0 to the same datatype zero = typed.convert(0, dt); // callback cf = typed.find(callback, [dt, dt]); } // result arrays var cvalues = avalues && bvalues ? [] : undefined; var cindex = []; var cptr = []; // matrix var c = new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [rows, columns], datatype: dt }); // workspaces var x = cvalues ? [] : undefined; // marks indicating we have a value in x for a given column var w = []; // vars var i, j, k, k0, k1; // loop columns for (j = 0; j < columns; j++) { // update cptr cptr[j] = cindex.length; // column mark var mark = j + 1; // check we need to process values if (x) { // loop B(:,j) for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { // row i = bindex[k]; // update workspace w[i] = mark; x[i] = bvalues[k]; } } // loop A(:,j) for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row i = aindex[k]; // check we need to process values if (x) { // b value @ i,j var vb = w[i] === mark ? x[i] : zero; // invoke f var vc = cf(avalues[k], vb); // check zero value if (!eq(vc, zero)) { // push index cindex.push(i); // push value cvalues.push(vc); } } else { // push index cindex.push(i); } } } // update cptr cptr[columns] = cindex.length; // return sparse matrix return c; }; return algorithm09; } exports.name = 'algorithm09'; exports.factory = factory; /***/ }), /* 129 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { /** * A ResultSet contains a list or results * @class ResultSet * @param {Array} entries * @constructor ResultSet */ function ResultSet(entries) { if (!(this instanceof ResultSet)) { throw new SyntaxError('Constructor must be called with the new operator'); } this.entries = entries || []; } /** * Attach type information */ ResultSet.prototype.type = 'ResultSet'; ResultSet.prototype.isResultSet = true; /** * Returns the array with results hold by this ResultSet * @memberof ResultSet * @returns {Array} entries */ ResultSet.prototype.valueOf = function () { return this.entries; }; /** * Returns the stringified results of the ResultSet * @memberof ResultSet * @returns {string} string */ ResultSet.prototype.toString = function () { return '[' + this.entries.join(', ') + ']'; }; /** * Get a JSON representation of the ResultSet * @memberof ResultSet * @returns {Object} Returns a JSON object structured as: * `{"mathjs": "ResultSet", "entries": [...]}` */ ResultSet.prototype.toJSON = function () { return { mathjs: 'ResultSet', entries: this.entries }; }; /** * Instantiate a ResultSet from a JSON object * @memberof ResultSet * @param {Object} json A JSON object structured as: * `{"mathjs": "ResultSet", "entries": [...]}` * @return {ResultSet} */ ResultSet.fromJSON = function (json) { return new ResultSet(json.entries); }; return ResultSet; } exports.name = 'ResultSet'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 130 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var endsWith = __webpack_require__(19).endsWith; var clone = __webpack_require__(4).clone; var constants = __webpack_require__(131); function factory (type, config, load, typed, math) { var add = load(__webpack_require__(17)); var subtract = load(__webpack_require__(18)); var multiply = load(__webpack_require__(20)); var divide = load(__webpack_require__(16)); var pow = load(__webpack_require__(35)); var abs = load(__webpack_require__(25)); var fix = load(__webpack_require__(101)); var equal = load(__webpack_require__(70)); var isNumeric = load(__webpack_require__(71)); var format = load(__webpack_require__(120)); var getTypeOf = load(__webpack_require__(123)); var toNumber = load(__webpack_require__(75)); var Complex = load(__webpack_require__(124)); /** * A unit can be constructed in the following ways: * var a = new Unit(value, name); * var b = new Unit(null, name); * var c = Unit.parse(str); * * Example usage: * var a = new Unit(5, 'cm'); // 50 mm * var b = Unit.parse('23 kg'); // 23 kg * var c = math.in(a, new Unit(null, 'm'); // 0.05 m * var d = new Unit(9.81, "m/s^2"); // 9.81 m/s^2 * * @class Unit * @constructor Unit * @param {number | BigNumber | Fraction | Complex | boolean} [value] A value like 5.2 * @param {string} [name] A unit name like "cm" or "inch", or a derived unit of the form: "u1[^ex1] [u2[^ex2] ...] [/ u3[^ex3] [u4[^ex4]]]", such as "kg m^2/s^2", where each unit appearing after the forward slash is taken to be in the denominator. "kg m^2 s^-2" is a synonym and is also acceptable. Any of the units can include a prefix. */ function Unit(value, name) { if (!(this instanceof Unit)) { throw new Error('Constructor must be called with the new operator'); } if (!(value === undefined || isNumeric(value) || value.isComplex)) { throw new TypeError('First parameter in Unit constructor must be number, BigNumber, Fraction, Complex, or undefined'); } if (name != undefined && (typeof name !== 'string' || name == '')) { throw new TypeError('Second parameter in Unit constructor must be a string'); } if (name != undefined) { var u = Unit.parse(name); this.units = u.units; this.dimensions = u.dimensions; } else { this.units = [ { unit: UNIT_NONE, prefix: PREFIXES.NONE, // link to a list with supported prefixes power: 0 } ]; this.dimensions = []; for(var i=0; i= '0' && c <= '9') || c == '.'); } function isDigit(c) { return ((c >= '0' && c <= '9')); } function next() { index++; c = text.charAt(index); } function revert(oldIndex) { index = oldIndex; c = text.charAt(index); } function parseNumber() { var number = ''; var oldIndex; oldIndex = index; if (c == '+') { next(); } else if (c == '-') { number += c; next(); } if (!isDigitDot(c)) { // a + or - must be followed by a digit revert(oldIndex); return null; } // get number, can have a single dot if (c == '.') { number += c; next(); if (!isDigit(c)) { // this is no legal number, it is just a dot revert(oldIndex); return null; } } else { while (isDigit(c)) { number += c; next(); } if (c == '.') { number += c; next(); } } while (isDigit(c)) { number += c; next(); } // check for exponential notation like "2.3e-4" or "1.23e50" if (c == 'E' || c == 'e') { // The grammar branches here. This could either be part of an exponent or the start of a unit that begins with the letter e, such as "4exabytes" var tentativeNumber = ''; var tentativeIndex = index; tentativeNumber += c; next(); if (c == '+' || c == '-') { tentativeNumber += c; next(); } // Scientific notation MUST be followed by an exponent (otherwise we assume it is not scientific notation) if (!isDigit(c)) { // The e or E must belong to something else, so return the number without the e or E. revert(tentativeIndex); return number; } // We can now safely say that this is scientific notation. number = number + tentativeNumber; while (isDigit(c)) { number += c; next(); } } return number; } function parseUnit() { var unitName = ''; // Alphanumeric characters only; matches [a-zA-Z0-9] var code = text.charCodeAt(index); while ( (code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122)) { unitName += c; next(); code = text.charCodeAt(index); } // Must begin with [a-zA-Z] code = unitName.charCodeAt(0); if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) { return unitName || null; } else { return null; } } function parseCharacter(toFind) { if (c === toFind) { next(); return toFind; } else { return null; } } /** * Parse a string into a unit. The value of the unit is parsed as number, * BigNumber, or Fraction depending on the math.js config setting `number`. * * Throws an exception if the provided string does not contain a valid unit or * cannot be parsed. * @memberof Unit * @param {string} str A string like "5.2 inch", "4e2 cm/s^2" * @return {Unit} unit */ Unit.parse = function (str, options) { options = options || {}; text = str; index = -1; c = ''; if (typeof text !== 'string') { throw new TypeError('Invalid argument in Unit.parse, string expected'); } var unit = new Unit(); unit.units = []; // A unit should follow this pattern: // [number]unit[^number] [unit[^number]]...[/unit[^number] [unit[^number]]] // Rules: // number is any floating point number. // unit is any alphanumeric string beginning with an alpha. Units with names like e3 should be avoided because they look like the exponent of a floating point number! // The string may optionally begin with a number. // Each unit may optionally be followed by ^number. // Whitespace or a forward slash is recommended between consecutive units, although the following technically is parseable: // 2m^2kg/s^2 // it is not good form. If a unit starts with e, then it could be confused as a floating point number: // 4erg next(); skipWhitespace(); // Optional number at the start of the string var valueStr = parseNumber(); var value = null; if(valueStr) { if (config.number === 'BigNumber') { value = new type.BigNumber(valueStr); } else if (config.number === 'Fraction') { value = new type.Fraction(valueStr); } else { // number value = parseFloat(valueStr); } } skipWhitespace(); // Whitespace is not required here // Next, we read any number of unit[^number] var powerMultiplierCurrent = 1; var expectingUnit = false; // Stack to keep track of powerMultipliers applied to each parentheses group var powerMultiplierStack = []; // Running product of all elements in powerMultiplierStack var powerMultiplierStackProduct = 1; while (true) { skipWhitespace(); // Check for and consume opening parentheses, pushing powerMultiplierCurrent to the stack // A '(' will always appear directly before a unit. while (c === '(') { powerMultiplierStack.push(powerMultiplierCurrent); powerMultiplierStackProduct *= powerMultiplierCurrent; powerMultiplierCurrent = 1; next(); skipWhitespace(); } // Is there something here? if(c) { var oldC = c; var uStr = parseUnit(); if(uStr == null) { throw new SyntaxError('Unexpected "' + oldC + '" in "' + text + '" at index ' + index.toString()); } } else { // End of input. break; } // Verify the unit exists and get the prefix (if any) var res = _findUnit(uStr); if(res == null) { // Unit not found. throw new SyntaxError('Unit "' + uStr + '" not found.'); } var power = powerMultiplierCurrent * powerMultiplierStackProduct; // Is there a "^ number"? skipWhitespace(); if (parseCharacter('^')) { skipWhitespace(); var p = parseNumber(); if(p == null) { // No valid number found for the power! throw new SyntaxError('In "' + str + '", "^" must be followed by a floating-point number'); } power *= p; } // Add the unit to the list unit.units.push( { unit: res.unit, prefix: res.prefix, power: power }); for(var i=0; i 1 || Math.abs(this.units[0].power - 1.0) > 1e-15; }; /** * Normalize a value, based on its currently set unit(s) * @memberof Unit * @param {number | BigNumber | Fraction | boolean} value * @return {number | BigNumber | Fraction | boolean} normalized value * @private */ Unit.prototype._normalize = function (value) { var unitValue, unitOffset, unitPower, unitPrefixValue; var convert; if (value == null || this.units.length === 0) { return value; } else if (this._isDerived()) { // This is a derived unit, so do not apply offsets. // For example, with J kg^-1 degC^-1 you would NOT want to apply the offset. var res = value; convert = Unit._getNumberConverter(getTypeOf(value)); // convert to Fraction or BigNumber if needed for(var i=0; i < this.units.length; i++) { unitValue = convert(this.units[i].unit.value); unitPrefixValue = convert(this.units[i].prefix.value); unitPower = convert(this.units[i].power); res = multiply(res, pow(multiply(unitValue, unitPrefixValue), unitPower)); } return res; } else { // This is a single unit of power 1, like kg or degC convert = Unit._getNumberConverter(getTypeOf(value)); // convert to Fraction or BigNumber if needed unitValue = convert(this.units[0].unit.value); unitOffset = convert(this.units[0].unit.offset); unitPrefixValue = convert(this.units[0].prefix.value); return multiply(add(value, unitOffset), multiply(unitValue, unitPrefixValue)); } }; /** * Denormalize a value, based on its currently set unit(s) * @memberof Unit * @param {number} value * @param {number} [prefixValue] Optional prefix value to be used (ignored if this is a derived unit) * @return {number} denormalized value * @private */ Unit.prototype._denormalize = function (value, prefixValue) { var unitValue, unitOffset, unitPower, unitPrefixValue; var convert; if (value == null || this.units.length === 0) { return value; } else if (this._isDerived()) { // This is a derived unit, so do not apply offsets. // For example, with J kg^-1 degC^-1 you would NOT want to apply the offset. // Also, prefixValue is ignored--but we will still use the prefix value stored in each unit, since kg is usually preferable to g unless the user decides otherwise. var res = value; convert = Unit._getNumberConverter(getTypeOf(value)); // convert to Fraction or BigNumber if needed for (var i = 0; i < this.units.length; i++) { unitValue = convert(this.units[i].unit.value); unitPrefixValue = convert(this.units[i].prefix.value); unitPower = convert(this.units[i].power); res = divide(res, pow(multiply(unitValue, unitPrefixValue), unitPower)); } return res; } else { // This is a single unit of power 1, like kg or degC convert = Unit._getNumberConverter(getTypeOf(value)); // convert to Fraction or BigNumber if needed unitValue = convert(this.units[0].unit.value); unitPrefixValue = convert(this.units[0].prefix.value); unitOffset = convert(this.units[0].unit.offset); if (prefixValue == undefined) { return subtract(divide(divide(value, unitValue), unitPrefixValue), unitOffset); } else { return subtract(divide(divide(value, unitValue), prefixValue), unitOffset); } } }; /** * Find a unit from a string * @memberof Unit * @param {string} str A string like 'cm' or 'inch' * @returns {Object | null} result When found, an object with fields unit and * prefix is returned. Else, null is returned. * @private */ function _findUnit(str) { // First, match units names exactly. For example, a user could define 'mm' as 10^-4 m, which is silly, but then we would want 'mm' to match the user-defined unit. if(UNITS.hasOwnProperty(str)) { var unit = UNITS[str]; var prefix = unit.prefixes['']; return { unit: unit, prefix: prefix } } for (var name in UNITS) { if (UNITS.hasOwnProperty(name)) { if (endsWith(str, name)) { var unit = UNITS[name]; var prefixLen = (str.length - name.length); var prefixName = str.substring(0, prefixLen); var prefix = unit.prefixes[prefixName]; if (prefix !== undefined) { // store unit, prefix, and value return { unit: unit, prefix: prefix }; } } } } return null; } /** * Test if the given expression is a unit. * The unit can have a prefix but cannot have a value. * @memberof Unit * @param {string} name A string to be tested whether it is a value less unit. * The unit can have prefix, like "cm" * @return {boolean} true if the given string is a unit */ Unit.isValuelessUnit = function (name) { return (_findUnit(name) != null); }; /** * check if this unit has given base unit * If this unit is a derived unit, this will ALWAYS return false, since by definition base units are not derived. * @memberof Unit * @param {BASE_UNITS | string | undefined} base */ Unit.prototype.hasBase = function (base) { if(typeof(base) === "string") { base = BASE_UNITS[base]; } if(!base) return false; // All dimensions must be the same for(var i=0; i 1e-12) { return false; } } return true; }; /** * Check if this unit has a base or bases equal to another base or bases * For derived units, the exponent on each base also must match * @memberof Unit * @param {Unit} other * @return {boolean} true if equal base */ Unit.prototype.equalBase = function (other) { // All dimensions must be the same for(var i=0; i 1e-12) { return false; } } return true; }; /** * Check if this unit equals another unit * @memberof Unit * @param {Unit} other * @return {boolean} true if both units are equal */ Unit.prototype.equals = function (other) { return (this.equalBase(other) && equal(this.value, other.value)); }; /** * Multiply this unit with another one * @memberof Unit * @param {Unit} other * @return {Unit} product of this unit and the other unit */ Unit.prototype.multiply = function (other) { var res = this.clone(); for(var i = 0; i 1e-12) { if(currentUnitSystem.hasOwnProperty(baseDim)) { proposedUnitList.push({ unit: currentUnitSystem[baseDim].unit, prefix: currentUnitSystem[baseDim].prefix, power: this.dimensions[i] || 0 }); } else { missingBaseDim = true; } } } // Is the proposed unit list "simpler" than the existing one? if(proposedUnitList.length < this.units.length && !missingBaseDim) { // Replace this unit list with the proposed list this.units = proposedUnitList; } } } this.isUnitListSimplified = true; }; /** * Get a string representation of the units of this Unit, without the value. * @memberof Unit * @return {string} */ Unit.prototype.formatUnits = function () { // Lazy evaluation of the unit list this.simplifyUnitListLazy(); var strNum = ""; var strDen = ""; var nNum = 0; var nDen = 0; for(var i=0; i 0) { nNum++; strNum += " " + this.units[i].prefix.name + this.units[i].unit.name; if(Math.abs(this.units[i].power - 1.0) > 1e-15) { strNum += "^" + this.units[i].power; } } else if(this.units[i].power < 0) { nDen++; } } if(nDen > 0) { for(var i=0; i 0) { strDen += " " + this.units[i].prefix.name + this.units[i].unit.name; if(Math.abs(this.units[i].power + 1.0) > 1e-15) { strDen += "^" + (-this.units[i].power); } } else { strDen += " " + this.units[i].prefix.name + this.units[i].unit.name; strDen += "^" + (this.units[i].power); } } } } // Remove leading " " strNum = strNum.substr(1); strDen = strDen.substr(1); // Add parans for better copy/paste back into the eval, for example, or for better pretty print formatting if(nNum > 1 && nDen > 0) { strNum = "(" + strNum + ")"; } if(nDen > 1 && nNum > 0) { strDen = "(" + strDen + ")"; } var str = strNum; if(nNum > 0 && nDen > 0) { str += " / "; } str += strDen; return str; }; /** * Get a string representation of the Unit, with optional formatting options. * @memberof Unit * @param {Object | number | Function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @return {string} */ Unit.prototype.format = function (options) { // Simplfy the unit list, if necessary this.simplifyUnitListLazy(); // Apply some custom logic for handling VA and VAR. The goal is to express the value of the unit as a real value, if possible. Otherwise, use a real-valued unit instead of a complex-valued one. var isImaginary = false; var isReal = true; if(typeof(this.value) !== 'undefined' && this.value !== null && this.value.isComplex) { // TODO: Make this better, for example, use relative magnitude of re and im rather than absolute isImaginary = Math.abs(this.value.re) < 1e-14; isReal = Math.abs(this.value.im) < 1e-14; } for(var i in this.units) { if(this.units[i].unit) { if(this.units[i].unit.name === 'VA' && isImaginary) { this.units[i].unit = UNITS["VAR"]; } else if(this.units[i].unit.name === 'VAR' && !isImaginary) { this.units[i].unit = UNITS["VA"]; } } } // Now apply the best prefix // Units must have only one unit and not have the fixPrefix flag set if (this.units.length === 1 && !this.fixPrefix) { // Units must have integer powers, otherwise the prefix will change the // outputted value by not-an-integer-power-of-ten if (Math.abs(this.units[0].power - Math.round(this.units[0].power)) < 1e-14) { // Apply the best prefix this.units[0].prefix = this._bestPrefix(); } } var value = this._denormalize(this.value); var str = (this.value !== null) ? format(value, options || {}) : ''; var unitStr = this.formatUnits(); if(this.value && this.value.isComplex) { str = "(" + str + ")"; // Surround complex values with ( ) to enable better parsing } if(unitStr.length > 0 && str.length > 0) { str += " "; } str += unitStr; return str; }; /** * Calculate the best prefix using current value. * @memberof Unit * @returns {Object} prefix * @private */ Unit.prototype._bestPrefix = function () { if (this.units.length !== 1) { throw new Error("Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!"); } if (Math.abs(this.units[0].power - Math.round(this.units[0].power)) >= 1e-14) { throw new Error("Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!"); } // find the best prefix value (resulting in the value of which // the absolute value of the log10 is closest to zero, // though with a little offset of 1.2 for nicer values: you get a // sequence 1mm 100mm 500mm 0.6m 1m 10m 100m 500m 0.6km 1km ... // Note: the units value can be any numeric type, but to find the best // prefix it's enough to work with limited precision of a regular number // Update: using mathjs abs since we also allow complex numbers var absValue = abs(this.value); var absUnitValue = abs(this.units[0].unit.value); var bestPrefix = this.units[0].prefix; if (absValue === 0) { return bestPrefix; } var power = this.units[0].power; var bestDiff = Math.log(absValue / Math.pow(bestPrefix.value * absUnitValue, power)) / Math.LN10 - 1.2; if(bestDiff > -2.200001 && bestDiff < 1.800001) return bestPrefix; // Allow the original prefix bestDiff = Math.abs(bestDiff); var prefixes = this.units[0].unit.prefixes; for (var p in prefixes) { if (prefixes.hasOwnProperty(p)) { var prefix = prefixes[p]; if (prefix.scientific) { var diff = Math.abs( Math.log(absValue / Math.pow(prefix.value * absUnitValue, power)) / Math.LN10 - 1.2); if (diff < bestDiff || (diff === bestDiff && prefix.name.length < bestPrefix.name.length)) { // choose the prefix with the smallest diff, or if equal, choose the one // with the shortest name (can happen with SHORTLONG for example) bestPrefix = prefix; bestDiff = diff; } } } } return bestPrefix; }; /** * Returns an array of units whose sum is equal to this unit * @memberof Unit * @param {Array} [parts] An array of strings or valueless units. * * Example: * * var u = new Unit(1, 'm'); * u.splitUnit(['feet', 'inch']); * [ 3 feet, 3.3700787401575 inch ] * * @return {Array} An array of units. */ Unit.prototype.splitUnit = function(parts) { var x = this.clone(); var ret = []; for(var i=0; i= '0' && c <= '9'); } if(i === 0 && !isValidAlpha(c)) throw new Error('Invalid unit name (must begin with alpha character): "' + name + '"'); if(i > 0 && !( isValidAlpha(c) || isDigit(c))) throw new Error('Invalid unit name (only alphanumeric characters are allowed): "' + name + '"'); } } /** * Wrapper around createUnitSingle. * Example: * createUnit({ * foo: { }, * bar: { * definition: 'kg/foo', * aliases: ['ba', 'barr', 'bars'], * offset: 200 * }, * baz: '4 bar' * }, * { * override: true; * }); * @param {object} obj Object map. Each key becomes a unit which is defined by its value. * @param {object} options */ Unit.createUnit = function(obj, options) { if(typeof(obj) !== 'object') { throw new TypeError("createUnit expects first parameter to be of type 'Object'"); } // Remove all units and aliases we are overriding if(options && options.override) { for(var key in obj) { if(obj.hasOwnProperty(key)) { Unit.deleteUnit(key); } if(obj[key].aliases) { for(var i=0; i foo_STUFF, or the essence of foo if(BASE_DIMENSIONS.indexOf(baseName) >= 0) { throw new Error('Cannot create new base unit "' + name + '": a base unit with that name already exists (and cannot be overridden)'); } BASE_DIMENSIONS.push(baseName); // Push 0 onto existing base units for(var b in BASE_UNITS) { if(BASE_UNITS.hasOwnProperty(b)) { BASE_UNITS[b].dimensions[BASE_DIMENSIONS.length-1] = 0; } } // Add the new base unit var newBaseUnit = { dimensions: [] }; for(var i=0; i 1e-12) { match = false; break; } } if(match) { anyMatch = true; break; } } } if(!anyMatch) { var baseName = name + "_STUFF"; // foo --> foo_STUFF, or the essence of foo // Add the new base unit var newBaseUnit = { dimensions: defUnit.dimensions.slice(0) }; newBaseUnit.key = baseName; BASE_UNITS[baseName] = newBaseUnit; currentUnitSystem[baseName] = { unit: newUnit, prefix: PREFIXES.NONE[''] }; newUnit.base = baseName; } } Unit.UNITS[name] = newUnit; for (var i=0; i} array * @param {string} item * @return {boolean} */ function contains (array, item) { return array.indexOf(item) !== -1; } /** * Find a string in an array. Case insensitive search * @param {Array.} array * @param {string} item * @return {number} Returns the index when found. Returns -1 when not found */ function findIndex (array, item) { return array .map(function (i) { return i.toLowerCase(); }) .indexOf(item.toLowerCase()); } /** * Validate an option * @param {Object} options Object with options * @param {string} name Name of the option to validate * @param {Array.} values Array with valid values for this option */ function validateOption(options, name, values) { if (options[name] !== undefined && !contains(values, options[name])) { var index = findIndex(values, options[name]); if (index !== -1) { // right value, wrong casing // TODO: lower case values are deprecated since v3, remove this warning some day. console.warn('Warning: Wrong casing for configuration option "' + name + '", should be "' + values[index] + '" instead of "' + options[name] + '".'); options[name] = values[index]; // change the option to the right casing } else { // unknown value console.warn('Warning: Unknown value "' + options[name] + '" for configuration option "' + name + '". Available options: ' + values.map(JSON.stringify).join(', ') + '.'); } } } exports.name = 'config'; exports.math = true; // request the math namespace as fifth argument exports.factory = factory; /***/ }), /* 138 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var lazy = __webpack_require__(4).lazy; var isFactory = __webpack_require__(4).isFactory; var traverse = __webpack_require__(4).traverse; var extend = __webpack_require__(4).extend; var ArgumentsError = __webpack_require__(41); function factory (type, config, load, typed, math) { /** * Import functions from an object or a module * * Syntax: * * math.import(object) * math.import(object, options) * * Where: * * - `object: Object` * An object with functions to be imported. * - `options: Object` An object with import options. Available options: * - `override: boolean` * If true, existing functions will be overwritten. False by default. * - `silent: boolean` * If true, the function will not throw errors on duplicates or invalid * types. False by default. * - `wrap: boolean` * If true, the functions will be wrapped in a wrapper function * which converts data types like Matrix to primitive data types like Array. * The wrapper is needed when extending math.js with libraries which do not * support these data type. False by default. * * Examples: * * // define new functions and variables * math.import({ * myvalue: 42, * hello: function (name) { * return 'hello, ' + name + '!'; * } * }); * * // use the imported function and variable * math.myvalue * 2; // 84 * math.hello('user'); // 'hello, user!' * * // import the npm module 'numbers' * // (must be installed first with `npm install numbers`) * math.import(require('numbers'), {wrap: true}); * * math.fibonacci(7); // returns 13 * * @param {Object | Array} object Object with functions to be imported. * @param {Object} [options] Import options. */ function math_import(object, options) { var num = arguments.length; if (num != 1 && num != 2) { throw new ArgumentsError('import', num, 1, 2); } if (!options) { options = {}; } if (isFactory(object)) { _importFactory(object, options); } // TODO: allow a typed-function with name too else if (Array.isArray(object)) { object.forEach(function (entry) { math_import(entry, options); }); } else if (typeof object === 'object') { // a map with functions for (var name in object) { if (object.hasOwnProperty(name)) { var value = object[name]; if (isSupportedType(value)) { _import(name, value, options); } else if (isFactory(object)) { _importFactory(object, options); } else { math_import(value, options); } } } } else { if (!options.silent) { throw new TypeError('Factory, Object, or Array expected'); } } } /** * Add a property to the math namespace and create a chain proxy for it. * @param {string} name * @param {*} value * @param {Object} options See import for a description of the options * @private */ function _import(name, value, options) { if (options.wrap && typeof value === 'function') { // create a wrapper around the function value = _wrap(value); } if (isTypedFunction(math[name]) && isTypedFunction(value)) { if (options.override) { // give the typed function the right name value = typed(name, value.signatures); } else { // merge the existing and typed function value = typed(math[name], value); } math[name] = value; _importTransform(name, value); math.emit('import', name, function resolver() { return value; }); return; } if (math[name] === undefined || options.override) { math[name] = value; _importTransform(name, value); math.emit('import', name, function resolver() { return value; }); return; } if (!options.silent) { throw new Error('Cannot import "' + name + '": already exists'); } } function _importTransform (name, value) { if (value && typeof value.transform === 'function') { math.expression.transform[name] = value.transform; } } /** * Create a wrapper a round an function which converts the arguments * to their primitive values (like convert a Matrix to Array) * @param {Function} fn * @return {Function} Returns the wrapped function * @private */ function _wrap (fn) { var wrapper = function wrapper () { var args = []; for (var i = 0, len = arguments.length; i < len; i++) { var arg = arguments[i]; args[i] = arg && arg.valueOf(); } return fn.apply(math, args); }; if (fn.transform) { wrapper.transform = fn.transform; } return wrapper; } /** * Import an instance of a factory into math.js * @param {{factory: Function, name: string, path: string, math: boolean}} factory * @param {Object} options See import for a description of the options * @private */ function _importFactory(factory, options) { if (typeof factory.name === 'string') { var name = factory.name; var namespace = factory.path ? traverse(math, factory.path) : math; var existing = namespace.hasOwnProperty(name) ? namespace[name] : undefined; var resolver = function () { var instance = load(factory); if (instance && typeof instance.transform === 'function') { throw new Error('Transforms cannot be attached to factory functions. ' + 'Please create a separate function for it with exports.path="expression.transform"'); } if (isTypedFunction(existing) && isTypedFunction(instance)) { if (options.override) { // replace the existing typed function (nothing to do) } else { // merge the existing and new typed function instance = typed(existing, instance); } return instance; } if (existing === undefined || options.override) { return instance; } if (!options.silent) { throw new Error('Cannot import "' + name + '": already exists'); } }; if (factory.lazy !== false) { lazy(namespace, name, resolver); } else { namespace[name] = resolver(); } math.emit('import', name, resolver, factory.path); } else { // unnamed factory. // no lazy loading load(factory); } } /** * Check whether given object is a type which can be imported * @param {Function | number | string | boolean | null | Unit | Complex} object * @return {boolean} * @private */ function isSupportedType(object) { return typeof object == 'function' || typeof object === 'number' || typeof object === 'string' || typeof object === 'boolean' || object === null || (object && object.isUnit === true) || (object && object.isComplex === true) || (object && object.isBigNumber === true) || (object && object.isFraction === true) || (object && object.isMatrix === true) || (object && Array.isArray(object) === true) } /** * Test whether a given thing is a typed-function * @param {*} fn * @return {boolean} Returns true when `fn` is a typed-function */ function isTypedFunction (fn) { return typeof fn === 'function' && typeof fn.signatures === 'object'; } return math_import; } exports.math = true; // request access to the math namespace as 5th argument of the factory function exports.name = 'import'; exports.factory = factory; exports.lazy = true; /***/ }), /* 139 */ /***/ (function(module, exports, __webpack_require__) { var typedFunction = __webpack_require__(508); var digits = __webpack_require__(2).digits; // returns a new instance of typed-function var createTyped = function () { // initially, return the original instance of typed-function // consecutively, return a new instance from typed.create. createTyped = typedFunction.create; return typedFunction; }; /** * Factory function for creating a new typed instance * @param {Object} type Object with data types like Complex and BigNumber * @returns {Function} */ exports.create = function create(type) { // TODO: typed-function must be able to silently ignore signatures with unknown data types // get a new instance of typed-function var typed = createTyped(); // define all types. The order of the types determines in which order function // arguments are type-checked (so for performance it's important to put the // most used types first). typed.types = [ { name: 'number', test: function (x) { return typeof x === 'number' } }, { name: 'Complex', test: function (x) { return x && x.isComplex } }, { name: 'BigNumber', test: function (x) { return x && x.isBigNumber } }, { name: 'Fraction', test: function (x) { return x && x.isFraction } }, { name: 'Unit', test: function (x) { return x && x.isUnit } }, { name: 'string', test: function (x) { return typeof x === 'string' } }, { name: 'Array', test: Array.isArray }, { name: 'Matrix', test: function (x) { return x && x.isMatrix } }, { name: 'DenseMatrix', test: function (x) { return x && x.isDenseMatrix } }, { name: 'SparseMatrix', test: function (x) { return x && x.isSparseMatrix } }, { name: 'Range', test: function (x) { return x && x.isRange } }, { name: 'Index', test: function (x) { return x && x.isIndex } }, { name: 'boolean', test: function (x) { return typeof x === 'boolean' } }, { name: 'ResultSet', test: function (x) { return x && x.isResultSet } }, { name: 'Help', test: function (x) { return x && x.isHelp } }, { name: 'function', test: function (x) { return typeof x === 'function'} }, { name: 'Date', test: function (x) { return x instanceof Date } }, { name: 'RegExp', test: function (x) { return x instanceof RegExp } }, { name: 'Object', test: function (x) { return typeof x === 'object' } }, { name: 'null', test: function (x) { return x === null } }, { name: 'undefined', test: function (x) { return x === undefined } }, { name: 'OperatorNode', test: function (x) { return x && x.isOperatorNode } }, { name: 'ConstantNode', test: function (x) { return x && x.isConstantNode } }, { name: 'SymbolNode', test: function (x) { return x && x.isSymbolNode } }, { name: 'ParenthesisNode', test: function (x) { return x && x.isParenthesisNode } }, { name: 'FunctionNode', test: function (x) { return x && x.isFunctionNode } }, { name: 'FunctionAssignmentNode', test: function (x) { return x && x.isFunctionAssignmentNode } }, { name: 'ArrayNode', test: function (x) { return x && x.isArrayNode } }, { name: 'AssignmentNode', test: function (x) { return x && x.isAssignmentNode } }, { name: 'BlockNode', test: function (x) { return x && x.isBlockNode } }, { name: 'ConditionalNode', test: function (x) { return x && x.isConditionalNode } }, { name: 'IndexNode', test: function (x) { return x && x.isIndexNode } }, { name: 'RangeNode', test: function (x) { return x && x.isRangeNode } }, { name: 'UpdateNode', test: function (x) { return x && x.isUpdateNode } }, { name: 'Node', test: function (x) { return x && x.isNode } } ]; // TODO: add conversion from BigNumber to number? typed.conversions = [ { from: 'number', to: 'BigNumber', convert: function (x) { // note: conversion from number to BigNumber can fail if x has >15 digits if (digits(x) > 15) { throw new TypeError('Cannot implicitly convert a number with >15 significant digits to BigNumber ' + '(value: ' + x + '). ' + 'Use function bignumber(x) to convert to BigNumber.'); } return new type.BigNumber(x); } }, { from: 'number', to: 'Complex', convert: function (x) { return new type.Complex(x, 0); } }, { from: 'number', to: 'string', convert: function (x) { return x + ''; } }, { from: 'BigNumber', to: 'Complex', convert: function (x) { return new type.Complex(x.toNumber(), 0); } }, { from: 'Fraction', to: 'BigNumber', convert: function (x) { throw new TypeError('Cannot implicitly convert a Fraction to BigNumber or vice versa. ' + 'Use function bignumber(x) to convert to BigNumber or fraction(x) to convert to Fraction.'); } }, { from: 'Fraction', to: 'Complex', convert: function (x) { return new type.Complex(x.valueOf(), 0); } }, { from: 'number', to: 'Fraction', convert: function (x) { if (digits(x) > 15) { throw new TypeError('Cannot implicitly convert a number with >15 significant digits to Fraction ' + '(value: ' + x + '). ' + 'Use function fraction(x) to convert to Fraction.'); } return new type.Fraction(x); } }, { // FIXME: add conversion from Fraction to number, for example for `sqrt(fraction(1,3))` // from: 'Fraction', // to: 'number', // convert: function (x) { // return x.valueOf(); // } //}, { from: 'string', to: 'number', convert: function (x) { var n = Number(x); if (isNaN(n)) { throw new Error('Cannot convert "' + x + '" to a number'); } return n; } }, { from: 'string', to: 'BigNumber', convert: function (x) { try { return new type.BigNumber(x); } catch (err) { throw new Error('Cannot convert "' + x + '" to BigNumber'); } } }, { from: 'string', to: 'Fraction', convert: function (x) { try { return new type.Fraction(x); } catch (err) { throw new Error('Cannot convert "' + x + '" to Fraction'); } } }, { from: 'string', to: 'Complex', convert: function (x) { try { return new type.Complex(x); } catch (err) { throw new Error('Cannot convert "' + x + '" to Complex'); } } }, { from: 'boolean', to: 'number', convert: function (x) { return +x; } }, { from: 'boolean', to: 'BigNumber', convert: function (x) { return new type.BigNumber(+x); } }, { from: 'boolean', to: 'Fraction', convert: function (x) { return new type.Fraction(+x); } }, { from: 'boolean', to: 'string', convert: function (x) { return +x; } }, { from: 'null', to: 'number', convert: function () { return 0; } }, { from: 'null', to: 'string', convert: function () { return 'null'; } }, { from: 'null', to: 'BigNumber', convert: function () { return new type.BigNumber(0); } }, { from: 'null', to: 'Fraction', convert: function () { return new type.Fraction(0); } }, { from: 'Array', to: 'Matrix', convert: function (array) { // TODO: how to decide on the right type of matrix to create? return new type.DenseMatrix(array); } }, { from: 'Matrix', to: 'Array', convert: function (matrix) { return matrix.valueOf(); } } ]; return typed; }; /***/ }), /* 140 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var ArgumentsError = __webpack_require__(41); var DimensionError = __webpack_require__(9); var IndexError = __webpack_require__(42); module.exports = [ { name: 'ArgumentsError', path: 'error', factory: function () { return ArgumentsError; } }, { name: 'DimensionError', path: 'error', factory: function () { return DimensionError; } }, { name: 'IndexError', path: 'error', factory: function () { return IndexError; } } ]; // TODO: implement an InvalidValueError? /***/ }), /* 141 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var object = __webpack_require__(4); var string = __webpack_require__(19); function factory (type, config, load, typed) { var parser = load(__webpack_require__(82))(); /** * Documentation object * @param {Object} doc Object containing properties: * {string} name * {string} category * {string} description * {string[]} syntax * {string[]} examples * {string[]} seealso * @constructor */ function Help(doc) { if (!(this instanceof Help)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (!doc) throw new Error('Argument "doc" missing'); this.doc = doc; } /** * Attach type information */ Help.prototype.type = 'Help'; Help.prototype.isHelp = true; /** * Generate a string representation of the Help object * @return {string} Returns a string * @private */ Help.prototype.toString = function () { var doc = this.doc || {}; var desc = '\n'; if (doc.name) { desc += 'Name: ' + doc.name + '\n\n'; } if (doc.category) { desc += 'Category: ' + doc.category + '\n\n'; } if (doc.description) { desc += 'Description:\n ' + doc.description + '\n\n'; } if (doc.syntax) { desc += 'Syntax:\n ' + doc.syntax.join('\n ') + '\n\n'; } if (doc.examples) { desc += 'Examples:\n'; for (var i = 0; i < doc.examples.length; i++) { var expr = doc.examples[i]; desc += ' ' + expr + '\n'; var res; try { res = parser.eval(expr); } catch (e) { res = e; } if (res && !res.isHelp) { desc += ' ' + string.format(res, {precision: 14}) + '\n'; } } desc += '\n'; } if (doc.seealso) { desc += 'See also: ' + doc.seealso.join(', ') + '\n'; } return desc; }; /** * Export the help object to JSON */ Help.prototype.toJSON = function () { var obj = object.clone(this.doc); obj.mathjs = 'Help'; return obj; }; /** * Instantiate a Help object from a JSON object * @param {Object} json * @returns {Help} Returns a new Help object */ Help.fromJSON = function (json) { var doc = {}; for (var prop in json) { if (prop !== 'mathjs') { // ignore mathjs field doc[prop] = json[prop]; } } return new Help(doc); }; /** * Returns a string representation of the Help object */ Help.prototype.valueOf = Help.prototype.toString; return Help; } exports.name = 'Help'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 142 */ /***/ (function(module, exports) { module.exports = { 'name': 'Infinity', 'category': 'Constants', 'syntax': [ 'Infinity' ], 'description': 'Infinity, a number which is larger than the maximum number that can be handled by a floating point number.', 'examples': [ 'Infinity', '1 / 0' ], 'seealso': [] }; /***/ }), /* 143 */ /***/ (function(module, exports) { module.exports = { 'name': 'LN10', 'category': 'Constants', 'syntax': [ 'LN10' ], 'description': 'Returns the natural logarithm of 10, approximately equal to 2.302', 'examples': [ 'LN10', 'log(10)' ], 'seealso': [] }; /***/ }), /* 144 */ /***/ (function(module, exports) { module.exports = { 'name': 'LN2', 'category': 'Constants', 'syntax': [ 'LN2' ], 'description': 'Returns the natural logarithm of 2, approximately equal to 0.693', 'examples': [ 'LN2', 'log(2)' ], 'seealso': [] }; /***/ }), /* 145 */ /***/ (function(module, exports) { module.exports = { 'name': 'LOG10E', 'category': 'Constants', 'syntax': [ 'LOG10E' ], 'description': 'Returns the base-10 logarithm of E, approximately equal to 0.434', 'examples': [ 'LOG10E', 'log(e, 10)' ], 'seealso': [] }; /***/ }), /* 146 */ /***/ (function(module, exports) { module.exports = { 'name': 'LOG2E', 'category': 'Constants', 'syntax': [ 'LOG2E' ], 'description': 'Returns the base-2 logarithm of E, approximately equal to 1.442', 'examples': [ 'LOG2E', 'log(e, 2)' ], 'seealso': [] }; /***/ }), /* 147 */ /***/ (function(module, exports) { module.exports = { 'name': 'NaN', 'category': 'Constants', 'syntax': [ 'NaN' ], 'description': 'Not a number', 'examples': [ 'NaN', '0 / 0' ], 'seealso': [] }; /***/ }), /* 148 */ /***/ (function(module, exports) { module.exports = { 'name': 'SQRT1_2', 'category': 'Constants', 'syntax': [ 'SQRT1_2' ], 'description': 'Returns the square root of 1/2, approximately equal to 0.707', 'examples': [ 'SQRT1_2', 'sqrt(1/2)' ], 'seealso': [] }; /***/ }), /* 149 */ /***/ (function(module, exports) { module.exports = { 'name': 'SQRT2', 'category': 'Constants', 'syntax': [ 'SQRT2' ], 'description': 'Returns the square root of 2, approximately equal to 1.414', 'examples': [ 'SQRT2', 'sqrt(2)' ], 'seealso': [] }; /***/ }), /* 150 */ /***/ (function(module, exports) { module.exports = { 'name': 'false', 'category': 'Constants', 'syntax': [ 'false' ], 'description': 'Boolean value false', 'examples': [ 'false' ], 'seealso': ['true'] }; /***/ }), /* 151 */ /***/ (function(module, exports) { module.exports = { 'name': 'i', 'category': 'Constants', 'syntax': [ 'i' ], 'description': 'Imaginary unit, defined as i*i=-1. A complex number is described as a + b*i, where a is the real part, and b is the imaginary part.', 'examples': [ 'i', 'i * i', 'sqrt(-1)' ], 'seealso': [] }; /***/ }), /* 152 */ /***/ (function(module, exports) { module.exports = { 'name': 'null', 'category': 'Constants', 'syntax': [ 'null' ], 'description': 'Value null', 'examples': [ 'null' ], 'seealso': ['true', 'false'] }; /***/ }), /* 153 */ /***/ (function(module, exports) { module.exports = { 'name': 'phi', 'category': 'Constants', 'syntax': [ 'phi' ], 'description': 'Phi is the golden ratio. Two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Phi is defined as `(1 + sqrt(5)) / 2` and is approximately 1.618034...', 'examples': [ 'tau' ], 'seealso': [] }; /***/ }), /* 154 */ /***/ (function(module, exports) { module.exports = { 'name': 'tau', 'category': 'Constants', 'syntax': [ 'tau' ], 'description': 'Tau is the ratio constant of a circle\'s circumference to radius, equal to 2 * pi, approximately 6.2832.', 'examples': [ 'tau', '2 * pi' ], 'seealso': ['pi'] }; /***/ }), /* 155 */ /***/ (function(module, exports) { module.exports = { 'name': 'true', 'category': 'Constants', 'syntax': [ 'true' ], 'description': 'Boolean value true', 'examples': [ 'true' ], 'seealso': ['false'] }; /***/ }), /* 156 */ /***/ (function(module, exports) { module.exports = { 'name': 'version', 'category': 'Constants', 'syntax': [ 'version' ], 'description': 'A string with the version number of math.js', 'examples': [ 'version' ], 'seealso': [] }; /***/ }), /* 157 */ /***/ (function(module, exports) { module.exports = { 'name': 'bignumber', 'category': 'Construction', 'syntax': [ 'bignumber(x)' ], 'description': 'Create a big number from a number or string.', 'examples': [ '0.1 + 0.2', 'bignumber(0.1) + bignumber(0.2)', 'bignumber("7.2")', 'bignumber("7.2e500")', 'bignumber([0.1, 0.2, 0.3])' ], 'seealso': [ 'boolean', 'complex', 'fraction', 'index', 'matrix', 'string', 'unit' ] }; /***/ }), /* 158 */ /***/ (function(module, exports) { module.exports = { 'name': 'boolean', 'category': 'Construction', 'syntax': [ 'x', 'boolean(x)' ], 'description': 'Convert a string or number into a boolean.', 'examples': [ 'boolean(0)', 'boolean(1)', 'boolean(3)', 'boolean("true")', 'boolean("false")', 'boolean([1, 0, 1, 1])' ], 'seealso': [ 'bignumber', 'complex', 'index', 'matrix', 'number', 'string', 'unit' ] }; /***/ }), /* 159 */ /***/ (function(module, exports) { module.exports = { 'name': 'complex', 'category': 'Construction', 'syntax': [ 'complex()', 'complex(re, im)', 'complex(string)' ], 'description': 'Create a complex number.', 'examples': [ 'complex()', 'complex(2, 3)', 'complex("7 - 2i")' ], 'seealso': [ 'bignumber', 'boolean', 'index', 'matrix', 'number', 'string', 'unit' ] }; /***/ }), /* 160 */ /***/ (function(module, exports) { module.exports = { 'name': 'createUnit', 'category': 'Construction', 'syntax': [ 'createUnit(definitions)', 'createUnit(name, definition)' ], 'description': 'Create a user-defined unit and register it with the Unit type.', 'examples': [ 'createUnit("foo")', 'createUnit("knot", {definition: "0.514444444 m/s", aliases: ["knots", "kt", "kts"]})', 'createUnit("mph", "1 mile/hour")' ], 'seealso': [ 'unit', 'splitUnit' ] }; /***/ }), /* 161 */ /***/ (function(module, exports) { module.exports = { 'name': 'fraction', 'category': 'Construction', 'syntax': [ 'fraction(num)', 'fraction(num,den)' ], 'description': 'Create a fraction from a number or from a numerator and denominator.', 'examples': [ 'fraction(0.125)', 'fraction(1, 3) + fraction(2, 5)' ], 'seealso': [ 'bignumber', 'boolean', 'complex', 'index', 'matrix', 'string', 'unit' ] }; /***/ }), /* 162 */ /***/ (function(module, exports) { module.exports = { 'name': 'index', 'category': 'Construction', 'syntax': [ '[start]', '[start:end]', '[start:step:end]', '[start1, start 2, ...]', '[start1:end1, start2:end2, ...]', '[start1:step1:end1, start2:step2:end2, ...]' ], 'description': 'Create an index to get or replace a subset of a matrix', 'examples': [ '[]', '[1, 2, 3]', 'A = [1, 2, 3; 4, 5, 6]', 'A[1, :]', 'A[1, 2] = 50', 'A[0:2, 0:2] = ones(2, 2)' ], 'seealso': [ 'bignumber', 'boolean', 'complex', 'matrix,', 'number', 'range', 'string', 'unit' ] }; /***/ }), /* 163 */ /***/ (function(module, exports) { module.exports = { 'name': 'matrix', 'category': 'Construction', 'syntax': [ '[]', '[a1, b1, ...; a2, b2, ...]', 'matrix()', 'matrix("dense")', 'matrix([...])' ], 'description': 'Create a matrix.', 'examples': [ '[]', '[1, 2, 3]', '[1, 2, 3; 4, 5, 6]', 'matrix()', 'matrix([3, 4])', 'matrix([3, 4; 5, 6], "sparse")', 'matrix([3, 4; 5, 6], "sparse", "number")' ], 'seealso': [ 'bignumber', 'boolean', 'complex', 'index', 'number', 'string', 'unit', 'sparse' ] }; /***/ }), /* 164 */ /***/ (function(module, exports) { module.exports = { 'name': 'number', 'category': 'Construction', 'syntax': [ 'x', 'number(x)' ], 'description': 'Create a number or convert a string or boolean into a number.', 'examples': [ '2', '2e3', '4.05', 'number(2)', 'number("7.2")', 'number(true)', 'number([true, false, true, true])', 'number("52cm", "m")' ], 'seealso': [ 'bignumber', 'boolean', 'complex', 'fraction', 'index', 'matrix', 'string', 'unit' ] }; /***/ }), /* 165 */ /***/ (function(module, exports) { module.exports = { 'name': 'sparse', 'category': 'Construction', 'syntax': [ 'sparse()', 'sparse([a1, b1, ...; a1, b2, ...])', 'sparse([a1, b1, ...; a1, b2, ...], "number")' ], 'description': 'Create a sparse matrix.', 'examples': [ 'sparse()', 'sparse([3, 4; 5, 6])', 'sparse([3, 0; 5, 0], "number")' ], 'seealso': [ 'bignumber', 'boolean', 'complex', 'index', 'number', 'string', 'unit', 'matrix' ] }; /***/ }), /* 166 */ /***/ (function(module, exports) { module.exports = { 'name': 'splitUnit', 'category': 'Construction', 'syntax': [ 'splitUnit(unit: Unit, parts: Unit[])' ], 'description': 'Split a unit in an array of units whose sum is equal to the original unit.', 'examples': [ 'splitUnit(1 m, ["feet", "inch"])' ], 'seealso': [ 'unit', 'createUnit' ] }; /***/ }), /* 167 */ /***/ (function(module, exports) { module.exports = { 'name': 'string', 'category': 'Construction', 'syntax': [ '"text"', 'string(x)' ], 'description': 'Create a string or convert a value to a string', 'examples': [ '"Hello World!"', 'string(4.2)', 'string(3 + 2i)' ], 'seealso': [ 'bignumber', 'boolean', 'complex', 'index', 'matrix', 'number', 'unit' ] }; /***/ }), /* 168 */ /***/ (function(module, exports) { module.exports = { 'name': 'unit', 'category': 'Construction', 'syntax': [ 'value unit', 'unit(value, unit)', 'unit(string)' ], 'description': 'Create a unit.', 'examples': [ '5.5 mm', '3 inch', 'unit(7.1, "kilogram")', 'unit("23 deg")' ], 'seealso': [ 'bignumber', 'boolean', 'complex', 'index', 'matrix', 'number', 'string' ] }; /***/ }), /* 169 */ /***/ (function(module, exports) { module.exports = { 'name': 'config', 'category': 'Core', 'syntax': [ 'config()', 'config(options)' ], 'description': 'Get configuration or change configuration.', 'examples': [ 'config()', '1/3 + 1/4', 'config({number: "Fraction"})', '1/3 + 1/4' ], 'seealso': [] }; /***/ }), /* 170 */ /***/ (function(module, exports) { module.exports = { 'name': 'import', 'category': 'Core', 'syntax': [ 'import(functions)', 'import(functions, options)' ], 'description': 'Import functions or constants from an object.', 'examples': [ 'import({myFn: f(x)=x^2, myConstant: 32 })', 'myFn(2)', 'myConstant' ], 'seealso': [] }; /***/ }), /* 171 */ /***/ (function(module, exports) { module.exports = { 'name': 'typed', 'category': 'Core', 'syntax': [ 'typed(signatures)', 'typed(name, signatures)' ], 'description': 'Create a typed function.', 'examples': [ 'double = typed({ "number, number": f(x)=x+x })', 'double(2)', 'double("hello")' ], 'seealso': [] }; /***/ }), /* 172 */ /***/ (function(module, exports) { module.exports = { 'name': 'derivative', 'category': 'Algebra', 'syntax': [ 'derivative(expr)', 'derivative(expr, {simplify: boolean})' ], 'description': 'Takes the derivative of an expression expressed in parser Nodes. The derivative will be taken over the supplied variable in the second parameter. If there are multiple variables in the expression, it will return a partial derivative.', 'examples': [ 'derivative("2x^3", "x")', 'derivative("2x^3", "x", {simplify: false})', 'derivative("2x^2 + 3x + 4", "x")', 'derivative("sin(2x)", "x")', 'f = parse("x^2 + x")', 'x = parse("x")', 'df = derivative(f, x)', 'df.eval({x: 3})' ], 'seealso': [ 'simplify', 'parse', 'eval' ] }; /***/ }), /* 173 */ /***/ (function(module, exports) { module.exports = { 'name': 'lsolve', 'category': 'Algebra', 'syntax': [ 'x=lsolve(L, b)' ], 'description': 'Solves the linear system L * x = b where L is an [n x n] lower triangular matrix and b is a [n] column vector.', 'examples': [ 'a = [-2, 3; 2, 1]', 'b = [11, 9]', 'x = lsolve(a, b)' ], 'seealso': [ 'lup', 'lusolve', 'usolve', 'matrix', 'sparse' ] }; /***/ }), /* 174 */ /***/ (function(module, exports) { module.exports = { 'name': 'lup', 'category': 'Algebra', 'syntax': [ 'lup(m)' ], 'description': 'Calculate the Matrix LU decomposition with partial pivoting. Matrix A is decomposed in three matrices (L, U, P) where P * A = L * U', 'examples': [ 'lup([[2, 1], [1, 4]])', 'lup(matrix([[2, 1], [1, 4]]))', 'lup(sparse([[2, 1], [1, 4]]))' ], 'seealso': [ 'lusolve', 'lsolve', 'usolve', 'matrix', 'sparse', 'slu' ] }; /***/ }), /* 175 */ /***/ (function(module, exports) { module.exports = { 'name': 'lusolve', 'category': 'Algebra', 'syntax': [ 'x=lusolve(A, b)', 'x=lusolve(lu, b)' ], 'description': 'Solves the linear system A * x = b where A is an [n x n] matrix and b is a [n] column vector.', 'examples': [ 'a = [-2, 3; 2, 1]', 'b = [11, 9]', 'x = lusolve(a, b)' ], 'seealso': [ 'lup', 'slu', 'lsolve', 'usolve', 'matrix', 'sparse' ] }; /***/ }), /* 176 */ /***/ (function(module, exports) { module.exports = { 'name': 'simplify', 'category': 'Algebra', 'syntax': [ 'simplify(expr)', 'simplify(expr, rules)' ], 'description': 'Simplify an expression tree.', 'examples': [ 'simplify("3 + 2 / 4")', 'simplify("2x + x")', 'f = parse("x * (x + 2 + x)")', 'simplified = simplify(f)', 'simplified.eval({x: 2})' ], 'seealso': [ 'derivative', 'parse', 'eval' ] }; /***/ }), /* 177 */ /***/ (function(module, exports) { module.exports = { 'name': 'slu', 'category': 'Algebra', 'syntax': [ 'slu(A, order, threshold)' ], 'description': 'Calculate the Matrix LU decomposition with full pivoting. Matrix A is decomposed in two matrices (L, U) and two permutation vectors (pinv, q) where P * A * Q = L * U', 'examples': [ 'slu(sparse([4.5, 0, 3.2, 0; 3.1, 2.9, 0, 0.9; 0, 1.7, 3, 0; 3.5, 0.4, 0, 1]), 1, 0.001)' ], 'seealso': [ 'lusolve', 'lsolve', 'usolve', 'matrix', 'sparse', 'lup' ] }; /***/ }), /* 178 */ /***/ (function(module, exports) { module.exports = { 'name': 'usolve', 'category': 'Algebra', 'syntax': [ 'x=usolve(U, b)' ], 'description': 'Solves the linear system U * x = b where U is an [n x n] upper triangular matrix and b is a [n] column vector.', 'examples': [ 'x=usolve(sparse([1, 1, 1, 1; 0, 1, 1, 1; 0, 0, 1, 1; 0, 0, 0, 1]), [1; 2; 3; 4])' ], 'seealso': [ 'lup', 'lusolve', 'lsolve', 'matrix', 'sparse' ] }; /***/ }), /* 179 */ /***/ (function(module, exports) { module.exports = { 'name': 'abs', 'category': 'Arithmetic', 'syntax': [ 'abs(x)' ], 'description': 'Compute the absolute value.', 'examples': [ 'abs(3.5)', 'abs(-4.2)' ], 'seealso': ['sign'] }; /***/ }), /* 180 */ /***/ (function(module, exports) { module.exports = { 'name': 'add', 'category': 'Operators', 'syntax': [ 'x + y', 'add(x, y)' ], 'description': 'Add two values.', 'examples': [ 'a = 2.1 + 3.6', 'a - 3.6', '3 + 2i', '3 cm + 2 inch', '"2.3" + "4"' ], 'seealso': [ 'subtract' ] }; /***/ }), /* 181 */ /***/ (function(module, exports) { module.exports = { 'name': 'cbrt', 'category': 'Arithmetic', 'syntax': [ 'cbrt(x)', 'cbrt(x, allRoots)' ], 'description': 'Compute the cubic root value. If x = y * y * y, then y is the cubic root of x. When `x` is a number or complex number, an optional second argument `allRoots` can be provided to return all three cubic roots. If not provided, the principal root is returned', 'examples': [ 'cbrt(64)', 'cube(4)', 'cbrt(-8)', 'cbrt(2 + 3i)', 'cbrt(8i)', 'cbrt(8i, true)', 'cbrt(27 m^3)' ], 'seealso': [ 'square', 'sqrt', 'cube', 'multiply' ] }; /***/ }), /* 182 */ /***/ (function(module, exports) { module.exports = { 'name': 'ceil', 'category': 'Arithmetic', 'syntax': [ 'ceil(x)' ], 'description': 'Round a value towards plus infinity. If x is complex, both real and imaginary part are rounded towards plus infinity.', 'examples': [ 'ceil(3.2)', 'ceil(3.8)', 'ceil(-4.2)' ], 'seealso': ['floor', 'fix', 'round'] }; /***/ }), /* 183 */ /***/ (function(module, exports) { module.exports = { 'name': 'cube', 'category': 'Arithmetic', 'syntax': [ 'cube(x)' ], 'description': 'Compute the cube of a value. The cube of x is x * x * x.', 'examples': [ 'cube(2)', '2^3', '2 * 2 * 2' ], 'seealso': [ 'multiply', 'square', 'pow' ] }; /***/ }), /* 184 */ /***/ (function(module, exports) { module.exports = { 'name': 'divide', 'category': 'Operators', 'syntax': [ 'x / y', 'divide(x, y)' ], 'description': 'Divide two values.', 'examples': [ 'a = 2 / 3', 'a * 3', '4.5 / 2', '3 + 4 / 2', '(3 + 4) / 2', '18 km / 4.5' ], 'seealso': [ 'multiply' ] }; /***/ }), /* 185 */ /***/ (function(module, exports) { module.exports = { 'name': 'dotDivide', 'category': 'Operators', 'syntax': [ 'x ./ y', 'dotDivide(x, y)' ], 'description': 'Divide two values element wise.', 'examples': [ 'a = [1, 2, 3; 4, 5, 6]', 'b = [2, 1, 1; 3, 2, 5]', 'a ./ b' ], 'seealso': [ 'multiply', 'dotMultiply', 'divide' ] }; /***/ }), /* 186 */ /***/ (function(module, exports) { module.exports = { 'name': 'dotMultiply', 'category': 'Operators', 'syntax': [ 'x .* y', 'dotMultiply(x, y)' ], 'description': 'Multiply two values element wise.', 'examples': [ 'a = [1, 2, 3; 4, 5, 6]', 'b = [2, 1, 1; 3, 2, 5]', 'a .* b' ], 'seealso': [ 'multiply', 'divide', 'dotDivide' ] }; /***/ }), /* 187 */ /***/ (function(module, exports) { module.exports = { 'name': 'dotpow', 'category': 'Operators', 'syntax': [ 'x .^ y', 'dotpow(x, y)' ], 'description': 'Calculates the power of x to y element wise.', 'examples': [ 'a = [1, 2, 3; 4, 5, 6]', 'a .^ 2' ], 'seealso': [ 'pow' ] }; /***/ }), /* 188 */ /***/ (function(module, exports) { module.exports = { 'name': 'exp', 'category': 'Arithmetic', 'syntax': [ 'exp(x)' ], 'description': 'Calculate the exponent of a value.', 'examples': [ 'exp(1.3)', 'e ^ 1.3', 'log(exp(1.3))', 'x = 2.4', '(exp(i*x) == cos(x) + i*sin(x)) # Euler\'s formula' ], 'seealso': [ 'pow', 'log' ] }; /***/ }), /* 189 */ /***/ (function(module, exports) { module.exports = { 'name': 'fix', 'category': 'Arithmetic', 'syntax': [ 'fix(x)' ], 'description': 'Round a value towards zero. If x is complex, both real and imaginary part are rounded towards zero.', 'examples': [ 'fix(3.2)', 'fix(3.8)', 'fix(-4.2)', 'fix(-4.8)' ], 'seealso': ['ceil', 'floor', 'round'] }; /***/ }), /* 190 */ /***/ (function(module, exports) { module.exports = { 'name': 'floor', 'category': 'Arithmetic', 'syntax': [ 'floor(x)' ], 'description': 'Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.', 'examples': [ 'floor(3.2)', 'floor(3.8)', 'floor(-4.2)' ], 'seealso': ['ceil', 'fix', 'round'] }; /***/ }), /* 191 */ /***/ (function(module, exports) { module.exports = { 'name': 'gcd', 'category': 'Arithmetic', 'syntax': [ 'gcd(a, b)', 'gcd(a, b, c, ...)' ], 'description': 'Compute the greatest common divisor.', 'examples': [ 'gcd(8, 12)', 'gcd(-4, 6)', 'gcd(25, 15, -10)' ], 'seealso': [ 'lcm', 'xgcd' ] }; /***/ }), /* 192 */ /***/ (function(module, exports) { module.exports = { 'name': 'hypot', 'category': 'Arithmetic', 'syntax': [ 'hypot(a, b, c, ...)', 'hypot([a, b, c, ...])' ], 'description': 'Calculate the hypotenusa of a list with values. ', 'examples': [ 'hypot(3, 4)', 'sqrt(3^2 + 4^2)', 'hypot(-2)', 'hypot([3, 4, 5])' ], 'seealso': [ 'abs', 'norm' ] }; /***/ }), /* 193 */ /***/ (function(module, exports) { module.exports = { 'name': 'lcm', 'category': 'Arithmetic', 'syntax': [ 'lcm(x, y)' ], 'description': 'Compute the least common multiple.', 'examples': [ 'lcm(4, 6)', 'lcm(6, 21)', 'lcm(6, 21, 5)' ], 'seealso': [ 'gcd' ] }; /***/ }), /* 194 */ /***/ (function(module, exports) { module.exports = { 'name': 'log', 'category': 'Arithmetic', 'syntax': [ 'log(x)', 'log(x, base)' ], 'description': 'Compute the logarithm of a value. If no base is provided, the natural logarithm of x is calculated. If base if provided, the logarithm is calculated for the specified base. log(x, base) is defined as log(x) / log(base).', 'examples': [ 'log(3.5)', 'a = log(2.4)', 'exp(a)', '10 ^ 4', 'log(10000, 10)', 'log(10000) / log(10)', 'b = log(1024, 2)', '2 ^ b' ], 'seealso': [ 'exp', 'log10' ] }; /***/ }), /* 195 */ /***/ (function(module, exports) { module.exports = { 'name': 'log10', 'category': 'Arithmetic', 'syntax': [ 'log10(x)' ], 'description': 'Compute the 10-base logarithm of a value.', 'examples': [ 'log10(0.00001)', 'log10(10000)', '10 ^ 4', 'log(10000) / log(10)', 'log(10000, 10)' ], 'seealso': [ 'exp', 'log' ] }; /***/ }), /* 196 */ /***/ (function(module, exports) { module.exports = { 'name': 'mod', 'category': 'Operators', 'syntax': [ 'x % y', 'x mod y', 'mod(x, y)' ], 'description': 'Calculates the modulus, the remainder of an integer division.', 'examples': [ '7 % 3', '11 % 2', '10 mod 4', 'function isOdd(x) = x % 2', 'isOdd(2)', 'isOdd(3)' ], 'seealso': ['divide'] }; /***/ }), /* 197 */ /***/ (function(module, exports) { module.exports = { 'name': 'multiply', 'category': 'Operators', 'syntax': [ 'x * y', 'multiply(x, y)' ], 'description': 'multiply two values.', 'examples': [ 'a = 2.1 * 3.4', 'a / 3.4', '2 * 3 + 4', '2 * (3 + 4)', '3 * 2.1 km' ], 'seealso': [ 'divide' ] }; /***/ }), /* 198 */ /***/ (function(module, exports) { module.exports = { 'name': 'norm', 'category': 'Arithmetic', 'syntax': [ 'norm(x)', 'norm(x, p)' ], 'description': 'Calculate the norm of a number, vector or matrix.', 'examples': [ 'abs(-3.5)', 'norm(-3.5)', 'norm(3 - 4i))', 'norm([1, 2, -3], Infinity)', 'norm([1, 2, -3], -Infinity)', 'norm([3, 4], 2)', 'norm([[1, 2], [3, 4]], 1)', 'norm([[1, 2], [3, 4]], \'inf\')', 'norm([[1, 2], [3, 4]], \'fro\')' ] }; /***/ }), /* 199 */ /***/ (function(module, exports) { module.exports = { 'name': 'nthRoot', 'category': 'Arithmetic', 'syntax': [ 'nthRoot(a)', 'nthRoot(a, root)' ], 'description': 'Calculate the nth root of a value. ' + 'The principal nth root of a positive real number A, ' + 'is the positive real solution of the equation "x^root = A".', 'examples': [ '4 ^ 3', 'nthRoot(64, 3)', 'nthRoot(9, 2)', 'sqrt(9)' ], 'seealso': [ 'sqrt', 'pow' ] }; /***/ }), /* 200 */ /***/ (function(module, exports) { module.exports = { 'name': 'pow', 'category': 'Operators', 'syntax': [ 'x ^ y', 'pow(x, y)' ], 'description': 'Calculates the power of x to y, x^y.', 'examples': [ '2^3 = 8', '2*2*2', '1 + e ^ (pi * i)' ], 'seealso': [ 'multiply' ] }; /***/ }), /* 201 */ /***/ (function(module, exports) { module.exports = { 'name': 'round', 'category': 'Arithmetic', 'syntax': [ 'round(x)', 'round(x, n)' ], 'description': 'round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.', 'examples': [ 'round(3.2)', 'round(3.8)', 'round(-4.2)', 'round(-4.8)', 'round(pi, 3)', 'round(123.45678, 2)' ], 'seealso': ['ceil', 'floor', 'fix'] }; /***/ }), /* 202 */ /***/ (function(module, exports) { module.exports = { 'name': 'sign', 'category': 'Arithmetic', 'syntax': [ 'sign(x)' ], 'description': 'Compute the sign of a value. The sign of a value x is 1 when x>1, -1 when x<0, and 0 when x=0.', 'examples': [ 'sign(3.5)', 'sign(-4.2)', 'sign(0)' ], 'seealso': [ 'abs' ] }; /***/ }), /* 203 */ /***/ (function(module, exports) { module.exports = { 'name': 'sqrt', 'category': 'Arithmetic', 'syntax': [ 'sqrt(x)' ], 'description': 'Compute the square root value. If x = y * y, then y is the square root of x.', 'examples': [ 'sqrt(25)', '5 * 5', 'sqrt(-1)' ], 'seealso': [ 'square', 'multiply' ] }; /***/ }), /* 204 */ /***/ (function(module, exports) { module.exports = { 'name': 'square', 'category': 'Arithmetic', 'syntax': [ 'square(x)' ], 'description': 'Compute the square of a value. The square of x is x * x.', 'examples': [ 'square(3)', 'sqrt(9)', '3^2', '3 * 3' ], 'seealso': [ 'multiply', 'pow', 'sqrt', 'cube' ] }; /***/ }), /* 205 */ /***/ (function(module, exports) { module.exports = { 'name': 'subtract', 'category': 'Operators', 'syntax': [ 'x - y', 'subtract(x, y)' ], 'description': 'subtract two values.', 'examples': [ 'a = 5.3 - 2', 'a + 2', '2/3 - 1/6', '2 * 3 - 3', '2.1 km - 500m' ], 'seealso': [ 'add' ] }; /***/ }), /* 206 */ /***/ (function(module, exports) { module.exports = { 'name': 'unaryMinus', 'category': 'Operators', 'syntax': [ '-x', 'unaryMinus(x)' ], 'description': 'Inverse the sign of a value. Converts booleans and strings to numbers.', 'examples': [ '-4.5', '-(-5.6)', '-"22"' ], 'seealso': [ 'add', 'subtract', 'unaryPlus' ] }; /***/ }), /* 207 */ /***/ (function(module, exports) { module.exports = { 'name': 'unaryPlus', 'category': 'Operators', 'syntax': [ '+x', 'unaryPlus(x)' ], 'description': 'Converts booleans and strings to numbers.', 'examples': [ '+true', '+"2"' ], 'seealso': [ 'add', 'subtract', 'unaryMinus' ] }; /***/ }), /* 208 */ /***/ (function(module, exports) { module.exports = { 'name': 'xgcd', 'category': 'Arithmetic', 'syntax': [ 'xgcd(a, b)' ], 'description': 'Calculate the extended greatest common divisor for two values', 'examples': [ 'xgcd(8, 12)', 'gcd(8, 12)', 'xgcd(36163, 21199)' ], 'seealso': [ 'gcd', 'lcm' ] }; /***/ }), /* 209 */ /***/ (function(module, exports) { module.exports = { 'name': 'bitAnd', 'category': 'Bitwise', 'syntax': [ 'x & y', 'bitAnd(x, y)' ], 'description': 'Bitwise AND operation. Performs the logical AND operation on each pair of the corresponding bits of the two given values by multiplying them. If both bits in the compared position are 1, the bit in the resulting binary representation is 1, otherwise, the result is 0', 'examples': [ '5 & 3', 'bitAnd(53, 131)', '[1, 12, 31] & 42' ], 'seealso': [ 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift' ] }; /***/ }), /* 210 */ /***/ (function(module, exports) { module.exports = { 'name': 'bitNot', 'category': 'Bitwise', 'syntax': [ '~x', 'bitNot(x)' ], 'description': 'Bitwise NOT operation. Performs a logical negation on each bit of the given value. Bits that are 0 become 1, and those that are 1 become 0.', 'examples': [ '~1', '~2', 'bitNot([2, -3, 4])' ], 'seealso': [ 'bitAnd', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift' ] }; /***/ }), /* 211 */ /***/ (function(module, exports) { module.exports = { 'name': 'bitOr', 'category': 'Bitwise', 'syntax': [ 'x | y', 'bitOr(x, y)' ], 'description': 'Bitwise OR operation. Performs the logical inclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if the first bit is 1 or the second bit is 1 or both bits are 1, otherwise, the result is 0.', 'examples': [ '5 | 3', 'bitOr([1, 2, 3], 4)' ], 'seealso': [ 'bitAnd', 'bitNot', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift' ] }; /***/ }), /* 212 */ /***/ (function(module, exports) { module.exports = { 'name': 'bitXor', 'category': 'Bitwise', 'syntax': [ 'bitXor(x, y)' ], 'description': 'Bitwise XOR operation, exclusive OR. Performs the logical exclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.', 'examples': [ 'bitOr(1, 2)', 'bitXor([2, 3, 4], 4)' ], 'seealso': [ 'bitAnd', 'bitNot', 'bitOr', 'leftShift', 'rightArithShift', 'rightLogShift' ] }; /***/ }), /* 213 */ /***/ (function(module, exports) { module.exports = { 'name': 'leftShift', 'category': 'Bitwise', 'syntax': [ 'x << y', 'leftShift(x, y)' ], 'description': 'Bitwise left logical shift of a value x by y number of bits.', 'examples': [ '4 << 1', '8 >> 1' ], 'seealso': [ 'bitAnd', 'bitNot', 'bitOr', 'bitXor', 'rightArithShift', 'rightLogShift' ] }; /***/ }), /* 214 */ /***/ (function(module, exports) { module.exports = { 'name': 'rightArithShift', 'category': 'Bitwise', 'syntax': [ 'x >> y', 'leftShift(x, y)' ], 'description': 'Bitwise right arithmetic shift of a value x by y number of bits.', 'examples': [ '8 >> 1', '4 << 1', '-12 >> 2' ], 'seealso': [ 'bitAnd', 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightLogShift' ] }; /***/ }), /* 215 */ /***/ (function(module, exports) { module.exports = { 'name': 'rightLogShift', 'category': 'Bitwise', 'syntax': [ 'x >> y', 'leftShift(x, y)' ], 'description': 'Bitwise right logical shift of a value x by y number of bits.', 'examples': [ '8 >>> 1', '4 << 1', '-12 >>> 2' ], 'seealso': [ 'bitAnd', 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift' ] }; /***/ }), /* 216 */ /***/ (function(module, exports) { module.exports = { 'name': 'bellNumbers', 'category': 'Combinatorics', 'syntax': [ 'bellNumbers(n)' ], 'description': 'The Bell Numbers count the number of partitions of a set. A partition is a pairwise disjoint subset of S whose union is S. `bellNumbers` only takes integer arguments. The following condition must be enforced: n >= 0.', 'examples': [ 'bellNumbers(3)', 'bellNumbers(8)' ], 'seealso': ['stirlingS2'] }; /***/ }), /* 217 */ /***/ (function(module, exports) { module.exports = { 'name': 'catalan', 'category': 'Combinatorics', 'syntax': [ 'catalan(n)' ], 'description': 'The Catalan Numbers enumerate combinatorial structures of many different types. catalan only takes integer arguments. The following condition must be enforced: n >= 0.', 'examples': [ 'catalan(3)', 'catalan(8)' ], 'seealso': ['bellNumbers'] }; /***/ }), /* 218 */ /***/ (function(module, exports) { module.exports = { 'name': 'composition', 'category': 'Combinatorics', 'syntax': [ 'composition(n, k)' ], 'description': 'The composition counts of n into k parts. composition only takes integer arguments. The following condition must be enforced: k <= n.', 'examples': [ 'composition(5, 3)' ], 'seealso': ['combinations'] }; /***/ }), /* 219 */ /***/ (function(module, exports) { module.exports = { 'name': 'stirlingS2', 'category': 'Combinatorics', 'syntax': [ 'stirlingS2(n, k)' ], 'description': 'he Stirling numbers of the second kind, counts the number of ways to partition a set of n labelled objects into k nonempty unlabelled subsets. `stirlingS2` only takes integer arguments. The following condition must be enforced: k <= n. If n = k or k = 1, then s(n,k) = 1.', 'examples': [ 'stirlingS2(5, 3)' ], 'seealso': ['bellNumbers'] }; /***/ }), /* 220 */ /***/ (function(module, exports) { module.exports = { 'name': 'arg', 'category': 'Complex', 'syntax': [ 'arg(x)' ], 'description': 'Compute the argument of a complex value. If x = a+bi, the argument is computed as atan2(b, a).', 'examples': [ 'arg(2 + 2i)', 'atan2(3, 2)', 'arg(2 + 3i)' ], 'seealso': [ 're', 'im', 'conj', 'abs' ] }; /***/ }), /* 221 */ /***/ (function(module, exports) { module.exports = { 'name': 'conj', 'category': 'Complex', 'syntax': [ 'conj(x)' ], 'description': 'Compute the complex conjugate of a complex value. If x = a+bi, the complex conjugate is a-bi.', 'examples': [ 'conj(2 + 3i)', 'conj(2 - 3i)', 'conj(-5.2i)' ], 'seealso': [ 're', 'im', 'abs', 'arg' ] }; /***/ }), /* 222 */ /***/ (function(module, exports) { module.exports = { 'name': 'im', 'category': 'Complex', 'syntax': [ 'im(x)' ], 'description': 'Get the imaginary part of a complex number.', 'examples': [ 'im(2 + 3i)', 're(2 + 3i)', 'im(-5.2i)', 'im(2.4)' ], 'seealso': [ 're', 'conj', 'abs', 'arg' ] }; /***/ }), /* 223 */ /***/ (function(module, exports) { module.exports = { 'name': 're', 'category': 'Complex', 'syntax': [ 're(x)' ], 'description': 'Get the real part of a complex number.', 'examples': [ 're(2 + 3i)', 'im(2 + 3i)', 're(-5.2i)', 're(2.4)' ], 'seealso': [ 'im', 'conj', 'abs', 'arg' ] }; /***/ }), /* 224 */ /***/ (function(module, exports) { module.exports = { 'name': 'eval', 'category': 'Expression', 'syntax': [ 'eval(expression)', 'eval([expr1, expr2, expr3, ...])' ], 'description': 'Evaluate an expression or an array with expressions.', 'examples': [ 'eval("2 + 3")', 'eval("sqrt(" + 4 + ")")' ], 'seealso': [] }; /***/ }), /* 225 */ /***/ (function(module, exports) { module.exports = { 'name': 'help', 'category': 'Expression', 'syntax': [ 'help(object)', 'help(string)' ], 'description': 'Display documentation on a function or data type.', 'examples': [ 'help(sqrt)', 'help("complex")' ], 'seealso': [] }; /***/ }), /* 226 */ /***/ (function(module, exports) { module.exports = { 'name': 'distance', 'category': 'Geometry', 'syntax': [ 'distance([x1, y1], [x2, y2])', 'distance([[x1, y1], [x2, y2])' ], 'description': 'Calculates the Euclidean distance between two points.', 'examples': [ 'distance([0,0], [4,4])', 'distance([[0,0], [4,4]])' ], 'seealso': [] }; /***/ }), /* 227 */ /***/ (function(module, exports) { module.exports = { 'name': 'intersect', 'category': 'Geometry', 'syntax': [ 'intersect(expr1, expr2, expr3, expr4)', 'intersect(expr1, expr2, expr3)' ], 'description': 'Computes the intersection point of lines and/or planes.', 'examples': [ 'intersect([0, 0], [10, 10], [10, 0], [0, 10])', 'intersect([1, 0, 1], [4, -2, 2], [1, 1, 1, 6])' ], 'seealso': [] }; /***/ }), /* 228 */ /***/ (function(module, exports) { module.exports = { 'name': 'and', 'category': 'Logical', 'syntax': [ 'x and y', 'and(x, y)' ], 'description': 'Logical and. Test whether two values are both defined with a nonzero/nonempty value.', 'examples': [ 'true and false', 'true and true', '2 and 4' ], 'seealso': [ 'not', 'or', 'xor' ] }; /***/ }), /* 229 */ /***/ (function(module, exports) { module.exports = { 'name': 'not', 'category': 'Logical', 'syntax': [ 'not x', 'not(x)' ], 'description': 'Logical not. Flips the boolean value of given argument.', 'examples': [ 'not true', 'not false', 'not 2', 'not 0' ], 'seealso': [ 'and', 'or', 'xor' ] }; /***/ }), /* 230 */ /***/ (function(module, exports) { module.exports = { 'name': 'or', 'category': 'Logical', 'syntax': [ 'x or y', 'or(x, y)' ], 'description': 'Logical or. Test if at least one value is defined with a nonzero/nonempty value.', 'examples': [ 'true or false', 'false or false', '0 or 4' ], 'seealso': [ 'not', 'and', 'xor' ] }; /***/ }), /* 231 */ /***/ (function(module, exports) { module.exports = { 'name': 'xor', 'category': 'Logical', 'syntax': [ 'x or y', 'or(x, y)' ], 'description': 'Logical exclusive or, xor. Test whether one and only one value is defined with a nonzero/nonempty value.', 'examples': [ 'true xor false', 'false xor false', 'true xor true', '0 or 4' ], 'seealso': [ 'not', 'and', 'or' ] }; /***/ }), /* 232 */ /***/ (function(module, exports) { module.exports = { 'name': 'concat', 'category': 'Matrix', 'syntax': [ 'concat(A, B, C, ...)', 'concat(A, B, C, ..., dim)' ], 'description': 'Concatenate matrices. By default, the matrices are concatenated by the last dimension. The dimension on which to concatenate can be provided as last argument.', 'examples': [ 'A = [1, 2; 5, 6]', 'B = [3, 4; 7, 8]', 'concat(A, B)', 'concat(A, B, 1)', 'concat(A, B, 2)' ], 'seealso': [ 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 233 */ /***/ (function(module, exports) { module.exports = { 'name': 'cross', 'category': 'Matrix', 'syntax': [ 'cross(A, B)' ], 'description': 'Calculate the cross product for two vectors in three dimensional space.', 'examples': [ 'cross([1, 1, 0], [0, 1, 1])', 'cross([3, -3, 1], [4, 9, 2])', 'cross([2, 3, 4], [5, 6, 7])' ], 'seealso': [ 'multiply', 'dot' ] }; /***/ }), /* 234 */ /***/ (function(module, exports) { module.exports = { 'name': 'det', 'category': 'Matrix', 'syntax': [ 'det(x)' ], 'description': 'Calculate the determinant of a matrix', 'examples': [ 'det([1, 2; 3, 4])', 'det([-2, 2, 3; -1, 1, 3; 2, 0, -1])' ], 'seealso': [ 'concat', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 235 */ /***/ (function(module, exports) { module.exports = { 'name': 'diag', 'category': 'Matrix', 'syntax': [ 'diag(x)', 'diag(x, k)' ], 'description': 'Create a diagonal matrix or retrieve the diagonal of a matrix. When x is a vector, a matrix with the vector values on the diagonal will be returned. When x is a matrix, a vector with the diagonal values of the matrix is returned. When k is provided, the k-th diagonal will be filled in or retrieved, if k is positive, the values are placed on the super diagonal. When k is negative, the values are placed on the sub diagonal.', 'examples': [ 'diag(1:3)', 'diag(1:3, 1)', 'a = [1, 2, 3; 4, 5, 6; 7, 8, 9]', 'diag(a)' ], 'seealso': [ 'concat', 'det', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 236 */ /***/ (function(module, exports) { module.exports = { 'name': 'dot', 'category': 'Matrix', 'syntax': [ 'dot(A, B)' ], 'description': 'Calculate the dot product of two vectors. ' + 'The dot product of A = [a1, a2, a3, ..., an] and B = [b1, b2, b3, ..., bn] ' + 'is defined as dot(A, B) = a1 * b1 + a2 * b2 + a3 * b3 + ... + an * bn', 'examples': [ 'dot([2, 4, 1], [2, 2, 3])', '[2, 4, 1] * [2, 2, 3]' ], 'seealso': [ 'multiply', 'cross' ] }; /***/ }), /* 237 */ /***/ (function(module, exports) { module.exports = { 'name': 'eye', 'category': 'Matrix', 'syntax': [ 'eye(n)', 'eye(m, n)', 'eye([m, n])', 'eye' ], 'description': 'Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.', 'examples': [ 'eye(3)', 'eye(3, 5)', 'a = [1, 2, 3; 4, 5, 6]', 'eye(size(a))' ], 'seealso': [ 'concat', 'det', 'diag', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 238 */ /***/ (function(module, exports) { module.exports = { 'name': 'filter', 'category': 'Matrix', 'syntax': [ 'filter(x, test)' ], 'description': 'Filter items in a matrix.', 'examples': [ 'isPositive(x) = x > 0', 'filter([6, -2, -1, 4, 3], isPositive)', 'filter([6, -2, 0, 1, 0], x != 0)' ], 'seealso': ['sort', 'map', 'forEach'] }; /***/ }), /* 239 */ /***/ (function(module, exports) { module.exports = { 'name': 'flatten', 'category': 'Matrix', 'syntax': [ 'flatten(x)' ], 'description': 'Flatten a multi dimensional matrix into a single dimensional matrix.', 'examples': [ 'a = [1, 2, 3; 4, 5, 6]', 'size(a)', 'b = flatten(a)', 'size(b)' ], 'seealso': [ 'concat', 'resize', 'size', 'squeeze' ] }; /***/ }), /* 240 */ /***/ (function(module, exports) { module.exports = { 'name': 'forEach', 'category': 'Matrix', 'syntax': [ 'forEach(x, callback)' ], 'description': 'Iterates over all elements of a matrix/array, and executes the given callback function.', 'examples': [ 'forEach([1, 2, 3], function(val) { console.log(val) })' ], 'seealso': ['map', 'sort', 'filter'] }; /***/ }), /* 241 */ /***/ (function(module, exports) { module.exports = { 'name': 'inv', 'category': 'Matrix', 'syntax': [ 'inv(x)' ], 'description': 'Calculate the inverse of a matrix', 'examples': [ 'inv([1, 2; 3, 4])', 'inv(4)', '1 / 4' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 242 */ /***/ (function(module, exports) { module.exports = { 'name': 'kron', 'category': 'Matrix', 'syntax': [ 'math.kron(x, y)' ], 'description': 'Calculates the kronecker product of 2 matrices or vectors.', 'examples': [ 'kron([[1, 0], [0, 1]], [[1, 2], [3, 4]])', 'kron([1,1], [2,3,4])' ], 'seealso': [ 'multiply', 'dot', 'cross' ] }; /***/ }), /* 243 */ /***/ (function(module, exports) { module.exports = { 'name': 'map', 'category': 'Matrix', 'syntax': [ 'map(x, callback)' ], 'description': 'Create a new matrix or array with the results of the callback function executed on each entry of the matrix/array.', 'examples': [ 'map([1, 2, 3], function(val) { return value * value })' ], 'seealso': ['filter', 'forEach'] }; /***/ }), /* 244 */ /***/ (function(module, exports) { module.exports = { 'name': 'ones', 'category': 'Matrix', 'syntax': [ 'ones(m)', 'ones(m, n)', 'ones(m, n, p, ...)', 'ones([m])', 'ones([m, n])', 'ones([m, n, p, ...])', 'ones' ], 'description': 'Create a matrix containing ones.', 'examples': [ 'ones(3)', 'ones(3, 5)', 'ones([2,3]) * 4.5', 'a = [1, 2, 3; 4, 5, 6]', 'ones(size(a))' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 245 */ /***/ (function(module, exports) { module.exports = { 'name': 'partitionSelect', 'category': 'Matrix', 'syntax': [ 'partitionSelect(x, k)', 'partitionSelect(x, k, compare)' ], 'description': 'Partition-based selection of an array or 1D matrix. Will find the kth smallest value, and mutates the input array. Uses Quickselect.', 'examples': [ 'partitionSelect([5, 10, 1], 2)', 'partitionSelect(["C", "B", "A", "D"], 1)' ], 'seealso': ['sort'] }; /***/ }), /* 246 */ /***/ (function(module, exports) { module.exports = { 'name': 'range', 'category': 'Type', 'syntax': [ 'start:end', 'start:step:end', 'range(start, end)', 'range(start, end, step)', 'range(string)' ], 'description': 'Create a range. Lower bound of the range is included, upper bound is excluded.', 'examples': [ '1:5', '3:-1:-3', 'range(3, 7)', 'range(0, 12, 2)', 'range("4:10")', 'a = [1, 2, 3, 4; 5, 6, 7, 8]', 'a[1:2, 1:2]' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 247 */ /***/ (function(module, exports) { module.exports = { 'name': 'resize', 'category': 'Matrix', 'syntax': [ 'resize(x, size)', 'resize(x, size, defaultValue)' ], 'description': 'Resize a matrix.', 'examples': [ 'resize([1,2,3,4,5], [3])', 'resize([1,2,3], [5])', 'resize([1,2,3], [5], -1)', 'resize(2, [2, 3])', 'resize("hello", [8], "!")' ], 'seealso': [ 'size', 'subset', 'squeeze' ] }; /***/ }), /* 248 */ /***/ (function(module, exports) { module.exports = { 'name': 'size', 'category': 'Matrix', 'syntax': [ 'size(x)' ], 'description': 'Calculate the size of a matrix.', 'examples': [ 'size(2.3)', 'size("hello world")', 'a = [1, 2; 3, 4; 5, 6]', 'size(a)', 'size(1:6)' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 249 */ /***/ (function(module, exports) { module.exports = { 'name': 'sort', 'category': 'Matrix', 'syntax': [ 'sort(x)', 'sort(x, compare)' ], 'description': 'Sort the items in a matrix. Compare can be a string "asc" or "desc", or a custom sort function.', 'examples': [ 'sort([5, 10, 1])', 'sort(["C", "B", "A", "D"])', 'sortByLength(a, b) = size(a)[1] - size(b)[1]', 'sort(["Langdon", "Tom", "Sara"], sortByLength)' ], 'seealso': ['map', 'filter', 'forEach'] }; /***/ }), /* 250 */ /***/ (function(module, exports) { module.exports = { 'name': 'squeeze', 'category': 'Matrix', 'syntax': [ 'squeeze(x)' ], 'description': 'Remove inner and outer singleton dimensions from a matrix.', 'examples': [ 'a = zeros(3,2,1)', 'size(squeeze(a))', 'b = zeros(1,1,3)', 'size(squeeze(b))' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'subset', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 251 */ /***/ (function(module, exports) { module.exports = { 'name': 'subset', 'category': 'Matrix', 'syntax': [ 'value(index)', 'value(index) = replacement', 'subset(value, [index])', 'subset(value, [index], replacement)' ], 'description': 'Get or set a subset of a matrix or string. ' + 'Indexes are one-based. ' + 'Both the ranges lower-bound and upper-bound are included.', 'examples': [ 'd = [1, 2; 3, 4]', 'e = []', 'e[1, 1:2] = [5, 6]', 'e[2, :] = [7, 8]', 'f = d * e', 'f[2, 1]', 'f[:, 1]' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'trace', 'transpose', 'zeros' ] }; /***/ }), /* 252 */ /***/ (function(module, exports) { module.exports = { 'name': 'trace', 'category': 'Matrix', 'syntax': [ 'trace(A)' ], 'description': 'Calculate the trace of a matrix: the sum of the elements on the main diagonal of a square matrix.', 'examples': [ 'A = [1, 2, 3; -1, 2, 3; 2, 0, 3]', 'trace(A)' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'transpose', 'zeros' ] }; /***/ }), /* 253 */ /***/ (function(module, exports) { module.exports = { 'name': 'transpose', 'category': 'Matrix', 'syntax': [ 'x\'', 'transpose(x)' ], 'description': 'Transpose a matrix', 'examples': [ 'a = [1, 2, 3; 4, 5, 6]', 'a\'', 'transpose(a)' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'zeros' ] }; /***/ }), /* 254 */ /***/ (function(module, exports) { module.exports = { 'name': 'zeros', 'category': 'Matrix', 'syntax': [ 'zeros(m)', 'zeros(m, n)', 'zeros(m, n, p, ...)', 'zeros([m])', 'zeros([m, n])', 'zeros([m, n, p, ...])', 'zeros' ], 'description': 'Create a matrix containing zeros.', 'examples': [ 'zeros(3)', 'zeros(3, 5)', 'a = [1, 2, 3; 4, 5, 6]', 'zeros(size(a))' ], 'seealso': [ 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose' ] }; /***/ }), /* 255 */ /***/ (function(module, exports) { module.exports = { 'name': 'combinations', 'category': 'Probability', 'syntax': [ 'combinations(n, k)' ], 'description': 'Compute the number of combinations of n items taken k at a time', 'examples': [ 'combinations(7, 5)' ], 'seealso': ['permutations', 'factorial'] }; /***/ }), /* 256 */ /***/ (function(module, exports) { module.exports = { 'name': 'factorial', 'category': 'Probability', 'syntax': [ 'kldivergence(x, y)' ], 'description': 'Compute the factorial of a value', 'examples': [ '5!', '5 * 4 * 3 * 2 * 1', '3!' ], 'seealso': ['combinations', 'permutations', 'gamma'] }; /***/ }), /* 257 */ /***/ (function(module, exports) { module.exports = { 'name': 'gamma', 'category': 'Probability', 'syntax': [ 'gamma(n)' ], 'description': 'Compute the gamma function. For small values, the Lanczos approximation is used, and for large values the extended Stirling approximation.', 'examples': [ 'gamma(4)', '3!', 'gamma(1/2)', 'sqrt(pi)' ], 'seealso': ['factorial'] }; /***/ }), /* 258 */ /***/ (function(module, exports) { module.exports = { 'name': 'kldivergence', 'category': 'Probability', 'syntax': [ 'n!', 'factorial(n)' ], 'description': 'Calculate the Kullback-Leibler (KL) divergence between two distributions.', 'examples': [ 'math.kldivergence([0.7,0.5,0.4], [0.2,0.9,0.5])' ], 'seealso': [] }; /***/ }), /* 259 */ /***/ (function(module, exports) { module.exports = { 'name': 'multinomial', 'category': 'Probability', 'syntax': [ 'multinomial(A)' ], 'description': 'Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities. multinomial takes one array of integers as an argument. The following condition must be enforced: every ai <= 0.', 'examples': [ 'multinomial([1, 2, 1])' ], 'seealso': ['combinations', 'factorial'] }; /***/ }), /* 260 */ /***/ (function(module, exports) { module.exports = { 'name': 'permutations', 'category': 'Probability', 'syntax': [ 'permutations(n)', 'permutations(n, k)' ], 'description': 'Compute the number of permutations of n items taken k at a time', 'examples': [ 'permutations(5)', 'permutations(5, 3)' ], 'seealso': ['combinations', 'factorial'] }; /***/ }), /* 261 */ /***/ (function(module, exports) { module.exports = { 'name': 'pickRandom', 'category': 'Probability', 'syntax': [ 'pickRandom(array)', 'pickRandom(array, number)', 'pickRandom(array, weights)', 'pickRandom(array, number, weights)', 'pickRandom(array, weights, number)' ], 'description': 'Pick a random entry from a given array.', 'examples': [ 'pickRandom(0:10)', 'pickRandom([1, 3, 1, 6])', 'pickRandom([1, 3, 1, 6], 2)', 'pickRandom([1, 3, 1, 6], [2, 3, 2, 1])', 'pickRandom([1, 3, 1, 6], 2, [2, 3, 2, 1])', 'pickRandom([1, 3, 1, 6], [2, 3, 2, 1], 2)' ], 'seealso': ['random', 'randomInt'] }; /***/ }), /* 262 */ /***/ (function(module, exports) { module.exports = { 'name': 'random', 'category': 'Probability', 'syntax': [ 'random()', 'random(max)', 'random(min, max)', 'random(size)', 'random(size, max)', 'random(size, min, max)' ], 'description': 'Return a random number.', 'examples': [ 'random()', 'random(10, 20)', 'random([2, 3])' ], 'seealso': ['pickRandom', 'randomInt'] }; /***/ }), /* 263 */ /***/ (function(module, exports) { module.exports = { 'name': 'randInt', 'category': 'Probability', 'syntax': [ 'randInt(max)', 'randInt(min, max)', 'randInt(size)', 'randInt(size, max)', 'randInt(size, min, max)' ], 'description': 'Return a random integer number', 'examples': [ 'randInt(10, 20)', 'randInt([2, 3], 10)' ], 'seealso': ['pickRandom', 'random'] }; /***/ }), /* 264 */ /***/ (function(module, exports) { module.exports = { 'name': 'compare', 'category': 'Relational', 'syntax': [ 'compare(x, y)' ], 'description': 'Compare two values. Returns 1 if x is larger than y, -1 if x is smaller than y, and 0 if x and y are equal.', 'examples': [ 'compare(2, 3)', 'compare(3, 2)', 'compare(2, 2)', 'compare(5cm, 40mm)', 'compare(2, [1, 2, 3])' ], 'seealso': [ 'equal', 'unequal', 'smaller', 'smallerEq', 'largerEq' ] }; /***/ }), /* 265 */ /***/ (function(module, exports) { module.exports = { 'name': 'deepEqual', 'category': 'Relational', 'syntax': [ 'deepEqual(x, y)' ], 'description': 'Check equality of two matrices element wise. Returns true if the size of both matrices is equal and when and each of the elements are equal.', 'examples': [ '[1,3,4] == [1,3,4]', '[1,3,4] == [1,3]' ], 'seealso': [ 'equal', 'unequal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare' ] }; /***/ }), /* 266 */ /***/ (function(module, exports) { module.exports = { 'name': 'equal', 'category': 'Relational', 'syntax': [ 'x == y', 'equal(x, y)' ], 'description': 'Check equality of two values. Returns true if the values are equal, and false if not.', 'examples': [ '2+2 == 3', '2+2 == 4', 'a = 3.2', 'b = 6-2.8', 'a == b', '50cm == 0.5m' ], 'seealso': [ 'unequal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare', 'deepEqual' ] }; /***/ }), /* 267 */ /***/ (function(module, exports) { module.exports = { 'name': 'larger', 'category': 'Relational', 'syntax': [ 'x > y', 'larger(x, y)' ], 'description': 'Check if value x is larger than y. Returns true if x is larger than y, and false if not.', 'examples': [ '2 > 3', '5 > 2*2', 'a = 3.3', 'b = 6-2.8', '(a > b)', '(b < a)', '5 cm > 2 inch' ], 'seealso': [ 'equal', 'unequal', 'smaller', 'smallerEq', 'largerEq', 'compare' ] }; /***/ }), /* 268 */ /***/ (function(module, exports) { module.exports = { 'name': 'largerEq', 'category': 'Relational', 'syntax': [ 'x >= y', 'largerEq(x, y)' ], 'description': 'Check if value x is larger or equal to y. Returns true if x is larger or equal to y, and false if not.', 'examples': [ '2 > 1+1', '2 >= 1+1', 'a = 3.2', 'b = 6-2.8', '(a > b)' ], 'seealso': [ 'equal', 'unequal', 'smallerEq', 'smaller', 'largerEq', 'compare' ] }; /***/ }), /* 269 */ /***/ (function(module, exports) { module.exports = { 'name': 'smaller', 'category': 'Relational', 'syntax': [ 'x < y', 'smaller(x, y)' ], 'description': 'Check if value x is smaller than value y. Returns true if x is smaller than y, and false if not.', 'examples': [ '2 < 3', '5 < 2*2', 'a = 3.3', 'b = 6-2.8', '(a < b)', '5 cm < 2 inch' ], 'seealso': [ 'equal', 'unequal', 'larger', 'smallerEq', 'largerEq', 'compare' ] }; /***/ }), /* 270 */ /***/ (function(module, exports) { module.exports = { 'name': 'smallerEq', 'category': 'Relational', 'syntax': [ 'x <= y', 'smallerEq(x, y)' ], 'description': 'Check if value x is smaller or equal to value y. Returns true if x is smaller than y, and false if not.', 'examples': [ '2 < 1+1', '2 <= 1+1', 'a = 3.2', 'b = 6-2.8', '(a < b)' ], 'seealso': [ 'equal', 'unequal', 'larger', 'smaller', 'largerEq', 'compare' ] }; /***/ }), /* 271 */ /***/ (function(module, exports) { module.exports = { 'name': 'unequal', 'category': 'Relational', 'syntax': [ 'x != y', 'unequal(x, y)' ], 'description': 'Check unequality of two values. Returns true if the values are unequal, and false if they are equal.', 'examples': [ '2+2 != 3', '2+2 != 4', 'a = 3.2', 'b = 6-2.8', 'a != b', '50cm != 0.5m', '5 cm != 2 inch' ], 'seealso': [ 'equal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare', 'deepEqual' ] }; /***/ }), /* 272 */ /***/ (function(module, exports) { module.exports = { 'name': 'erf', 'category': 'Special', 'syntax': [ 'erf(x)' ], 'description': 'Compute the erf function of a value using a rational Chebyshev approximations for different intervals of x', 'examples': [ 'erf(0.2)', 'erf(-0.5)', 'erf(4)' ], 'seealso': [] }; /***/ }), /* 273 */ /***/ (function(module, exports) { module.exports = { 'name': 'mad', 'category': 'Statistics', 'syntax': [ 'mad(a, b, c, ...)', 'mad(A)' ], 'description': 'Compute the median absolute deviation of a matrix or a list with values. The median absolute deviation is defined as the median of the absolute deviations from the median.', 'examples': [ 'mad(10, 20, 30)', 'mad([1, 2, 3])', 'mad(10, 20, 30)' ], 'seealso': [ 'mean', 'median', 'std', 'abs' ] }; /***/ }), /* 274 */ /***/ (function(module, exports) { module.exports = { 'name': 'max', 'category': 'Statistics', 'syntax': [ 'max(a, b, c, ...)', 'max(A)', 'max(A, dim)' ], 'description': 'Compute the maximum value of a list of values.', 'examples': [ 'max(2, 3, 4, 1)', 'max([2, 3, 4, 1])', 'max([2, 5; 4, 3])', 'max([2, 5; 4, 3], 1)', 'max([2, 5; 4, 3], 2)', 'max(2.7, 7.1, -4.5, 2.0, 4.1)', 'min(2.7, 7.1, -4.5, 2.0, 4.1)' ], 'seealso': [ 'mean', 'median', 'min', 'prod', 'std', 'sum', 'var' ] }; /***/ }), /* 275 */ /***/ (function(module, exports) { module.exports = { 'name': 'mean', 'category': 'Statistics', 'syntax': [ 'mean(a, b, c, ...)', 'mean(A)', 'mean(A, dim)' ], 'description': 'Compute the arithmetic mean of a list of values.', 'examples': [ 'mean(2, 3, 4, 1)', 'mean([2, 3, 4, 1])', 'mean([2, 5; 4, 3])', 'mean([2, 5; 4, 3], 1)', 'mean([2, 5; 4, 3], 2)', 'mean([1.0, 2.7, 3.2, 4.0])' ], 'seealso': [ 'max', 'median', 'min', 'prod', 'std', 'sum', 'var' ] }; /***/ }), /* 276 */ /***/ (function(module, exports) { module.exports = { 'name': 'median', 'category': 'Statistics', 'syntax': [ 'median(a, b, c, ...)', 'median(A)' ], 'description': 'Compute the median of all values. The values are sorted and the middle value is returned. In case of an even number of values, the average of the two middle values is returned.', 'examples': [ 'median(5, 2, 7)', 'median([3, -1, 5, 7])' ], 'seealso': [ 'max', 'mean', 'min', 'prod', 'std', 'sum', 'var', 'quantileSeq' ] }; /***/ }), /* 277 */ /***/ (function(module, exports) { module.exports = { 'name': 'min', 'category': 'Statistics', 'syntax': [ 'min(a, b, c, ...)', 'min(A)', 'min(A, dim)' ], 'description': 'Compute the minimum value of a list of values.', 'examples': [ 'min(2, 3, 4, 1)', 'min([2, 3, 4, 1])', 'min([2, 5; 4, 3])', 'min([2, 5; 4, 3], 1)', 'min([2, 5; 4, 3], 2)', 'min(2.7, 7.1, -4.5, 2.0, 4.1)', 'max(2.7, 7.1, -4.5, 2.0, 4.1)' ], 'seealso': [ 'max', 'mean', 'median', 'prod', 'std', 'sum', 'var' ] }; /***/ }), /* 278 */ /***/ (function(module, exports) { module.exports = { 'name': 'mode', 'category': 'Statistics', 'syntax': [ 'mode(a, b, c, ...)', 'mode(A)', 'mode(A, a, b, B, c, ...)' ], 'description': 'Computes the mode of all values as an array. In case mode being more than one, multiple values are returned in an array.', 'examples': [ 'mode(5, 2, 7)', 'mode([3, -1, 5, 7])' ], 'seealso': [ 'max', 'mean', 'min', 'median', 'prod', 'std', 'sum', 'var' ] }; /***/ }), /* 279 */ /***/ (function(module, exports) { module.exports = { 'name': 'prod', 'category': 'Statistics', 'syntax': [ 'prod(a, b, c, ...)', 'prod(A)' ], 'description': 'Compute the product of all values.', 'examples': [ 'prod(2, 3, 4)', 'prod([2, 3, 4])', 'prod([2, 5; 4, 3])' ], 'seealso': [ 'max', 'mean', 'min', 'median', 'min', 'std', 'sum', 'var' ] }; /***/ }), /* 280 */ /***/ (function(module, exports) { module.exports = { 'name': 'quantileSeq', 'category': 'Statistics', 'syntax': [ 'quantileSeq(A, prob[, sorted])', 'quantileSeq(A, [prob1, prob2, ...][, sorted])', 'quantileSeq(A, N[, sorted])' ], 'description': 'Compute the prob order quantile of a matrix or a list with values. The sequence is sorted and the middle value is returned. Supported types of sequence values are: Number, BigNumber, Unit Supported types of probablity are: Number, BigNumber. \n\nIn case of a (multi dimensional) array or matrix, the prob order quantile of all elements will be calculated.', 'examples': [ 'quantileSeq([3, -1, 5, 7], 0.5)', 'quantileSeq([3, -1, 5, 7], [1/3, 2/3])', 'quantileSeq([3, -1, 5, 7], 2)', 'quantileSeq([-1, 3, 5, 7], 0.5, true)' ], 'seealso': [ 'mean', 'median', 'min', 'max', 'prod', 'std', 'sum', 'var' ] }; /***/ }), /* 281 */ /***/ (function(module, exports) { module.exports = { 'name': 'std', 'category': 'Statistics', 'syntax': [ 'std(a, b, c, ...)', 'std(A)', 'std(A, normalization)' ], 'description': 'Compute the standard deviation of all values, defined as std(A) = sqrt(var(A)). Optional parameter normalization can be "unbiased" (default), "uncorrected", or "biased".', 'examples': [ 'std(2, 4, 6)', 'std([2, 4, 6, 8])', 'std([2, 4, 6, 8], "uncorrected")', 'std([2, 4, 6, 8], "biased")', 'std([1, 2, 3; 4, 5, 6])' ], 'seealso': [ 'max', 'mean', 'min', 'median', 'min', 'prod', 'sum', 'var' ] }; /***/ }), /* 282 */ /***/ (function(module, exports) { module.exports = { 'name': 'sum', 'category': 'Statistics', 'syntax': [ 'sum(a, b, c, ...)', 'sum(A)' ], 'description': 'Compute the sum of all values.', 'examples': [ 'sum(2, 3, 4, 1)', 'sum([2, 3, 4, 1])', 'sum([2, 5; 4, 3])' ], 'seealso': [ 'max', 'mean', 'median', 'min', 'prod', 'std', 'sum', 'var' ] }; /***/ }), /* 283 */ /***/ (function(module, exports) { module.exports = { 'name': 'var', 'category': 'Statistics', 'syntax': [ 'var(a, b, c, ...)', 'var(A)', 'var(A, normalization)' ], 'description': 'Compute the variance of all values. Optional parameter normalization can be "unbiased" (default), "uncorrected", or "biased".', 'examples': [ 'var(2, 4, 6)', 'var([2, 4, 6, 8])', 'var([2, 4, 6, 8], "uncorrected")', 'var([2, 4, 6, 8], "biased")', 'var([1, 2, 3; 4, 5, 6])' ], 'seealso': [ 'max', 'mean', 'min', 'median', 'min', 'prod', 'std', 'sum' ] }; /***/ }), /* 284 */ /***/ (function(module, exports) { module.exports = { 'name': 'acos', 'category': 'Trigonometry', 'syntax': [ 'acos(x)' ], 'description': 'Compute the inverse cosine of a value in radians.', 'examples': [ 'acos(0.5)', 'acos(cos(2.3))' ], 'seealso': [ 'cos', 'atan', 'asin' ] }; /***/ }), /* 285 */ /***/ (function(module, exports) { module.exports = { 'name': 'acosh', 'category': 'Trigonometry', 'syntax': [ 'acosh(x)' ], 'description': 'Calculate the hyperbolic arccos of a value, defined as `acosh(x) = ln(sqrt(x^2 - 1) + x)`.', 'examples': [ 'acosh(1.5)' ], 'seealso': [ 'cosh', 'asinh', 'atanh' ] }; /***/ }), /* 286 */ /***/ (function(module, exports) { module.exports = { 'name': 'acot', 'category': 'Trigonometry', 'syntax': [ 'acot(x)' ], 'description': 'Calculate the inverse cotangent of a value.', 'examples': [ 'acot(0.5)', 'acot(cot(0.5))', 'acot(2)' ], 'seealso': [ 'cot', 'atan' ] }; /***/ }), /* 287 */ /***/ (function(module, exports) { module.exports = { 'name': 'acoth', 'category': 'Trigonometry', 'syntax': [ 'acoth(x)' ], 'description': 'Calculate the hyperbolic arccotangent of a value, defined as `acoth(x) = (ln((x+1)/x) + ln(x/(x-1))) / 2`.', 'examples': [ 'acoth(0.5)' ], 'seealso': [ 'acsch', 'asech' ] }; /***/ }), /* 288 */ /***/ (function(module, exports) { module.exports = { 'name': 'acsc', 'category': 'Trigonometry', 'syntax': [ 'acsc(x)' ], 'description': 'Calculate the inverse cotangent of a value.', 'examples': [ 'acsc(0.5)', 'acsc(csc(0.5))', 'acsc(2)' ], 'seealso': [ 'csc', 'asin', 'asec' ] }; /***/ }), /* 289 */ /***/ (function(module, exports) { module.exports = { 'name': 'acsch', 'category': 'Trigonometry', 'syntax': [ 'acsch(x)' ], 'description': 'Calculate the hyperbolic arccosecant of a value, defined as `acsch(x) = ln(1/x + sqrt(1/x^2 + 1))`.', 'examples': [ 'acsch(0.5)' ], 'seealso': [ 'asech', 'acoth' ] }; /***/ }), /* 290 */ /***/ (function(module, exports) { module.exports = { 'name': 'asec', 'category': 'Trigonometry', 'syntax': [ 'asec(x)' ], 'description': 'Calculate the inverse secant of a value.', 'examples': [ 'asec(0.5)', 'asec(sec(0.5))', 'asec(2)' ], 'seealso': [ 'acos', 'acot', 'acsc' ] }; /***/ }), /* 291 */ /***/ (function(module, exports) { module.exports = { 'name': 'asech', 'category': 'Trigonometry', 'syntax': [ 'asech(x)' ], 'description': 'Calculate the inverse secant of a value.', 'examples': [ 'asech(0.5)' ], 'seealso': [ 'acsch', 'acoth' ] }; /***/ }), /* 292 */ /***/ (function(module, exports) { module.exports = { 'name': 'asin', 'category': 'Trigonometry', 'syntax': [ 'asin(x)' ], 'description': 'Compute the inverse sine of a value in radians.', 'examples': [ 'asin(0.5)', 'asin(sin(2.3))' ], 'seealso': [ 'sin', 'acos', 'atan' ] }; /***/ }), /* 293 */ /***/ (function(module, exports) { module.exports = { 'name': 'asinh', 'category': 'Trigonometry', 'syntax': [ 'asinh(x)' ], 'description': 'Calculate the hyperbolic arcsine of a value, defined as `asinh(x) = ln(x + sqrt(x^2 + 1))`.', 'examples': [ 'asinh(0.5)' ], 'seealso': [ 'acosh', 'atanh' ] }; /***/ }), /* 294 */ /***/ (function(module, exports) { module.exports = { 'name': 'atan', 'category': 'Trigonometry', 'syntax': [ 'atan(x)' ], 'description': 'Compute the inverse tangent of a value in radians.', 'examples': [ 'atan(0.5)', 'atan(tan(2.3))' ], 'seealso': [ 'tan', 'acos', 'asin' ] }; /***/ }), /* 295 */ /***/ (function(module, exports) { module.exports = { 'name': 'atan2', 'category': 'Trigonometry', 'syntax': [ 'atan2(y, x)' ], 'description': 'Computes the principal value of the arc tangent of y/x in radians.', 'examples': [ 'atan2(2, 2) / pi', 'angle = 60 deg in rad', 'x = cos(angle)', 'y = sin(angle)', 'atan2(y, x)' ], 'seealso': [ 'sin', 'cos', 'tan' ] }; /***/ }), /* 296 */ /***/ (function(module, exports) { module.exports = { 'name': 'atanh', 'category': 'Trigonometry', 'syntax': [ 'atanh(x)' ], 'description': 'Calculate the hyperbolic arctangent of a value, defined as `atanh(x) = ln((1 + x)/(1 - x)) / 2`.', 'examples': [ 'atanh(0.5)' ], 'seealso': [ 'acosh', 'asinh' ] }; /***/ }), /* 297 */ /***/ (function(module, exports) { module.exports = { 'name': 'cos', 'category': 'Trigonometry', 'syntax': [ 'cos(x)' ], 'description': 'Compute the cosine of x in radians.', 'examples': [ 'cos(2)', 'cos(pi / 4) ^ 2', 'cos(180 deg)', 'cos(60 deg)', 'sin(0.2)^2 + cos(0.2)^2' ], 'seealso': [ 'acos', 'sin', 'tan' ] }; /***/ }), /* 298 */ /***/ (function(module, exports) { module.exports = { 'name': 'cosh', 'category': 'Trigonometry', 'syntax': [ 'cosh(x)' ], 'description': 'Compute the hyperbolic cosine of x in radians.', 'examples': [ 'cosh(0.5)' ], 'seealso': [ 'sinh', 'tanh', 'coth' ] }; /***/ }), /* 299 */ /***/ (function(module, exports) { module.exports = { 'name': 'cot', 'category': 'Trigonometry', 'syntax': [ 'cot(x)' ], 'description': 'Compute the cotangent of x in radians. Defined as 1/tan(x)', 'examples': [ 'cot(2)', '1 / tan(2)' ], 'seealso': [ 'sec', 'csc', 'tan' ] }; /***/ }), /* 300 */ /***/ (function(module, exports) { module.exports = { 'name': 'coth', 'category': 'Trigonometry', 'syntax': [ 'coth(x)' ], 'description': 'Compute the hyperbolic cotangent of x in radians.', 'examples': [ 'coth(2)', '1 / tanh(2)' ], 'seealso': [ 'sech', 'csch', 'tanh' ] }; /***/ }), /* 301 */ /***/ (function(module, exports) { module.exports = { 'name': 'csc', 'category': 'Trigonometry', 'syntax': [ 'csc(x)' ], 'description': 'Compute the cosecant of x in radians. Defined as 1/sin(x)', 'examples': [ 'csc(2)', '1 / sin(2)' ], 'seealso': [ 'sec', 'cot', 'sin' ] }; /***/ }), /* 302 */ /***/ (function(module, exports) { module.exports = { 'name': 'csch', 'category': 'Trigonometry', 'syntax': [ 'csch(x)' ], 'description': 'Compute the hyperbolic cosecant of x in radians. Defined as 1/sinh(x)', 'examples': [ 'csch(2)', '1 / sinh(2)' ], 'seealso': [ 'sech', 'coth', 'sinh' ] }; /***/ }), /* 303 */ /***/ (function(module, exports) { module.exports = { 'name': 'sec', 'category': 'Trigonometry', 'syntax': [ 'sec(x)' ], 'description': 'Compute the secant of x in radians. Defined as 1/cos(x)', 'examples': [ 'sec(2)', '1 / cos(2)' ], 'seealso': [ 'cot', 'csc', 'cos' ] }; /***/ }), /* 304 */ /***/ (function(module, exports) { module.exports = { 'name': 'sech', 'category': 'Trigonometry', 'syntax': [ 'sech(x)' ], 'description': 'Compute the hyperbolic secant of x in radians. Defined as 1/cosh(x)', 'examples': [ 'sech(2)', '1 / cosh(2)' ], 'seealso': [ 'coth', 'csch', 'cosh' ] }; /***/ }), /* 305 */ /***/ (function(module, exports) { module.exports = { 'name': 'sin', 'category': 'Trigonometry', 'syntax': [ 'sin(x)' ], 'description': 'Compute the sine of x in radians.', 'examples': [ 'sin(2)', 'sin(pi / 4) ^ 2', 'sin(90 deg)', 'sin(30 deg)', 'sin(0.2)^2 + cos(0.2)^2' ], 'seealso': [ 'asin', 'cos', 'tan' ] }; /***/ }), /* 306 */ /***/ (function(module, exports) { module.exports = { 'name': 'sinh', 'category': 'Trigonometry', 'syntax': [ 'sinh(x)' ], 'description': 'Compute the hyperbolic sine of x in radians.', 'examples': [ 'sinh(0.5)' ], 'seealso': [ 'cosh', 'tanh' ] }; /***/ }), /* 307 */ /***/ (function(module, exports) { module.exports = { 'name': 'tan', 'category': 'Trigonometry', 'syntax': [ 'tan(x)' ], 'description': 'Compute the tangent of x in radians.', 'examples': [ 'tan(0.5)', 'sin(0.5) / cos(0.5)', 'tan(pi / 4)', 'tan(45 deg)' ], 'seealso': [ 'atan', 'sin', 'cos' ] }; /***/ }), /* 308 */ /***/ (function(module, exports) { module.exports = { 'name': 'tanh', 'category': 'Trigonometry', 'syntax': [ 'tanh(x)' ], 'description': 'Compute the hyperbolic tangent of x in radians.', 'examples': [ 'tanh(0.5)', 'sinh(0.5) / cosh(0.5)' ], 'seealso': [ 'sinh', 'cosh' ] }; /***/ }), /* 309 */ /***/ (function(module, exports) { module.exports = { 'name': 'to', 'category': 'Units', 'syntax': [ 'x to unit', 'to(x, unit)' ], 'description': 'Change the unit of a value.', 'examples': [ '5 inch to cm', '3.2kg to g', '16 bytes in bits' ], 'seealso': [] }; /***/ }), /* 310 */ /***/ (function(module, exports) { module.exports = { 'name': 'clone', 'category': 'Utils', 'syntax': [ 'clone(x)' ], 'description': 'Clone a variable. Creates a copy of primitive variables,and a deep copy of matrices', 'examples': [ 'clone(3.5)', 'clone(2 - 4i)', 'clone(45 deg)', 'clone([1, 2; 3, 4])', 'clone("hello world")' ], 'seealso': [] }; /***/ }), /* 311 */ /***/ (function(module, exports) { module.exports = { 'name': 'format', 'category': 'Utils', 'syntax': [ 'format(value)', 'format(value, precision)' ], 'description': 'Format a value of any type as string.', 'examples': [ 'format(2.3)', 'format(3 - 4i)', 'format([])', 'format(pi, 3)' ], 'seealso': ['print'] }; /***/ }), /* 312 */ /***/ (function(module, exports) { module.exports = { 'name': 'isInteger', 'category': 'Utils', 'syntax': [ 'isInteger(x)' ], 'description': 'Test whether a value is an integer number.', 'examples': [ 'isInteger(2)', 'isInteger(3.5)', 'isInteger([3, 0.5, -2])' ], 'seealso': ['isNegative', 'isNumeric', 'isPositive', 'isZero'] }; /***/ }), /* 313 */ /***/ (function(module, exports) { module.exports = { 'name': 'isNaN', 'category': 'Utils', 'syntax': [ 'isNaN(x)' ], 'description': 'Test whether a value is NaN (not a number)', 'examples': [ 'isNaN(2)', 'isNaN(0 / 0)', 'isNaN(NaN)', 'isNaN(Infinity)' ], 'seealso': ['isNegative', 'isNumeric', 'isPositive', 'isZero'] }; /***/ }), /* 314 */ /***/ (function(module, exports) { module.exports = { 'name': 'isNegative', 'category': 'Utils', 'syntax': [ 'isNegative(x)' ], 'description': 'Test whether a value is negative: smaller than zero.', 'examples': [ 'isNegative(2)', 'isNegative(0)', 'isNegative(-4)', 'isNegative([3, 0.5, -2])' ], 'seealso': ['isInteger', 'isNumeric', 'isPositive', 'isZero'] }; /***/ }), /* 315 */ /***/ (function(module, exports) { module.exports = { 'name': 'isNumeric', 'category': 'Utils', 'syntax': [ 'isNumeric(x)' ], 'description': 'Test whether a value is a numeric value. ' + 'Returns true when the input is a number, BigNumber, Fraction, or boolean.', 'examples': [ 'isNumeric(2)', 'isNumeric(0)', 'isNumeric(bignumber(500))', 'isNumeric(fraction(0.125))', 'isNumeric("3")', 'isNumeric(2 + 3i)', 'isNumeric([2.3, "foo", false])' ], 'seealso': ['isInteger', 'isZero', 'isNegative', 'isPositive', 'isNaN'] }; /***/ }), /* 316 */ /***/ (function(module, exports) { module.exports = { 'name': 'isPositive', 'category': 'Utils', 'syntax': [ 'isPositive(x)' ], 'description': 'Test whether a value is positive: larger than zero.', 'examples': [ 'isPositive(2)', 'isPositive(0)', 'isPositive(-4)', 'isPositive([3, 0.5, -2])' ], 'seealso': ['isInteger', 'isNumeric', 'isNegative', 'isZero'] }; /***/ }), /* 317 */ /***/ (function(module, exports) { module.exports = { 'name': 'isPrime', 'category': 'Utils', 'syntax': [ 'isPrime(x)' ], 'description': 'Test whether a value is prime: has no divisors other than itself and one.', 'examples': [ 'isPrime(3)', 'isPrime(-2)', 'isPrime([2, 17, 100])' ], 'seealso': ['isInteger', 'isNumeric', 'isNegative', 'isZero'] }; /***/ }), /* 318 */ /***/ (function(module, exports) { module.exports = { 'name': 'isZero', 'category': 'Utils', 'syntax': [ 'isZero(x)' ], 'description': 'Test whether a value is zero.', 'examples': [ 'isZero(2)', 'isZero(0)', 'isZero(-4)', 'isZero([3, 0, -2, 0])' ], 'seealso': ['isInteger', 'isNumeric', 'isNegative', 'isPositive'] }; /***/ }), /* 319 */ /***/ (function(module, exports) { module.exports = { 'name': 'typeof', 'category': 'Utils', 'syntax': [ 'typeof(x)' ], 'description': 'Get the type of a variable.', 'examples': [ 'typeof(3.5)', 'typeof(2 - 4i)', 'typeof(45 deg)', 'typeof("hello world")' ], 'seealso': [] }; /***/ }), /* 320 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var parse = load(__webpack_require__(28)); /** * Parse and compile an expression. * Returns a an object with a function `eval([scope])` to evaluate the * compiled expression. * * Syntax: * * math.compile(expr) // returns one node * math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes * * Examples: * * var code = math.compile('sqrt(3^2 + 4^2)'); * code.eval(); // 5 * * var scope = {a: 3, b: 4} * var code = math.compile('a * b'); // 12 * code.eval(scope); // 12 * scope.a = 5; * code.eval(scope); // 20 * * var nodes = math.compile(['a = 3', 'b = 4', 'a * b']); * nodes[2].eval(); // 12 * * See also: * * parse, eval * * @param {string | string[] | Array | Matrix} expr * The expression to be compiled * @return {{eval: Function} | Array.<{eval: Function}>} code * An object with the compiled expression * @throws {Error} */ return typed('compile', { 'string': function (expr) { return parse(expr).compile(); }, 'Array | Matrix': function (expr) { return deepMap(expr, function (entry) { return parse(entry).compile(); }); } }); } exports.name = 'compile'; exports.factory = factory; /***/ }), /* 321 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var parse = load(__webpack_require__(28)); /** * Evaluate an expression. * * Syntax: * * math.eval(expr) * math.eval(expr, scope) * math.eval([expr1, expr2, expr3, ...]) * math.eval([expr1, expr2, expr3, ...], scope) * * Example: * * math.eval('(2+3)/4'); // 1.25 * math.eval('sqrt(3^2 + 4^2)'); // 5 * math.eval('sqrt(-4)'); // 2i * math.eval(['a=3', 'b=4', 'a*b']);, // [3, 4, 12] * * var scope = {a:3, b:4}; * math.eval('a * b', scope); // 12 * * See also: * * parse, compile * * @param {string | string[] | Matrix} expr The expression to be evaluated * @param {Object} [scope] Scope to read/write variables * @return {*} The result of the expression * @throws {Error} */ return typed('compile', { 'string': function (expr) { var scope = {}; return parse(expr).compile().eval(scope); }, 'string, Object': function (expr, scope) { return parse(expr).compile().eval(scope); }, 'Array | Matrix': function (expr) { var scope = {}; return deepMap(expr, function (entry) { return parse(entry).compile().eval(scope); }); }, 'Array | Matrix, Object': function (expr, scope) { return deepMap(expr, function (entry) { return parse(entry).compile().eval(scope); }); } }); } exports.name = 'eval'; exports.factory = factory; /***/ }), /* 322 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed, math) { var docs = load(__webpack_require__(81)); /** * Retrieve help on a function or data type. * Help files are retrieved from the documentation in math.expression.docs. * * Syntax: * * math.help(search) * * Examples: * * console.log(math.help('sin').toString()); * console.log(math.help(math.add).toString()); * console.log(math.help(math.add).toJSON()); * * @param {Function | string | Object} search A function or function name * for which to get help * @return {Help} A help object */ return typed('help', { 'any': function (search) { var prop; var name = search; if (typeof search !== 'string') { for (prop in math) { // search in functions and constants if (math.hasOwnProperty(prop) && (search === math[prop])) { name = prop; break; } } /* TODO: implement help for data types if (!text) { // search data type for (prop in math.type) { if (math.type.hasOwnProperty(prop)) { if (search === math.type[prop]) { text = prop; break; } } } } */ } var doc = docs[name]; if (!doc) { throw new Error('No documentation found on "' + name + '"'); } return new type.Help(doc); } }); } exports.math = true; // request access to the math namespace as 5th argument of the factory function exports.name = 'help'; exports.factory = factory; /***/ }), /* 323 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(320), __webpack_require__(321), __webpack_require__(322), __webpack_require__(324), __webpack_require__(82) ]; /***/ }), /* 324 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var parse = load(__webpack_require__(28)); /** * Parse an expression. Returns a node tree, which can be evaluated by * invoking node.eval(); * * Syntax: * * math.parse(expr) * math.parse(expr, options) * math.parse([expr1, expr2, expr3, ...]) * math.parse([expr1, expr2, expr3, ...], options) * * Example: * * var node = math.parse('sqrt(3^2 + 4^2)'); * node.compile().eval(); // 5 * * var scope = {a:3, b:4} * var node = math.parse('a * b'); // 12 * var code = node.compile(); * code.eval(scope); // 12 * scope.a = 5; * code.eval(scope); // 20 * * var nodes = math.parse(['a = 3', 'b = 4', 'a * b']); * nodes[2].compile().eval(); // 12 * * See also: * * eval, compile * * @param {string | string[] | Matrix} expr Expression to be parsed * @param {{nodes: Object}} [options] Available options: * - `nodes` a set of custom nodes * @return {Node | Node[]} node * @throws {Error} */ return typed('parse', { 'string | Array | Matrix': parse, 'string | Array | Matrix, Object': parse }); } exports.name = 'parse'; exports.factory = factory; /***/ }), /* 325 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(81), __webpack_require__(323), __webpack_require__(327), __webpack_require__(332), __webpack_require__(141), __webpack_require__(28), __webpack_require__(78) ]; /***/ }), /* 326 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { /** * @constructor UpdateNode */ function UpdateNode() { // TODO: deprecated since v3. Cleanup some day throw new Error('UpdateNode is deprecated. Use AssignmentNode instead.'); } return UpdateNode; } exports.name = 'UpdateNode'; exports.path = 'expression.node'; exports.factory = factory; /***/ }), /* 327 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(83), __webpack_require__(62), __webpack_require__(84), __webpack_require__(85), __webpack_require__(86), __webpack_require__(43), __webpack_require__(88), __webpack_require__(87), __webpack_require__(44), __webpack_require__(11), __webpack_require__(89), __webpack_require__(48), __webpack_require__(49), __webpack_require__(63), __webpack_require__(24), __webpack_require__(326) ]; /***/ }), /* 328 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var errorTransform = __webpack_require__(29).transform; function factory (type, config, load, typed) { var subset = load(__webpack_require__(51)); var matrix = load(__webpack_require__(1)); /** * Replace part of an object: * * - Assign a property to an object * - Replace a part of a string * - Replace a matrix subset * * @param {Object | Array | Matrix | string} object * @param {Index} index * @param {*} value * @return {Object | Array | Matrix | string} Returns the original object * except in case of a string */ return function assign(object, index, value) { try { if (Array.isArray(object)) { return matrix(object).subset(index, value).valueOf(); } else if (object && typeof object.subset === 'function') { // Matrix return object.subset(index, value); } else if (typeof object === 'string') { // TODO: move setStringSubset into a separate util file, use that return subset(object, index, value); } else if (typeof object === 'object') { if (!index.isObjectProperty()) { throw TypeError('Cannot apply a numeric index as object property'); } object[index.getObjectProperty()] = value; return object; } else { throw new TypeError('Cannot apply index: unsupported type of object'); } } catch (err) { throw errorTransform(err); } } } exports.factory = factory; /***/ }), /* 329 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var errorTransform = __webpack_require__(29).transform; /** * Attach a transform function to math.range * Adds a property transform containing the transform function. * * This transform changed the last `dim` parameter of function concat * from one-based to zero based */ function factory (type, config, load, typed) { var concat = load(__webpack_require__(105)); // @see: comment of concat itself return typed('concat', { '...any': function (args) { // change last argument from one-based to zero-based var lastIndex = args.length - 1; var last = args[lastIndex]; if (typeof last === 'number') { args[lastIndex] = last - 1; } else if (last && last.isBigNumber === true) { args[lastIndex] = last.minus(1); } try { return concat.apply(null, args); } catch (err) { throw errorTransform(err); } } }); } exports.name = 'concat'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 330 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Attach a transform function to math.filter * Adds a property transform containing the transform function. * * This transform adds support for equations as test function for math.filter, * so you can do something like 'filter([3, -2, 5], x > 0)'. */ function factory (type, config, load, typed) { var filter = load(__webpack_require__(107)); var SymbolNode = load(__webpack_require__(24)); function filterTransform(args, math, scope) { var x, test; if (args[0]) { x = args[0].compile().eval(scope); } if (args[1]) { if (args[1] && args[1].isSymbolNode) { // a function pointer, like filter([3, -2, 5], myTestFunction); test = args[1].compile().eval(scope); } else { // an equation like filter([3, -2, 5], x > 0) // find an undefined symbol var _scope = scope || {}; var symbol = args[1] .filter(function (node) { return (node && node.isSymbolNode) && !(node.name in math) && !(node.name in _scope); })[0]; // create a test function for this equation var sub = Object.create(_scope); var eq = args[1].compile(); if (symbol) { var name = symbol.name; test = function (x) { sub[name] = x; return eq.eval(sub); } } else { throw new Error('No undefined variable found in filter equation'); } } } return filter(x, test); } filterTransform.rawArgs = true; return filterTransform; } exports.name = 'filter'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 331 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var maxArgumentCount = __webpack_require__(33).maxArgumentCount; /** * Attach a transform function to math.forEach * Adds a property transform containing the transform function. * * This transform creates a one-based index instead of a zero-based index */ function factory (type, config, load, typed) { var forEach = load(__webpack_require__(108)); return typed('forEach', { 'Array | Matrix, function': function (array, callback) { // figure out what number of arguments the callback function expects var args = maxArgumentCount(callback); var recurse = function (value, index) { if (Array.isArray(value)) { value.forEach(function (child, i) { // we create a copy of the index array and append the new index value recurse(child, index.concat(i + 1)); // one based index, hence i+1 }); } else { // invoke the callback function with the right number of arguments if (args === 1) { callback(value); } else if (args === 2) { callback(value, index); } else { // 3 or -1 callback(value, index, array); } } }; recurse(array.valueOf(), []); // pass Array } }); } exports.name = 'forEach'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 332 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(329), __webpack_require__(330), __webpack_require__(331), __webpack_require__(333), __webpack_require__(334), __webpack_require__(335), __webpack_require__(336), __webpack_require__(337), __webpack_require__(338), __webpack_require__(339) ]; /***/ }), /* 333 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Attach a transform function to math.index * Adds a property transform containing the transform function. * * This transform creates a one-based index instead of a zero-based index */ function factory (type, config, load) { return function indexTransform() { var args = []; for (var i = 0, ii = arguments.length; i < ii; i++) { var arg = arguments[i]; // change from one-based to zero based, and convert BigNumber to number if (arg && arg.isRange === true) { arg.start--; arg.end -= (arg.step > 0 ? 0 : 2); } else if (arg && arg.isSet === true) { arg = arg.map(function (v) { return v - 1; }); } else if (arg && (arg.isArray === true || arg.isMatrix)) { arg = arg.map(function (v) { return v - 1; }); } else if (typeof arg === 'number') { arg--; } else if (arg && arg.isBigNumber === true) { arg = arg.toNumber() - 1; } else if (typeof arg === 'string') { // leave as is } else { throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range'); } args[i] = arg; } var res = new type.Index(); type.Index.apply(res, args); return res; }; } exports.name = 'index'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 334 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var maxArgumentCount = __webpack_require__(33).maxArgumentCount; /** * Attach a transform function to math.map * Adds a property transform containing the transform function. * * This transform creates a one-based index instead of a zero-based index */ function factory (type, config, load, typed) { var map = load(__webpack_require__(67)); var matrix = load(__webpack_require__(1)); return typed('max', { 'Array, function': function (x, callback) { return _map(x, callback, x); }, 'Matrix, function': function (x, callback) { return matrix(_map(x.valueOf(), callback, x)); } }); } /** * Map for a multi dimensional array. One-based indexes * @param {Array} array * @param {function} callback * @param {Array} orig * @return {Array} * @private */ function _map (array, callback, orig) { // figure out what number of arguments the callback function expects var args = maxArgumentCount(callback); function recurse(value, index) { if (Array.isArray(value)) { return value.map(function (child, i) { // we create a copy of the index array and append the new index value return recurse(child, index.concat(i + 1)); // one based index, hence i + 1 }); } else { // invoke the callback function with the right number of arguments if (args === 1) { return callback(value); } else if (args === 2) { return callback(value, index); } else { // 3 or -1 return callback(value, index, orig); } } } return recurse(array, []); } exports.name = 'map'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 335 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var errorTransform = __webpack_require__(29).transform; var isCollection = __webpack_require__(40); /** * Attach a transform function to math.max * Adds a property transform containing the transform function. * * This transform changed the last `dim` parameter of function max * from one-based to zero based */ function factory (type, config, load, typed) { var max = load(__webpack_require__(114)); return typed('max', { '...any': function (args) { // change last argument dim from one-based to zero-based if (args.length == 2 && isCollection(args[0])) { var dim = args[1]; if (typeof dim === 'number') { args[1] = dim - 1; } else if (dim && dim.isBigNumber === true) { args[1] = dim.minus(1); } } try { return max.apply(null, args); } catch (err) { throw errorTransform(err); } } }); } exports.name = 'max'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 336 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var errorTransform = __webpack_require__(29).transform; var isCollection = __webpack_require__(40); /** * Attach a transform function to math.mean * Adds a property transform containing the transform function. * * This transform changed the last `dim` parameter of function mean * from one-based to zero based */ function factory (type, config, load, typed) { var mean = load(__webpack_require__(115)); return typed('mean', { '...any': function (args) { // change last argument dim from one-based to zero-based if (args.length == 2 && isCollection(args[0])) { var dim = args[1]; if (typeof dim === 'number') { args[1] = dim - 1; } else if (dim && dim.isBigNumber === true) { args[1] = dim.minus(1); } } try { return mean.apply(null, args); } catch (err) { throw errorTransform(err); } } }); } exports.name = 'mean'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 337 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var errorTransform = __webpack_require__(29).transform; var isCollection = __webpack_require__(40); /** * Attach a transform function to math.min * Adds a property transform containing the transform function. * * This transform changed the last `dim` parameter of function min * from one-based to zero based */ function factory (type, config, load, typed) { var min = load(__webpack_require__(117)); return typed('min', { '...any': function (args) { // change last argument dim from one-based to zero-based if (args.length == 2 && isCollection(args[0])) { var dim = args[1]; if (typeof dim === 'number') { args[1] = dim - 1; } else if (dim && dim.isBigNumber === true) { args[1] = dim.minus(1); } } try { return min.apply(null, args); } catch (err) { throw errorTransform(err); } } }); } exports.name = 'min'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 338 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Attach a transform function to math.range * Adds a property transform containing the transform function. * * This transform creates a range which includes the end value */ function factory (type, config, load, typed) { var range = load(__webpack_require__(110)); return typed('range', { '...any': function (args) { var lastIndex = args.length - 1; var last = args[lastIndex]; if (typeof last !== 'boolean') { // append a parameter includeEnd=true args.push(true); } return range.apply(null, args); } }); } exports.name = 'range'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 339 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var errorTransform = __webpack_require__(29).transform; /** * Attach a transform function to math.subset * Adds a property transform containing the transform function. * * This transform creates a range which includes the end value */ function factory (type, config, load, typed) { var subset = load(__webpack_require__(51)); return typed('subset', { '...any': function (args) { try { return subset.apply(null, args); } catch (err) { throw errorTransform(err); } } }); } exports.name = 'subset'; exports.path = 'expression.transform'; exports.factory = factory; /***/ }), /* 340 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var parse = load(__webpack_require__(28)); var simplify = load(__webpack_require__(93)); var ConstantNode = load(__webpack_require__(43)); var FunctionNode = load(__webpack_require__(44)); var OperatorNode = load(__webpack_require__(48)); var ParenthesisNode = load(__webpack_require__(49)); var SymbolNode = load(__webpack_require__(24)); /** * Takes the derivative of an expression expressed in parser Nodes. * The derivative will be taken over the supplied variable in the * second parameter. If there are multiple variables in the expression, * it will return a partial derivative. * * This uses rules of differentiation which can be found here: * * - [Differentiation rules (Wikipedia)](http://en.wikipedia.org/wiki/Differentiation_rules) * * Syntax: * * derivative(expr, variable) * derivative(expr, variable, options) * * Examples: * * math.derivative('x^2', 'x'); // Node {2 * x} * math.derivative('x^2', 'x', {simplify: false}); // Node {2 * 1 * x ^ (2 - 1) * math.derivative('sin(2x)', 'x')); // Node {2 * cos(2 * x)} * math.derivative('2*x', 'x').eval(); // number 2 * math.derivative('x^2', 'x').eval({x: 4}); // number 8 * var f = math.parse('x^2'); * var x = math.parse('x'); * math.derivative(f, x); // Node {2 * x} * * See also: * * simplify, parse, eval * * @param {Node | string} expr The expression to differentiate * @param {SymbolNode | string} variable The variable over which to differentiate * @param {{simplify: boolean}} [options] * There is one option available, `simplify`, which * is true by default. When false, output will not * be simplified. * @return {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} The derivative of `expr` */ var derivative = typed('derivative', { 'Node, SymbolNode, Object': function (expr, variable, options) { var constNodes = {}; constTag(constNodes, expr, variable.name); var res = _derivative(expr, constNodes); return options.simplify ? simplify(res) : res; }, 'Node, SymbolNode': function (expr, variable) { return derivative(expr, variable, {simplify: true}) }, 'string, SymbolNode': function (expr, variable) { return derivative(parse(expr), variable) }, 'string, SymbolNode, Object': function (expr, variable, options) { return derivative(parse(expr), variable, options) }, 'string, string': function (expr, variable) { return derivative(parse(expr), parse(variable)) }, 'string, string, Object': function (expr, variable, options) { return derivative(parse(expr), parse(variable), options) }, 'Node, string': function (expr, variable) { return derivative(expr, parse(variable)) }, 'Node, string, Object': function (expr, variable, options) { return derivative(expr, parse(variable), options) } // TODO: replace the 8 signatures above with 4 as soon as typed-function supports optional arguments /* TODO: implement and test syntax with order of derivatives -> implement as an option {order: number} 'Node, SymbolNode, ConstantNode': function (expr, variable, {order}) { var res = expr; for (var i = 0; i < order; i++) { var constNodes = {}; constTag(constNodes, expr, variable.name); res = _derivative(res, constNodes); } return res; } */ }); derivative._simplify = true derivative.toTex = function(deriv) { return _derivTex.apply(null, deriv.args); } var _derivTex = typed('_derivTex', { 'Node, SymbolNode': function (expr, x) { return _derivTex(expr.toString(), x.toString(), 1); }, 'Node, SymbolNode, ConstantNode': function (expr, x, order) { return _derivTex(expr.toString(), x.name, order.value); }, 'string, string, number': function (expr, x, order) { var d; if (order === 1) { d = "{d\\over d" + x + "}"; } else { d = "{d^{" + order + "}\\over d" + x + "^{" + order + "}}"; } return d + "\\left[" + expr + "\\right]" } }); /** * Does a depth-first search on the expression tree to identify what Nodes * are constants (e.g. 2 + 2), and stores the ones that are constants in * constNodes. Classification is done as follows: * * 1. ConstantNodes are constants. * 2. If there exists a SymbolNode, of which we are differentiating over, * in the subtree it is not constant. * * @param {Object} constNodes Holds the nodes that are constant * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node * @param {string} varName Variable that we are differentiating * @return {boolean} if node is constant */ // TODO: can we rewrite constTag into a pure function? var constTag = typed('constTag', { 'Object, ConstantNode, string': function (constNodes, node) { return constNodes[node] = true; }, 'Object, SymbolNode, string': function (constNodes, node, varName) { // Treat other variables like constants. For reasoning, see: // https://en.wikipedia.org/wiki/Partial_derivative if (node.name != varName) { return constNodes[node] = true; } return false; }, 'Object, ParenthesisNode, string': function (constNodes, node, varName) { return constTag(constNodes, node.content, varName); }, 'Object, FunctionAssignmentNode, string': function (constNodes, node, varName) { if (node.params.indexOf(varName) == -1) { return constNodes[node] = true; } return constTag(constNodes, node.expr, varName); }, 'Object, FunctionNode | OperatorNode, string': function (constNodes, node, varName) { if (node.args.length != 0) { var isConst = constTag(constNodes, node.args[0], varName); for (var i = 1; i < node.args.length; ++i) { isConst = constTag(constNodes, node.args[i], varName) && isConst; } if (isConst) { return constNodes[node] = true; } } return false; } }); /** * Applies differentiation rules. * * @param {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} node * @param {Object} constNodes Holds the nodes that are constant * @return {ConstantNode | SymbolNode | ParenthesisNode | FunctionNode | OperatorNode} The derivative of `expr` */ var _derivative = typed('_derivative', { 'ConstantNode, Object': function (node) { return new ConstantNode('0', node.valueType); }, 'SymbolNode, Object': function (node, constNodes) { if (constNodes[node] !== undefined) { return new ConstantNode('0', config.number); } return new ConstantNode('1', config.number); }, 'ParenthesisNode, Object': function (node, constNodes) { return new ParenthesisNode(_derivative(node.content, constNodes)); }, 'FunctionAssignmentNode, Object': function (node, constNodes) { if (constNodes[node] !== undefined) { return new ConstantNode('0', config.number); } return _derivative(node.expr, constNodes); }, 'FunctionNode, Object': function (node, constNodes) { if (node.args.length != 1) { funcArgsCheck(node); } if (constNodes[node] !== undefined) { return new ConstantNode('0', config.number); } var arg1 = node.args[0]; var arg2; var div = false; // is output a fraction? var negative = false; // is output negative? var funcDerivative; switch (node.name) { case 'cbrt': // d/dx(cbrt(x)) = 1 / (3x^(2/3)) div = true; funcDerivative = new OperatorNode('*', 'multiply', [ new ConstantNode('3', config.number), new OperatorNode('^', 'pow', [ arg1, new OperatorNode('/', 'divide', [ new ConstantNode('2', config.number), new ConstantNode('3', config.number) ]) ]) ]); break; case 'sqrt': case 'nthRoot': // d/dx(sqrt(x)) = 1 / (2*sqrt(x)) if (node.args.length == 1) { div = true; funcDerivative = new OperatorNode('*', 'multiply', [ new ConstantNode('2', config.number), new FunctionNode('sqrt', [arg1]) ]); break; } // Rearrange from nthRoot(x, a) -> x^(1/a) arg2 = new OperatorNode('/', 'divide', [ new ConstantNode('1', config.number), node.args[1] ]); // Is a variable? constNodes[arg2] = constNodes[node.args[1]]; return _derivative(new OperatorNode('^', 'pow', [arg1, arg2]), constNodes); case 'log10': arg2 = new ConstantNode('10', config.number); case 'log': if (!arg2 && node.args.length == 1) { // d/dx(log(x)) = 1 / x funcDerivative = arg1.clone(); } else if (arg2 || constNodes[node.args[1]] !== undefined) { // d/dx(log(x, c)) = 1 / (x*ln(c)) funcDerivative = new OperatorNode('*', 'multiply', [ arg1.clone(), new FunctionNode('log', [arg2 || node.args[1]]) ]); } else { // d/dx(log(f(x), g(x))) = d/dx(log(f(x)) / log(g(x))) return _derivative(new OperatorNode('/', 'divide', [ new FunctionNode('log', [arg1]), new FunctionNode('log', [node.args[1]]) ]), constNodes); } div = true; break; case 'exp': // d/dx(e^x) = e^x funcDerivative = new FunctionNode('exp', [arg1.clone()]); break; case 'sin': // d/dx(sin(x)) = cos(x) funcDerivative = new FunctionNode('cos', [arg1.clone()]); break; case 'cos': // d/dx(cos(x)) = -sin(x) funcDerivative = new OperatorNode('-', 'unaryMinus', [ new FunctionNode('sin', [arg1.clone()]) ]); break; case 'tan': // d/dx(tan(x)) = sec(x)^2 funcDerivative = new OperatorNode('^', 'pow', [ new FunctionNode('sec', [arg1.clone()]), new ConstantNode('2', config.number) ]); break; case 'sec': // d/dx(sec(x)) = sec(x)tan(x) funcDerivative = new OperatorNode('*', 'multiply', [ node, new FunctionNode('tan', [arg1.clone()]) ]); break; case 'csc': // d/dx(csc(x)) = -csc(x)cot(x) negative = true; funcDerivative = new OperatorNode('*', 'multiply', [ node, new FunctionNode('cot', [arg1.clone()]) ]); break; case 'cot': // d/dx(cot(x)) = -csc(x)^2 negative = true; funcDerivative = new OperatorNode('^', 'pow', [ new FunctionNode('csc', [arg1.clone()]), new ConstantNode('2', config.number) ]); break; case 'asin': // d/dx(asin(x)) = 1 / sqrt(1 - x^2) div = true; funcDerivative = new FunctionNode('sqrt', [ new OperatorNode('-', 'subtract', [ new ConstantNode('1', config.number), new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]) ]) ]); break; case 'acos': // d/dx(acos(x)) = -1 / sqrt(1 - x^2) div = true; negative = true; funcDerivative = new FunctionNode('sqrt', [ new OperatorNode('-', 'subtract', [ new ConstantNode('1', config.number), new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]) ]) ]); break; case 'atan': // d/dx(atan(x)) = 1 / (x^2 + 1) div = true; funcDerivative = new OperatorNode('+', 'add', [ new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]), new ConstantNode('1', config.number) ]); break; case 'asec': // d/dx(asec(x)) = 1 / (|x|*sqrt(x^2 - 1)) div = true; funcDerivative = new OperatorNode('*', 'multiply', [ new FunctionNode('abs', [arg1.clone()]), new FunctionNode('sqrt', [ new OperatorNode('-', 'subtract', [ new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]), new ConstantNode('1', config.number) ]) ]) ]); break; case 'acsc': // d/dx(acsc(x)) = -1 / (|x|*sqrt(x^2 - 1)) div = true; negative = true; funcDerivative = new OperatorNode('*', 'multiply', [ new FunctionNode('abs', [arg1.clone()]), new FunctionNode('sqrt', [ new OperatorNode('-', 'subtract', [ new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]), new ConstantNode('1', config.number) ]) ]) ]); break; case 'acot': // d/dx(acot(x)) = -1 / (x^2 + 1) div = true; negative = true; funcDerivative = new OperatorNode('+', 'add', [ new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]), new ConstantNode('1', config.number) ]); break; case 'sinh': // d/dx(sinh(x)) = cosh(x) funcDerivative = new FunctionNode('cosh', [arg1.clone()]); break; case 'cosh': // d/dx(cosh(x)) = sinh(x) funcDerivative = new FunctionNode('sinh', [arg1.clone()]); break; case 'tanh': // d/dx(tanh(x)) = sech(x)^2 funcDerivative = new OperatorNode('^', 'pow', [ new FunctionNode('sech', [arg1.clone()]), new ConstantNode('2', config.number) ]); break; case 'sech': // d/dx(sech(x)) = -sech(x)tanh(x) negative = true; funcDerivative = new OperatorNode('*', 'multiply', [ node, new FunctionNode('tanh', [arg1.clone()]) ]); break; case 'csch': // d/dx(csch(x)) = -csch(x)coth(x) negative = true; funcDerivative = new OperatorNode('*', 'multiply', [ node, new FunctionNode('coth', [arg1.clone()]) ]); break; case 'coth': // d/dx(coth(x)) = -csch(x)^2 negative = true; funcDerivative = new OperatorNode('^', 'pow', [ new FunctionNode('csch', [arg1.clone()]), new ConstantNode('2', config.number) ]); break; case 'asinh': // d/dx(asinh(x)) = 1 / sqrt(x^2 + 1) div = true; funcDerivative = new FunctionNode('sqrt', [ new OperatorNode('+', 'add', [ new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]), new ConstantNode('1', config.number) ]) ]); break; case 'acosh': // d/dx(acosh(x)) = 1 / sqrt(x^2 - 1); XXX potentially only for x >= 1 (the real spectrum) div = true; funcDerivative = new FunctionNode('sqrt', [ new OperatorNode('-', 'subtract', [ new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]), new ConstantNode('1', config.number), ]) ]); break; case 'atanh': // d/dx(atanh(x)) = 1 / (1 - x^2) div = true; funcDerivative = new OperatorNode('-', 'subtract', [ new ConstantNode('1', config.number), new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]) ]); break; case 'asech': // d/dx(asech(x)) = -1 / (x*sqrt(1 - x^2)) div = true; negative = true; funcDerivative = new OperatorNode('*', 'multiply', [ arg1.clone(), new FunctionNode('sqrt', [ new OperatorNode('-', 'subtract', [ new ConstantNode('1', config.number), new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]) ]) ]) ]); break; case 'acsch': // d/dx(acsch(x)) = -1 / (|x|*sqrt(x^2 + 1)) div = true; negative = true; funcDerivative = new OperatorNode('*', 'multiply', [ new FunctionNode('abs', [arg1.clone()]), new FunctionNode('sqrt', [ new OperatorNode('+', 'add', [ new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]), new ConstantNode('1', config.number) ]) ]) ]); break; case 'acoth': // d/dx(acoth(x)) = -1 / (1 - x^2) div = true; negative = true; funcDerivative = new OperatorNode('-', 'subtract', [ new ConstantNode('1', config.number), new OperatorNode('^', 'pow', [ arg1.clone(), new ConstantNode('2', config.number) ]) ]); break; case 'gamma': // Needs digamma function, d/dx(gamma(x)) = gamma(x)digamma(x) default: throw new Error('Function "' + node.name + '" not supported by derivative'); } var op, func; if (div) { op = '/'; func = 'divide'; } else { op = '*'; func = 'multiply'; } /* Apply chain rule to all functions: F(x) = f(g(x)) F'(x) = g'(x)*f'(g(x)) */ var chainDerivative = _derivative(arg1, constNodes); if (negative) { chainDerivative = new OperatorNode('-', 'unaryMinus', [chainDerivative]); } return new OperatorNode(op, func, [chainDerivative, funcDerivative]); }, 'OperatorNode, Object': function (node, constNodes) { if (constNodes[node] !== undefined) { return new ConstantNode('0', config.number); } var arg1 = node.args[0]; var arg2 = node.args[1]; switch (node.op) { case '+': case '-': // d/dx(+/-f(x)) = +/-f'(x) if (node.args.length == 1) { return new OperatorNode(node.op, node.fn, [_derivative(arg1, constNodes)]); } // Linearity of differentiation, d/dx(f(x) +/- g(x)) = f'(x) +/- g'(x) return new OperatorNode(node.op, node.fn, [ _derivative(arg1, constNodes), _derivative(arg2, constNodes) ]); case '*': // d/dx(c*f(x)) = c*f'(x) if (constNodes[arg1] !== undefined || constNodes[arg2] !== undefined) { var newArgs = (constNodes[arg1] !== undefined) ? [arg1.clone(), _derivative(arg2, constNodes)] : [arg2.clone(), _derivative(arg1, constNodes)]; return new OperatorNode('*', 'multiply', newArgs); } // Product Rule, d/dx(f(x)*g(x)) = f'(x)*g(x) + f(x)*g'(x) return new OperatorNode('+', 'add', [ new OperatorNode('*', 'multiply', [_derivative(arg1, constNodes), arg2.clone()]), new OperatorNode('*', 'multiply', [arg1.clone(), _derivative(arg2, constNodes)]) ]); case '/': // d/dx(f(x) / c) = f'(x) / c if (constNodes[arg2] !== undefined) { return new OperatorNode('/', 'divide', [_derivative(arg1, constNodes), arg2]); } // Reciprocal Rule, d/dx(c / f(x)) = -c(f'(x)/f(x)^2) if (constNodes[arg1] !== undefined) { return new OperatorNode('*', 'multiply', [ new OperatorNode('-', 'unaryMinus', [arg1]), new OperatorNode('/', 'divide', [ _derivative(arg2, constNodes), new OperatorNode('^', 'pow', [arg2.clone(), new ConstantNode('2', config.number)]) ]) ]); } // Quotient rule, d/dx(f(x) / g(x)) = (f'(x)g(x) - f(x)g'(x)) / g(x)^2 return new OperatorNode('/', 'divide', [ new OperatorNode('-', 'subtract', [ new OperatorNode('*', 'multiply', [_derivative(arg1, constNodes), arg2.clone()]), new OperatorNode('*', 'multiply', [arg1.clone(), _derivative(arg2, constNodes)]) ]), new OperatorNode('^', 'pow', [arg2.clone(), new ConstantNode('2', config.number)]) ]); case '^': if (constNodes[arg1] !== undefined) { // If is secretly constant; 0^f(x) = 1 (in JS), 1^f(x) = 1 if (arg1.isConstantNode && (arg1.value == '0' || arg1.value == '1')) { return new ConstantNode('0', config.number); } // d/dx(c^f(x)) = c^f(x)*ln(c)*f'(x) return new OperatorNode('*', 'multiply', [ node, new OperatorNode('*', 'multiply', [ new FunctionNode('log', [arg1.clone()]), _derivative(arg2.clone(), constNodes) ]) ]); } if (constNodes[arg2] !== undefined) { if (arg2.isConstantNode) { var expValue = arg2.value; // If is secretly constant; f(x)^0 = 1 -> d/dx(1) = 0 if (expValue == '0') { return new ConstantNode('0', config.number); } // Ignore exponent; f(x)^1 = f(x) if (expValue == '1') { return _derivative(arg1, constNodes); } } // Elementary Power Rule, d/dx(f(x)^c) = c*f'(x)*f(x)^(c-1) var powMinusOne = new OperatorNode('^', 'pow', [ arg1.clone(), new OperatorNode('-', 'subtract', [ arg2, new ConstantNode('1', config.number) ]) ]); return new OperatorNode('*', 'multiply', [ arg2.clone(), new OperatorNode('*', 'multiply', [ _derivative(arg1, constNodes), powMinusOne ]), ]); } // Functional Power Rule, d/dx(f^g) = f^g*[f'*(g/f) + g'ln(f)] return new OperatorNode('*', 'multiply', [ new OperatorNode('^', 'pow', [arg1.clone(), arg2.clone()]), new OperatorNode('+', 'add', [ new OperatorNode('*', 'multiply', [ _derivative(arg1, constNodes), new OperatorNode('/', 'divide', [arg2.clone(), arg1.clone()]) ]), new OperatorNode('*', 'multiply', [ _derivative(arg2, constNodes), new FunctionNode('log', [arg1.clone()]) ]) ]) ]); case '%': case 'mod': default: throw new Error('Operator "' + node.op + '" not supported by derivative'); } } }); /** * Ensures the number of arguments for a function are correct, * and will throw an error otherwise. * * @param {FunctionNode} node */ function funcArgsCheck(node) { //TODO add min, max etc if ((node.name == 'log' || node.name == 'nthRoot') && node.args.length == 2) { return; } // There should be an incorrect number of arguments if we reach here // Change all args to constants to avoid unidentified // symbol error when compiling function for (var i = 0; i < node.args.length; ++i) { node.args[i] = new ConstantNode(0); } node.compile().eval(); throw new Error('Expected TypeError, but none found'); } return derivative; } exports.name = 'derivative'; exports.factory = factory; /***/ }), /* 341 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(340), // simplify __webpack_require__(93), // decomposition __webpack_require__(91), __webpack_require__(92), // solver __webpack_require__(95), __webpack_require__(343), __webpack_require__(96) ]; /***/ }), /* 342 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var digits = __webpack_require__(2).digits; // TODO this could be improved by simplifying seperated constants under associative and commutative operators function factory(type, config, load, typed, math) { var util = load(__webpack_require__(94)); var isCommutative = util.isCommutative; var isAssociative = util.isAssociative; var allChildren = util.allChildren; var createMakeNodeFunction = util.createMakeNodeFunction; var ConstantNode = math.expression.node.ConstantNode; var OperatorNode = math.expression.node.OperatorNode; function simplifyConstant(expr) { var res = foldFraction(expr); return res.isNode ? res : _toNode(res); } function _eval(fnname, args) { try { return _toNumber(math[fnname].apply(null, args)); } catch (ignore) { // sometimes the implicit type conversion causes the evaluation to fail, so we'll try again using just numbers args = args.map(function(x){ return x.valueOf(); }); return _toNumber(math[fnname].apply(null, args)); } } var _toNode = typed({ 'Fraction': _fractionToNode, 'number': _numberToNode, 'BigNumber': function(s) { return _numberToNode(s._toNumber()); }, 'Complex': function(s) { throw 'Cannot convert Complex number to Node'; } }); var _toNumber = typed({ 'Fraction': function(s) { return s; }, 'BigNumber': function(s) { if (s.decimalPlaces() <= 15) { return math.fraction(s.toNumber()) } return s.toNumber(); }, 'number': function(s) { if (digits(s) <= 15) { return math.fraction(s); } return s; }, 'Complex': function(s) { if (s.im !== 0) { return s; } if (digits(s.re) <= 15) { return math.fraction(s.re); } return s.re; }, }); function _numberToNode(n) { if (n < 0) { return new OperatorNode('-', 'unaryMinus', [new ConstantNode(-n)]) } return new ConstantNode(n); } function _fractionToNode(f) { var n; var vn = f.s*f.n; if (vn < 0) { n = new OperatorNode('-', 'unaryMinus', [new ConstantNode(-vn)]) } else { n = new ConstantNode(vn); } if (f.d === 1) { return n; } return new OperatorNode('/', 'divide', [n, new ConstantNode(f.d)]); } /* * Create a binary tree from a list of Fractions and Nodes. * Tries to fold Fractions by evaluating them until the first Node in the list is hit, so * `args` should be sorted to have the Fractions at the start (if the operator is commutative). * @param args - list of Fractions and Nodes * @param fn - evaluator for the binary operation evaluator that accepts two Fractions * @param makeNode - creates a binary OperatorNode/FunctionNode from a list of child Nodes * if args.length is 1, returns args[0] * @return - Either a Node representing a binary expression or Fraction */ function foldOp(fn, args, makeNode) { return args.reduce(function(a, b) { if (!a.isNode && !b.isNode) { try { return _eval(fn, [a,b]); } catch (ignoreandcontinue) {} a = _toNode(a); b = _toNode(b); } else if (!a.isNode) { a = _toNode(a); } else if (!b.isNode) { b = _toNode(b); } return makeNode([a, b]); }); } // destroys the original node and returns a folded one function foldFraction(node) { switch(node.type) { case 'SymbolNode': return node; case 'ConstantNode': return _toNumber(node.value); case 'FunctionNode': if (math[node.name] && math[node.name].rawArgs) { return node; } /* falls through */ case 'OperatorNode': var fn = node.fn.toString(); var args; var res; var makeNode = createMakeNodeFunction(node); if (node.args.length === 1) { args = [foldFraction(node.args[0])]; if (!args[0].isNode) { res = _eval(fn, args); } else { res = makeNode(args); } } else if (isAssociative(node)) { args = allChildren(node); args = args.map(foldFraction); if (isCommutative(fn)) { // commutative binary operator var consts = [], vars = []; for (var i=0; i < args.length; i++) { if (!args[i].isNode) { consts.push(args[i]); } else { vars.push(args[i]); } } if (consts.length > 1) { res = foldOp(fn, consts, makeNode); vars.unshift(res); res = foldOp(fn, vars, makeNode); } else { // we won't change the children order since it's not neccessary res = foldOp(fn, args, makeNode); } } else { // non-commutative binary operator res = foldOp(fn, args, makeNode); } } else { // non-associative binary operator args = node.args.map(foldFraction); res = foldOp(fn, args, makeNode); } return res; case 'ParenthesisNode': // remove the uneccessary parenthesis return foldFraction(node.content); case 'AccessorNode': /* falls through */ case 'ArrayNode': /* falls through */ case 'AssignmentNode': /* falls through */ case 'BlockNode': /* falls through */ case 'FunctionAssignmentNode': /* falls through */ case 'IndexNode': /* falls through */ case 'ObjectNode': /* falls through */ case 'RangeNode': /* falls through */ case 'UpdateNode': /* falls through */ case 'ConditionalNode': /* falls through */ default: throw 'Unimplemented node type in simplifyConstant: '+node.type; } } return simplifyConstant; } exports.math = true; exports.name = 'simplifyConstant'; exports.path = 'algebra.simplify'; exports.factory = factory; /***/ }), /* 343 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isArray = Array.isArray; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var lup = load(__webpack_require__(91)); var slu = load(__webpack_require__(92)); var cs_ipvec = load(__webpack_require__(349)); var solveValidation = load(__webpack_require__(64)); var usolve = load(__webpack_require__(96)); var lsolve = load(__webpack_require__(95)); /** * Solves the linear system `A * x = b` where `A` is an [n x n] matrix and `b` is a [n] column vector. * * Syntax: * * math.lusolve(A, b) // returns column vector with the solution to the linear system A * x = b * math.lusolve(lup, b) // returns column vector with the solution to the linear system A * x = b, lup = math.lup(A) * * Examples: * * var m = [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]; * * var x = math.lusolve(m, [-1, -1, -1, -1]); // x = [[-1], [-0.5], [-1/3], [-0.25]] * * var f = math.lup(m); * var x1 = math.lusolve(f, [-1, -1, -1, -1]); // x1 = [[-1], [-0.5], [-1/3], [-0.25]] * var x2 = math.lusolve(f, [1, 2, 1, -1]); // x2 = [[1], [1], [1/3], [-0.25]] * * var a = [[-2, 3], [2, 1]]; * var b = [11, 9]; * var x = lusolve(a, b); // [[-5.5], [20]] * * See also: * * lup, slu, lsolve, usolve * * @param {Matrix | Array | Object} A Invertible Matrix or the Matrix LU decomposition * @param {Matrix | Array} b Column Vector * @param {number} [order] The Symbolic Ordering and Analysis order, see slu for details. Matrix must be a SparseMatrix * @param {Number} [threshold] Partial pivoting threshold (1 for partial pivoting), see slu for details. Matrix must be a SparseMatrix. * * @return {DenseMatrix | Array} Column vector with the solution to the linear system A * x = b */ var lusolve = typed('lusolve', { 'Array, Array | Matrix': function (a, b) { // convert a to matrix a = matrix(a); // matrix lup decomposition var d = lup(a); // solve var x = _lusolve(d.L, d.U, d.p, null, b); // convert result to array return x.valueOf(); }, 'DenseMatrix, Array | Matrix': function (a, b) { // matrix lup decomposition var d = lup(a); // solve return _lusolve(d.L, d.U, d.p, null, b); }, 'SparseMatrix, Array | Matrix': function (a, b) { // matrix lup decomposition var d = lup(a); // solve return _lusolve(d.L, d.U, d.p, null, b); }, 'SparseMatrix, Array | Matrix, number, number': function (a, b, order, threshold) { // matrix lu decomposition var d = slu(a, order, threshold); // solve return _lusolve(d.L, d.U, d.p, d.q, b); }, 'Object, Array | Matrix': function (d, b) { // solve return _lusolve(d.L, d.U, d.p, d.q, b); } }); var _toMatrix = function (a) { // check it is a matrix if (a && a.isMatrix === true) return a; // check array if (isArray(a)) return matrix(a); // throw throw new TypeError('Invalid Matrix LU decomposition'); }; var _lusolve = function (l, u, p, q, b) { // verify L, U, P l = _toMatrix(l); u = _toMatrix(u); // validate matrix and vector b = solveValidation(l, b, false); // apply row permutations if needed (b is a DenseMatrix) if (p) b._data = cs_ipvec(p, b._data); // use forward substitution to resolve L * y = b var y = lsolve(l, b); // use backward substitution to resolve U * x = y var x = usolve(u, y); // apply column permutations if needed (x is a DenseMatrix) if (q) x._data = cs_ipvec(q, x._data); // return solution return x; }; return lusolve; } exports.name = 'lusolve'; exports.factory = factory; /***/ }), /* 344 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var cs_flip = load(__webpack_require__(65)); var cs_fkeep = load(__webpack_require__(348)); var cs_tdfs = load(__webpack_require__(99)); var add = load(__webpack_require__(15)); var multiply = load(__webpack_require__(10)); var transpose = load(__webpack_require__(52)); /** * Approximate minimum degree ordering. The minimum degree algorithm is a widely used * heuristic for finding a permutation P so that P*A*P' has fewer nonzeros in its factorization * than A. It is a gready method that selects the sparsest pivot row and column during the course * of a right looking sparse Cholesky factorization. * * Reference: http://faculty.cse.tamu.edu/davis/publications.html * * @param {Number} order 0: Natural, 1: Cholesky, 2: LU, 3: QR * @param {Matrix} m Sparse Matrix * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_amd = function (order, a) { // check input parameters if (!a || order <= 0 || order > 3) return null; // a matrix arrays var asize = a._size; // rows and columns var m = asize[0]; var n = asize[1]; // initialize vars var lemax = 0; // dense threshold var dense = Math.max(16, 10 * Math.sqrt(n)); dense = Math.min(n - 2, dense); // create target matrix C var cm = _createTargetMatrix(order, a, m, n, dense); // drop diagonal entries cs_fkeep(cm, _diag, null); // C matrix arrays var cindex = cm._index; var cptr = cm._ptr; // number of nonzero elements in C var cnz = cptr[n]; // allocate result (n+1) var P = []; // create workspace (8 * (n + 1)) var W = []; var len = 0; // first n + 1 entries var nv = n + 1; // next n + 1 entries var next = 2 * (n + 1); // next n + 1 entries var head = 3 * (n + 1); // next n + 1 entries var elen = 4 * (n + 1); // next n + 1 entries var degree = 5 * (n + 1); // next n + 1 entries var w = 6 * (n + 1); // next n + 1 entries var hhead = 7 * (n + 1); // last n + 1 entries // use P as workspace for last var last = P; // initialize quotient graph var mark = _initializeQuotientGraph(n, cptr, W, len, head, last, next, hhead, nv, w, elen, degree); // initialize degree lists var nel = _initializeDegreeLists(n, cptr, W, degree, elen, w, dense, nv, head, last, next); // minimum degree node var mindeg = 0; // vars var i, j, k, k1, k2, e, pj, ln, nvi, pk, eln, p1, p2, pn, h, d; // while (selecting pivots) do while (nel < n) { // select node of minimum approximate degree. amd() is now ready to start eliminating the graph. It first // finds a node k of minimum degree and removes it from its degree list. The variable nel keeps track of thow // many nodes have been eliminated. for (k = -1; mindeg < n && (k = W[head + mindeg]) == -1; mindeg++); if (W[next + k] != -1) last[W[next + k]] = -1; // remove k from degree list W[head + mindeg] = W[next + k]; // elenk = |Ek| var elenk = W[elen + k]; // # of nodes k represents var nvk = W[nv + k]; // W[nv + k] nodes of A eliminated nel += nvk; // Construct a new element. The new element Lk is constructed in place if |Ek| = 0. nv[i] is // negated for all nodes i in Lk to flag them as members of this set. Each node i is removed from the // degree lists. All elements e in Ek are absorved into element k. var dk = 0; // flag k as in Lk W[nv + k] = -nvk; var p = cptr[k]; // do in place if W[elen + k] == 0 var pk1 = (elenk === 0) ? p : cnz; var pk2 = pk1; for (k1 = 1; k1 <= elenk + 1; k1++) { if (k1 > elenk) { // search the nodes in k e = k; // list of nodes starts at cindex[pj] pj = p; // length of list of nodes in k ln = W[len + k] - elenk; } else { // search the nodes in e e = cindex[p++]; pj = cptr[e]; // length of list of nodes in e ln = W[len + e]; } for (k2 = 1; k2 <= ln; k2++) { i = cindex[pj++]; // check node i dead, or seen if ((nvi = W[nv + i]) <= 0) continue; // W[degree + Lk] += size of node i dk += nvi; // negate W[nv + i] to denote i in Lk W[nv + i] = -nvi; // place i in Lk cindex[pk2++] = i; if (W[next + i] != -1) last[W[next + i]] = last[i]; // check we need to remove i from degree list if (last[i] != -1) W[next + last[i]] = W[next + i]; else W[head + W[degree + i]] = W[next + i]; } if (e != k) { // absorb e into k cptr[e] = cs_flip(k); // e is now a dead element W[w + e] = 0; } } // cindex[cnz...nzmax] is free if (elenk !== 0) cnz = pk2; // external degree of k - |Lk\i| W[degree + k] = dk; // element k is in cindex[pk1..pk2-1] cptr[k] = pk1; W[len + k] = pk2 - pk1; // k is now an element W[elen + k] = -2; // Find set differences. The scan1 function now computes the set differences |Le \ Lk| for all elements e. At the start of the // scan, no entry in the w array is greater than or equal to mark. // clear w if necessary mark = _wclear(mark, lemax, W, w, n); // scan 1: find |Le\Lk| for (pk = pk1; pk < pk2; pk++) { i = cindex[pk]; // check if W[elen + i] empty, skip it if ((eln = W[elen + i]) <= 0) continue; // W[nv + i] was negated nvi = -W[nv + i]; var wnvi = mark - nvi; // scan Ei for (p = cptr[i], p1 = cptr[i] + eln - 1; p <= p1; p++) { e = cindex[p]; if (W[w + e] >= mark) { // decrement |Le\Lk| W[w + e] -= nvi; } else if (W[w + e] !== 0) { // ensure e is a live element, 1st time e seen in scan 1 W[w + e] = W[degree + e] + wnvi; } } } // degree update // The second pass computes the approximate degree di, prunes the sets Ei and Ai, and computes a hash // function h(i) for all nodes in Lk. // scan2: degree update for (pk = pk1; pk < pk2; pk++) { // consider node i in Lk i = cindex[pk]; p1 = cptr[i]; p2 = p1 + W[elen + i] - 1; pn = p1; // scan Ei for (h = 0, d = 0, p = p1; p <= p2; p++) { e = cindex[p]; // check e is an unabsorbed element if (W[w + e] !== 0) { // dext = |Le\Lk| var dext = W[w + e] - mark; if (dext > 0) { // sum up the set differences d += dext; // keep e in Ei cindex[pn++] = e; // compute the hash of node i h += e; } else { // aggressive absorb. e->k cptr[e] = cs_flip(k); // e is a dead element W[w + e] = 0; } } } // W[elen + i] = |Ei| W[elen + i] = pn - p1 + 1; var p3 = pn; var p4 = p1 + W[len + i]; // prune edges in Ai for (p = p2 + 1; p < p4; p++) { j = cindex[p]; // check node j dead or in Lk var nvj = W[nv + j]; if (nvj <= 0) continue; // degree(i) += |j| d += nvj; // place j in node list of i cindex[pn++] = j; // compute hash for node i h += j; } // check for mass elimination if (d === 0) { // absorb i into k cptr[i] = cs_flip(k); nvi = -W[nv + i]; // |Lk| -= |i| dk -= nvi; // |k| += W[nv + i] nvk += nvi; nel += nvi; W[nv + i] = 0; // node i is dead W[elen + i] = -1; } else { // update degree(i) W[degree + i] = Math.min(W[degree + i], d); // move first node to end cindex[pn] = cindex[p3]; // move 1st el. to end of Ei cindex[p3] = cindex[p1]; // add k as 1st element in of Ei cindex[p1] = k; // new len of adj. list of node i W[len + i] = pn - p1 + 1; // finalize hash of i h = (h < 0 ? -h : h) % n; // place i in hash bucket W[next + i] = W[hhead + h]; W[hhead + h] = i; // save hash of i in last[i] last[i] = h; } } // finalize |Lk| W[degree + k] = dk; lemax = Math.max(lemax, dk); // clear w mark = _wclear(mark + lemax, lemax, W, w, n); // Supernode detection. Supernode detection relies on the hash function h(i) computed for each node i. // If two nodes have identical adjacency lists, their hash functions wil be identical. for (pk = pk1; pk < pk2; pk++) { i = cindex[pk]; // check i is dead, skip it if (W[nv + i] >= 0) continue; // scan hash bucket of node i h = last[i]; i = W[hhead + h]; // hash bucket will be empty W[hhead + h] = -1; for (; i != -1 && W[next + i] != -1; i = W[next + i], mark++) { ln = W[len + i]; eln = W[elen + i]; for (p = cptr[i] + 1; p <= cptr[i] + ln - 1; p++) W[w + cindex[p]] = mark; var jlast = i; // compare i with all j for (j = W[next + i]; j != -1; ) { var ok = W[len + j] === ln && W[elen + j] === eln; for (p = cptr[j] + 1; ok && p <= cptr[j] + ln - 1; p++) { // compare i and j if (W[w + cindex[p]] != mark) ok = 0; } // check i and j are identical if (ok) { // absorb j into i cptr[j] = cs_flip(i); W[nv + i] += W[nv + j]; W[nv + j] = 0; // node j is dead W[elen + j] = -1; // delete j from hash bucket j = W[next + j]; W[next + jlast] = j; } else { // j and i are different jlast = j; j = W[next + j]; } } } } // Finalize new element. The elimination of node k is nearly complete. All nodes i in Lk are scanned one last time. // Node i is removed from Lk if it is dead. The flagged status of nv[i] is cleared. for (p = pk1, pk = pk1; pk < pk2; pk++) { i = cindex[pk]; // check i is dead, skip it if ((nvi = -W[nv + i]) <= 0) continue; // restore W[nv + i] W[nv + i] = nvi; // compute external degree(i) d = W[degree + i] + dk - nvi; d = Math.min(d, n - nel - nvi); if (W[head + d] != -1) last[W[head + d]] = i; // put i back in degree list W[next + i] = W[head + d]; last[i] = -1; W[head + d] = i; // find new minimum degree mindeg = Math.min(mindeg, d); W[degree + i] = d; // place i in Lk cindex[p++] = i; } // # nodes absorbed into k W[nv + k] = nvk; // length of adj list of element k if ((W[len + k] = p - pk1) === 0) { // k is a root of the tree cptr[k] = -1; // k is now a dead element W[w + k] = 0; } if (elenk !== 0) { // free unused space in Lk cnz = p; } } // Postordering. The elimination is complete, but no permutation has been computed. All that is left // of the graph is the assembly tree (ptr) and a set of dead nodes and elements (i is a dead node if // nv[i] is zero and a dead element if nv[i] > 0). It is from this information only that the final permutation // is computed. The tree is restored by unflipping all of ptr. // fix assembly tree for (i = 0; i < n; i++) cptr[i] = cs_flip(cptr[i]); for (j = 0; j <= n; j++) W[head + j] = -1; // place unordered nodes in lists for (j = n; j >= 0; j--) { // skip if j is an element if (W[nv + j] > 0) continue; // place j in list of its parent W[next + j] = W[head + cptr[j]]; W[head + cptr[j]] = j; } // place elements in lists for (e = n; e >= 0; e--) { // skip unless e is an element if (W[nv + e] <= 0) continue; if (cptr[e] != -1) { // place e in list of its parent W[next + e] = W[head + cptr[e]]; W[head + cptr[e]] = e; } } // postorder the assembly tree for (k = 0, i = 0; i <= n; i++) { if (cptr[i] == -1) k = cs_tdfs(i, k, W, head, next, P, w); } // remove last item in array P.splice(P.length - 1, 1); // return P return P; }; /** * Creates the matrix that will be used by the approximate minimum degree ordering algorithm. The function accepts the matrix M as input and returns a permutation * vector P. The amd algorithm operates on a symmetrix matrix, so one of three symmetric matrices is formed. * * Order: 0 * A natural ordering P=null matrix is returned. * * Order: 1 * Matrix must be square. This is appropriate for a Cholesky or LU factorization. * P = M + M' * * Order: 2 * Dense columns from M' are dropped, M recreated from M'. This is appropriatefor LU factorization of unsymmetric matrices. * P = M' * M * * Order: 3 * This is best used for QR factorization or LU factorization is matrix M has no dense rows. A dense row is a row with more than 10*sqr(columns) entries. * P = M' * M */ var _createTargetMatrix = function (order, a, m, n, dense) { // compute A' var at = transpose(a); // check order = 1, matrix must be square if (order === 1 && n === m) { // C = A + A' return add(a, at); } // check order = 2, drop dense columns from M' if (order == 2) { // transpose arrays var tindex = at._index; var tptr = at._ptr; // new column index var p2 = 0; // loop A' columns (rows) for (var j = 0; j < m; j++) { // column j of AT starts here var p = tptr[j]; // new column j starts here tptr[j] = p2; // skip dense col j if (tptr[j + 1] - p > dense) continue; // map rows in column j of A for (var p1 = tptr[j + 1]; p < p1; p++) tindex[p2++] = tindex[p]; } // finalize AT tptr[m] = p2; // recreate A from new transpose matrix a = transpose(at); // use A' * A return multiply(at, a); } // use A' * A, square or rectangular matrix return multiply(at, a); }; /** * Initialize quotient graph. There are four kind of nodes and elements that must be represented: * * - A live node is a node i (or a supernode) that has not been selected as a pivot nad has not been merged into another supernode. * - A dead node i is one that has been removed from the graph, having been absorved into r = flip(ptr[i]). * - A live element e is one that is in the graph, having been formed when node e was selected as the pivot. * - A dead element e is one that has benn absorved into a subsequent element s = flip(ptr[e]). */ var _initializeQuotientGraph = function (n, cptr, W, len, head, last, next, hhead, nv, w, elen, degree) { // Initialize quotient graph for (var k = 0; k < n; k++) W[len + k] = cptr[k + 1] - cptr[k]; W[len + n] = 0; // initialize workspace for (var i = 0; i <= n; i++) { // degree list i is empty W[head + i] = -1; last[i] = -1; W[next + i] = -1; // hash list i is empty W[hhead + i] = -1; // node i is just one node W[nv + i] = 1; // node i is alive W[w + i] = 1; // Ek of node i is empty W[elen + i] = 0; // degree of node i W[degree + i] = W[len + i]; } // clear w var mark = _wclear(0, 0, W, w, n); // n is a dead element W[elen + n] = -2; // n is a root of assembly tree cptr[n] = -1; // n is a dead element W[w + n] = 0; // return mark return mark; }; /** * Initialize degree lists. Each node is placed in its degree lists. Nodes of zero degree are eliminated immediately. Nodes with * degree >= dense are alsol eliminated and merged into a placeholder node n, a dead element. Thes nodes will appera last in the * output permutation p. */ var _initializeDegreeLists = function (n, cptr, W, degree, elen, w, dense, nv, head, last, next) { // result var nel = 0; // loop columns for (var i = 0; i < n; i++) { // degree @ i var d = W[degree + i]; // check node i is empty if (d === 0) { // element i is dead W[elen + i] = -2; nel++; // i is a root of assembly tree cptr[i] = -1; W[w + i] = 0; } else if (d > dense) { // absorb i into element n W[nv + i] = 0; // node i is dead W[elen + i] = -1; nel++; cptr[i] = cs_flip(n); W[nv + n]++; } else { var h = W[head + d]; if (h != -1) last[h] = i; // put node i in degree list d W[next + i] = W[head + d]; W[head + d] = i; } } return nel; }; var _wclear = function(mark, lemax, W, w, n) { if (mark < 2 || (mark + lemax < 0)) { for (var k = 0; k < n; k++) { if (W[w + k] !== 0) W[w + k] = 1; } mark = 2 ; } // at this point, W [0..n-1] < mark holds return mark; }; var _diag = function (i, j) { return i != j; }; return cs_amd; } exports.name = 'cs_amd'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 345 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var transpose = load(__webpack_require__(52)); var cs_leaf = load(__webpack_require__(350)); /** * Computes the column counts using the upper triangular part of A. * It transposes A internally, none of the input parameters are modified. * * @param {Matrix} a The sparse matrix A * * @param {Matrix} ata Count the columns of A'A instead * * @return An array of size n of the column counts or null on error * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_counts = function (a, parent, post, ata) { // check inputs if (!a || !parent || !post) return null; // a matrix arrays var asize = a._size; // rows and columns var m = asize[0]; var n = asize[1]; // variables var i, j, k, J, p, p0, p1; // workspace size var s = 4 * n + (ata ? (n + m + 1) : 0); // allocate workspace var w = []; // (s) var ancestor = 0; // first n entries var maxfirst = n; // next n entries var prevleaf = 2 * n; // next n entries var first = 3 * n; // next n entries var head = 4 * n; // next n + 1 entries (used when ata is true) var next = 5 * n + 1; // last entries in workspace // clear workspace w[0..s-1] for (k = 0; k < s; k++) w[k] = -1; // allocate result var colcount = []; // (n); // AT = A' var at = transpose(a); // at arrays var tindex = at._index; var tptr = at._ptr; // find w[first + j] for (k = 0; k < n; k++) { j = post[k]; // colcount[j]=1 if j is a leaf colcount[j] = (w[first + j] == -1) ? 1 : 0; for (; j != -1 && w[first + j] == -1; j = parent[j]) w[first + j] = k; } // initialize ata if needed if (ata) { // invert post for (k = 0; k < n; k++) w[post[k]] = k; // loop rows (columns in AT) for (i = 0; i < m; i++) { // values in column i of AT for (k = n, p0 = tptr[i], p1 = tptr[i + 1], p = p0; p < p1; p++) k = Math.min(k, w[tindex[p]]); // place row i in linked list k w[next + i] = w[head + k]; w[head + k] = i; } } // each node in its own set for (i = 0; i < n; i++) w[ancestor + i] = i; for (k = 0; k < n; k++) { // j is the kth node in postordered etree j = post[k]; // check j is not a root if (parent[j] != -1) colcount[parent[j]]--; // J=j for LL'=A case for (J = (ata ? w[head + k] : j); J != -1; J = (ata ? w[next + J] : -1)) { for (p = tptr[J]; p < tptr[J+1]; p++) { i = tindex[p]; var r = cs_leaf(i, j, w, first, maxfirst, prevleaf, ancestor); // check A(i,j) is in skeleton if (r.jleaf >= 1) colcount[j]++; // check account for overlap in q if (r.jleaf == 2) colcount[r.q]--; } } if (parent[j] != -1) w[ancestor + j] = parent[j]; } // sum up colcount's of each child for (j = 0; j < n; j++) { if (parent[j] != -1) colcount[parent[j]] += colcount[j]; } return colcount; }; return cs_counts; } exports.name = 'cs_counts'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 346 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var cs_marked = load(__webpack_require__(98)); var cs_mark = load(__webpack_require__(97)); var cs_unflip = load(__webpack_require__(357)); /** * Depth-first search computes the nonzero pattern xi of the directed graph G (Matrix) starting * at nodes in B (see cs_reach()). * * @param {Number} j The starting node for the DFS algorithm * @param {Matrix} g The G matrix to search, ptr array modified, then restored * @param {Number} top Start index in stack xi[top..n-1] * @param {Number} k The kth column in B * @param {Array} xi The nonzero pattern xi[top] .. xi[n - 1], an array of size = 2 * n * The first n entries is the nonzero pattern, the last n entries is the stack * @param {Array} pinv The inverse row permutation vector, must be null for L * x = b * * @return {Number} New value of top * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_dfs = function (j, g, top, xi, pinv) { // g arrays var index = g._index; var ptr = g._ptr; var size = g._size; // columns var n = size[1]; // vars var i, p, p2; // initialize head var head = 0; // initialize the recursion stack xi[0] = j; // loop while (head >= 0) { // get j from the top of the recursion stack j = xi[head]; // apply permutation vector var jnew = pinv ? pinv[j] : j; // check node j is marked if (!cs_marked(ptr, j)) { // mark node j as visited cs_mark(ptr, j); // update stack (last n entries in xi) xi[n + head] = jnew < 0 ? 0 : cs_unflip(ptr[jnew]); } // node j done if no unvisited neighbors var done = 1; // examine all neighbors of j, stack (last n entries in xi) for (p = xi[n + head], p2 = jnew < 0 ? 0 : cs_unflip(ptr[jnew+1]); p < p2; p++) { // consider neighbor node i i = index[p]; // check we have visited node i, skip it if (cs_marked(ptr, i)) continue; // pause depth-first search of node j, update stack (last n entries in xi) xi[n + head] = p; // start dfs at node i xi[++head] = i; // node j is not done done = 0; // break, to start dfs(i) break; } // check depth-first search at node j is done if (done) { // remove j from the recursion stack head--; // and place in the output stack xi[--top] = j; } } return top; }; return cs_dfs; } exports.name = 'cs_dfs'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 347 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory () { /** * Computes the elimination tree of Matrix A (using triu(A)) or the * elimination tree of A'A without forming A'A. * * @param {Matrix} a The A Matrix * @param {boolean} ata A value of true the function computes the etree of A'A * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_etree = function (a, ata) { // check inputs if (!a) return null; // a arrays var aindex = a._index; var aptr = a._ptr; var asize = a._size; // rows & columns var m = asize[0]; var n = asize[1]; // allocate result var parent = []; // (n) // allocate workspace var w = []; // (n + (ata ? m : 0)) var ancestor = 0; // first n entries in w var prev = n; // last m entries (ata = true) var i, inext; // check we are calculating A'A if (ata) { // initialize workspace for (i = 0; i < m; i++) w[prev + i] = -1; } // loop columns for (var k = 0; k < n; k++) { // node k has no parent yet parent[k] = -1; // nor does k have an ancestor w[ancestor + k] = -1; // values in column k for (var p0 = aptr[k], p1 = aptr[k + 1], p = p0; p < p1; p++) { // row var r = aindex[p]; // node i = ata ? (w[prev + r]) : r; // traverse from i to k for (; i != -1 && i < k; i = inext) { // inext = ancestor of i inext = w[ancestor + i]; // path compression w[ancestor + i] = k; // check no anc., parent is k if (inext == -1) parent[i] = k; } if (ata) w[prev + r] = k; } } return parent; }; return cs_etree; } exports.name = 'cs_etree'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 348 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory () { /** * Keeps entries in the matrix when the callback function returns true, removes the entry otherwise * * @param {Matrix} a The sparse matrix * @param {function} callback The callback function, function will be invoked with the following args: * - The entry row * - The entry column * - The entry value * - The state parameter * @param {any} other The state * * @return The number of nonzero elements in the matrix * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_fkeep = function (a, callback, other) { // a arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var asize = a._size; // columns var n = asize[1]; // nonzero items var nz = 0; // loop columns for (var j = 0; j < n; j++) { // get current location of col j var p = aptr[j]; // record new location of col j aptr[j] = nz; for (; p < aptr[j+1]; p++) { // check we need to keep this item if (callback(aindex[p], j, avalues ? avalues[p] : 1, other)) { // keep A(i,j) aindex[nz] = aindex[p]; // check we need to process values (pattern only) if (avalues) avalues[nz] = avalues[p]; // increment nonzero items nz++; } } } // finalize A aptr[n] = nz; // trim arrays aindex.splice(nz, aindex.length - nz); // check we need to process values (pattern only) if (avalues) avalues.splice(nz, avalues.length - nz); // return number of nonzero items return (nz); }; return cs_fkeep; } exports.name = 'cs_fkeep'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 349 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory () { /** * Permutes a vector; x = P'b. In MATLAB notation, x(p)=b. * * @param {Array} p The permutation vector of length n. null value denotes identity * @param {Array} b The input vector * * @return {Array} The output vector x = P'b */ var cs_ipvec = function (p, b, n) { // vars var k; var n = b.length; var x = []; // check permutation vector was provided, p = null denotes identity if (p) { // loop vector for (k = 0; k < n; k++) { // apply permutation x[p[k]] = b[k]; } } else { // loop vector for (k = 0; k < n; k++) { // x[i] = b[i] x[k] = b[k]; } } return x; }; return cs_ipvec; } exports.name = 'cs_ipvec'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 350 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory () { /** * This function determines if j is a leaf of the ith row subtree. * Consider A(i,j), node j in ith row subtree and return lca(jprev,j) * * @param {Number} i The ith row subtree * @param {Number} j The node to test * @param {Array} w The workspace array * @param {Number} first The index offset within the workspace for the first array * @param {Number} maxfirst The index offset within the workspace for the maxfirst array * @param {Number} prevleaf The index offset within the workspace for the prevleaf array * @param {Number} ancestor The index offset within the workspace for the ancestor array * * @return {Object} * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_leaf = function (i, j, w, first, maxfirst, prevleaf, ancestor) { var s, sparent, jprev; // our result var jleaf = 0; var q; // check j is a leaf if (i <= j || w[first + j] <= w[maxfirst + i]) return (-1); // update max first[j] seen so far w[maxfirst + i] = w[first + j]; // jprev = previous leaf of ith subtree jprev = w[prevleaf + i]; w[prevleaf + i] = j; // check j is first or subsequent leaf if (jprev === -1) { // 1st leaf, q = root of ith subtree jleaf = 1; q = i; } else { // update jleaf jleaf = 2; // q = least common ancester (jprev,j) for (q = jprev; q != w[ancestor + q]; q = w[ancestor + q]); for (s = jprev; s != q; s = sparent) { // path compression sparent = w[ancestor + s]; w[ancestor + s] = q; } } return { jleaf: jleaf, q: q }; }; return cs_leaf; } exports.name = 'cs_leaf'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 351 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var abs = load(__webpack_require__(25)); var divideScalar = load(__webpack_require__(16)); var multiply = load(__webpack_require__(10)); var larger = load(__webpack_require__(26)); var largerEq = load(__webpack_require__(113)); var cs_spsolve = load(__webpack_require__(355)); var SparseMatrix = type.SparseMatrix; /** * Computes the numeric LU factorization of the sparse matrix A. Implements a Left-looking LU factorization * algorithm that computes L and U one column at a tume. At the kth step, it access columns 1 to k-1 of L * and column k of A. Given the fill-reducing column ordering q (see parameter s) computes L, U and pinv so * L * U = A(p, q), where p is the inverse of pinv. * * @param {Matrix} m The A Matrix to factorize * @param {Object} s The symbolic analysis from cs_sqr(). Provides the fill-reducing * column ordering q * @param {Number} tol Partial pivoting threshold (1 for partial pivoting) * * @return {Number} The numeric LU factorization of A or null * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_lu = function (m, s, tol) { // validate input if (!m) return null; // m arrays var size = m._size; // columns var n = size[1]; // symbolic analysis result var q; var lnz = 100; var unz = 100; // update symbolic analysis parameters if (s) { q = s.q; lnz = s.lnz || lnz; unz = s.unz || unz; } // L arrays var lvalues = []; // (lnz) var lindex = []; // (lnz); var lptr = []; // (n + 1); // L var L = new SparseMatrix({ values: lvalues, index: lindex, ptr: lptr, size: [n, n] }); // U arrays var uvalues = []; // (unz); var uindex = []; // (unz); var uptr = []; // (n + 1); // U var U = new SparseMatrix({ values: uvalues, index: uindex, ptr: uptr, size: [n, n] }); // inverse of permutation vector var pinv = []; // (n); // vars var i, p; // allocate arrays var x = []; // (n); var xi = []; // (2 * n); // initialize variables for (i = 0; i < n; i++) { // clear workspace x[i] = 0; // no rows pivotal yet pinv[i] = -1; // no cols of L yet lptr[i + 1] = 0; } // reset number of nonzero elements in L and U lnz = 0; unz = 0; // compute L(:,k) and U(:,k) for (var k = 0; k < n; k++) { // update ptr lptr[k] = lnz; uptr[k] = unz; // apply column permutations if needed var col = q ? q[k] : k; // solve triangular system, x = L\A(:,col) var top = cs_spsolve(L, m, col, xi, x, pinv, 1); // find pivot var ipiv = -1; var a = -1; // loop xi[] from top -> n for (p = top; p < n; p++) { // x[i] is nonzero i = xi[p]; // check row i is not yet pivotal if (pinv[i] < 0) { // absolute value of x[i] var xabs = abs(x[i]); // check absoulte value is greater than pivot value if (larger(xabs, a)) { // largest pivot candidate so far a = xabs; ipiv = i; } } else { // x(i) is the entry U(pinv[i],k) uindex[unz] = pinv[i]; uvalues[unz++] = x[i]; } } // validate we found a valid pivot if (ipiv == -1 || a <= 0) return null; // update actual pivot column, give preference to diagonal value if (pinv[col] < 0 && largerEq(abs(x[col]), multiply(a, tol))) ipiv = col; // the chosen pivot var pivot = x[ipiv]; // last entry in U(:,k) is U(k,k) uindex[unz] = k; uvalues[unz++] = pivot; // ipiv is the kth pivot row pinv[ipiv] = k; // first entry in L(:,k) is L(k,k) = 1 lindex[lnz] = ipiv; lvalues[lnz++] = 1; // L(k+1:n,k) = x / pivot for (p = top; p < n; p++) { // row i = xi[p]; // check x(i) is an entry in L(:,k) if (pinv[i] < 0) { // save unpermuted row in L lindex[lnz] = i; // scale pivot column lvalues[lnz++] = divideScalar(x[i], pivot); } // x[0..n-1] = 0 for next k x[i] = 0; } } // update ptr lptr[n] = lnz; uptr[n] = unz; // fix row indices of L for final pinv for (p = 0; p < lnz; p++) lindex[p] = pinv[lindex[p]]; // trim arrays lvalues.splice(lnz, lvalues.length - lnz); lindex.splice(lnz, lindex.length - lnz); uvalues.splice(unz, uvalues.length - unz); uindex.splice(unz, uindex.length - unz); // return LU factor return { L: L, U: U, pinv: pinv }; }; return cs_lu; } exports.name = 'cs_lu'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 352 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type) { var SparseMatrix = type.SparseMatrix; /** * Permutes a sparse matrix C = P * A * Q * * @param {Matrix} a The Matrix A * @param {Array} pinv The row permutation vector * @param {Array} q The column permutation vector * @param {boolean} values Create a pattern matrix (false), values and pattern otherwise * * @return {Matrix} C = P * A * Q, null on error * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_permute = function (a, pinv, q, values) { // a arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; var asize = a._size; var adt = a._datatype; // rows & columns var m = asize[0]; var n = asize[1]; // c arrays var cvalues = values && a._values ? [] : null; var cindex = []; // (aptr[n]); var cptr = []; // (n + 1); // initialize vars var nz = 0; // loop columns for (var k = 0; k < n; k++) { // column k of C is column q[k] of A cptr[k] = nz; // apply column permutation var j = q ? (q[k]) : k; // loop values in column j of A for (var t0 = aptr[j], t1 = aptr[j + 1], t = t0; t < t1; t++) { // row i of A is row pinv[i] of C var r = pinv ? pinv[aindex[t]] : aindex[t]; // index cindex[nz] = r; // check we need to populate values if (cvalues) cvalues[nz] = avalues[t]; // increment number of nonzero elements nz++; } } // finalize the last column of C cptr[n] = nz; // return C matrix return new SparseMatrix({ values: cvalues, index: cindex, ptr: cptr, size: [m, n], datatype: adt }); }; return cs_permute; } exports.name = 'cs_permute'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 353 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var cs_tdfs = load(__webpack_require__(99)); /** * Post order a tree of forest * * @param {Array} parent The tree or forest * @param {Number} n Number of columns * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_post = function (parent, n) { // check inputs if (!parent) return null; // vars var k = 0; var j; // allocate result var post = []; // (n); // workspace, head: first n entries, next: next n entries, stack: last n entries var w = []; // (3 * n); var head = 0; var next = n; var stack = 2 * n; // initialize workspace for (j = 0; j < n; j++) { // empty linked lists w[head + j] = -1; } // traverse nodes in reverse order for (j = n-1; j >= 0; j--) { // check j is a root if (parent[j] == -1) continue; // add j to list of its parent w[next + j] = w[head + parent[j]]; w[head + parent[j]] = j; } // loop nodes for (j = 0; j < n; j++) { // skip j if it is not a root if (parent[j] != -1) continue; // depth-first search k = cs_tdfs(j, k, w, head, next, post, stack); } return post; }; return cs_post; } exports.name = 'cs_post'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 354 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var cs_dfs = load(__webpack_require__(346)); var cs_marked = load(__webpack_require__(98)); var cs_mark = load(__webpack_require__(97)); /** * The cs_reach function computes X = Reach(B), where B is the nonzero pattern of the n-by-1 * sparse column of vector b. The function returns the set of nodes reachable from any node in B. The * nonzero pattern xi of the solution x to the sparse linear system Lx=b is given by X=Reach(B). * * @param {Matrix} g The G matrix * @param {Matrix} b The B matrix * @param {Number} k The kth column in B * @param {Array} xi The nonzero pattern xi[top] .. xi[n - 1], an array of size = 2 * n * The first n entries is the nonzero pattern, the last n entries is the stack * @param {Array} pinv The inverse row permutation vector * * @return {Number} The index for the nonzero pattern * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_reach = function (g, b, k, xi, pinv) { // g arrays var gptr = g._ptr; var gsize = g._size; // b arrays var bindex = b._index; var bptr = b._ptr; // columns var n = gsize[1]; // vars var p, p0, p1; // initialize top var top = n; // loop column indeces in B for (p0 = bptr[k], p1 = bptr[k + 1], p = p0; p < p1; p++) { // node i var i = bindex[p]; // check node i is marked if (!cs_marked(gptr, i)) { // start a dfs at unmarked node i top = cs_dfs(i, g, top, xi, pinv); } } // loop columns from top -> n - 1 for (p = top; p < n; p++) { // restore G cs_mark(gptr, xi[p]); } return top; }; return cs_reach; } exports.name = 'cs_reach'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 355 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var divideScalar = load(__webpack_require__(16)); var multiply = load(__webpack_require__(10)); var subtract = load(__webpack_require__(18)); var cs_reach = load(__webpack_require__(354)); /** * The function cs_spsolve() computes the solution to G * x = bk, where bk is the * kth column of B. When lo is true, the function assumes G = L is lower triangular with the * diagonal entry as the first entry in each column. When lo is true, the function assumes G = U * is upper triangular with the diagonal entry as the last entry in each column. * * @param {Matrix} g The G matrix * @param {Matrix} b The B matrix * @param {Number} k The kth column in B * @param {Array} xi The nonzero pattern xi[top] .. xi[n - 1], an array of size = 2 * n * The first n entries is the nonzero pattern, the last n entries is the stack * @param {Array} x The soluton to the linear system G * x = b * @param {Array} pinv The inverse row permutation vector, must be null for L * x = b * @param {boolean} lo The lower (true) upper triangular (false) flag * * @return {Number} The index for the nonzero pattern * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_spsolve = function (g, b, k, xi, x, pinv, lo) { // g arrays var gvalues = g._values; var gindex = g._index; var gptr = g._ptr; var gsize = g._size; // columns var n = gsize[1]; // b arrays var bvalues = b._values; var bindex = b._index; var bptr = b._ptr; // vars var p, p0, p1, q; // xi[top..n-1] = cs_reach(B(:,k)) var top = cs_reach(g, b, k, xi, pinv); // clear x for (p = top; p < n; p++) x[xi[p]] = 0; // scatter b for (p0 = bptr[k], p1 = bptr[k + 1], p = p0; p < p1; p++) x[bindex[p]] = bvalues[p]; // loop columns for (var px = top; px < n; px++) { // x array index for px var j = xi[px]; // apply permutation vector (U x = b), j maps to column J of G var J = pinv ? pinv[j] : j; // check column J is empty if (J < 0) continue; // column value indeces in G, p0 <= p < p1 p0 = gptr[J]; p1 = gptr[J + 1]; // x(j) /= G(j,j) x[j] = divideScalar(x[j], gvalues[lo ? p0 : (p1 - 1)]); // first entry L(j,j) p = lo ? (p0 + 1) : p0; q = lo ? (p1) : (p1 - 1); // loop for ( ; p < q ; p++) { // row var i = gindex[p]; // x(i) -= G(i,j) * x(j) x[i] = subtract(x[i], multiply(gvalues[p], x[j])); } } // return top of stack return top; }; return cs_spsolve; } exports.name = 'cs_spsolve'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 356 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var cs_amd = load(__webpack_require__(344)); var cs_permute = load(__webpack_require__(352)); var cs_etree = load(__webpack_require__(347)); var cs_post = load(__webpack_require__(353)); var cs_counts = load(__webpack_require__(345)); /** * Symbolic ordering and analysis for QR and LU decompositions. * * @param {Number} order The ordering strategy (see cs_amd for more details) * @param {Matrix} a The A matrix * @param {boolean} qr Symbolic ordering and analysis for QR decomposition (true) or * symbolic ordering and analysis for LU decomposition (false) * * @return {Object} The Symbolic ordering and analysis for matrix A * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_sqr = function (order, a, qr) { // a arrays var aptr = a._ptr; var asize = a._size; // columns var n = asize[1]; // vars var k; // symbolic analysis result var s = {}; // fill-reducing ordering s.q = cs_amd(order, a); // validate results if (order && !s.q) return null; // QR symbolic analysis if (qr) { // apply permutations if needed var c = order ? cs_permute(a, null, s.q, 0) : a; // etree of C'*C, where C=A(:,q) s.parent = cs_etree(c, 1); // post order elimination tree var post = cs_post (s.parent, n); // col counts chol(C'*C) s.cp = cs_counts(c, s.parent, post, 1); // check we have everything needed to calculate number of nonzero elements if (c && s.parent && s.cp && _vcount(c, s)) { // calculate number of nonzero elements for (s.unz = 0, k = 0; k < n; k++) s.unz += s.cp[k]; } } else { // for LU factorization only, guess nnz(L) and nnz(U) s.unz = 4 * (aptr[n]) + n; s.lnz = s.unz; } // return result S return s; }; /** * Compute nnz(V) = s.lnz, s.pinv, s.leftmost, s.m2 from A and s.parent */ var _vcount = function (a, s) { // a arrays var aptr = a._ptr; var aindex = a._index; var asize = a._size; // rows & columns var m = asize[0]; var n = asize[1]; // initialize s arrays s.pinv = []; // (m + n); s.leftmost = []; // (m); // vars var parent = s.parent; var pinv = s.pinv; var leftmost = s.leftmost; // workspace, next: first m entries, head: next n entries, tail: next n entries, nque: next n entries var w = []; // (m + 3 * n); var next = 0; var head = m; var tail = m + n; var nque = m + 2 * n; // vars var i, k, p, p0, p1; // initialize w for (k = 0; k < n; k++) { // queue k is empty w[head + k] = -1; w[tail + k] = -1; w[nque + k] = 0; } // initialize row arrays for (i = 0; i < m; i++) leftmost[i] = -1; // loop columns backwards for (k = n - 1; k >= 0; k--) { // values & index for column k for (p0 = aptr[k], p1 = aptr[k + 1], p = p0; p < p1; p++) { // leftmost[i] = min(find(A(i,:))) leftmost[aindex[p]] = k; } } // scan rows in reverse order for (i = m - 1; i >= 0; i--) { // row i is not yet ordered pinv[i] = -1; k = leftmost[i]; // check row i is empty if (k == -1) continue; // first row in queue k if (w[nque + k]++ === 0) w[tail + k] = i; // put i at head of queue k w[next + i] = w[head + k]; w[head + k] = i; } s.lnz = 0; s.m2 = m; // find row permutation and nnz(V) for (k = 0; k < n; k++) { // remove row i from queue k i = w[head + k]; // count V(k,k) as nonzero s.lnz++; // add a fictitious row if (i < 0) i = s.m2++; // associate row i with V(:,k) pinv[i] = k; // skip if V(k+1:m,k) is empty if (--nque[k] <= 0) continue; // nque[k] is nnz (V(k+1:m,k)) s.lnz += w[nque + k]; // move all rows to parent of k var pa = parent[k]; if (pa != -1) { if (w[nque + pa] === 0) w[tail + pa] = w[tail + k]; w[next + w[tail + k]] = w[head + pa]; w[head + pa] = w[next + i]; w[nque + pa] += w[nque + k]; } } for (i = 0; i < m; i++) { if (pinv[i] < 0) pinv[i] = k++; } return true; }; return cs_sqr; } exports.name = 'cs_sqr'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 357 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var cs_flip = load(__webpack_require__(65)); /** * Flips the value if it is negative of returns the same value otherwise. * * @param {Number} i The value to flip * * Reference: http://faculty.cse.tamu.edu/davis/publications.html */ var cs_unflip = function (i) { // flip the value if it is negative return i < 0 ? cs_flip(i) : i; }; return cs_unflip; } exports.name = 'cs_unflip'; exports.path = 'sparse'; exports.factory = factory; /***/ }), /* 358 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var unaryMinus = load(__webpack_require__(36)); var isNegative = load(__webpack_require__(47)); var matrix = load(__webpack_require__(1)); /** * Calculate the cubic root of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.cbrt(x) * math.cbrt(x, allRoots) * * Examples: * * math.cbrt(27); // returns 3 * math.cube(3); // returns 27 * math.cbrt(-64); // returns -4 * math.cbrt(math.unit('27 m^3')); // returns Unit 3 m * math.cbrt([27, 64, 125]); // returns [3, 4, 5] * * var x = math.complex('8i'); * math.cbrt(x); // returns Complex 1.7320508075689 + i * math.cbrt(x, true); // returns Matrix [ * // 1.7320508075689 + i * // -1.7320508075689 + i * // -2i * // ] * * See also: * * square, sqrt, cube * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x * Value for which to calculate the cubic root. * @param {boolean} [allRoots] Optional, false by default. Only applicable * when `x` is a number or complex number. If true, all complex * roots are returned, if false (default) the principal root is * returned. * @return {number | BigNumber | Complex | Unit | Array | Matrix} * Returns the cubic root of `x` */ var cbrt = typed('cbrt', { 'number': _cbrtNumber, // note: signature 'number, boolean' is also supported, // created by typed as it knows how to convert number to Complex 'Complex': _cbrtComplex, 'Complex, boolean': _cbrtComplex, 'BigNumber': function (x) { return x.cbrt(); }, 'Unit': _cbrtUnit, 'Array | Matrix': function (x) { // deep map collection, skip zeros since cbrt(0) = 0 return deepMap(x, cbrt, true); } }); /** * Calculate the cubic root for a complex number * @param {Complex} x * @param {boolean} [allRoots] If true, the function will return an array * with all three roots. If false or undefined, * the principal root is returned. * @returns {Complex | Array. | Matrix.} Returns the cubic root(s) of x * @private */ function _cbrtComplex(x, allRoots) { // https://www.wikiwand.com/en/Cube_root#/Complex_numbers var arg_3 = x.arg() / 3; var abs = x.abs(); // principal root: var principal = new type.Complex(_cbrtNumber(abs), 0).mul( new type.Complex(0, arg_3).exp()); if (allRoots) { var all = [ principal, new type.Complex(_cbrtNumber(abs), 0).mul( new type.Complex(0, arg_3 + Math.PI * 2 / 3).exp()), new type.Complex(_cbrtNumber(abs), 0).mul( new type.Complex(0, arg_3 - Math.PI * 2 / 3).exp()) ]; return (config.matrix === 'Array') ? all : matrix(all); } else { return principal; } } /** * Calculate the cubic root for a Unit * @param {Unit} x * @return {Unit} Returns the cubic root of x * @private */ function _cbrtUnit(x) { if(x.value && x.value.isComplex) { var result = x.clone(); result.value = 1.0; result = result.pow(1.0/3); // Compute the units result.value = _cbrtComplex(x.value); // Compute the value return result; } else { var negate = isNegative(x.value); if (negate) { x.value = unaryMinus(x.value); } // TODO: create a helper function for this var third; if (x.value && x.value.isBigNumber) { third = new type.BigNumber(1).div(3); } else if (x.value && x.value.isFraction) { third = new type.Fraction(1, 3); } else { third = 1/3; } var result = x.pow(third); if (negate) { result.value = unaryMinus(result.value); } return result; } } cbrt.toTex = {1: '\\sqrt[3]{${args[0]}}'}; return cbrt; } /** * Calculate cbrt for a number * * Code from es6-shim.js: * https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1564-L1577 * * @param {number} x * @returns {number | Complex} Returns the cubic root of x * @private */ var _cbrtNumber = Math.cbrt || function (x) { if (x === 0) { return x; } var negate = x < 0; var result; if (negate) { x = -x; } if (isFinite(x)) { result = Math.exp(Math.log(x) / 3); // from http://en.wikipedia.org/wiki/Cube_root#Numerical_methods result = (x / (result * result) + (2 * result)) / 3; } else { result = x; } return negate ? -result : result; }; exports.name = 'cbrt'; exports.factory = factory; /***/ }), /* 359 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Round a value towards plus infinity * If `x` is complex, both real and imaginary part are rounded towards plus infinity. * For matrices, the function is evaluated element wise. * * Syntax: * * math.ceil(x) * * Examples: * * math.ceil(3.2); // returns number 4 * math.ceil(3.8); // returns number 4 * math.ceil(-4.2); // returns number -4 * math.ceil(-4.7); // returns number -4 * * var c = math.complex(3.2, -2.7); * math.ceil(c); // returns Complex 4 - 2i * * math.ceil([3.2, 3.8, -4.7]); // returns Array [4, 4, -4] * * See also: * * floor, fix, round * * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value */ var ceil = typed('ceil', { 'number': Math.ceil, 'Complex': function (x) { return x.ceil(); }, 'BigNumber': function (x) { return x.ceil(); }, 'Fraction': function (x) { return x.ceil(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since ceil(0) = 0 return deepMap(x, ceil, true); } }); ceil.toTex = {1: '\\left\\lceil${args[0]}\\right\\rceil'}; return ceil; } exports.name = 'ceil'; exports.factory = factory; /***/ }), /* 360 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Compute the cube of a value, `x * x * x`. * For matrices, the function is evaluated element wise. * * Syntax: * * math.cube(x) * * Examples: * * math.cube(2); // returns number 8 * math.pow(2, 3); // returns number 8 * math.cube(4); // returns number 64 * 4 * 4 * 4; // returns number 64 * * math.cube([1, 2, 3, 4]); // returns Array [1, 8, 27, 64] * * See also: * * multiply, square, pow, cbrt * * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x Number for which to calculate the cube * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} Cube of x */ var cube = typed('cube', { 'number': function (x) { return x * x * x; }, 'Complex': function (x) { return x.mul(x).mul(x); // Is faster than pow(x, 3) }, 'BigNumber': function (x) { return x.times(x).times(x); }, 'Fraction': function (x) { return x.pow(3); // Is faster than mul()mul()mul() }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since cube(0) = 0 return deepMap(x, cube, true); }, 'Unit': function(x) { return x.pow(3); } }); cube.toTex = {1: '\\left(${args[0]}\\right)^3'}; return cube; } exports.name = 'cube'; exports.factory = factory; /***/ }), /* 361 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var multiplyScalar = load(__webpack_require__(20)); var latex = __webpack_require__(3); var algorithm02 = load(__webpack_require__(21)); var algorithm09 = load(__webpack_require__(128)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Multiply two matrices element wise. The function accepts both matrices and * scalar values. * * Syntax: * * math.dotMultiply(x, y) * * Examples: * * math.dotMultiply(2, 4); // returns 8 * * a = [[9, 5], [6, 1]]; * b = [[3, 2], [5, 2]]; * * math.dotMultiply(a, b); // returns [[27, 10], [30, 2]] * math.multiply(a, b); // returns [[52, 28], [23, 14]] * * See also: * * multiply, divide, dotDivide * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Left hand value * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Right hand value * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Multiplication of `x` and `y` */ var dotMultiply = typed('dotMultiply', { 'any, any': multiplyScalar, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse .* sparse c = algorithm09(x, y, multiplyScalar, false); break; default: // sparse .* dense c = algorithm02(y, x, multiplyScalar, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense .* sparse c = algorithm02(x, y, multiplyScalar, false); break; default: // dense .* dense c = algorithm13(x, y, multiplyScalar); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return dotMultiply(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return dotMultiply(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return dotMultiply(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, multiplyScalar, false); break; default: c = algorithm14(x, y, multiplyScalar, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm11(y, x, multiplyScalar, true); break; default: c = algorithm14(y, x, multiplyScalar, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, multiplyScalar, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, multiplyScalar, true).valueOf(); } }); dotMultiply.toTex = { 2: '\\left(${args[0]}' + latex.operators['dotMultiply'] + '${args[1]}\\right)' }; return dotMultiply; } exports.name = 'dotMultiply'; exports.factory = factory; /***/ }), /* 362 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var pow = load(__webpack_require__(35)); var latex = __webpack_require__(3); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm11 = load(__webpack_require__(13)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Calculates the power of x to y element wise. * * Syntax: * * math.dotPow(x, y) * * Examples: * * math.dotPow(2, 3); // returns number 8 * * var a = [[1, 2], [4, 3]]; * math.dotPow(a, 2); // returns Array [[1, 4], [16, 9]] * math.pow(a, 2); // returns Array [[9, 8], [16, 17]] * * See also: * * pow, sqrt, multiply * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x The base * @param {number | BigNumber | Complex | Unit | Array | Matrix} y The exponent * @return {number | BigNumber | Complex | Unit | Array | Matrix} The value of `x` to the power `y` */ var dotPow = typed('dotPow', { 'any, any': pow, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse .^ sparse c = algorithm07(x, y, pow, false); break; default: // sparse .^ dense c = algorithm03(y, x, pow, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense .^ sparse c = algorithm03(x, y, pow, false); break; default: // dense .^ dense c = algorithm13(x, y, pow); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return dotPow(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return dotPow(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return dotPow(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, dotPow, false); break; default: c = algorithm14(x, y, dotPow, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, dotPow, true); break; default: c = algorithm14(y, x, dotPow, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, dotPow, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, dotPow, true).valueOf(); } }); dotPow.toTex = { 2: '\\left(${args[0]}' + latex.operators['dotPow'] + '${args[1]}\\right)' }; return dotPow; } exports.name = 'dotPow'; exports.factory = factory; /***/ }), /* 363 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the exponent of a value. * For matrices, the function is evaluated element wise. * * Syntax: * * math.exp(x) * * Examples: * * math.exp(2); // returns number 7.3890560989306495 * math.pow(math.e, 2); // returns number 7.3890560989306495 * math.log(math.exp(2)); // returns number 2 * * math.exp([1, 2, 3]); * // returns Array [ * // 2.718281828459045, * // 7.3890560989306495, * // 20.085536923187668 * // ] * * See also: * * log, pow * * @param {number | BigNumber | Complex | Array | Matrix} x A number or matrix to exponentiate * @return {number | BigNumber | Complex | Array | Matrix} Exponent of `x` */ var exp = typed('exp', { 'number': Math.exp, 'Complex': function (x) { return x.exp(); }, 'BigNumber': function (x) { return x.exp(); }, 'Array | Matrix': function (x) { // TODO: exp(sparse) should return a dense matrix since exp(0)==1 return deepMap(x, exp); } }); exp.toTex = {1: '\\exp\\left(${args[0]}\\right)'}; return exp; } exports.name = 'exp'; exports.factory = factory; /***/ }), /* 364 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Round a value towards minus infinity. * For matrices, the function is evaluated element wise. * * Syntax: * * math.floor(x) * * Examples: * * math.floor(3.2); // returns number 3 * math.floor(3.8); // returns number 3 * math.floor(-4.2); // returns number -5 * math.floor(-4.7); // returns number -5 * * var c = math.complex(3.2, -2.7); * math.floor(c); // returns Complex 3 - 3i * * math.floor([3.2, 3.8, -4.7]); // returns Array [3, 3, -5] * * See also: * * ceil, fix, round * * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value */ var floor = typed('floor', { 'number': Math.floor, 'Complex': function (x) { return x.floor(); }, 'BigNumber': function (x) { return x.floor(); }, 'Fraction': function (x) { return x.floor(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since floor(0) = 0 return deepMap(x, floor, true); } }); floor.toTex = {1: '\\left\\lfloor${args[0]}\\right\\rfloor'}; return floor; } exports.name = 'floor'; exports.factory = factory; /***/ }), /* 365 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm01 = load(__webpack_require__(27)); var algorithm04 = load(__webpack_require__(73)); var algorithm10 = load(__webpack_require__(30)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Calculate the greatest common divisor for two or more values or arrays. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.gcd(a, b) * math.gcd(a, b, c, ...) * * Examples: * * math.gcd(8, 12); // returns 4 * math.gcd(-4, 6); // returns 2 * math.gcd(25, 15, -10); // returns 5 * * math.gcd([8, -4], [12, 6]); // returns [4, 2] * * See also: * * lcm, xgcd * * @param {... number | BigNumber | Fraction | Array | Matrix} args Two or more integer numbers * @return {number | BigNumber | Fraction | Array | Matrix} The greatest common divisor */ var gcd = typed('gcd', { 'number, number': _gcd, 'BigNumber, BigNumber': _gcdBigNumber, 'Fraction, Fraction': function (x, y) { return x.gcd(y); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm04(x, y, gcd); break; default: // sparse + dense c = algorithm01(y, x, gcd, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm01(x, y, gcd, false); break; default: // dense + dense c = algorithm13(x, y, gcd); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return gcd(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return gcd(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return gcd(x, matrix(y)); }, 'Matrix, number | BigNumber': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm10(x, y, gcd, false); break; default: c = algorithm14(x, y, gcd, false); break; } return c; }, 'number | BigNumber, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm10(y, x, gcd, true); break; default: c = algorithm14(y, x, gcd, true); break; } return c; }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, gcd, false).valueOf(); }, 'number | BigNumber, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, gcd, true).valueOf(); }, // TODO: need a smarter notation here 'Array | Matrix | number | BigNumber, Array | Matrix | number | BigNumber, ...Array | Matrix | number | BigNumber': function (a, b, args) { var res = gcd(a, b); for (var i = 0; i < args.length; i++) { res = gcd(res, args[i]); } return res; } }); gcd.toTex = '\\gcd\\left(${args}\\right)'; return gcd; /** * Calculate gcd for BigNumbers * @param {BigNumber} a * @param {BigNumber} b * @returns {BigNumber} Returns greatest common denominator of a and b * @private */ function _gcdBigNumber(a, b) { if (!a.isInt() || !b.isInt()) { throw new Error('Parameters in function gcd must be integer numbers'); } // http://en.wikipedia.org/wiki/Euclidean_algorithm var zero = new type.BigNumber(0); while (!b.isZero()) { var r = a.mod(b); a = b; b = r; } return a.lt(zero) ? a.neg() : a; } } /** * Calculate gcd for numbers * @param {number} a * @param {number} b * @returns {number} Returns the greatest common denominator of a and b * @private */ function _gcd(a, b) { if (!isInteger(a) || !isInteger(b)) { throw new Error('Parameters in function gcd must be integer numbers'); } // http://en.wikipedia.org/wiki/Euclidean_algorithm var r; while (b != 0) { r = a % b; a = b; b = r; } return (a < 0) ? -a : a; } exports.name = 'gcd'; exports.factory = factory; /***/ }), /* 366 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var flatten = __webpack_require__(7).flatten; function factory (type, config, load, typed) { var abs = load(__webpack_require__(25)); var add = load(__webpack_require__(17)); var divide = load(__webpack_require__(16)); var multiply = load(__webpack_require__(20)); var sqrt = load(__webpack_require__(50)); var smaller = load(__webpack_require__(38)); var isPositive = load(__webpack_require__(55)); /** * Calculate the hypotenusa of a list with values. The hypotenusa is defined as: * * hypot(a, b, c, ...) = sqrt(a^2 + b^2 + c^2 + ...) * * For matrix input, the hypotenusa is calculated for all values in the matrix. * * Syntax: * * math.hypot(a, b, ...) * math.hypot([a, b, c, ...]) * * Examples: * * math.hypot(3, 4); // 5 * math.hypot(3, 4, 5); // 7.0710678118654755 * math.hypot([3, 4, 5]); // 7.0710678118654755 * math.hypot(-2); // 2 * * See also: * * abs, norm * * @param {... number | BigNumber} args * @return {number | BigNumber} Returns the hypothenusa of the input values. */ var hypot = typed('hypot', { '... number | BigNumber': _hypot, 'Array': function (x) { return hypot.apply(hypot, flatten(x)); }, 'Matrix': function (x) { return hypot.apply(hypot, flatten(x.toArray())); } }); /** * Calculate the hypotenusa for an Array with values * @param {Array.} args * @return {number | BigNumber} Returns the result * @private */ function _hypot (args) { // code based on `hypot` from es6-shim: // https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1619-L1633 var result = 0; var largest = 0; for (var i = 0; i < args.length; i++) { var value = abs(args[i]); if (smaller(largest, value)) { result = multiply(result, multiply(divide(largest, value), divide(largest, value))); result = add(result, 1); largest = value; } else { result = add(result, isPositive(value) ? multiply(divide(value, largest), divide(value, largest)) : value); } } return multiply(largest, sqrt(result)); } hypot.toTex = '\\hypot\\left(${args}\\right)'; return hypot; } exports.name = 'hypot'; exports.factory = factory; /***/ }), /* 367 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(25), __webpack_require__(15), __webpack_require__(17), __webpack_require__(358), __webpack_require__(359), __webpack_require__(360), __webpack_require__(34), __webpack_require__(100), __webpack_require__(361), __webpack_require__(362), __webpack_require__(363), __webpack_require__(101), __webpack_require__(364), __webpack_require__(365), __webpack_require__(366), __webpack_require__(368), __webpack_require__(102), __webpack_require__(369), __webpack_require__(370), __webpack_require__(10), __webpack_require__(371), __webpack_require__(372), __webpack_require__(35), __webpack_require__(373), __webpack_require__(374), __webpack_require__(50), __webpack_require__(375), __webpack_require__(18), __webpack_require__(36), __webpack_require__(376), __webpack_require__(377) ]; /***/ }), /* 368 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm02 = load(__webpack_require__(21)); var algorithm06 = load(__webpack_require__(57)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Calculate the least common multiple for two or more values or arrays. * * lcm is defined as: * * lcm(a, b) = abs(a * b) / gcd(a, b) * * For matrices, the function is evaluated element wise. * * Syntax: * * math.lcm(a, b) * math.lcm(a, b, c, ...) * * Examples: * * math.lcm(4, 6); // returns 12 * math.lcm(6, 21); // returns 42 * math.lcm(6, 21, 5); // returns 210 * * math.lcm([4, 6], [6, 21]); // returns [12, 42] * * See also: * * gcd, xgcd * * @param {... number | BigNumber | Array | Matrix} args Two or more integer numbers * @return {number | BigNumber | Array | Matrix} The least common multiple */ var lcm = typed('lcm', { 'number, number': _lcm, 'BigNumber, BigNumber': _lcmBigNumber, 'Fraction, Fraction': function (x, y) { return x.lcm(y); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm06(x, y, lcm); break; default: // sparse + dense c = algorithm02(y, x, lcm, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm02(x, y, lcm, false); break; default: // dense + dense c = algorithm13(x, y, lcm); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return lcm(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return lcm(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return lcm(x, matrix(y)); }, 'Matrix, number | BigNumber': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, lcm, false); break; default: c = algorithm14(x, y, lcm, false); break; } return c; }, 'number | BigNumber, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm11(y, x, lcm, true); break; default: c = algorithm14(y, x, lcm, true); break; } return c; }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, lcm, false).valueOf(); }, 'number | BigNumber, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, lcm, true).valueOf(); }, // TODO: need a smarter notation here 'Array | Matrix | number | BigNumber, Array | Matrix | number | BigNumber, ...Array | Matrix | number | BigNumber': function (a, b, args) { var res = lcm(a, b); for (var i = 0; i < args.length; i++) { res = lcm(res, args[i]); } return res; } }); lcm.toTex = undefined; // use default template return lcm; /** * Calculate lcm for two BigNumbers * @param {BigNumber} a * @param {BigNumber} b * @returns {BigNumber} Returns the least common multiple of a and b * @private */ function _lcmBigNumber(a, b) { if (!a.isInt() || !b.isInt()) { throw new Error('Parameters in function lcm must be integer numbers'); } if (a.isZero() || b.isZero()) { return new type.BigNumber(0); } // http://en.wikipedia.org/wiki/Euclidean_algorithm // evaluate lcm here inline to reduce overhead var prod = a.times(b); while (!b.isZero()) { var t = b; b = a.mod(t); a = t; } return prod.div(a).abs(); } } /** * Calculate lcm for two numbers * @param {number} a * @param {number} b * @returns {number} Returns the least common multiple of a and b * @private */ function _lcm (a, b) { if (!isInteger(a) || !isInteger(b)) { throw new Error('Parameters in function lcm must be integer numbers'); } if (a == 0 || b == 0) { return 0; } // http://en.wikipedia.org/wiki/Euclidean_algorithm // evaluate lcm here inline to reduce overhead var t; var prod = a * b; while (b != 0) { t = b; b = a % t; a = t; } return Math.abs(prod / a); } exports.name = 'lcm'; exports.factory = factory; /***/ }), /* 369 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the 10-base logarithm of a value. This is the same as calculating `log(x, 10)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.log10(x) * * Examples: * * math.log10(0.00001); // returns -5 * math.log10(10000); // returns 4 * math.log(10000) / math.log(10); // returns 4 * math.pow(10, 4); // returns 10000 * * See also: * * exp, log * * @param {number | BigNumber | Complex | Array | Matrix} x * Value for which to calculate the logarithm. * @return {number | BigNumber | Complex | Array | Matrix} * Returns the 10-base logarithm of `x` */ var log10 = typed('log10', { 'number': function (x) { if (x >= 0 || config.predictable) { return _log10(x); } else { // negative value -> complex value computation return new type.Complex(x, 0).log().div(Math.LN10); } }, 'Complex': function (x) { return new type.Complex(x).log().div(Math.LN10); }, 'BigNumber': function (x) { if (!x.isNegative() || config.predictable) { return x.log(); } else { // downgrade to number, return Complex valued result return new type.Complex(x.toNumber(), 0).log().div(Math.LN10); } }, 'Array | Matrix': function (x) { return deepMap(x, log10); } }); log10.toTex = {1: '\\log_{10}\\left(${args[0]}\\right)'}; return log10; } /** * Calculate the 10-base logarithm of a number * @param {number} x * @return {number} * @private */ var _log10 = Math.log10 || function (x) { return Math.log(x) / Math.LN10; }; exports.name = 'log10'; exports.factory = factory; /***/ }), /* 370 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var latex = __webpack_require__(3); var algorithm02 = load(__webpack_require__(21)); var algorithm03 = load(__webpack_require__(12)); var algorithm05 = load(__webpack_require__(56)); var algorithm11 = load(__webpack_require__(13)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Calculates the modulus, the remainder of an integer division. * * For matrices, the function is evaluated element wise. * * The modulus is defined as: * * x - y * floor(x / y) * * See http://en.wikipedia.org/wiki/Modulo_operation. * * Syntax: * * math.mod(x, y) * * Examples: * * math.mod(8, 3); // returns 2 * math.mod(11, 2); // returns 1 * * function isOdd(x) { * return math.mod(x, 2) != 0; * } * * isOdd(2); // returns false * isOdd(3); // returns true * * See also: * * divide * * @param {number | BigNumber | Fraction | Array | Matrix} x Dividend * @param {number | BigNumber | Fraction | Array | Matrix} y Divisor * @return {number | BigNumber | Fraction | Array | Matrix} Returns the remainder of `x` divided by `y`. */ var mod = typed('mod', { 'number, number': _mod, 'BigNumber, BigNumber': function (x, y) { return y.isZero() ? x : x.mod(y); }, 'Fraction, Fraction': function (x, y) { return x.mod(y); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // mod(sparse, sparse) c = algorithm05(x, y, mod, false); break; default: // mod(sparse, dense) c = algorithm02(y, x, mod, true); break; } break; default: switch (y.storage()) { case 'sparse': // mod(dense, sparse) c = algorithm03(x, y, mod, false); break; default: // mod(dense, dense) c = algorithm13(x, y, mod); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return mod(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return mod(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return mod(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, mod, false); break; default: c = algorithm14(x, y, mod, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, mod, true); break; default: c = algorithm14(y, x, mod, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, mod, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, mod, true).valueOf(); } }); mod.toTex = { 2: '\\left(${args[0]}' + latex.operators['mod'] + '${args[1]}\\right)' }; return mod; /** * Calculate the modulus of two numbers * @param {number} x * @param {number} y * @returns {number} res * @private */ function _mod(x, y) { if (y > 0) { // We don't use JavaScript's % operator here as this doesn't work // correctly for x < 0 and x == 0 // see http://en.wikipedia.org/wiki/Modulo_operation return x - y * Math.floor(x / y); } else if (y === 0) { return x; } else { // y < 0 // TODO: implement mod for a negative divisor throw new Error('Cannot calculate mod for a negative divisor'); } } } exports.name = 'mod'; exports.factory = factory; /***/ }), /* 371 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var abs = load(__webpack_require__(25)); var add = load(__webpack_require__(15)); var pow = load(__webpack_require__(35)); var sqrt = load(__webpack_require__(50)); var multiply = load(__webpack_require__(10)); var equalScalar = load(__webpack_require__(8)); var larger = load(__webpack_require__(26)); var smaller = load(__webpack_require__(38)); var matrix = load(__webpack_require__(1)); var trace = load(__webpack_require__(111)); var transpose = load(__webpack_require__(52)); /** * Calculate the norm of a number, vector or matrix. * * The second parameter p is optional. If not provided, it defaults to 2. * * Syntax: * * math.norm(x) * math.norm(x, p) * * Examples: * * math.abs(-3.5); // returns 3.5 * math.norm(-3.5); // returns 3.5 * * math.norm(math.complex(3, -4)); // returns 5 * * math.norm([1, 2, -3], Infinity); // returns 3 * math.norm([1, 2, -3], -Infinity); // returns 1 * * math.norm([3, 4], 2); // returns 5 * * math.norm([[1, 2], [3, 4]], 1) // returns 6 * math.norm([[1, 2], [3, 4]], 'inf'); // returns 7 * math.norm([[1, 2], [3, 4]], 'fro'); // returns 5.477225575051661 * * See also: * * abs, hypot * * @param {number | BigNumber | Complex | Array | Matrix} x * Value for which to calculate the norm * @param {number | BigNumber | string} [p=2] * Vector space. * Supported numbers include Infinity and -Infinity. * Supported strings are: 'inf', '-inf', and 'fro' (The Frobenius norm) * @return {number | BigNumber} the p-norm */ var norm = typed('norm', { 'number': Math.abs, 'Complex': function (x) { return x.abs(); }, 'BigNumber': function (x) { // norm(x) = abs(x) return x.abs(); }, 'boolean | null' : function (x) { // norm(x) = abs(x) return Math.abs(x); }, 'Array': function (x) { return _norm(matrix(x), 2); }, 'Matrix': function (x) { return _norm(x, 2); }, 'number | Complex | BigNumber | boolean | null, number | BigNumber | string': function (x) { // ignore second parameter, TODO: remove the option of second parameter for these types return norm(x); }, 'Array, number | BigNumber | string': function (x, p) { return _norm(matrix(x), p); }, 'Matrix, number | BigNumber | string': function (x, p) { return _norm(x, p); } }); /** * Calculate the norm for an array * @param {Array} x * @param {number | string} p * @returns {number} Returns the norm * @private */ function _norm (x, p) { // size var sizeX = x.size(); // check if it is a vector if (sizeX.length == 1) { // check p if (p === Number.POSITIVE_INFINITY || p === 'inf') { // norm(x, Infinity) = max(abs(x)) var pinf = 0; // skip zeros since abs(0) == 0 x.forEach( function (value) { var v = abs(value); if (larger(v, pinf)) pinf = v; }, true); return pinf; } if (p === Number.NEGATIVE_INFINITY || p === '-inf') { // norm(x, -Infinity) = min(abs(x)) var ninf; // skip zeros since abs(0) == 0 x.forEach( function (value) { var v = abs(value); if (!ninf || smaller(v, ninf)) ninf = v; }, true); return ninf || 0; } if (p === 'fro') { return _norm(x, 2); } if (typeof p === 'number' && !isNaN(p)) { // check p != 0 if (!equalScalar(p, 0)) { // norm(x, p) = sum(abs(xi) ^ p) ^ 1/p var n = 0; // skip zeros since abs(0) == 0 x.forEach( function (value) { n = add(pow(abs(value), p), n); }, true); return pow(n, 1 / p); } return Number.POSITIVE_INFINITY; } // invalid parameter value throw new Error('Unsupported parameter value'); } // MxN matrix if (sizeX.length == 2) { // check p if (p === 1) { // norm(x) = the largest column sum var c = []; // result var maxc = 0; // skip zeros since abs(0) == 0 x.forEach( function (value, index) { var j = index[1]; var cj = add(c[j] || 0, abs(value)); if (larger(cj, maxc)) maxc = cj; c[j] = cj; }, true); return maxc; } if (p === Number.POSITIVE_INFINITY || p === 'inf') { // norm(x) = the largest row sum var r = []; // result var maxr = 0; // skip zeros since abs(0) == 0 x.forEach( function (value, index) { var i = index[0]; var ri = add(r[i] || 0, abs(value)); if (larger(ri, maxr)) maxr = ri; r[i] = ri; }, true); return maxr; } if (p === 'fro') { // norm(x) = sqrt(sum(diag(x'x))) return sqrt(trace(multiply(transpose(x), x))); } if (p === 2) { // not implemented throw new Error('Unsupported parameter value, missing implementation of matrix singular value decomposition'); } // invalid parameter value throw new Error('Unsupported parameter value'); } } norm.toTex = { 1: '\\left\\|${args[0]}\\right\\|', 2: undefined // use default template }; return norm; } exports.name = 'norm'; exports.factory = factory; /***/ }), /* 372 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm01 = load(__webpack_require__(27)); var algorithm02 = load(__webpack_require__(21)); var algorithm06 = load(__webpack_require__(57)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Calculate the nth root of a value. * The principal nth root of a positive real number A, is the positive real * solution of the equation * * x^root = A * * For matrices, the function is evaluated element wise. * * Syntax: * * math.nthRoot(a) * math.nthRoot(a, root) * * Examples: * * math.nthRoot(9, 2); // returns 3, as 3^2 == 9 * math.sqrt(9); // returns 3, as 3^2 == 9 * math.nthRoot(64, 3); // returns 4, as 4^3 == 64 * * See also: * * sqrt, pow * * @param {number | BigNumber | Array | Matrix | Complex} a * Value for which to calculate the nth root * @param {number | BigNumber} [root=2] The root. * @return {number | Complex | Array | Matrix} Returns the nth root of `a` */ var nthRoot = typed('nthRoot', { 'number': function (x) { return _nthRoot(x, 2); }, 'number, number': _nthRoot, 'BigNumber': function (x) { return _bigNthRoot(x, new type.BigNumber(2)); }, 'Complex' : function(x) { return _nthComplexRoot(x, 2); }, 'Complex, number' : _nthComplexRoot, 'BigNumber, BigNumber': _bigNthRoot, 'Array | Matrix': function (x) { return nthRoot(x, 2); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // density must be one (no zeros in matrix) if (y.density() === 1) { // sparse + sparse c = algorithm06(x, y, nthRoot); } else { // throw exception throw new Error('Root must be non-zero'); } break; default: // sparse + dense c = algorithm02(y, x, nthRoot, true); break; } break; default: switch (y.storage()) { case 'sparse': // density must be one (no zeros in matrix) if (y.density() === 1) { // dense + sparse c = algorithm01(x, y, nthRoot, false); } else { // throw exception throw new Error('Root must be non-zero'); } break; default: // dense + dense c = algorithm13(x, y, nthRoot); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return nthRoot(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return nthRoot(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return nthRoot(x, matrix(y)); }, 'Matrix, number | BigNumber': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, nthRoot, false); break; default: c = algorithm14(x, y, nthRoot, false); break; } return c; }, 'number | BigNumber, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': // density must be one (no zeros in matrix) if (y.density() === 1) { // sparse - scalar c = algorithm11(y, x, nthRoot, true); } else { // throw exception throw new Error('Root must be non-zero'); } break; default: c = algorithm14(y, x, nthRoot, true); break; } return c; }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return nthRoot(matrix(x), y).valueOf(); }, 'number | BigNumber, Array': function (x, y) { // use matrix implementation return nthRoot(x, matrix(y)).valueOf(); } }); nthRoot.toTex = {2: '\\sqrt[${args[1]}]{${args[0]}}'}; return nthRoot; /** * Calculate the nth root of a for BigNumbers, solve x^root == a * http://rosettacode.org/wiki/Nth_root#JavaScript * @param {BigNumber} a * @param {BigNumber} root * @private */ function _bigNthRoot(a, root) { var precision = type.BigNumber.precision; var Big = type.BigNumber.clone({precision: precision + 2}); var zero = new type.BigNumber(0); var one = new Big(1); var inv = root.isNegative(); if (inv) { root = root.neg(); } if (root.isZero()) { throw new Error('Root must be non-zero'); } if (a.isNegative() && !root.abs().mod(2).equals(1)) { throw new Error('Root must be odd when a is negative.'); } // edge cases zero and infinity if (a.isZero()) { return inv ? new Big(Infinity) : 0; } if (!a.isFinite()) { return inv ? zero : a; } var x = a.abs().pow(one.div(root)); // If a < 0, we require that root is an odd integer, // so (-1) ^ (1/root) = -1 x = a.isNeg() ? x.neg() : x; return new type.BigNumber((inv ? one.div(x) : x).toPrecision(precision)); } } /** * Calculate the nth root of a, solve x^root == a * http://rosettacode.org/wiki/Nth_root#JavaScript * @param {number} a * @param {number} root * @private */ function _nthRoot(a, root) { var inv = root < 0; if (inv) { root = -root; } if (root === 0) { throw new Error('Root must be non-zero'); } if (a < 0 && (Math.abs(root) % 2 != 1)) { throw new Error('Root must be odd when a is negative.'); } // edge cases zero and infinity if (a == 0) { return inv ? Infinity : 0; } if (!isFinite(a)) { return inv ? 0 : a; } var x = Math.pow(Math.abs(a), 1/root); // If a < 0, we require that root is an odd integer, // so (-1) ^ (1/root) = -1 x = a < 0 ? -x : x; return inv ? 1 / x : x; // Very nice algorithm, but fails with nthRoot(-2, 3). // Newton's method has some well-known problems at times: // https://en.wikipedia.org/wiki/Newton%27s_method#Failure_analysis /* var x = 1; // Initial guess var xPrev = 1; var i = 0; var iMax = 10000; do { var delta = (a / Math.pow(x, root - 1) - x) / root; xPrev = x; x = x + delta; i++; } while (xPrev !== x && i < iMax); if (xPrev !== x) { throw new Error('Function nthRoot failed to converge'); } return inv ? 1 / x : x; */ } /** * Calculate the nth root of a Complex Number a using De Moviers Theorem. * @param {Complex} a * @param {number} root * @return {Array} array or n Complex Roots in Polar Form. */ function _nthComplexRoot(a, root) { if (root < 0) throw new Error('Root must be greater than zero'); if (root === 0) throw new Error('Root must be non-zero'); if (root % 1 !== 0) throw new Error('Root must be an integer'); var arg = a.arg(); var abs = a.abs(); var roots = []; var r = Math.pow(abs, 1/root); for(var k = 0; k < root; k++) { roots.push({r: r, phi: (arg + 2 * Math.PI * k)/root}); } return roots; } exports.name = 'nthRoot'; exports.factory = factory; /***/ }), /* 373 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var toFixed = __webpack_require__(2).toFixed; var deepMap = __webpack_require__(0); var NO_INT = 'Number of decimals in function round must be an integer'; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var equalScalar = load(__webpack_require__(8)); var zeros = load(__webpack_require__(37)); var algorithm11 = load(__webpack_require__(13)); var algorithm12 = load(__webpack_require__(14)); var algorithm14 = load(__webpack_require__(5)); /** * Round a value towards the nearest integer. * For matrices, the function is evaluated element wise. * * Syntax: * * math.round(x) * math.round(x, n) * * Examples: * * math.round(3.2); // returns number 3 * math.round(3.8); // returns number 4 * math.round(-4.2); // returns number -4 * math.round(-4.7); // returns number -5 * math.round(math.pi, 3); // returns number 3.142 * math.round(123.45678, 2); // returns number 123.46 * * var c = math.complex(3.2, -2.7); * math.round(c); // returns Complex 3 - 3i * * math.round([3.2, 3.8, -4.7]); // returns Array [3, 4, -5] * * See also: * * ceil, fix, floor * * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded * @param {number | BigNumber | Array} [n=0] Number of decimals * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value */ var round = typed('round', { 'number': Math.round, 'number, number': function (x, n) { if (!isInteger(n)) {throw new TypeError(NO_INT);} if (n < 0 || n > 15) {throw new Error('Number of decimals in function round must be in te range of 0-15');} return _round(x, n); }, 'Complex': function (x) { return x.round(); }, 'Complex, number': function (x, n) { if (n % 1) {throw new TypeError(NO_INT);} return x.round(n); }, 'Complex, BigNumber': function (x, n) { if (!n.isInteger()) {throw new TypeError(NO_INT);} var _n = n.toNumber(); return x.round(_n); }, 'number, BigNumber': function (x, n) { if (!n.isInteger()) {throw new TypeError(NO_INT);} return new type.BigNumber(x).toDecimalPlaces(n.toNumber()); }, 'BigNumber': function (x) { return x.toDecimalPlaces(0); }, 'BigNumber, BigNumber': function (x, n) { if (!n.isInteger()) {throw new TypeError(NO_INT);} return x.toDecimalPlaces(n.toNumber()); }, 'Fraction': function (x) { return x.round(); }, 'Fraction, number': function (x, n) { if (n % 1) {throw new TypeError(NO_INT);} return x.round(n); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since round(0) = 0 return deepMap(x, round, true); }, 'Matrix, number | BigNumber': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, round, false); break; default: c = algorithm14(x, y, round, false); break; } return c; }, 'number | Complex | BigNumber, Matrix': function (x, y) { // check scalar is zero if (!equalScalar(x, 0)) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, round, true); break; default: c = algorithm14(y, x, round, true); break; } return c; } // do not execute algorithm, result will be a zero matrix return zeros(y.size(), y.storage()); }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, round, false).valueOf(); }, 'number | Complex | BigNumber, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, round, true).valueOf(); } }); round.toTex = { 1: '\\left\\lfloor${args[0]}\\right\\rceil', 2: undefined // use default template }; return round; } /** * round a number to the given number of decimals, or to zero if decimals is * not provided * @param {number} value * @param {number} decimals number of decimals, between 0 and 15 (0 by default) * @return {number} roundedValue * @private */ function _round (value, decimals) { return parseFloat(toFixed(value, decimals)); } exports.name = 'round'; exports.factory = factory; /***/ }), /* 374 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var number = __webpack_require__(2); var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Compute the sign of a value. The sign of a value x is: * * - 1 when x > 1 * - -1 when x < 0 * - 0 when x == 0 * * For matrices, the function is evaluated element wise. * * Syntax: * * math.sign(x) * * Examples: * * math.sign(3.5); // returns 1 * math.sign(-4.2); // returns -1 * math.sign(0); // returns 0 * * math.sign([3, 5, -2, 0, 2]); // returns [1, 1, -1, 0, 1] * * See also: * * abs * * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x * The number for which to determine the sign * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}e * The sign of `x` */ var sign = typed('sign', { 'number': number.sign, 'Complex': function (x) { return x.sign(); }, 'BigNumber': function (x) { return new type.BigNumber(x.cmp(0)); }, 'Fraction': function (x) { return new type.Fraction(x.s, 1); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since sign(0) = 0 return deepMap(x, sign, true); }, 'Unit': function(x) { return sign(x.value); } }); sign.toTex = {1: '\\mathrm{${name}}\\left(${args[0]}\\right)'}; return sign; } exports.name = 'sign'; exports.factory = factory; /***/ }), /* 375 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Compute the square of a value, `x * x`. * For matrices, the function is evaluated element wise. * * Syntax: * * math.square(x) * * Examples: * * math.square(2); // returns number 4 * math.square(3); // returns number 9 * math.pow(3, 2); // returns number 9 * math.multiply(3, 3); // returns number 9 * * math.square([1, 2, 3, 4]); // returns Array [1, 4, 9, 16] * * See also: * * multiply, cube, sqrt, pow * * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x * Number for which to calculate the square * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} * Squared value */ var square = typed('square', { 'number': function (x) { return x * x; }, 'Complex': function (x) { return x.mul(x); }, 'BigNumber': function (x) { return x.times(x); }, 'Fraction': function (x) { return x.mul(x); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since square(0) = 0 return deepMap(x, square, true); }, 'Unit': function(x) { return x.pow(2); } }); square.toTex = {1: '\\left(${args[0]}\\right)^2'}; return square; } exports.name = 'square'; exports.factory = factory; /***/ }), /* 376 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var latex = __webpack_require__(3); /** * Unary plus operation. * Boolean values and strings will be converted to a number, numeric values will be returned as is. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.unaryPlus(x) * * Examples: * * math.unaryPlus(3.5); // returns 3.5 * math.unaryPlus(1); // returns 1 * * See also: * * unaryMinus, add, subtract * * @param {number | BigNumber | Fraction | string | Complex | Unit | Array | Matrix} x * Input value * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} * Returns the input value when numeric, converts to a number when input is non-numeric. */ var unaryPlus = typed('unaryPlus', { 'number': function (x) { return x; }, 'Complex': function (x) { return x; // complex numbers are immutable }, 'BigNumber': function (x) { return x; // bignumbers are immutable }, 'Fraction': function (x) { return x; // fractions are immutable }, 'Unit': function (x) { return x.clone(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since unaryPlus(0) = 0 return deepMap(x, unaryPlus, true); }, 'boolean | string | null': function (x) { // convert to a number or bignumber return (config.number == 'BigNumber') ? new type.BigNumber(+x): +x; } }); unaryPlus.toTex = { 1: latex.operators['unaryPlus'] + '\\left(${args[0]}\\right)' }; return unaryPlus; } exports.name = 'unaryPlus'; exports.factory = factory; /***/ }), /* 377 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Calculate the extended greatest common divisor for two values. * See http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm. * * Syntax: * * math.xgcd(a, b) * * Examples: * * math.xgcd(8, 12); // returns [4, -1, 1] * math.gcd(8, 12); // returns 4 * math.xgcd(36163, 21199); // returns [1247, -7, 12] * * See also: * * gcd, lcm * * @param {number | BigNumber} a An integer number * @param {number | BigNumber} b An integer number * @return {Array} Returns an array containing 3 integers `[div, m, n]` * where `div = gcd(a, b)` and `a*m + b*n = div` */ var xgcd = typed('xgcd', { 'number, number': _xgcd, 'BigNumber, BigNumber': _xgcdBigNumber // TODO: implement support for Fraction }); xgcd.toTex = undefined; // use default template return xgcd; /** * Calculate xgcd for two numbers * @param {number} a * @param {number} b * @return {number} result * @private */ function _xgcd (a, b) { // source: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm var t, // used to swap two variables q, // quotient r, // remainder x = 0, lastx = 1, y = 1, lasty = 0; if (!isInteger(a) || !isInteger(b)) { throw new Error('Parameters in function xgcd must be integer numbers'); } while (b) { q = Math.floor(a / b); r = a % b; t = x; x = lastx - q * x; lastx = t; t = y; y = lasty - q * y; lasty = t; a = b; b = r; } var res; if (a < 0) { res = [-a, -lastx, -lasty]; } else { res = [a, a ? lastx : 0, lasty]; } return (config.matrix === 'Array') ? res : matrix(res); } /** * Calculate xgcd for two BigNumbers * @param {BigNumber} a * @param {BigNumber} b * @return {BigNumber[]} result * @private */ function _xgcdBigNumber(a, b) { // source: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm var t, // used to swap two variables q, // quotient r, // remainder zero = new type.BigNumber(0), one = new type.BigNumber(1), x = zero, lastx = one, y = one, lasty = zero; if (!a.isInt() || !b.isInt()) { throw new Error('Parameters in function xgcd must be integer numbers'); } while (!b.isZero()) { q = a.div(b).floor(); r = a.mod(b); t = x; x = lastx.minus(q.times(x)); lastx = t; t = y; y = lasty.minus(q.times(y)); lasty = t; a = b; b = r; } var res; if (a.lt(zero)) { res = [a.neg(), lastx.neg(), lasty.neg()]; } else { res = [a, !a.isZero() ? lastx : 0, lasty]; } return (config.matrix === 'Array') ? res : matrix(res); } } exports.name = 'xgcd'; exports.factory = factory; /***/ }), /* 378 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var bigBitAnd = __webpack_require__(495); function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var algorithm02 = load(__webpack_require__(21)); var algorithm06 = load(__webpack_require__(57)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Bitwise AND two values, `x & y`. * For matrices, the function is evaluated element wise. * * Syntax: * * math.bitAnd(x, y) * * Examples: * * math.bitAnd(53, 131); // returns number 1 * * math.bitAnd([1, 12, 31], 42); // returns Array [0, 8, 10] * * See also: * * bitNot, bitOr, bitXor, leftShift, rightArithShift, rightLogShift * * @param {number | BigNumber | Array | Matrix} x First value to and * @param {number | BigNumber | Array | Matrix} y Second value to and * @return {number | BigNumber | Array | Matrix} AND of `x` and `y` */ var bitAnd = typed('bitAnd', { 'number, number': function (x, y) { if (!isInteger(x) || !isInteger(y)) { throw new Error('Integers expected in function bitAnd'); } return x & y; }, 'BigNumber, BigNumber': bigBitAnd, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse & sparse c = algorithm06(x, y, bitAnd, false); break; default: // sparse & dense c = algorithm02(y, x, bitAnd, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense & sparse c = algorithm02(x, y, bitAnd, false); break; default: // dense & dense c = algorithm13(x, y, bitAnd); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return bitAnd(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return bitAnd(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return bitAnd(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, bitAnd, false); break; default: c = algorithm14(x, y, bitAnd, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm11(y, x, bitAnd, true); break; default: c = algorithm14(y, x, bitAnd, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, bitAnd, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, bitAnd, true).valueOf(); } }); bitAnd.toTex = { 2: '\\left(${args[0]}' + latex.operators['bitAnd'] + '${args[1]}\\right)' }; return bitAnd; } exports.name = 'bitAnd'; exports.factory = factory; /***/ }), /* 379 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var bigBitNot = __webpack_require__(76); var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var latex = __webpack_require__(3); /** * Bitwise NOT value, `~x`. * For matrices, the function is evaluated element wise. * For units, the function is evaluated on the best prefix base. * * Syntax: * * math.bitNot(x) * * Examples: * * math.bitNot(1); // returns number -2 * * math.bitNot([2, -3, 4]); // returns Array [-3, 2, 5] * * See also: * * bitAnd, bitOr, bitXor, leftShift, rightArithShift, rightLogShift * * @param {number | BigNumber | Array | Matrix} x Value to not * @return {number | BigNumber | Array | Matrix} NOT of `x` */ var bitNot = typed('bitNot', { 'number': function (x) { if (!isInteger(x)) { throw new Error('Integer expected in function bitNot'); } return ~x; }, 'BigNumber': bigBitNot, 'Array | Matrix': function (x) { return deepMap(x, bitNot); } }); bitNot.toTex = { 1: latex.operators['bitNot'] + '\\left(${args[0]}\\right)' }; return bitNot; } exports.name = 'bitNot'; exports.factory = factory; /***/ }), /* 380 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var bigBitOr = __webpack_require__(496); function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var algorithm01 = load(__webpack_require__(27)); var algorithm04 = load(__webpack_require__(73)); var algorithm10 = load(__webpack_require__(30)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Bitwise OR two values, `x | y`. * For matrices, the function is evaluated element wise. * For units, the function is evaluated on the lowest print base. * * Syntax: * * math.bitOr(x, y) * * Examples: * * math.bitOr(1, 2); // returns number 3 * * math.bitOr([1, 2, 3], 4); // returns Array [5, 6, 7] * * See also: * * bitAnd, bitNot, bitXor, leftShift, rightArithShift, rightLogShift * * @param {number | BigNumber | Array | Matrix} x First value to or * @param {number | BigNumber | Array | Matrix} y Second value to or * @return {number | BigNumber | Array | Matrix} OR of `x` and `y` */ var bitOr = typed('bitOr', { 'number, number': function (x, y) { if (!isInteger(x) || !isInteger(y)) { throw new Error('Integers expected in function bitOr'); } return x | y; }, 'BigNumber, BigNumber': bigBitOr, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm04(x, y, bitOr); break; default: // sparse + dense c = algorithm01(y, x, bitOr, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm01(x, y, bitOr, false); break; default: c = algorithm13(x, y, bitOr); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return bitOr(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return bitOr(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return bitOr(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm10(x, y, bitOr, false); break; default: c = algorithm14(x, y, bitOr, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm10(y, x, bitOr, true); break; default: c = algorithm14(y, x, bitOr, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, bitOr, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, bitOr, true).valueOf(); } }); bitOr.toTex = { 2: '\\left(${args[0]}' + latex.operators['bitOr'] + '${args[1]}\\right)' }; return bitOr; } exports.name = 'bitOr'; exports.factory = factory; /***/ }), /* 381 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var bigBitXor = __webpack_require__(497); function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Bitwise XOR two values, `x ^ y`. * For matrices, the function is evaluated element wise. * * Syntax: * * math.bitXor(x, y) * * Examples: * * math.bitXor(1, 2); // returns number 3 * * math.bitXor([2, 3, 4], 4); // returns Array [6, 7, 0] * * See also: * * bitAnd, bitNot, bitOr, leftShift, rightArithShift, rightLogShift * * @param {number | BigNumber | Array | Matrix} x First value to xor * @param {number | BigNumber | Array | Matrix} y Second value to xor * @return {number | BigNumber | Array | Matrix} XOR of `x` and `y` */ var bitXor = typed('bitXor', { 'number, number': function (x, y) { if (!isInteger(x) || !isInteger(y)) { throw new Error('Integers expected in function bitXor'); } return x ^ y; }, 'BigNumber, BigNumber': bigBitXor, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, bitXor); break; default: // sparse + dense c = algorithm03(y, x, bitXor, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, bitXor, false); break; default: // dense + dense c = algorithm13(x, y, bitXor); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return bitXor(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return bitXor(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return bitXor(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, bitXor, false); break; default: c = algorithm14(x, y, bitXor, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, bitXor, true); break; default: c = algorithm14(y, x, bitXor, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, bitXor, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, bitXor, true).valueOf(); } }); bitXor.toTex = { 2: '\\left(${args[0]}' + latex.operators['bitXor'] + '${args[1]}\\right)' }; return bitXor; } exports.name = 'bitXor'; exports.factory = factory; /***/ }), /* 382 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(378), __webpack_require__(379), __webpack_require__(380), __webpack_require__(381), __webpack_require__(383), __webpack_require__(384), __webpack_require__(385) ]; /***/ }), /* 383 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var bigLeftShift = __webpack_require__(499); function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var equalScalar = load(__webpack_require__(8)); var zeros = load(__webpack_require__(37)); var algorithm01 = load(__webpack_require__(27)); var algorithm02 = load(__webpack_require__(21)); var algorithm08 = load(__webpack_require__(74)); var algorithm10 = load(__webpack_require__(30)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Bitwise left logical shift of a value x by y number of bits, `x << y`. * For matrices, the function is evaluated element wise. * For units, the function is evaluated on the best prefix base. * * Syntax: * * math.leftShift(x, y) * * Examples: * * math.leftShift(1, 2); // returns number 4 * * math.leftShift([1, 2, 3], 4); // returns Array [16, 32, 64] * * See also: * * leftShift, bitNot, bitOr, bitXor, rightArithShift, rightLogShift * * @param {number | BigNumber | Array | Matrix} x Value to be shifted * @param {number | BigNumber} y Amount of shifts * @return {number | BigNumber | Array | Matrix} `x` shifted left `y` times */ var leftShift = typed('leftShift', { 'number, number': function (x, y) { if (!isInteger(x) || !isInteger(y)) { throw new Error('Integers expected in function leftShift'); } return x << y; }, 'BigNumber, BigNumber': bigLeftShift, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse & sparse c = algorithm08(x, y, leftShift, false); break; default: // sparse & dense c = algorithm02(y, x, leftShift, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense & sparse c = algorithm01(x, y, leftShift, false); break; default: // dense & dense c = algorithm13(x, y, leftShift); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return leftShift(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return leftShift(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return leftShift(x, matrix(y)); }, 'Matrix, number | BigNumber': function (x, y) { // check scalar if (!equalScalar(y, 0)) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, leftShift, false); break; default: c = algorithm14(x, y, leftShift, false); break; } return c; } return x.clone(); }, 'number | BigNumber, Matrix': function (x, y) { // check scalar if (!equalScalar(x, 0)) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm10(y, x, leftShift, true); break; default: c = algorithm14(y, x, leftShift, true); break; } return c; } return zeros(y.size(), y.storage()); }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return leftShift(matrix(x), y).valueOf(); }, 'number | BigNumber, Array': function (x, y) { // use matrix implementation return leftShift(x, matrix(y)).valueOf(); } }); leftShift.toTex = { 2: '\\left(${args[0]}' + latex.operators['leftShift'] + '${args[1]}\\right)' }; return leftShift; } exports.name = 'leftShift'; exports.factory = factory; /***/ }), /* 384 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var bigRightArithShift = __webpack_require__(500); function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var equalScalar = load(__webpack_require__(8)); var zeros = load(__webpack_require__(37)); var algorithm01 = load(__webpack_require__(27)); var algorithm02 = load(__webpack_require__(21)); var algorithm08 = load(__webpack_require__(74)); var algorithm10 = load(__webpack_require__(30)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Bitwise right arithmetic shift of a value x by y number of bits, `x >> y`. * For matrices, the function is evaluated element wise. * For units, the function is evaluated on the best prefix base. * * Syntax: * * math.rightArithShift(x, y) * * Examples: * * math.rightArithShift(4, 2); // returns number 1 * * math.rightArithShift([16, -32, 64], 4); // returns Array [1, -2, 3] * * See also: * * bitAnd, bitNot, bitOr, bitXor, rightArithShift, rightLogShift * * @param {number | BigNumber | Array | Matrix} x Value to be shifted * @param {number | BigNumber} y Amount of shifts * @return {number | BigNumber | Array | Matrix} `x` sign-filled shifted right `y` times */ var rightArithShift = typed('rightArithShift', { 'number, number': function (x, y) { if (!isInteger(x) || !isInteger(y)) { throw new Error('Integers expected in function rightArithShift'); } return x >> y; }, 'BigNumber, BigNumber': bigRightArithShift, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse & sparse c = algorithm08(x, y, rightArithShift, false); break; default: // sparse & dense c = algorithm02(y, x, rightArithShift, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense & sparse c = algorithm01(x, y, rightArithShift, false); break; default: // dense & dense c = algorithm13(x, y, rightArithShift); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return rightArithShift(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return rightArithShift(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return rightArithShift(x, matrix(y)); }, 'Matrix, number | BigNumber': function (x, y) { // check scalar if (!equalScalar(y, 0)) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, rightArithShift, false); break; default: c = algorithm14(x, y, rightArithShift, false); break; } return c; } return x.clone(); }, 'number | BigNumber, Matrix': function (x, y) { // check scalar if (!equalScalar(x, 0)) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm10(y, x, rightArithShift, true); break; default: c = algorithm14(y, x, rightArithShift, true); break; } return c; } return zeros(y.size(), y.storage()); }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return rightArithShift(matrix(x), y).valueOf(); }, 'number | BigNumber, Array': function (x, y) { // use matrix implementation return rightArithShift(x, matrix(y)).valueOf(); } }); rightArithShift.toTex = { 2: '\\left(${args[0]}' + latex.operators['rightArithShift'] + '${args[1]}\\right)' }; return rightArithShift; } exports.name = 'rightArithShift'; exports.factory = factory; /***/ }), /* 385 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var equalScalar = load(__webpack_require__(8)); var zeros = load(__webpack_require__(37)); var algorithm01 = load(__webpack_require__(27)); var algorithm02 = load(__webpack_require__(21)); var algorithm08 = load(__webpack_require__(74)); var algorithm10 = load(__webpack_require__(30)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Bitwise right logical shift of value x by y number of bits, `x >>> y`. * For matrices, the function is evaluated element wise. * For units, the function is evaluated on the best prefix base. * * Syntax: * * math.rightLogShift(x, y) * * Examples: * * math.rightLogShift(4, 2); // returns number 1 * * math.rightLogShift([16, -32, 64], 4); // returns Array [1, 2, 3] * * See also: * * bitAnd, bitNot, bitOr, bitXor, leftShift, rightLogShift * * @param {number | Array | Matrix} x Value to be shifted * @param {number} y Amount of shifts * @return {number | Array | Matrix} `x` zero-filled shifted right `y` times */ var rightLogShift = typed('rightLogShift', { 'number, number': function (x, y) { if (!isInteger(x) || !isInteger(y)) { throw new Error('Integers expected in function rightLogShift'); } return x >>> y; }, // 'BigNumber, BigNumber': ..., // TODO: implement BigNumber support for rightLogShift 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse & sparse c = algorithm08(x, y, rightLogShift, false); break; default: // sparse & dense c = algorithm02(y, x, rightLogShift, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense & sparse c = algorithm01(x, y, rightLogShift, false); break; default: // dense & dense c = algorithm13(x, y, rightLogShift); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return rightLogShift(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return rightLogShift(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return rightLogShift(x, matrix(y)); }, 'Matrix, number | BigNumber': function (x, y) { // check scalar if (!equalScalar(y, 0)) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, rightLogShift, false); break; default: c = algorithm14(x, y, rightLogShift, false); break; } return c; } return x.clone(); }, 'number | BigNumber, Matrix': function (x, y) { // check scalar if (!equalScalar(x, 0)) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm10(y, x, rightLogShift, true); break; default: c = algorithm14(y, x, rightLogShift, true); break; } return c; } return zeros(y.size(), y.storage()); }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return rightLogShift(matrix(x), y).valueOf(); }, 'number | BigNumber, Array': function (x, y) { // use matrix implementation return rightLogShift(x, matrix(y)).valueOf(); } }); rightLogShift.toTex = { 2: '\\left(${args[0]}' + latex.operators['rightLogShift'] + '${args[1]}\\right)' }; return rightLogShift; } exports.name = 'rightLogShift'; exports.factory = factory; /***/ }), /* 386 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var add = load(__webpack_require__(15)); var stirlingS2 = load(__webpack_require__(103)); var isNegative = load(__webpack_require__(47)); var isInteger = load(__webpack_require__(39)); /** * The Bell Numbers count the number of partitions of a set. A partition is a pairwise disjoint subset of S whose union is S. * bellNumbers only takes integer arguments. * The following condition must be enforced: n >= 0 * * Syntax: * * math.bellNumbers(n) * * Examples: * * math.bellNumbers(3); // returns 5; * math.bellNumbers(8); // returns 4140; * * See also: * * stirlingS2 * * @param {Number | BigNumber} n Total number of objects in the set * @return {Number | BigNumber} B(n) */ var bellNumbers = typed('bellNumbers', { 'number | BigNumber': function (n) { if (!isInteger(n) || isNegative(n)) { throw new TypeError('Non-negative integer value expected in function bellNumbers'); } // Sum (k=0, n) S(n,k). var result = 0; for(var i = 0; i <= n; i++) { result = add(result, stirlingS2(n, i)); } return result; } }); bellNumbers.toTex = {1: '\\mathrm{B}_{${args[0]}}'}; return bellNumbers; } exports.name = 'bellNumbers'; exports.factory = factory; /***/ }), /* 387 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var add = load(__webpack_require__(15)); var divide = load(__webpack_require__(34)); var multiply = load(__webpack_require__(10)); var combinations = load(__webpack_require__(53)); var isNegative = load(__webpack_require__(47)); var isInteger = load(__webpack_require__(39)); /** * The Catalan Numbers enumerate combinatorial structures of many different types. * catalan only takes integer arguments. * The following condition must be enforced: n >= 0 * * Syntax: * * math.catalan(n) * * Examples: * * math.catalan(3); // returns 5; * math.catalan(8); // returns 1430; * * See also: * * bellNumbers * * @param {Number | BigNumber} n nth Catalan number * @return {Number | BigNumber} Cn(n) */ var catalan = typed('catalan', { 'number | BigNumber': function (n) { if (!isInteger(n) || isNegative(n)) { throw new TypeError('Non-negative integer value expected in function catalan'); } return divide(combinations(multiply(n,2), n), add(n,1)); } }); catalan.toTex = {1: '\\mathrm{C}_{${args[0]}}'}; return catalan; } exports.name = 'catalan'; exports.factory = factory; /***/ }), /* 388 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var combinations = load(__webpack_require__(53)); var add = load(__webpack_require__(17)); var isPositive = load(__webpack_require__(55)); var isInteger = load(__webpack_require__(39)); var larger = load(__webpack_require__(26)); /** * The composition counts of n into k parts. * * composition only takes integer arguments. * The following condition must be enforced: k <= n. * * Syntax: * * math.composition(n, k) * * Examples: * * math.composition(5, 3); // returns 6 * * See also: * * combinations * * @param {Number | BigNumber} n Total number of objects in the set * @param {Number | BigNumber} k Number of objects in the subset * @return {Number | BigNumber} Returns the composition counts of n into k parts. */ var composition = typed('composition', { 'number | BigNumber, number | BigNumber': function (n, k) { if (!isInteger(n) || !isPositive(n) || !isInteger(k) || !isPositive(k)) { throw new TypeError('Positive integer value expected in function composition'); } else if (larger(k, n)) { throw new TypeError('k must be less than or equal to n in function composition'); } return combinations(add(n, -1), add(k, -1)); } }); composition.toTex = undefined; // use default template return composition; } exports.name = 'composition'; exports.factory = factory; /***/ }), /* 389 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(386), __webpack_require__(388), __webpack_require__(103), __webpack_require__(387) ]; /***/ }), /* 390 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Compute the argument of a complex value. * For a complex number `a + bi`, the argument is computed as `atan2(b, a)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.arg(x) * * Examples: * * var a = math.complex(2, 2); * math.arg(a) / math.pi; // returns number 0.25 * * var b = math.complex('2 + 3i'); * math.arg(b); // returns number 0.982793723247329 * math.atan2(3, 2); // returns number 0.982793723247329 * * See also: * * re, im, conj, abs * * @param {number | BigNumber | Complex | Array | Matrix} x * A complex number or array with complex numbers * @return {number | BigNumber | Array | Matrix} The argument of x */ var arg = typed('arg', { 'number': function (x) { return Math.atan2(0, x); }, 'BigNumber': function (x) { return type.BigNumber.atan2(0, x); }, 'Complex': function (x) { return x.arg(); }, // TODO: implement BigNumber support for function arg 'Array | Matrix': function (x) { return deepMap(x, arg); } }); arg.toTex = {1: '\\arg\\left(${args[0]}\\right)'}; return arg; } exports.name = 'arg'; exports.factory = factory; /***/ }), /* 391 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Compute the complex conjugate of a complex value. * If `x = a+bi`, the complex conjugate of `x` is `a - bi`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.conj(x) * * Examples: * * math.conj(math.complex('2 + 3i')); // returns Complex 2 - 3i * math.conj(math.complex('2 - 3i')); // returns Complex 2 + 3i * math.conj(math.complex('-5.2i')); // returns Complex 5.2i * * See also: * * re, im, arg, abs * * @param {number | BigNumber | Complex | Array | Matrix} x * A complex number or array with complex numbers * @return {number | BigNumber | Complex | Array | Matrix} * The complex conjugate of x */ var conj = typed('conj', { 'number': function (x) { return x; }, 'BigNumber': function (x) { return x; }, 'Complex': function (x) { return x.conjugate(); }, 'Array | Matrix': function (x) { return deepMap(x, conj); } }); conj.toTex = {1: '\\left(${args[0]}\\right)^*'}; return conj; } exports.name = 'conj'; exports.factory = factory; /***/ }), /* 392 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Get the imaginary part of a complex number. * For a complex number `a + bi`, the function returns `b`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.im(x) * * Examples: * * var a = math.complex(2, 3); * math.re(a); // returns number 2 * math.im(a); // returns number 3 * * math.re(math.complex('-5.2i')); // returns number -5.2 * math.re(math.complex(2.4)); // returns number 0 * * See also: * * re, conj, abs, arg * * @param {number | BigNumber | Complex | Array | Matrix} x * A complex number or array with complex numbers * @return {number | BigNumber | Array | Matrix} The imaginary part of x */ var im = typed('im', { 'number': function (x) { return 0; }, 'BigNumber': function (x) { return new type.BigNumber(0); }, 'Complex': function (x) { return x.im; }, 'Array | Matrix': function (x) { return deepMap(x, im); } }); im.toTex = {1: '\\Im\\left\\lbrace${args[0]}\\right\\rbrace'}; return im; } exports.name = 'im'; exports.factory = factory; /***/ }), /* 393 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(390), __webpack_require__(391), __webpack_require__(392), __webpack_require__(394) ]; /***/ }), /* 394 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Get the real part of a complex number. * For a complex number `a + bi`, the function returns `a`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.re(x) * * Examples: * * var a = math.complex(2, 3); * math.re(a); // returns number 2 * math.im(a); // returns number 3 * * math.re(math.complex('-5.2i')); // returns number 0 * math.re(math.complex(2.4)); // returns number 2.4 * * See also: * * im, conj, abs, arg * * @param {number | BigNumber | Complex | Array | Matrix} x * A complex number or array with complex numbers * @return {number | BigNumber | Array | Matrix} The real part of x */ var re = typed('re', { 'number': function (x) { return x; }, 'BigNumber': function (x) { return x; }, 'Complex': function (x) { return x.re; }, 'Array | Matrix': function (x) { return deepMap(x, re); } }); re.toTex = {1: '\\Re\\left\\lbrace${args[0]}\\right\\rbrace'}; return re; } exports.name = 're'; exports.factory = factory; /***/ }), /* 395 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Calculates: * The eucledian distance between two points in 2 and 3 dimensional spaces. * Distance between point and a line in 2 and 3 dimensional spaces. * Pairwise distance between a set of 2D or 3D points * NOTE: * When substituting coefficients of a line(a, b and c), use ax + by + c = 0 instead of ax + by = c * For parametric equation of a 3D line, x0, y0, z0, a, b, c are from: (x−x0, y−y0, z−z0) = t(a, b, c) * * Syntax: * math.distance([x1, y1], [x2, y2]) *- math.distance({pointOneX: 4, pointOneY: 5}, {pointTwoX: 2, pointTwoY: 7}) * math.distance([x1, y1, z1], [x2, y2, z2]) * math.distance({pointOneX: 4, pointOneY: 5, pointOneZ: 8}, {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9}) * math.distance([[A], [B], [C]...]) * math.distance([x1, y1], [LinePtX1, LinePtY1], [LinePtX2, LinePtY2]) * math.distance({pointX: 1, pointY: 4}, {lineOnePtX: 6, lineOnePtY: 3}, {lineTwoPtX: 2, lineTwoPtY: 8}) * math.distance([x1, y1, z1], [LinePtX1, LinePtY1, LinePtZ1], [LinePtX2, LinePtY2, LinePtZ2]) * math.distance({pointX: 1, pointY: 4, pointZ: 7}, {lineOnePtX: 6, lineOnePtY: 3, lineOnePtZ: 4}, {lineTwoPtX: 2, lineTwoPtY: 8, lineTwoPtZ: 5}) * math.distance([x1, y1], [xCoeffLine, yCoeffLine, constant]) * math.distance({pointX: 10, pointY: 10}, {xCoeffLine: 8, yCoeffLine: 1, constant: 3}) * math.distance([x1, y1, z1], [x0, y0, z0, a-tCoeff, b-tCoeff, c-tCoeff]) point and parametric equation of 3D line * math.distance([x, y, z], [x0, y0, z0, a, b, c]) * math.distance({pointX: 2, pointY: 5, pointZ: 9}, {x0: 4, y0: 6, z0: 3, a: 4, b: 2, c: 0}) * * Examples: * math.distance([0,0], [4,4]) // Returns 5.6569 * math.distance( * {pointOneX: 0, pointOneY: 0}, * {pointTwoX: 10, pointTwoY: 10}) // Returns 14.142135623730951 * math.distance([1, 0, 1], [4, -2, 2]) // Returns 3.74166 * math.distance( * {pointOneX: 4, pointOneY: 5, pointOneZ: 8}, * {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9}) // Returns 3 * math.distance([[1, 2], [1, 2], [1, 3]]) // Returns [0, 1, 1] * math.distance([[1,2,4], [1,2,6], [8,1,3]]) // Returns [2, 7.14142842854285, 7.681145747868608] * math.distance([10, 10], [8, 1, 3]) // Returns 11.535230316796387 * math.distance([10, 10], [2, 3], [-8, 0]) // Returns 8.759953130362847 * math.distance( * {pointX: 1, pointY: 4}, * {lineOnePtX: 6, lineOnePtY: 3}, * {lineTwoPtX: 2, lineTwoPtY: 8}) // Returns 2.720549372624744 * math.distance([2, 3, 1], [1, 1, 2, 5, 0, 1]) // Returns 2.3204774044612857 * math.distance( * {pointX: 2, pointY: 3, pointZ: 1}, * {x0: 1, y0: 1, z0: 2, a: 5, b: 0, c: 1} // Returns 2.3204774044612857 * * @param {Array | Matrix | Object} x Co-ordinates of first point * @param {Array | Matrix | Object} y Co-ordinates of second point * @return {Number | BigNumber} Returns the distance from two/three points */ var distance = typed('distance', { 'Array, Array, Array': function(x, y, z){ // Point to Line 2D; (x=Point, y=LinePoint1, z=LinePoint2) if (x.length == 2 && y.length == 2 && z.length == 2){ if (!_2d(x)) { throw new TypeError('Array with 2 numbers expected for first argument'); } if (!_2d(y)) { throw new TypeError('Array with 2 numbers expected for second argument'); } if (!_2d(z)) { throw new TypeError('Array with 2 numbers expected for third argument'); } var m = (z[1]-z[0])/(y[1]-y[0]); var xCoeff = m*m*y[0]; var yCoeff = -1*(m*y[0]); var constant = x[1]; return _distancePointLine2D(x[0], x[1], xCoeff, yCoeff, constant); } else{ throw new TypeError('Invalid Arguments: Try again'); } }, 'Object, Object, Object': function(x, y, z){ if (Object.keys(x).length == 2 && Object.keys(y).length == 2 && Object.keys(z).length == 2){ if (!_2d(x)) { throw new TypeError('Values of pointX and pointY should be numbers'); } if (!_2d(y)) { throw new TypeError('Values of lineOnePtX and lineOnePtY should be numbers'); } if (!_2d(z)) { throw new TypeError('Values of lineTwoPtX and lineTwoPtY should be numbers'); } if (x.hasOwnProperty('pointX') && x.hasOwnProperty('pointY') && y.hasOwnProperty('lineOnePtX') && y.hasOwnProperty('lineOnePtY') && z.hasOwnProperty('lineTwoPtX') && z.hasOwnProperty('lineTwoPtY')){ var m = (z.lineTwoPtY-z.lineTwoPtX)/(y.lineOnePtY-y.lineOnePtX); var xCoeff = m*m*y.lineOnePtX; var yCoeff = -1*(m*y.lineOnePtX); var constant = x.pointX; return _distancePointLine2D(x.pointX, x.pointY, xCoeff, yCoeff, constant); } else{ throw new TypeError('Key names do not match'); } } else{ throw new TypeError('Invalid Arguments: Try again'); } }, 'Array, Array': function(x, y){ // Point to Line 2D; (x=[pointX, pointY], y=[x-coeff, y-coeff, const]) if (x.length == 2 && y.length == 3){ if (!_2d(x)) { throw new TypeError('Array with 2 numbers expected for first argument'); } if (!_3d(y)) { throw new TypeError('Array with 3 numbers expected for second argument'); } return _distancePointLine2D(x[0], x[1], y[0], y[1], y[2]); } // Point to Line 3D else if (x.length == 3 && y.length == 6){ if (!_3d(x)) { throw new TypeError('Array with 3 numbers expected for first argument'); } if (!_parametricLine(y)) { throw new TypeError('Array with 6 numbers expected for second argument'); } return _distancePointLine3D(x[0], x[1], x[2], y[0], y[1], y[2], y[3], y[4], y[5]); } // Point to Point 2D else if (x.length == 2 && y.length == 2){ if (!_2d(x)) { throw new TypeError('Array with 2 numbers expected for first argument'); } if (!_2d(y)) { throw new TypeError('Array with 2 numbers expected for second argument'); } return _distance2d(x[0], x[1], y[0], y[1]); } // Point to Point 3D else if(x.length == 3 && y.length == 3){ if (!_3d(x)) { throw new TypeError('Array with 3 numbers expected for first argument'); } if (!_3d(y)) { throw new TypeError('Array with 3 numbers expected for second argument'); } return _distance3d(x[0], x[1], x[2], y[0], y[1], y[2]); } else{ throw new TypeError('Invalid Arguments: Try again'); } }, 'Object, Object': function(x, y){ if (Object.keys(x).length == 2 && Object.keys(y).length == 3){ if (!_2d(x)) { throw new TypeError('Values of pointX and pointY should be numbers'); } if (!_3d(y)) { throw new TypeError('Values of xCoeffLine, yCoeffLine and constant should be numbers'); } if (x.hasOwnProperty('pointX') && x.hasOwnProperty('pointY') && y.hasOwnProperty('xCoeffLine') && y.hasOwnProperty('yCoeffLine') && y.hasOwnProperty('yCoeffLine')){ return _distancePointLine2D(x.pointX, x.pointY, y.xCoeffLine, y.yCoeffLine, y.constant); } else{ throw new TypeError('Key names do not match'); } } // Point to Line 3D else if (Object.keys(x).length == 3 && Object.keys(y).length == 6){ if (!_3d(x)) { throw new TypeError('Values of pointX, pointY and pointZ should be numbers'); } if (!_parametricLine(y)) { throw new TypeError('Values of x0, y0, z0, a, b and c should be numbers'); } if (x.hasOwnProperty('pointX') && x.hasOwnProperty('pointY') && y.hasOwnProperty('x0') && y.hasOwnProperty('y0') && y.hasOwnProperty('z0') && y.hasOwnProperty('a') && y.hasOwnProperty('b') && y.hasOwnProperty('c')){ return _distancePointLine3D(x.pointX, x.pointY, x.pointZ, y.x0, y.y0, y.z0, y.a, y.b, y.c); } else{ throw new TypeError('Key names do not match'); } } // Point to Point 2D else if (Object.keys(x).length == 2 && Object.keys(y).length == 2){ if (!_2d(x)) { throw new TypeError('Values of pointOneX and pointOneY should be numbers'); } if (!_2d(y)) { throw new TypeError('Values of pointTwoX and pointTwoY should be numbers'); } if (x.hasOwnProperty('pointOneX') && x.hasOwnProperty('pointOneY') && y.hasOwnProperty('pointTwoX') && y.hasOwnProperty('pointTwoY')){ return _distance2d(x.pointOneX, x.pointOneY, y.pointTwoX, y.pointTwoY); } else{ throw new TypeError('Key names do not match'); } } // Point to Point 3D else if(Object.keys(x).length == 3 && Object.keys(y).length == 3){ if (!_3d(x)) { throw new TypeError('Values of pointOneX, pointOneY and pointOneZ should be numbers'); } if (!_3d(y)) { throw new TypeError('Values of pointTwoX, pointTwoY and pointTwoZ should be numbers'); } if (x.hasOwnProperty('pointOneX') && x.hasOwnProperty('pointOneY') && x.hasOwnProperty('pointOneZ') && y.hasOwnProperty('pointTwoX') && y.hasOwnProperty('pointTwoY') && y.hasOwnProperty('pointTwoZ')){ return _distance3d(x.pointOneX, x.pointOneY, x.pointOneZ, y.pointTwoX, y.pointTwoY, y.pointTwoZ); } else { throw new TypeError('Key names do not match'); } } else{ throw new TypeError('Invalid Arguments: Try again'); } }, 'Array': function(arr){ if (!_pairwise(arr)) { throw new TypeError('Incorrect array format entered for pairwise distance calculation'); } return _distancePairwise(arr); } }); return distance; } function _2d(a){ // checks if the number of arguments are correct in count and are valid (should be numbers) if (a.constructor !== Array){ a = _objectToArray(a); } return typeof a[0] === 'number' && typeof a[1] === 'number'; } function _3d(a){ // checks if the number of arguments are correct in count and are valid (should be numbers) if (a.constructor !== Array){ a = _objectToArray(a); } return typeof a[0] === 'number' && typeof a[1] === 'number' && typeof a[2] === 'number'; } function _parametricLine(a){ if (a.constructor !== Array){ a = _objectToArray(a); } return typeof a[0] === 'number' && typeof a[1] === 'number' && typeof a[2] === 'number' && typeof a[3] === 'number' && typeof a[4] === 'number' && typeof a[5] === 'number'; } function _objectToArray(o){ var keys = Object.keys(o); var a = []; for (var i = 0; i < keys.length; i++) { a.push(o[keys[i]]); } return a; } function _pairwise(a){ //checks for valid arguments passed to _distancePairwise(Array) if (a[0].length == 2 && typeof a[0][0] === 'number' && typeof a[0][1] === 'number'){ for(var i in a){ if (a[i].length != 2 || typeof a[i][0] !== 'number' || typeof a[i][1] !== 'number'){ return false; } } } else if (a[0].length == 3 && typeof a[0][0] === 'number' && typeof a[0][1] === 'number' && typeof a[0][2] === 'number'){ for(var i in a){ if (a[i].length != 3 || typeof a[i][0] !== 'number' || typeof a[i][1] !== 'number' || typeof a[i][2] !== 'number'){ return false; } } } else{ return false; } return true; } function _distancePointLine2D(x, y, a, b, c){ var num = Math.abs(a*x + b*y + c); var den = Math.pow((a*a + b*b), 0.5); var result = (num/den); return result; } function _distancePointLine3D(x, y, z, x0, y0, z0, a, b, c){ var num = [((y0-y)*(c))-((z0-z)*(b)), ((z0-z)*(a))-((x0-x)*(c)), ((x0-x)*(b))-((y0-y)*(a))] num = Math.pow(num[0]*num[0] + num[1]*num[1] + num[2]*num[2], 0.5); var den = Math.pow(a*a + b*b + c*c, 0.5); var result = num/den; return result; } function _distance2d(x1, y1, x2, y2){ var yDiff = y2 - y1; var xDiff = x2 - x1; var radicant = yDiff * yDiff + xDiff * xDiff; var result = Math.pow(radicant, 0.5); return result; } function _distance3d(x1, y1, z1, x2, y2, z2){ var zDiff = z2 - z1; var yDiff = y2 - y1; var xDiff = x2 - x1; var radicant = zDiff * zDiff + yDiff * yDiff + xDiff * xDiff; var result = Math.pow(radicant, 0.5); return result; } function _distancePairwise(a){ var result = []; for(var i = 0; i < a.length-1; i++){ for(var j = i+1; j < a.length; j++){ if (a[0].length == 2){ result.push(_distance2d(a[i][0], a[i][1], a[j][0], a[j][1])); } else if (a[0].length == 3){ result.push(_distance3d(a[i][0], a[i][1], a[i][2], a[j][0], a[j][1], a[j][2])); } } } return result; } exports.name = 'distance'; exports.factory = factory; /***/ }), /* 396 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(397), __webpack_require__(395) ]; /***/ }), /* 397 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var abs = load(__webpack_require__(25)); var add = load(__webpack_require__(15)); var matrix = load(__webpack_require__(1)); var multiply = load(__webpack_require__(10)); var subtract = load(__webpack_require__(18)); /** * Calculates the point of intersection of two lines in two or three dimensions * and of a line and a plane in three dimensions. The inputs are in the form of * arrays or 1 dimensional matrices. The line intersection functions return null * if the lines do not meet. * * Note: Fill the plane coefficients as `x + y + z = c` and not as `x + y + z + c = 0`. * * Syntax: * * math.intersect(endPoint1Line1, endPoint2Line1, endPoint1Line2, endPoint2Line2) * math.intersect(endPoint1, endPoint2, planeCoefficients) * * Examples: * * math.intersect([0, 0], [10, 10], [10, 0], [0, 10]); // Returns [5, 5] * math.intersect([0, 0, 0], [10, 10, 0], [10, 0, 0], [0, 10, 0]); // Returns [5, 5, 0] * math.intersect([1, 0, 1], [4, -2, 2], [1, 1, 1, 6]); // Returns [7, -4, 3] * * @param {Array | Matrix} w Co-ordinates of first end-point of first line * @param {Array | Matrix} x Co-ordinates of second end-point of first line * @param {Array | Matrix} y Co-ordinates of first end-point of second line * OR Co-efficients of the plane's equation * @param {Array | Matrix} z Co-ordinates of second end-point of second line * OR null if the calculation is for line and plane * @return {Array} Returns the point of intersection of lines/lines-planes */ var intersect = typed('intersect', { 'Array, Array, Array': function (x, y, plane) { if (!_3d(x)) { throw new TypeError('Array with 3 numbers expected for first argument'); } if (!_3d(y)) { throw new TypeError('Array with 3 numbers expected for second argument'); } if (!_4d(plane)) { throw new TypeError('Array with 4 numbers expected as third argument'); } return _intersectLinePlane(x[0], x[1], x[2], y[0], y[1], y[2], plane[0], plane[1], plane[2], plane[3]); }, 'Array, Array, Array, Array': function (w, x, y, z) { if (w.length === 2) { if (!_2d(w)) { throw new TypeError('Array with 2 numbers expected for first argument'); } if (!_2d(x)) { throw new TypeError('Array with 2 numbers expected for second argument'); } if (!_2d(y)) { throw new TypeError('Array with 2 numbers expected for third argument'); } if (!_2d(z)) { throw new TypeError('Array with 2 numbers expected for fourth argument'); } return _intersect2d(w, x, y, z); } else if (w.length === 3) { if (!_3d(w)) { throw new TypeError('Array with 3 numbers expected for first argument'); } if (!_3d(x)) { throw new TypeError('Array with 3 numbers expected for second argument'); } if (!_3d(y)) { throw new TypeError('Array with 3 numbers expected for third argument'); } if (!_3d(z)) { throw new TypeError('Array with 3 numbers expected for fourth argument'); } return _intersect3d(w[0], w[1], w[2], x[0], x[1], x[2], y[0], y[1], y[2], z[0], z[1], z[2]); } else { throw new TypeError('Arrays with two or thee dimensional points expected'); } }, 'Matrix, Matrix, Matrix': function (x, y, plane) { return matrix(intersect(x.valueOf(), y.valueOf(), plane.valueOf())); }, 'Matrix, Matrix, Matrix, Matrix': function (w, x, y, z) { // TODO: output matrix type should match input matrix type return matrix(intersect(w.valueOf(), x.valueOf(), y.valueOf(), z.valueOf())); } }); function _2d(x) { return x.length === 2 && typeof x[0] === 'number' && typeof x[1] === 'number'; } function _3d(x) { return x.length === 3 && typeof x[0] === 'number' && typeof x[1] === 'number' && typeof x[2] === 'number'; } function _4d(x) { return x.length === 4 && typeof x[0] === 'number' && typeof x[1] === 'number' && typeof x[2] === 'number' && typeof x[3] === 'number'; } function _intersect2d(p1a, p1b, p2a, p2b){ var o1 = p1a; var o2 = p2a; var d1 = subtract(o1, p1b); var d2 = subtract(o2, p2b); var det = d1[0]*d2[1] - d2[0]*d1[1]; if (abs(det) < config.epsilon) { return null; } var t = (d2[0]*o1[1] - d2[1]*o1[0] - d2[0]*o2[1] + d2[1]*o2[0]) / det; return add(multiply(d1, t), o1); } function _intersect3d(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4){ var d1343 = (x1 - x3)*(x4 - x3) + (y1 - y3)*(y4 - y3) + (z1 - z3)*(z4 - z3); var d4321 = (x4 - x3)*(x2 - x1) + (y4 - y3)*(y2 - y1) + (z4 - z3)*(z2 - z1); var d1321 = (x1 - x3)*(x2 - x1) + (y1 - y3)*(y2 - y1) + (z1 - z3)*(z2 - z1); var d4343 = (x4 - x3)*(x4 - x3) + (y4 - y3)*(y4 - y3) + (z4 - z3)*(z4 - z3); var d2121 = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1); var ta = ( d1343*d4321 - d1321*d4343 ) / ( d2121*d4343 - d4321*d4321 ); var tb = ( d1343 + ta * d4321 ) / (d4343); var pax = x1 + ta * (x2 - x1); var pay = y1 + ta * (y2 - y1); var paz = z1 + ta * (z2 - z1); var pbx = x3 + tb * (x4 - x3); var pby = y3 + tb * (y4 - y3); var pbz = z3 + tb * (z4 - z3); if (pax === pbx && pay === pby && paz === pbz){ return [pax, pay, paz]; } else{ return null; } } function _intersectLinePlane(x1, y1, z1, x2, y2, z2, x, y, z, c){ var t = (c - x1*x - y1*y - z1*z)/(x2*x + y2*y + z2*z - x1 - y1 - z1); var px = x1 + t * (x2 - x1); var py = y1 + t * (y2 - y1); var pz = z1 + t * (z2 - z1); return [px, py, pz]; // TODO: Add cases when line is parallel to the plane: // (a) no intersection, // (b) line contained in plane } return intersect; } exports.name = 'intersect'; exports.factory = factory; /***/ }), /* 398 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(341), __webpack_require__(367), __webpack_require__(382), __webpack_require__(389), __webpack_require__(393), __webpack_require__(396), __webpack_require__(400), __webpack_require__(407), __webpack_require__(414), __webpack_require__(422), __webpack_require__(426), __webpack_require__(427), __webpack_require__(433), __webpack_require__(453), __webpack_require__(460), __webpack_require__(463) ]; /***/ }), /* 399 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var zeros = load(__webpack_require__(37)); var not = load(__webpack_require__(104)); var isZero = load(__webpack_require__(122)); var algorithm02 = load(__webpack_require__(21)); var algorithm06 = load(__webpack_require__(57)); var algorithm11 = load(__webpack_require__(13)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Logical `and`. Test whether two values are both defined with a nonzero/nonempty value. * For matrices, the function is evaluated element wise. * * Syntax: * * math.and(x, y) * * Examples: * * math.and(2, 4); // returns true * * a = [2, 0, 0]; * b = [3, 7, 0]; * c = 0; * * math.and(a, b); // returns [true, false, false] * math.and(a, c); // returns [false, false, false] * * See also: * * not, or, xor * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check * @return {boolean | Array | Matrix} * Returns true when both inputs are defined with a nonzero/nonempty value. */ var and = typed('and', { 'number, number': function (x, y) { return !!(x && y); }, 'Complex, Complex': function (x, y) { return (x.re !== 0 || x.im !== 0) && (y.re !== 0 || y.im !== 0); }, 'BigNumber, BigNumber': function (x, y) { return !x.isZero() && !y.isZero() && !x.isNaN() && !y.isNaN(); }, 'Unit, Unit': function (x, y) { return and(x.value, y.value); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse & sparse c = algorithm06(x, y, and, false); break; default: // sparse & dense c = algorithm02(y, x, and, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense & sparse c = algorithm02(x, y, and, false); break; default: // dense & dense c = algorithm13(x, y, and); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return and(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return and(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return and(x, matrix(y)); }, 'Matrix, any': function (x, y) { // check scalar if (not(y)) { // return zero matrix return zeros(x.size(), x.storage()); } // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, and, false); break; default: c = algorithm14(x, y, and, false); break; } return c; }, 'any, Matrix': function (x, y) { // check scalar if (not(x)) { // return zero matrix return zeros(x.size(), x.storage()); } // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm11(y, x, and, true); break; default: c = algorithm14(y, x, and, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return and(matrix(x), y).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return and(x, matrix(y)).valueOf(); } }); and.toTex = { 2: '\\left(${args[0]}' + latex.operators['and'] + '${args[1]}\\right)' }; return and; } exports.name = 'and'; exports.factory = factory; /***/ }), /* 400 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(399), __webpack_require__(104), __webpack_require__(401), __webpack_require__(402) ]; /***/ }), /* 401 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm05 = load(__webpack_require__(56)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Logical `or`. Test if at least one value is defined with a nonzero/nonempty value. * For matrices, the function is evaluated element wise. * * Syntax: * * math.or(x, y) * * Examples: * * math.or(2, 4); // returns true * * a = [2, 5, 0]; * b = [0, 22, 0]; * c = 0; * * math.or(a, b); // returns [true, true, false] * math.or(b, c); // returns [false, true, false] * * See also: * * and, not, xor * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check * @return {boolean | Array | Matrix} * Returns true when one of the inputs is defined with a nonzero/nonempty value. */ var or = typed('or', { 'number, number': function (x, y) { return !!(x || y); }, 'Complex, Complex': function (x, y) { return (x.re !== 0 || x.im !== 0) || (y.re !== 0 || y.im !== 0); }, 'BigNumber, BigNumber': function (x, y) { return (!x.isZero() && !x.isNaN()) || (!y.isZero() && !y.isNaN()); }, 'Unit, Unit': function (x, y) { return or(x.value, y.value); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm05(x, y, or); break; default: // sparse + dense c = algorithm03(y, x, or, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, or, false); break; default: // dense + dense c = algorithm13(x, y, or); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return or(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return or(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return or(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, or, false); break; default: c = algorithm14(x, y, or, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, or, true); break; default: c = algorithm14(y, x, or, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, or, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, or, true).valueOf(); } }); or.toTex = { 2: '\\left(${args[0]}' + latex.operators['or'] + '${args[1]}\\right)' }; return or; } exports.name = 'or'; exports.factory = factory; /***/ }), /* 402 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Logical `xor`. Test whether one and only one value is defined with a nonzero/nonempty value. * For matrices, the function is evaluated element wise. * * Syntax: * * math.xor(x, y) * * Examples: * * math.xor(2, 4); // returns false * * a = [2, 0, 0]; * b = [2, 7, 0]; * c = 0; * * math.xor(a, b); // returns [false, true, false] * math.xor(a, c); // returns [true, false, false] * * See also: * * and, not, or * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check * @param {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check * @return {boolean | Array | Matrix} * Returns true when one and only one input is defined with a nonzero/nonempty value. */ var xor = typed('xor', { 'number, number': function (x, y) { return !!(!!x ^ !!y); }, 'Complex, Complex': function (x, y) { return ((x.re !== 0 || x.im !== 0) !== (y.re !== 0 || y.im !== 0)); }, 'BigNumber, BigNumber': function (x, y) { return ((!x.isZero() && !x.isNaN()) !== (!y.isZero() && !y.isNaN())); }, 'Unit, Unit': function (x, y) { return xor(x.value, y.value); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, xor); break; default: // sparse + dense c = algorithm03(y, x, xor, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, xor, false); break; default: // dense + dense c = algorithm13(x, y, xor); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return xor(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return xor(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return xor(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, xor, false); break; default: c = algorithm14(x, y, xor, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, xor, true); break; default: c = algorithm14(y, x, xor, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, xor, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, xor, true).valueOf(); } }); xor.toTex = { 2: '\\left(${args[0]}' + latex.operators['xor'] + '${args[1]}\\right)' }; return xor; } exports.name = 'xor'; exports.factory = factory; /***/ }), /* 403 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var array = __webpack_require__(7); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var subtract = load(__webpack_require__(18)); var multiply = load(__webpack_require__(10)); /** * Calculate the cross product for two vectors in three dimensional space. * The cross product of `A = [a1, a2, a3]` and `B = [b1, b2, b3]` is defined * as: * * cross(A, B) = [ * a2 * b3 - a3 * b2, * a3 * b1 - a1 * b3, * a1 * b2 - a2 * b1 * ] * * If one of the input vectors has a dimension greater than 1, the output * vector will be a 1x3 (2-dimensional) matrix. * * Syntax: * * math.cross(x, y) * * Examples: * * math.cross([1, 1, 0], [0, 1, 1]); // Returns [1, -1, 1] * math.cross([3, -3, 1], [4, 9, 2]); // Returns [-15, -2, 39] * math.cross([2, 3, 4], [5, 6, 7]); // Returns [-3, 6, -3] * math.cross([[1, 2, 3]], [[4], [5], [6]]); // Returns [[-3, 6, -3]] * * See also: * * dot, multiply * * @param {Array | Matrix} x First vector * @param {Array | Matrix} y Second vector * @return {Array | Matrix} Returns the cross product of `x` and `y` */ var cross = typed('cross', { 'Matrix, Matrix': function (x, y) { return matrix(_cross(x.toArray(), y.toArray())); }, 'Matrix, Array': function (x, y) { return matrix(_cross(x.toArray(), y)); }, 'Array, Matrix': function (x, y) { return matrix(_cross(x, y.toArray())); }, 'Array, Array': _cross }); cross.toTex = { 2: '\\left(${args[0]}\\right)\\times\\left(${args[1]}\\right)' }; return cross; /** * Calculate the cross product for two arrays * @param {Array} x First vector * @param {Array} y Second vector * @returns {Array} Returns the cross product of x and y * @private */ function _cross(x, y) { var highestDimension = Math.max(array.size(x).length, array.size(y).length); x = array.squeeze(x); y = array.squeeze(y); var xSize = array.size(x); var ySize = array.size(y); if (xSize.length != 1 || ySize.length != 1 || xSize[0] != 3 || ySize[0] != 3) { throw new RangeError('Vectors with length 3 expected ' + '(Size A = [' + xSize.join(', ') + '], B = [' + ySize.join(', ') + '])'); } var product = [ subtract(multiply(x[1], y[2]), multiply(x[2], y[1])), subtract(multiply(x[2], y[0]), multiply(x[0], y[2])), subtract(multiply(x[0], y[1]), multiply(x[1], y[0])) ]; if (highestDimension > 1) { return [product]; } else { return product; } } } exports.name = 'cross'; exports.factory = factory; /***/ }), /* 404 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var array = __webpack_require__(7); var clone = __webpack_require__(4).clone; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Create a diagonal matrix or retrieve the diagonal of a matrix * * When `x` is a vector, a matrix with vector `x` on the diagonal will be returned. * When `x` is a two dimensional matrix, the matrixes `k`th diagonal will be returned as vector. * When k is positive, the values are placed on the super diagonal. * When k is negative, the values are placed on the sub diagonal. * * Syntax: * * math.diag(X) * math.diag(X, format) * math.diag(X, k) * math.diag(X, k, format) * * Examples: * * // create a diagonal matrix * math.diag([1, 2, 3]); // returns [[1, 0, 0], [0, 2, 0], [0, 0, 3]] * math.diag([1, 2, 3], 1); // returns [[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3]] * math.diag([1, 2, 3], -1); // returns [[0, 0, 0], [1, 0, 0], [0, 2, 0], [0, 0, 3]] * * // retrieve the diagonal from a matrix * var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; * math.diag(a); // returns [1, 5, 9] * * See also: * * ones, zeros, eye * * @param {Matrix | Array} x A two dimensional matrix or a vector * @param {number | BigNumber} [k=0] The diagonal where the vector will be filled * in or retrieved. * @param {string} [format='dense'] The matrix storage format. * * @returns {Matrix | Array} Diagonal matrix from input vector, or diagonal from input matrix. */ var diag = typed('diag', { // FIXME: simplify this huge amount of signatures as soon as typed-function supports optional arguments 'Array': function (x) { return _diag(x, 0, array.size(x), null); }, 'Array, number': function (x, k) { return _diag(x, k, array.size(x), null); }, 'Array, BigNumber': function (x, k) { return _diag(x, k.toNumber(), array.size(x), null); }, 'Array, string': function (x, format) { return _diag(x, 0, array.size(x), format); }, 'Array, number, string': function (x, k, format) { return _diag(x, k, array.size(x), format); }, 'Array, BigNumber, string': function (x, k, format) { return _diag(x, k.toNumber(), array.size(x), format); }, 'Matrix': function (x) { return _diag(x, 0, x.size(), x.storage()); }, 'Matrix, number': function (x, k) { return _diag(x, k, x.size(), x.storage()); }, 'Matrix, BigNumber': function (x, k) { return _diag(x, k.toNumber(), x.size(), x.storage()); }, 'Matrix, string': function (x, format) { return _diag(x, 0, x.size(), format); }, 'Matrix, number, string': function (x, k, format) { return _diag(x, k, x.size(), format); }, 'Matrix, BigNumber, string': function (x, k, format) { return _diag(x, k.toNumber(), x.size(), format); } }); diag.toTex = undefined; // use default template return diag; /** * Creeate diagonal matrix from a vector or vice versa * @param {Array | Matrix} x * @param {number} k * @param {string} format Storage format for matrix. If null, * an Array is returned * @returns {Array | Matrix} * @private */ function _diag (x, k, size, format) { if (!isInteger(k)) { throw new TypeError ('Second parameter in function diag must be an integer'); } var kSuper = k > 0 ? k : 0; var kSub = k < 0 ? -k : 0; // check dimensions switch (size.length) { case 1: return _createDiagonalMatrix(x, k, format, size[0], kSub, kSuper); case 2: return _getDiagonal(x, k, format, size, kSub, kSuper); } throw new RangeError('Matrix for function diag must be 2 dimensional'); } function _createDiagonalMatrix(x, k, format, l, kSub, kSuper) { // matrix size var ms = [l + kSub, l + kSuper]; // get matrix constructor var F = type.Matrix.storage(format || 'dense'); // create diagonal matrix var m = F.diagonal(ms, x, k); // check we need to return a matrix return format !== null ? m : m.valueOf(); } function _getDiagonal(x, k, format, s, kSub, kSuper) { // check x is a Matrix if (x && x.isMatrix === true) { // get diagonal matrix var dm = x.diagonal(k); // check we need to return a matrix if (format !== null) { // check we need to change matrix format if (format !== dm.storage()) return matrix(dm, format); return dm; } return dm.valueOf(); } // vector size var n = Math.min(s[0] - kSub, s[1] - kSuper); // diagonal values var vector = []; // loop diagonal for (var i = 0; i < n; i++) { vector[i] = x[i + kSub][i + kSuper]; } // check we need to return a matrix return format !== null ? matrix(vector) : vector; } } exports.name = 'diag'; exports.factory = factory; /***/ }), /* 405 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var size = __webpack_require__(7).size; function factory (type, config, load, typed) { var add = load(__webpack_require__(15)); var multiply = load(__webpack_require__(10)); /** * Calculate the dot product of two vectors. The dot product of * `A = [a1, a2, a3, ..., an]` and `B = [b1, b2, b3, ..., bn]` is defined as: * * dot(A, B) = a1 * b1 + a2 * b2 + a3 * b3 + ... + an * bn * * Syntax: * * math.dot(x, y) * * Examples: * * math.dot([2, 4, 1], [2, 2, 3]); // returns number 15 * math.multiply([2, 4, 1], [2, 2, 3]); // returns number 15 * * See also: * * multiply, cross * * @param {Array | Matrix} x First vector * @param {Array | Matrix} y Second vector * @return {number} Returns the dot product of `x` and `y` */ var dot = typed('dot', { 'Matrix, Matrix': function (x, y) { return _dot(x.toArray(), y.toArray()); }, 'Matrix, Array': function (x, y) { return _dot(x.toArray(), y); }, 'Array, Matrix': function (x, y) { return _dot(x, y.toArray()); }, 'Array, Array': _dot }); dot.toTex = {2: '\\left(${args[0]}\\cdot${args[1]}\\right)'}; return dot; /** * Calculate the dot product for two arrays * @param {Array} x First vector * @param {Array} y Second vector * @returns {number} Returns the dot product of x and y * @private */ // TODO: double code with math.multiply function _dot(x, y) { var xSize= size(x); var ySize = size(y); var len = xSize[0]; if (xSize.length !== 1 || ySize.length !== 1) throw new RangeError('Vector expected'); // TODO: better error message if (xSize[0] != ySize[0]) throw new RangeError('Vectors must have equal length (' + xSize[0] + ' != ' + ySize[0] + ')'); if (len == 0) throw new RangeError('Cannot calculate the dot product of empty vectors'); var prod = 0; for (var i = 0; i < len; i++) { prod = add(prod, multiply(x[i], y[i])); } return prod; } } exports.name = 'dot'; exports.factory = factory; /***/ }), /* 406 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var clone = __webpack_require__(4).clone; var _flatten = __webpack_require__(7).flatten; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Flatten a multi dimensional matrix into a single dimensional matrix. * * Syntax: * * math.flatten(x) * * Examples: * * math.flatten([[1,2], [3,4]]); // returns [1, 2, 3, 4] * * See also: * * concat, resize, size, squeeze * * @param {Matrix | Array} x Matrix to be flattened * @return {Matrix | Array} Returns the flattened matrix */ var flatten = typed('flatten', { 'Array': function (x) { return _flatten(clone(x)); }, 'Matrix': function (x) { var flat = _flatten(clone(x.toArray())); // TODO: return the same matrix type as x return matrix(flat); } }); flatten.toTex = undefined; // use default template return flatten; } exports.name = 'flatten'; exports.factory = factory; /***/ }), /* 407 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(105), __webpack_require__(403), __webpack_require__(106), __webpack_require__(404), __webpack_require__(405), __webpack_require__(66), __webpack_require__(107), __webpack_require__(406), __webpack_require__(108), __webpack_require__(109), __webpack_require__(408), __webpack_require__(67), __webpack_require__(409), __webpack_require__(68), __webpack_require__(110), __webpack_require__(410), __webpack_require__(411), __webpack_require__(412), __webpack_require__(413), __webpack_require__(51), __webpack_require__(111), __webpack_require__(52), __webpack_require__(37) ]; /***/ }), /* 408 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var size = __webpack_require__(7).size; function factory(type, config, load, typed) { var matrix = load(__webpack_require__(1)); var multiplyScalar = load(__webpack_require__(20)) /** * Calculates the kronecker product of 2 matrices or vectors. * * NOTE: If a one dimensional vector / matrix is given, it will be * wrapped so its two dimensions. * See the examples. * * Syntax: * * math.kron(x, y) * * Examples: * * math.kron([[1, 0], [0, 1]], [[1, 2], [3, 4]]); * // returns [ [ 1, 2, 0, 0 ], [ 3, 4, 0, 0 ], [ 0, 0, 1, 2 ], [ 0, 0, 3, 4 ] ] * * math.kron([1,1], [2,3,4]); * // returns [ [ 2, 3, 4, 2, 3, 4 ] ] * * See also: * * multiply, dot, cross * * @param {Array | Matrix} x First vector * @param {Array | Matrix} y Second vector * @return {Array | Matrix} Returns the kronecker product of `x` and `y` */ var kron = typed('kron', { 'Matrix, Matrix': function(x, y) { return matrix(_kron(x.toArray(), y.toArray())); }, 'Matrix, Array': function(x, y) { return matrix(_kron(x.toArray(), y)); }, 'Array, Matrix': function(x, y) { return matrix(_kron(x, y.toArray())); }, 'Array, Array': _kron }); return kron; /** * Calculate the kronecker product of two matrices / vectors * @param {Array} a First vector * @param {Array} b Second vector * @returns {Array} Returns the kronecker product of x and y * @private */ function _kron(a, b) { // Deal with the dimensions of the matricies. if (size(a).length === 1) { // Wrap it in a 2D Matrix a = [a]; } if (size(b).length === 1) { // Wrap it in a 2D Matrix b = [b] } if (size(a).length > 2 || size(b).length > 2) { throw new RangeError('Vectors with dimensions greater then 2 are not supported expected ' + '(Size x = ' + JSON.stringify(a.length) + ', y = ' + JSON.stringify(b.length) + ')'); } var t = []; var r = []; return a.map(function(a) { return b.map(function(b) { return a.map(function(y) { return b.map(function(x) { return r.push(multiplyScalar(y, x)); }); }, t.push(r = [])); }); }, t = []) && t; } } exports.name = 'kron'; exports.factory = factory; /***/ }), /* 409 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var resize = __webpack_require__(7).resize; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Create a matrix filled with ones. The created matrix can have one or * multiple dimensions. * * Syntax: * * math.ones(m) * math.ones(m, format) * math.ones(m, n) * math.ones(m, n, format) * math.ones([m, n]) * math.ones([m, n], format) * math.ones([m, n, p, ...]) * math.ones([m, n, p, ...], format) * * Examples: * * math.ones(3); // returns [1, 1, 1] * math.ones(3, 2); // returns [[1, 1], [1, 1], [1, 1]] * math.ones(3, 2, 'dense'); // returns Dense Matrix [[1, 1], [1, 1], [1, 1]] * * var A = [[1, 2, 3], [4, 5, 6]]; * math.ones(math.size(A)); // returns [[1, 1, 1], [1, 1, 1]] * * See also: * * zeros, eye, size, range * * @param {...number | Array} size The size of each dimension of the matrix * @param {string} [format] The Matrix storage format * * @return {Array | Matrix | number} A matrix filled with ones */ var ones = typed('ones', { '': function () { return (config.matrix === 'Array') ? _ones([]) : _ones([], 'default'); }, // math.ones(m, n, p, ..., format) // TODO: more accurate signature '...number | BigNumber, string' as soon as typed-function supports this '...number | BigNumber | string': function (size) { var last = size[size.length - 1]; if (typeof last === 'string') { var format = size.pop(); return _ones(size, format); } else if (config.matrix === 'Array') { return _ones(size); } else { return _ones(size, 'default'); } }, 'Array': _ones, 'Matrix': function (size) { var format = size.storage(); return _ones(size.valueOf(), format); }, 'Array | Matrix, string': function (size, format) { return _ones (size.valueOf(), format); } }); ones.toTex = undefined; // use default template return ones; /** * Create an Array or Matrix with ones * @param {Array} size * @param {string} [format='default'] * @return {Array | Matrix} * @private */ function _ones(size, format) { var hasBigNumbers = _normalize(size); var defaultValue = hasBigNumbers ? new type.BigNumber(1) : 1; _validate(size); if (format) { // return a matrix var m = matrix(format); if (size.length > 0) { return m.resize(size, defaultValue); } return m; } else { // return an Array var arr = []; if (size.length > 0) { return resize(arr, size, defaultValue); } return arr; } } // replace BigNumbers with numbers, returns true if size contained BigNumbers function _normalize(size) { var hasBigNumbers = false; size.forEach(function (value, index, arr) { if (value && value.isBigNumber === true) { hasBigNumbers = true; arr[index] = value.toNumber(); } }); return hasBigNumbers; } // validate arguments function _validate (size) { size.forEach(function (value) { if (typeof value !== 'number' || !isInteger(value) || value < 0) { throw new Error('Parameters in function ones must be positive integers'); } }); } } exports.name = 'ones'; exports.factory = factory; /***/ }), /* 410 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var DimensionError = __webpack_require__(9); var ArgumentsError = __webpack_require__(41); var isInteger = __webpack_require__(2).isInteger; var format = __webpack_require__(19).format; var clone = __webpack_require__(4).clone; var array = __webpack_require__(7); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Resize a matrix * * Syntax: * * math.resize(x, size) * math.resize(x, size, defaultValue) * * Examples: * * math.resize([1, 2, 3, 4, 5], [3]); // returns Array [1, 2, 3] * math.resize([1, 2, 3], [5], 0); // returns Array [1, 2, 3, 0, 0] * math.resize(2, [2, 3], 0); // returns Matrix [[2, 0, 0], [0, 0, 0]] * math.resize("hello", [8], "!"); // returns string 'hello!!!' * * See also: * * size, squeeze, subset * * @param {Array | Matrix | *} x Matrix to be resized * @param {Array | Matrix} size One dimensional array with numbers * @param {number | string} [defaultValue=0] Zero by default, except in * case of a string, in that case * defaultValue = ' ' * @return {* | Array | Matrix} A resized clone of matrix `x` */ // TODO: rework resize to a typed-function var resize = function resize (x, size, defaultValue) { if (arguments.length != 2 && arguments.length != 3) { throw new ArgumentsError('resize', arguments.length, 2, 3); } if (size && size.isMatrix === true) { size = size.valueOf(); // get Array } if (size.length && size[0] && size[0].isBigNumber === true) { // convert bignumbers to numbers size = size.map(function (value) { return (value && value.isBigNumber === true) ? value.toNumber() : value; }); } // check x is a Matrix if (x && x.isMatrix === true) { // use optimized matrix implementation, return copy return x.resize(size, defaultValue, true); } if (typeof x === 'string') { // resize string return _resizeString(x, size, defaultValue); } // check result should be a matrix var asMatrix = Array.isArray(x) ? false : (config.matrix !== 'Array'); if (size.length == 0) { // output a scalar while (Array.isArray(x)) { x = x[0]; } return clone(x); } else { // output an array/matrix if (!Array.isArray(x)) { x = [x]; } x = clone(x); var res = array.resize(x, size, defaultValue); return asMatrix ? matrix(res) : res; } }; resize.toTex = undefined; // use default template return resize; /** * Resize a string * @param {string} str * @param {number[]} size * @param {string} [defaultChar=' '] * @private */ function _resizeString(str, size, defaultChar) { if (defaultChar !== undefined) { if (typeof defaultChar !== 'string' || defaultChar.length !== 1) { throw new TypeError('Single character expected as defaultValue'); } } else { defaultChar = ' '; } if (size.length !== 1) { throw new DimensionError(size.length, 1); } var len = size[0]; if (typeof len !== 'number' || !isInteger(len)) { throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + format(size) + ')'); } if (str.length > len) { return str.substring(0, len); } else if (str.length < len) { var res = str; for (var i = 0, ii = len - str.length; i < ii; i++) { res += defaultChar; } return res; } else { return str; } } } exports.name = 'resize'; exports.factory = factory; /***/ }), /* 411 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var array = __webpack_require__(7); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Calculate the size of a matrix or scalar. * * Syntax: * * math.size(x) * * Examples: * * math.size(2.3); // returns [] * math.size('hello world'); // returns [11] * * var A = [[1, 2, 3], [4, 5, 6]]; * math.size(A); // returns [2, 3] * math.size(math.range(1,6)); // returns [5] * * See also: * * resize, squeeze, subset * * @param {boolean | number | Complex | Unit | string | Array | Matrix} x A matrix * @return {Array | Matrix} A vector with size of `x`. */ var size = typed('size', { 'Matrix': function (x) { // TODO: return the same matrix type as the input return matrix(x.size()); }, 'Array': array.size, 'string': function (x) { return (config.matrix === 'Array') ? [x.length] : matrix([x.length]); }, 'number | Complex | BigNumber | Unit | boolean | null': function (x) { // scalar return (config.matrix === 'Array') ? [] : matrix([]); } }); size.toTex = undefined; // use default template return size; } exports.name = 'size'; exports.factory = factory; /***/ }), /* 412 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var size = __webpack_require__(7).size; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var asc = load(__webpack_require__(46)); var desc = function (a, b) { return -asc(a, b); }; /** * Sort the items in a matrix. * * Syntax: * * math.sort(x) * math.sort(x, compare) * * Examples: * * math.sort([5, 10, 1]); // returns [1, 5, 10] * math.sort(['C', 'B', 'A', 'D']); // returns ['A', 'B', 'C', 'D'] * * function sortByLength (a, b) { * return a.length - b.length; * } * math.sort(['Langdon', 'Tom', 'Sara'], sortByLength); // returns ['Tom', 'Sara', 'Langdon'] * * See also: * * filter, forEach, map * * @param {Matrix | Array} x A one dimensional matrix or array to sort * @param {Function | 'asc' | 'desc'} [compare='asc'] * An optional _comparator function. The function is called as * `compare(a, b)`, and must return 1 when a > b, -1 when a < b, * and 0 when a == b. * @return {Matrix | Array} Returns the sorted matrix. */ var sort = typed('sort', { 'Array': function (x) { _arrayIsVector(x); return x.sort(asc); }, 'Matrix': function (x) { _matrixIsVector(x); return matrix(x.toArray().sort(asc), x.storage()); }, 'Array, function': function (x, _comparator) { _arrayIsVector(x); return x.sort(_comparator); }, 'Matrix, function': function (x, _comparator) { _matrixIsVector(x); return matrix(x.toArray().sort(_comparator), x.storage()); }, 'Array, string': function (x, order) { _arrayIsVector(x); return x.sort(_comparator(order)); }, 'Matrix, string': function (x, order) { _matrixIsVector(x); return matrix(x.toArray().sort(_comparator(order)), x.storage()); } }); sort.toTex = undefined; // use default template /** * Get the comparator for given order ('asc' or 'desc') * @param {'asc' | 'desc'} order * @return {Function} Returns a _comparator function */ function _comparator (order) { if (order === 'asc') { return asc; } else if (order === 'desc') { return desc; } else { throw new Error('String "asc" or "desc" expected'); } } /** * Validate whether an array is one dimensional * Throws an error when this is not the case * @param {Array} array * @private */ function _arrayIsVector (array) { if (size(array).length !== 1) { throw new Error('One dimensional array expected'); } } /** * Validate whether a matrix is one dimensional * Throws an error when this is not the case * @param {Matrix} matrix * @private */ function _matrixIsVector (matrix) { if (matrix.size().length !== 1) { throw new Error('One dimensional matrix expected'); } } return sort; } exports.name = 'sort'; exports.factory = factory; /***/ }), /* 413 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var object = __webpack_require__(4); var array = __webpack_require__(7); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); /** * Squeeze a matrix, remove inner and outer singleton dimensions from a matrix. * * Syntax: * * math.squeeze(x) * * Examples: * * math.squeeze([3]); // returns 3 * math.squeeze([[3]]); // returns 3 * * var A = math.zeros(3, 1); // returns [[0], [0], [0]] (size 3x1) * math.squeeze(A); // returns [0, 0, 0] (size 3) * * var B = math.zeros(1, 3); // returns [[0, 0, 0]] (size 1x3) * math.squeeze(B); // returns [0, 0, 0] (size 3) * * // only inner and outer dimensions are removed * var C = math.zeros(2, 1, 3); // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3) * math.squeeze(C); // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3) * * See also: * * subset * * @param {Matrix | Array} x Matrix to be squeezed * @return {Matrix | Array} Squeezed matrix */ var squeeze = typed('squeeze', { 'Array': function (x) { return array.squeeze(object.clone(x)); }, 'Matrix': function (x) { var res = array.squeeze(x.toArray()); // FIXME: return the same type of matrix as the input return Array.isArray(res) ? matrix(res) : res; }, 'any': function (x) { // scalar return object.clone(x); } }); squeeze.toTex = undefined; // use default template return squeeze; } exports.name = 'squeeze'; exports.factory = factory; /***/ }), /* 414 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ //require('./distribution'), // TODO: rethink math.distribution __webpack_require__(53), __webpack_require__(54), __webpack_require__(112), __webpack_require__(415), __webpack_require__(416), __webpack_require__(417), __webpack_require__(418), __webpack_require__(419), __webpack_require__(420) ]; /***/ }), /* 415 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory(type, config, load, typed) { var matrix = load(__webpack_require__(1)); var divide = load(__webpack_require__(34)); var sum = load(__webpack_require__(118)); var multiply = load(__webpack_require__(10)); var dotDivide = load(__webpack_require__(100)); var log = load(__webpack_require__(102)); var isNumeric = load(__webpack_require__(71)); /** * Calculate the Kullback-Leibler (KL) divergence between two distributions * * Syntax: * * math.kldivergence(x, y) * * Examples: * * math.kldivergence([0.7,0.5,0.4], [0.2,0.9,0.5]); //returns 0.24376698773121153 * * * @param {Array | Matrix} q First vector * @param {Array | Matrix} p Second vector * @return {number} Returns distance between q and p */ var kldivergence = typed('kldivergence', { 'Array, Array': function(q, p) { return _kldiv(matrix(q), matrix(p)); }, 'Matrix, Array': function(q, p) { return _kldiv(q, matrix(p)); }, 'Array, Matrix': function(q, p){ return _kldiv(matrix(q), p); }, 'Matrix, Matrix': function(q, p){ return _kldiv(q, p); } }); function _kldiv(q, p) { var plength = p.size().length; var qlength = q.size().length; if (plength > 1) { throw new Error('first object must be one dimensional'); } if (qlength > 1) { throw new Error('second object must be one dimensional'); } if(plength !== qlength){ throw new Error("Length of two vectors must be equal"); } //Before calculation, apply normalization var sumq = sum(q); if (sumq === 0) { throw new Error("Sum of elements in first object must be non zero"); } var sump = sum(p); if (sump === 0) { throw new Error("Sum of elements in second object must be non zero"); } var qnorm = divide(q, sum(q)); var pnorm = divide(p, sum(p)); var result = sum(multiply(qnorm, log(dotDivide(qnorm, pnorm)))); if (isNumeric(result)) { return result; } else { return Number.NaN; } } return kldivergence; } exports.name = 'kldivergence'; exports.factory = factory; /***/ }), /* 416 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepForEach = __webpack_require__(32); function factory (type, config, load, typed) { var add = load(__webpack_require__(15)); var multiply = load(__webpack_require__(10)); var divide = load(__webpack_require__(34)); var factorial = load(__webpack_require__(54)); var isInteger = load(__webpack_require__(39)); var isPositive = load(__webpack_require__(55)); /** * Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities. * * multinomial takes one array of integers as an argument. * The following condition must be enforced: every ai <= 0 * * Syntax: * * math.multinomial(a) // a is an array type * * Examples: * * math.multinomial([1,2,1]); // returns 12 * * See also: * * combinations, factorial * * @param {number[] | BigNumber[]} a Integer numbers of objects in the subset * @return {Number | BigNumber} Multinomial coefficient. */ return typed('multinomial', { 'Array | Matrix': function (a) { var sum = 0; var denom = 1; deepForEach(a, function(ai) { if(!isInteger(ai) || !isPositive(ai)) { throw new TypeError('Positive integer value expected in function multinomial'); } sum = add(sum, ai); denom = multiply(denom, factorial(ai)); }); return divide(factorial(sum), denom); } }); } exports.name = 'multinomial'; exports.factory = factory; /***/ }), /* 417 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; function factory (type, config, load, typed) { var factorial = load(__webpack_require__(54)); /** * Compute the number of ways of obtaining an ordered subset of `k` elements * from a set of `n` elements. * * Permutations only takes integer arguments. * The following condition must be enforced: k <= n. * * Syntax: * * math.permutations(n) * math.permutations(n, k) * * Examples: * * math.permutations(5); // 120 * math.permutations(5, 3); // 60 * * See also: * * combinations, factorial * * @param {number | BigNumber} n The number of objects in total * @param {number | BigNumber} [k] The number of objects in the subset * @return {number | BigNumber} The number of permutations */ var permutations = typed('permutations', { 'number | BigNumber': factorial, 'number, number': function (n, k) { var result, i; if (!isInteger(n) || n < 0) { throw new TypeError('Positive integer value expected in function permutations'); } if (!isInteger(k) || k < 0) { throw new TypeError('Positive integer value expected in function permutations'); } if (k > n) { throw new TypeError('second argument k must be less than or equal to first argument n'); } // Permute n objects, k at a time result = 1; for (i = n - k + 1; i <= n; i++) { result = result * i; } return result; }, 'BigNumber, BigNumber': function (n, k) { var result, i; if (!isPositiveInteger(n) || !isPositiveInteger(k)) { throw new TypeError('Positive integer value expected in function permutations'); } if (k.gt(n)) { throw new TypeError('second argument k must be less than or equal to first argument n'); } result = new type.BigNumber(1); for (i = n.minus(k).plus(1); i.lte(n); i = i.plus(1)) { result = result.times(i); } return result; } // TODO: implement support for collection in permutations }); permutations.toTex = undefined; // use default template return permutations; } /** * Test whether BigNumber n is a positive integer * @param {BigNumber} n * @returns {boolean} isPositiveInteger */ function isPositiveInteger(n) { return n.isInteger() && n.gte(0); } exports.name = 'permutations'; exports.factory = factory; /***/ }), /* 418 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var distribution = load(__webpack_require__(69)); /** * Random pick one or more values from a one dimensional array. * Array elements are picked using a random function with uniform or weighted distribution. * * Syntax: * * math.pickRandom(array) * math.pickRandom(array, number) * math.pickRandom(array, weights) * math.pickRandom(array, number, weights) * math.pickRandom(array, weights, number) * * Examples: * * math.pickRandom([3, 6, 12, 2]); // returns one of the values in the array * math.pickRandom([3, 6, 12, 2], 2); // returns an array of two of the values in the array * math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]); // returns one of the values in the array with weighted distribution * math.pickRandom([3, 6, 12, 2], 2, [1, 3, 2, 1]); // returns an array of two of the values in the array with weighted distribution * math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1], 2); // returns an array of two of the values in the array with weighted distribution * * See also: * * random, randomInt * * @param {Array} array A one dimensional array * @param {Int} number An int or float * @param {Array} weights An array of ints or floats * @return {number} One of the elements of the provided input array * @return {array} An array of elements of the provided input array */ // TODO: rework pickRandom to a typed-function var pickRandom = distribution('uniform').pickRandom; pickRandom.toTex = undefined; // use default template return pickRandom; } exports.name = 'pickRandom'; exports.factory = factory; /***/ }), /* 419 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var distribution = load(__webpack_require__(69)); /** * Return a random number larger or equal to `min` and smaller than `max` * using a uniform distribution. * * Syntax: * * math.random() // generate a random number between 0 and 1 * math.random(max) // generate a random number between 0 and max * math.random(min, max) // generate a random number between min and max * math.random(size) // generate a matrix with random numbers between 0 and 1 * math.random(size, max) // generate a matrix with random numbers between 0 and max * math.random(size, min, max) // generate a matrix with random numbers between min and max * * Examples: * * math.random(); // returns a random number between 0 and 1 * math.random(100); // returns a random number between 0 and 100 * math.random(30, 40); // returns a random number between 30 and 40 * math.random([2, 3]); // returns a 2x3 matrix with random numbers between 0 and 1 * * See also: * * randomInt, pickRandom * * @param {Array | Matrix} [size] If provided, an array or matrix with given * size and filled with random values is returned * @param {number} [min] Minimum boundary for the random value, included * @param {number} [max] Maximum boundary for the random value, excluded * @return {number | Array | Matrix} A random number */ // TODO: rework random to a typed-function var random = distribution('uniform').random; random.toTex = undefined; // use default template return random; } exports.name = 'random'; exports.factory = factory; /***/ }), /* 420 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var distribution = load(__webpack_require__(69)); /** * Return a random integer number larger or equal to `min` and smaller than `max` * using a uniform distribution. * * Syntax: * * math.randomInt(max) // generate a random integer between 0 and max * math.randomInt(min, max) // generate a random integer between min and max * math.randomInt(size) // generate a matrix with random integer between 0 and 1 * math.randomInt(size, max) // generate a matrix with random integer between 0 and max * math.randomInt(size, min, max) // generate a matrix with random integer between min and max * * Examples: * * math.randomInt(100); // returns a random integer between 0 and 100 * math.randomInt(30, 40); // returns a random integer between 30 and 40 * math.randomInt([2, 3]); // returns a 2x3 matrix with random integers between 0 and 1 * * See also: * * random, pickRandom * * @param {Array | Matrix} [size] If provided, an array or matrix with given * size and filled with random values is returned * @param {number} [min] Minimum boundary for the random value, included * @param {number} [max] Maximum boundary for the random value, excluded * @return {number | Array | Matrix} A random integer value */ // TODO: rework randomInt to a typed-function var randomInt = distribution('uniform').randomInt; randomInt.toTex = undefined; // use default template return randomInt; } exports.name = 'randomInt'; exports.factory = factory; /***/ }), /* 421 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var equal = load(__webpack_require__(70)); /** * Test element wise whether two matrices are equal. * The function accepts both matrices and scalar values. * * Syntax: * * math.deepEqual(x, y) * * Examples: * * math.deepEqual(2, 4); // returns false * * a = [2, 5, 1]; * b = [2, 7, 1]; * * math.deepEqual(a, b); // returns false * math.equal(a, b); // returns [true, false, true] * * See also: * * equal, unequal * * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First matrix to compare * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second matrix to compare * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} * Returns true when the input matrices have the same size and each of their elements is equal. */ var deepEqual = typed('deepEqual', { 'any, any': function (x, y) { return _deepEqual(x.valueOf(), y.valueOf()); } }); deepEqual.toTex = undefined; // use default template return deepEqual; /** * Test whether two arrays have the same size and all elements are equal * @param {Array | *} x * @param {Array | *} y * @return {boolean} Returns true if both arrays are deep equal */ function _deepEqual(x, y) { if (Array.isArray(x)) { if (Array.isArray(y)) { var len = x.length; if (len !== y.length) { return false; } for (var i = 0; i < len; i++) { if (!_deepEqual(x[i], y[i])) { return false; } } return true; } else { return false; } } else { if (Array.isArray(y)) { return false; } else { return equal(x, y); } } } } exports.name = 'deepEqual'; exports.factory = factory; /***/ }), /* 422 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(46), __webpack_require__(421), __webpack_require__(70), __webpack_require__(26), __webpack_require__(113), __webpack_require__(38), __webpack_require__(423), __webpack_require__(424) ]; /***/ }), /* 423 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var nearlyEqual = __webpack_require__(2).nearlyEqual; var bigNearlyEqual = __webpack_require__(31); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); var latex = __webpack_require__(3); /** * Test whether value x is smaller or equal to y. * * The function returns true when x is smaller than y or the relative * difference between x and y is smaller than the configured epsilon. The * function cannot be used to compare values smaller than approximately 2.22e-16. * For matrices, the function is evaluated element wise. * * Syntax: * * math.smallerEq(x, y) * * Examples: * * math.smaller(1 + 2, 3); // returns false * math.smallerEq(1 + 2, 3); // returns true * * See also: * * equal, unequal, smaller, larger, largerEq, compare * * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare * @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false */ var smallerEq = typed('smallerEq', { 'boolean, boolean': function (x, y) { return x <= y; }, 'number, number': function (x, y) { return x <= y || nearlyEqual(x, y, config.epsilon); }, 'BigNumber, BigNumber': function (x, y) { return x.lte(y) || bigNearlyEqual(x, y, config.epsilon); }, 'Fraction, Fraction': function (x, y) { return x.compare(y) !== 1; }, 'Complex, Complex': function () { throw new TypeError('No ordering relation is defined for complex numbers'); }, 'Unit, Unit': function (x, y) { if (!x.equalBase(y)) { throw new Error('Cannot compare units with different base'); } return smallerEq(x.value, y.value); }, 'string, string': function (x, y) { return x <= y; }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, smallerEq); break; default: // sparse + dense c = algorithm03(y, x, smallerEq, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, smallerEq, false); break; default: // dense + dense c = algorithm13(x, y, smallerEq); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return smallerEq(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return smallerEq(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return smallerEq(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, smallerEq, false); break; default: c = algorithm14(x, y, smallerEq, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, smallerEq, true); break; default: c = algorithm14(y, x, smallerEq, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, smallerEq, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, smallerEq, true).valueOf(); } }); smallerEq.toTex = { 2: '\\left(${args[0]}' + latex.operators['smallerEq'] + '${args[1]}\\right)' }; return smallerEq; } exports.name = 'smallerEq'; exports.factory = factory; /***/ }), /* 424 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var nearlyEqual = __webpack_require__(2).nearlyEqual; var bigNearlyEqual = __webpack_require__(31); function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm03 = load(__webpack_require__(12)); var algorithm07 = load(__webpack_require__(22)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); var latex = __webpack_require__(3); /** * Test whether two values are unequal. * * The function tests whether the relative difference between x and y is * larger than the configured epsilon. The function cannot be used to compare * values smaller than approximately 2.22e-16. * * For matrices, the function is evaluated element wise. * In case of complex numbers, x.re must unequal y.re, or x.im must unequal y.im. * * Values `null` and `undefined` are compared strictly, thus `null` is unequal * with everything except `null`, and `undefined` is unequal with everying * except. `undefined`. * * Syntax: * * math.unequal(x, y) * * Examples: * * math.unequal(2 + 2, 3); // returns true * math.unequal(2 + 2, 4); // returns false * * var a = math.unit('50 cm'); * var b = math.unit('5 m'); * math.unequal(a, b); // returns false * * var c = [2, 5, 1]; * var d = [2, 7, 1]; * * math.unequal(c, d); // returns [false, true, false] * math.deepEqual(c, d); // returns false * * math.unequal(0, null); // returns true * See also: * * equal, deepEqual, smaller, smallerEq, larger, largerEq, compare * * @param {number | BigNumber | Fraction | boolean | Complex | Unit | string | Array | Matrix | undefined} x First value to compare * @param {number | BigNumber | Fraction | boolean | Complex | Unit | string | Array | Matrix | undefined} y Second value to compare * @return {boolean | Array | Matrix} Returns true when the compared values are unequal, else returns false */ var unequal = typed('unequal', { 'any, any': function (x, y) { // strict equality for null and undefined? if (x === null) { return y !== null; } if (y === null) { return x !== null; } if (x === undefined) { return y !== undefined; } if (y === undefined) { return x !== undefined; } return _unequal(x, y); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse + sparse c = algorithm07(x, y, _unequal); break; default: // sparse + dense c = algorithm03(y, x, _unequal, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense + sparse c = algorithm03(x, y, _unequal, false); break; default: // dense + dense c = algorithm13(x, y, _unequal); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return unequal(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return unequal(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return unequal(x, matrix(y)); }, 'Matrix, any': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm12(x, y, _unequal, false); break; default: c = algorithm14(x, y, _unequal, false); break; } return c; }, 'any, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, _unequal, true); break; default: c = algorithm14(y, x, _unequal, true); break; } return c; }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, _unequal, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, _unequal, true).valueOf(); } }); var _unequal = typed('_unequal', { 'boolean, boolean': function (x, y) { return x !== y; }, 'number, number': function (x, y) { return !nearlyEqual(x, y, config.epsilon); }, 'BigNumber, BigNumber': function (x, y) { return !bigNearlyEqual(x, y, config.epsilon); }, 'Fraction, Fraction': function (x, y) { return !x.equals(y); }, 'Complex, Complex': function (x, y) { return !x.equals(y); }, 'Unit, Unit': function (x, y) { if (!x.equalBase(y)) { throw new Error('Cannot compare units with different base'); } return unequal(x.value, y.value); }, 'string, string': function (x, y) { return x !== y; } }); unequal.toTex = { 2: '\\left(${args[0]}' + latex.operators['unequal'] + '${args[1]}\\right)' }; return unequal; } exports.name = 'unequal'; exports.factory = factory; /***/ }), /* 425 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var sign = __webpack_require__(2).sign; function factory (type, config, load, typed) { /** * Compute the erf function of a value using a rational Chebyshev * approximations for different intervals of x. * * This is a translation of W. J. Cody's Fortran implementation from 1987 * ( http://www.netlib.org/specfun/erf ). See the AMS publication * "Rational Chebyshev Approximations for the Error Function" by W. J. Cody * for an explanation of this process. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.erf(x) * * Examples: * * math.erf(0.2); // returns 0.22270258921047847 * math.erf(-0.5); // returns -0.5204998778130465 * math.erf(4); // returns 0.9999999845827421 * * @param {number | Array | Matrix} x A real number * @return {number | Array | Matrix} The erf of `x` */ var erf = typed('erf', { 'number': function (x) { var y = Math.abs(x); if (y >= MAX_NUM) { return sign(x); } if (y <= THRESH) { return sign(x) * erf1(y); } if (y <= 4.0) { return sign(x) * (1 - erfc2(y)); } return sign(x) * (1 - erfc3(y)); }, // TODO: Not sure if there's a way to guarantee some degree of accuracy here. // Perhaps it would be best to set the precision of the number to that which // is guaranteed by erf() 'BigNumber': function (n) { return new type.BigNumber(erf(n.toNumber())); }, 'Array | Matrix': function (n) { return deepMap(n, erf); } // TODO: For complex numbers, use the approximation for the Faddeeva function // from "More Efficient Computation of the Complex Error Function" (AMS) }); /** * Approximates the error function erf() for x <= 0.46875 using this function: * n * erf(x) = x * sum (p_j * x^(2j)) / (q_j * x^(2j)) * j=0 */ function erf1(y) { var ysq = y * y; var xnum = P[0][4]*ysq; var xden = ysq; var i; for (i = 0; i < 3; i += 1) { xnum = (xnum + P[0][i]) * ysq; xden = (xden + Q[0][i]) * ysq; } return y * (xnum + P[0][3]) / (xden + Q[0][3]); } /** * Approximates the complement of the error function erfc() for * 0.46875 <= x <= 4.0 using this function: * n * erfc(x) = e^(-x^2) * sum (p_j * x^j) / (q_j * x^j) * j=0 */ function erfc2(y) { var xnum = P[1][8] * y; var xden = y; var i; for (i = 0; i < 7; i += 1) { xnum = (xnum + P[1][i]) * y; xden = (xden + Q[1][i]) * y; } var result = (xnum + P[1][7]) / (xden + Q[1][7]); var ysq = parseInt(y * 16) / 16; var del = (y - ysq) * (y + ysq); return Math.exp(-ysq*ysq) * Math.exp(-del) * result; } /** * Approximates the complement of the error function erfc() for x > 4.0 using * this function: * * erfc(x) = (e^(-x^2) / x) * [ 1/sqrt(pi) + * n * 1/(x^2) * sum (p_j * x^(-2j)) / (q_j * x^(-2j)) ] * j=0 */ function erfc3(y) { var ysq = 1 / (y * y); var xnum = P[2][5] * ysq; var xden = ysq; var i; for (i = 0; i < 4; i += 1) { xnum = (xnum + P[2][i]) * ysq; xden = (xden + Q[2][i]) * ysq; } var result = ysq * (xnum + P[2][4]) / (xden + Q[2][4]); result = (SQRPI - result) / y; ysq = parseInt(y * 16) / 16; var del = (y - ysq) * (y + ysq); return Math.exp(-ysq*ysq) * Math.exp(-del) * result; } erf.toTex = {1: 'erf\\left(${args[0]}\\right)'}; return erf; } /** * Upper bound for the first approximation interval, 0 <= x <= THRESH * @constant */ var THRESH = 0.46875; /** * Constant used by W. J. Cody's Fortran77 implementation to denote sqrt(pi) * @constant */ var SQRPI = 5.6418958354775628695e-1; /** * Coefficients for each term of the numerator sum (p_j) for each approximation * interval (see W. J. Cody's paper for more details) * @constant */ var P = [[ 3.16112374387056560e00, 1.13864154151050156e02, 3.77485237685302021e02, 3.20937758913846947e03, 1.85777706184603153e-1 ], [ 5.64188496988670089e-1, 8.88314979438837594e00, 6.61191906371416295e01, 2.98635138197400131e02, 8.81952221241769090e02, 1.71204761263407058e03, 2.05107837782607147e03, 1.23033935479799725e03, 2.15311535474403846e-8 ], [ 3.05326634961232344e-1, 3.60344899949804439e-1, 1.25781726111229246e-1, 1.60837851487422766e-2, 6.58749161529837803e-4, 1.63153871373020978e-2 ]]; /** * Coefficients for each term of the denominator sum (q_j) for each approximation * interval (see W. J. Cody's paper for more details) * @constant */ var Q = [[ 2.36012909523441209e01, 2.44024637934444173e02, 1.28261652607737228e03, 2.84423683343917062e03 ], [ 1.57449261107098347e01, 1.17693950891312499e02, 5.37181101862009858e02, 1.62138957456669019e03, 3.29079923573345963e03, 4.36261909014324716e03, 3.43936767414372164e03, 1.23033935480374942e03 ], [ 2.56852019228982242e00, 1.87295284992346047e00, 5.27905102951428412e-1, 6.05183413124413191e-2, 2.33520497626869185e-3 ]]; /** * Maximum/minimum safe numbers to input to erf() (in ES6+, this number is * Number.[MAX|MIN]_SAFE_INTEGER). erf() for all numbers beyond this limit will * return 1 */ var MAX_NUM = Math.pow(2, 53); exports.name = 'erf'; exports.factory = factory; /***/ }), /* 426 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(425) ]; /***/ }), /* 427 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(428), __webpack_require__(114), __webpack_require__(115), __webpack_require__(116), __webpack_require__(117), __webpack_require__(429), __webpack_require__(430), __webpack_require__(431), __webpack_require__(432), __webpack_require__(118), __webpack_require__(119) ]; /***/ }), /* 428 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var flatten = __webpack_require__(7).flatten; function factory (type, config, load, typed) { var abs = load(__webpack_require__(25)); var map = load(__webpack_require__(67)); var median = load(__webpack_require__(116)); var subtract = load(__webpack_require__(18)); /** * Compute the median absolute deviation of a matrix or a list with values. * The median absolute deviation is defined as the median of the absolute * deviations from the median. * * Syntax: * * math.mad(a, b, c, ...) * math.mad(A) * * Examples: * * math.mad(10, 20, 30); // returns 10 * math.mad([1, 2, 3]); // returns 1 * math.mad([[1, 2, 3], [4, 5, 6]]); // returns 1.5 * * See also: * * median, mean, std, abs * * @param {Array | Matrix} array * A single matrix or multiple scalar values. * @return {*} The median absolute deviation. */ var mad = typed('mad', { // mad([a, b, c, d, ...]) 'Array | Matrix': _mad, // mad(a, b, c, d, ...) '...': function (args) { return _mad(args); } }); mad.toTex = undefined; // use default template return mad; function _mad(array) { array = flatten(array.valueOf()); if (array.length === 0) { throw new Error('Cannot calculate median absolute deviation of an empty array'); } var med = median(array); return median(map(array, function (value) { return abs(subtract(value, med)); })); } } exports.name = 'mad'; exports.factory = factory; /***/ }), /* 429 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var flatten = __webpack_require__(7).flatten; function factory (type, config, load, typed) { /** * Computes the mode of a set of numbers or a list with values(numbers or characters). * If there are more than one modes, it returns a list of those values. * * Syntax: * * math.mode(a, b, c, ...) * math.mode(A) * * Examples: * * math.mode(2, 1, 4, 3, 1); // returns [1] * math.mode([1, 2.7, 3.2, 4, 2.7]); // returns [2.7] * math.mode(1, 4, 6, 1, 6) // returns [1, 6] * math.mode('a','a','b','c') // returns ["a"] * math.mode(1, 1.5, 'abc') // returns [1, 1.5, "abc"] * * See also: * * median, * mean * * @param {... *} args A single matrix * @return {*} The mode of all values */ var mode = typed('mode', { 'Array | Matrix' : _mode, '...': function (args) { return _mode(args); } }); return mode; /** * Calculates the mode in an 1-dimensional array * @param {Array} values * @return {number} mode * @private */ function _mode(values) { values = flatten(values.valueOf()); var num = values.length; if (num == 0) { throw new Error('Cannot calculate mode of an empty array'); } var count = {}, mode = [], max = 0; for (var i in values) { if (!(values[i] in count)){ count[values[i]] = 0; } count[values[i]]++; if (count[values[i]] == max){ mode.push(values[i]); } else if (count[values[i]] > max) { max = count[values[i]]; mode = [values[i]]; } } return mode; }; } exports.name = 'mode'; exports.factory = factory; /***/ }), /* 430 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepForEach = __webpack_require__(32); function factory (type, config, load, typed) { var multiply = load(__webpack_require__(20)); /** * Compute the product of a matrix or a list with values. * In case of a (multi dimensional) array or matrix, the sum of all * elements will be calculated. * * Syntax: * * math.prod(a, b, c, ...) * math.prod(A) * * Examples: * * math.multiply(2, 3); // returns 6 * math.prod(2, 3); // returns 6 * math.prod(2, 3, 4); // returns 24 * math.prod([2, 3, 4]); // returns 24 * math.prod([[2, 5], [4, 3]]); // returns 120 * * See also: * * mean, median, min, max, sum, std, var * * @param {... *} args A single matrix or or multiple scalar values * @return {*} The product of all values */ var prod = typed('prod', { // prod([a, b, c, d, ...]) 'Array | Matrix': _prod, // prod([a, b, c, d, ...], dim) 'Array | Matrix, number | BigNumber': function (array, dim) { // TODO: implement prod(A, dim) throw new Error('prod(A, dim) is not yet supported'); //return reduce(arguments[0], arguments[1], math.prod); }, // prod(a, b, c, d, ...) '...': function (args) { return _prod(args); } }); prod.toTex = undefined; // use default template return prod; /** * Recursively calculate the product of an n-dimensional array * @param {Array} array * @return {number} prod * @private */ function _prod(array) { var prod = undefined; deepForEach(array, function (value) { prod = (prod === undefined) ? value : multiply(prod, value); }); if (prod === undefined) { throw new Error('Cannot calculate prod of an empty array'); } return prod; } } exports.name = 'prod'; exports.factory = factory; /***/ }), /* 431 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isInteger = __webpack_require__(2).isInteger; var isNumber = __webpack_require__(2).isNumber; var flatten = __webpack_require__(7).flatten; var isCollection = __webpack_require__(40); function factory (type, config, load, typed) { var add = load(__webpack_require__(15)); var multiply = load(__webpack_require__(10)); var partitionSelect = load(__webpack_require__(68)); var compare = load(__webpack_require__(46)); /** * Compute the prob order quantile of a matrix or a list with values. * The sequence is sorted and the middle value is returned. * Supported types of sequence values are: Number, BigNumber, Unit * Supported types of probability are: Number, BigNumber * * In case of a (multi dimensional) array or matrix, the prob order quantile * of all elements will be calculated. * * Syntax: * * math.quantileSeq(A, prob[, sorted]) * math.quantileSeq(A, [prob1, prob2, ...][, sorted]) * math.quantileSeq(A, N[, sorted]) * * Examples: * * math.quantileSeq([3, -1, 5, 7], 0.5); // returns 4 * math.quantileSeq([3, -1, 5, 7], [1/3, 2/3]); // returns [3, 5] * math.quantileSeq([3, -1, 5, 7], 2); // returns [3, 5] * math.quantileSeq([-1, 3, 5, 7], 0.5, true); // returns 4 * * See also: * * median, mean, min, max, sum, prod, std, var * * @param {Array, Matrix} data A single matrix or Array * @param {Number, BigNumber, Array} probOrN prob is the order of the quantile, while N is * the amount of evenly distributed steps of * probabilities; only one of these options can * be provided * @param {Boolean} sorted=false is data sorted in ascending order * @return {Number, BigNumber, Unit, Array} Quantile(s) */ function quantileSeq(data, probOrN, sorted) { var probArr, dataArr, one; if (arguments.length < 2 || arguments.length > 3) { throw new SyntaxError('Function quantileSeq requires two or three parameters'); } if (isCollection(data)) { sorted = sorted || false; if (typeof sorted === 'boolean') { dataArr = data.valueOf(); if (isNumber(probOrN)) { if (probOrN < 0) { throw new Error('N/prob must be non-negative'); } if (probOrN <= 1) { // quantileSeq([a, b, c, d, ...], prob[,sorted]) return _quantileSeq(dataArr, probOrN, sorted); } if (probOrN > 1) { // quantileSeq([a, b, c, d, ...], N[,sorted]) if (!isInteger(probOrN)) { throw new Error('N must be a positive integer'); } var nPlusOne = probOrN + 1; probArr = new Array(probOrN); for (var i = 0; i < probOrN;) { probArr[i] = _quantileSeq(dataArr, (++i) / nPlusOne, sorted); } return probArr; } } if (probOrN && probOrN.isBigNumber) { if (probOrN.isNegative()) { throw new Error('N/prob must be non-negative'); } one = new probOrN.constructor(1); if (probOrN.lte(one)) { // quantileSeq([a, b, c, d, ...], prob[,sorted]) return _quantileSeq(dataArr, probOrN, sorted); } if (probOrN.gt(one)) { // quantileSeq([a, b, c, d, ...], N[,sorted]) if (!probOrN.isInteger()) { throw new Error('N must be a positive integer'); } // largest possible Array length is 2^32-1; // 2^32 < 10^15, thus safe conversion guaranteed var intN = probOrN.toNumber(); if (intN > 4294967295) { throw new Error('N must be less than or equal to 2^32-1, as that is the maximum length of an Array'); } var nPlusOne = new type.BigNumber(intN + 1); probArr = new Array(intN); for (var i = 0; i < intN;) { probArr[i] = _quantileSeq(dataArr, new type.BigNumber(++i).div(nPlusOne), sorted); } return probArr; } } if (Array.isArray(probOrN)) { // quantileSeq([a, b, c, d, ...], [prob1, prob2, ...][,sorted]) probArr = new Array(probOrN.length); for (var i = 0; i < probArr.length; ++i) { var currProb = probOrN[i]; if (isNumber(currProb)) { if (currProb < 0 || currProb > 1) { throw new Error('Probability must be between 0 and 1, inclusive'); } } else if (currProb && currProb.isBigNumber) { one = new currProb.constructor(1); if (currProb.isNegative() || currProb.gt(one)) { throw new Error('Probability must be between 0 and 1, inclusive'); } } else { throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function } probArr[i] = _quantileSeq(dataArr, currProb, sorted); } return probArr; } throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function } throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function } throw new TypeError('Unexpected type of argument in function quantileSeq'); // FIXME: becomes redundant when converted to typed-function } /** * Calculate the prob order quantile of an n-dimensional array. * * @param {Array} array * @param {Number, BigNumber} prob * @param {Boolean} sorted * @return {Number, BigNumber, Unit} prob order quantile * @private */ function _quantileSeq(array, prob, sorted) { var flat = flatten(array); var len = flat.length; if (len === 0) { throw new Error('Cannot calculate quantile of an empty sequence'); } if (isNumber(prob)) { var index = prob * (len-1); var fracPart = index % 1; if (fracPart === 0) { var value = sorted ? flat[index] : partitionSelect(flat, index); validate(value); return value; } var integerPart = Math.floor(index); var left, right; if (sorted) { left = flat[integerPart]; right = flat[integerPart+1]; } else { right = partitionSelect(flat, integerPart+1); // max of partition is kth largest left = flat[integerPart]; for (var i = 0; i < integerPart; ++i) { if (compare(flat[i], left) > 0) { left = flat[i]; } } } validate(left); validate(right); // Q(prob) = (1-f)*A[floor(index)] + f*A[floor(index)+1] return add(multiply(left, 1 - fracPart), multiply(right, fracPart)); } // If prob is a BigNumber var index = prob.times(len-1); if (index.isInteger()) { index = index.toNumber(); var value = sorted ? flat[index] : partitionSelect(flat, index); validate(value); return value; } var integerPart = index.floor(); var fracPart = index.minus(integerPart); var integerPartNumber = integerPart.toNumber(); var left, right; if (sorted) { left = flat[integerPartNumber]; right = flat[integerPartNumber+1]; } else { right = partitionSelect(flat, integerPartNumber+1); // max of partition is kth largest left = flat[integerPartNumber]; for (var i = 0; i < integerPartNumber; ++i) { if (compare(flat[i], left) > 0) { left = flat[i]; } } } validate(left); validate(right); // Q(prob) = (1-f)*A[floor(index)] + f*A[floor(index)+1] var one = new fracPart.constructor(1); return add(multiply(left, one.minus(fracPart)), multiply(right, fracPart)); } /** * Check if array value types are valid, throw error otherwise. * @param {number | BigNumber | Unit} x * @param {number | BigNumber | Unit} x * @private */ var validate = typed({ 'number | BigNumber | Unit': function (x) { return x; } }); return quantileSeq; } exports.name = 'quantileSeq'; exports.factory = factory; /***/ }), /* 432 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var sqrt = load(__webpack_require__(50)); var variance = load(__webpack_require__(119)); /** * Compute the standard deviation of a matrix or a list with values. * The standard deviations is defined as the square root of the variance: * `std(A) = sqrt(var(A))`. * In case of a (multi dimensional) array or matrix, the standard deviation * over all elements will be calculated. * * Optionally, the type of normalization can be specified as second * parameter. The parameter `normalization` can be one of the following values: * * - 'unbiased' (default) The sum of squared errors is divided by (n - 1) * - 'uncorrected' The sum of squared errors is divided by n * - 'biased' The sum of squared errors is divided by (n + 1) * * Syntax: * * math.std(a, b, c, ...) * math.std(A) * math.std(A, normalization) * * Examples: * * math.std(2, 4, 6); // returns 2 * math.std([2, 4, 6, 8]); // returns 2.581988897471611 * math.std([2, 4, 6, 8], 'uncorrected'); // returns 2.23606797749979 * math.std([2, 4, 6, 8], 'biased'); // returns 2 * * math.std([[1, 2, 3], [4, 5, 6]]); // returns 1.8708286933869707 * * See also: * * mean, median, max, min, prod, sum, var * * @param {Array | Matrix} array * A single matrix or or multiple scalar values * @param {string} [normalization='unbiased'] * Determines how to normalize the variance. * Choose 'unbiased' (default), 'uncorrected', or 'biased'. * @return {*} The standard deviation */ var std = typed('std', { // std([a, b, c, d, ...]) 'Array | Matrix': _std, // std([a, b, c, d, ...], normalization) 'Array | Matrix, string': _std, // std(a, b, c, d, ...) '...': function (args) { return _std(args); } }); std.toTex = undefined; // use default template return std; function _std(array, normalization) { if (array.length == 0) { throw new SyntaxError('Function std requires one or more parameters (0 provided)'); } return sqrt(variance.apply(null, arguments)); } } exports.name = 'std'; exports.factory = factory; /***/ }), /* 433 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(120), __webpack_require__(434) ]; /***/ }), /* 434 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var isString = __webpack_require__(19).isString; var format = __webpack_require__(19).format; function factory (type, config, load, typed) { /** * Interpolate values into a string template. * * Syntax: * * math.print(template, values) * math.print(template, values, precision) * math.print(template, values, options) * * Example usage: * * // the following outputs: 'Lucy is 5 years old' * math.print('Lucy is $age years old', {age: 5}); * * // the following outputs: 'The value of pi is 3.141592654' * math.print('The value of pi is $pi', {pi: math.pi}, 10); * * // the following outputs: 'hello Mary! The date is 2013-03-23' * math.print('Hello $user.name! The date is $date', { * user: { * name: 'Mary', * }, * date: new Date(2013, 2, 23).toISOString().substring(0, 10) * }); * * See also: * * format * * @param {string} template A string containing variable placeholders. * @param {Object} values An object containing variables which will * be filled in in the template. * @param {number | Object} [options] Formatting options, * or the number of digits to format numbers. * See function math.format for a description * of all options. * @return {string} Interpolated string */ var print = typed ('print', { 'string, Object': _print, 'string, Object, number | Object': _print }); print.toTex = undefined; // use default template return print; } /** * Interpolate values into a string template. * @param {string} template * @param {Object} values * @param {number | Object} [options] * @returns {string} Interpolated string * @private */ function _print(template, values, options) { return template.replace(/\$([\w\.]+)/g, function (original, key) { var keys = key.split('.'); var value = values[keys.shift()]; while (keys.length && value !== undefined) { var k = keys.shift(); value = k ? value[k] : value + '.'; } if (value !== undefined) { if (!isString(value)) { return format(value, options); } else { return value; } } return original; } ); } exports.name = 'print'; exports.factory = factory; /***/ }), /* 435 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the inverse cosine of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.acos(x) * * Examples: * * math.acos(0.5); // returns number 1.0471975511965979 * math.acos(math.cos(1.5)); // returns number 1.5 * * math.acos(2); // returns Complex 0 + 1.3169578969248166 i * * See also: * * cos, atan, asin * * @param {number | BigNumber | Complex | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} The arc cosine of x */ var acos = typed('acos', { 'number': function (x) { if ((x >= -1 && x <= 1) || config.predictable) { return Math.acos(x); } else { return new type.Complex(x, 0).acos(); } }, 'Complex': function (x) { return x.acos(); }, 'BigNumber': function (x) { return x.acos(); }, 'Array | Matrix': function (x) { return deepMap(x, acos); } }); acos.toTex = {1: '\\cos^{-1}\\left(${args[0]}\\right)'}; return acos; } exports.name = 'acos'; exports.factory = factory; /***/ }), /* 436 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the inverse cotangent of a value, defined as `acot(x) = atan(1/x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.acot(x) * * Examples: * * math.acot(0.5); // returns number 0.4636476090008061 * math.acot(math.cot(1.5)); // returns number 1.5 * * math.acot(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i * * See also: * * cot, atan * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} The arc cotangent of x */ var acot = typed('acot', { 'number': function (x) { return Math.atan(1 / x); }, 'Complex': function (x) { return x.acot(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x).atan(); }, 'Array | Matrix': function (x) { return deepMap(x, acot); } }); acot.toTex = {1: '\\cot^{-1}\\left(${args[0]}\\right)'}; return acot; } exports.name = 'acot'; exports.factory = factory; /***/ }), /* 437 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic arccotangent of a value, * defined as `acoth(x) = atanh(1/x) = (ln((x+1)/x) + ln(x/(x-1))) / 2`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.acoth(x) * * Examples: * * math.acoth(0.5); // returns 0.8047189562170503 * * See also: * * acsch, asech * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic arccotangent of x */ var acoth = typed('acoth', { 'number': function (x) { if (x >= 1 || x <= -1 || config.predictable) { return isFinite(x) ? (Math.log((x+1)/x) + Math.log(x/(x-1))) / 2 : 0; } return new type.Complex(x, 0).acoth(); }, 'Complex': function (x) { return x.acoth(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x).atanh(); }, 'Array | Matrix': function (x) { return deepMap(x, acoth); } }); acoth.toTex = {1: '\\coth^{-1}\\left(${args[0]}\\right)'}; return acoth; } exports.name = 'acoth'; exports.factory = factory; /***/ }), /* 438 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the inverse cosecant of a value, defined as `acsc(x) = asin(1/x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.acsc(x) * * Examples: * * math.acsc(0.5); // returns number 0.5235987755982989 * math.acsc(math.csc(1.5)); // returns number ~1.5 * * math.acsc(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i * * See also: * * csc, asin, asec * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} The arc cosecant of x */ var acsc = typed('acsc', { 'number': function (x) { if (x <= -1 || x >= 1 || config.predictable) { return Math.asin(1 / x); } return new type.Complex(x, 0).acsc(); }, 'Complex': function (x) { return x.acsc(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x).asin(); }, 'Array | Matrix': function (x) { return deepMap(x, acsc); } }); acsc.toTex = {1: '\\csc^{-1}\\left(${args[0]}\\right)'}; return acsc; } exports.name = 'acsc'; exports.factory = factory; /***/ }), /* 439 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic arccosecant of a value, * defined as `acsch(x) = asinh(1/x) = ln(1/x + sqrt(1/x^2 + 1))`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.acsch(x) * * Examples: * * math.acsch(0.5); // returns 1.4436354751788103 * * See also: * * asech, acoth * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic arccosecant of x */ var acsch = typed('acsch', { 'number': function (x) { x = 1 / x; return Math.log(x + Math.sqrt(x*x + 1)); }, 'Complex': function (x) { return x.acsch(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x).asinh(); }, 'Array | Matrix': function (x) { return deepMap(x, acsch); } }); acsch.toTex = {1: '\\mathrm{csch}^{-1}\\left(${args[0]}\\right)'}; return acsch; } exports.name = 'acsch'; exports.factory = factory; /***/ }), /* 440 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the inverse secant of a value. Defined as `asec(x) = acos(1/x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.asec(x) * * Examples: * * math.asec(0.5); // returns 1.0471975511965979 * math.asec(math.sec(1.5)); // returns 1.5 * * math.asec(2); // returns 0 + 1.3169578969248166 i * * See also: * * acos, acot, acsc * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} The arc secant of x */ var asec = typed('asec', { 'number': function (x) { if (x <= -1 || x >= 1 || config.predictable) { return Math.acos(1 / x); } return new type.Complex(x, 0).asec(); }, 'Complex': function (x) { return x.asec(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x).acos(); }, 'Array | Matrix': function (x) { return deepMap(x, asec); } }); asec.toTex = {1: '\\sec^{-1}\\left(${args[0]}\\right)'}; return asec; } exports.name = 'asec'; exports.factory = factory; /***/ }), /* 441 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var acosh = typed.find(load(__webpack_require__(121)), ['Complex']); /** * Calculate the hyperbolic arcsecant of a value, * defined as `asech(x) = acosh(1/x) = ln(sqrt(1/x^2 - 1) + 1/x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.asech(x) * * Examples: * * math.asech(0.5); // returns 1.3169578969248166 * * See also: * * acsch, acoth * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic arcsecant of x */ var asech = typed('asech', { 'number': function (x) { if ((x <= 1 && x >= -1) || config.predictable) { x = 1 / x; var ret = Math.sqrt(x*x - 1); if (x > 0 || config.predictable) { return Math.log(ret + x); } return new type.Complex(Math.log(ret - x), Math.PI); } return new type.Complex(x, 0).asech(); }, 'Complex': function (x) { return x.asech() }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x).acosh(); }, 'Array | Matrix': function (x) { return deepMap(x, asech); } }); asech.toTex = {1: '\\mathrm{sech}^{-1}\\left(${args[0]}\\right)'}; return asech; } exports.name = 'asech'; exports.factory = factory; /***/ }), /* 442 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the inverse sine of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.asin(x) * * Examples: * * math.asin(0.5); // returns number 0.5235987755982989 * math.asin(math.sin(1.5)); // returns number ~1.5 * * math.asin(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i * * See also: * * sin, atan, acos * * @param {number | BigNumber | Complex | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} The arc sine of x */ var asin = typed('asin', { 'number': function (x) { if ((x >= -1 && x <= 1) || config.predictable) { return Math.asin(x); } else { return new type.Complex(x, 0).asin(); } }, 'Complex': function (x) { return x.asin(); }, 'BigNumber': function (x) { return x.asin(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since asin(0) = 0 return deepMap(x, asin, true); } }); asin.toTex = {1: '\\sin^{-1}\\left(${args[0]}\\right)'}; return asin; } exports.name = 'asin'; exports.factory = factory; /***/ }), /* 443 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic arcsine of a value, * defined as `asinh(x) = ln(x + sqrt(x^2 + 1))`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.asinh(x) * * Examples: * * math.asinh(0.5); // returns 0.48121182505960347 * * See also: * * acosh, atanh * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic arcsine of x */ var asinh = typed('asinh', { 'number': Math.asinh || function (x) { return Math.log(Math.sqrt(x*x + 1) + x); }, 'Complex': function (x) { return x.asinh(); }, 'BigNumber': function (x) { return x.asinh(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since asinh(0) = 0 return deepMap(x, asinh, true); } }); asinh.toTex = {1: '\\sinh^{-1}\\left(${args[0]}\\right)'}; return asinh; } exports.name = 'asinh'; exports.factory = factory; /***/ }), /* 444 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the inverse tangent of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.atan(x) * * Examples: * * math.atan(0.5); // returns number 0.4636476090008061 * math.atan(math.tan(1.5)); // returns number 1.5 * * math.atan(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i * * See also: * * tan, asin, acos * * @param {number | BigNumber | Complex | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} The arc tangent of x */ var atan = typed('atan', { 'number': function (x) { return Math.atan(x); }, 'Complex': function (x) { return x.atan(); }, 'BigNumber': function (x) { return x.atan(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since atan(0) = 0 return deepMap(x, atan, true); } }); atan.toTex = {1: '\\tan^{-1}\\left(${args[0]}\\right)'}; return atan; } exports.name = 'atan'; exports.factory = factory; /***/ }), /* 445 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var matrix = load(__webpack_require__(1)); var algorithm02 = load(__webpack_require__(21)); var algorithm03 = load(__webpack_require__(12)); var algorithm09 = load(__webpack_require__(128)); var algorithm11 = load(__webpack_require__(13)); var algorithm12 = load(__webpack_require__(14)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Calculate the inverse tangent function with two arguments, y/x. * By providing two arguments, the right quadrant of the computed angle can be * determined. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.atan2(y, x) * * Examples: * * math.atan2(2, 2) / math.pi; // returns number 0.25 * * var angle = math.unit(60, 'deg'); // returns Unit 60 deg * var x = math.cos(angle); * var y = math.sin(angle); * * math.atan(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i * * See also: * * tan, atan, sin, cos * * @param {number | Array | Matrix} y Second dimension * @param {number | Array | Matrix} x First dimension * @return {number | Array | Matrix} Four-quadrant inverse tangent */ var atan2 = typed('atan2', { 'number, number': Math.atan2, // Complex numbers doesn't seem to have a reasonable implementation of // atan2(). Even Matlab removed the support, after they only calculated // the atan only on base of the real part of the numbers and ignored the imaginary. 'BigNumber, BigNumber': function (y, x) { return type.BigNumber.atan2(y, x); }, 'Matrix, Matrix': function (x, y) { // result var c; // process matrix storage switch (x.storage()) { case 'sparse': switch (y.storage()) { case 'sparse': // sparse .* sparse c = algorithm09(x, y, atan2, false); break; default: // sparse .* dense c = algorithm02(y, x, atan2, true); break; } break; default: switch (y.storage()) { case 'sparse': // dense .* sparse c = algorithm03(x, y, atan2, false); break; default: // dense .* dense c = algorithm13(x, y, atan2); break; } break; } return c; }, 'Array, Array': function (x, y) { // use matrix implementation return atan2(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return atan2(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return atan2(x, matrix(y)); }, 'Matrix, number | BigNumber': function (x, y) { // result var c; // check storage format switch (x.storage()) { case 'sparse': c = algorithm11(x, y, atan2, false); break; default: c = algorithm14(x, y, atan2, false); break; } return c; }, 'number | BigNumber, Matrix': function (x, y) { // result var c; // check storage format switch (y.storage()) { case 'sparse': c = algorithm12(y, x, atan2, true); break; default: c = algorithm14(y, x, atan2, true); break; } return c; }, 'Array, number | BigNumber': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, atan2, false).valueOf(); }, 'number | BigNumber, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, atan2, true).valueOf(); } }); atan2.toTex = {2: '\\mathrm{atan2}\\left(${args}\\right)'}; return atan2; } exports.name = 'atan2'; exports.factory = factory; /***/ }), /* 446 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic arctangent of a value, * defined as `atanh(x) = ln((1 + x)/(1 - x)) / 2`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.atanh(x) * * Examples: * * math.atanh(0.5); // returns 0.5493061443340549 * * See also: * * acosh, asinh * * @param {number | Complex | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic arctangent of x */ var atanh = typed('atanh', { 'number': function (x) { if ((x <= 1 && x >= -1) || config.predictable) { return _atanh(x); } return new type.Complex(x, 0).atanh(); }, 'Complex': function (x) { return x.atanh(); }, 'BigNumber': function (x) { return x.atanh(); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since atanh(0) = 0 return deepMap(x, atanh, true); } }); atanh.toTex = {1: '\\tanh^{-1}\\left(${args[0]}\\right)'}; return atanh; } /** * Calculate the hyperbolic arctangent of a number * @param {number} x * @return {number} * @private */ var _atanh = Math.atanh || function (x) { return Math.log((1 + x)/(1 - x)) / 2 }; exports.name = 'atanh'; exports.factory = factory; /***/ }), /* 447 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the cosine of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.cos(x) * * Examples: * * math.cos(2); // returns number -0.4161468365471422 * math.cos(math.pi / 4); // returns number 0.7071067811865475 * math.cos(math.unit(180, 'deg')); // returns number -1 * math.cos(math.unit(60, 'deg')); // returns number 0.5 * * var angle = 0.2; * math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2); // returns number ~1 * * See also: * * cos, tan * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} Cosine of x */ var cos = typed('cos', { 'number': Math.cos, 'Complex': function (x) { return x.cos(); }, 'BigNumber': function (x) { return x.cos(); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function cos is no angle'); } return cos(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, cos); } }); cos.toTex = {1: '\\cos\\left(${args[0]}\\right)'}; return cos; } exports.name = 'cos'; exports.factory = factory; /***/ }), /* 448 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic cosine of a value, * defined as `cosh(x) = 1/2 * (exp(x) + exp(-x))`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.cosh(x) * * Examples: * * math.cosh(0.5); // returns number 1.1276259652063807 * * See also: * * sinh, tanh * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} Hyperbolic cosine of x */ var cosh = typed('cosh', { 'number': _cosh, 'Complex': function (x) { return x.cosh(); }, 'BigNumber': function (x) { return x.cosh(); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function cosh is no angle'); } return cosh(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, cosh); } }); cosh.toTex = {1: '\\cosh\\left(${args[0]}\\right)'}; return cosh; } /** * Calculate the hyperbolic cosine of a number * @param {number} x * @returns {number} * @private */ var _cosh = Math.cosh || function (x) { return (Math.exp(x) + Math.exp(-x)) / 2; }; exports.name = 'cosh'; exports.factory = factory; /***/ }), /* 449 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the cotangent of a value. Defined as `cot(x) = 1 / tan(x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.cot(x) * * Examples: * * math.cot(2); // returns number -0.45765755436028577 * 1 / math.tan(2); // returns number -0.45765755436028577 * * See also: * * tan, sec, csc * * @param {number | Complex | Unit | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Cotangent of x */ var cot = typed('cot', { 'number': function (x) { return 1 / Math.tan(x); }, 'Complex': function (x) { return x.cot(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x.tan()); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function cot is no angle'); } return cot(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, cot); } }); cot.toTex = {1: '\\cot\\left(${args[0]}\\right)'}; return cot; } exports.name = 'cot'; exports.factory = factory; /***/ }), /* 450 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic cotangent of a value, * defined as `coth(x) = 1 / tanh(x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.coth(x) * * Examples: * * // coth(x) = 1 / tanh(x) * math.coth(2); // returns 1.0373147207275482 * 1 / math.tanh(2); // returns 1.0373147207275482 * * See also: * * sinh, tanh, cosh * * @param {number | Complex | Unit | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic cotangent of x */ var coth = typed('coth', { 'number': _coth, 'Complex': function (x) { return x.coth(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x.tanh()); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function coth is no angle'); } return coth(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, coth); } }); coth.toTex = {1: '\\coth\\left(${args[0]}\\right)'}; return coth; } /** * Calculate the hyperbolic cosine of a number * @param {number} x * @returns {number} * @private */ function _coth(x) { var e = Math.exp(2 * x); return (e + 1) / (e - 1); } exports.name = 'coth'; exports.factory = factory; /***/ }), /* 451 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the cosecant of a value, defined as `csc(x) = 1/sin(x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.csc(x) * * Examples: * * math.csc(2); // returns number 1.099750170294617 * 1 / math.sin(2); // returns number 1.099750170294617 * * See also: * * sin, sec, cot * * @param {number | Complex | Unit | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Cosecant of x */ var csc = typed('csc', { 'number': function (x) { return 1 / Math.sin(x); }, 'Complex': function (x) { return x.csc(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x.sin()); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function csc is no angle'); } return csc(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, csc); } }); csc.toTex = {1: '\\csc\\left(${args[0]}\\right)'}; return csc; } exports.name = 'csc'; exports.factory = factory; /***/ }), /* 452 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var sign = __webpack_require__(2).sign; function factory (type, config, load, typed) { /** * Calculate the hyperbolic cosecant of a value, * defined as `csch(x) = 1 / sinh(x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.csch(x) * * Examples: * * // csch(x) = 1/ sinh(x) * math.csch(0.5); // returns 1.9190347513349437 * 1 / math.sinh(0.5); // returns 1.9190347513349437 * * See also: * * sinh, sech, coth * * @param {number | Complex | Unit | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic cosecant of x */ var csch = typed('csch', { 'number': _csch, 'Complex': function (x) { return x.csch(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x.sinh()); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function csch is no angle'); } return csch(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, csch); } }); csch.toTex = {1: '\\mathrm{csch}\\left(${args[0]}\\right)'}; return csch; } /** * Calculate the hyperbolic cosecant of a number * @param {number} x * @returns {number} * @private */ function _csch(x) { // consider values close to zero (+/-) if (x == 0) { return Number.POSITIVE_INFINITY; } else { return Math.abs(2 / (Math.exp(x) - Math.exp(-x))) * sign(x); } } exports.name = 'csch'; exports.factory = factory; /***/ }), /* 453 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(435), __webpack_require__(121), __webpack_require__(436), __webpack_require__(437), __webpack_require__(438), __webpack_require__(439), __webpack_require__(440), __webpack_require__(441), __webpack_require__(442), __webpack_require__(443), __webpack_require__(444), __webpack_require__(445), __webpack_require__(446), __webpack_require__(447), __webpack_require__(448), __webpack_require__(449), __webpack_require__(450), __webpack_require__(451), __webpack_require__(452), __webpack_require__(454), __webpack_require__(455), __webpack_require__(456), __webpack_require__(457), __webpack_require__(458), __webpack_require__(459) ]; /***/ }), /* 454 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the secant of a value, defined as `sec(x) = 1/cos(x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.sec(x) * * Examples: * * math.sec(2); // returns number -2.4029979617223822 * 1 / math.cos(2); // returns number -2.4029979617223822 * * See also: * * cos, csc, cot * * @param {number | Complex | Unit | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Secant of x */ var sec = typed('sec', { 'number': function (x) { return 1 / Math.cos(x); }, 'Complex': function (x) { return x.sec(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x.cos()); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function sec is no angle'); } return sec(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, sec); } }); sec.toTex = {1: '\\sec\\left(${args[0]}\\right)'}; return sec; } exports.name = 'sec'; exports.factory = factory; /***/ }), /* 455 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic secant of a value, * defined as `sech(x) = 1 / cosh(x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.sech(x) * * Examples: * * // sech(x) = 1/ cosh(x) * math.sech(0.5); // returns 0.886818883970074 * 1 / math.cosh(0.5); // returns 0.886818883970074 * * See also: * * cosh, csch, coth * * @param {number | Complex | Unit | Array | Matrix} x Function input * @return {number | Complex | Array | Matrix} Hyperbolic secant of x */ var sech = typed('sech', { 'number': _sech, 'Complex': function (x) { return x.sech(); }, 'BigNumber': function (x) { return new type.BigNumber(1).div(x.cosh()); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function sech is no angle'); } return sech(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, sech); } }); sech.toTex = {1: '\\mathrm{sech}\\left(${args[0]}\\right)'}; return sech; } /** * Calculate the hyperbolic secant of a number * @param {number} x * @returns {number} * @private */ function _sech(x) { return 2 / (Math.exp(x) + Math.exp(-x)); } exports.name = 'sech'; exports.factory = factory; /***/ }), /* 456 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the sine of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.sin(x) * * Examples: * * math.sin(2); // returns number 0.9092974268256813 * math.sin(math.pi / 4); // returns number 0.7071067811865475 * math.sin(math.unit(90, 'deg')); // returns number 1 * math.sin(math.unit(30, 'deg')); // returns number 0.5 * * var angle = 0.2; * math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2); // returns number ~1 * * See also: * * cos, tan * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} Sine of x */ var sin = typed('sin', { 'number': Math.sin, 'Complex': function (x) { return x.sin(); }, 'BigNumber': function (x) { return x.sin(); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function sin is no angle'); } return sin(x.value); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since sin(0) = 0 return deepMap(x, sin, true); } }); sin.toTex = {1: '\\sin\\left(${args[0]}\\right)'}; return sin; } exports.name = 'sin'; exports.factory = factory; /***/ }), /* 457 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic sine of a value, * defined as `sinh(x) = 1/2 * (exp(x) - exp(-x))`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.sinh(x) * * Examples: * * math.sinh(0.5); // returns number 0.5210953054937474 * * See also: * * cosh, tanh * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} Hyperbolic sine of x */ var sinh = typed('sinh', { 'number': _sinh, 'Complex': function (x) { return x.sinh(); }, 'BigNumber': function (x) { return x.sinh(); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function sinh is no angle'); } return sinh(x.value); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since sinh(0) = 0 return deepMap(x, sinh, true); } }); sinh.toTex = {1: '\\sinh\\left(${args[0]}\\right)'}; return sinh; } /** * Calculate the hyperbolic sine of a number * @param {number} x * @returns {number} * @private */ var _sinh = Math.sinh || function (x) { return (Math.exp(x) - Math.exp(-x)) / 2; }; exports.name = 'sinh'; exports.factory = factory; /***/ }), /* 458 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the tangent of a value. `tan(x)` is equal to `sin(x) / cos(x)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.tan(x) * * Examples: * * math.tan(0.5); // returns number 0.5463024898437905 * math.sin(0.5) / math.cos(0.5); // returns number 0.5463024898437905 * math.tan(math.pi / 4); // returns number 1 * math.tan(math.unit(45, 'deg')); // returns number 1 * * See also: * * atan, sin, cos * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} Tangent of x */ var tan = typed('tan', { 'number': Math.tan, 'Complex': function (x) { return x.tan(); }, 'BigNumber': function (x) { return x.tan(); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function tan is no angle'); } return tan(x.value); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since tan(0) = 0 return deepMap(x, tan, true); } }); tan.toTex = {1: '\\tan\\left(${args[0]}\\right)'}; return tan; } exports.name = 'tan'; exports.factory = factory; /***/ }), /* 459 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Calculate the hyperbolic tangent of a value, * defined as `tanh(x) = (exp(2 * x) - 1) / (exp(2 * x) + 1)`. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.tanh(x) * * Examples: * * // tanh(x) = sinh(x) / cosh(x) = 1 / coth(x) * math.tanh(0.5); // returns 0.46211715726000974 * math.sinh(0.5) / math.cosh(0.5); // returns 0.46211715726000974 * 1 / math.coth(0.5); // returns 0.46211715726000974 * * See also: * * sinh, cosh, coth * * @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input * @return {number | BigNumber | Complex | Array | Matrix} Hyperbolic tangent of x */ var tanh = typed('tanh', { 'number': _tanh, 'Complex': function (x) { return x.tanh(); }, 'BigNumber': function (x) { return x.tanh(); }, 'Unit': function (x) { if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) { throw new TypeError ('Unit in function tanh is no angle'); } return tanh(x.value); }, 'Array | Matrix': function (x) { // deep map collection, skip zeros since tanh(0) = 0 return deepMap(x, tanh, true); } }); tanh.toTex = {1: '\\tanh\\left(${args[0]}\\right)'}; return tanh; } /** * Calculate the hyperbolic tangent of a number * @param {number} x * @returns {number} * @private */ var _tanh = Math.tanh || function (x) { var e = Math.exp(2 * x); return (e - 1) / (e + 1); }; exports.name = 'tanh'; exports.factory = factory; /***/ }), /* 460 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(461) ]; /***/ }), /* 461 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var latex = __webpack_require__(3); var matrix = load(__webpack_require__(1)); var algorithm13 = load(__webpack_require__(6)); var algorithm14 = load(__webpack_require__(5)); /** * Change the unit of a value. * * For matrices, the function is evaluated element wise. * * Syntax: * * math.to(x, unit) * * Examples: * * math.to(math.unit('2 inch'), 'cm'); // returns Unit 5.08 cm * math.to(math.unit('2 inch'), math.unit(null, 'cm')); // returns Unit 5.08 cm * math.to(math.unit(16, 'bytes'), 'bits'); // returns Unit 128 bits * * See also: * * unit * * @param {Unit | Array | Matrix} x The unit to be converted. * @param {Unit | Array | Matrix} unit New unit. Can be a string like "cm" * or a unit without value. * @return {Unit | Array | Matrix} value with changed, fixed unit. */ var to = typed('to', { 'Unit, Unit | string': function (x, unit) { return x.to(unit); }, 'Matrix, Matrix': function (x, y) { // SparseMatrix does not support Units return algorithm13(x, y, to); }, 'Array, Array': function (x, y) { // use matrix implementation return to(matrix(x), matrix(y)).valueOf(); }, 'Array, Matrix': function (x, y) { // use matrix implementation return to(matrix(x), y); }, 'Matrix, Array': function (x, y) { // use matrix implementation return to(x, matrix(y)); }, 'Matrix, any': function (x, y) { // SparseMatrix does not support Units return algorithm14(x, y, to, false); }, 'any, Matrix': function (x, y) { // SparseMatrix does not support Units return algorithm14(y, x, to, true); }, 'Array, any': function (x, y) { // use matrix implementation return algorithm14(matrix(x), y, to, false).valueOf(); }, 'any, Array': function (x, y) { // use matrix implementation return algorithm14(matrix(y), x, to, true).valueOf(); } }); to.toTex = { 2: '\\left(${args[0]}' + latex.operators['to'] + '${args[1]}\\right)' }; return to; } exports.name = 'to'; exports.factory = factory; /***/ }), /* 462 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var object= __webpack_require__(4); function factory (type, config, load, typed) { /** * Clone an object. * * Syntax: * * math.clone(x) * * Examples: * * math.clone(3.5); // returns number 3.5 * math.clone(math.complex('2-4i'); // returns Complex 2 - 4i * math.clone(math.unit(45, 'deg')); // returns Unit 45 deg * math.clone([[1, 2], [3, 4]]); // returns Array [[1, 2], [3, 4]] * math.clone("hello world"); // returns string "hello world" * * @param {*} x Object to be cloned * @return {*} A clone of object x */ var clone = typed('clone', { 'any': object.clone }); clone.toTex = undefined; // use default template return clone; } exports.name = 'clone'; exports.factory = factory; /***/ }), /* 463 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(462), __webpack_require__(39), __webpack_require__(47), __webpack_require__(71), __webpack_require__(55), __webpack_require__(465), __webpack_require__(122), __webpack_require__(464), __webpack_require__(123) ]; /***/ }), /* 464 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Test whether a value is NaN (not a number). * The function supports types `number`, `BigNumber`, `Fraction`, `Unit` and `Complex`. * * The function is evaluated element-wise in case of Array or Matrix input. * * Syntax: * * math.isNaN(x) * * Examples: * * math.isNaN(3); // returns false * math.isNaN(NaN); // returns true * math.isNaN(0); // returns false * math.isNaN(math.bignumber(NaN)); // returns true * math.isNaN(math.bignumber(0)); // returns false * math.isNaN(math.fraction(-2, 5)); // returns false * math.isNaN('-2'); // returns false * math.isNaN([2, 0, -3, NaN]'); // returns [false, false, false, true] * * See also: * * isNumeric, isNegative, isPositive, isZero, isInteger * * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested * @return {boolean} Returns true when `x` is NaN. * Throws an error in case of an unknown data type. */ var isNaN = typed('isNaN', { 'number': function (x) { return Number.isNaN(x); }, 'BigNumber': function (x) { return x.isNaN(); }, 'Fraction': function (x) { return false; }, 'Complex': function (x) { return Number.isNaN(x.re) && Number.isNaN(x.im); }, 'Unit': function (x) { return Number.isNaN(x.value); }, 'Array | Matrix': function (x) { return deepMap(x, Number.isNaN); } }); return isNaN; } exports.name = 'isNaN'; exports.factory = factory; /***/ }), /* 465 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Test whether a value is prime: has no divisors other than itself and one. * The function supports type `number`, `bignumber`. * * The function is evaluated element-wise in case of Array or Matrix input. * * Syntax: * * math.isPrime(x) * * Examples: * * math.isPrime(3); // returns true * math.isPrime(-2); // returns false * math.isPrime(0); // returns false * math.isPrime(-0); // returns false * math.isPrime(0.5); // returns false * math.isPrime('2'); // returns true * math.isPrime([2, 17, 100]'); // returns [true, true, false] * * See also: * * isNumeric, isZero, isNegative, isInteger * * @param {number | BigNumber | Array | Matrix} x Value to be tested * @return {boolean} Returns true when `x` is larger than zero. * Throws an error in case of an unknown data type. */ var isPrime = typed('isPrime', { 'number': function (x) { if (x < 2){ return false; } if (x == 2){ return true; } if (x % 2 == 0){ return false; } for (var i = 3; i * i <= x; i += 2){ if (x % i == 0){ return false; } } return true; }, 'BigNumber': function (x) { if (x.lt(2)){ return false; } if (x.equals(2)){ return true; } if (x.mod(2).isZero()){ return false; } for(var i = type.BigNumber(3); i.times(i).lte(x); i = i.plus(1)){ if (x.mod(i).isZero()){ return false; } } return true; }, 'Array | Matrix': function (x) { return deepMap(x, isPrime); } }); return isPrime; } exports.name = 'isPrime'; exports.factory = factory; /***/ }), /* 466 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(467) ]; /***/ }), /* 467 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { /** * Instantiate mathjs data types from their JSON representation * @param {string} key * @param {*} value * @returns {*} Returns the revived object */ return function reviver(key, value) { var constructor = type[value && value.mathjs]; if (constructor && typeof constructor.fromJSON === 'function') { return constructor.fromJSON(value); } return value; } } exports.name = 'reviver'; exports.path = 'json'; exports.factory = factory; /***/ }), /* 468 */ /***/ (function(module, exports, __webpack_require__) { var Decimal = __webpack_require__(505); function factory (type, config, load, typed, math) { var BigNumber = Decimal.clone({precision: config.precision}); /** * Attach type information */ BigNumber.prototype.type = 'BigNumber'; BigNumber.prototype.isBigNumber = true; /** * Get a JSON representation of a BigNumber containing * type information * @returns {Object} Returns a JSON object structured as: * `{"mathjs": "BigNumber", "value": "0.2"}` */ BigNumber.prototype.toJSON = function () { return { mathjs: 'BigNumber', value: this.toString() }; }; /** * Instantiate a BigNumber from a JSON object * @param {Object} json a JSON object structured as: * `{"mathjs": "BigNumber", "value": "0.2"}` * @return {BigNumber} */ BigNumber.fromJSON = function (json) { return new BigNumber(json.value); }; // listen for changed in the configuration, automatically apply changed precision math.on('config', function (curr, prev) { if (curr.precision !== prev.precision) { BigNumber.config({ precision: curr.precision }); } }); return BigNumber; } exports.name = 'BigNumber'; exports.path = 'type'; exports.factory = factory; exports.math = true; // request access to the math namespace /***/ }), /* 469 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Create a BigNumber, which can store numbers with arbitrary precision. * When a matrix is provided, all elements will be converted to BigNumber. * * Syntax: * * math.bignumber(x) * * Examples: * * 0.1 + 0.2; // returns number 0.30000000000000004 * math.bignumber(0.1) + math.bignumber(0.2); // returns BigNumber 0.3 * * * 7.2e500; // returns number Infinity * math.bignumber('7.2e500'); // returns BigNumber 7.2e500 * * See also: * * boolean, complex, index, matrix, string, unit * * @param {number | string | Fraction | BigNumber | Array | Matrix | boolean | null} [value] Value for the big number, * 0 by default. * @returns {BigNumber} The created bignumber */ var bignumber = typed('bignumber', { '': function () { return new type.BigNumber(0); }, 'number': function (x) { // convert to string to prevent errors in case of >15 digits return new type.BigNumber(x + ''); }, 'string': function (x) { return new type.BigNumber(x); }, 'BigNumber': function (x) { // we assume a BigNumber is immutable return x; }, 'Fraction': function (x) { return new type.BigNumber(x.n).div(x.d); }, 'Array | Matrix': function (x) { return deepMap(x, bignumber); } }); bignumber.toTex = { 0: '0', 1: '\\left(${args[0]}\\right)' }; return bignumber; } exports.name = 'bignumber'; exports.factory = factory; /***/ }), /* 470 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ // type __webpack_require__(468), // construction function __webpack_require__(469) ]; /***/ }), /* 471 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Create a boolean or convert a string or number to a boolean. * In case of a number, `true` is returned for non-zero numbers, and `false` in * case of zero. * Strings can be `'true'` or `'false'`, or can contain a number. * When value is a matrix, all elements will be converted to boolean. * * Syntax: * * math.boolean(x) * * Examples: * * math.boolean(0); // returns false * math.boolean(1); // returns true * math.boolean(-3); // returns true * math.boolean('true'); // returns true * math.boolean('false'); // returns false * math.boolean([1, 0, 1, 1]); // returns [true, false, true, true] * * See also: * * bignumber, complex, index, matrix, string, unit * * @param {string | number | boolean | Array | Matrix | null} value A value of any type * @return {boolean | Array | Matrix} The boolean value */ var bool = typed('bool', { '': function () { return false; }, 'boolean': function (x) { return x; }, 'number': function (x) { return !!x; }, 'BigNumber': function (x) { return !x.isZero(); }, 'string': function (x) { // try case insensitive var lcase = x.toLowerCase(); if (lcase === 'true') { return true; } else if (lcase === 'false') { return false; } // test whether value is a valid number var num = Number(x); if (x != '' && !isNaN(num)) { return !!num; } throw new Error('Cannot convert "' + x + '" to a boolean'); }, 'Array | Matrix': function (x) { return deepMap(x, bool); } }); return bool; } exports.name = 'boolean'; exports.factory = factory; /***/ }), /* 472 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var format = __webpack_require__(19).format; var lazy = __webpack_require__(4).lazy; function factory (type, config, load, typed, math) { /** * @constructor Chain * Wrap any value in a chain, allowing to perform chained operations on * the value. * * All methods available in the math.js library can be called upon the chain, * and then will be evaluated with the value itself as first argument. * The chain can be closed by executing chain.done(), which will return * the final value. * * The Chain has a number of special functions: * - done() Finalize the chained operation and return the * chain's value. * - valueOf() The same as done() * - toString() Returns a string representation of the chain's value. * * @param {*} [value] */ function Chain (value) { if (!(this instanceof Chain)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (value && value.isChain) { this.value = value.value; } else { this.value = value; } } /** * Attach type information */ Chain.prototype.type = 'Chain'; Chain.prototype.isChain = true; /** * Close the chain. Returns the final value. * Does the same as method valueOf() * @returns {*} value */ Chain.prototype.done = function () { return this.value; }; /** * Close the chain. Returns the final value. * Does the same as method done() * @returns {*} value */ Chain.prototype.valueOf = function () { return this.value; }; /** * Get a string representation of the value in the chain * @returns {string} */ Chain.prototype.toString = function () { return format(this.value); }; /** * Create a proxy method for the chain * @param {string} name * @param {Function} fn The function to be proxied * If fn is no function, it is silently ignored. * @private */ function createProxy(name, fn) { if (typeof fn === 'function') { Chain.prototype[name] = chainify(fn); } } /** * Create a proxy method for the chain * @param {string} name * @param {function} resolver The function resolving with the * function to be proxied * @private */ function createLazyProxy(name, resolver) { lazy(Chain.prototype, name, function outerResolver() { var fn = resolver(); if (typeof fn === 'function') { return chainify(fn); } return undefined; // if not a function, ignore }); } /** * Make a function chainable * @param {function} fn * @return {Function} chain function * @private */ function chainify (fn) { return function () { var args = [this.value]; // `this` will be the context of a Chain instance for (var i = 0; i < arguments.length; i++) { args[i + 1] = arguments[i]; } return new Chain(fn.apply(fn, args)); } } /** * Create a proxy for a single method, or an object with multiple methods. * Example usage: * * Chain.createProxy('add', function add (x, y) {...}); * Chain.createProxy({ * add: function add (x, y) {...}, * subtract: function subtract (x, y) {...} * } * * @param {string | Object} arg0 A name (string), or an object with * functions * @param {*} [arg1] A function, when arg0 is a name */ Chain.createProxy = function (arg0, arg1) { if (typeof arg0 === 'string') { // createProxy(name, value) createProxy(arg0, arg1); } else { // createProxy(values) for (var prop in arg0) { if (arg0.hasOwnProperty(prop)) { createProxy(prop, arg0[prop]); } } } }; // create proxy for everything that is in math.js Chain.createProxy(math); // register on the import event, automatically add a proxy for every imported function. math.on('import', function (name, resolver, path) { if (path === undefined) { // an imported function (not a data type or something special) createLazyProxy(name, resolver); } }); return Chain; } exports.name = 'Chain'; exports.path = 'type'; exports.factory = factory; exports.math = true; // require providing the math namespace as 5th argument exports.lazy = false; // we need to register a listener on the import events, so no lazy loading /***/ }), /* 473 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { /** * Wrap any value in a chain, allowing to perform chained operations on * the value. * * All methods available in the math.js library can be called upon the chain, * and then will be evaluated with the value itself as first argument. * The chain can be closed by executing `chain.done()`, which returns * the final value. * * The chain has a number of special functions: * * - `done()` Finalize the chain and return the chain's value. * - `valueOf()` The same as `done()` * - `toString()` Executes `math.format()` onto the chain's value, returning * a string representation of the value. * * Syntax: * * math.chain(value) * * Examples: * * math.chain(3) * .add(4) * .subtract(2) * .done(); // 5 * * math.chain( [[1, 2], [3, 4]] ) * .subset(math.index(0, 0), 8) * .multiply(3) * .done(); // [[24, 6], [9, 12]] * * @param {*} [value] A value of any type on which to start a chained operation. * @return {math.type.Chain} The created chain */ return typed('chain', { '': function() { return new type.Chain(); }, 'any': function(value) { return new type.Chain(value); } }); } exports.name = 'chain'; exports.factory = factory; /***/ }), /* 474 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ // type __webpack_require__(472), // construction function __webpack_require__(473) ]; /***/ }), /* 475 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { var latex = __webpack_require__(3); /** * Create a complex value or convert a value to a complex value. * * Syntax: * * math.complex() // creates a complex value with zero * // as real and imaginary part. * math.complex(re : number, im : string) // creates a complex value with provided * // values for real and imaginary part. * math.complex(re : number) // creates a complex value with provided * // real value and zero imaginary part. * math.complex(complex : Complex) // clones the provided complex value. * math.complex(arg : string) // parses a string into a complex value. * math.complex(array : Array) // converts the elements of the array * // or matrix element wise into a * // complex value. * math.complex({re: number, im: number}) // creates a complex value with provided * // values for real an imaginary part. * math.complex({r: number, phi: number}) // creates a complex value with provided * // polar coordinates * * Examples: * * var a = math.complex(3, -4); // a = Complex 3 - 4i * a.re = 5; // a = Complex 5 - 4i * var i = a.im; // Number -4; * var b = math.complex('2 + 6i'); // Complex 2 + 6i * var c = math.complex(); // Complex 0 + 0i * var d = math.add(a, b); // Complex 5 + 2i * * See also: * * bignumber, boolean, index, matrix, number, string, unit * * @param {* | Array | Matrix} [args] * Arguments specifying the real and imaginary part of the complex number * @return {Complex | Array | Matrix} Returns a complex value */ var complex = typed('complex', { '': function () { return type.Complex.ZERO; }, 'number': function (x) { return new type.Complex(x, 0); }, 'number, number': function (re, im) { return new type.Complex(re, im); }, // TODO: this signature should be redundant 'BigNumber, BigNumber': function (re, im) { return new type.Complex(re.toNumber(), im.toNumber()); }, 'Complex': function (x) { return x.clone(); }, 'string': function (x) { return type.Complex(x); // for example '2 + 3i' }, 'Object': function (x) { if('re' in x && 'im' in x) { return new type.Complex(x.re, x.im); } if ('r' in x && 'phi' in x) { return new type.Complex(x); } throw new Error('Expected object with either properties re and im, or properties r and phi.'); }, 'Array | Matrix': function (x) { return deepMap(x, complex); } }); complex.toTex = { 0: '0', 1: '\\left(${args[0]}\\right)', 2: '\\left(\\left(${args[0]}\\right)+' + latex.symbols['i'] + '\\cdot\\left(${args[1]}\\right)\\right)' }; return complex; } exports.name = 'complex'; exports.factory = factory; /***/ }), /* 476 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ // type __webpack_require__(124), // construction function __webpack_require__(475) ]; /***/ }), /* 477 */ /***/ (function(module, exports, __webpack_require__) { var Fraction = __webpack_require__(506); /** * Attach type information */ Fraction.prototype.type = 'Fraction'; Fraction.prototype.isFraction = true; /** * Get a JSON representation of a Fraction containing type information * @returns {Object} Returns a JSON object structured as: * `{"mathjs": "Fraction", "n": 3, "d": 8}` */ Fraction.prototype.toJSON = function () { return { mathjs: 'Fraction', n: this.s * this.n, d: this.d }; }; /** * Instantiate a Fraction from a JSON object * @param {Object} json a JSON object structured as: * `{"mathjs": "Fraction", "n": 3, "d": 8}` * @return {BigNumber} */ Fraction.fromJSON = function (json) { return new Fraction(json); }; function factory (type, config, load, typed) { return Fraction; } exports.name = 'Fraction'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 478 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ // type __webpack_require__(477), // construction function __webpack_require__(125) ]; /***/ }), /* 479 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ __webpack_require__(470), __webpack_require__(471), __webpack_require__(474), __webpack_require__(476), __webpack_require__(478), __webpack_require__(487), __webpack_require__(75), __webpack_require__(488), __webpack_require__(489), __webpack_require__(493) ]; /***/ }), /* 480 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var smaller = load(__webpack_require__(38)); var larger = load(__webpack_require__(26)); var oneOverLogPhi = 1.0 / Math.log((1.0 + Math.sqrt(5.0)) / 2.0); /** * Fibonacci Heap implementation, used interally for Matrix math. * @class FibonacciHeap * @constructor FibonacciHeap */ function FibonacciHeap() { if (!(this instanceof FibonacciHeap)) throw new SyntaxError('Constructor must be called with the new operator'); // initialize fields this._minimum = null; this._size = 0; } /** * Attach type information */ FibonacciHeap.prototype.type = 'FibonacciHeap'; FibonacciHeap.prototype.isFibonacciHeap = true; /** * Inserts a new data element into the heap. No heap consolidation is * performed at this time, the new node is simply inserted into the root * list of this heap. Running time: O(1) actual. * @memberof FibonacciHeap */ FibonacciHeap.prototype.insert = function (key, value) { // create node var node = { key: key, value: value, degree: 0 }; // check we have a node in the minimum if (this._minimum) { // minimum node var minimum = this._minimum; // update left & right of node node.left = minimum; node.right = minimum.right; minimum.right = node; node.right.left = node; // update minimum node in heap if needed if (smaller(key, minimum.key)) { // node has a smaller key, use it as minimum this._minimum = node; } } else { // set left & right node.left = node; node.right = node; // this is the first node this._minimum = node; } // increment number of nodes in heap this._size++; // return node return node; }; /** * Returns the number of nodes in heap. Running time: O(1) actual. * @memberof FibonacciHeap */ FibonacciHeap.prototype.size = function () { return this._size; }; /** * Removes all elements from this heap. * @memberof FibonacciHeap */ FibonacciHeap.prototype.clear = function () { this._minimum = null; this._size = 0; }; /** * Returns true if the heap is empty, otherwise false. * @memberof FibonacciHeap */ FibonacciHeap.prototype.isEmpty = function () { return !!this._minimum; }; /** * Extracts the node with minimum key from heap. Amortized running * time: O(log n). * @memberof FibonacciHeap */ FibonacciHeap.prototype.extractMinimum = function () { // node to remove var node = this._minimum; // check we have a minimum if (node === null) return node; // current minimum var minimum = this._minimum; // get number of children var numberOfChildren = node.degree; // pointer to the first child var x = node.child; // for each child of node do... while (numberOfChildren > 0) { // store node in right side var tempRight = x.right; // remove x from child list x.left.right = x.right; x.right.left = x.left; // add x to root list of heap x.left = minimum; x.right = minimum.right; minimum.right = x; x.right.left = x; // set Parent[x] to null x.parent = null; x = tempRight; numberOfChildren--; } // remove node from root list of heap node.left.right = node.right; node.right.left = node.left; // update minimum if (node == node.right) { // empty minimum = null; } else { // update minimum minimum = node.right; // we need to update the pointer to the root with minimum key minimum = _findMinimumNode(minimum, this._size); } // decrement size of heap this._size--; // update minimum this._minimum = minimum; // return node return node; }; /** * Removes a node from the heap given the reference to the node. The trees * in the heap will be consolidated, if necessary. This operation may fail * to remove the correct element if there are nodes with key value -Infinity. * Running time: O(log n) amortized. * @memberof FibonacciHeap */ FibonacciHeap.prototype.remove = function (node) { // decrease key value this._minimum = _decreaseKey(this._minimum, node, -1); // remove the smallest this.extractMinimum(); }; /** * Decreases the key value for a heap node, given the new value to take on. * The structure of the heap may be changed and will not be consolidated. * Running time: O(1) amortized. * @memberof FibonacciHeap */ var _decreaseKey = function (minimum, node, key) { // set node key node.key = key; // get parent node var parent = node.parent; if (parent && smaller(node.key, parent.key)) { // remove node from parent _cut(minimum, node, parent); // remove all nodes from parent to the root parent _cascadingCut(minimum, parent); } // update minimum node if needed if (smaller(node.key, minimum.key)) minimum = node; // return minimum return minimum; }; /** * The reverse of the link operation: removes node from the child list of parent. * This method assumes that min is non-null. Running time: O(1). * @memberof FibonacciHeap */ var _cut = function (minimum, node, parent) { // remove node from parent children and decrement Degree[parent] node.left.right = node.right; node.right.left = node.left; parent.degree--; // reset y.child if necessary if (parent.child == node) parent.child = node.right; // remove child if degree is 0 if (parent.degree === 0) parent.child = null; // add node to root list of heap node.left = minimum; node.right = minimum.right; minimum.right = node; node.right.left = node; // set parent[node] to null node.parent = null; // set mark[node] to false node.mark = false; }; /** * Performs a cascading cut operation. This cuts node from its parent and then * does the same for its parent, and so on up the tree. * Running time: O(log n); O(1) excluding the recursion. * @memberof FibonacciHeap */ var _cascadingCut= function (minimum, node) { // store parent node var parent = node.parent; // if there's a parent... if (!parent) return; // if node is unmarked, set it marked if (!node.mark) { node.mark = true; } else { // it's marked, cut it from parent _cut(minimum, node, parent); // cut its parent as well _cascadingCut(parent); } }; /** * Make the first node a child of the second one. Running time: O(1) actual. * @memberof FibonacciHeap */ var _linkNodes = function (node, parent) { // remove node from root list of heap node.left.right = node.right; node.right.left = node.left; // make node a Child of parent node.parent = parent; if (!parent.child) { parent.child = node; node.right = node; node.left = node; } else { node.left = parent.child; node.right = parent.child.right; parent.child.right = node; node.right.left = node; } // increase degree[parent] parent.degree++; // set mark[node] false node.mark = false; }; var _findMinimumNode = function (minimum, size) { // to find trees of the same degree efficiently we use an array of length O(log n) in which we keep a pointer to one root of each degree var arraySize = Math.floor(Math.log(size) * oneOverLogPhi) + 1; // create list with initial capacity var array = new Array(arraySize); // find the number of root nodes. var numRoots = 0; var x = minimum; if (x) { numRoots++; x = x.right; while (x !== minimum) { numRoots++; x = x.right; } } // vars var y; // For each node in root list do... while (numRoots > 0) { // access this node's degree.. var d = x.degree; // get next node var next = x.right; // check if there is a node already in array with the same degree while (true) { // get node with the same degree is any y = array[d]; if (!y) break; // make one node with the same degree a child of the other, do this based on the key value. if (larger(x.key, y.key)) { var temp = y; y = x; x = temp; } // make y a child of x _linkNodes(y, x); // we have handled this degree, go to next one. array[d] = null; d++; } // save this node for later when we might encounter another of the same degree. array[d] = x; // move forward through list. x = next; numRoots--; } // Set min to null (effectively losing the root list) and reconstruct the root list from the array entries in array[]. minimum = null; // loop nodes in array for (var i = 0; i < arraySize; i++) { // get current node y = array[i]; if (!y) continue; // check if we have a linked list if (minimum) { // First remove node from root list. y.left.right = y.right; y.right.left = y.left; // now add to root list, again. y.left = minimum; y.right = minimum.right; minimum.right = y; y.right.left = y; // check if this is a new min. if (smaller(y.key, minimum.key)) minimum = y; } else minimum = y; } return minimum; }; return FibonacciHeap; } exports.name = 'FibonacciHeap'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 481 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var string = util.string; var object = util.object; var isArray = Array.isArray; var isString = string.isString; function factory (type, config, load) { var DenseMatrix = load(__webpack_require__(126)); var smaller = load(__webpack_require__(38)); function ImmutableDenseMatrix(data, datatype) { if (!(this instanceof ImmutableDenseMatrix)) throw new SyntaxError('Constructor must be called with the new operator'); if (datatype && !isString(datatype)) throw new Error('Invalid datatype: ' + datatype); if ((data && data.isMatrix === true) || isArray(data)) { // use DenseMatrix implementation var matrix = new DenseMatrix(data, datatype); // internal structures this._data = matrix._data; this._size = matrix._size; this._datatype = matrix._datatype; this._min = null; this._max = null; } else if (data && isArray(data.data) && isArray(data.size)) { // initialize fields from JSON representation this._data = data.data; this._size = data.size; this._datatype = data.datatype; this._min = typeof data.min !== 'undefined' ? data.min : null; this._max = typeof data.max !== 'undefined' ? data.max : null; } else if (data) { // unsupported type throw new TypeError('Unsupported type of data (' + util.types.type(data) + ')'); } else { // nothing provided this._data = []; this._size = [0]; this._datatype = datatype; this._min = null; this._max = null; } } ImmutableDenseMatrix.prototype = new DenseMatrix(); /** * Attach type information */ ImmutableDenseMatrix.prototype.type = 'ImmutableDenseMatrix'; ImmutableDenseMatrix.prototype.isImmutableDenseMatrix = true; /** * Get a subset of the matrix, or replace a subset of the matrix. * * Usage: * var subset = matrix.subset(index) // retrieve subset * var value = matrix.subset(index, replacement) // replace subset * * @param {Index} index * @param {Array | ImmutableDenseMatrix | *} [replacement] * @param {*} [defaultValue=0] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be filled with zeros. */ ImmutableDenseMatrix.prototype.subset = function (index) { switch (arguments.length) { case 1: // use base implementation var m = DenseMatrix.prototype.subset.call(this, index); // check result is a matrix if (m.isMatrix) { // return immutable matrix return new ImmutableDenseMatrix({ data: m._data, size: m._size, datatype: m._datatype }); } return m; // intentional fall through case 2: case 3: throw new Error('Cannot invoke set subset on an Immutable Matrix instance'); default: throw new SyntaxError('Wrong number of arguments'); } }; /** * Replace a single element in the matrix. * @param {Number[]} index Zero-based index * @param {*} value * @param {*} [defaultValue] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be left undefined. * @return {ImmutableDenseMatrix} self */ ImmutableDenseMatrix.prototype.set = function () { throw new Error('Cannot invoke set on an Immutable Matrix instance'); }; /** * Resize the matrix to the given size. Returns a copy of the matrix when * `copy=true`, otherwise return the matrix itself (resize in place). * * @param {Number[]} size The new size the matrix should have. * @param {*} [defaultValue=0] Default value, filled in on new entries. * If not provided, the matrix elements will * be filled with zeros. * @param {boolean} [copy] Return a resized copy of the matrix * * @return {Matrix} The resized matrix */ ImmutableDenseMatrix.prototype.resize = function () { throw new Error('Cannot invoke resize on an Immutable Matrix instance'); }; /** * Create a clone of the matrix * @return {ImmutableDenseMatrix} clone */ ImmutableDenseMatrix.prototype.clone = function () { var m = new ImmutableDenseMatrix({ data: object.clone(this._data), size: object.clone(this._size), datatype: this._datatype }); return m; }; /** * Get a JSON representation of the matrix * @returns {Object} */ ImmutableDenseMatrix.prototype.toJSON = function () { return { mathjs: 'ImmutableDenseMatrix', data: this._data, size: this._size, datatype: this._datatype }; }; /** * Generate a matrix from a JSON object * @param {Object} json An object structured like * `{"mathjs": "ImmutableDenseMatrix", data: [], size: []}`, * where mathjs is optional * @returns {ImmutableDenseMatrix} */ ImmutableDenseMatrix.fromJSON = function (json) { return new ImmutableDenseMatrix(json); }; /** * Swap rows i and j in Matrix. * * @param {Number} i Matrix row index 1 * @param {Number} j Matrix row index 2 * * @return {Matrix} The matrix reference */ ImmutableDenseMatrix.prototype.swapRows = function () { throw new Error('Cannot invoke swapRows on an Immutable Matrix instance'); }; /** * Calculate the minimum value in the set * @return {Number | undefined} min */ ImmutableDenseMatrix.prototype.min = function () { // check min has been calculated before if (this._min === null) { // minimum var m = null; // compute min this.forEach(function (v) { if (m === null || smaller(v, m)) m = v; }); this._min = m !== null ? m : undefined; } return this._min; }; /** * Calculate the maximum value in the set * @return {Number | undefined} max */ ImmutableDenseMatrix.prototype.max = function () { // check max has been calculated before if (this._max === null) { // maximum var m = null; // compute max this.forEach(function (v) { if (m === null || smaller(m, v)) m = v; }); this._max = m !== null ? m : undefined; } return this._max; }; // exports return ImmutableDenseMatrix; } exports.name = 'ImmutableDenseMatrix'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 482 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var clone = __webpack_require__(4).clone; var isInteger = __webpack_require__(2).isInteger; function factory (type) { /** * Create an index. An Index can store ranges and sets for multiple dimensions. * Matrix.get, Matrix.set, and math.subset accept an Index as input. * * Usage: * var index = new Index(range1, range2, matrix1, array1, ...); * * Where each parameter can be any of: * A number * A string (containing a name of an object property) * An instance of Range * An Array with the Set values * A Matrix with the Set values * * The parameters start, end, and step must be integer numbers. * * @class Index * @Constructor Index * @param {...*} ranges */ function Index(ranges) { if (!(this instanceof Index)) { throw new SyntaxError('Constructor must be called with the new operator'); } this._dimensions = []; this._isScalar = true; for (var i = 0, ii = arguments.length; i < ii; i++) { var arg = arguments[i]; if (arg && (arg.isRange === true)) { this._dimensions.push(arg); this._isScalar = false; } else if (arg && (Array.isArray(arg) || arg.isMatrix === true)) { // create matrix var m = _createImmutableMatrix(arg.valueOf()); this._dimensions.push(m); // size var size = m.size(); // scalar if (size.length !== 1 || size[0] !== 1) { this._isScalar = false; } } else if (typeof arg === 'number') { this._dimensions.push(_createImmutableMatrix([arg])); } else if (typeof arg === 'string') { // object property (arguments.count should be 1) this._dimensions.push(arg); } // TODO: implement support for wildcard '*' else { throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range'); } } } /** * Attach type information */ Index.prototype.type = 'Index'; Index.prototype.isIndex = true; function _createImmutableMatrix(arg) { // loop array elements for (var i = 0, l = arg.length; i < l; i++) { if (typeof arg[i] !== 'number' || !isInteger(arg[i])) { throw new TypeError('Index parameters must be positive integer numbers'); } } // create matrix return new type.ImmutableDenseMatrix(arg); } /** * Create a clone of the index * @memberof Index * @return {Index} clone */ Index.prototype.clone = function () { var index = new Index(); index._dimensions = clone(this._dimensions); index._isScalar = this._isScalar; return index; }; /** * Create an index from an array with ranges/numbers * @memberof Index * @param {Array.} ranges * @return {Index} index * @private */ Index.create = function (ranges) { var index = new Index(); Index.apply(index, ranges); return index; }; /** * Retrieve the size of the index, the number of elements for each dimension. * @memberof Index * @returns {number[]} size */ Index.prototype.size = function () { var size = []; for (var i = 0, ii = this._dimensions.length; i < ii; i++) { var d = this._dimensions[i]; size[i] = (typeof d === 'string') ? 1 : d.size()[0]; } return size; }; /** * Get the maximum value for each of the indexes ranges. * @memberof Index * @returns {number[]} max */ Index.prototype.max = function () { var values = []; for (var i = 0, ii = this._dimensions.length; i < ii; i++) { var range = this._dimensions[i]; values[i] = (typeof range === 'string') ? range : range.max(); } return values; }; /** * Get the minimum value for each of the indexes ranges. * @memberof Index * @returns {number[]} min */ Index.prototype.min = function () { var values = []; for (var i = 0, ii = this._dimensions.length; i < ii; i++) { var range = this._dimensions[i]; values[i] = (typeof range === 'string') ? range : range.min(); } return values; }; /** * Loop over each of the ranges of the index * @memberof Index * @param {Function} callback Called for each range with a Range as first * argument, the dimension as second, and the * index object as third. */ Index.prototype.forEach = function (callback) { for (var i = 0, ii = this._dimensions.length; i < ii; i++) { callback(this._dimensions[i], i, this); } }; /** * Retrieve the dimension for the given index * @memberof Index * @param {Number} dim Number of the dimension * @returns {Range | null} range */ Index.prototype.dimension = function (dim) { return this._dimensions[dim] || null; }; /** * Test whether this index contains an object property * @returns {boolean} Returns true if the index is an object property */ Index.prototype.isObjectProperty = function () { return this._dimensions.length === 1 && typeof this._dimensions[0] === 'string'; }; /** * Returns the object property name when the Index holds a single object property, * else returns null * @returns {string | null} */ Index.prototype.getObjectProperty = function () { return this.isObjectProperty() ? this._dimensions[0] : null; }; /** * Test whether this index contains only a single value. * * This is the case when the index is created with only scalar values as ranges, * not for ranges resolving into a single value. * @memberof Index * @return {boolean} isScalar */ Index.prototype.isScalar = function () { return this._isScalar; }; /** * Expand the Index into an array. * For example new Index([0,3], [2,7]) returns [[0,1,2], [2,3,4,5,6]] * @memberof Index * @returns {Array} array */ Index.prototype.toArray = function () { var array = []; for (var i = 0, ii = this._dimensions.length; i < ii; i++) { var dimension = this._dimensions[i]; array.push((typeof dimension === 'string') ? dimension : dimension.toArray()); } return array; }; /** * Get the primitive value of the Index, a two dimensional array. * Equivalent to Index.toArray(). * @memberof Index * @returns {Array} array */ Index.prototype.valueOf = Index.prototype.toArray; /** * Get the string representation of the index, for example '[2:6]' or '[0:2:10, 4:7, [1,2,3]]' * @memberof Index * @returns {String} str */ Index.prototype.toString = function () { var strings = []; for (var i = 0, ii = this._dimensions.length; i < ii; i++) { var dimension = this._dimensions[i]; if (typeof dimension === 'string') { strings.push(JSON.stringify(dimension)); } else { strings.push(dimension.toString()); } } return '[' + strings.join(', ') + ']'; }; /** * Get a JSON representation of the Index * @memberof Index * @returns {Object} Returns a JSON object structured as: * `{"mathjs": "Index", "ranges": [{"mathjs": "Range", start: 0, end: 10, step:1}, ...]}` */ Index.prototype.toJSON = function () { return { mathjs: 'Index', dimensions: this._dimensions }; }; /** * Instantiate an Index from a JSON object * @memberof Index * @param {Object} json A JSON object structured as: * `{"mathjs": "Index", "dimensions": [{"mathjs": "Range", start: 0, end: 10, step:1}, ...]}` * @return {Index} */ Index.fromJSON = function (json) { return Index.create(json.dimensions); }; return Index; } exports.name = 'Index'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 483 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load) { var add = load(__webpack_require__(15)); var equalScalar = load(__webpack_require__(8)); /** * An ordered Sparse Accumulator is a representation for a sparse vector that includes a dense array * of the vector elements and an ordered list of non-zero elements. */ function Spa() { if (!(this instanceof Spa)) throw new SyntaxError('Constructor must be called with the new operator'); // allocate vector, TODO use typed arrays this._values = []; this._heap = new type.FibonacciHeap(); } /** * Attach type information */ Spa.prototype.type = 'Spa'; Spa.prototype.isSpa = true; /** * Set the value for index i. * * @param {number} i The index * @param {number | BigNumber | Complex} The value at index i */ Spa.prototype.set = function (i, v) { // check we have a value @ i if (!this._values[i]) { // insert in heap var node = this._heap.insert(i, v); // set the value @ i this._values[i] = node; } else { // update the value @ i this._values[i].value = v; } }; Spa.prototype.get = function (i) { var node = this._values[i]; if (node) return node.value; return 0; }; Spa.prototype.accumulate = function (i, v) { // node @ i var node = this._values[i]; if (!node) { // insert in heap node = this._heap.insert(i, v); // initialize value this._values[i] = node; } else { // accumulate value node.value = add(node.value, v); } }; Spa.prototype.forEach = function (from, to, callback) { // references var heap = this._heap; var values = this._values; // nodes var nodes = []; // node with minimum key, save it var node = heap.extractMinimum(); if (node) nodes.push(node); // extract nodes from heap (ordered) while (node && node.key <= to) { // check it is in range if (node.key >= from) { // check value is not zero if (!equalScalar(node.value, 0)) { // invoke callback callback(node.key, node.value, this); } } // extract next node, save it node = heap.extractMinimum(); if (node) nodes.push(node); } // reinsert all nodes in heap for (var i = 0; i < nodes.length; i++) { // current node var n = nodes[i]; // insert node in heap node = heap.insert(n.key, n.value); // update values values[node.key] = node; } }; Spa.prototype.swap = function (i, j) { // node @ i and j var nodei = this._values[i]; var nodej = this._values[j]; // check we need to insert indeces if (!nodei && nodej) { // insert in heap nodei = this._heap.insert(i, nodej.value); // remove from heap this._heap.remove(nodej); // set values this._values[i] = nodei; this._values[j] = undefined; } else if (nodei && !nodej) { // insert in heap nodej = this._heap.insert(j, nodei.value); // remove from heap this._heap.remove(nodei); // set values this._values[j] = nodej; this._values[i] = undefined; } else if (nodei && nodej) { // swap values var v = nodei.value; nodei.value = nodej.value; nodej.value = v; } }; return Spa; } exports.name = 'Spa'; exports.path = 'type'; exports.factory = factory; /***/ }), /* 484 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var util = __webpack_require__(23); var DimensionError = __webpack_require__(9); var array = util.array; var object = util.object; var string = util.string; var number = util.number; var isArray = Array.isArray; var isNumber = number.isNumber; var isInteger = number.isInteger; var isString = string.isString; var validateIndex = array.validateIndex; function factory (type, config, load, typed) { var Matrix = load(__webpack_require__(72)); // force loading Matrix (do not use via type.Matrix) var equalScalar = load(__webpack_require__(8)); /** * Sparse Matrix implementation. This type implements a Compressed Column Storage format * for sparse matrices. * @class SparseMatrix */ function SparseMatrix(data, datatype) { if (!(this instanceof SparseMatrix)) throw new SyntaxError('Constructor must be called with the new operator'); if (datatype && !isString(datatype)) throw new Error('Invalid datatype: ' + datatype); if (data && data.isMatrix === true) { // create from matrix _createFromMatrix(this, data, datatype); } else if (data && isArray(data.index) && isArray(data.ptr) && isArray(data.size)) { // initialize fields this._values = data.values; this._index = data.index; this._ptr = data.ptr; this._size = data.size; this._datatype = datatype || data.datatype; } else if (isArray(data)) { // create from array _createFromArray(this, data, datatype); } else if (data) { // unsupported type throw new TypeError('Unsupported type of data (' + util.types.type(data) + ')'); } else { // nothing provided this._values = []; this._index = []; this._ptr = [0]; this._size = [0, 0]; this._datatype = datatype; } } var _createFromMatrix = function (matrix, source, datatype) { // check matrix type if (source.type === 'SparseMatrix') { // clone arrays matrix._values = source._values ? object.clone(source._values) : undefined; matrix._index = object.clone(source._index); matrix._ptr = object.clone(source._ptr); matrix._size = object.clone(source._size); matrix._datatype = datatype || source._datatype; } else { // build from matrix data _createFromArray(matrix, source.valueOf(), datatype || source._datatype); } }; var _createFromArray = function (matrix, data, datatype) { // initialize fields matrix._values = []; matrix._index = []; matrix._ptr = []; matrix._datatype = datatype; // discover rows & columns, do not use math.size() to avoid looping array twice var rows = data.length; var columns = 0; // equal signature to use var eq = equalScalar; // zero value var zero = 0; if (isString(datatype)) { // find signature that matches (datatype, datatype) eq = typed.find(equalScalar, [datatype, datatype]) || equalScalar; // convert 0 to the same datatype zero = typed.convert(0, datatype); } // check we have rows (empty array) if (rows > 0) { // column index var j = 0; do { // store pointer to values index matrix._ptr.push(matrix._index.length); // loop rows for (var i = 0; i < rows; i++) { // current row var row = data[i]; // check row is an array if (isArray(row)) { // update columns if needed (only on first column) if (j === 0 && columns < row.length) columns = row.length; // check row has column if (j < row.length) { // value var v = row[j]; // check value != 0 if (!eq(v, zero)) { // store value matrix._values.push(v); // index matrix._index.push(i); } } } else { // update columns if needed (only on first column) if (j === 0 && columns < 1) columns = 1; // check value != 0 (row is a scalar) if (!eq(row, zero)) { // store value matrix._values.push(row); // index matrix._index.push(i); } } } // increment index j++; } while (j < columns); } // store number of values in ptr matrix._ptr.push(matrix._index.length); // size matrix._size = [rows, columns]; }; SparseMatrix.prototype = new Matrix(); /** * Attach type information */ SparseMatrix.prototype.type = 'SparseMatrix'; SparseMatrix.prototype.isSparseMatrix = true; /** * Get the storage format used by the matrix. * * Usage: * var format = matrix.storage() // retrieve storage format * * @memberof SparseMatrix * @return {string} The storage format. */ SparseMatrix.prototype.storage = function () { return 'sparse'; }; /** * Get the datatype of the data stored in the matrix. * * Usage: * var format = matrix.datatype() // retrieve matrix datatype * * @memberof SparseMatrix * @return {string} The datatype. */ SparseMatrix.prototype.datatype = function () { return this._datatype; }; /** * Create a new SparseMatrix * @memberof SparseMatrix * @param {Array} data * @param {string} [datatype] */ SparseMatrix.prototype.create = function (data, datatype) { return new SparseMatrix(data, datatype); }; /** * Get the matrix density. * * Usage: * var density = matrix.density() // retrieve matrix density * * @memberof SparseMatrix * @return {number} The matrix density. */ SparseMatrix.prototype.density = function () { // rows & columns var rows = this._size[0]; var columns = this._size[1]; // calculate density return rows !== 0 && columns !== 0 ? (this._index.length / (rows * columns)) : 0; }; /** * Get a subset of the matrix, or replace a subset of the matrix. * * Usage: * var subset = matrix.subset(index) // retrieve subset * var value = matrix.subset(index, replacement) // replace subset * * @memberof SparseMatrix * @param {Index} index * @param {Array | Maytrix | *} [replacement] * @param {*} [defaultValue=0] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be filled with zeros. */ SparseMatrix.prototype.subset = function (index, replacement, defaultValue) { // check it is a pattern matrix if (!this._values) throw new Error('Cannot invoke subset on a Pattern only matrix'); // check arguments switch (arguments.length) { case 1: return _getsubset(this, index); // intentional fall through case 2: case 3: return _setsubset(this, index, replacement, defaultValue); default: throw new SyntaxError('Wrong number of arguments'); } }; var _getsubset = function (matrix, idx) { // check idx if (!idx || idx.isIndex !== true) { throw new TypeError('Invalid index'); } var isScalar = idx.isScalar(); if (isScalar) { // return a scalar return matrix.get(idx.min()); } // validate dimensions var size = idx.size(); if (size.length != matrix._size.length) { throw new DimensionError(size.length, matrix._size.length); } // vars var i, ii, k, kk; // validate if any of the ranges in the index is out of range var min = idx.min(); var max = idx.max(); for (i = 0, ii = matrix._size.length; i < ii; i++) { validateIndex(min[i], matrix._size[i]); validateIndex(max[i], matrix._size[i]); } // matrix arrays var mvalues = matrix._values; var mindex = matrix._index; var mptr = matrix._ptr; // rows & columns dimensions for result matrix var rows = idx.dimension(0); var columns = idx.dimension(1); // workspace & permutation vector var w = []; var pv = []; // loop rows in resulting matrix rows.forEach(function (i, r) { // update permutation vector pv[i] = r[0]; // mark i in workspace w[i] = true; }); // result matrix arrays var values = mvalues ? [] : undefined; var index = []; var ptr = []; // loop columns in result matrix columns.forEach(function (j) { // update ptr ptr.push(index.length); // loop values in column j for (k = mptr[j], kk = mptr[j + 1]; k < kk; k++) { // row i = mindex[k]; // check row is in result matrix if (w[i] === true) { // push index index.push(pv[i]); // check we need to process values if (values) values.push(mvalues[k]); } } }); // update ptr ptr.push(index.length); // return matrix return new SparseMatrix({ values: values, index: index, ptr: ptr, size: size, datatype: matrix._datatype }); }; var _setsubset = function (matrix, index, submatrix, defaultValue) { // check index if (!index || index.isIndex !== true) { throw new TypeError('Invalid index'); } // get index size and check whether the index contains a single value var iSize = index.size(), isScalar = index.isScalar(); // calculate the size of the submatrix, and convert it into an Array if needed var sSize; if (submatrix && submatrix.isMatrix === true) { // submatrix size sSize = submatrix.size(); // use array representation submatrix = submatrix.toArray(); } else { // get submatrix size (array, scalar) sSize = array.size(submatrix); } // check index is a scalar if (isScalar) { // verify submatrix is a scalar if (sSize.length !== 0) { throw new TypeError('Scalar expected'); } // set value matrix.set(index.min(), submatrix, defaultValue); } else { // validate dimensions, index size must be one or two dimensions if (iSize.length !== 1 && iSize.length !== 2) { throw new DimensionError(iSize.length, matrix._size.length, '<'); } // check submatrix and index have the same dimensions if (sSize.length < iSize.length) { // calculate number of missing outer dimensions var i = 0; var outer = 0; while (iSize[i] === 1 && sSize[i] === 1) { i++; } while (iSize[i] === 1) { outer++; i++; } // unsqueeze both outer and inner dimensions submatrix = array.unsqueeze(submatrix, iSize.length, outer, sSize); } // check whether the size of the submatrix matches the index size if (!object.deepEqual(iSize, sSize)) { throw new DimensionError(iSize, sSize, '>'); } // offsets var x0 = index.min()[0]; var y0 = index.min()[1]; // submatrix rows and columns var m = sSize[0]; var n = sSize[1]; // loop submatrix for (var x = 0; x < m; x++) { // loop columns for (var y = 0; y < n; y++) { // value at i, j var v = submatrix[x][y]; // invoke set (zero value will remove entry from matrix) matrix.set([x + x0, y + y0], v, defaultValue); } } } return matrix; }; /** * Get a single element from the matrix. * @memberof SparseMatrix * @param {number[]} index Zero-based index * @return {*} value */ SparseMatrix.prototype.get = function (index) { if (!isArray(index)) throw new TypeError('Array expected'); if (index.length != this._size.length) throw new DimensionError(index.length, this._size.length); // check it is a pattern matrix if (!this._values) throw new Error('Cannot invoke get on a Pattern only matrix'); // row and column var i = index[0]; var j = index[1]; // check i, j are valid validateIndex(i, this._size[0]); validateIndex(j, this._size[1]); // find value index var k = _getValueIndex(i, this._ptr[j], this._ptr[j + 1], this._index); // check k is prior to next column k and it is in the correct row if (k < this._ptr[j + 1] && this._index[k] === i) return this._values[k]; return 0; }; /** * Replace a single element in the matrix. * @memberof SparseMatrix * @param {number[]} index Zero-based index * @param {*} value * @param {*} [defaultValue] Default value, filled in on new entries when * the matrix is resized. If not provided, * new matrix elements will be set to zero. * @return {SparseMatrix} self */ SparseMatrix.prototype.set = function (index, v, defaultValue) { if (!isArray(index)) throw new TypeError('Array expected'); if (index.length != this._size.length) throw new DimensionError(index.length, this._size.length); // check it is a pattern matrix if (!this._values) throw new Error('Cannot invoke set on a Pattern only matrix'); // row and column var i = index[0]; var j = index[1]; // rows & columns var rows = this._size[0]; var columns = this._size[1]; // equal signature to use var eq = equalScalar; // zero value var zero = 0; if (isString(this._datatype)) { // find signature that matches (datatype, datatype) eq = typed.find(equalScalar, [this._datatype, this._datatype]) || equalScalar; // convert 0 to the same datatype zero = typed.convert(0, this._datatype); } // check we need to resize matrix if (i > rows - 1 || j > columns - 1) { // resize matrix _resize(this, Math.max(i + 1, rows), Math.max(j + 1, columns), defaultValue); // update rows & columns rows = this._size[0]; columns = this._size[1]; } // check i, j are valid validateIndex(i, rows); validateIndex(j, columns); // find value index var k = _getValueIndex(i, this._ptr[j], this._ptr[j + 1], this._index); // check k is prior to next column k and it is in the correct row if (k < this._ptr[j + 1] && this._index[k] === i) { // check value != 0 if (!eq(v, zero)) { // update value this._values[k] = v; } else { // remove value from matrix _remove(k, j, this._values, this._index, this._ptr); } } else { // insert value @ (i, j) _insert(k, i, j, v, this._values, this._index, this._ptr); } return this; }; var _getValueIndex = function(i, top, bottom, index) { // check row is on the bottom side if (bottom - top === 0) return bottom; // loop rows [top, bottom[ for (var r = top; r < bottom; r++) { // check we found value index if (index[r] === i) return r; } // we did not find row return top; }; var _remove = function (k, j, values, index, ptr) { // remove value @ k values.splice(k, 1); index.splice(k, 1); // update pointers for (var x = j + 1; x < ptr.length; x++) ptr[x]--; }; var _insert = function (k, i, j, v, values, index, ptr) { // insert value values.splice(k, 0, v); // update row for k index.splice(k, 0, i); // update column pointers for (var x = j + 1; x < ptr.length; x++) ptr[x]++; }; /** * Resize the matrix to the given size. Returns a copy of the matrix when * `copy=true`, otherwise return the matrix itself (resize in place). * * @memberof SparseMatrix * @param {number[]} size The new size the matrix should have. * @param {*} [defaultValue=0] Default value, filled in on new entries. * If not provided, the matrix elements will * be filled with zeros. * @param {boolean} [copy] Return a resized copy of the matrix * * @return {Matrix} The resized matrix */ SparseMatrix.prototype.resize = function (size, defaultValue, copy) { // validate arguments if (!isArray(size)) throw new TypeError('Array expected'); if (size.length !== 2) throw new Error('Only two dimensions matrix are supported'); // check sizes size.forEach(function (value) { if (!number.isNumber(value) || !number.isInteger(value) || value < 0) { throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + string.format(size) + ')'); } }); // matrix to resize var m = copy ? this.clone() : this; // resize matrix return _resize(m, size[0], size[1], defaultValue); }; var _resize = function (matrix, rows, columns, defaultValue) { // value to insert at the time of growing matrix var value = defaultValue || 0; // equal signature to use var eq = equalScalar; // zero value var zero = 0; if (isString(matrix._datatype)) { // find signature that matches (datatype, datatype) eq = typed.find(equalScalar, [matrix._datatype, matrix._datatype]) || equalScalar; // convert 0 to the same datatype zero = typed.convert(0, matrix._datatype); // convert value to the same datatype value = typed.convert(value, matrix._datatype); } // should we insert the value? var ins = !eq(value, zero); // old columns and rows var r = matrix._size[0]; var c = matrix._size[1]; var i, j, k; // check we need to increase columns if (columns > c) { // loop new columns for (j = c; j < columns; j++) { // update matrix._ptr for current column matrix._ptr[j] = matrix._values.length; // check we need to insert matrix._values if (ins) { // loop rows for (i = 0; i < r; i++) { // add new matrix._values matrix._values.push(value); // update matrix._index matrix._index.push(i); } } } // store number of matrix._values in matrix._ptr matrix._ptr[columns] = matrix._values.length; } else if (columns < c) { // truncate matrix._ptr matrix._ptr.splice(columns + 1, c - columns); // truncate matrix._values and matrix._index matrix._values.splice(matrix._ptr[columns], matrix._values.length); matrix._index.splice(matrix._ptr[columns], matrix._index.length); } // update columns c = columns; // check we need to increase rows if (rows > r) { // check we have to insert values if (ins) { // inserts var n = 0; // loop columns for (j = 0; j < c; j++) { // update matrix._ptr for current column matrix._ptr[j] = matrix._ptr[j] + n; // where to insert matrix._values k = matrix._ptr[j + 1] + n; // pointer var p = 0; // loop new rows, initialize pointer for (i = r; i < rows; i++, p++) { // add value matrix._values.splice(k + p, 0, value); // update matrix._index matrix._index.splice(k + p, 0, i); // increment inserts n++; } } // store number of matrix._values in matrix._ptr matrix._ptr[c] = matrix._values.length; } } else if (rows < r) { // deletes var d = 0; // loop columns for (j = 0; j < c; j++) { // update matrix._ptr for current column matrix._ptr[j] = matrix._ptr[j] - d; // where matrix._values start for next column var k0 = matrix._ptr[j]; var k1 = matrix._ptr[j + 1] - d; // loop matrix._index for (k = k0; k < k1; k++) { // row i = matrix._index[k]; // check we need to delete value and matrix._index if (i > rows - 1) { // remove value matrix._values.splice(k, 1); // remove item from matrix._index matrix._index.splice(k, 1); // increase deletes d++; } } } // update matrix._ptr for current column matrix._ptr[j] = matrix._values.length; } // update matrix._size matrix._size[0] = rows; matrix._size[1] = columns; // return matrix return matrix; }; /** * Create a clone of the matrix * @memberof SparseMatrix * @return {SparseMatrix} clone */ SparseMatrix.prototype.clone = function () { var m = new SparseMatrix({ values: this._values ? object.clone(this._values) : undefined, index: object.clone(this._index), ptr: object.clone(this._ptr), size: object.clone(this._size), datatype: this._datatype }); return m; }; /** * Retrieve the size of the matrix. * @memberof SparseMatrix * @returns {number[]} size */ SparseMatrix.prototype.size = function() { return this._size.slice(0); // copy the Array }; /** * Create a new matrix with the results of the callback function executed on * each entry of the matrix. * @memberof SparseMatrix * @param {Function} callback The callback function is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix being traversed. * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. * * @return {SparseMatrix} matrix */ SparseMatrix.prototype.map = function (callback, skipZeros) { // check it is a pattern matrix if (!this._values) throw new Error('Cannot invoke map on a Pattern only matrix'); // matrix instance var me = this; // rows and columns var rows = this._size[0]; var columns = this._size[1]; // invoke callback var invoke = function (v, i, j) { // invoke callback return callback(v, [i, j], me); }; // invoke _map return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros); }; /** * Create a new matrix with the results of the callback function executed on the interval * [minRow..maxRow, minColumn..maxColumn]. */ var _map = function (matrix, minRow, maxRow, minColumn, maxColumn, callback, skipZeros) { // result arrays var values = []; var index = []; var ptr = []; // equal signature to use var eq = equalScalar; // zero value var zero = 0; if (isString(matrix._datatype)) { // find signature that matches (datatype, datatype) eq = typed.find(equalScalar, [matrix._datatype, matrix._datatype]) || equalScalar; // convert 0 to the same datatype zero = typed.convert(0, matrix._datatype); } // invoke callback var invoke = function (v, x, y) { // invoke callback v = callback(v, x, y); // check value != 0 if (!eq(v, zero)) { // store value values.push(v); // index index.push(x); } }; // loop columns for (var j = minColumn; j <= maxColumn; j++) { // store pointer to values index ptr.push(values.length); // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = matrix._ptr[j]; var k1 = matrix._ptr[j + 1]; // row pointer var p = minRow; // loop k within [k0, k1[ for (var k = k0; k < k1; k++) { // row index var i = matrix._index[k]; // check i is in range if (i >= minRow && i <= maxRow) { // zero values if (!skipZeros) { for (var x = p; x < i; x++) invoke(0, x - minRow, j - minColumn); } // value @ k invoke(matrix._values[k], i - minRow, j - minColumn); } // update pointer p = i + 1; } // zero values if (!skipZeros) { for (var y = p; y <= maxRow; y++) invoke(0, y - minRow, j - minColumn); } } // store number of values in ptr ptr.push(values.length); // return sparse matrix return new SparseMatrix({ values: values, index: index, ptr: ptr, size: [maxRow - minRow + 1, maxColumn - minColumn + 1] }); }; /** * Execute a callback function on each entry of the matrix. * @memberof SparseMatrix * @param {Function} callback The callback function is invoked with three * parameters: the value of the element, the index * of the element, and the Matrix being traversed. * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. */ SparseMatrix.prototype.forEach = function (callback, skipZeros) { // check it is a pattern matrix if (!this._values) throw new Error('Cannot invoke forEach on a Pattern only matrix'); // matrix instance var me = this; // rows and columns var rows = this._size[0]; var columns = this._size[1]; // loop columns for (var j = 0; j < columns; j++) { // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = this._ptr[j]; var k1 = this._ptr[j + 1]; // column pointer var p = 0; // loop k within [k0, k1[ for (var k = k0; k < k1; k++) { // row index var i = this._index[k]; // check we need to process zeros if (!skipZeros) { // zero values for (var x = p; x < i; x++) callback(0, [x, j], me); } // value @ k callback(this._values[k], [i, j], me); // update pointer p = i + 1; } // check we need to process zeros if (!skipZeros) { // zero values for (var y = p; y < rows; y++) callback(0, [y, j], me); } } }; /** * Create an Array with a copy of the data of the SparseMatrix * @memberof SparseMatrix * @returns {Array} array */ SparseMatrix.prototype.toArray = function () { return _toArray(this._values, this._index, this._ptr, this._size, true); }; /** * Get the primitive value of the SparseMatrix: a two dimensions array * @memberof SparseMatrix * @returns {Array} array */ SparseMatrix.prototype.valueOf = function () { return _toArray(this._values, this._index, this._ptr, this._size, false); }; var _toArray = function (values, index, ptr, size, copy) { // rows and columns var rows = size[0]; var columns = size[1]; // result var a = []; // vars var i, j; // initialize array for (i = 0; i < rows; i++) { a[i] = []; for (j = 0; j < columns; j++) a[i][j] = 0; } // loop columns for (j = 0; j < columns; j++) { // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = ptr[j]; var k1 = ptr[j + 1]; // loop k within [k0, k1[ for (var k = k0; k < k1; k++) { // row index i = index[k]; // set value (use one for pattern matrix) a[i][j] = values ? (copy ? object.clone(values[k]) : values[k]) : 1; } } return a; }; /** * Get a string representation of the matrix, with optional formatting options. * @memberof SparseMatrix * @param {Object | number | Function} [options] Formatting options. See * lib/utils/number:format for a * description of the available * options. * @returns {string} str */ SparseMatrix.prototype.format = function (options) { // rows and columns var rows = this._size[0]; var columns = this._size[1]; // density var density = this.density(); // rows & columns var str = 'Sparse Matrix [' + string.format(rows, options) + ' x ' + string.format(columns, options) + '] density: ' + string.format(density, options) + '\n'; // loop columns for (var j = 0; j < columns; j++) { // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = this._ptr[j]; var k1 = this._ptr[j + 1]; // loop k within [k0, k1[ for (var k = k0; k < k1; k++) { // row index var i = this._index[k]; // append value str += '\n (' + string.format(i, options) + ', ' + string.format(j, options) + ') ==> ' + (this._values ? string.format(this._values[k], options) : 'X'); } } return str; }; /** * Get a string representation of the matrix * @memberof SparseMatrix * @returns {string} str */ SparseMatrix.prototype.toString = function () { return string.format(this.toArray()); }; /** * Get a JSON representation of the matrix * @memberof SparseMatrix * @returns {Object} */ SparseMatrix.prototype.toJSON = function () { return { mathjs: 'SparseMatrix', values: this._values, index: this._index, ptr: this._ptr, size: this._size, datatype: this._datatype }; }; /** * Get the kth Matrix diagonal. * * @memberof SparseMatrix * @param {number | BigNumber} [k=0] The kth diagonal where the vector will retrieved. * * @returns {Matrix} The matrix vector with the diagonal values. */ SparseMatrix.prototype.diagonal = function(k) { // validate k if any if (k) { // convert BigNumber to a number if (k.isBigNumber === true) k = k.toNumber(); // is must be an integer if (!isNumber(k) || !isInteger(k)) { throw new TypeError ('The parameter k must be an integer number'); } } else { // default value k = 0; } var kSuper = k > 0 ? k : 0; var kSub = k < 0 ? -k : 0; // rows & columns var rows = this._size[0]; var columns = this._size[1]; // number diagonal values var n = Math.min(rows - kSub, columns - kSuper); // diagonal arrays var values = []; var index = []; var ptr = []; // initial ptr value ptr[0] = 0; // loop columns for (var j = kSuper; j < columns && values.length < n; j++) { // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = this._ptr[j]; var k1 = this._ptr[j + 1]; // loop x within [k0, k1[ for (var x = k0; x < k1; x++) { // row index var i = this._index[x]; // check row if (i === j - kSuper + kSub) { // value on this column values.push(this._values[x]); // store row index[values.length - 1] = i - kSub; // exit loop break; } } } // close ptr ptr.push(values.length); // return matrix return new SparseMatrix({ values: values, index: index, ptr: ptr, size: [n, 1] }); }; /** * Generate a matrix from a JSON object * @memberof SparseMatrix * @param {Object} json An object structured like * `{"mathjs": "SparseMatrix", "values": [], "index": [], "ptr": [], "size": []}`, * where mathjs is optional * @returns {SparseMatrix} */ SparseMatrix.fromJSON = function (json) { return new SparseMatrix(json); }; /** * Create a diagonal matrix. * * @memberof SparseMatrix * @param {Array} size The matrix size. * @param {number | Array | Matrix } value The values for the diagonal. * @param {number | BigNumber} [k=0] The kth diagonal where the vector will be filled in. * @param {string} [datatype] The Matrix datatype, values must be of this datatype. * * @returns {SparseMatrix} */ SparseMatrix.diagonal = function (size, value, k, defaultValue, datatype) { if (!isArray(size)) throw new TypeError('Array expected, size parameter'); if (size.length !== 2) throw new Error('Only two dimensions matrix are supported'); // map size & validate size = size.map(function (s) { // check it is a big number if (s && s.isBigNumber === true) { // convert it s = s.toNumber(); } // validate arguments if (!isNumber(s) || !isInteger(s) || s < 1) { throw new Error('Size values must be positive integers'); } return s; }); // validate k if any if (k) { // convert BigNumber to a number if (k.isBigNumber === true) k = k.toNumber(); // is must be an integer if (!isNumber(k) || !isInteger(k)) { throw new TypeError ('The parameter k must be an integer number'); } } else { // default value k = 0; } // equal signature to use var eq = equalScalar; // zero value var zero = 0; if (isString(datatype)) { // find signature that matches (datatype, datatype) eq = typed.find(equalScalar, [datatype, datatype]) || equalScalar; // convert 0 to the same datatype zero = typed.convert(0, datatype); } var kSuper = k > 0 ? k : 0; var kSub = k < 0 ? -k : 0; // rows and columns var rows = size[0]; var columns = size[1]; // number of non-zero items var n = Math.min(rows - kSub, columns - kSuper); // value extraction function var _value; // check value if (isArray(value)) { // validate array if (value.length !== n) { // number of values in array must be n throw new Error('Invalid value array length'); } // define function _value = function (i) { // return value @ i return value[i]; }; } else if (value && value.isMatrix === true) { // matrix size var ms = value.size(); // validate matrix if (ms.length !== 1 || ms[0] !== n) { // number of values in array must be n throw new Error('Invalid matrix length'); } // define function _value = function (i) { // return value @ i return value.get([i]); }; } else { // define function _value = function () { // return value return value; }; } // create arrays var values = []; var index = []; var ptr = []; // loop items for (var j = 0; j < columns; j++) { // number of rows with value ptr.push(values.length); // diagonal index var i = j - kSuper; // check we need to set diagonal value if (i >= 0 && i < n) { // get value @ i var v = _value(i); // check for zero if (!eq(v, zero)) { // column index.push(i + kSub); // add value values.push(v); } } } // last value should be number of values ptr.push(values.length); // create SparseMatrix return new SparseMatrix({ values: values, index: index, ptr: ptr, size: [rows, columns] }); }; /** * Swap rows i and j in Matrix. * * @memberof SparseMatrix * @param {number} i Matrix row index 1 * @param {number} j Matrix row index 2 * * @return {Matrix} The matrix reference */ SparseMatrix.prototype.swapRows = function (i, j) { // check index if (!isNumber(i) || !isInteger(i) || !isNumber(j) || !isInteger(j)) { throw new Error('Row index must be positive integers'); } // check dimensions if (this._size.length !== 2) { throw new Error('Only two dimensional matrix is supported'); } // validate index validateIndex(i, this._size[0]); validateIndex(j, this._size[0]); // swap rows SparseMatrix._swapRows(i, j, this._size[1], this._values, this._index, this._ptr); // return current instance return this; }; /** * Loop rows with data in column j. * * @param {number} j Column * @param {Array} values Matrix values * @param {Array} index Matrix row indeces * @param {Array} ptr Matrix column pointers * @param {Function} callback Callback function invoked for every row in column j */ SparseMatrix._forEachRow = function (j, values, index, ptr, callback) { // indeces for column j var k0 = ptr[j]; var k1 = ptr[j + 1]; // loop for (var k = k0; k < k1; k++) { // invoke callback callback(index[k], values[k]); } }; /** * Swap rows x and y in Sparse Matrix data structures. * * @param {number} x Matrix row index 1 * @param {number} y Matrix row index 2 * @param {number} columns Number of columns in matrix * @param {Array} values Matrix values * @param {Array} index Matrix row indeces * @param {Array} ptr Matrix column pointers */ SparseMatrix._swapRows = function (x, y, columns, values, index, ptr) { // loop columns for (var j = 0; j < columns; j++) { // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] var k0 = ptr[j]; var k1 = ptr[j + 1]; // find value index @ x var kx = _getValueIndex(x, k0, k1, index); // find value index @ x var ky = _getValueIndex(y, k0, k1, index); // check both rows exist in matrix if (kx < k1 && ky < k1 && index[kx] === x && index[ky] === y) { // swap values (check for pattern matrix) if (values) { var v = values[kx]; values[kx] = values[ky]; values[ky] = v; } // next column continue; } // check x row exist & no y row if (kx < k1 && index[kx] === x && (ky >= k1 || index[ky] !== y)) { // value @ x (check for pattern matrix) var vx = values ? values[kx] : undefined; // insert value @ y index.splice(ky, 0, y); if (values) values.splice(ky, 0, vx); // remove value @ x (adjust array index if needed) index.splice(ky <= kx ? kx + 1 : kx, 1); if (values) values.splice(ky <= kx ? kx + 1 : kx, 1); // next column continue; } // check y row exist & no x row if (ky < k1 && index[ky] === y && (kx >= k1 || index[kx] !== x)) { // value @ y (check for pattern matrix) var vy = values ? values[ky] : undefined; // insert value @ x index.splice(kx, 0, x); if (values) values.splice(kx, 0, vy); // remove value @ y (adjust array index if needed) index.splice(kx <= ky ? ky + 1 : ky, 1); if (values) values.splice(kx <= ky ? ky + 1 : ky, 1); } } }; // register this type in the base class Matrix type.Matrix._storage.sparse = SparseMatrix; return SparseMatrix; } exports.name = 'SparseMatrix'; exports.path = 'type'; exports.factory = factory; exports.lazy = false; // no lazy loading, as we alter type.Matrix._storage /***/ }), /* 485 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { /** * 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. * * Syntax: * * math.index(range1, range2, ...) * * Where each range can be any of: * * - A number * - A string for getting/setting an object property * - An instance of `Range` * - A one-dimensional Array or a Matrix with numbers * * Indexes must be zero-based, integer numbers. * * Examples: * * var math = math.js * * var b = [1, 2, 3, 4, 5]; * math.subset(b, math.index([1, 2, 3])); // returns [2, 3, 4] * * var a = math.matrix([[1, 2], [3, 4]]); * a.subset(math.index(0, 1)); // returns 2 * * See also: * * bignumber, boolean, complex, matrix, number, string, unit * * @param {...*} ranges Zero or more ranges or numbers. * @return {Index} Returns the created index */ return typed('index', { '...number | string | BigNumber | Range | Array | Matrix': function (args) { var ranges = args.map(function (arg) { if (arg && arg.isBigNumber === true) { return arg.toNumber(); // convert BigNumber to Number } else if (arg && (Array.isArray(arg) || arg.isMatrix === true)) { return arg.map(function (elem) { // convert BigNumber to Number return (elem && elem.isBigNumber === true) ? elem.toNumber() : elem; }); } else { return arg; } }); var res = new type.Index(); type.Index.apply(res, ranges); return res; } }); } exports.name = 'index'; exports.factory = factory; /***/ }), /* 486 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; function factory (type, config, load, typed) { var SparseMatrix = type.SparseMatrix; /** * Create a Sparse Matrix. The function creates a new `math.type.Matrix` object from * an `Array`. A Matrix has utility functions to manipulate the data in the * matrix, like getting the size and getting or setting values in the matrix. * * Syntax: * * math.sparse() // creates an empty sparse matrix. * math.sparse(data) // creates a sparse matrix with initial data. * math.sparse(data, 'number') // creates a sparse matrix with initial data, number datatype. * * Examples: * * var m = math.sparse([[1, 2], [3, 4]]); * m.size(); // Array [2, 2] * m.resize([3, 2], 5); * m.valueOf(); // Array [[1, 2], [3, 4], [5, 5]] * m.get([1, 0]) // number 3 * * See also: * * bignumber, boolean, complex, index, number, string, unit, matrix * * @param {Array | Matrix} [data] A two dimensional array * * @return {Matrix} The created matrix */ var sparse = typed('sparse', { '': function () { return new SparseMatrix([]); }, 'string': function (datatype) { return new SparseMatrix([], datatype); }, 'Array | Matrix': function (data) { return new SparseMatrix(data); }, 'Array | Matrix, string': function (data, datatype) { return new SparseMatrix(data, datatype); } }); sparse.toTex = { 0: '\\begin{bsparse}\\end{bsparse}', 1: '\\left(${args[0]}\\right)' }; return sparse; } exports.name = 'sparse'; exports.factory = factory; /***/ }), /* 487 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ // types __webpack_require__(72), __webpack_require__(126), __webpack_require__(484), __webpack_require__(483), __webpack_require__(480), __webpack_require__(481), __webpack_require__(482), __webpack_require__(127), // construction functions __webpack_require__(485), __webpack_require__(1), __webpack_require__(486) ]; /***/ }), /* 488 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ // type __webpack_require__(129) ]; /***/ }), /* 489 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); var number = __webpack_require__(2); function factory (type, config, load, typed) { /** * Create a string or convert any object into a string. * Elements of Arrays and Matrices are processed element wise. * * Syntax: * * math.string(value) * * Examples: * * math.string(4.2); // returns string '4.2' * math.string(math.complex(3, 2); // returns string '3 + 2i' * * var u = math.unit(5, 'km'); * math.string(u.to('m')); // returns string '5000 m' * * math.string([true, false]); // returns ['true', 'false'] * * See also: * * bignumber, boolean, complex, index, matrix, number, unit * * @param {* | Array | Matrix | null} [value] A value to convert to a string * @return {string | Array | Matrix} The created string */ var string = typed('string', { '': function () { return ''; }, 'number': number.format, 'null': function (x) { return 'null'; }, 'boolean': function (x) { return x + ''; }, 'string': function (x) { return x; }, 'Array | Matrix': function (x) { return deepMap(x, string); }, 'any': function (x) { return String(x); } }); string.toTex = { 0: '\\mathtt{""}', 1: '\\mathrm{string}\\left(${args[0]}\\right)' }; return string; } exports.name = 'string'; exports.factory = factory; /***/ }), /* 490 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Create a user-defined unit and register it with the Unit type. * * Syntax: * * math.createUnit({ * baseUnit1: { * aliases: [string, ...] * prefixes: object * }, * unit2: { * definition: string, * aliases: [string, ...] * prefixes: object, * offset: number * }, * unit3: string // Shortcut * }) * * // Another shortcut: * math.createUnit(string, unit : string, [object]) * * Examples: * * math.createUnit('foo'); * math.createUnit('knot', {definition: '0.514444444 m/s', aliases: ['knots', 'kt', 'kts']}); * math.createUnit('mph', '1 mile/hour'); * * @param {string} name The name of the new unit. Must be unique. Example: 'knot' * @param {string, Unit} definition Definition of the unit in terms of existing units. For example, '0.514444444 m / s'. * @param {Object} options (optional) An object containing any of the following properties: * prefixes {string} "none", "short", "long", "binary_short", or "binary_long". The default is "none". * aliases {Array} Array of strings. Example: ['knots', 'kt', 'kts'] * offset {Numeric} An offset to apply when converting from the unit. For example, the offset for celsius is 273.15. Default is 0. * * See also: * * unit * * @return {Unit} The new unit */ var createUnit = typed('createUnit', { // General function signature. First parameter is an object where each property is the definition of a new unit. The object keys are the unit names and the values are the definitions. The values can be objects, strings, or Units. If a property is an empty object or an empty string, a new base unit is created. The second parameter is the options. 'Object, Object': function(obj, options) { return type.Unit.createUnit(obj, options); }, // Same as above but without the options. 'Object': function(obj) { return type.Unit.createUnit(obj, {}); }, // Shortcut method for creating one unit. 'string, Unit | string | Object, Object': function (name, def, options) { var obj = {}; obj[name] = def; return type.Unit.createUnit(obj, options); }, // Same as above but without the options. 'string, Unit | string | Object': function (name, def) { var obj = {}; obj[name] = def; return type.Unit.createUnit(obj, {}); }, // Without a definition, creates a base unit. 'string': function (name) { var obj = {}; obj[name] = {}; return type.Unit.createUnit(obj, {}); }, }); return createUnit; } exports.name = 'createUnit'; exports.factory = factory; /***/ }), /* 491 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Split a unit in an array of units whose sum is equal to the original unit. * * Syntax: * * splitUnit(unit: Unit, parts: Array.) * * Example: * * math.splitUnit(new Unit(1, 'm'), ['feet', 'inch']); * // [ 3 feet, 3.3700787401575 inch ] * * See also: * * unit * * @param {Array} [parts] An array of strings or valueless units. * @return {Array} An array of units. */ var splitUnit = typed('splitUnit', { 'Unit, Array': function(unit, parts) { return unit.splitUnit(parts); } }); return splitUnit; } exports.name = 'splitUnit'; exports.factory = factory; /***/ }), /* 492 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var deepMap = __webpack_require__(0); function factory (type, config, load, typed) { /** * Create a unit. Depending on the passed arguments, the function * will create and return a new math.type.Unit object. * When a matrix is provided, all elements will be converted to units. * * Syntax: * * math.unit(unit : string) * math.unit(value : number, unit : string) * * Examples: * * var a = math.unit(5, 'cm'); // returns Unit 50 mm * var b = math.unit('23 kg'); // returns Unit 23 kg * a.to('m'); // returns Unit 0.05 m * * See also: * * bignumber, boolean, complex, index, matrix, number, string, createUnit * * @param {* | Array | Matrix} args A number and unit. * @return {Unit | Array | Matrix} The created unit */ var unit = typed('unit', { 'Unit': function (x) { return x.clone(); }, 'string': function (x) { if (type.Unit.isValuelessUnit(x)) { return new type.Unit(null, x); // a pure unit } return type.Unit.parse(x); // a unit with value, like '5cm' }, 'number | BigNumber | Fraction | Complex, string': function (value, unit) { return new type.Unit(value, unit); }, 'Array | Matrix': function (x) { return deepMap(x, unit); } }); unit.toTex = { 1: '\\left(${args[0]}\\right)', 2: '\\left(\\left(${args[0]}\\right)${args[1]}\\right)' }; return unit; } exports.name = 'unit'; exports.factory = factory; /***/ }), /* 493 */ /***/ (function(module, exports, __webpack_require__) { module.exports = [ // type __webpack_require__(130), // construction function __webpack_require__(492), // create new units __webpack_require__(490), // split units __webpack_require__(491), // physical constants __webpack_require__(494) ]; /***/ }), /* 494 */ /***/ (function(module, exports, __webpack_require__) { var lazy = __webpack_require__(4).lazy; function factory (type, config, load, typed, math) { // helper function to create a unit with a fixed prefix function fixedUnit(str) { var unit = type.Unit.parse(str); unit.fixPrefix = true; return unit; } // Source: http://www.wikiwand.com/en/Physical_constant // Universal constants lazy(math, 'speedOfLight', function () {return fixedUnit('299792458 m s^-1')}); lazy(math, 'gravitationConstant', function () {return fixedUnit('6.6738480e-11 m^3 kg^-1 s^-2')}); lazy(math, 'planckConstant', function () {return fixedUnit('6.626069311e-34 J s')}); lazy(math, 'reducedPlanckConstant',function () {return fixedUnit('1.05457172647e-34 J s')}); // Electromagnetic constants lazy(math, 'magneticConstant', function () {return fixedUnit('1.2566370614e-6 N A^-2')}); lazy(math, 'electricConstant', function () {return fixedUnit('8.854187817e-12 F m^-1')}); lazy(math, 'vacuumImpedance', function () {return fixedUnit('376.730313461 ohm')}); lazy(math, 'coulomb', function () {return fixedUnit('8.9875517873681764e9 N m^2 C^-2')}); lazy(math, 'elementaryCharge', function () {return fixedUnit('1.60217656535e-19 C')}); lazy(math, 'bohrMagneton', function () {return fixedUnit('9.2740096820e-24 J T^-1')}); lazy(math, 'conductanceQuantum', function () {return fixedUnit('7.748091734625e-5 S')}); lazy(math, 'inverseConductanceQuantum', function () {return fixedUnit('12906.403721742 ohm')}); lazy(math, 'magneticFluxQuantum', function () {return fixedUnit('2.06783375846e-15 Wb')}); lazy(math, 'nuclearMagneton', function () {return fixedUnit('5.0507835311e-27 J T^-1')}); lazy(math, 'klitzing', function () {return fixedUnit('25812.807443484 ohm')}); //lazy(math, 'josephson', function () {return fixedUnit('4.8359787011e-14 Hz V^-1')}); // TODO: support for Hz needed // Atomic and nuclear constants lazy(math, 'bohrRadius', function () {return fixedUnit('5.291772109217e-11 m')}); lazy(math, 'classicalElectronRadius', function () {return fixedUnit('2.817940326727e-15 m')}); lazy(math, 'electronMass', function () {return fixedUnit('9.1093829140e-31 kg')}); lazy(math, 'fermiCoupling', function () {return fixedUnit('1.1663645e-5 GeV^-2')}); lazy(math, 'fineStructure', function () {return 7.297352569824e-3}); lazy(math, 'hartreeEnergy', function () {return fixedUnit('4.3597443419e-18 J')}); lazy(math, 'protonMass', function () {return fixedUnit('1.67262177774e-27 kg')}); lazy(math, 'deuteronMass', function () {return fixedUnit('3.3435830926e-27 kg')}); lazy(math, 'neutronMass', function () {return fixedUnit('1.6749271613e-27 kg')}); lazy(math, 'quantumOfCirculation', function () {return fixedUnit('3.636947552024e-4 m^2 s^-1')}); lazy(math, 'rydberg', function () {return fixedUnit('10973731.56853955 m^-1')}); lazy(math, 'thomsonCrossSection', function () {return fixedUnit('6.65245873413e-29 m^2')}); lazy(math, 'weakMixingAngle', function () {return 0.222321}); lazy(math, 'efimovFactor', function () {return 22.7}); // Physico-chemical constants lazy(math, 'atomicMass', function () {return fixedUnit('1.66053892173e-27 kg')}); lazy(math, 'avogadro', function () {return fixedUnit('6.0221412927e23 mol^-1')}); lazy(math, 'boltzmann', function () {return fixedUnit('1.380648813e-23 J K^-1')}); lazy(math, 'faraday', function () {return fixedUnit('96485.336521 C mol^-1')}); lazy(math, 'firstRadiation', function () {return fixedUnit('3.7417715317e-16 W m^2')}); // lazy(math, 'spectralRadiance', function () {return fixedUnit('1.19104286953e-16 W m^2 sr^-1')}); // TODO spectralRadiance lazy(math, 'loschmidt', function () {return fixedUnit('2.686780524e25 m^-3')}); lazy(math, 'gasConstant', function () {return fixedUnit('8.314462175 J K^-1 mol^-1')}); lazy(math, 'molarPlanckConstant', function () {return fixedUnit('3.990312717628e-10 J s mol^-1')}); lazy(math, 'molarVolume', function () {return fixedUnit('2.241396820e-10 m^3 mol^-1')}); lazy(math, 'sackurTetrode', function () {return -1.164870823}); lazy(math, 'secondRadiation', function () {return fixedUnit('1.438777013e-2 m K')}); lazy(math, 'stefanBoltzmann', function () {return fixedUnit('5.67037321e-8 W m^-2 K^-4')}); lazy(math, 'wienDisplacement', function () {return fixedUnit('2.897772126e-3 m K')}); // Adopted values lazy(math, 'molarMass', function () {return fixedUnit('1e-3 kg mol^-1')}); lazy(math, 'molarMassC12', function () {return fixedUnit('1.2e-2 kg mol^-1')}); lazy(math, 'gravity', function () {return fixedUnit('9.80665 m s^-2')}); // atm is defined in Unit.js // Natural units lazy(math, 'planckLength', function () {return fixedUnit('1.61619997e-35 m')}); lazy(math, 'planckMass', function () {return fixedUnit('2.1765113e-8 kg')}); lazy(math, 'planckTime', function () {return fixedUnit('5.3910632e-44 s')}); lazy(math, 'planckCharge', function () {return fixedUnit('1.87554595641e-18 C')}); lazy(math, 'planckTemperature', function () {return fixedUnit('1.41683385e+32 K')}); } exports.factory = factory; exports.lazy = false; // no lazy loading of constants, the constants themselves are lazy when needed exports.math = true; // request access to the math namespace /***/ }), /* 495 */ /***/ (function(module, exports, __webpack_require__) { var bitwise = __webpack_require__(77); /** * Bitwise and for Bignumbers * * Special Cases: * N & n = N * n & 0 = 0 * n & -1 = n * n & n = n * I & I = I * -I & -I = -I * I & -I = 0 * I & n = n * I & -n = I * -I & n = 0 * -I & -n = -I * * @param {BigNumber} x * @param {BigNumber} y * @return {BigNumber} Result of `x` & `y`, is fully precise * @private */ module.exports = function bitAnd(x, y) { if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) { throw new Error('Integers expected in function bitAnd'); } var BigNumber = x.constructor; if (x.isNaN() || y.isNaN()) { return new BigNumber(NaN); } if (x.isZero() || y.eq(-1) || x.eq(y)) { return x; } if (y.isZero() || x.eq(-1)) { return y; } if (!x.isFinite() || !y.isFinite()) { if (!x.isFinite() && !y.isFinite()) { if (x.isNegative() == y.isNegative()) { return x; } return new BigNumber(0); } if (!x.isFinite()) { if (y.isNegative()) { return x; } if (x.isNegative()) { return new BigNumber(0); } return y; } if (!y.isFinite()) { if (x.isNegative()) { return y; } if (y.isNegative()) { return new BigNumber(0); } return x; } } return bitwise(x, y, function (a, b) { return a & b }); }; /***/ }), /* 496 */ /***/ (function(module, exports, __webpack_require__) { var bitwise = __webpack_require__(77); /** * Bitwise OR for BigNumbers * * Special Cases: * N | n = N * n | 0 = n * n | -1 = -1 * n | n = n * I | I = I * -I | -I = -I * I | -n = -1 * I | -I = -1 * I | n = I * -I | n = -I * -I | -n = -n * * @param {BigNumber} x * @param {BigNumber} y * @return {BigNumber} Result of `x` | `y`, fully precise */ module.exports = function bitOr (x, y) { if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) { throw new Error('Integers expected in function bitOr'); } var BigNumber = x.constructor; if (x.isNaN() || y.isNaN()) { return new BigNumber(NaN); } var negOne = new BigNumber(-1); if (x.isZero() || y.eq(negOne) || x.eq(y)) { return y; } if (y.isZero() || x.eq(negOne)) { return x; } if (!x.isFinite() || !y.isFinite()) { if ((!x.isFinite() && !x.isNegative() && y.isNegative()) || (x.isNegative() && !y.isNegative() && !y.isFinite())) { return negOne; } if (x.isNegative() && y.isNegative()) { return x.isFinite() ? x : y; } return x.isFinite() ? y : x; } return bitwise(x, y, function (a, b) { return a | b }); }; /***/ }), /* 497 */ /***/ (function(module, exports, __webpack_require__) { var bitwise = __webpack_require__(77); var bitNot = __webpack_require__(76); /** * Bitwise XOR for BigNumbers * * Special Cases: * N ^ n = N * n ^ 0 = n * n ^ n = 0 * n ^ -1 = ~n * I ^ n = I * I ^ -n = -I * I ^ -I = -1 * -I ^ n = -I * -I ^ -n = I * * @param {BigNumber} x * @param {BigNumber} y * @return {BigNumber} Result of `x` ^ `y`, fully precise * */ module.exports = function bitXor(x, y) { if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) { throw new Error('Integers expected in function bitXor'); } var BigNumber = x.constructor; if (x.isNaN() || y.isNaN()) { return new BigNumber(NaN); } if (x.isZero()) { return y; } if (y.isZero()) { return x; } if (x.eq(y)) { return new BigNumber(0); } var negOne = new BigNumber(-1); if (x.eq(negOne)) { return bitNot(y); } if (y.eq(negOne)) { return bitNot(x); } if (!x.isFinite() || !y.isFinite()) { if (!x.isFinite() && !y.isFinite()) { return negOne; } return new BigNumber(x.isNegative() == y.isNegative() ? Infinity : -Infinity); } return bitwise(x, y, function (a, b) { return a ^ b }); }; /***/ }), /* 498 */ /***/ (function(module, exports) { /** * Convert a BigNumber to a formatted string representation. * * Syntax: * * format(value) * format(value, options) * format(value, precision) * format(value, fn) * * Where: * * {number} value The value to be formatted * {Object} options An object with formatting options. Available options: * {string} notation * Number notation. Choose from: * 'fixed' Always use regular number notation. * For example '123.40' and '14000000' * 'exponential' Always use exponential notation. * For example '1.234e+2' and '1.4e+7' * 'auto' (default) Regular number notation for numbers * having an absolute value between * `lower` and `upper` bounds, and uses * exponential notation elsewhere. * Lower bound is included, upper bound * is excluded. * For example '123.4' and '1.4e7'. * {number} precision A number between 0 and 16 to round * the digits of the number. * In case of notations 'exponential' and * 'auto', `precision` defines the total * number of significant digits returned * and is undefined by default. * In case of notation 'fixed', * `precision` defines the number of * significant digits after the decimal * point, and is 0 by default. * {Object} exponential An object containing two parameters, * {number} lower and {number} upper, * used by notation 'auto' to determine * when to return exponential notation. * Default values are `lower=1e-3` and * `upper=1e5`. * Only applicable for notation `auto`. * {Function} fn A custom formatting function. Can be used to override the * built-in notations. Function `fn` is called with `value` as * parameter and must return a string. Is useful for example to * format all values inside a matrix in a particular way. * * Examples: * * format(6.4); // '6.4' * format(1240000); // '1.24e6' * format(1/3); // '0.3333333333333333' * format(1/3, 3); // '0.333' * format(21385, 2); // '21000' * format(12.071, {notation: 'fixed'}); // '12' * format(2.3, {notation: 'fixed', precision: 2}); // '2.30' * format(52.8, {notation: 'exponential'}); // '5.28e+1' * * @param {BigNumber} value * @param {Object | Function | number} [options] * @return {string} str The formatted value */ exports.format = function (value, options) { if (typeof options === 'function') { // handle format(value, fn) return options(value); } // handle special cases if (!value.isFinite()) { return value.isNaN() ? 'NaN' : (value.gt(0) ? 'Infinity' : '-Infinity'); } // default values for options var notation = 'auto'; var precision = undefined; if (options !== undefined) { // determine notation from options if (options.notation) { notation = options.notation; } // determine precision from options if (typeof options === 'number') { precision = options; } else if (options.precision) { precision = options.precision; } } // handle the various notations switch (notation) { case 'fixed': return exports.toFixed(value, precision); case 'exponential': return exports.toExponential(value, precision); case 'auto': // determine lower and upper bound for exponential notation. // TODO: implement support for upper and lower to be BigNumbers themselves var lower = 1e-3; var upper = 1e5; if (options && options.exponential) { if (options.exponential.lower !== undefined) { lower = options.exponential.lower; } if (options.exponential.upper !== undefined) { upper = options.exponential.upper; } } // adjust the configuration of the BigNumber constructor (yeah, this is quite tricky...) var oldConfig = { toExpNeg: value.constructor.toExpNeg, toExpPos: value.constructor.toExpPos }; value.constructor.config({ toExpNeg: Math.round(Math.log(lower) / Math.LN10), toExpPos: Math.round(Math.log(upper) / Math.LN10) }); // handle special case zero if (value.isZero()) return '0'; // determine whether or not to output exponential notation var str; var abs = value.abs(); if (abs.gte(lower) && abs.lt(upper)) { // normal number notation str = value.toSignificantDigits(precision).toFixed(); } else { // exponential notation str = exports.toExponential(value, precision); } // remove trailing zeros after the decimal point return str.replace(/((\.\d*?)(0+))($|e)/, function () { var digits = arguments[2]; var e = arguments[4]; return (digits !== '.') ? digits + e : e; }); default: throw new Error('Unknown notation "' + notation + '". ' + 'Choose "auto", "exponential", or "fixed".'); } }; /** * Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3' * @param {BigNumber} value * @param {number} [precision] Number of digits in formatted output. * If not provided, the maximum available digits * is used. * @returns {string} str */ exports.toExponential = function (value, precision) { if (precision !== undefined) { return value.toExponential(precision - 1); // Note the offset of one } else { return value.toExponential(); } }; /** * Format a number with fixed notation. * @param {BigNumber} value * @param {number} [precision=0] Optional number of decimals after the * decimal point. Zero by default. */ exports.toFixed = function (value, precision) { return value.toFixed(precision || 0); // Note: the (precision || 0) is needed as the toFixed of BigNumber has an // undefined default precision instead of 0. }; /***/ }), /* 499 */ /***/ (function(module, exports) { /** * Bitwise left shift * * Special Cases: * n << -n = N * n << N = N * N << n = N * n << 0 = n * 0 << n = 0 * I << I = N * I << n = I * n << I = I * * @param {BigNumber} x * @param {BigNumber} y * @return {BigNumber} Result of `x` << `y` * */ module.exports = function leftShift (x, y) { if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) { throw new Error('Integers expected in function leftShift'); } var BigNumber = x.constructor; if (x.isNaN() || y.isNaN() || (y.isNegative() && !y.isZero())) { return new BigNumber(NaN); } if (x.isZero() || y.isZero()) { return x; } if (!x.isFinite() && !y.isFinite()) { return new BigNumber(NaN); } // Math.pow(2, y) is fully precise for y < 55, and fast if (y.lt(55)) { return x.times(Math.pow(2, y.toNumber()) + ''); } return x.times(new BigNumber(2).pow(y)); }; /***/ }), /* 500 */ /***/ (function(module, exports) { /* * Special Cases: * n >> -n = N * n >> N = N * N >> n = N * I >> I = N * n >> 0 = n * I >> n = I * -I >> n = -I * -I >> I = -I * n >> I = I * -n >> I = -1 * 0 >> n = 0 * * @param {BigNumber} value * @param {BigNumber} value * @return {BigNumber} Result of `x` >> `y` * */ module.exports = function rightArithShift (x, y) { if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) { throw new Error('Integers expected in function rightArithShift'); } var BigNumber = x.constructor; if (x.isNaN() || y.isNaN() || (y.isNegative() && !y.isZero())) { return new BigNumber(NaN); } if (x.isZero() || y.isZero()) { return x; } if (!y.isFinite()) { if (x.isNegative()) { return new BigNumber(-1); } if (!x.isFinite()) { return new BigNumber(NaN); } return new BigNumber(0); } // Math.pow(2, y) is fully precise for y < 55, and fast if (y.lt(55)) { return x.div(Math.pow(2, y.toNumber()) + '').floor(); } return x.div(new BigNumber(2).pow(y)).floor(); }; /***/ }), /* 501 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * Test whether value is a boolean * @param {*} value * @return {boolean} isBoolean */ exports.isBoolean = function(value) { return typeof value == 'boolean'; }; /***/ }), /* 502 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; module.exports = function scatter(a, j, w, x, u, mark, c, f, inverse, update, value) { // a arrays var avalues = a._values; var aindex = a._index; var aptr = a._ptr; // c arrays var cindex = c._index; // vars var k, k0, k1, i; // check we need to process values (pattern matrix) if (x) { // values in j for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row i = aindex[k]; // check value exists in current j if (w[i] !== mark) { // i is new entry in j w[i] = mark; // add i to pattern of C cindex.push(i); // x(i) = A, check we need to call function this time if (update) { // copy value to workspace calling callback function x[i] = inverse ? f(avalues[k], value) : f(value, avalues[k]); // function was called on current row u[i] = mark; } else { // copy value to workspace x[i] = avalues[k]; } } else { // i exists in C already x[i] = inverse ? f(avalues[k], x[i]) : f(x[i], avalues[k]); // function was called on current row u[i] = mark; } } } else { // values in j for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { // row i = aindex[k]; // check value exists in current j if (w[i] !== mark) { // i is new entry in j w[i] = mark; // add i to pattern of C cindex.push(i); } else { // indicate function was called on current row u[i] = mark; } } } }; /***/ }), /* 503 */ /***/ (function(module, exports) { module.exports = '3.9.3'; // Note: This file is automatically generated when building math.js. // Changes made in this file will be overwritten. /***/ }), /* 504 */ /***/ (function(module, exports, __webpack_require__) { var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** * @license Complex.js v2.0.1 11/02/2016 * * Copyright (c) 2016, Robert Eisele (robert@xarg.org) * Dual licensed under the MIT or GPL Version 2 licenses. **/ /** * * This class allows the manipilation of complex numbers. * You can pass a complex number in different formats. Either as object, double, string or two integer parameters. * * Object form * { re: , im: } * { arg: , abs: } * { phi: , r: } * * Double form * 99.3 - Single double value * * String form * "23.1337" - Simple real number * "15+3i" - a simple complex number * "3-i" - a simple complex number * * Example: * * var c = new Complex("99.3+8i"); * c.mul({r: 3, i: 9}).div(4.9).sub(3, 2); * */ (function(root) { "use strict"; var P = {'re': 0, 'im': 0}; Math.cosh = Math.cosh || function(x) { return (Math.exp(x) + Math.exp(-x)) * 0.5; }; Math.sinh = Math.sinh || function(x) { return (Math.exp(x) - Math.exp(-x)) * 0.5; }; var parser_exit = function() { throw SyntaxError("Invalid Param"); }; /** * Calculates log(sqrt(a^2+b^2)) in a way to avoid overflows * * @param {number} a * @param {number} b * @returns {number} */ function logHypot(a, b) { var _a = Math.abs(a); var _b = Math.abs(b); if (a === 0) { return Math.log(_b); } if (b === 0) { return Math.log(_a); } if (_a < 3000 && _b < 3000) { return Math.log(a * a + b * b) * 0.5; } /* I got 4 ideas to compute this property without overflow: * * Testing 1000000 times with random samples for a,b ∈ [1, 1000000000] against a big decimal library to get an error estimate * * 1. Only eliminate the square root: (OVERALL ERROR: 3.9122483030951116e-11) Math.log(a * a + b * b) / 2 * * * 2. Try to use the non-overflowing pythagoras: (OVERALL ERROR: 8.889760039210159e-10) var fn = function(a, b) { a = Math.abs(a); b = Math.abs(b); var t = Math.min(a, b); a = Math.max(a, b); t = t / a; return Math.log(a) + Math.log(1 + t * t) / 2; }; * 3. Abuse the identity cos(atan(y/x) = x / sqrt(x^2+y^2): (OVERALL ERROR: 3.4780178737037204e-10) Math.log(a / Math.cos(Math.atan2(b, a))) * 4. Use 3. and apply log rules: (OVERALL ERROR: 1.2014087502620896e-9) Math.log(a) - Math.log(Math.cos(Math.atan2(b, a))) */ return Math.log(a / Math.cos(Math.atan2(b, a))); } var parse = function(a, b) { if (a === undefined || a === null) { P["re"] = P["im"] = 0; } else if (b !== undefined) { P["re"] = a; P["im"] = b; } else switch (typeof a) { case "object": if ("im" in a && "re" in a) { P["re"] = a["re"]; P["im"] = a["im"]; } else if ("abs" in a && "arg" in a) { P["re"] = a["abs"] * Math.cos(a["arg"]); P["im"] = a["abs"] * Math.sin(a["arg"]); } else if ("r" in a && "phi" in a) { P["re"] = a["r"] * Math.cos(a["phi"]); P["im"] = a["r"] * Math.sin(a["phi"]); } else { parser_exit(); } break; case "string": P["im"] = /* void */ P["re"] = 0; var tokens = a.match(/\d+\.?\d*e[+-]?\d+|\d+\.?\d*|\.\d+|./g); var plus = 1; var minus = 0; if (tokens === null) { parser_exit(); } for (var i = 0; i < tokens.length; i++) { var c = tokens[i]; if (c === ' ' || c === '\t' || c === '\n') { /* void */ } else if (c === '+') { plus++; } else if (c === '-') { minus++; } else if (c === 'i' || c === 'I') { if (plus + minus === 0) { parser_exit(); } if (tokens[i + 1] !== ' ' && !isNaN(tokens[i + 1])) { P["im"]+= parseFloat((minus % 2 ? "-" : "") + tokens[i + 1]); i++; } else { P["im"]+= parseFloat((minus % 2 ? "-" : "") + "1"); } plus = minus = 0; } else { if (plus + minus === 0 || isNaN(c)) { parser_exit(); } if (tokens[i + 1] === 'i' || tokens[i + 1] === 'I') { P["im"]+= parseFloat((minus % 2 ? "-" : "") + c); i++; } else { P["re"]+= parseFloat((minus % 2 ? "-" : "") + c); } plus = minus = 0; } } // Still something on the stack if (plus + minus > 0) { parser_exit(); } break; case "number": P["im"] = 0; P["re"] = a; break; default: parser_exit(); } if (isNaN(P["re"]) || isNaN(P["im"])) { // If a calculation is NaN, we treat it as NaN and don't throw //parser_exit(); } }; /** * @constructor * @returns {Complex} */ function Complex(a, b) { if (!(this instanceof Complex)) { return new Complex(a, b); } parse(a, b); // mutates P this["re"] = P["re"]; this["im"] = P["im"]; } Complex.prototype = { "re": 0, "im": 0, /** * Calculates the sign of a complex number * * @returns {Complex} */ "sign": function() { var abs = this["abs"](); return new Complex( this["re"] / abs, this["im"] / abs); }, /** * Adds two complex numbers * * @returns {Complex} */ "add": function(a, b) { parse(a, b); // mutates P return new Complex( this["re"] + P["re"], this["im"] + P["im"]); }, /** * Subtracts two complex numbers * * @returns {Complex} */ "sub": function(a, b) { parse(a, b); // mutates P return new Complex( this["re"] - P["re"], this["im"] - P["im"]); }, /** * Multiplies two complex numbers * * @returns {Complex} */ "mul": function(a, b) { parse(a, b); // mutates P // Besides the addition/subtraction, this helps having a solution for rational Infinity if (P['im'] === 0 && this['im'] === 0) { return new Complex(this['re'] * P['re'], 0); } return new Complex( this["re"] * P["re"] - this["im"] * P["im"], this["re"] * P["im"] + this["im"] * P["re"]); }, /** * Divides two complex numbers * * @returns {Complex} */ "div": function(a, b) { parse(a, b); // mutates P a = this["re"]; b = this["im"]; var c = P["re"]; var d = P["im"]; var t, x; // Divisor is zero if (0 === c && 0 === d) { return new Complex( (a !== 0) ? (a / 0) : 0, (b !== 0) ? (b / 0) : 0); } // Divisor is rational if (0 === d) { return new Complex(a / c, b / c); } if (Math.abs(c) < Math.abs(d)) { x = c / d; t = c * x + d; return new Complex( (a * x + b) / t, (b * x - a) / t); } else { x = d / c; t = d * x + c; return new Complex( (a + b * x) / t, (b - a * x) / t); } }, /** * Calculate the power of two complex numbers * * @returns {Complex} */ "pow": function(a, b) { parse(a, b); // mutates P a = this["re"]; b = this["im"]; if (a === 0 && b === 0) { return new Complex(0, 0); } var arg = Math.atan2(b, a); var loh = logHypot(a, b); if (P["im"] === 0) { if (b === 0 && a >= 0) { return new Complex(Math.pow(a, P["re"]), 0); } else if (a === 0) { switch (P["re"] % 4) { case 0: return new Complex(Math.pow(b, P["re"]), 0); case 1: return new Complex(0, Math.pow(b, P["re"])); case 2: return new Complex(-Math.pow(b, P["re"]), 0); case 3: return new Complex(0, -Math.pow(b, P["re"])); } } } /* I couldn"t find a good formula, so here is a derivation and optimization * * z_1^z_2 = (a + bi)^(c + di) * = exp((c + di) * log(a + bi) * = pow(a^2 + b^2, (c + di) / 2) * exp(i(c + di)atan2(b, a)) * =>... * Re = (pow(a^2 + b^2, c / 2) * exp(-d * atan2(b, a))) * cos(d * log(a^2 + b^2) / 2 + c * atan2(b, a)) * Im = (pow(a^2 + b^2, c / 2) * exp(-d * atan2(b, a))) * sin(d * log(a^2 + b^2) / 2 + c * atan2(b, a)) * * =>... * Re = exp(c * log(sqrt(a^2 + b^2)) - d * atan2(b, a)) * cos(d * log(sqrt(a^2 + b^2)) + c * atan2(b, a)) * Im = exp(c * log(sqrt(a^2 + b^2)) - d * atan2(b, a)) * sin(d * log(sqrt(a^2 + b^2)) + c * atan2(b, a)) * * => * Re = exp(c * logsq2 - d * arg(z_1)) * cos(d * logsq2 + c * arg(z_1)) * Im = exp(c * logsq2 - d * arg(z_1)) * sin(d * logsq2 + c * arg(z_1)) * */ a = Math.exp(P["re"] * loh - P["im"] * arg); b = P["im"] * loh + P["re"] * arg; return new Complex( a * Math.cos(b), a * Math.sin(b)); }, /** * Calculate the complex square root * * @returns {Complex} */ "sqrt": function() { var a = this["re"]; var b = this["im"]; var r = this["abs"](); var re, im; if (a >= 0 && b === 0) { return new Complex(Math.sqrt(a), 0); } if (a >= 0) { re = 0.5 * Math.sqrt(2.0 * (r + a)); } else { re = Math.abs(b) / Math.sqrt(2 * (r - a)); } if (a <= 0) { im = 0.5 * Math.sqrt(2.0 * (r - a)); } else { im = Math.abs(b) / Math.sqrt(2 * (r + a)); } return new Complex(re, b >= 0 ? im : -im); }, /** * Calculate the complex exponent * * @returns {Complex} */ "exp": function() { var tmp = Math.exp(this["re"]); if (this["im"] === 0) { //return new Complex(tmp, 0); } return new Complex( tmp * Math.cos(this["im"]), tmp * Math.sin(this["im"])); }, /** * Calculate the natural log * * @returns {Complex} */ "log": function() { var a = this["re"]; var b = this["im"]; if (b === 0 && a > 0) { //return new Complex(Math.log(a), 0); } return new Complex( logHypot(a, b), Math.atan2(b, a)); }, /** * Calculate the magniture of the complex number * * @returns {number} */ "abs": function() { var a = Math.abs(this["re"]); var b = Math.abs(this["im"]); if (a < 3000 && b < 3000) { return Math.sqrt(a * a + b * b); } if (a < b) { a = b; b = this["re"] / this["im"]; } else { b = this["im"] / this["re"]; } return a * Math.sqrt(1 + b * b); }, /** * Calculate the angle of the complex number * * @returns {number} */ "arg": function() { return Math.atan2(this["im"], this["re"]); }, /** * Calculate the sine of the complex number * * @returns {Complex} */ "sin": function() { var a = this["re"]; var b = this["im"]; return new Complex( Math.sin(a) * Math.cosh(b), Math.cos(a) * Math.sinh(b)); }, /** * Calculate the cosine * * @returns {Complex} */ "cos": function() { var a = this["re"]; var b = this["im"]; return new Complex( Math.cos(a) * Math.cosh(b), -Math.sin(a) * Math.sinh(b)); }, /** * Calculate the tangent * * @returns {Complex} */ "tan": function() { var a = 2 * this["re"]; var b = 2 * this["im"]; var d = Math.cos(a) + Math.cosh(b); return new Complex( Math.sin(a) / d, Math.sinh(b) / d); }, /** * Calculate the cotangent * * @returns {Complex} */ "cot": function() { var a = 2 * this["re"]; var b = 2 * this["im"]; var d = Math.cos(a) - Math.cosh(b); return new Complex( -Math.sin(a) / d, Math.sinh(b) / d); }, /** * Calculate the secant * * @returns {Complex} */ "sec": function() { var a = this["re"]; var b = this["im"]; var d = 0.5 * Math.cosh(2 * b) + 0.5 * Math.cos(2 * a); return new Complex( Math.cos(a) * Math.cosh(b) / d, Math.sin(a) * Math.sinh(b) / d); }, /** * Calculate the cosecans * * @returns {Complex} */ "csc": function() { var a = this["re"]; var b = this["im"]; var d = 0.5 * Math.cosh(2 * b) - 0.5 * Math.cos(2 * a); return new Complex( Math.sin(a) * Math.cosh(b) / d, -Math.cos(a) * Math.sinh(b) / d); }, /** * Calculate the complex arcus sinus * * @returns {Complex} */ "asin": function() { var a = this["re"]; var b = this["im"]; var t1 = new Complex( b * b - a * a + 1, -2 * a * b)['sqrt'](); var t2 = new Complex( t1['re'] - b, t1['im'] + a)['log'](); return new Complex(t2['im'], -t2['re']); }, /** * Calculate the complex arcus cosinus * * @returns {Complex} */ "acos": function() { var a = this["re"]; var b = this["im"]; var t1 = new Complex( b * b - a * a + 1, -2 * a * b)['sqrt'](); var t2 = new Complex( t1["re"] - b, t1["im"] + a)['log'](); return new Complex(Math.PI / 2 - t2["im"], t2["re"]); }, /** * Calculate the complex arcus tangent * * @returns {Complex} */ "atan": function() { var a = this["re"]; var b = this["im"]; if (a === 0) { if (b === 1) { return new Complex(0, Infinity); } if (b === -1) { return new Complex(0, -Infinity); } } var d = a * a + (1.0 - b) * (1.0 - b); var t1 = new Complex( (1 - b * b - a * a) / d, -2 * a / d).log(); return new Complex(-0.5 * t1["im"], 0.5 * t1["re"]); }, /** * Calculate the complex arcus cotangent * * @returns {Complex} */ "acot": function() { var a = this["re"]; var b = this["im"]; if (b === 0) { return new Complex(Math.atan2(1, a), 0); } var d = a * a + b * b; return (d !== 0) ? new Complex( a / d, -b / d).atan() : new Complex( (a !== 0) ? a / 0 : 0, (b !== 0) ?-b / 0 : 0).atan(); }, /** * Calculate the complex arcus secant * * @returns {Complex} */ "asec": function() { var a = this["re"]; var b = this["im"]; if (a === 0 && b === 0) { return new Complex(0, Infinity); } var d = a * a + b * b; return (d !== 0) ? new Complex( a / d, -b / d).acos() : new Complex( (a !== 0) ? a / 0 : 0, (b !== 0) ?-b / 0 : 0).acos(); }, /** * Calculate the complex arcus cosecans * * @returns {Complex} */ "acsc": function() { var a = this["re"]; var b = this["im"]; if (a === 0 && b === 0) { return new Complex(Math.PI / 2, Infinity); } var d = a * a + b * b; return (d !== 0) ? new Complex( a / d, -b / d).asin() : new Complex( (a !== 0) ? a / 0 : 0, (b !== 0) ?-b / 0 : 0).asin(); }, /** * Calculate the complex sinh * * @returns {Complex} */ "sinh": function() { var a = this["re"]; var b = this["im"]; return new Complex( Math.sinh(a) * Math.cos(b), Math.cosh(a) * Math.sin(b)); }, /** * Calculate the complex cosh * * @returns {Complex} */ "cosh": function() { var a = this["re"]; var b = this["im"]; return new Complex( Math.cosh(a) * Math.cos(b), Math.sinh(a) * Math.sin(b)); }, /** * Calculate the complex tanh * * @returns {Complex} */ "tanh": function() { var a = 2 * this["re"]; var b = 2 * this["im"]; var d = Math.cosh(a) + Math.cos(b); return new Complex( Math.sinh(a) / d, Math.sin(b) / d); }, /** * Calculate the complex coth * * @returns {Complex} */ "coth": function() { var a = 2 * this["re"]; var b = 2 * this["im"]; var d = Math.cosh(a) - Math.cos(b); return new Complex( Math.sinh(a) / d, -Math.sin(b) / d); }, /** * Calculate the complex coth * * @returns {Complex} */ "csch": function() { var a = this["re"]; var b = this["im"]; var d = Math.cos(2 * b) - Math.cosh(2 * a); return new Complex( -2 * Math.sinh(a) * Math.cos(b) / d, 2 * Math.cosh(a) * Math.sin(b) / d); }, /** * Calculate the complex sech * * @returns {Complex} */ "sech": function() { var a = this["re"]; var b = this["im"]; var d = Math.cos(2 * b) + Math.cosh(2 * a); return new Complex( 2 * Math.cosh(a) * Math.cos(b) / d, -2 * Math.sinh(a) * Math.sin(b) / d); }, /** * Calculate the complex asinh * * @returns {Complex} */ "asinh": function() { var tmp = this["im"]; this["im"] = -this["re"]; this["re"] = tmp; var res = this["asin"](); this["re"] = -this["im"]; this["im"] = tmp; tmp = res["re"]; res["re"] = -res["im"]; res["im"] = tmp; return res; }, /** * Calculate the complex asinh * * @returns {Complex} */ "acosh": function() { var tmp; var res = this["acos"](); if (res["im"] <= 0) { tmp = res["re"]; res["re"] = -res["im"]; res["im"] = tmp; } else { tmp = res["im"]; res["im"] = -res["re"]; res["re"] = tmp; } return res; }, /** * Calculate the complex atanh * * @returns {Complex} */ "atanh": function() { var a = this["re"]; var b = this["im"]; var noIM = a > 1 && b === 0; var oneMinus = 1 - a; var onePlus = 1 + a; var d = oneMinus * oneMinus + b * b; var x = (d !== 0) ? new Complex( (onePlus * oneMinus - b * b) / d, (b * oneMinus + onePlus * b) / d) : new Complex( (a !== -1) ? (a / 0) : 0, (b !== 0) ? (b / 0) : 0); var temp = x["re"]; x["re"] = logHypot(x["re"], x["im"]) / 2; x["im"] = Math.atan2(x["im"], temp) / 2; if (noIM) { x["im"] = -x["im"]; } return x; }, /** * Calculate the complex acoth * * @returns {Complex} */ "acoth": function() { var a = this["re"]; var b = this["im"]; if (a === 0 && b === 0) { return new Complex(0, Math.PI / 2); } var d = a * a + b * b; return (d !== 0) ? new Complex( a / d, -b / d).atanh() : new Complex( (a !== 0) ? a / 0 : 0, (b !== 0) ?-b / 0 : 0).atanh(); }, /** * Calculate the complex acsch * * @returns {Complex} */ "acsch": function() { var a = this["re"]; var b = this["im"]; if (b === 0) { return new Complex( (a !== 0) ? Math.log(a + Math.sqrt(a * a + 1)) : Infinity, 0); } var d = a * a + b * b; return (d !== 0) ? new Complex( a / d, -b / d).asinh() : new Complex( (a !== 0) ? a / 0 : 0, (b !== 0) ?-b / 0 : 0).asinh(); }, /** * Calculate the complex asech * * @returns {Complex} */ "asech": function() { var a = this["re"]; var b = this["im"]; if (a === 0 && b === 0) { return new Complex(Infinity, 0); } var d = a * a + b * b; return (d !== 0) ? new Complex( a / d, -b / d).acosh() : new Complex( (a !== 0) ? a / 0 : 0, (b !== 0) ?-b / 0 : 0).acosh(); }, /** * Calculate the complex inverse 1/z * * @returns {Complex} */ "inverse": function() { var a = this["re"]; var b = this["im"]; var d = a * a + b * b; return new Complex( a !== 0 ? a / d : 0, b !== 0 ?-b / d : 0); }, /** * Returns the complex conjugate * * @returns {Complex} */ "conjugate": function() { return new Complex(this["re"], -this["im"]); }, /** * Gets the negated complex number * * @returns {Complex} */ "neg": function() { return new Complex(-this["re"], -this["im"]); }, /** * Ceils the actual complex number * * @returns {Complex} */ "ceil": function(places) { places = Math.pow(10, places || 0); return new Complex( Math.ceil(this["re"] * places) / places, Math.ceil(this["im"] * places) / places); }, /** * Floors the actual complex number * * @returns {Complex} */ "floor": function(places) { places = Math.pow(10, places || 0); return new Complex( Math.floor(this["re"] * places) / places, Math.floor(this["im"] * places) / places); }, /** * Ceils the actual complex number * * @returns {Complex} */ "round": function(places) { places = Math.pow(10, places || 0); return new Complex( Math.round(this["re"] * places) / places, Math.round(this["im"] * places) / places); }, /** * Compares two complex numbers * * @returns {boolean} */ "equals": function(a, b) { parse(a, b); // mutates P return Math.abs(P["re"] - this["re"]) <= Complex["EPSILON"] && Math.abs(P["im"] - this["im"]) <= Complex["EPSILON"]; }, /** * Clones the actual object * * @returns {Complex} */ "clone": function() { return new Complex(this["re"], this["im"]); }, /** * Gets a string of the actual complex number * * @returns {string} */ "toString": function() { var a = this["re"]; var b = this["im"]; var ret = ""; if (isNaN(a) || isNaN(b)) { return "NaN"; } if (a !== 0) { ret+= a; } if (b !== 0) { if (a !== 0) { ret+= b < 0 ? " - " : " + "; } else if (b < 0) { ret+= "-"; } b = Math.abs(b); if (1 !== b) { ret+= b; } ret+= "i"; } if (!ret) return "0"; return ret; }, /** * Returns the actual number as a vector * * @returns {Array} */ "toVector": function() { return [this["re"], this["im"]]; }, /** * Returns the actual real value of the current object * * @returns {number|null} */ "valueOf": function() { if (this["im"] === 0) { return this["re"]; } return null; }, /** * Checks if the given complex number is not a number * * @returns {boolean} */ isNaN: function() { return isNaN(this['re']) || isNaN(this['im']); } }; Complex["ZERO"] = new Complex(0, 0); Complex["ONE"] = new Complex(1, 0); Complex["I"] = new Complex(0, 1); Complex["PI"] = new Complex(Math.PI, 0); Complex["E"] = new Complex(Math.E, 0); Complex['EPSILON'] = 1e-16; if (true) { !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function() { return Complex; }.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); } else if (typeof exports === "object") { module["exports"] = Complex; } else { root["Complex"] = Complex; } })(this); /***/ }), /* 505 */ /***/ (function(module, exports, __webpack_require__) { var __WEBPACK_AMD_DEFINE_RESULT__;/*! decimal.js v7.1.1 https://github.com/MikeMcl/decimal.js/LICENCE */ ;(function (globalScope) { 'use strict'; /* * decimal.js v7.1.1 * An arbitrary-precision Decimal type for JavaScript. * https://github.com/MikeMcl/decimal.js * Copyright (c) 2016 Michael Mclaughlin * MIT Licence */ // ----------------------------------- EDITABLE DEFAULTS ------------------------------------ // // The maximum exponent magnitude. // The limit on the value of `toExpNeg`, `toExpPos`, `minE` and `maxE`. var EXP_LIMIT = 9e15, // 0 to 9e15 // The limit on the value of `precision`, and on the value of the first argument to // `toDecimalPlaces`, `toExponential`, `toFixed`, `toPrecision` and `toSignificantDigits`. MAX_DIGITS = 1e9, // 0 to 1e9 // Base conversion alphabet. NUMERALS = '0123456789abcdef', // The natural logarithm of 10 (1025 digits). LN10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058', // Pi (1025 digits). PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789', // The initial configuration properties of the Decimal constructor. Decimal = { // These values must be integers within the stated ranges (inclusive). // Most of these values can be changed at run-time using the `Decimal.config` method. // The maximum number of significant digits of the result of a calculation or base conversion. // E.g. `Decimal.config({ precision: 20 });` precision: 20, // 1 to MAX_DIGITS // The rounding mode used when rounding to `precision`. // // ROUND_UP 0 Away from zero. // ROUND_DOWN 1 Towards zero. // ROUND_CEIL 2 Towards +Infinity. // ROUND_FLOOR 3 Towards -Infinity. // ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up. // ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down. // ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour. // ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity. // ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity. // // E.g. // `Decimal.rounding = 4;` // `Decimal.rounding = Decimal.ROUND_HALF_UP;` rounding: 4, // 0 to 8 // The modulo mode used when calculating the modulus: a mod n. // The quotient (q = a / n) is calculated according to the corresponding rounding mode. // The remainder (r) is calculated as: r = a - n * q. // // UP 0 The remainder is positive if the dividend is negative, else is negative. // DOWN 1 The remainder has the same sign as the dividend (JavaScript %). // FLOOR 3 The remainder has the same sign as the divisor (Python %). // HALF_EVEN 6 The IEEE 754 remainder function. // EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive. // // Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian // division (9) are commonly used for the modulus operation. The other rounding modes can also // be used, but they may not give useful results. modulo: 1, // 0 to 9 // The exponent value at and beneath which `toString` returns exponential notation. // JavaScript numbers: -7 toExpNeg: -7, // 0 to -EXP_LIMIT // The exponent value at and above which `toString` returns exponential notation. // JavaScript numbers: 21 toExpPos: 21, // 0 to EXP_LIMIT // The minimum exponent value, beneath which underflow to zero occurs. // JavaScript numbers: -324 (5e-324) minE: -EXP_LIMIT, // -1 to -EXP_LIMIT // The maximum exponent value, above which overflow to Infinity occurs. // JavaScript numbers: 308 (1.7976931348623157e+308) maxE: EXP_LIMIT, // 1 to EXP_LIMIT // Whether to use cryptographically-secure random number generation, if available. crypto: false // true/false }, // ----------------------------------- END OF EDITABLE DEFAULTS ------------------------------- // inexact, noConflict, quadrant, external = true, decimalError = '[DecimalError] ', invalidArgument = decimalError + 'Invalid argument: ', precisionLimitExceeded = decimalError + 'Precision limit exceeded', cryptoUnavailable = decimalError + 'crypto unavailable', mathfloor = Math.floor, mathpow = Math.pow, isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i, isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i, isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i, isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i, BASE = 1e7, LOG_BASE = 7, MAX_SAFE_INTEGER = 9007199254740991, LN10_PRECISION = LN10.length - 1, PI_PRECISION = PI.length - 1, // Decimal.prototype object P = {}; // Decimal prototype methods /* * absoluteValue abs * ceil * comparedTo cmp * cosine cos * cubeRoot cbrt * decimalPlaces dp * dividedBy div * dividedToIntegerBy divToInt * equals eq * floor * greaterThan gt * greaterThanOrEqualTo gte * hyperbolicCosine cosh * hyperbolicSine sinh * hyperbolicTangent tanh * inverseCosine acos * inverseHyperbolicCosine acosh * inverseHyperbolicSine asinh * inverseHyperbolicTangent atanh * inverseSine asin * inverseTangent atan * isFinite * isInteger isInt * isNaN * isNegative isNeg * isPositive isPos * isZero * lessThan lt * lessThanOrEqualTo lte * logarithm log * [maximum] [max] * [minimum] [min] * minus sub * modulo mod * naturalExponential exp * naturalLogarithm ln * negated neg * plus add * precision sd * round * sine sin * squareRoot sqrt * tangent tan * times mul * toBinary * toDecimalPlaces toDP * toExponential * toFixed * toFraction * toHexadecimal toHex * toNearest * toNumber * toOctal * toPower pow * toPrecision * toSignificantDigits toSD * toString * truncated trunc * valueOf toJSON */ /* * Return a new Decimal whose value is the absolute value of this Decimal. * */ P.absoluteValue = P.abs = function () { var x = new this.constructor(this); if (x.s < 0) x.s = 1; return finalise(x); }; /* * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the * direction of positive Infinity. * */ P.ceil = function () { return finalise(new this.constructor(this), this.e + 1, 2); }; /* * Return * 1 if the value of this Decimal is greater than the value of `y`, * -1 if the value of this Decimal is less than the value of `y`, * 0 if they have the same value, * NaN if the value of either Decimal is NaN. * */ P.comparedTo = P.cmp = function (y) { var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s; // Either NaN or ±Infinity? if (!xd || !yd) { return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1; } // Either zero? if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0; // Signs differ? if (xs !== ys) return xs; // Compare exponents. if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1; xdL = xd.length; ydL = yd.length; // Compare digit by digit. for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) { if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1; } // Compare lengths. return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1; }; /* * Return a new Decimal whose value is the cosine of the value in radians of this Decimal. * * Domain: [-Infinity, Infinity] * Range: [-1, 1] * * cos(0) = 1 * cos(-0) = 1 * cos(Infinity) = NaN * cos(-Infinity) = NaN * cos(NaN) = NaN * */ P.cosine = P.cos = function () { var pr, rm, x = this, Ctor = x.constructor; if (!x.d) return new Ctor(NaN); // cos(0) = cos(-0) = 1 if (!x.d[0]) return new Ctor(1); pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE; Ctor.rounding = 1; x = cosine(Ctor, toLessThanHalfPi(Ctor, x)); Ctor.precision = pr; Ctor.rounding = rm; return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true); }; /* * * Return a new Decimal whose value is the cube root of the value of this Decimal, rounded to * `precision` significant digits using rounding mode `rounding`. * * cbrt(0) = 0 * cbrt(-0) = -0 * cbrt(1) = 1 * cbrt(-1) = -1 * cbrt(N) = N * cbrt(-I) = -I * cbrt(I) = I * * Math.cbrt(x) = (x < 0 ? -Math.pow(-x, 1/3) : Math.pow(x, 1/3)) * */ P.cubeRoot = P.cbrt = function () { var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor; if (!x.isFinite() || x.isZero()) return new Ctor(x); external = false; // Initial estimate. s = x.s * Math.pow(x.s * x, 1 / 3); // Math.cbrt underflow/overflow? // Pass x to Math.pow as integer, then adjust the exponent of the result. if (!s || Math.abs(s) == 1 / 0) { n = digitsToString(x.d); e = x.e; // Adjust n exponent so it is a multiple of 3 away from x exponent. if (s = (e - n.length + 1) % 3) n += (s == 1 || s == -2 ? '0' : '00'); s = Math.pow(n, 1 / 3); // Rarely, e may be one less than the result exponent value. e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2)); if (s == 1 / 0) { n = '5e' + e; } else { n = s.toExponential(); n = n.slice(0, n.indexOf('e') + 1) + e; } r = new Ctor(n); r.s = x.s; } else { r = new Ctor(s.toString()); } sd = (e = Ctor.precision) + 3; // Halley's method. // TODO? Compare Newton's method. for (;;) { t = r; t3 = t.times(t).times(t); t3plusx = t3.plus(x); r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1); // TODO? Replace with for-loop and checkRoundingDigits. if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) { n = n.slice(sd - 3, sd + 1); // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or 4999 // , i.e. approaching a rounding boundary, continue the iteration. if (n == '9999' || !rep && n == '4999') { // On the first iteration only, check to see if rounding up gives the exact result as the // nines may infinitely repeat. if (!rep) { finalise(t, e + 1, 0); if (t.times(t).times(t).eq(x)) { r = t; break; } } sd += 4; rep = 1; } else { // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result. // If not, then there are further digits and m will be truthy. if (!+n || !+n.slice(1) && n.charAt(0) == '5') { // Truncate to the first rounding digit. finalise(r, e + 1, 1); m = !r.times(r).times(r).eq(x); } break; } } } external = true; return finalise(r, e, Ctor.rounding, m); }; /* * Return the number of decimal places of the value of this Decimal. * */ P.decimalPlaces = P.dp = function () { var w, d = this.d, n = NaN; if (d) { w = d.length - 1; n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE; // Subtract the number of trailing zeros of the last word. w = d[w]; if (w) for (; w % 10 == 0; w /= 10) n--; if (n < 0) n = 0; } return n; }; /* * n / 0 = I * n / N = N * n / I = 0 * 0 / n = 0 * 0 / 0 = N * 0 / N = N * 0 / I = 0 * N / n = N * N / 0 = N * N / N = N * N / I = N * I / n = I * I / 0 = I * I / N = N * I / I = N * * Return a new Decimal whose value is the value of this Decimal divided by `y`, rounded to * `precision` significant digits using rounding mode `rounding`. * */ P.dividedBy = P.div = function (y) { return divide(this, new this.constructor(y)); }; /* * Return a new Decimal whose value is the integer part of dividing the value of this Decimal * by the value of `y`, rounded to `precision` significant digits using rounding mode `rounding`. * */ P.dividedToIntegerBy = P.divToInt = function (y) { var x = this, Ctor = x.constructor; return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding); }; /* * Return true if the value of this Decimal is equal to the value of `y`, otherwise return false. * */ P.equals = P.eq = function (y) { return this.cmp(y) === 0; }; /* * Return a new Decimal whose value is the value of this Decimal rounded to a whole number in the * direction of negative Infinity. * */ P.floor = function () { return finalise(new this.constructor(this), this.e + 1, 3); }; /* * Return true if the value of this Decimal is greater than the value of `y`, otherwise return * false. * */ P.greaterThan = P.gt = function (y) { return this.cmp(y) > 0; }; /* * Return true if the value of this Decimal is greater than or equal to the value of `y`, * otherwise return false. * */ P.greaterThanOrEqualTo = P.gte = function (y) { var k = this.cmp(y); return k == 1 || k === 0; }; /* * Return a new Decimal whose value is the hyperbolic cosine of the value in radians of this * Decimal. * * Domain: [-Infinity, Infinity] * Range: [1, Infinity] * * cosh(x) = 1 + x^2/2! + x^4/4! + x^6/6! + ... * * cosh(0) = 1 * cosh(-0) = 1 * cosh(Infinity) = Infinity * cosh(-Infinity) = Infinity * cosh(NaN) = NaN * * x time taken (ms) result * 1000 9 9.8503555700852349694e+433 * 10000 25 4.4034091128314607936e+4342 * 100000 171 1.4033316802130615897e+43429 * 1000000 3817 1.5166076984010437725e+434294 * 10000000 abandoned after 2 minute wait * * TODO? Compare performance of cosh(x) = 0.5 * (exp(x) + exp(-x)) * */ P.hyperbolicCosine = P.cosh = function () { var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1); if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN); if (x.isZero()) return one; pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + Math.max(x.e, x.sd()) + 4; Ctor.rounding = 1; len = x.d.length; // Argument reduction: cos(4x) = 1 - 8cos^2(x) + 8cos^4(x) + 1 // i.e. cos(x) = 1 - cos^2(x/4)(8 - 8cos^2(x/4)) // Estimate the optimum number of times to use the argument reduction. // TODO? Estimation reused from cosine() and may not be optimal here. if (len < 32) { k = Math.ceil(len / 3); n = Math.pow(4, -k).toString(); } else { k = 16; n = '2.3283064365386962890625e-10'; } x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true); // Reverse argument reduction var cosh2_x, i = k, d8 = new Ctor(8); for (; i--;) { cosh2_x = x.times(x); x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8)))); } return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true); }; /* * Return a new Decimal whose value is the hyperbolic sine of the value in radians of this * Decimal. * * Domain: [-Infinity, Infinity] * Range: [-Infinity, Infinity] * * sinh(x) = x + x^3/3! + x^5/5! + x^7/7! + ... * * sinh(0) = 0 * sinh(-0) = -0 * sinh(Infinity) = Infinity * sinh(-Infinity) = -Infinity * sinh(NaN) = NaN * * x time taken (ms) * 10 2 ms * 100 5 ms * 1000 14 ms * 10000 82 ms * 100000 886 ms 1.4033316802130615897e+43429 * 200000 2613 ms * 300000 5407 ms * 400000 8824 ms * 500000 13026 ms 8.7080643612718084129e+217146 * 1000000 48543 ms * * TODO? Compare performance of sinh(x) = 0.5 * (exp(x) - exp(-x)) * */ P.hyperbolicSine = P.sinh = function () { var k, pr, rm, len, x = this, Ctor = x.constructor; if (!x.isFinite() || x.isZero()) return new Ctor(x); pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + Math.max(x.e, x.sd()) + 4; Ctor.rounding = 1; len = x.d.length; if (len < 3) { x = taylorSeries(Ctor, 2, x, x, true); } else { // Alternative argument reduction: sinh(3x) = sinh(x)(3 + 4sinh^2(x)) // i.e. sinh(x) = sinh(x/3)(3 + 4sinh^2(x/3)) // 3 multiplications and 1 addition // Argument reduction: sinh(5x) = sinh(x)(5 + sinh^2(x)(20 + 16sinh^2(x))) // i.e. sinh(x) = sinh(x/5)(5 + sinh^2(x/5)(20 + 16sinh^2(x/5))) // 4 multiplications and 2 additions // Estimate the optimum number of times to use the argument reduction. k = 1.4 * Math.sqrt(len); k = k > 16 ? 16 : k | 0; x = x.times(Math.pow(5, -k)); x = taylorSeries(Ctor, 2, x, x, true); // Reverse argument reduction var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20); for (; k--;) { sinh2_x = x.times(x); x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20)))); } } Ctor.precision = pr; Ctor.rounding = rm; return finalise(x, pr, rm, true); }; /* * Return a new Decimal whose value is the hyperbolic tangent of the value in radians of this * Decimal. * * Domain: [-Infinity, Infinity] * Range: [-1, 1] * * tanh(x) = sinh(x) / cosh(x) * * tanh(0) = 0 * tanh(-0) = -0 * tanh(Infinity) = 1 * tanh(-Infinity) = -1 * tanh(NaN) = NaN * */ P.hyperbolicTangent = P.tanh = function () { var pr, rm, x = this, Ctor = x.constructor; if (!x.isFinite()) return new Ctor(x.s); if (x.isZero()) return new Ctor(x); pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + 7; Ctor.rounding = 1; return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm); }; /* * Return a new Decimal whose value is the arccosine (inverse cosine) in radians of the value of * this Decimal. * * Domain: [-1, 1] * Range: [0, pi] * * acos(x) = pi/2 - asin(x) * * acos(0) = pi/2 * acos(-0) = pi/2 * acos(1) = 0 * acos(-1) = pi * acos(1/2) = pi/3 * acos(-1/2) = 2*pi/3 * acos(|x| > 1) = NaN * acos(NaN) = NaN * */ P.inverseCosine = P.acos = function () { var halfPi, x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding; if (k !== -1) { return k === 0 // |x| is 1 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) // |x| > 1 or x is NaN : new Ctor(NaN); } if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(0.5); // TODO? Special case acos(0.5) = pi/3 and acos(-0.5) = 2*pi/3 Ctor.precision = pr + 6; Ctor.rounding = 1; x = x.asin(); halfPi = getPi(Ctor, pr + 4, rm).times(0.5); Ctor.precision = pr; Ctor.rounding = rm; return halfPi.minus(x); }; /* * Return a new Decimal whose value is the inverse of the hyperbolic cosine in radians of the * value of this Decimal. * * Domain: [1, Infinity] * Range: [0, Infinity] * * acosh(x) = ln(x + sqrt(x^2 - 1)) * * acosh(x < 1) = NaN * acosh(NaN) = NaN * acosh(Infinity) = Infinity * acosh(-Infinity) = NaN * acosh(0) = NaN * acosh(-0) = NaN * acosh(1) = 0 * acosh(-1) = NaN * */ P.inverseHyperbolicCosine = P.acosh = function () { var pr, rm, x = this, Ctor = x.constructor; if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN); if (!x.isFinite()) return new Ctor(x); pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4; Ctor.rounding = 1; external = false; x = x.times(x).minus(1).sqrt().plus(x); external = true; Ctor.precision = pr; Ctor.rounding = rm; return x.ln(); }; /* * Return a new Decimal whose value is the inverse of the hyperbolic sine in radians of the value * of this Decimal. * * Domain: [-Infinity, Infinity] * Range: [-Infinity, Infinity] * * asinh(x) = ln(x + sqrt(x^2 + 1)) * * asinh(NaN) = NaN * asinh(Infinity) = Infinity * asinh(-Infinity) = -Infinity * asinh(0) = 0 * asinh(-0) = -0 * */ P.inverseHyperbolicSine = P.asinh = function () { var pr, rm, x = this, Ctor = x.constructor; if (!x.isFinite() || x.isZero()) return new Ctor(x); pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6; Ctor.rounding = 1; external = false; x = x.times(x).plus(1).sqrt().plus(x); external = true; Ctor.precision = pr; Ctor.rounding = rm; return x.ln(); }; /* * Return a new Decimal whose value is the inverse of the hyperbolic tangent in radians of the * value of this Decimal. * * Domain: [-1, 1] * Range: [-Infinity, Infinity] * * atanh(x) = 0.5 * ln((1 + x) / (1 - x)) * * atanh(|x| > 1) = NaN * atanh(NaN) = NaN * atanh(Infinity) = NaN * atanh(-Infinity) = NaN * atanh(0) = 0 * atanh(-0) = -0 * atanh(1) = Infinity * atanh(-1) = -Infinity * */ P.inverseHyperbolicTangent = P.atanh = function () { var pr, rm, wpr, xsd, x = this, Ctor = x.constructor; if (!x.isFinite()) return new Ctor(NaN); if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN); pr = Ctor.precision; rm = Ctor.rounding; xsd = x.sd(); if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true); Ctor.precision = wpr = xsd - x.e; x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1); Ctor.precision = pr + 4; Ctor.rounding = 1; x = x.ln(); Ctor.precision = pr; Ctor.rounding = rm; return x.times(0.5); }; /* * Return a new Decimal whose value is the arcsine (inverse sine) in radians of the value of this * Decimal. * * Domain: [-Infinity, Infinity] * Range: [-pi/2, pi/2] * * asin(x) = 2*atan(x/(1 + sqrt(1 - x^2))) * * asin(0) = 0 * asin(-0) = -0 * asin(1/2) = pi/6 * asin(-1/2) = -pi/6 * asin(1) = pi/2 * asin(-1) = -pi/2 * asin(|x| > 1) = NaN * asin(NaN) = NaN * * TODO? Compare performance of Taylor series. * */ P.inverseSine = P.asin = function () { var halfPi, k, pr, rm, x = this, Ctor = x.constructor; if (x.isZero()) return new Ctor(x); k = x.abs().cmp(1); pr = Ctor.precision; rm = Ctor.rounding; if (k !== -1) { // |x| is 1 if (k === 0) { halfPi = getPi(Ctor, pr + 4, rm).times(0.5); halfPi.s = x.s; return halfPi; } // |x| > 1 or x is NaN return new Ctor(NaN); } // TODO? Special case asin(1/2) = pi/6 and asin(-1/2) = -pi/6 Ctor.precision = pr + 6; Ctor.rounding = 1; x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan(); Ctor.precision = pr; Ctor.rounding = rm; return x.times(2); }; /* * Return a new Decimal whose value is the arctangent (inverse tangent) in radians of the value * of this Decimal. * * Domain: [-Infinity, Infinity] * Range: [-pi/2, pi/2] * * atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ... * * atan(0) = 0 * atan(-0) = -0 * atan(1) = pi/4 * atan(-1) = -pi/4 * atan(Infinity) = pi/2 * atan(-Infinity) = -pi/2 * atan(NaN) = NaN * */ P.inverseTangent = P.atan = function () { var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding; if (!x.isFinite()) { if (!x.s) return new Ctor(NaN); if (pr + 4 <= PI_PRECISION) { r = getPi(Ctor, pr + 4, rm).times(0.5); r.s = x.s; return r; } } else if (x.isZero()) { return new Ctor(x); } else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) { r = getPi(Ctor, pr + 4, rm).times(0.25); r.s = x.s; return r; } Ctor.precision = wpr = pr + 10; Ctor.rounding = 1; // TODO? if (x >= 1 && pr <= PI_PRECISION) atan(x) = halfPi * x.s - atan(1 / x); // Argument reduction // Ensure |x| < 0.42 // atan(x) = 2 * atan(x / (1 + sqrt(1 + x^2))) k = Math.min(28, wpr / LOG_BASE + 2 | 0); for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1)); external = false; j = Math.ceil(wpr / LOG_BASE); n = 1; x2 = x.times(x); r = new Ctor(x); px = x; // atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ... for (; i !== -1;) { px = px.times(x2); t = r.minus(px.div(n += 2)); px = px.times(x2); r = t.plus(px.div(n += 2)); if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--;); } if (k) r = r.times(2 << (k - 1)); external = true; return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true); }; /* * Return true if the value of this Decimal is a finite number, otherwise return false. * */ P.isFinite = function () { return !!this.d; }; /* * Return true if the value of this Decimal is an integer, otherwise return false. * */ P.isInteger = P.isInt = function () { return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2; }; /* * Return true if the value of this Decimal is NaN, otherwise return false. * */ P.isNaN = function () { return !this.s; }; /* * Return true if the value of this Decimal is negative, otherwise return false. * */ P.isNegative = P.isNeg = function () { return this.s < 0; }; /* * Return true if the value of this Decimal is positive, otherwise return false. * */ P.isPositive = P.isPos = function () { return this.s > 0; }; /* * Return true if the value of this Decimal is 0 or -0, otherwise return false. * */ P.isZero = function () { return !!this.d && this.d[0] === 0; }; /* * Return true if the value of this Decimal is less than `y`, otherwise return false. * */ P.lessThan = P.lt = function (y) { return this.cmp(y) < 0; }; /* * Return true if the value of this Decimal is less than or equal to `y`, otherwise return false. * */ P.lessThanOrEqualTo = P.lte = function (y) { return this.cmp(y) < 1; }; /* * Return the logarithm of the value of this Decimal to the specified base, rounded to `precision` * significant digits using rounding mode `rounding`. * * If no base is specified, return log[10](arg). * * log[base](arg) = ln(arg) / ln(base) * * The result will always be correctly rounded if the base of the log is 10, and 'almost always' * otherwise: * * Depending on the rounding mode, the result may be incorrectly rounded if the first fifteen * rounding digits are [49]99999999999999 or [50]00000000000000. In that case, the maximum error * between the result and the correctly rounded result will be one ulp (unit in the last place). * * log[-b](a) = NaN * log[0](a) = NaN * log[1](a) = NaN * log[NaN](a) = NaN * log[Infinity](a) = NaN * log[b](0) = -Infinity * log[b](-0) = -Infinity * log[b](-a) = NaN * log[b](1) = 0 * log[b](Infinity) = Infinity * log[b](NaN) = NaN * * [base] {number|string|Decimal} The base of the logarithm. * */ P.logarithm = P.log = function (base) { var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5; // Default base is 10. if (base == null) { base = new Ctor(10); isBase10 = true; } else { base = new Ctor(base); d = base.d; // Return NaN if base is negative, or non-finite, or is 0 or 1. if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN); isBase10 = base.eq(10); } d = arg.d; // Is arg negative, non-finite, 0 or 1? if (arg.s < 0 || !d || !d[0] || arg.eq(1)) { return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0); } // The result will have a non-terminating decimal expansion if base is 10 and arg is not an // integer power of 10. if (isBase10) { if (d.length > 1) { inf = true; } else { for (k = d[0]; k % 10 === 0;) k /= 10; inf = k !== 1; } } external = false; sd = pr + guard; num = naturalLogarithm(arg, sd); denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd); // The result will have 5 rounding digits. r = divide(num, denominator, sd, 1); // If at a rounding boundary, i.e. the result's rounding digits are [49]9999 or [50]0000, // calculate 10 further digits. // // If the result is known to have an infinite decimal expansion, repeat this until it is clear // that the result is above or below the boundary. Otherwise, if after calculating the 10 // further digits, the last 14 are nines, round up and assume the result is exact. // Also assume the result is exact if the last 14 are zero. // // Example of a result that will be incorrectly rounded: // log[1048576](4503599627370502) = 2.60000000000000009610279511444746... // The above result correctly rounded using ROUND_CEIL to 1 decimal place should be 2.7, but it // will be given as 2.6 as there are 15 zeros immediately after the requested decimal place, so // the exact result would be assumed to be 2.6, which rounded using ROUND_CEIL to 1 decimal // place is still 2.6. if (checkRoundingDigits(r.d, k = pr, rm)) { do { sd += 10; num = naturalLogarithm(arg, sd); denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd); r = divide(num, denominator, sd, 1); if (!inf) { // Check for 14 nines from the 2nd rounding digit, as the first may be 4. if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) { r = finalise(r, pr + 1, 0); } break; } } while (checkRoundingDigits(r.d, k += 10, rm)); } external = true; return finalise(r, pr, rm); }; /* * Return a new Decimal whose value is the maximum of the arguments and the value of this Decimal. * * arguments {number|string|Decimal} * P.max = function () { Array.prototype.push.call(arguments, this); return maxOrMin(this.constructor, arguments, 'lt'); }; */ /* * Return a new Decimal whose value is the minimum of the arguments and the value of this Decimal. * * arguments {number|string|Decimal} * P.min = function () { Array.prototype.push.call(arguments, this); return maxOrMin(this.constructor, arguments, 'gt'); }; */ /* * n - 0 = n * n - N = N * n - I = -I * 0 - n = -n * 0 - 0 = 0 * 0 - N = N * 0 - I = -I * N - n = N * N - 0 = N * N - N = N * N - I = N * I - n = I * I - 0 = I * I - N = N * I - I = N * * Return a new Decimal whose value is the value of this Decimal minus `y`, rounded to `precision` * significant digits using rounding mode `rounding`. * */ P.minus = P.sub = function (y) { var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor; y = new Ctor(y); // If either is not finite... if (!x.d || !y.d) { // Return NaN if either is NaN. if (!x.s || !y.s) y = new Ctor(NaN); // Return y negated if x is finite and y is ±Infinity. else if (x.d) y.s = -y.s; // Return x if y is finite and x is ±Infinity. // Return x if both are ±Infinity with different signs. // Return NaN if both are ±Infinity with the same sign. else y = new Ctor(y.d || x.s !== y.s ? x : NaN); return y; } // If signs differ... if (x.s != y.s) { y.s = -y.s; return x.plus(y); } xd = x.d; yd = y.d; pr = Ctor.precision; rm = Ctor.rounding; // If either is zero... if (!xd[0] || !yd[0]) { // Return y negated if x is zero and y is non-zero. if (yd[0]) y.s = -y.s; // Return x if y is zero and x is non-zero. else if (xd[0]) y = new Ctor(x); // Return zero if both are zero. // From IEEE 754 (2008) 6.3: 0 - 0 = -0 - -0 = -0 when rounding to -Infinity. else return new Ctor(rm === 3 ? -0 : 0); return external ? finalise(y, pr, rm) : y; } // x and y are finite, non-zero numbers with the same sign. // Calculate base 1e7 exponents. e = mathfloor(y.e / LOG_BASE); xe = mathfloor(x.e / LOG_BASE); xd = xd.slice(); k = xe - e; // If base 1e7 exponents differ... if (k) { xLTy = k < 0; if (xLTy) { d = xd; k = -k; len = yd.length; } else { d = yd; e = xe; len = xd.length; } // Numbers with massively different exponents would result in a very high number of // zeros needing to be prepended, but this can be avoided while still ensuring correct // rounding by limiting the number of zeros to `Math.ceil(pr / LOG_BASE) + 2`. i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2; if (k > i) { k = i; d.length = 1; } // Prepend zeros to equalise exponents. d.reverse(); for (i = k; i--;) d.push(0); d.reverse(); // Base 1e7 exponents equal. } else { // Check digits to determine which is the bigger number. i = xd.length; len = yd.length; xLTy = i < len; if (xLTy) len = i; for (i = 0; i < len; i++) { if (xd[i] != yd[i]) { xLTy = xd[i] < yd[i]; break; } } k = 0; } if (xLTy) { d = xd; xd = yd; yd = d; y.s = -y.s; } len = xd.length; // Append zeros to `xd` if shorter. // Don't add zeros to `yd` if shorter as subtraction only needs to start at `yd` length. for (i = yd.length - len; i > 0; --i) xd[len++] = 0; // Subtract yd from xd. for (i = yd.length; i > k;) { if (xd[--i] < yd[i]) { for (j = i; j && xd[--j] === 0;) xd[j] = BASE - 1; --xd[j]; xd[i] += BASE; } xd[i] -= yd[i]; } // Remove trailing zeros. for (; xd[--len] === 0;) xd.pop(); // Remove leading zeros and adjust exponent accordingly. for (; xd[0] === 0; xd.shift()) --e; // Zero? if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0); y.d = xd; y.e = getBase10Exponent(xd, e); return external ? finalise(y, pr, rm) : y; }; /* * n % 0 = N * n % N = N * n % I = n * 0 % n = 0 * -0 % n = -0 * 0 % 0 = N * 0 % N = N * 0 % I = 0 * N % n = N * N % 0 = N * N % N = N * N % I = N * I % n = N * I % 0 = N * I % N = N * I % I = N * * Return a new Decimal whose value is the value of this Decimal modulo `y`, rounded to * `precision` significant digits using rounding mode `rounding`. * * The result depends on the modulo mode. * */ P.modulo = P.mod = function (y) { var q, x = this, Ctor = x.constructor; y = new Ctor(y); // Return NaN if x is ±Infinity or NaN, or y is NaN or ±0. if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN); // Return x if y is ±Infinity or x is ±0. if (!y.d || x.d && !x.d[0]) { return finalise(new Ctor(x), Ctor.precision, Ctor.rounding); } // Prevent rounding of intermediate calculations. external = false; if (Ctor.modulo == 9) { // Euclidian division: q = sign(y) * floor(x / abs(y)) // result = x - q * y where 0 <= result < abs(y) q = divide(x, y.abs(), 0, 3, 1); q.s *= y.s; } else { q = divide(x, y, 0, Ctor.modulo, 1); } q = q.times(y); external = true; return x.minus(q); }; /* * Return a new Decimal whose value is the natural exponential of the value of this Decimal, * i.e. the base e raised to the power the value of this Decimal, rounded to `precision` * significant digits using rounding mode `rounding`. * */ P.naturalExponential = P.exp = function () { return naturalExponential(this); }; /* * Return a new Decimal whose value is the natural logarithm of the value of this Decimal, * rounded to `precision` significant digits using rounding mode `rounding`. * */ P.naturalLogarithm = P.ln = function () { return naturalLogarithm(this); }; /* * Return a new Decimal whose value is the value of this Decimal negated, i.e. as if multiplied by * -1. * */ P.negated = P.neg = function () { var x = new this.constructor(this); x.s = -x.s; return finalise(x); }; /* * n + 0 = n * n + N = N * n + I = I * 0 + n = n * 0 + 0 = 0 * 0 + N = N * 0 + I = I * N + n = N * N + 0 = N * N + N = N * N + I = N * I + n = I * I + 0 = I * I + N = N * I + I = I * * Return a new Decimal whose value is the value of this Decimal plus `y`, rounded to `precision` * significant digits using rounding mode `rounding`. * */ P.plus = P.add = function (y) { var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor; y = new Ctor(y); // If either is not finite... if (!x.d || !y.d) { // Return NaN if either is NaN. if (!x.s || !y.s) y = new Ctor(NaN); // Return x if y is finite and x is ±Infinity. // Return x if both are ±Infinity with the same sign. // Return NaN if both are ±Infinity with different signs. // Return y if x is finite and y is ±Infinity. else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN); return y; } // If signs differ... if (x.s != y.s) { y.s = -y.s; return x.minus(y); } xd = x.d; yd = y.d; pr = Ctor.precision; rm = Ctor.rounding; // If either is zero... if (!xd[0] || !yd[0]) { // Return x if y is zero. // Return y if y is non-zero. if (!yd[0]) y = new Ctor(x); return external ? finalise(y, pr, rm) : y; } // x and y are finite, non-zero numbers with the same sign. // Calculate base 1e7 exponents. k = mathfloor(x.e / LOG_BASE); e = mathfloor(y.e / LOG_BASE); xd = xd.slice(); i = k - e; // If base 1e7 exponents differ... if (i) { if (i < 0) { d = xd; i = -i; len = yd.length; } else { d = yd; e = k; len = xd.length; } // Limit number of zeros prepended to max(ceil(pr / LOG_BASE), len) + 1. k = Math.ceil(pr / LOG_BASE); len = k > len ? k + 1 : len + 1; if (i > len) { i = len; d.length = 1; } // Prepend zeros to equalise exponents. Note: Faster to use reverse then do unshifts. d.reverse(); for (; i--;) d.push(0); d.reverse(); } len = xd.length; i = yd.length; // If yd is longer than xd, swap xd and yd so xd points to the longer array. if (len - i < 0) { i = len; d = yd; yd = xd; xd = d; } // Only start adding at yd.length - 1 as the further digits of xd can be left as they are. for (carry = 0; i;) { carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0; xd[i] %= BASE; } if (carry) { xd.unshift(carry); ++e; } // Remove trailing zeros. // No need to check for zero, as +x + +y != 0 && -x + -y != 0 for (len = xd.length; xd[--len] == 0;) xd.pop(); y.d = xd; y.e = getBase10Exponent(xd, e); return external ? finalise(y, pr, rm) : y; }; /* * Return the number of significant digits of the value of this Decimal. * * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0. * */ P.precision = P.sd = function (z) { var k, x = this; if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z); if (x.d) { k = getPrecision(x.d); if (z && x.e + 1 > k) k = x.e + 1; } else { k = NaN; } return k; }; /* * Return a new Decimal whose value is the value of this Decimal rounded to a whole number using * rounding mode `rounding`. * */ P.round = function () { var x = this, Ctor = x.constructor; return finalise(new Ctor(x), x.e + 1, Ctor.rounding); }; /* * Return a new Decimal whose value is the sine of the value in radians of this Decimal. * * Domain: [-Infinity, Infinity] * Range: [-1, 1] * * sin(x) = x - x^3/3! + x^5/5! - ... * * sin(0) = 0 * sin(-0) = -0 * sin(Infinity) = NaN * sin(-Infinity) = NaN * sin(NaN) = NaN * */ P.sine = P.sin = function () { var pr, rm, x = this, Ctor = x.constructor; if (!x.isFinite()) return new Ctor(NaN); if (x.isZero()) return new Ctor(x); pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE; Ctor.rounding = 1; x = sine(Ctor, toLessThanHalfPi(Ctor, x)); Ctor.precision = pr; Ctor.rounding = rm; return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true); }; /* * Return a new Decimal whose value is the square root of this Decimal, rounded to `precision` * significant digits using rounding mode `rounding`. * * sqrt(-n) = N * sqrt(N) = N * sqrt(-I) = N * sqrt(I) = I * sqrt(0) = 0 * sqrt(-0) = -0 * */ P.squareRoot = P.sqrt = function () { var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor; // Negative/NaN/Infinity/zero? if (s !== 1 || !d || !d[0]) { return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0); } external = false; // Initial estimate. s = Math.sqrt(+x); // Math.sqrt underflow/overflow? // Pass x to Math.sqrt as integer, then adjust the exponent of the result. if (s == 0 || s == 1 / 0) { n = digitsToString(d); if ((n.length + e) % 2 == 0) n += '0'; s = Math.sqrt(n); e = mathfloor((e + 1) / 2) - (e < 0 || e % 2); if (s == 1 / 0) { n = '1e' + e; } else { n = s.toExponential(); n = n.slice(0, n.indexOf('e') + 1) + e; } r = new Ctor(n); } else { r = new Ctor(s.toString()); } sd = (e = Ctor.precision) + 3; // Newton-Raphson iteration. for (;;) { t = r; r = t.plus(divide(x, t, sd + 2, 1)).times(0.5); // TODO? Replace with for-loop and checkRoundingDigits. if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) { n = n.slice(sd - 3, sd + 1); // The 4th rounding digit may be in error by -1 so if the 4 rounding digits are 9999 or // 4999, i.e. approaching a rounding boundary, continue the iteration. if (n == '9999' || !rep && n == '4999') { // On the first iteration only, check to see if rounding up gives the exact result as the // nines may infinitely repeat. if (!rep) { finalise(t, e + 1, 0); if (t.times(t).eq(x)) { r = t; break; } } sd += 4; rep = 1; } else { // If the rounding digits are null, 0{0,4} or 50{0,3}, check for an exact result. // If not, then there are further digits and m will be truthy. if (!+n || !+n.slice(1) && n.charAt(0) == '5') { // Truncate to the first rounding digit. finalise(r, e + 1, 1); m = !r.times(r).eq(x); } break; } } } external = true; return finalise(r, e, Ctor.rounding, m); }; /* * Return a new Decimal whose value is the tangent of the value in radians of this Decimal. * * Domain: [-Infinity, Infinity] * Range: [-Infinity, Infinity] * * tan(0) = 0 * tan(-0) = -0 * tan(Infinity) = NaN * tan(-Infinity) = NaN * tan(NaN) = NaN * */ P.tangent = P.tan = function () { var pr, rm, x = this, Ctor = x.constructor; if (!x.isFinite()) return new Ctor(NaN); if (x.isZero()) return new Ctor(x); pr = Ctor.precision; rm = Ctor.rounding; Ctor.precision = pr + 10; Ctor.rounding = 1; x = x.sin(); x.s = 1; x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0); Ctor.precision = pr; Ctor.rounding = rm; return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true); }; /* * n * 0 = 0 * n * N = N * n * I = I * 0 * n = 0 * 0 * 0 = 0 * 0 * N = N * 0 * I = N * N * n = N * N * 0 = N * N * N = N * N * I = N * I * n = I * I * 0 = N * I * N = N * I * I = I * * Return a new Decimal whose value is this Decimal times `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * */ P.times = P.mul = function (y) { var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d; y.s *= x.s; // If either is NaN, ±Infinity or ±0... if (!xd || !xd[0] || !yd || !yd[0]) { return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd // Return NaN if either is NaN. // Return NaN if x is ±0 and y is ±Infinity, or y is ±0 and x is ±Infinity. ? NaN // Return ±Infinity if either is ±Infinity. // Return ±0 if either is ±0. : !xd || !yd ? y.s / 0 : y.s * 0); } e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE); xdL = xd.length; ydL = yd.length; // Ensure xd points to the longer array. if (xdL < ydL) { r = xd; xd = yd; yd = r; rL = xdL; xdL = ydL; ydL = rL; } // Initialise the result array with zeros. r = []; rL = xdL + ydL; for (i = rL; i--;) r.push(0); // Multiply! for (i = ydL; --i >= 0;) { carry = 0; for (k = xdL + i; k > i;) { t = r[k] + yd[i] * xd[k - i - 1] + carry; r[k--] = t % BASE | 0; carry = t / BASE | 0; } r[k] = (r[k] + carry) % BASE | 0; } // Remove trailing zeros. for (; !r[--rL];) r.pop(); if (carry) ++e; else r.shift(); y.d = r; y.e = getBase10Exponent(r, e); return external ? finalise(y, Ctor.precision, Ctor.rounding) : y; }; /* * Return a string representing the value of this Decimal in base 2, round to `sd` significant * digits using rounding mode `rm`. * * If the optional `sd` argument is present then return binary exponential notation. * * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * */ P.toBinary = function (sd, rm) { return toStringBinary(this, 2, sd, rm); }; /* * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `dp` * decimal places using rounding mode `rm` or `rounding` if `rm` is omitted. * * If `dp` is omitted, return a new Decimal whose value is the value of this Decimal. * * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * */ P.toDecimalPlaces = P.toDP = function (dp, rm) { var x = this, Ctor = x.constructor; x = new Ctor(x); if (dp === void 0) return x; checkInt32(dp, 0, MAX_DIGITS); if (rm === void 0) rm = Ctor.rounding; else checkInt32(rm, 0, 8); return finalise(x, dp + x.e + 1, rm); }; /* * Return a string representing the value of this Decimal in exponential notation rounded to * `dp` fixed decimal places using rounding mode `rounding`. * * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * */ P.toExponential = function (dp, rm) { var str, x = this, Ctor = x.constructor; if (dp === void 0) { str = finiteToString(x, true); } else { checkInt32(dp, 0, MAX_DIGITS); if (rm === void 0) rm = Ctor.rounding; else checkInt32(rm, 0, 8); x = finalise(new Ctor(x), dp + 1, rm); str = finiteToString(x, true, dp + 1); } return x.isNeg() && !x.isZero() ? '-' + str : str; }; /* * Return a string representing the value of this Decimal in normal (fixed-point) notation to * `dp` fixed decimal places and rounded using rounding mode `rm` or `rounding` if `rm` is * omitted. * * As with JavaScript numbers, (-0).toFixed(0) is '0', but e.g. (-0.00001).toFixed(0) is '-0'. * * [dp] {number} Decimal places. Integer, 0 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'. * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'. * (-0).toFixed(3) is '0.000'. * (-0.5).toFixed(0) is '-0'. * */ P.toFixed = function (dp, rm) { var str, y, x = this, Ctor = x.constructor; if (dp === void 0) { str = finiteToString(x); } else { checkInt32(dp, 0, MAX_DIGITS); if (rm === void 0) rm = Ctor.rounding; else checkInt32(rm, 0, 8); y = finalise(new Ctor(x), dp + x.e + 1, rm); str = finiteToString(y, false, dp + y.e + 1); } // To determine whether to add the minus sign look at the value before it was rounded, // i.e. look at `x` rather than `y`. return x.isNeg() && !x.isZero() ? '-' + str : str; }; /* * Return an array representing the value of this Decimal as a simple fraction with an integer * numerator and an integer denominator. * * The denominator will be a positive non-zero value less than or equal to the specified maximum * denominator. If a maximum denominator is not specified, the denominator will be the lowest * value necessary to represent the number exactly. * * [maxD] {number|string|Decimal} Maximum denominator. Integer >= 1 and < Infinity. * */ P.toFraction = function (maxD) { var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor; if (!xd) return new Ctor(x); n1 = d0 = new Ctor(1); d1 = n0 = new Ctor(0); d = new Ctor(d1); e = d.e = getPrecision(xd) - x.e - 1; k = e % LOG_BASE; d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k); if (maxD == null) { // d is 10**e, the minimum max-denominator needed. maxD = e > 0 ? d : n1; } else { n = new Ctor(maxD); if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n); maxD = n.gt(d) ? (e > 0 ? d : n1) : n; } external = false; n = new Ctor(digitsToString(xd)); pr = Ctor.precision; Ctor.precision = e = xd.length * LOG_BASE * 2; for (;;) { q = divide(n, d, 0, 1, 1); d2 = d0.plus(q.times(d1)); if (d2.cmp(maxD) == 1) break; d0 = d1; d1 = d2; d2 = n1; n1 = n0.plus(q.times(d2)); n0 = d2; d2 = d; d = n.minus(q.times(d2)); n = d2; } d2 = divide(maxD.minus(d0), d1, 0, 1, 1); n0 = n0.plus(d2.times(n1)); d0 = d0.plus(d2.times(d1)); n0.s = n1.s = x.s; // Determine which fraction is closer to x, n0/d0 or n1/d1? r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0]; Ctor.precision = pr; external = true; return r; }; /* * Return a string representing the value of this Decimal in base 16, round to `sd` significant * digits using rounding mode `rm`. * * If the optional `sd` argument is present then return binary exponential notation. * * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * */ P.toHexadecimal = P.toHex = function (sd, rm) { return toStringBinary(this, 16, sd, rm); }; /* * Returns a new Decimal whose value is the nearest multiple of the magnitude of `y` to the value * of this Decimal. * * If the value of this Decimal is equidistant from two multiples of `y`, the rounding mode `rm`, * or `Decimal.rounding` if `rm` is omitted, determines the direction of the nearest multiple. * * In the context of this method, rounding mode 4 (ROUND_HALF_UP) is the same as rounding mode 0 * (ROUND_UP), and so on. * * The return value will always have the same sign as this Decimal, unless either this Decimal * or `y` is NaN, in which case the return value will be also be NaN. * * The return value is not affected by the value of `precision`. * * y {number|string|Decimal} The magnitude to round to a multiple of. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * * 'toNearest() rounding mode not an integer: {rm}' * 'toNearest() rounding mode out of range: {rm}' * */ P.toNearest = function (y, rm) { var x = this, Ctor = x.constructor; x = new Ctor(x); if (y == null) { // If x is not finite, return x. if (!x.d) return x; y = new Ctor(1); rm = Ctor.rounding; } else { y = new Ctor(y); if (rm !== void 0) checkInt32(rm, 0, 8); // If x is not finite, return x if y is not NaN, else NaN. if (!x.d) return y.s ? x : y; // If y is not finite, return Infinity with the sign of x if y is Infinity, else NaN. if (!y.d) { if (y.s) y.s = x.s; return y; } } // If y is not zero, calculate the nearest multiple of y to x. if (y.d[0]) { external = false; if (rm < 4) rm = [4, 5, 7, 8][rm]; x = divide(x, y, 0, rm, 1).times(y); external = true; finalise(x); // If y is zero, return zero with the sign of x. } else { y.s = x.s; x = y; } return x; }; /* * Return the value of this Decimal converted to a number primitive. * Zero keeps its sign. * */ P.toNumber = function () { return +this; }; /* * Return a string representing the value of this Decimal in base 8, round to `sd` significant * digits using rounding mode `rm`. * * If the optional `sd` argument is present then return binary exponential notation. * * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * */ P.toOctal = function (sd, rm) { return toStringBinary(this, 8, sd, rm); }; /* * Return a new Decimal whose value is the value of this Decimal raised to the power `y`, rounded * to `precision` significant digits using rounding mode `rounding`. * * ECMAScript compliant. * * pow(x, NaN) = NaN * pow(x, ±0) = 1 * pow(NaN, non-zero) = NaN * pow(abs(x) > 1, +Infinity) = +Infinity * pow(abs(x) > 1, -Infinity) = +0 * pow(abs(x) == 1, ±Infinity) = NaN * pow(abs(x) < 1, +Infinity) = +0 * pow(abs(x) < 1, -Infinity) = +Infinity * pow(+Infinity, y > 0) = +Infinity * pow(+Infinity, y < 0) = +0 * pow(-Infinity, odd integer > 0) = -Infinity * pow(-Infinity, even integer > 0) = +Infinity * pow(-Infinity, odd integer < 0) = -0 * pow(-Infinity, even integer < 0) = +0 * pow(+0, y > 0) = +0 * pow(+0, y < 0) = +Infinity * pow(-0, odd integer > 0) = -0 * pow(-0, even integer > 0) = +0 * pow(-0, odd integer < 0) = -Infinity * pow(-0, even integer < 0) = +Infinity * pow(finite x < 0, finite non-integer) = NaN * * For non-integer or very large exponents pow(x, y) is calculated using * * x^y = exp(y*ln(x)) * * Assuming the first 15 rounding digits are each equally likely to be any digit 0-9, the * probability of an incorrectly rounded result * P([49]9{14} | [50]0{14}) = 2 * 0.2 * 10^-14 = 4e-15 = 1/2.5e+14 * i.e. 1 in 250,000,000,000,000 * * If a result is incorrectly rounded the maximum error will be 1 ulp (unit in last place). * * y {number|string|Decimal} The power to which to raise this Decimal. * */ P.toPower = P.pow = function (y) { var e, k, pr, r, rm, sign, yIsInt, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y)); // Either ±Infinity, NaN or ±0? if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn)); x = new Ctor(x); if (x.eq(1)) return x; pr = Ctor.precision; rm = Ctor.rounding; if (y.eq(1)) return finalise(x, pr, rm); e = mathfloor(y.e / LOG_BASE); k = y.d.length - 1; yIsInt = e >= k; sign = x.s; if (!yIsInt) { if (sign < 0) return new Ctor(NaN); // If y is a small integer use the 'exponentiation by squaring' algorithm. } else if ((k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) { r = intPow(Ctor, x, k, pr); return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm); } // Result is negative if x is negative and the last digit of integer y is odd. sign = sign < 0 && y.d[Math.max(e, k)] & 1 ? -1 : 1; // Estimate result exponent. // x^y = 10^e, where e = y * log10(x) // log10(x) = log10(x_significand) + x_exponent // log10(x_significand) = ln(x_significand) / ln(10) k = mathpow(+x, yn); e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log('0.' + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + '').e; // Estimate may be incorrect e.g. x: 0.999999999999999999, y: 2.29, e: 0, r.e: -1. // Overflow/underflow? if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? sign / 0 : 0); external = false; Ctor.rounding = x.s = 1; // Estimate the extra guard digits needed to ensure five correct rounding digits from // naturalLogarithm(x). Example of failure without these extra digits (precision: 10): // new Decimal(2.32456).pow('2087987436534566.46411') // should be 1.162377823e+764914905173815, but is 1.162355823e+764914905173815 k = Math.min(12, (e + '').length); // r = x^y = exp(y*ln(x)) r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr); // Truncate to the required precision plus five rounding digits. r = finalise(r, pr + 5, 1); // If the rounding digits are [49]9999 or [50]0000 increase the precision by 10 and recalculate // the result. if (checkRoundingDigits(r.d, pr, rm)) { e = pr + 10; // Truncate to the increased precision plus five rounding digits. r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1); // Check for 14 nines from the 2nd rounding digit (the first rounding digit may be 4 or 9). if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) { r = finalise(r, pr + 1, 0); } } r.s = sign; external = true; Ctor.rounding = rm; return finalise(r, pr, rm); }; /* * Return a string representing the value of this Decimal rounded to `sd` significant digits * using rounding mode `rounding`. * * Return exponential notation if `sd` is less than the number of digits necessary to represent * the integer part of the value in normal notation. * * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * */ P.toPrecision = function (sd, rm) { var str, x = this, Ctor = x.constructor; if (sd === void 0) { str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos); } else { checkInt32(sd, 1, MAX_DIGITS); if (rm === void 0) rm = Ctor.rounding; else checkInt32(rm, 0, 8); x = finalise(new Ctor(x), sd, rm); str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd); } return x.isNeg() && !x.isZero() ? '-' + str : str; }; /* * Return a new Decimal whose value is the value of this Decimal rounded to a maximum of `sd` * significant digits using rounding mode `rm`, or to `precision` and `rounding` respectively if * omitted. * * [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive. * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. * * 'toSD() digits out of range: {sd}' * 'toSD() digits not an integer: {sd}' * 'toSD() rounding mode not an integer: {rm}' * 'toSD() rounding mode out of range: {rm}' * */ P.toSignificantDigits = P.toSD = function (sd, rm) { var x = this, Ctor = x.constructor; if (sd === void 0) { sd = Ctor.precision; rm = Ctor.rounding; } else { checkInt32(sd, 1, MAX_DIGITS); if (rm === void 0) rm = Ctor.rounding; else checkInt32(rm, 0, 8); } return finalise(new Ctor(x), sd, rm); }; /* * Return a string representing the value of this Decimal. * * Return exponential notation if this Decimal has a positive exponent equal to or greater than * `toExpPos`, or a negative exponent equal to or less than `toExpNeg`. * */ P.toString = function () { var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos); return x.isNeg() && !x.isZero() ? '-' + str : str; }; /* * Return a new Decimal whose value is the value of this Decimal truncated to a whole number. * */ P.truncated = P.trunc = function () { return finalise(new this.constructor(this), this.e + 1, 1); }; /* * Return a string representing the value of this Decimal. * Unlike `toString`, negative zero will include the minus sign. * */ P.valueOf = P.toJSON = function () { var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos); return x.isNeg() ? '-' + str : str; }; /* // Add aliases to match BigDecimal method names. // P.add = P.plus; P.subtract = P.minus; P.multiply = P.times; P.divide = P.div; P.remainder = P.mod; P.compareTo = P.cmp; P.negate = P.neg; */ // Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers. /* * digitsToString P.cubeRoot, P.logarithm, P.squareRoot, P.toFraction, P.toPower, * finiteToString, naturalExponential, naturalLogarithm * checkInt32 P.toDecimalPlaces, P.toExponential, P.toFixed, P.toNearest, * P.toPrecision, P.toSignificantDigits, toStringBinary, random * checkRoundingDigits P.logarithm, P.toPower, naturalExponential, naturalLogarithm * convertBase toStringBinary, parseOther * cos P.cos * divide P.atanh, P.cubeRoot, P.dividedBy, P.dividedToIntegerBy, * P.logarithm, P.modulo, P.squareRoot, P.tan, P.tanh, P.toFraction, * P.toNearest, toStringBinary, naturalExponential, naturalLogarithm, * taylorSeries, atan2, parseOther * finalise P.absoluteValue, P.atan, P.atanh, P.ceil, P.cos, P.cosh, * P.cubeRoot, P.dividedToIntegerBy, P.floor, P.logarithm, P.minus, * P.modulo, P.negated, P.plus, P.round, P.sin, P.sinh, P.squareRoot, * P.tan, P.times, P.toDecimalPlaces, P.toExponential, P.toFixed, * P.toNearest, P.toPower, P.toPrecision, P.toSignificantDigits, * P.truncated, divide, getLn10, getPi, naturalExponential, * naturalLogarithm, ceil, floor, round, trunc * finiteToString P.toExponential, P.toFixed, P.toPrecision, P.toString, P.valueOf, * toStringBinary * getBase10Exponent P.minus, P.plus, P.times, parseOther * getLn10 P.logarithm, naturalLogarithm * getPi P.acos, P.asin, P.atan, toLessThanHalfPi, atan2 * getPrecision P.precision, P.toFraction * getZeroString digitsToString, finiteToString * intPow P.toPower, parseOther * isOdd toLessThanHalfPi * maxOrMin max, min * naturalExponential P.naturalExponential, P.toPower * naturalLogarithm P.acosh, P.asinh, P.atanh, P.logarithm, P.naturalLogarithm, * P.toPower, naturalExponential * nonFiniteToString finiteToString, toStringBinary * parseDecimal Decimal * parseOther Decimal * sin P.sin * taylorSeries P.cosh, P.sinh, cos, sin * toLessThanHalfPi P.cos, P.sin * toStringBinary P.toBinary, P.toHexadecimal, P.toOctal * truncate intPow * * Throws: P.logarithm, P.precision, P.toFraction, checkInt32, getLn10, getPi, * naturalLogarithm, config, parseOther, random, Decimal */ function digitsToString(d) { var i, k, ws, indexOfLastWord = d.length - 1, str = '', w = d[0]; if (indexOfLastWord > 0) { str += w; for (i = 1; i < indexOfLastWord; i++) { ws = d[i] + ''; k = LOG_BASE - ws.length; if (k) str += getZeroString(k); str += ws; } w = d[i]; ws = w + ''; k = LOG_BASE - ws.length; if (k) str += getZeroString(k); } else if (w === 0) { return '0'; } // Remove trailing zeros of last w. for (; w % 10 === 0;) w /= 10; return str + w; } function checkInt32(i, min, max) { if (i !== ~~i || i < min || i > max) { throw Error(invalidArgument + i); } } /* * Check 5 rounding digits if `repeating` is null, 4 otherwise. * `repeating == null` if caller is `log` or `pow`, * `repeating != null` if caller is `naturalLogarithm` or `naturalExponential`. */ function checkRoundingDigits(d, i, rm, repeating) { var di, k, r, rd; // Get the length of the first word of the array d. for (k = d[0]; k >= 10; k /= 10) --i; // Is the rounding digit in the first word of d? if (--i < 0) { i += LOG_BASE; di = 0; } else { di = Math.ceil((i + 1) / LOG_BASE); i %= LOG_BASE; } // i is the index (0 - 6) of the rounding digit. // E.g. if within the word 3487563 the first rounding digit is 5, // then i = 4, k = 1000, rd = 3487563 % 1000 = 563 k = mathpow(10, LOG_BASE - i); rd = d[di] % k | 0; if (repeating == null) { if (i < 3) { if (i == 0) rd = rd / 100 | 0; else if (i == 1) rd = rd / 10 | 0; r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 50000 || rd == 0; } else { r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 || (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0; } } else { if (i < 4) { if (i == 0) rd = rd / 1000 | 0; else if (i == 1) rd = rd / 100 | 0; else if (i == 2) rd = rd / 10 | 0; r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999; } else { r = ((repeating || rm < 4) && rd + 1 == k || (!repeating && rm > 3) && rd + 1 == k / 2) && (d[di + 1] / k / 1000 | 0) == mathpow(10, i - 3) - 1; } } return r; } // Convert string of `baseIn` to an array of numbers of `baseOut`. // Eg. convertBase('255', 10, 16) returns [15, 15]. // Eg. convertBase('ff', 16, 10) returns [2, 5, 5]. function convertBase(str, baseIn, baseOut) { var j, arr = [0], arrL, i = 0, strL = str.length; for (; i < strL;) { for (arrL = arr.length; arrL--;) arr[arrL] *= baseIn; arr[0] += NUMERALS.indexOf(str.charAt(i++)); for (j = 0; j < arr.length; j++) { if (arr[j] > baseOut - 1) { if (arr[j + 1] === void 0) arr[j + 1] = 0; arr[j + 1] += arr[j] / baseOut | 0; arr[j] %= baseOut; } } } return arr.reverse(); } /* * cos(x) = 1 - x^2/2! + x^4/4! - ... * |x| < pi/2 * */ function cosine(Ctor, x) { var k, y, len = x.d.length; // Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1 // i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1 // Estimate the optimum number of times to use the argument reduction. if (len < 32) { k = Math.ceil(len / 3); y = Math.pow(4, -k).toString(); } else { k = 16; y = '2.3283064365386962890625e-10'; } Ctor.precision += k; x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1)); // Reverse argument reduction for (var i = k; i--;) { var cos2x = x.times(x); x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1); } Ctor.precision -= k; return x; } /* * Perform division in the specified base. */ var divide = (function () { // Assumes non-zero x and k, and hence non-zero result. function multiplyInteger(x, k, base) { var temp, carry = 0, i = x.length; for (x = x.slice(); i--;) { temp = x[i] * k + carry; x[i] = temp % base | 0; carry = temp / base | 0; } if (carry) x.unshift(carry); return x; } function compare(a, b, aL, bL) { var i, r; if (aL != bL) { r = aL > bL ? 1 : -1; } else { for (i = r = 0; i < aL; i++) { if (a[i] != b[i]) { r = a[i] > b[i] ? 1 : -1; break; } } } return r; } function subtract(a, b, aL, base) { var i = 0; // Subtract b from a. for (; aL--;) { a[aL] -= i; i = a[aL] < b[aL] ? 1 : 0; a[aL] = i * base + a[aL] - b[aL]; } // Remove leading zeros. for (; !a[0] && a.length > 1;) a.shift(); } return function (x, y, pr, rm, dp, base) { var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, yL, yz, Ctor = x.constructor, sign = x.s == y.s ? 1 : -1, xd = x.d, yd = y.d; // Either NaN, Infinity or 0? if (!xd || !xd[0] || !yd || !yd[0]) { return new Ctor(// Return NaN if either NaN, or both Infinity or 0. !x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : // Return ±0 if x is 0 or y is ±Infinity, or return ±Infinity as y is 0. xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0); } if (base) { logBase = 1; e = x.e - y.e; } else { base = BASE; logBase = LOG_BASE; e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase); } yL = yd.length; xL = xd.length; q = new Ctor(sign); qd = q.d = []; // Result exponent may be one less than e. // The digit array of a Decimal from toStringBinary may have trailing zeros. for (i = 0; yd[i] == (xd[i] || 0); i++); if (yd[i] > (xd[i] || 0)) e--; if (pr == null) { sd = pr = Ctor.precision; rm = Ctor.rounding; } else if (dp) { sd = pr + (x.e - y.e) + 1; } else { sd = pr; } if (sd < 0) { qd.push(1); more = true; } else { // Convert precision in number of base 10 digits to base 1e7 digits. sd = sd / logBase + 2 | 0; i = 0; // divisor < 1e7 if (yL == 1) { k = 0; yd = yd[0]; sd++; // k is the carry. for (; (i < xL || k) && sd--; i++) { t = k * base + (xd[i] || 0); qd[i] = t / yd | 0; k = t % yd | 0; } more = k || i < xL; // divisor >= 1e7 } else { // Normalise xd and yd so highest order digit of yd is >= base/2 k = base / (yd[0] + 1) | 0; if (k > 1) { yd = multiplyInteger(yd, k, base); xd = multiplyInteger(xd, k, base); yL = yd.length; xL = xd.length; } xi = yL; rem = xd.slice(0, yL); remL = rem.length; // Add zeros to make remainder as long as divisor. for (; remL < yL;) rem[remL++] = 0; yz = yd.slice(); yz.unshift(0); yd0 = yd[0]; if (yd[1] >= base / 2) ++yd0; do { k = 0; // Compare divisor and remainder. cmp = compare(yd, rem, yL, remL); // If divisor < remainder. if (cmp < 0) { // Calculate trial digit, k. rem0 = rem[0]; if (yL != remL) rem0 = rem0 * base + (rem[1] || 0); // k will be how many times the divisor goes into the current remainder. k = rem0 / yd0 | 0; // Algorithm: // 1. product = divisor * trial digit (k) // 2. if product > remainder: product -= divisor, k-- // 3. remainder -= product // 4. if product was < remainder at 2: // 5. compare new remainder and divisor // 6. If remainder > divisor: remainder -= divisor, k++ if (k > 1) { if (k >= base) k = base - 1; // product = divisor * trial digit. prod = multiplyInteger(yd, k, base); prodL = prod.length; remL = rem.length; // Compare product and remainder. cmp = compare(prod, rem, prodL, remL); // product > remainder. if (cmp == 1) { k--; // Subtract divisor from product. subtract(prod, yL < prodL ? yz : yd, prodL, base); } } else { // cmp is -1. // If k is 0, there is no need to compare yd and rem again below, so change cmp to 1 // to avoid it. If k is 1 there is a need to compare yd and rem again below. if (k == 0) cmp = k = 1; prod = yd.slice(); } prodL = prod.length; if (prodL < remL) prod.unshift(0); // Subtract product from remainder. subtract(rem, prod, remL, base); // If product was < previous remainder. if (cmp == -1) { remL = rem.length; // Compare divisor and new remainder. cmp = compare(yd, rem, yL, remL); // If divisor < new remainder, subtract divisor from remainder. if (cmp < 1) { k++; // Subtract divisor from remainder. subtract(rem, yL < remL ? yz : yd, remL, base); } } remL = rem.length; } else if (cmp === 0) { k++; rem = [0]; } // if cmp === 1, k will be 0 // Add the next digit, k, to the result array. qd[i++] = k; // Update the remainder. if (cmp && rem[0]) { rem[remL++] = xd[xi] || 0; } else { rem = [xd[xi]]; remL = 1; } } while ((xi++ < xL || rem[0] !== void 0) && sd--); more = rem[0] !== void 0; } // Leading zero? if (!qd[0]) qd.shift(); } // logBase is 1 when divide is being used for base conversion. if (logBase == 1) { q.e = e; inexact = more; } else { // To calculate q.e, first get the number of digits of qd[0]. for (i = 1, k = qd[0]; k >= 10; k /= 10) i++; q.e = i + e * logBase - 1; finalise(q, dp ? pr + q.e + 1 : pr, rm, more); } return q; }; })(); /* * Round `x` to `sd` significant digits using rounding mode `rm`. * Check for over/under-flow. */ function finalise(x, sd, rm, isTruncated) { var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor; // Don't round if sd is null or undefined. out: if (sd != null) { xd = x.d; // Infinity/NaN. if (!xd) return x; // rd: the rounding digit, i.e. the digit after the digit that may be rounded up. // w: the word of xd containing rd, a base 1e7 number. // xdi: the index of w within xd. // digits: the number of digits of w. // i: what would be the index of rd within w if all the numbers were 7 digits long (i.e. if // they had leading zeros) // j: if > 0, the actual index of rd within w (if < 0, rd is a leading zero). // Get the length of the first word of the digits array xd. for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++; i = sd - digits; // Is the rounding digit in the first word of xd? if (i < 0) { i += LOG_BASE; j = sd; w = xd[xdi = 0]; // Get the rounding digit at index j of w. rd = w / mathpow(10, digits - j - 1) % 10 | 0; } else { xdi = Math.ceil((i + 1) / LOG_BASE); k = xd.length; if (xdi >= k) { if (isTruncated) { // Needed by `naturalExponential`, `naturalLogarithm` and `squareRoot`. for (; k++ <= xdi;) xd.push(0); w = rd = 0; digits = 1; i %= LOG_BASE; j = i - LOG_BASE + 1; } else { break out; } } else { w = k = xd[xdi]; // Get the number of digits of w. for (digits = 1; k >= 10; k /= 10) digits++; // Get the index of rd within w. i %= LOG_BASE; // Get the index of rd within w, adjusted for leading zeros. // The number of leading zeros of w is given by LOG_BASE - digits. j = i - LOG_BASE + digits; // Get the rounding digit at index j of w. rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0; } } // Are there any non-zero digits after the rounding digit? isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1)); // The expression `w % mathpow(10, digits - j - 1)` returns all the digits of w to the right // of the digit at (left-to-right) index j, e.g. if w is 908714 and j is 2, the expression // will give 714. roundUp = rm < 4 ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 && // Check whether the digit to the left of the rounding digit is odd. ((i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10) & 1 || rm == (x.s < 0 ? 8 : 7)); if (sd < 1 || !xd[0]) { xd.length = 0; if (roundUp) { // Convert sd to decimal places. sd -= x.e + 1; // 1, 0.1, 0.01, 0.001, 0.0001 etc. xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE); x.e = -sd || 0; } else { // Zero. xd[0] = x.e = 0; } return x; } // Remove excess digits. if (i == 0) { xd.length = xdi; k = 1; xdi--; } else { xd.length = xdi + 1; k = mathpow(10, LOG_BASE - i); // E.g. 56700 becomes 56000 if 7 is the rounding digit. // j > 0 means i > number of leading zeros of w. xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0; } if (roundUp) { for (;;) { // Is the digit to be rounded up in the first word of xd? if (xdi == 0) { // i will be the length of xd[0] before k is added. for (i = 1, j = xd[0]; j >= 10; j /= 10) i++; j = xd[0] += k; for (k = 1; j >= 10; j /= 10) k++; // if i != k the length has increased. if (i != k) { x.e++; if (xd[0] == BASE) xd[0] = 1; } break; } else { xd[xdi] += k; if (xd[xdi] != BASE) break; xd[xdi--] = 0; k = 1; } } } // Remove trailing zeros. for (i = xd.length; xd[--i] === 0;) xd.pop(); } if (external) { // Overflow? if (x.e > Ctor.maxE) { // Infinity. x.d = null; x.e = NaN; // Underflow? } else if (x.e < Ctor.minE) { // Zero. x.e = 0; x.d = [0]; // Ctor.underflow = true; } // else Ctor.underflow = false; } return x; } function finiteToString(x, isExp, sd) { if (!x.isFinite()) return nonFiniteToString(x); var k, e = x.e, str = digitsToString(x.d), len = str.length; if (isExp) { if (sd && (k = sd - len) > 0) { str = str.charAt(0) + '.' + str.slice(1) + getZeroString(k); } else if (len > 1) { str = str.charAt(0) + '.' + str.slice(1); } str = str + (x.e < 0 ? 'e' : 'e+') + x.e; } else if (e < 0) { str = '0.' + getZeroString(-e - 1) + str; if (sd && (k = sd - len) > 0) str += getZeroString(k); } else if (e >= len) { str += getZeroString(e + 1 - len); if (sd && (k = sd - e - 1) > 0) str = str + '.' + getZeroString(k); } else { if ((k = e + 1) < len) str = str.slice(0, k) + '.' + str.slice(k); if (sd && (k = sd - len) > 0) { if (e + 1 === len) str += '.'; str += getZeroString(k); } } return str; } // Calculate the base 10 exponent from the base 1e7 exponent. function getBase10Exponent(digits, e) { var w = digits[0]; // Add the number of digits of the first word of the digits array. for ( e *= LOG_BASE; w >= 10; w /= 10) e++; return e; } function getLn10(Ctor, sd, pr) { if (sd > LN10_PRECISION) { // Reset global state in case the exception is caught. external = true; if (pr) Ctor.precision = pr; throw Error(precisionLimitExceeded); } return finalise(new Ctor(LN10), sd, 1, true); } function getPi(Ctor, sd, rm) { if (sd > PI_PRECISION) throw Error(precisionLimitExceeded); return finalise(new Ctor(PI), sd, rm, true); } function getPrecision(digits) { var w = digits.length - 1, len = w * LOG_BASE + 1; w = digits[w]; // If non-zero... if (w) { // Subtract the number of trailing zeros of the last word. for (; w % 10 == 0; w /= 10) len--; // Add the number of digits of the first word. for (w = digits[0]; w >= 10; w /= 10) len++; } return len; } function getZeroString(k) { var zs = ''; for (; k--;) zs += '0'; return zs; } /* * Return a new Decimal whose value is the value of Decimal `x` to the power `n`, where `n` is an * integer of type number. * * Implements 'exponentiation by squaring'. Called by `pow` and `parseOther`. * */ function intPow(Ctor, x, n, pr) { var isTruncated, r = new Ctor(1), // Max n of 9007199254740991 takes 53 loop iterations. // Maximum digits array length; leaves [28, 34] guard digits. k = Math.ceil(pr / LOG_BASE + 4); external = false; for (;;) { if (n % 2) { r = r.times(x); if (truncate(r.d, k)) isTruncated = true; } n = mathfloor(n / 2); if (n === 0) { // To ensure correct rounding when r.d is truncated, increment the last word if it is zero. n = r.d.length - 1; if (isTruncated && r.d[n] === 0) ++r.d[n]; break; } x = x.times(x); truncate(x.d, k); } external = true; return r; } function isOdd(n) { return n.d[n.d.length - 1] & 1; } /* * Handle `max` and `min`. `ltgt` is 'lt' or 'gt'. */ function maxOrMin(Ctor, args, ltgt) { var y, x = new Ctor(args[0]), i = 0; for (; ++i < args.length;) { y = new Ctor(args[i]); if (!y.s) { x = y; break; } else if (x[ltgt](y)) { x = y; } } return x; } /* * Return a new Decimal whose value is the natural exponential of `x` rounded to `sd` significant * digits. * * Taylor/Maclaurin series. * * exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + ... * * Argument reduction: * Repeat x = x / 32, k += 5, until |x| < 0.1 * exp(x) = exp(x / 2^k)^(2^k) * * Previously, the argument was initially reduced by * exp(x) = exp(r) * 10^k where r = x - k * ln10, k = floor(x / ln10) * to first put r in the range [0, ln10], before dividing by 32 until |x| < 0.1, but this was * found to be slower than just dividing repeatedly by 32 as above. * * Max integer argument: exp('20723265836946413') = 6.3e+9000000000000000 * Min integer argument: exp('-20723265836946411') = 1.2e-9000000000000000 * (Math object integer min/max: Math.exp(709) = 8.2e+307, Math.exp(-745) = 5e-324) * * exp(Infinity) = Infinity * exp(-Infinity) = 0 * exp(NaN) = NaN * exp(±0) = 1 * * exp(x) is non-terminating for any finite, non-zero x. * * The result will always be correctly rounded. * */ function naturalExponential(x, sd) { var denominator, guard, j, pow, sum, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision; // 0/NaN/Infinity? if (!x.d || !x.d[0] || x.e > 17) { return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : 0 / 0); } if (sd == null) { external = false; wpr = pr; } else { wpr = sd; } t = new Ctor(0.03125); // while abs(x) >= 0.1 while (x.e > -2) { // x = x / 2^5 x = x.times(t); k += 5; } // Use 2 * log10(2^k) + 5 (empirically derived) to estimate the increase in precision // necessary to ensure the first 4 rounding digits are correct. guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0; wpr += guard; denominator = pow = sum = new Ctor(1); Ctor.precision = wpr; for (;;) { pow = finalise(pow.times(x), wpr, 1); denominator = denominator.times(++i); t = sum.plus(divide(pow, denominator, wpr, 1)); if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) { j = k; while (j--) sum = finalise(sum.times(sum), wpr, 1); // Check to see if the first 4 rounding digits are [49]999. // If so, repeat the summation with a higher precision, otherwise // e.g. with precision: 18, rounding: 1 // exp(18.404272462595034083567793919843761) = 98372560.1229999999 (should be 98372560.123) // `wpr - guard` is the index of first rounding digit. if (sd == null) { if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) { Ctor.precision = wpr += 10; denominator = pow = t = new Ctor(1); i = 0; rep++; } else { return finalise(sum, Ctor.precision = pr, rm, external = true); } } else { Ctor.precision = pr; return sum; } } sum = t; } } /* * Return a new Decimal whose value is the natural logarithm of `x` rounded to `sd` significant * digits. * * ln(-n) = NaN * ln(0) = -Infinity * ln(-0) = -Infinity * ln(1) = 0 * ln(Infinity) = Infinity * ln(-Infinity) = NaN * ln(NaN) = NaN * * ln(n) (n != 1) is non-terminating. * */ function naturalLogarithm(y, sd) { var c, c0, denominator, e, numerator, rep, sum, t, wpr, x1, x2, n = 1, guard = 10, x = y, xd = x.d, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision; // Is x negative or Infinity, NaN, 0 or 1? if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) { return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x); } if (sd == null) { external = false; wpr = pr; } else { wpr = sd; } Ctor.precision = wpr += guard; c = digitsToString(xd); c0 = c.charAt(0); if (Math.abs(e = x.e) < 1.5e15) { // Argument reduction. // The series converges faster the closer the argument is to 1, so using // ln(a^b) = b * ln(a), ln(a) = ln(a^b) / b // multiply the argument by itself until the leading digits of the significand are 7, 8, 9, // 10, 11, 12 or 13, recording the number of multiplications so the sum of the series can // later be divided by this number, then separate out the power of 10 using // ln(a*10^b) = ln(a) + b*ln(10). // max n is 21 (gives 0.9, 1.0 or 1.1) (9e15 / 21 = 4.2e14). //while (c0 < 9 && c0 != 1 || c0 == 1 && c.charAt(1) > 1) { // max n is 6 (gives 0.7 - 1.3) while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) { x = x.times(y); c = digitsToString(x.d); c0 = c.charAt(0); n++; } e = x.e; if (c0 > 1) { x = new Ctor('0.' + c); e++; } else { x = new Ctor(c0 + '.' + c.slice(1)); } } else { // The argument reduction method above may result in overflow if the argument y is a massive // number with exponent >= 1500000000000000 (9e15 / 6 = 1.5e15), so instead recall this // function using ln(x*10^e) = ln(x) + e*ln(10). t = getLn10(Ctor, wpr + 2, pr).times(e + ''); x = naturalLogarithm(new Ctor(c0 + '.' + c.slice(1)), wpr - guard).plus(t); Ctor.precision = pr; return sd == null ? finalise(x, pr, rm, external = true) : x; } // x1 is x reduced to a value near 1. x1 = x; // Taylor series. // ln(y) = ln((1 + x)/(1 - x)) = 2(x + x^3/3 + x^5/5 + x^7/7 + ...) // where x = (y - 1)/(y + 1) (|x| < 1) sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1); x2 = finalise(x.times(x), wpr, 1); denominator = 3; for (;;) { numerator = finalise(numerator.times(x2), wpr, 1); t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1)); if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) { sum = sum.times(2); // Reverse the argument reduction. Check that e is not 0 because, besides preventing an // unnecessary calculation, -0 + 0 = +0 and to ensure correct rounding -0 needs to stay -0. if (e !== 0) sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + '')); sum = divide(sum, new Ctor(n), wpr, 1); // Is rm > 3 and the first 4 rounding digits 4999, or rm < 4 (or the summation has // been repeated previously) and the first 4 rounding digits 9999? // If so, restart the summation with a higher precision, otherwise // e.g. with precision: 12, rounding: 1 // ln(135520028.6126091714265381533) = 18.7246299999 when it should be 18.72463. // `wpr - guard` is the index of first rounding digit. if (sd == null) { if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) { Ctor.precision = wpr += guard; t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1); x2 = finalise(x.times(x), wpr, 1); denominator = rep = 1; } else { return finalise(sum, Ctor.precision = pr, rm, external = true); } } else { Ctor.precision = pr; return sum; } } sum = t; denominator += 2; } } // ±Infinity, NaN. function nonFiniteToString(x) { // Unsigned. return String(x.s * x.s / 0); } /* * Parse the value of a new Decimal `x` from string `str`. */ function parseDecimal(x, str) { var e, i, len; // Decimal point? if ((e = str.indexOf('.')) > -1) str = str.replace('.', ''); // Exponential form? if ((i = str.search(/e/i)) > 0) { // Determine exponent. if (e < 0) e = i; e += +str.slice(i + 1); str = str.substring(0, i); } else if (e < 0) { // Integer. e = str.length; } // Determine leading zeros. for (i = 0; str.charCodeAt(i) === 48; i++); // Determine trailing zeros. for (len = str.length; str.charCodeAt(len - 1) === 48; --len); str = str.slice(i, len); if (str) { len -= i; x.e = e = e - i - 1; x.d = []; // Transform base // e is the base 10 exponent. // i is where to slice str to get the first word of the digits array. i = (e + 1) % LOG_BASE; if (e < 0) i += LOG_BASE; if (i < len) { if (i) x.d.push(+str.slice(0, i)); for (len -= LOG_BASE; i < len;) x.d.push(+str.slice(i, i += LOG_BASE)); str = str.slice(i); i = LOG_BASE - str.length; } else { i -= len; } for (; i--;) str += '0'; x.d.push(+str); if (external) { // Overflow? if (x.e > x.constructor.maxE) { // Infinity. x.d = null; x.e = NaN; // Underflow? } else if (x.e < x.constructor.minE) { // Zero. x.e = 0; x.d = [0]; // x.constructor.underflow = true; } // else x.constructor.underflow = false; } } else { // Zero. x.e = 0; x.d = [0]; } return x; } /* * Parse the value of a new Decimal `x` from a string `str`, which is not a decimal value. */ function parseOther(x, str) { var base, Ctor, divisor, i, isFloat, len, p, xd, xe; if (str === 'Infinity' || str === 'NaN') { if (!+str) x.s = NaN; x.e = NaN; x.d = null; return x; } if (isHex.test(str)) { base = 16; str = str.toLowerCase(); } else if (isBinary.test(str)) { base = 2; } else if (isOctal.test(str)) { base = 8; } else { throw Error(invalidArgument + str); } // Is there a binary exponent part? i = str.search(/p/i); if (i > 0) { p = +str.slice(i + 1); str = str.substring(2, i); } else { str = str.slice(2); } // Convert `str` as an integer then divide the result by `base` raised to a power such that the // fraction part will be restored. i = str.indexOf('.'); isFloat = i >= 0; Ctor = x.constructor; if (isFloat) { str = str.replace('.', ''); len = str.length; i = len - i; // log[10](16) = 1.2041... , log[10](88) = 1.9444.... divisor = intPow(Ctor, new Ctor(base), i, i * 2); } xd = convertBase(str, base, BASE); xe = xd.length - 1; // Remove trailing zeros. for (i = xe; xd[i] === 0; --i) xd.pop(); if (i < 0) return new Ctor(x.s * 0); x.e = getBase10Exponent(xd, xe); x.d = xd; external = false; // At what precision to perform the division to ensure exact conversion? // maxDecimalIntegerPartDigitCount = ceil(log[10](b) * otherBaseIntegerPartDigitCount) // log[10](2) = 0.30103, log[10](8) = 0.90309, log[10](16) = 1.20412 // E.g. ceil(1.2 * 3) = 4, so up to 4 decimal digits are needed to represent 3 hex int digits. // maxDecimalFractionPartDigitCount = {Hex:4|Oct:3|Bin:1} * otherBaseFractionPartDigitCount // Therefore using 4 * the number of digits of str will always be enough. if (isFloat) x = divide(x, divisor, len * 4); // Multiply by the binary exponent part if present. if (p) x = x.times(Math.abs(p) < 54 ? Math.pow(2, p) : Decimal.pow(2, p)); external = true; return x; } /* * sin(x) = x - x^3/3! + x^5/5! - ... * |x| < pi/2 * */ function sine(Ctor, x) { var k, len = x.d.length; if (len < 3) return taylorSeries(Ctor, 2, x, x); // Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x) // i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5) // and sin(x) = sin(x/5)(5 + sin^2(x/5)(16sin^2(x/5) - 20)) // Estimate the optimum number of times to use the argument reduction. k = 1.4 * Math.sqrt(len); k = k > 16 ? 16 : k | 0; // Max k before Math.pow precision loss is 22 x = x.times(Math.pow(5, -k)); x = taylorSeries(Ctor, 2, x, x); // Reverse argument reduction var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20); for (; k--;) { sin2_x = x.times(x); x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20)))); } return x; } // Calculate Taylor series for `cos`, `cosh`, `sin` and `sinh`. function taylorSeries(Ctor, n, x, y, isHyperbolic) { var j, t, u, x2, i = 1, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE); external = false; x2 = x.times(x); u = new Ctor(y); for (;;) { t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1); u = isHyperbolic ? y.plus(t) : y.minus(t); y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1); t = u.plus(y); if (t.d[k] !== void 0) { for (j = k; t.d[j] === u.d[j] && j--;); if (j == -1) break; } j = u; u = y; y = t; t = j; i++; } external = true; t.d.length = k + 1; return t; } // Return the absolute value of `x` reduced to less than or equal to half pi. function toLessThanHalfPi(Ctor, x) { var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(0.5); x = x.abs(); if (x.lte(halfPi)) { quadrant = isNeg ? 4 : 1; return x; } t = x.divToInt(pi); if (t.isZero()) { quadrant = isNeg ? 3 : 2; } else { x = x.minus(t.times(pi)); // 0 <= x < pi if (x.lte(halfPi)) { quadrant = isOdd(t) ? (isNeg ? 2 : 3) : (isNeg ? 4 : 1); return x; } quadrant = isOdd(t) ? (isNeg ? 1 : 4) : (isNeg ? 3 : 2); } return x.minus(pi).abs(); } /* * Return the value of Decimal `x` as a string in base `baseOut`. * * If the optional `sd` argument is present include a binary exponent suffix. */ function toStringBinary(x, baseOut, sd, rm) { var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== void 0; if (isExp) { checkInt32(sd, 1, MAX_DIGITS); if (rm === void 0) rm = Ctor.rounding; else checkInt32(rm, 0, 8); } else { sd = Ctor.precision; rm = Ctor.rounding; } if (!x.isFinite()) { str = nonFiniteToString(x); } else { str = finiteToString(x); i = str.indexOf('.'); // Use exponential notation according to `toExpPos` and `toExpNeg`? No, but if required: // maxBinaryExponent = floor((decimalExponent + 1) * log[2](10)) // minBinaryExponent = floor(decimalExponent * log[2](10)) // log[2](10) = 3.321928094887362347870319429489390175864 if (isExp) { base = 2; if (baseOut == 16) { sd = sd * 4 - 3; } else if (baseOut == 8) { sd = sd * 3 - 2; } } else { base = baseOut; } // Convert the number as an integer then divide the result by its base raised to a power such // that the fraction part will be restored. // Non-integer. if (i >= 0) { str = str.replace('.', ''); y = new Ctor(1); y.e = str.length - i; y.d = convertBase(finiteToString(y), 10, base); y.e = y.d.length; } xd = convertBase(str, 10, base); e = len = xd.length; // Remove trailing zeros. for (; xd[--len] == 0;) xd.pop(); if (!xd[0]) { str = isExp ? '0p+0' : '0'; } else { if (i < 0) { e--; } else { x = new Ctor(x); x.d = xd; x.e = e; x = divide(x, y, sd, rm, 0, base); xd = x.d; e = x.e; roundUp = inexact; } // The rounding digit, i.e. the digit after the digit that may be rounded up. i = xd[sd]; k = base / 2; roundUp = roundUp || xd[sd + 1] !== void 0; roundUp = rm < 4 ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2)) : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 || rm === (x.s < 0 ? 8 : 7)); xd.length = sd; if (roundUp) { // Rounding up may mean the previous digit has to be rounded up and so on. for (; ++xd[--sd] > base - 1;) { xd[sd] = 0; if (!sd) { ++e; xd.unshift(1); } } } // Determine trailing zeros. for (len = xd.length; !xd[len - 1]; --len); // E.g. [4, 11, 15] becomes 4bf. for (i = 0, str = ''; i < len; i++) str += NUMERALS.charAt(xd[i]); // Add binary exponent suffix? if (isExp) { if (len > 1) { if (baseOut == 16 || baseOut == 8) { i = baseOut == 16 ? 4 : 3; for (--len; len % i; len++) str += '0'; xd = convertBase(str, base, baseOut); for (len = xd.length; !xd[len - 1]; --len); // xd[0] will always be be 1 for (i = 1, str = '1.'; i < len; i++) str += NUMERALS.charAt(xd[i]); } else { str = str.charAt(0) + '.' + str.slice(1); } } str = str + (e < 0 ? 'p' : 'p+') + e; } else if (e < 0) { for (; ++e;) str = '0' + str; str = '0.' + str; } else { if (++e > len) for (e -= len; e-- ;) str += '0'; else if (e < len) str = str.slice(0, e) + '.' + str.slice(e); } } str = (baseOut == 16 ? '0x' : baseOut == 2 ? '0b' : baseOut == 8 ? '0o' : '') + str; } return x.s < 0 ? '-' + str : str; } // Does not strip trailing zeros. function truncate(arr, len) { if (arr.length > len) { arr.length = len; return true; } } // Decimal methods /* * abs * acos * acosh * add * asin * asinh * atan * atanh * atan2 * cbrt * ceil * clone * config * cos * cosh * div * exp * floor * hypot * ln * log * log2 * log10 * max * min * mod * mul * pow * random * round * set * sign * sin * sinh * sqrt * sub * tan * tanh * trunc */ /* * Return a new Decimal whose value is the absolute value of `x`. * * x {number|string|Decimal} * */ function abs(x) { return new this(x).abs(); } /* * Return a new Decimal whose value is the arccosine in radians of `x`. * * x {number|string|Decimal} * */ function acos(x) { return new this(x).acos(); } /* * Return a new Decimal whose value is the inverse of the hyperbolic cosine of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function acosh(x) { return new this(x).acosh(); } /* * Return a new Decimal whose value is the sum of `x` and `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * * x {number|string|Decimal} * y {number|string|Decimal} * */ function add(x, y) { return new this(x).plus(y); } /* * Return a new Decimal whose value is the arcsine in radians of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} * */ function asin(x) { return new this(x).asin(); } /* * Return a new Decimal whose value is the inverse of the hyperbolic sine of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function asinh(x) { return new this(x).asinh(); } /* * Return a new Decimal whose value is the arctangent in radians of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} * */ function atan(x) { return new this(x).atan(); } /* * Return a new Decimal whose value is the inverse of the hyperbolic tangent of `x`, rounded to * `precision` significant digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function atanh(x) { return new this(x).atanh(); } /* * Return a new Decimal whose value is the arctangent in radians of `y/x` in the range -pi to pi * (inclusive), rounded to `precision` significant digits using rounding mode `rounding`. * * Domain: [-Infinity, Infinity] * Range: [-pi, pi] * * y {number|string|Decimal} The y-coordinate. * x {number|string|Decimal} The x-coordinate. * * atan2(±0, -0) = ±pi * atan2(±0, +0) = ±0 * atan2(±0, -x) = ±pi for x > 0 * atan2(±0, x) = ±0 for x > 0 * atan2(-y, ±0) = -pi/2 for y > 0 * atan2(y, ±0) = pi/2 for y > 0 * atan2(±y, -Infinity) = ±pi for finite y > 0 * atan2(±y, +Infinity) = ±0 for finite y > 0 * atan2(±Infinity, x) = ±pi/2 for finite x * atan2(±Infinity, -Infinity) = ±3*pi/4 * atan2(±Infinity, +Infinity) = ±pi/4 * atan2(NaN, x) = NaN * atan2(y, NaN) = NaN * */ function atan2(y, x) { y = new this(y); x = new this(x); var r, pr = this.precision, rm = this.rounding, wpr = pr + 4; // Either NaN if (!y.s || !x.s) { r = new this(NaN); // Both ±Infinity } else if (!y.d && !x.d) { r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75); r.s = y.s; // x is ±Infinity or y is ±0 } else if (!x.d || y.isZero()) { r = x.s < 0 ? getPi(this, pr, rm) : new this(0); r.s = y.s; // y is ±Infinity or x is ±0 } else if (!y.d || x.isZero()) { r = getPi(this, wpr, 1).times(0.5); r.s = y.s; // Both non-zero and finite } else if (x.s < 0) { this.precision = wpr; this.rounding = 1; r = this.atan(divide(y, x, wpr, 1)); x = getPi(this, wpr, 1); this.precision = pr; this.rounding = rm; r = y.s < 0 ? r.minus(x) : r.plus(x); } else { r = this.atan(divide(y, x, wpr, 1)); } return r; } /* * Return a new Decimal whose value is the cube root of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * * x {number|string|Decimal} * */ function cbrt(x) { return new this(x).cbrt(); } /* * Return a new Decimal whose value is `x` rounded to an integer using `ROUND_CEIL`. * * x {number|string|Decimal} * */ function ceil(x) { return finalise(x = new this(x), x.e + 1, 2); } /* * Configure global settings for a Decimal constructor. * * `obj` is an object with one or more of the following properties, * * precision {number} * rounding {number} * toExpNeg {number} * toExpPos {number} * maxE {number} * minE {number} * modulo {number} * crypto {boolean|number} * * E.g. Decimal.config({ precision: 20, rounding: 4 }) * */ function config(obj) { if (!obj || typeof obj !== 'object') throw Error(decimalError + 'Object expected'); var i, p, v, ps = [ 'precision', 1, MAX_DIGITS, 'rounding', 0, 8, 'toExpNeg', -EXP_LIMIT, 0, 'toExpPos', 0, EXP_LIMIT, 'maxE', 0, EXP_LIMIT, 'minE', -EXP_LIMIT, 0, 'modulo', 0, 9 ]; for (i = 0; i < ps.length; i += 3) { if ((v = obj[p = ps[i]]) !== void 0) { if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v; else throw Error(invalidArgument + p + ': ' + v); } } if ((v = obj[p = 'crypto']) !== void 0) { if (v === true || v === false || v === 0 || v === 1) { if (v) { if (typeof crypto != 'undefined' && crypto && (crypto.getRandomValues || crypto.randomBytes)) { this[p] = true; } else { throw Error(cryptoUnavailable); } } else { this[p] = false; } } else { throw Error(invalidArgument + p + ': ' + v); } } return this; } /* * Return a new Decimal whose value is the cosine of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function cos(x) { return new this(x).cos(); } /* * Return a new Decimal whose value is the hyperbolic cosine of `x`, rounded to precision * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function cosh(x) { return new this(x).cosh(); } /* * Create and return a Decimal constructor with the same configuration properties as this Decimal * constructor. * */ function clone(obj) { var i, p, ps; /* * The Decimal constructor and exported function. * Return a new Decimal instance. * * v {number|string|Decimal} A numeric value. * */ function Decimal(v) { var e, i, t, x = this; // Decimal called without new. if (!(x instanceof Decimal)) return new Decimal(v); // Retain a reference to this Decimal constructor, and shadow Decimal.prototype.constructor // which points to Object. x.constructor = Decimal; // Duplicate. if (v instanceof Decimal) { x.s = v.s; x.e = v.e; x.d = (v = v.d) ? v.slice() : v; return; } t = typeof v; if (t === 'number') { if (v === 0) { x.s = 1 / v < 0 ? -1 : 1; x.e = 0; x.d = [0]; return; } if (v < 0) { v = -v; x.s = -1; } else { x.s = 1; } // Fast path for small integers. if (v === ~~v && v < 1e7) { for (e = 0, i = v; i >= 10; i /= 10) e++; x.e = e; x.d = [v]; return; // Infinity, NaN. } else if (v * 0 !== 0) { if (!v) x.s = NaN; x.e = NaN; x.d = null; return; } return parseDecimal(x, v.toString()); } else if (t !== 'string') { throw Error(invalidArgument + v); } // Minus sign? if (v.charCodeAt(0) === 45) { v = v.slice(1); x.s = -1; } else { x.s = 1; } return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v); } Decimal.prototype = P; Decimal.ROUND_UP = 0; Decimal.ROUND_DOWN = 1; Decimal.ROUND_CEIL = 2; Decimal.ROUND_FLOOR = 3; Decimal.ROUND_HALF_UP = 4; Decimal.ROUND_HALF_DOWN = 5; Decimal.ROUND_HALF_EVEN = 6; Decimal.ROUND_HALF_CEIL = 7; Decimal.ROUND_HALF_FLOOR = 8; Decimal.EUCLID = 9; Decimal.config = Decimal.set = config; Decimal.clone = clone; Decimal.abs = abs; Decimal.acos = acos; Decimal.acosh = acosh; // ES6 Decimal.add = add; Decimal.asin = asin; Decimal.asinh = asinh; // ES6 Decimal.atan = atan; Decimal.atanh = atanh; // ES6 Decimal.atan2 = atan2; Decimal.cbrt = cbrt; // ES6 Decimal.ceil = ceil; Decimal.cos = cos; Decimal.cosh = cosh; // ES6 Decimal.div = div; Decimal.exp = exp; Decimal.floor = floor; Decimal.hypot = hypot; // ES6 Decimal.ln = ln; Decimal.log = log; Decimal.log10 = log10; // ES6 Decimal.log2 = log2; // ES6 Decimal.max = max; Decimal.min = min; Decimal.mod = mod; Decimal.mul = mul; Decimal.pow = pow; Decimal.random = random; Decimal.round = round; Decimal.sign = sign; // ES6 Decimal.sin = sin; Decimal.sinh = sinh; // ES6 Decimal.sqrt = sqrt; Decimal.sub = sub; Decimal.tan = tan; Decimal.tanh = tanh; // ES6 Decimal.trunc = trunc; // ES6 if (obj === void 0) obj = {}; if (obj) { ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto']; for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p]; } Decimal.config(obj); return Decimal; } /* * Return a new Decimal whose value is `x` divided by `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * * x {number|string|Decimal} * y {number|string|Decimal} * */ function div(x, y) { return new this(x).div(y); } /* * Return a new Decimal whose value is the natural exponential of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} The power to which to raise the base of the natural log. * */ function exp(x) { return new this(x).exp(); } /* * Return a new Decimal whose value is `x` round to an integer using `ROUND_FLOOR`. * * x {number|string|Decimal} * */ function floor(x) { return finalise(x = new this(x), x.e + 1, 3); } /* * Return a new Decimal whose value is the square root of the sum of the squares of the arguments, * rounded to `precision` significant digits using rounding mode `rounding`. * * hypot(a, b, ...) = sqrt(a^2 + b^2 + ...) * */ function hypot() { var i, n, t = new this(0); external = false; for (i = 0; i < arguments.length;) { n = new this(arguments[i++]); if (!n.d) { if (n.s) { external = true; return new this(1 / 0); } t = n; } else if (t.d) { t = t.plus(n.times(n)); } } external = true; return t.sqrt(); } /* * Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} * */ function ln(x) { return new this(x).ln(); } /* * Return a new Decimal whose value is the log of `x` to the base `y`, or to base 10 if no base * is specified, rounded to `precision` significant digits using rounding mode `rounding`. * * log[y](x) * * x {number|string|Decimal} The argument of the logarithm. * y {number|string|Decimal} The base of the logarithm. * */ function log(x, y) { return new this(x).log(y); } /* * Return a new Decimal whose value is the base 2 logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} * */ function log2(x) { return new this(x).log(2); } /* * Return a new Decimal whose value is the base 10 logarithm of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} * */ function log10(x) { return new this(x).log(10); } /* * Return a new Decimal whose value is the maximum of the arguments. * * arguments {number|string|Decimal} * */ function max() { return maxOrMin(this, arguments, 'lt'); } /* * Return a new Decimal whose value is the minimum of the arguments. * * arguments {number|string|Decimal} * */ function min() { return maxOrMin(this, arguments, 'gt'); } /* * Return a new Decimal whose value is `x` modulo `y`, rounded to `precision` significant digits * using rounding mode `rounding`. * * x {number|string|Decimal} * y {number|string|Decimal} * */ function mod(x, y) { return new this(x).mod(y); } /* * Return a new Decimal whose value is `x` multiplied by `y`, rounded to `precision` significant * digits using rounding mode `rounding`. * * x {number|string|Decimal} * y {number|string|Decimal} * */ function mul(x, y) { return new this(x).mul(y); } /* * Return a new Decimal whose value is `x` raised to the power `y`, rounded to precision * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} The base. * y {number|string|Decimal} The exponent. * */ function pow(x, y) { return new this(x).pow(y); } /* * Returns a new Decimal with a random value equal to or greater than 0 and less than 1, and with * `sd`, or `Decimal.precision` if `sd` is omitted, significant digits (or less if trailing zeros * are produced). * * [sd] {number} Significant digits. Integer, 0 to MAX_DIGITS inclusive. * */ function random(sd) { var d, e, k, n, i = 0, r = new this(1), rd = []; if (sd === void 0) sd = this.precision; else checkInt32(sd, 1, MAX_DIGITS); k = Math.ceil(sd / LOG_BASE); if (!this.crypto) { for (; i < k;) rd[i++] = Math.random() * 1e7 | 0; // Browsers supporting crypto.getRandomValues. } else if (crypto.getRandomValues) { d = crypto.getRandomValues(new Uint32Array(k)); for (; i < k;) { n = d[i]; // 0 <= n < 4294967296 // Probability n >= 4.29e9, is 4967296 / 4294967296 = 0.00116 (1 in 865). if (n >= 4.29e9) { d[i] = crypto.getRandomValues(new Uint32Array(1))[0]; } else { // 0 <= n <= 4289999999 // 0 <= (n % 1e7) <= 9999999 rd[i++] = n % 1e7; } } // Node.js supporting crypto.randomBytes. } else if (crypto.randomBytes) { // buffer d = crypto.randomBytes(k *= 4); for (; i < k;) { // 0 <= n < 2147483648 n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 0x7f) << 24); // Probability n >= 2.14e9, is 7483648 / 2147483648 = 0.0035 (1 in 286). if (n >= 2.14e9) { crypto.randomBytes(4).copy(d, i); } else { // 0 <= n <= 2139999999 // 0 <= (n % 1e7) <= 9999999 rd.push(n % 1e7); i += 4; } } i = k / 4; } else { throw Error(cryptoUnavailable); } k = rd[--i]; sd %= LOG_BASE; // Convert trailing digits to zeros according to sd. if (k && sd) { n = mathpow(10, LOG_BASE - sd); rd[i] = (k / n | 0) * n; } // Remove trailing words which are zero. for (; rd[i] === 0; i--) rd.pop(); // Zero? if (i < 0) { e = 0; rd = [0]; } else { e = -1; // Remove leading words which are zero and adjust exponent accordingly. for (; rd[0] === 0; e -= LOG_BASE) rd.shift(); // Count the digits of the first word of rd to determine leading zeros. for (k = 1, n = rd[0]; n >= 10; n /= 10) k++; // Adjust the exponent for leading zeros of the first word of rd. if (k < LOG_BASE) e -= LOG_BASE - k; } r.e = e; r.d = rd; return r; } /* * Return a new Decimal whose value is `x` rounded to an integer using rounding mode `rounding`. * * To emulate `Math.round`, set rounding to 7 (ROUND_HALF_CEIL). * * x {number|string|Decimal} * */ function round(x) { return finalise(x = new this(x), x.e + 1, this.rounding); } /* * Return * 1 if x > 0, * -1 if x < 0, * 0 if x is 0, * -0 if x is -0, * NaN otherwise * */ function sign(x) { x = new this(x); return x.d ? (x.d[0] ? x.s : 0 * x.s) : x.s || NaN; } /* * Return a new Decimal whose value is the sine of `x`, rounded to `precision` significant digits * using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function sin(x) { return new this(x).sin(); } /* * Return a new Decimal whose value is the hyperbolic sine of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function sinh(x) { return new this(x).sinh(); } /* * Return a new Decimal whose value is the square root of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * * x {number|string|Decimal} * */ function sqrt(x) { return new this(x).sqrt(); } /* * Return a new Decimal whose value is `x` minus `y`, rounded to `precision` significant digits * using rounding mode `rounding`. * * x {number|string|Decimal} * y {number|string|Decimal} * */ function sub(x, y) { return new this(x).sub(y); } /* * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant * digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function tan(x) { return new this(x).tan(); } /* * Return a new Decimal whose value is the hyperbolic tangent of `x`, rounded to `precision` * significant digits using rounding mode `rounding`. * * x {number|string|Decimal} A value in radians. * */ function tanh(x) { return new this(x).tanh(); } /* * Return a new Decimal whose value is `x` truncated to an integer. * * x {number|string|Decimal} * */ function trunc(x) { return finalise(x = new this(x), x.e + 1, 1); } // Create and configure initial Decimal constructor. Decimal = clone(Decimal); // Create the internal constants from their string values. LN10 = new Decimal(LN10); PI = new Decimal(PI); // Export. // AMD. if (true) { !(__WEBPACK_AMD_DEFINE_RESULT__ = function () { return Decimal; }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); // Node and other environments that support module.exports. } else if (typeof module != 'undefined' && module.exports) { module.exports = Decimal.default = Decimal.Decimal = Decimal; // Browser. } else { if (!globalScope) { globalScope = typeof self != 'undefined' && self && self.self == self ? self : Function('return this')(); } noConflict = globalScope.Decimal; Decimal.noConflict = function () { globalScope.Decimal = noConflict; return Decimal; }; globalScope.Decimal = Decimal; } })(this); /***/ }), /* 506 */ /***/ (function(module, exports, __webpack_require__) { var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** * @license Fraction.js v4.0.0 09/09/2015 * http://www.xarg.org/2014/03/rational-numbers-in-javascript/ * * Copyright (c) 2015, Robert Eisele (robert@xarg.org) * Dual licensed under the MIT or GPL Version 2 licenses. **/ /** * * This class offers the possibility to calculate fractions. * You can pass a fraction in different formats. Either as array, as double, as string or as an integer. * * Array/Object form * [ 0 => , 1 => ] * [ n => , d => ] * * Integer form * - Single integer value * * Double form * - Single double value * * String form * 123.456 - a simple double * 123/456 - a string fraction * 123.'456' - a double with repeating decimal places * 123.(456) - synonym * 123.45'6' - a double with repeating last place * 123.45(6) - synonym * * Example: * * var f = new Fraction("9.4'31'"); * f.mul([-4, 3]).div(4.9); * */ (function(root) { "use strict"; // Maximum search depth for cyclic rational numbers. 2000 should be more than enough. // Example: 1/7 = 0.(142857) has 6 repeating decimal places. // If MAX_CYCLE_LEN gets reduced, long cycles will not be detected and toString() only gets the first 10 digits var MAX_CYCLE_LEN = 2000; // Parsed data to avoid calling "new" all the time var P = { "s": 1, "n": 0, "d": 1 }; function createError(name) { var errorConstructor = function() { var temp = Error.apply(this, arguments); temp.name = this.name = name; this.stack = temp.stack; this.message = temp.message; } var IntermediateInheritor = function() {}; IntermediateInheritor.prototype = Error.prototype; errorConstructor.prototype = new IntermediateInheritor(); return errorConstructor; } var DivisionByZero = Fraction['DivisionByZero'] = createError('DivisionByZero'); var InvalidParameter = Fraction['InvalidParameter'] = createError('InvalidParameter'); function assign(n, s) { if (isNaN(n = parseInt(n, 10))) { throwInvalidParam(); } return n * s; } function throwInvalidParam() { throw new InvalidParameter(); } var parse = function(p1, p2) { var n = 0, d = 1, s = 1; var v = 0, w = 0, x = 0, y = 1, z = 1; var A = 0, B = 1; var C = 1, D = 1; var N = 10000000; var M; if (p1 === undefined || p1 === null) { /* void */ } else if (p2 !== undefined) { n = p1; d = p2; s = n * d; } else switch (typeof p1) { case "object": { if ("d" in p1 && "n" in p1) { n = p1["n"]; d = p1["d"]; if ("s" in p1) n*= p1["s"]; } else if (0 in p1) { n = p1[0]; if (1 in p1) d = p1[1]; } else { throwInvalidParam(); } s = n * d; break; } case "number": { if (p1 < 0) { s = p1; p1 = -p1; } if (p1 % 1 === 0) { n = p1; } else if (p1 > 0) { // check for != 0, scale would become NaN (log(0)), which converges really slow if (p1 >= 1) { z = Math.pow(10, Math.floor(1 + Math.log(p1) / Math.LN10)); p1/= z; } // Using Farey Sequences // http://www.johndcook.com/blog/2010/10/20/best-rational-approximation/ while (B <= N && D <= N) { M = (A + C) / (B + D); if (p1 === M) { if (B + D <= N) { n = A + C; d = B + D; } else if (D > B) { n = C; d = D; } else { n = A; d = B; } break; } else { if (p1 > M) { A+= C; B+= D; } else { C+= A; D+= B; } if (B > N) { n = C; d = D; } else { n = A; d = B; } } } n*= z; } else if (isNaN(p1) || isNaN(p2)) { d = n = NaN; } break; } case "string": { B = p1.match(/\d+|./g); if (B[A] === '-') {// Check for minus sign at the beginning s = -1; A++; } else if (B[A] === '+') {// Check for plus sign at the beginning A++; } if (B.length === A + 1) { // Check if it's just a simple number "1234" w = assign(B[A++], s); } else if (B[A + 1] === '.' || B[A] === '.') { // Check if it's a decimal number if (B[A] !== '.') { // Handle 0.5 and .5 v = assign(B[A++], s); } A++; // Check for decimal places if (A + 1 === B.length || B[A + 1] === '(' && B[A + 3] === ')' || B[A + 1] === "'" && B[A + 3] === "'") { w = assign(B[A], s); y = Math.pow(10, B[A].length); A++; } // Check for repeating places if (B[A] === '(' && B[A + 2] === ')' || B[A] === "'" && B[A + 2] === "'") { x = assign(B[A + 1], s); z = Math.pow(10, B[A + 1].length) - 1; A+= 3; } } else if (B[A + 1] === '/' || B[A + 1] === ':') { // Check for a simple fraction "123/456" or "123:456" w = assign(B[A], s); y = assign(B[A + 2], 1); A+= 3; } else if (B[A + 3] === '/' && B[A + 1] === ' ') { // Check for a complex fraction "123 1/2" v = assign(B[A], s); w = assign(B[A + 2], s); y = assign(B[A + 4], 1); A+= 5; } if (B.length <= A) { // Check for more tokens on the stack d = y * z; s = /* void */ n = x + d * v + z * w; break; } /* Fall through on error */ } default: throwInvalidParam(); } if (d === 0) { throw new DivisionByZero(); } P["s"] = s < 0 ? -1 : 1; P["n"] = Math.abs(n); P["d"] = Math.abs(d); }; var modpow = function(b, e, m) { for (var r = 1; e > 0; b = (b * b) % m, e >>= 1) { if (e & 1) { r = (r * b) % m; } } return r; }; var cycleLen = function(n, d) { for (; d % 2 === 0; d/= 2) {} for (; d % 5 === 0; d/= 5) {} if (d === 1) // Catch non-cyclic numbers return 0; // If we would like to compute really large numbers quicker, we could make use of Fermat's little theorem: // 10^(d-1) % d == 1 // However, we don't need such large numbers and MAX_CYCLE_LEN should be the capstone, // as we want to translate the numbers to strings. var rem = 10 % d; for (var t = 1; rem !== 1; t++) { rem = rem * 10 % d; if (t > MAX_CYCLE_LEN) return 0; // Returning 0 here means that we don't print it as a cyclic number. It's likely that the answer is `d-1` } return t; }; var cycleStart = function(n, d, len) { var rem1 = 1; var rem2 = modpow(10, len, d); for (var t = 0; t < 300; t++) { // s < ~log10(Number.MAX_VALUE) // Solve 10^s == 10^(s+t) (mod d) if (rem1 === rem2) return t; rem1 = rem1 * 10 % d; rem2 = rem2 * 10 % d; } return 0; }; var gcd = function(a, b) { if (!a) return b; if (!b) return a; while (1) { a%= b; if (!a) return b; b%= a; if (!b) return a; } }; /** * Module constructor * * @constructor * @param {number|Fraction} a * @param {number=} b */ function Fraction(a, b) { if (!(this instanceof Fraction)) { return new Fraction(a, b); } parse(a, b); if (Fraction['REDUCE']) { a = gcd(P["d"], P["n"]); // Abuse a } else { a = 1; } this["s"] = P["s"]; this["n"] = P["n"] / a; this["d"] = P["d"] / a; } /** * Boolean global variable to be able to disable automatic reduction of the fraction * */ Fraction['REDUCE'] = 1; Fraction.prototype = { "s": 1, "n": 0, "d": 1, /** * Calculates the absolute value * * Ex: new Fraction(-4).abs() => 4 **/ "abs": function() { return new Fraction(this["n"], this["d"]); }, /** * Inverts the sign of the current fraction * * Ex: new Fraction(-4).neg() => 4 **/ "neg": function() { return new Fraction(-this["s"] * this["n"], this["d"]); }, /** * Adds two rational numbers * * Ex: new Fraction({n: 2, d: 3}).add("14.9") => 467 / 30 **/ "add": function(a, b) { parse(a, b); return new Fraction( this["s"] * this["n"] * P["d"] + P["s"] * this["d"] * P["n"], this["d"] * P["d"] ); }, /** * Subtracts two rational numbers * * Ex: new Fraction({n: 2, d: 3}).add("14.9") => -427 / 30 **/ "sub": function(a, b) { parse(a, b); return new Fraction( this["s"] * this["n"] * P["d"] - P["s"] * this["d"] * P["n"], this["d"] * P["d"] ); }, /** * Multiplies two rational numbers * * Ex: new Fraction("-17.(345)").mul(3) => 5776 / 111 **/ "mul": function(a, b) { parse(a, b); return new Fraction( this["s"] * P["s"] * this["n"] * P["n"], this["d"] * P["d"] ); }, /** * Divides two rational numbers * * Ex: new Fraction("-17.(345)").inverse().div(3) **/ "div": function(a, b) { parse(a, b); return new Fraction( this["s"] * P["s"] * this["n"] * P["d"], this["d"] * P["n"] ); }, /** * Clones the actual object * * Ex: new Fraction("-17.(345)").clone() **/ "clone": function() { return new Fraction(this); }, /** * Calculates the modulo of two rational numbers - a more precise fmod * * Ex: new Fraction('4.(3)').mod([7, 8]) => (13/3) % (7/8) = (5/6) **/ "mod": function(a, b) { if (isNaN(this['n']) || isNaN(this['d'])) { return new Fraction(NaN); } if (a === undefined) { return new Fraction(this["s"] * this["n"] % this["d"], 1); } parse(a, b); if (0 === P["n"] && 0 === this["d"]) { Fraction(0, 0); // Throw DivisionByZero } /* * First silly attempt, kinda slow * return that["sub"]({ "n": num["n"] * Math.floor((this.n / this.d) / (num.n / num.d)), "d": num["d"], "s": this["s"] });*/ /* * New attempt: a1 / b1 = a2 / b2 * q + r * => b2 * a1 = a2 * b1 * q + b1 * b2 * r * => (b2 * a1 % a2 * b1) / (b1 * b2) */ return new Fraction( (this["s"] * P["d"] * this["n"]) % (P["n"] * this["d"]), P["d"] * this["d"] ); }, /** * Calculates the fractional gcd of two rational numbers * * Ex: new Fraction(5,8).gcd(3,7) => 1/56 */ "gcd": function(a, b) { parse(a, b); // gcd(a / b, c / d) = gcd(a, c) / lcm(b, d) return new Fraction(gcd(P["n"], this["n"]), P["d"] * this["d"] / gcd(P["d"], this["d"])); }, /** * Calculates the fractional lcm of two rational numbers * * Ex: new Fraction(5,8).lcm(3,7) => 15 */ "lcm": function(a, b) { parse(a, b); // lcm(a / b, c / d) = lcm(a, c) / gcd(b, d) if (P["n"] === 0 && this["n"] === 0) { return new Fraction; } return new Fraction(P["n"] * this["n"] / gcd(P["n"], this["n"]), gcd(P["d"], this["d"])); }, /** * Calculates the ceil of a rational number * * Ex: new Fraction('4.(3)').ceil() => (5 / 1) **/ "ceil": function(places) { places = Math.pow(10, places || 0); if (isNaN(this["n"]) || isNaN(this["d"])) { return new Fraction(NaN); } return new Fraction(Math.ceil(places * this["s"] * this["n"] / this["d"]), places); }, /** * Calculates the floor of a rational number * * Ex: new Fraction('4.(3)').floor() => (4 / 1) **/ "floor": function(places) { places = Math.pow(10, places || 0); if (isNaN(this["n"]) || isNaN(this["d"])) { return new Fraction(NaN); } return new Fraction(Math.floor(places * this["s"] * this["n"] / this["d"]), places); }, /** * Rounds a rational numbers * * Ex: new Fraction('4.(3)').round() => (4 / 1) **/ "round": function(places) { places = Math.pow(10, places || 0); if (isNaN(this["n"]) || isNaN(this["d"])) { return new Fraction(NaN); } return new Fraction(Math.round(places * this["s"] * this["n"] / this["d"]), places); }, /** * Gets the inverse of the fraction, means numerator and denumerator are exchanged * * Ex: new Fraction([-3, 4]).inverse() => -4 / 3 **/ "inverse": function() { return new Fraction(this["s"] * this["d"], this["n"]); }, /** * Calculates the fraction to some integer exponent * * Ex: new Fraction(-1,2).pow(-3) => -8 */ "pow": function(m) { if (m < 0) { return new Fraction(Math.pow(this['s'] * this["d"],-m), Math.pow(this["n"],-m)); } else { return new Fraction(Math.pow(this['s'] * this["n"], m), Math.pow(this["d"], m)); } }, /** * Check if two rational numbers are the same * * Ex: new Fraction(19.6).equals([98, 5]); **/ "equals": function(a, b) { parse(a, b); return this["s"] * this["n"] * P["d"] === P["s"] * P["n"] * this["d"]; // Same as compare() === 0 }, /** * Check if two rational numbers are the same * * Ex: new Fraction(19.6).equals([98, 5]); **/ "compare": function(a, b) { parse(a, b); var t = (this["s"] * this["n"] * P["d"] - P["s"] * P["n"] * this["d"]); return (0 < t) - (t < 0); }, /** * Check if two rational numbers are divisible * * Ex: new Fraction(19.6).divisible(1.5); */ "divisible": function(a, b) { parse(a, b); return !(!(P["n"] * this["d"]) || ((this["n"] * P["d"]) % (P["n"] * this["d"]))); }, /** * Returns a decimal representation of the fraction * * Ex: new Fraction("100.'91823'").valueOf() => 100.91823918239183 **/ 'valueOf': function() { return this["s"] * this["n"] / this["d"]; }, /** * Returns a string-fraction representation of a Fraction object * * Ex: new Fraction("1.'3'").toFraction() => "4 1/3" **/ 'toFraction': function(excludeWhole) { var whole, str = ""; var n = this["n"]; var d = this["d"]; if (this["s"] < 0) { str+= '-'; } if (d === 1) { str+= n; } else { if (excludeWhole && (whole = Math.floor(n / d)) > 0) { str+= whole; str+= " "; n%= d; } str+= n; str+= '/'; str+= d; } return str; }, /** * Returns a latex representation of a Fraction object * * Ex: new Fraction("1.'3'").toLatex() => "\frac{4}{3}" **/ 'toLatex': function(excludeWhole) { var whole, str = ""; var n = this["n"]; var d = this["d"]; if (this["s"] < 0) { str+= '-'; } if (d === 1) { str+= n; } else { if (excludeWhole && (whole = Math.floor(n / d)) > 0) { str+= whole; n%= d; } str+= "\\frac{"; str+= n; str+= '}{'; str+= d; str+= '}'; } return str; }, /** * Returns an array of continued fraction elements * * Ex: new Fraction("7/8").toContinued() => [0,1,7] */ 'toContinued': function() { var t; var a = this['n']; var b = this['d']; var res = []; do { res.push(Math.floor(a / b)); t = a % b; a = b; b = t; } while (a !== 1); return res; }, /** * Creates a string representation of a fraction with all digits * * Ex: new Fraction("100.'91823'").toString() => "100.(91823)" **/ 'toString': function() { var g; var N = this["n"]; var D = this["d"]; if (isNaN(N) || isNaN(D)) { return "NaN"; } if (!Fraction['REDUCE']) { g = gcd(N, D); N/= g; D/= g; } var p = String(N).split(""); // Numerator chars var t = 0; // Tmp var var ret = [~this["s"] ? "" : "-", "", ""]; // Return array, [0] is zero sign, [1] before comma, [2] after var zeros = ""; // Collection variable for zeros var cycLen = cycleLen(N, D); // Cycle length var cycOff = cycleStart(N, D, cycLen); // Cycle start var j = -1; var n = 1; // str index // rough estimate to fill zeros var length = 15 + cycLen + cycOff + p.length; // 15 = decimal places when no repitation for (var i = 0; i < length; i++, t*= 10) { if (i < p.length) { t+= Number(p[i]); } else { n = 2; j++; // Start now => after comma } if (cycLen > 0) { // If we have a repeating part if (j === cycOff) { ret[n]+= zeros + "("; zeros = ""; } else if (j === cycLen + cycOff) { ret[n]+= zeros + ")"; break; } } if (t >= D) { ret[n]+= zeros + ((t / D) | 0); // Flush zeros, Add current digit zeros = ""; t = t % D; } else if (n > 1) { // Add zeros to the zero buffer zeros+= "0"; } else if (ret[n]) { // If before comma, add zero only if already something was added ret[n]+= "0"; } } // If it's empty, it's a leading zero only ret[0]+= ret[1] || "0"; // If there is something after the comma, add the comma sign if (ret[2]) { return ret[0] + "." + ret[2]; } return ret[0]; } }; if (true) { !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function() { return Fraction; }.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); } else if (typeof exports === "object") { module["exports"] = Fraction; } else { root['Fraction'] = Fraction; } })(this); /***/ }), /* 507 */ /***/ (function(module, exports) { function E () { // Keep this empty so it's easier to inherit from // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) } E.prototype = { on: function (name, callback, ctx) { var e = this.e || (this.e = {}); (e[name] || (e[name] = [])).push({ fn: callback, ctx: ctx }); return this; }, once: function (name, callback, ctx) { var self = this; function listener () { self.off(name, listener); callback.apply(ctx, arguments); }; listener._ = callback return this.on(name, listener, ctx); }, emit: function (name) { var data = [].slice.call(arguments, 1); var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); var i = 0; var len = evtArr.length; for (i; i < len; i++) { evtArr[i].fn.apply(evtArr[i].ctx, data); } return this; }, off: function (name, callback) { var e = this.e || (this.e = {}); var evts = e[name]; var liveEvents = []; if (evts && callback) { for (var i = 0, len = evts.length; i < len; i++) { if (evts[i].fn !== callback && evts[i].fn._ !== callback) liveEvents.push(evts[i]); } } // Remove event from queue to prevent memory leak // Suggested by https://github.com/lazd // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 (liveEvents.length) ? e[name] = liveEvents : delete e[name]; return this; } }; module.exports = E; /***/ }), /* 508 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** * typed-function * * Type checking for JavaScript functions * * https://github.com/josdejong/typed-function */ (function (root, factory) { if (true) { // AMD. Register as an anonymous module. !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory), __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); } else if (typeof exports === 'object') { // OldNode. Does not work with strict CommonJS, but // only CommonJS-like environments that support module.exports, // like OldNode. module.exports = factory(); } else { // Browser globals (root is window) root.typed = factory(); } }(this, function () { // factory function to create a new instance of typed-function // TODO: allow passing configuration, types, tests via the factory function function create() { /** * Get a type test function for a specific data type * @param {string} name Name of a data type like 'number' or 'string' * @returns {Function(obj: *) : boolean} Returns a type testing function. * Throws an error for an unknown type. */ function getTypeTest(name) { var test; for (var i = 0; i < typed.types.length; i++) { var entry = typed.types[i]; if (entry.name === name) { test = entry.test; break; } } if (!test) { var hint; for (i = 0; i < typed.types.length; i++) { entry = typed.types[i]; if (entry.name.toLowerCase() == name.toLowerCase()) { hint = entry.name; break; } } throw new Error('Unknown type "' + name + '"' + (hint ? ('. Did you mean "' + hint + '"?') : '')); } return test; } /** * Retrieve the function name from a set of functions, and check * whether the name of all functions match (if given) * @param {Array.} fns */ function getName (fns) { var name = ''; for (var i = 0; i < fns.length; i++) { var fn = fns[i]; // merge function name when this is a typed function if (fn.signatures && fn.name != '') { if (name == '') { name = fn.name; } else if (name != fn.name) { var err = new Error('Function names do not match (expected: ' + name + ', actual: ' + fn.name + ')'); err.data = { actual: fn.name, expected: name }; throw err; } } } return name; } /** * Create an ArgumentsError. Creates messages like: * * Unexpected type of argument (expected: ..., actual: ..., index: ...) * Too few arguments (expected: ..., index: ...) * Too many arguments (expected: ..., actual: ...) * * @param {String} fn Function name * @param {number} argCount Number of arguments * @param {Number} index Current argument index * @param {*} actual Current argument * @param {string} [expected] An optional, comma separated string with * expected types on given index * @extends Error */ function createError(fn, argCount, index, actual, expected) { var actualType = getTypeOf(actual); var _expected = expected ? expected.split(',') : null; var _fn = (fn || 'unnamed'); var anyType = _expected && contains(_expected, 'any'); var message; var data = { fn: fn, index: index, actual: actual, expected: _expected }; if (_expected) { if (argCount > index && !anyType) { // unexpected type message = 'Unexpected type of argument in function ' + _fn + ' (expected: ' + _expected.join(' or ') + ', actual: ' + actualType + ', index: ' + index + ')'; } else { // too few arguments message = 'Too few arguments in function ' + _fn + ' (expected: ' + _expected.join(' or ') + ', index: ' + index + ')'; } } else { // too many arguments message = 'Too many arguments in function ' + _fn + ' (expected: ' + index + ', actual: ' + argCount + ')' } var err = new TypeError(message); err.data = data; return err; } /** * Collection with function references (local shortcuts to functions) * @constructor * @param {string} [name='refs'] Optional name for the refs, used to generate * JavaScript code */ function Refs(name) { this.name = name || 'refs'; this.categories = {}; } /** * Add a function reference. * @param {Function} fn * @param {string} [category='fn'] A function category, like 'fn' or 'signature' * @returns {string} Returns the function name, for example 'fn0' or 'signature2' */ Refs.prototype.add = function (fn, category) { var cat = category || 'fn'; if (!this.categories[cat]) this.categories[cat] = []; var index = this.categories[cat].indexOf(fn); if (index == -1) { index = this.categories[cat].length; this.categories[cat].push(fn); } return cat + index; }; /** * Create code lines for all function references * @returns {string} Returns the code containing all function references */ Refs.prototype.toCode = function () { var code = []; var path = this.name + '.categories'; var categories = this.categories; for (var cat in categories) { if (categories.hasOwnProperty(cat)) { var category = categories[cat]; for (var i = 0; i < category.length; i++) { code.push('var ' + cat + i + ' = ' + path + '[\'' + cat + '\'][' + i + '];'); } } } return code.join('\n'); }; /** * A function parameter * @param {string | string[] | Param} types A parameter type like 'string', * 'number | boolean' * @param {boolean} [varArgs=false] Variable arguments if true * @constructor */ function Param(types, varArgs) { // parse the types, can be a string with types separated by pipe characters | if (typeof types === 'string') { // parse variable arguments operator (ellipses '...number') var _types = types.trim(); var _varArgs = _types.substr(0, 3) === '...'; if (_varArgs) { _types = _types.substr(3); } if (_types === '') { this.types = ['any']; } else { this.types = _types.split('|'); for (var i = 0; i < this.types.length; i++) { this.types[i] = this.types[i].trim(); } } } else if (Array.isArray(types)) { this.types = types; } else if (types instanceof Param) { return types.clone(); } else { throw new Error('String or Array expected'); } // can hold a type to which to convert when handling this parameter this.conversions = []; // TODO: implement better API for conversions, be able to add conversions via constructor (support a new type Object?) // variable arguments this.varArgs = _varArgs || varArgs || false; // check for any type arguments this.anyType = this.types.indexOf('any') !== -1; } /** * Order Params * any type ('any') will be ordered last, and object as second last (as other * types may be an object as well, like Array). * * @param {Param} a * @param {Param} b * @returns {number} Returns 1 if a > b, -1 if a < b, and else 0. */ Param.compare = function (a, b) { // TODO: simplify parameter comparison, it's a mess if (a.anyType) return 1; if (b.anyType) return -1; if (contains(a.types, 'Object')) return 1; if (contains(b.types, 'Object')) return -1; if (a.hasConversions()) { if (b.hasConversions()) { var i, ac, bc; for (i = 0; i < a.conversions.length; i++) { if (a.conversions[i] !== undefined) { ac = a.conversions[i]; break; } } for (i = 0; i < b.conversions.length; i++) { if (b.conversions[i] !== undefined) { bc = b.conversions[i]; break; } } return typed.conversions.indexOf(ac) - typed.conversions.indexOf(bc); } else { return 1; } } else { if (b.hasConversions()) { return -1; } else { // both params have no conversions var ai, bi; for (i = 0; i < typed.types.length; i++) { if (typed.types[i].name === a.types[0]) { ai = i; break; } } for (i = 0; i < typed.types.length; i++) { if (typed.types[i].name === b.types[0]) { bi = i; break; } } return ai - bi; } } }; /** * Test whether this parameters types overlap an other parameters types. * Will not match ['any'] with ['number'] * @param {Param} other * @return {boolean} Returns true when there are overlapping types */ Param.prototype.overlapping = function (other) { for (var i = 0; i < this.types.length; i++) { if (contains(other.types, this.types[i])) { return true; } } return false; }; /** * Test whether this parameters types matches an other parameters types. * When any of the two parameters contains `any`, true is returned * @param {Param} other * @return {boolean} Returns true when there are matching types */ Param.prototype.matches = function (other) { return this.anyType || other.anyType || this.overlapping(other); }; /** * Create a clone of this param * @returns {Param} Returns a cloned version of this param */ Param.prototype.clone = function () { var param = new Param(this.types.slice(), this.varArgs); param.conversions = this.conversions.slice(); return param; }; /** * Test whether this parameter contains conversions * @returns {boolean} Returns true if the parameter contains one or * multiple conversions. */ Param.prototype.hasConversions = function () { return this.conversions.length > 0; }; /** * Tests whether this parameters contains any of the provided types * @param {Object} types A Map with types, like {'number': true} * @returns {boolean} Returns true when the parameter contains any * of the provided types */ Param.prototype.contains = function (types) { for (var i = 0; i < this.types.length; i++) { if (types[this.types[i]]) { return true; } } return false; }; /** * Return a string representation of this params types, like 'string' or * 'number | boolean' or '...number' * @param {boolean} [toConversion] If true, the returned types string * contains the types where the parameter * will convert to. If false (default) * the "from" types are returned * @returns {string} */ Param.prototype.toString = function (toConversion) { var types = []; var keys = {}; for (var i = 0; i < this.types.length; i++) { var conversion = this.conversions[i]; var type = toConversion && conversion ? conversion.to : this.types[i]; if (!(type in keys)) { keys[type] = true; types.push(type); } } return (this.varArgs ? '...' : '') + types.join('|'); }; /** * A function signature * @param {string | string[] | Param[]} params * Array with the type(s) of each parameter, * or a comma separated string with types * @param {Function} fn The actual function * @constructor */ function Signature(params, fn) { var _params; if (typeof params === 'string') { _params = (params !== '') ? params.split(',') : []; } else if (Array.isArray(params)) { _params = params; } else { throw new Error('string or Array expected'); } this.params = new Array(_params.length); this.anyType = false; this.varArgs = false; for (var i = 0; i < _params.length; i++) { var param = new Param(_params[i]); this.params[i] = param; if (param.anyType) { this.anyType = true; } if (i === _params.length - 1) { // the last argument this.varArgs = param.varArgs; } else { // non-last argument if (param.varArgs) { throw new SyntaxError('Unexpected variable arguments operator "..."'); } } } this.fn = fn; } /** * Create a clone of this signature * @returns {Signature} Returns a cloned version of this signature */ Signature.prototype.clone = function () { return new Signature(this.params.slice(), this.fn); }; /** * Expand a signature: split params with union types in separate signatures * For example split a Signature "string | number" into two signatures. * @return {Signature[]} Returns an array with signatures (at least one) */ Signature.prototype.expand = function () { var signatures = []; function recurse(signature, path) { if (path.length < signature.params.length) { var i, newParam, conversion; var param = signature.params[path.length]; if (param.varArgs) { // a variable argument. do not split the types in the parameter newParam = param.clone(); // add conversions to the parameter // recurse for all conversions for (i = 0; i < typed.conversions.length; i++) { conversion = typed.conversions[i]; if (!contains(param.types, conversion.from) && contains(param.types, conversion.to)) { var j = newParam.types.length; newParam.types[j] = conversion.from; newParam.conversions[j] = conversion; } } recurse(signature, path.concat(newParam)); } else { // split each type in the parameter for (i = 0; i < param.types.length; i++) { recurse(signature, path.concat(new Param(param.types[i]))); } // recurse for all conversions for (i = 0; i < typed.conversions.length; i++) { conversion = typed.conversions[i]; if (!contains(param.types, conversion.from) && contains(param.types, conversion.to)) { newParam = new Param(conversion.from); newParam.conversions[0] = conversion; recurse(signature, path.concat(newParam)); } } } } else { signatures.push(new Signature(path, signature.fn)); } } recurse(this, []); return signatures; }; /** * Compare two signatures. * * When two params are equal and contain conversions, they will be sorted * by lowest index of the first conversions. * * @param {Signature} a * @param {Signature} b * @returns {number} Returns 1 if a > b, -1 if a < b, and else 0. */ Signature.compare = function (a, b) { if (a.params.length > b.params.length) return 1; if (a.params.length < b.params.length) return -1; // count the number of conversions var i; var len = a.params.length; // a and b have equal amount of params var ac = 0; var bc = 0; for (i = 0; i < len; i++) { if (a.params[i].hasConversions()) ac++; if (b.params[i].hasConversions()) bc++; } if (ac > bc) return 1; if (ac < bc) return -1; // compare the order per parameter for (i = 0; i < a.params.length; i++) { var cmp = Param.compare(a.params[i], b.params[i]); if (cmp !== 0) { return cmp; } } return 0; }; /** * Test whether any of the signatures parameters has conversions * @return {boolean} Returns true when any of the parameters contains * conversions. */ Signature.prototype.hasConversions = function () { for (var i = 0; i < this.params.length; i++) { if (this.params[i].hasConversions()) { return true; } } return false; }; /** * Test whether this signature should be ignored. * Checks whether any of the parameters contains a type listed in * typed.ignore * @return {boolean} Returns true when the signature should be ignored */ Signature.prototype.ignore = function () { // create a map with ignored types var types = {}; for (var i = 0; i < typed.ignore.length; i++) { types[typed.ignore[i]] = true; } // test whether any of the parameters contains this type for (i = 0; i < this.params.length; i++) { if (this.params[i].contains(types)) { return true; } } return false; }; /** * Test whether the path of this signature matches a given path. * @param {Param[]} params */ Signature.prototype.paramsStartWith = function (params) { if (params.length === 0) { return true; } var aLast = last(this.params); var bLast = last(params); for (var i = 0; i < params.length; i++) { var a = this.params[i] || (aLast.varArgs ? aLast: null); var b = params[i] || (bLast.varArgs ? bLast: null); if (!a || !b || !a.matches(b)) { return false; } } return true; }; /** * Generate the code to invoke this signature * @param {Refs} refs * @param {string} prefix * @returns {string} Returns code */ Signature.prototype.toCode = function (refs, prefix) { var code = []; var args = new Array(this.params.length); for (var i = 0; i < this.params.length; i++) { var param = this.params[i]; var conversion = param.conversions[0]; if (param.varArgs) { args[i] = 'varArgs'; } else if (conversion) { args[i] = refs.add(conversion.convert, 'convert') + '(arg' + i + ')'; } else { args[i] = 'arg' + i; } } var ref = this.fn ? refs.add(this.fn, 'signature') : undefined; if (ref) { return prefix + 'return ' + ref + '(' + args.join(', ') + '); // signature: ' + this.params.join(', '); } return code.join('\n'); }; /** * Return a string representation of the signature * @returns {string} */ Signature.prototype.toString = function () { return this.params.join(', '); }; /** * A group of signatures with the same parameter on given index * @param {Param[]} path * @param {Signature} [signature] * @param {Node[]} childs * @param {boolean} [fallThrough=false] * @constructor */ function Node(path, signature, childs, fallThrough) { this.path = path || []; this.param = path[path.length - 1] || null; this.signature = signature || null; this.childs = childs || []; this.fallThrough = fallThrough || false; } /** * Generate code for this group of signatures * @param {Refs} refs * @param {string} prefix * @returns {string} Returns the code as string */ Node.prototype.toCode = function (refs, prefix) { // TODO: split this function in multiple functions, it's too large var code = []; if (this.param) { var index = this.path.length - 1; var conversion = this.param.conversions[0]; var comment = '// type: ' + (conversion ? (conversion.from + ' (convert to ' + conversion.to + ')') : this.param); // non-root node (path is non-empty) if (this.param.varArgs) { if (this.param.anyType) { // variable arguments with any type code.push(prefix + 'if (arguments.length > ' + index + ') {'); code.push(prefix + ' var varArgs = [];'); code.push(prefix + ' for (var i = ' + index + '; i < arguments.length; i++) {'); code.push(prefix + ' varArgs.push(arguments[i]);'); code.push(prefix + ' }'); code.push(this.signature.toCode(refs, prefix + ' ')); code.push(prefix + '}'); } else { // variable arguments with a fixed type var getTests = function (types, arg) { var tests = []; for (var i = 0; i < types.length; i++) { tests[i] = refs.add(getTypeTest(types[i]), 'test') + '(' + arg + ')'; } return tests.join(' || '); }.bind(this); var allTypes = this.param.types; var exactTypes = []; for (var i = 0; i < allTypes.length; i++) { if (this.param.conversions[i] === undefined) { exactTypes.push(allTypes[i]); } } code.push(prefix + 'if (' + getTests(allTypes, 'arg' + index) + ') { ' + comment); code.push(prefix + ' var varArgs = [arg' + index + '];'); code.push(prefix + ' for (var i = ' + (index + 1) + '; i < arguments.length; i++) {'); code.push(prefix + ' if (' + getTests(exactTypes, 'arguments[i]') + ') {'); code.push(prefix + ' varArgs.push(arguments[i]);'); for (var i = 0; i < allTypes.length; i++) { var conversion_i = this.param.conversions[i]; if (conversion_i) { var test = refs.add(getTypeTest(allTypes[i]), 'test'); var convert = refs.add(conversion_i.convert, 'convert'); code.push(prefix + ' }'); code.push(prefix + ' else if (' + test + '(arguments[i])) {'); code.push(prefix + ' varArgs.push(' + convert + '(arguments[i]));'); } } code.push(prefix + ' } else {'); code.push(prefix + ' throw createError(name, arguments.length, i, arguments[i], \'' + exactTypes.join(',') + '\');'); code.push(prefix + ' }'); code.push(prefix + ' }'); code.push(this.signature.toCode(refs, prefix + ' ')); code.push(prefix + '}'); } } else { if (this.param.anyType) { // any type code.push(prefix + '// type: any'); code.push(this._innerCode(refs, prefix)); } else { // regular type var type = this.param.types[0]; var test = type !== 'any' ? refs.add(getTypeTest(type), 'test') : null; code.push(prefix + 'if (' + test + '(arg' + index + ')) { ' + comment); code.push(this._innerCode(refs, prefix + ' ')); code.push(prefix + '}'); } } } else { // root node (path is empty) code.push(this._innerCode(refs, prefix)); } return code.join('\n'); }; /** * Generate inner code for this group of signatures. * This is a helper function of Node.prototype.toCode * @param {Refs} refs * @param {string} prefix * @returns {string} Returns the inner code as string * @private */ Node.prototype._innerCode = function (refs, prefix) { var code = []; var i; if (this.signature) { code.push(prefix + 'if (arguments.length === ' + this.path.length + ') {'); code.push(this.signature.toCode(refs, prefix + ' ')); code.push(prefix + '}'); } for (i = 0; i < this.childs.length; i++) { code.push(this.childs[i].toCode(refs, prefix)); } // TODO: shouldn't the this.param.anyType check be redundant if (!this.fallThrough || (this.param && this.param.anyType)) { var exceptions = this._exceptions(refs, prefix); if (exceptions) { code.push(exceptions); } } return code.join('\n'); }; /** * Generate code to throw exceptions * @param {Refs} refs * @param {string} prefix * @returns {string} Returns the inner code as string * @private */ Node.prototype._exceptions = function (refs, prefix) { var index = this.path.length; if (this.childs.length === 0) { // TODO: can this condition be simplified? (we have a fall-through here) return [ prefix + 'if (arguments.length > ' + index + ') {', prefix + ' throw createError(name, arguments.length, ' + index + ', arguments[' + index + ']);', prefix + '}' ].join('\n'); } else { var keys = {}; var types = []; for (var i = 0; i < this.childs.length; i++) { var node = this.childs[i]; if (node.param) { for (var j = 0; j < node.param.types.length; j++) { var type = node.param.types[j]; if (!(type in keys) && !node.param.conversions[j]) { keys[type] = true; types.push(type); } } } } return prefix + 'throw createError(name, arguments.length, ' + index + ', arguments[' + index + '], \'' + types.join(',') + '\');'; } }; /** * Split all raw signatures into an array with expanded Signatures * @param {Object.} rawSignatures * @return {Signature[]} Returns an array with expanded signatures */ function parseSignatures(rawSignatures) { // FIXME: need to have deterministic ordering of signatures, do not create via object var signature; var keys = {}; var signatures = []; var i; for (var types in rawSignatures) { if (rawSignatures.hasOwnProperty(types)) { var fn = rawSignatures[types]; signature = new Signature(types, fn); if (signature.ignore()) { continue; } var expanded = signature.expand(); for (i = 0; i < expanded.length; i++) { var signature_i = expanded[i]; var key = signature_i.toString(); var existing = keys[key]; if (!existing) { keys[key] = signature_i; } else { var cmp = Signature.compare(signature_i, existing); if (cmp < 0) { // override if sorted first keys[key] = signature_i; } else if (cmp === 0) { throw new Error('Signature "' + key + '" is defined twice'); } // else: just ignore } } } } // convert from map to array for (key in keys) { if (keys.hasOwnProperty(key)) { signatures.push(keys[key]); } } // order the signatures signatures.sort(function (a, b) { return Signature.compare(a, b); }); // filter redundant conversions from signatures with varArgs // TODO: simplify this loop or move it to a separate function for (i = 0; i < signatures.length; i++) { signature = signatures[i]; if (signature.varArgs) { var index = signature.params.length - 1; var param = signature.params[index]; var t = 0; while (t < param.types.length) { if (param.conversions[t]) { var type = param.types[t]; for (var j = 0; j < signatures.length; j++) { var other = signatures[j]; var p = other.params[index]; if (other !== signature && p && contains(p.types, type) && !p.conversions[index]) { // this (conversion) type already exists, remove it param.types.splice(t, 1); param.conversions.splice(t, 1); t--; break; } } } t++; } } } return signatures; } /** * Filter all any type signatures * @param {Signature[]} signatures * @return {Signature[]} Returns only any type signatures */ function filterAnyTypeSignatures (signatures) { var filtered = []; for (var i = 0; i < signatures.length; i++) { if (signatures[i].anyType) { filtered.push(signatures[i]); } } return filtered; } /** * create a map with normalized signatures as key and the function as value * @param {Signature[]} signatures An array with split signatures * @return {Object.} Returns a map with normalized * signatures as key, and the function * as value. */ function mapSignatures(signatures) { var normalized = {}; for (var i = 0; i < signatures.length; i++) { var signature = signatures[i]; if (signature.fn && !signature.hasConversions()) { var params = signature.params.join(','); normalized[params] = signature.fn; } } return normalized; } /** * Parse signatures recursively in a node tree. * @param {Signature[]} signatures Array with expanded signatures * @param {Param[]} path Traversed path of parameter types * @param {Signature[]} anys * @return {Node} Returns a node tree */ function parseTree(signatures, path, anys) { var i, signature; var index = path.length; var nodeSignature; var filtered = []; for (i = 0; i < signatures.length; i++) { signature = signatures[i]; // filter the first signature with the correct number of params if (signature.params.length === index && !nodeSignature) { nodeSignature = signature; } if (signature.params[index] != undefined) { filtered.push(signature); } } // sort the filtered signatures by param filtered.sort(function (a, b) { return Param.compare(a.params[index], b.params[index]); }); // recurse over the signatures var entries = []; for (i = 0; i < filtered.length; i++) { signature = filtered[i]; // group signatures with the same param at current index var param = signature.params[index]; // TODO: replace the next filter loop var existing = entries.filter(function (entry) { return entry.param.overlapping(param); })[0]; //var existing; //for (var j = 0; j < entries.length; j++) { // if (entries[j].param.overlapping(param)) { // existing = entries[j]; // break; // } //} if (existing) { if (existing.param.varArgs) { throw new Error('Conflicting types "' + existing.param + '" and "' + param + '"'); } existing.signatures.push(signature); } else { entries.push({ param: param, signatures: [signature] }); } } // find all any type signature that can still match our current path var matchingAnys = []; for (i = 0; i < anys.length; i++) { if (anys[i].paramsStartWith(path)) { matchingAnys.push(anys[i]); } } // see if there are any type signatures that don't match any of the // signatures that we have in our tree, i.e. we have alternative // matching signature(s) outside of our current tree and we should // fall through to them instead of throwing an exception var fallThrough = false; for (i = 0; i < matchingAnys.length; i++) { if (!contains(signatures, matchingAnys[i])) { fallThrough = true; break; } } // parse the childs var childs = new Array(entries.length); for (i = 0; i < entries.length; i++) { var entry = entries[i]; childs[i] = parseTree(entry.signatures, path.concat(entry.param), matchingAnys) } return new Node(path, nodeSignature, childs, fallThrough); } /** * Generate an array like ['arg0', 'arg1', 'arg2'] * @param {number} count Number of arguments to generate * @returns {Array} Returns an array with argument names */ function getArgs(count) { // create an array with all argument names var args = []; for (var i = 0; i < count; i++) { args[i] = 'arg' + i; } return args; } /** * Compose a function from sub-functions each handling a single type signature. * Signatures: * typed(signature: string, fn: function) * typed(name: string, signature: string, fn: function) * typed(signatures: Object.) * typed(name: string, signatures: Object.) * * @param {string | null} name * @param {Object.} signatures * @return {Function} Returns the typed function * @private */ function _typed(name, signatures) { var refs = new Refs(); // parse signatures, expand them var _signatures = parseSignatures(signatures); if (_signatures.length == 0) { throw new Error('No signatures provided'); } // filter all any type signatures var anys = filterAnyTypeSignatures(_signatures); // parse signatures into a node tree var node = parseTree(_signatures, [], anys); //var util = require('util'); //console.log('ROOT'); //console.log(util.inspect(node, { depth: null })); // generate code for the typed function var code = []; var _name = name || ''; var _args = getArgs(maxParams(_signatures)); code.push('function ' + _name + '(' + _args.join(', ') + ') {'); code.push(' "use strict";'); code.push(' var name = \'' + _name + '\';'); code.push(node.toCode(refs, ' ', false)); code.push('}'); // generate body for the factory function var body = [ refs.toCode(), 'return ' + code.join('\n') ].join('\n'); // evaluate the JavaScript code and attach function references var factory = (new Function(refs.name, 'createError', body)); var fn = factory(refs, createError); //console.log('FN\n' + fn.toString()); // TODO: cleanup // attach the signatures with sub-functions to the constructed function fn.signatures = mapSignatures(_signatures); return fn; } /** * Calculate the maximum number of parameters in givens signatures * @param {Signature[]} signatures * @returns {number} The maximum number of parameters */ function maxParams(signatures) { var max = 0; for (var i = 0; i < signatures.length; i++) { var len = signatures[i].params.length; if (len > max) { max = len; } } return max; } /** * Get the type of a value * @param {*} x * @returns {string} Returns a string with the type of value */ function getTypeOf(x) { var obj; for (var i = 0; i < typed.types.length; i++) { var entry = typed.types[i]; if (entry.name === 'Object') { // Array and Date are also Object, so test for Object afterwards obj = entry; } else { if (entry.test(x)) return entry.name; } } // at last, test whether an object if (obj && obj.test(x)) return obj.name; return 'unknown'; } /** * Test whether an array contains some item * @param {Array} array * @param {*} item * @return {boolean} Returns true if array contains item, false if not. */ function contains(array, item) { return array.indexOf(item) !== -1; } /** * Returns the last item in the array * @param {Array} array * @return {*} item */ function last (array) { return array[array.length - 1]; } // data type tests var types = [ { name: 'number', test: function (x) { return typeof x === 'number' } }, { name: 'string', test: function (x) { return typeof x === 'string' } }, { name: 'boolean', test: function (x) { return typeof x === 'boolean' } }, { name: 'Function', test: function (x) { return typeof x === 'function'} }, { name: 'Array', test: Array.isArray }, { name: 'Date', test: function (x) { return x instanceof Date } }, { name: 'RegExp', test: function (x) { return x instanceof RegExp } }, { name: 'Object', test: function (x) { return typeof x === 'object' } }, { name: 'null', test: function (x) { return x === null } }, { name: 'undefined', test: function (x) { return x === undefined } } ]; // configuration var config = {}; // type conversions. Order is important var conversions = []; // types to be ignored var ignore = []; // temporary object for holding types and conversions, for constructing // the `typed` function itself // TODO: find a more elegant solution for this var typed = { config: config, types: types, conversions: conversions, ignore: ignore }; /** * Construct the typed function itself with various signatures * * Signatures: * * typed(signatures: Object.) * typed(name: string, signatures: Object.) */ typed = _typed('typed', { 'Object': function (signatures) { var fns = []; for (var signature in signatures) { if (signatures.hasOwnProperty(signature)) { fns.push(signatures[signature]); } } var name = getName(fns); return _typed(name, signatures); }, 'string, Object': _typed, // TODO: add a signature 'Array.' '...Function': function (fns) { var err; var name = getName(fns); var signatures = {}; for (var i = 0; i < fns.length; i++) { var fn = fns[i]; // test whether this is a typed-function if (!(typeof fn.signatures === 'object')) { err = new TypeError('Function is no typed-function (index: ' + i + ')'); err.data = {index: i}; throw err; } // merge the signatures for (var signature in fn.signatures) { if (fn.signatures.hasOwnProperty(signature)) { if (signatures.hasOwnProperty(signature)) { if (fn.signatures[signature] !== signatures[signature]) { err = new Error('Signature "' + signature + '" is defined twice'); err.data = {signature: signature}; throw err; } // else: both signatures point to the same function, that's fine } else { signatures[signature] = fn.signatures[signature]; } } } } return _typed(name, signatures); } }); /** * Find a specific signature from a (composed) typed function, for * example: * * typed.find(fn, ['number', 'string']) * typed.find(fn, 'number, string') * * Function find only only works for exact matches. * * @param {Function} fn A typed-function * @param {string | string[]} signature Signature to be found, can be * an array or a comma separated string. * @return {Function} Returns the matching signature, or * throws an errror when no signature * is found. */ function find (fn, signature) { if (!fn.signatures) { throw new TypeError('Function is no typed-function'); } // normalize input var arr; if (typeof signature === 'string') { arr = signature.split(','); for (var i = 0; i < arr.length; i++) { arr[i] = arr[i].trim(); } } else if (Array.isArray(signature)) { arr = signature; } else { throw new TypeError('String array or a comma separated string expected'); } var str = arr.join(','); // find an exact match var match = fn.signatures[str]; if (match) { return match; } // TODO: extend find to match non-exact signatures throw new TypeError('Signature not found (signature: ' + (fn.name || 'unnamed') + '(' + arr.join(', ') + '))'); } /** * Convert a given value to another data type. * @param {*} value * @param {string} type */ function convert (value, type) { var from = getTypeOf(value); // check conversion is needed if (type === from) { return value; } for (var i = 0; i < typed.conversions.length; i++) { var conversion = typed.conversions[i]; if (conversion.from === from && conversion.to === type) { return conversion.convert(value); } } throw new Error('Cannot convert from ' + from + ' to ' + type); } // attach types and conversions to the final `typed` function typed.config = config; typed.types = types; typed.conversions = conversions; typed.ignore = ignore; typed.create = create; typed.find = find; typed.convert = convert; // add a type typed.addType = function (type) { if (!type || typeof type.name !== 'string' || typeof type.test !== 'function') { throw new TypeError('Object with properties {name: string, test: function} expected'); } typed.types.push(type); }; // add a conversion typed.addConversion = function (conversion) { if (!conversion || typeof conversion.from !== 'string' || typeof conversion.to !== 'string' || typeof conversion.convert !== 'function') { throw new TypeError('Object with properties {from: string, to: string, convert: function} expected'); } typed.conversions.push(conversion); }; return typed; } return create(); })); /***/ }), /* 509 */ /***/ (function(module, exports, __webpack_require__) { var core = __webpack_require__(133); /** * math.js factory function. Creates a new instance of math.js * * @param {Object} [config] Available configuration options: * {number} epsilon * Minimum relative difference between two * compared values, used by all comparison functions. * {string} matrix * A string 'matrix' (default) or 'array'. * {string} number * A string 'number' (default), 'bignumber', or * 'fraction' * {number} precision * The number of significant digits for BigNumbers. * Not applicable for Numbers. * {boolean} predictable * Predictable output type of functions. When true, * output type depends only on the input types. When * false (default), output type can vary depending * on input values. For example `math.sqrt(-4)` * returns `complex('2i')` when predictable is false, and * returns `NaN` when true. */ function create (config) { // create a new math.js instance var math = core.create(config); math.create = create; // import data types, functions, constants, expression parser, etc. math['import'](__webpack_require__(134)); return math; } // return a new instance of math.js module.exports = create(); /***/ }) /******/ ]); });