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