1 |
|
2 |
|
3 | const deepMap = require('../../utils/collection/deepMap')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | /**
|
7 | * Compute the cube of a value, `x * x * x`.
|
8 | * For matrices, the function is evaluated element wise.
|
9 | *
|
10 | * Syntax:
|
11 | *
|
12 | * math.cube(x)
|
13 | *
|
14 | * Examples:
|
15 | *
|
16 | * math.cube(2) // returns number 8
|
17 | * math.pow(2, 3) // returns number 8
|
18 | * math.cube(4) // returns number 64
|
19 | * 4 * 4 * 4 // returns number 64
|
20 | *
|
21 | * math.cube([1, 2, 3, 4]) // returns Array [1, 8, 27, 64]
|
22 | *
|
23 | * See also:
|
24 | *
|
25 | * multiply, square, pow, cbrt
|
26 | *
|
27 | * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x Number for which to calculate the cube
|
28 | * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} Cube of x
|
29 | */
|
30 | const cube = typed('cube', {
|
31 | 'number': function (x) {
|
32 | return x * x * x
|
33 | },
|
34 |
|
35 | 'Complex': function (x) {
|
36 | return x.mul(x).mul(x) // Is faster than pow(x, 3)
|
37 | },
|
38 |
|
39 | 'BigNumber': function (x) {
|
40 | return x.times(x).times(x)
|
41 | },
|
42 |
|
43 | 'Fraction': function (x) {
|
44 | return x.pow(3) // Is faster than mul()mul()mul()
|
45 | },
|
46 |
|
47 | 'Array | Matrix': function (x) {
|
48 | // deep map collection, skip zeros since cube(0) = 0
|
49 | return deepMap(x, cube, true)
|
50 | },
|
51 |
|
52 | 'Unit': function (x) {
|
53 | return x.pow(3)
|
54 | }
|
55 | })
|
56 |
|
57 | cube.toTex = { 1: `\\left(\${args[0]}\\right)^3` }
|
58 |
|
59 | return cube
|
60 | }
|
61 |
|
62 | exports.name = 'cube'
|
63 | exports.factory = factory
|