log1p.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createLog1p = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _collection = require("../../utils/collection.js");
  8. var _number = require("../../utils/number.js");
  9. var name = 'log1p';
  10. var dependencies = ['typed', 'config', 'divideScalar', 'log', 'Complex'];
  11. var createLog1p = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed,
  13. config = _ref.config,
  14. divideScalar = _ref.divideScalar,
  15. log = _ref.log,
  16. Complex = _ref.Complex;
  17. /**
  18. * Calculate the logarithm of a `value+1`.
  19. *
  20. * For matrices, the function is evaluated element wise.
  21. *
  22. * Syntax:
  23. *
  24. * math.log1p(x)
  25. * math.log1p(x, base)
  26. *
  27. * Examples:
  28. *
  29. * math.log1p(2.5) // returns 1.252762968495368
  30. * math.exp(math.log1p(1.4)) // returns 2.4
  31. *
  32. * math.pow(10, 4) // returns 10000
  33. * math.log1p(9999, 10) // returns 4
  34. * math.log1p(9999) / math.log(10) // returns 4
  35. *
  36. * See also:
  37. *
  38. * exp, log, log2, log10
  39. *
  40. * @param {number | BigNumber | Complex | Array | Matrix} x
  41. * Value for which to calculate the logarithm of `x+1`.
  42. * @param {number | BigNumber | Complex} [base=e]
  43. * Optional base for the logarithm. If not provided, the natural
  44. * logarithm of `x+1` is calculated.
  45. * @return {number | BigNumber | Complex | Array | Matrix}
  46. * Returns the logarithm of `x+1`
  47. */
  48. return typed(name, {
  49. number: function number(x) {
  50. if (x >= -1 || config.predictable) {
  51. return (0, _number.log1p)(x);
  52. } else {
  53. // negative value -> complex value computation
  54. return _log1pComplex(new Complex(x, 0));
  55. }
  56. },
  57. Complex: _log1pComplex,
  58. BigNumber: function BigNumber(x) {
  59. var y = x.plus(1);
  60. if (!y.isNegative() || config.predictable) {
  61. return y.ln();
  62. } else {
  63. // downgrade to number, return Complex valued result
  64. return _log1pComplex(new Complex(x.toNumber(), 0));
  65. }
  66. },
  67. 'Array | Matrix': typed.referToSelf(function (self) {
  68. return function (x) {
  69. return (0, _collection.deepMap)(x, self);
  70. };
  71. }),
  72. 'any, any': typed.referToSelf(function (self) {
  73. return function (x, base) {
  74. // calculate logarithm for a specified base, log1p(x, base)
  75. return divideScalar(self(x), log(base));
  76. };
  77. })
  78. });
  79. /**
  80. * Calculate the natural logarithm of a complex number + 1
  81. * @param {Complex} x
  82. * @returns {Complex}
  83. * @private
  84. */
  85. function _log1pComplex(x) {
  86. var xRe1p = x.re + 1;
  87. return new Complex(Math.log(Math.sqrt(xRe1p * xRe1p + x.im * x.im)), Math.atan2(x.im, xRe1p));
  88. }
  89. });
  90. exports.createLog1p = createLog1p;