hypot.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createHypot = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _array = require("../../utils/array.js");
  8. var _is = require("../../utils/is.js");
  9. var name = 'hypot';
  10. var dependencies = ['typed', 'abs', 'addScalar', 'divideScalar', 'multiplyScalar', 'sqrt', 'smaller', 'isPositive'];
  11. var createHypot = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed,
  13. abs = _ref.abs,
  14. addScalar = _ref.addScalar,
  15. divideScalar = _ref.divideScalar,
  16. multiplyScalar = _ref.multiplyScalar,
  17. sqrt = _ref.sqrt,
  18. smaller = _ref.smaller,
  19. isPositive = _ref.isPositive;
  20. /**
  21. * Calculate the hypotenusa of a list with values. The hypotenusa is defined as:
  22. *
  23. * hypot(a, b, c, ...) = sqrt(a^2 + b^2 + c^2 + ...)
  24. *
  25. * For matrix input, the hypotenusa is calculated for all values in the matrix.
  26. *
  27. * Syntax:
  28. *
  29. * math.hypot(a, b, ...)
  30. * math.hypot([a, b, c, ...])
  31. *
  32. * Examples:
  33. *
  34. * math.hypot(3, 4) // 5
  35. * math.hypot(3, 4, 5) // 7.0710678118654755
  36. * math.hypot([3, 4, 5]) // 7.0710678118654755
  37. * math.hypot(-2) // 2
  38. *
  39. * See also:
  40. *
  41. * abs, norm
  42. *
  43. * @param {... number | BigNumber | Array | Matrix} args A list with numeric values or an Array or Matrix.
  44. * Matrix and Array input is flattened and returns a
  45. * single number for the whole matrix.
  46. * @return {number | BigNumber} Returns the hypothenusa of the input values.
  47. */
  48. return typed(name, {
  49. '... number | BigNumber': _hypot,
  50. Array: _hypot,
  51. Matrix: function Matrix(M) {
  52. return _hypot((0, _array.flatten)(M.toArray()));
  53. }
  54. });
  55. /**
  56. * Calculate the hypotenusa for an Array with values
  57. * @param {Array.<number | BigNumber>} args
  58. * @return {number | BigNumber} Returns the result
  59. * @private
  60. */
  61. function _hypot(args) {
  62. // code based on `hypot` from es6-shim:
  63. // https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1619-L1633
  64. var result = 0;
  65. var largest = 0;
  66. for (var i = 0; i < args.length; i++) {
  67. if ((0, _is.isComplex)(args[i])) {
  68. throw new TypeError('Unexpected type of argument to hypot');
  69. }
  70. var value = abs(args[i]);
  71. if (smaller(largest, value)) {
  72. result = multiplyScalar(result, multiplyScalar(divideScalar(largest, value), divideScalar(largest, value)));
  73. result = addScalar(result, 1);
  74. largest = value;
  75. } else {
  76. result = addScalar(result, isPositive(value) ? multiplyScalar(divideScalar(value, largest), divideScalar(value, largest)) : value);
  77. }
  78. }
  79. return multiplyScalar(largest, sqrt(result));
  80. }
  81. });
  82. exports.createHypot = createHypot;