UNPKG

1.58 kBJavaScriptView Raw
1import { factory } from '../../utils/factory.js';
2import { deepMap } from '../../utils/collection.js';
3import { squareNumber } from '../../plain/number/index.js';
4var name = 'square';
5var dependencies = ['typed'];
6export var createSquare = /* #__PURE__ */factory(name, dependencies, (_ref) => {
7 var {
8 typed
9 } = _ref;
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 return 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, this, true);
51 },
52 Unit: function Unit(x) {
53 return x.pow(2);
54 }
55 });
56});
\No newline at end of file