abs.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { factory } from '../../utils/factory.js';
  2. import { deepMap } from '../../utils/collection.js';
  3. import { absNumber } from '../../plain/number/index.js';
  4. var name = 'abs';
  5. var dependencies = ['typed'];
  6. export var createAbs = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. typed
  9. } = _ref;
  10. /**
  11. * Calculate the absolute value of a number. For matrices, the function is
  12. * evaluated element wise.
  13. *
  14. * Syntax:
  15. *
  16. * math.abs(x)
  17. *
  18. * Examples:
  19. *
  20. * math.abs(3.5) // returns number 3.5
  21. * math.abs(-4.2) // returns number 4.2
  22. *
  23. * math.abs([3, -5, -1, 0, 2]) // returns Array [3, 5, 1, 0, 2]
  24. *
  25. * See also:
  26. *
  27. * sign
  28. *
  29. * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x
  30. * A number or matrix for which to get the absolute value
  31. * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}
  32. * Absolute value of `x`
  33. */
  34. return typed(name, {
  35. number: absNumber,
  36. 'Complex | BigNumber | Fraction | Unit': x => x.abs(),
  37. // deep map collection, skip zeros since abs(0) = 0
  38. 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true))
  39. });
  40. });