unaryMinus.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createUnaryMinus = 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 = 'unaryMinus';
  10. var dependencies = ['typed'];
  11. var createUnaryMinus = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed;
  13. /**
  14. * Inverse the sign of a value, apply a unary minus operation.
  15. *
  16. * For matrices, the function is evaluated element wise. Boolean values and
  17. * strings will be converted to a number. For complex numbers, both real and
  18. * complex value are inverted.
  19. *
  20. * Syntax:
  21. *
  22. * math.unaryMinus(x)
  23. *
  24. * Examples:
  25. *
  26. * math.unaryMinus(3.5) // returns -3.5
  27. * math.unaryMinus(-4.2) // returns 4.2
  28. *
  29. * See also:
  30. *
  31. * add, subtract, unaryPlus
  32. *
  33. * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Number to be inverted.
  34. * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Returns the value with inverted sign.
  35. */
  36. return typed(name, {
  37. number: _index.unaryMinusNumber,
  38. 'Complex | BigNumber | Fraction': function ComplexBigNumberFraction(x) {
  39. return x.neg();
  40. },
  41. Unit: typed.referToSelf(function (self) {
  42. return function (x) {
  43. var res = x.clone();
  44. res.value = typed.find(self, res.valueType())(x.value);
  45. return res;
  46. };
  47. }),
  48. // deep map collection, skip zeros since unaryMinus(0) = 0
  49. 'Array | Matrix': typed.referToSelf(function (self) {
  50. return function (x) {
  51. return (0, _collection.deepMap)(x, self, true);
  52. };
  53. })
  54. // TODO: add support for string
  55. });
  56. });
  57. exports.createUnaryMinus = createUnaryMinus;