cbrt.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { factory } from '../../utils/factory.js';
  2. import { isBigNumber, isComplex, isFraction } from '../../utils/is.js';
  3. import { cbrtNumber } from '../../plain/number/index.js';
  4. var name = 'cbrt';
  5. var dependencies = ['config', 'typed', 'isNegative', 'unaryMinus', 'matrix', 'Complex', 'BigNumber', 'Fraction'];
  6. export var createCbrt = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. config,
  9. typed,
  10. isNegative,
  11. unaryMinus,
  12. matrix,
  13. Complex,
  14. BigNumber,
  15. Fraction
  16. } = _ref;
  17. /**
  18. * Calculate the cubic root of a value.
  19. *
  20. * To avoid confusion with the matrix cube root, this function does not
  21. * apply to matrices. For a matrix, to take the cube root elementwise,
  22. * see the examples.
  23. *
  24. * Syntax:
  25. *
  26. * math.cbrt(x)
  27. * math.cbrt(x, allRoots)
  28. *
  29. * Examples:
  30. *
  31. * math.cbrt(27) // returns 3
  32. * math.cube(3) // returns 27
  33. * math.cbrt(-64) // returns -4
  34. * math.cbrt(math.unit('27 m^3')) // returns Unit 3 m
  35. * math.map([27, 64, 125], x => math.cbrt(x)) // returns [3, 4, 5]
  36. *
  37. * const x = math.complex('8i')
  38. * math.cbrt(x) // returns Complex 1.7320508075689 + i
  39. * math.cbrt(x, true) // returns Matrix [
  40. * // 1.7320508075689 + i
  41. * // -1.7320508075689 + i
  42. * // -2i
  43. * // ]
  44. *
  45. * See also:
  46. *
  47. * square, sqrt, cube
  48. *
  49. * @param {number | BigNumber | Complex | Unit} x
  50. * Value for which to calculate the cubic root.
  51. * @param {boolean} [allRoots] Optional, false by default. Only applicable
  52. * when `x` is a number or complex number. If true, all complex
  53. * roots are returned, if false (default) the principal root is
  54. * returned.
  55. * @return {number | BigNumber | Complex | Unit}
  56. * Returns the cubic root of `x`
  57. */
  58. return typed(name, {
  59. number: cbrtNumber,
  60. // note: signature 'number, boolean' is also supported,
  61. // created by typed as it knows how to convert number to Complex
  62. Complex: _cbrtComplex,
  63. 'Complex, boolean': _cbrtComplex,
  64. BigNumber: function BigNumber(x) {
  65. return x.cbrt();
  66. },
  67. Unit: _cbrtUnit
  68. });
  69. /**
  70. * Calculate the cubic root for a complex number
  71. * @param {Complex} x
  72. * @param {boolean} [allRoots] If true, the function will return an array
  73. * with all three roots. If false or undefined,
  74. * the principal root is returned.
  75. * @returns {Complex | Array.<Complex> | Matrix.<Complex>} Returns the cubic root(s) of x
  76. * @private
  77. */
  78. function _cbrtComplex(x, allRoots) {
  79. // https://www.wikiwand.com/en/Cube_root#/Complex_numbers
  80. var arg3 = x.arg() / 3;
  81. var abs = x.abs();
  82. // principal root:
  83. var principal = new Complex(cbrtNumber(abs), 0).mul(new Complex(0, arg3).exp());
  84. if (allRoots) {
  85. var all = [principal, new Complex(cbrtNumber(abs), 0).mul(new Complex(0, arg3 + Math.PI * 2 / 3).exp()), new Complex(cbrtNumber(abs), 0).mul(new Complex(0, arg3 - Math.PI * 2 / 3).exp())];
  86. return config.matrix === 'Array' ? all : matrix(all);
  87. } else {
  88. return principal;
  89. }
  90. }
  91. /**
  92. * Calculate the cubic root for a Unit
  93. * @param {Unit} x
  94. * @return {Unit} Returns the cubic root of x
  95. * @private
  96. */
  97. function _cbrtUnit(x) {
  98. if (x.value && isComplex(x.value)) {
  99. var result = x.clone();
  100. result.value = 1.0;
  101. result = result.pow(1.0 / 3); // Compute the units
  102. result.value = _cbrtComplex(x.value); // Compute the value
  103. return result;
  104. } else {
  105. var negate = isNegative(x.value);
  106. if (negate) {
  107. x.value = unaryMinus(x.value);
  108. }
  109. // TODO: create a helper function for this
  110. var third;
  111. if (isBigNumber(x.value)) {
  112. third = new BigNumber(1).div(3);
  113. } else if (isFraction(x.value)) {
  114. third = new Fraction(1, 3);
  115. } else {
  116. third = 1 / 3;
  117. }
  118. var _result = x.pow(third);
  119. if (negate) {
  120. _result.value = unaryMinus(_result.value);
  121. }
  122. return _result;
  123. }
  124. }
  125. });