cbrt.js 4.5 KB

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