UNPKG

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