log10.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { factory } from '../../utils/factory.js';
  2. import { deepMap } from '../../utils/collection.js';
  3. import { log10Number } from '../../plain/number/index.js';
  4. var name = 'log10';
  5. var dependencies = ['typed', 'config', 'Complex'];
  6. export var createLog10 = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. typed,
  9. config,
  10. Complex: _Complex
  11. } = _ref;
  12. /**
  13. * Calculate the 10-base logarithm of a value. This is the same as calculating `log(x, 10)`.
  14. *
  15. * For matrices, the function is evaluated element wise.
  16. *
  17. * Syntax:
  18. *
  19. * math.log10(x)
  20. *
  21. * Examples:
  22. *
  23. * math.log10(0.00001) // returns -5
  24. * math.log10(10000) // returns 4
  25. * math.log(10000) / math.log(10) // returns 4
  26. * math.pow(10, 4) // returns 10000
  27. *
  28. * See also:
  29. *
  30. * exp, log, log1p, log2
  31. *
  32. * @param {number | BigNumber | Complex | Array | Matrix} x
  33. * Value for which to calculate the logarithm.
  34. * @return {number | BigNumber | Complex | Array | Matrix}
  35. * Returns the 10-base logarithm of `x`
  36. */
  37. return typed(name, {
  38. number: function number(x) {
  39. if (x >= 0 || config.predictable) {
  40. return log10Number(x);
  41. } else {
  42. // negative value -> complex value computation
  43. return new _Complex(x, 0).log().div(Math.LN10);
  44. }
  45. },
  46. Complex: function Complex(x) {
  47. return new _Complex(x).log().div(Math.LN10);
  48. },
  49. BigNumber: function BigNumber(x) {
  50. if (!x.isNegative() || config.predictable) {
  51. return x.log();
  52. } else {
  53. // downgrade to number, return Complex valued result
  54. return new _Complex(x.toNumber(), 0).log().div(Math.LN10);
  55. }
  56. },
  57. 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
  58. });
  59. });