square.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { factory } from '../../utils/factory.js';
  2. import { squareNumber } from '../../plain/number/index.js';
  3. var name = 'square';
  4. var dependencies = ['typed'];
  5. export var createSquare = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed
  8. } = _ref;
  9. /**
  10. * Compute the square of a value, `x * x`.
  11. * To avoid confusion with multiplying a square matrix by itself,
  12. * this function does not apply to matrices. If you wish to square
  13. * every element of a matrix, see the examples.
  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.map([1, 2, 3, 4], math.square) // returns Array [1, 4, 9, 16]
  27. *
  28. * See also:
  29. *
  30. * multiply, cube, sqrt, pow
  31. *
  32. * @param {number | BigNumber | Fraction | Complex | Unit} x
  33. * Number for which to calculate the square
  34. * @return {number | BigNumber | Fraction | Complex | 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. Unit: function Unit(x) {
  49. return x.pow(2);
  50. }
  51. });
  52. });