unaryMinus.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { factory } from '../../utils/factory.js';
  2. import { deepMap } from '../../utils/collection.js';
  3. import { unaryMinusNumber } from '../../plain/number/index.js';
  4. var name = 'unaryMinus';
  5. var dependencies = ['typed'];
  6. export var createUnaryMinus = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. typed
  9. } = _ref;
  10. /**
  11. * Inverse the sign of a value, apply a unary minus operation.
  12. *
  13. * For matrices, the function is evaluated element wise. Boolean values and
  14. * strings will be converted to a number. For complex numbers, both real and
  15. * complex value are inverted.
  16. *
  17. * Syntax:
  18. *
  19. * math.unaryMinus(x)
  20. *
  21. * Examples:
  22. *
  23. * math.unaryMinus(3.5) // returns -3.5
  24. * math.unaryMinus(-4.2) // returns 4.2
  25. *
  26. * See also:
  27. *
  28. * add, subtract, unaryPlus
  29. *
  30. * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Number to be inverted.
  31. * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Returns the value with inverted sign.
  32. */
  33. return typed(name, {
  34. number: unaryMinusNumber,
  35. 'Complex | BigNumber | Fraction': x => x.neg(),
  36. Unit: typed.referToSelf(self => x => {
  37. var res = x.clone();
  38. res.value = typed.find(self, res.valueType())(x.value);
  39. return res;
  40. }),
  41. // deep map collection, skip zeros since unaryMinus(0) = 0
  42. 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self, true))
  43. // TODO: add support for string
  44. });
  45. });