simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

cube.js (1606B)


      1 import { factory } from '../../utils/factory.js';
      2 import { deepMap } from '../../utils/collection.js';
      3 import { cubeNumber } from '../../plain/number/index.js';
      4 var name = 'cube';
      5 var dependencies = ['typed'];
      6 export var createCube = /* #__PURE__ */factory(name, dependencies, _ref => {
      7   var {
      8     typed
      9   } = _ref;
     10 
     11   /**
     12    * Compute the cube of a value, `x * x * x`.
     13    * For matrices, the function is evaluated element wise.
     14    *
     15    * Syntax:
     16    *
     17    *    math.cube(x)
     18    *
     19    * Examples:
     20    *
     21    *    math.cube(2)            // returns number 8
     22    *    math.pow(2, 3)          // returns number 8
     23    *    math.cube(4)            // returns number 64
     24    *    4 * 4 * 4               // returns number 64
     25    *
     26    *    math.cube([1, 2, 3, 4]) // returns Array [1, 8, 27, 64]
     27    *
     28    * See also:
     29    *
     30    *    multiply, square, pow, cbrt
     31    *
     32    * @param  {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x  Number for which to calculate the cube
     33    * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} Cube of x
     34    */
     35   return typed(name, {
     36     number: cubeNumber,
     37     Complex: function Complex(x) {
     38       return x.mul(x).mul(x); // Is faster than pow(x, 3)
     39     },
     40     BigNumber: function BigNumber(x) {
     41       return x.times(x).times(x);
     42     },
     43     Fraction: function Fraction(x) {
     44       return x.pow(3); // Is faster than mul()mul()mul()
     45     },
     46     'Array | Matrix': function ArrayMatrix(x) {
     47       // deep map collection, skip zeros since cube(0) = 0
     48       return deepMap(x, this, true);
     49     },
     50     Unit: function Unit(x) {
     51       return x.pow(3);
     52     }
     53   });
     54 });