log.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { factory } from '../../utils/factory.js';
  2. import { logNumber } from '../../plain/number/index.js';
  3. var name = 'log';
  4. var dependencies = ['config', 'typed', 'divideScalar', 'Complex'];
  5. export var createLog = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. config,
  9. divideScalar,
  10. Complex
  11. } = _ref;
  12. /**
  13. * Calculate the logarithm of a value.
  14. *
  15. * To avoid confusion with the matrix logarithm, this function does not
  16. * apply to matrices.
  17. *
  18. * Syntax:
  19. *
  20. * math.log(x)
  21. * math.log(x, base)
  22. *
  23. * Examples:
  24. *
  25. * math.log(3.5) // returns 1.252762968495368
  26. * math.exp(math.log(2.4)) // returns 2.4
  27. *
  28. * math.pow(10, 4) // returns 10000
  29. * math.log(10000, 10) // returns 4
  30. * math.log(10000) / math.log(10) // returns 4
  31. *
  32. * math.log(1024, 2) // returns 10
  33. * math.pow(2, 10) // returns 1024
  34. *
  35. * See also:
  36. *
  37. * exp, log2, log10, log1p
  38. *
  39. * @param {number | BigNumber | Complex} x
  40. * Value for which to calculate the logarithm.
  41. * @param {number | BigNumber | Complex} [base=e]
  42. * Optional base for the logarithm. If not provided, the natural
  43. * logarithm of `x` is calculated.
  44. * @return {number | BigNumber | Complex}
  45. * Returns the logarithm of `x`
  46. */
  47. return typed(name, {
  48. number: function number(x) {
  49. if (x >= 0 || config.predictable) {
  50. return logNumber(x);
  51. } else {
  52. // negative value -> complex value computation
  53. return new Complex(x, 0).log();
  54. }
  55. },
  56. Complex: function Complex(x) {
  57. return x.log();
  58. },
  59. BigNumber: function BigNumber(x) {
  60. if (!x.isNegative() || config.predictable) {
  61. return x.ln();
  62. } else {
  63. // downgrade to number, return Complex valued result
  64. return new Complex(x.toNumber(), 0).log();
  65. }
  66. },
  67. 'any, any': typed.referToSelf(self => (x, base) => {
  68. // calculate logarithm for a specified base, log(x, base)
  69. return divideScalar(self(x), self(base));
  70. })
  71. });
  72. });