forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare.js
More file actions
59 lines (52 loc) · 1.48 KB
/
square.js
File metadata and controls
59 lines (52 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { factory } from '../../utils/factory.js'
import { deepMap } from '../../utils/collection.js'
import { squareNumber } from '../../plain/number/index.js'
const name = 'square'
const dependencies = ['typed']
export const createSquare = /* #__PURE__ */ factory(name, dependencies, ({ 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
*/
return typed(name, {
number: squareNumber,
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, this, true)
},
Unit: function (x) {
return x.pow(2)
}
})
})