abs.js 1.5 KB

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