/** * Calculates {@link BigComplex} x to the power of <code>long</code> y (x<sup>y</sup>). * * <p>The implementation tries to minimize the number of multiplications of {@link BigComplex x} (using squares whenever possible).</p> * * <p>See: <a href="https://en.wikipedia.org/wiki/Exponentiation#Efficient_computation_with_integer_exponents">Wikipedia: Exponentiation - efficient computation</a></p> * * @param x the {@link BigComplex} value to take to the power * @param y the <code>long</code> value to serve as exponent * @param mathContext the {@link MathContext} used for the result * @return the calculated x to the power of y with the precision specified in the <code>mathContext</code> */ public static BigComplex pow(this BigComplex x, long y, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 10, mathContext.getRoundingMode()); if (y < 0) { return(BigComplex.ONE.divide(pow(x, -y, mc), mc).round(mathContext)); } BigComplex result = BigComplex.ONE; while (y > 0) { if ((y & 1) == 1) { // odd exponent -> multiply result with x result = result.multiply(x, mc); y -= 1; } if (y > 0) { // even exponent -> square x x = x.multiply(x, mc); } y >>= 1; } return(result.round(mathContext)); }
public void Parse() { string expected = "3 + 4i"; string expected004 = "3"; string test001 = "(3, 4)"; string test002 = "3 + 4i"; string test003 = "(3 + 4i) "; string test004 = "3"; BigComplex bc001 = BigComplex.Parse(test001); BigComplex bc002 = BigComplex.Parse(test002); BigComplex bc003 = BigComplex.Parse(test003); BigComplex bc004 = BigComplex.Parse(test004); string actual001 = bc001.ToString(); string actual002 = bc002.ToString(); string actual003 = bc003.ToString(); string actual004 = bc004.ToString(); Assert.AreEqual(expected, actual001, $"{test001} => {actual001}"); Assert.AreEqual(expected, actual002, $"{test002} => {actual002}"); Assert.AreEqual(expected, actual003, $"{test003} => {actual003}"); Assert.AreEqual(expected004, actual004, $"{test004} => {actual004}"); }
/** * Calculates the cosine (cosinus) of {@link BigComplex} x in the complex domain. * * @param x the {@link BigComplex} to calculate the cosine for * @param mathContext the {@link MathContext} used for the result * @return the calculated cosine {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex cos(this BigComplex x, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return(BigComplex.valueOf( BigDecimalMath.cos(x.re, mc).multiply(BigDecimalMath.cosh(x.im, mc), mc).round(mathContext), BigDecimalMath.sin(x.re, mc).multiply(BigDecimalMath.sinh(x.im, mc), mc).negate().round(mathContext))); }
/** * Calculates the natural exponent of {@link BigComplex} x (e<sup>x</sup>) in the complex domain. * * <p>See: <a href="https://en.wikipedia.org/wiki/Exponential_function#Complex_plane">Wikipedia: Exponent (Complex plane)</a></p> * * @param x the {@link BigComplex} to calculate the exponent for * @param mathContext the {@link MathContext} used for the result * @return the calculated exponent {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex exp(this BigComplex x, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal expRe = BigDecimalMath.exp(x.re, mc); return(BigComplex.valueOf( expRe.multiply(BigDecimalMath.cos(x.im, mc), mc).round(mathContext), expRe.multiply(BigDecimalMath.sin(x.im, mc), mc)).round(mathContext)); }
/** * Calculates the natural logarithm of {@link BigComplex} x in the complex domain. * * <p>See: <a href="https://en.wikipedia.org/wiki/Complex_logarithm">Wikipedia: Complex logarithm</a></p> * * @param x the {@link BigComplex} to calculate the natural logarithm for * @param mathContext the {@link MathContext} used for the result * @return the calculated natural logarithm {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex log(this BigComplex x, MathContext mathContext) { // https://en.wikipedia.org/wiki/Complex_logarithm MathContext mc1 = new MathContext(mathContext.getPrecision() + 20, mathContext.getRoundingMode()); MathContext mc2 = new MathContext(mathContext.getPrecision() + 5, mathContext.getRoundingMode()); return(BigComplex.valueOf( BigDecimalMath.log(x.abs(mc1), mc1).round(mathContext), x.angle(mc2)).round(mathContext)); }
/** * Calculates {@link BigComplex} x to the power of {@link BigDecimal} y (x<sup>y</sup>). * * @param x the {@link BigComplex} value to take to the power * @param y the {@link BigDecimal} value to serve as exponent * @param mathContext the {@link MathContext} used for the result * @return the calculated x to the power of y with the precision specified in the <code>mathContext</code> */ public static BigComplex pow(this BigComplex x, BigDecimal y, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal angleTimesN = x.angle(mc).multiply(y, mc); return(BigComplex.valueOf( BigDecimalMath.cos(angleTimesN, mc), BigDecimalMath.sin(angleTimesN, mc)).multiply(BigDecimalMath.pow(x.abs(mc), y, mc), mc).round(mathContext)); }
/** * Calculates the square root of {@link BigComplex} x in the complex domain (√x). * * <p>See <a href="https://en.wikipedia.org/wiki/Square_root#Square_root_of_an_imaginary_number">Wikipedia: Square root (Square root of an imaginary number)</a></p> * * @param x the {@link BigComplex} to calculate the square root for * @param mathContext the {@link MathContext} used for the result * @return the calculated square root {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex sqrt(this BigComplex x, MathContext mathContext) { // https://math.stackexchange.com/questions/44406/how-do-i-get-the-square-root-of-a-complex-number MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); BigDecimal magnitude = x.abs(mc); BigComplex a = x.add(magnitude, mc); return(a.divide(a.abs(mc), mc).multiply(BigDecimalMath.sqrt(magnitude, mc), mc).round(mathContext)); }
public void TestReciprocal() { var expected = "0 - 1i"; BigComplex low = BigComplex.Parse("0 + 1i"); var result = BigComplex.Reciprocal(low); var actual = result.ToString(); string description = $"{low} => {expected}"; TestContext.WriteLine(description); Assert.AreEqual(expected, actual, description); }
public void TestAbs001() { var expected = "5"; BigComplex low = BigComplex.Parse("4 + 3i"); var result = BigComplex.Abs(low); var actual = result.ToString(); string description = $"{low} => {expected}"; TestContext.WriteLine(description); Assert.AreEqual(expected, actual, description); }
public void TestMultiply() { var expected = "-44 + 106i"; BigComplex low = BigComplex.Parse("3 + 13i"); BigComplex high = BigComplex.Parse("7 + 5i"); var result = high * low; var actual = result.ToString(); string description = $"{low} * {high} => {expected}"; TestContext.WriteLine(description); Assert.AreEqual(expected, actual, description); }
public void TestDivide() { var expected = "2"; BigComplex low = BigComplex.Parse("3 + 2i"); BigComplex high = BigComplex.Parse("6 + 4i"); var result = high / low; var actual = result.ToString(); string description = $"{high} / {low} => {expected}"; TestContext.WriteLine(description); Assert.AreEqual(expected, actual, description); }
/** * Calculates the arc cosine (inverted cosine) of {@link BigComplex} x in the complex domain. * * <p>See: <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_complex_plane">Wikipedia: Inverse trigonometric functions (Extension to complex plane)</a></p> * * @param x the {@link BigComplex} to calculate the arc cosine for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc cosine {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex acos(this BigComplex x, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return(I.negate().multiply(log(x.add(sqrt(x.multiply(x, mc).subtract(BigComplex.ONE, mc), mc), mc), mc), mc).round(mathContext)); }
/** * Calculates the angle in radians of the given complex number using the specified {@link MathContext}. * * @param x the complex number to calculate the angle * @param mathContext the {@link MathContext} used to calculate the result * @return the calculated {@link BigComplex} angle in radians * @see BigComplex#angle(MathContext) */ public static BigDecimal angle(this BigComplex x, MathContext mathContext) { return(x.angle(mathContext)); }
/** * Calculates the {@link BigComplex} n'th root of {@link BigComplex} x (<sup>n</sup>√x). * * <p>See <a href="http://en.wikipedia.org/wiki/Square_root">Wikipedia: Square root</a></p> * @param x the {@link BigComplex} value to calculate the n'th root * @param n the {@link BigComplex} defining the root * @param mathContext the {@link MathContext} used for the result * * @return the calculated n'th root of x with the precision specified in the <code>mathContext</code> */ public static BigComplex root(this BigComplex x, BigComplex n, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return(pow(x, BigComplex.ONE.divide(n, mc), mc).round(mathContext)); }
/** * Calculates {@link BigComplex} x to the power of {@link BigComplex} y (x<sup>y</sup>). * * @param x the {@link BigComplex} value to take to the power * @param y the {@link BigComplex} value to serve as exponent * @param mathContext the {@link MathContext} used for the result * @return the calculated x to the power of y with the precision specified in the <code>mathContext</code> */ public static BigComplex pow(this BigComplex x, BigComplex y, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return(exp(y.multiply(log(x, mc), mc), mc).round(mathContext)); }
/** * Calculates the conjugate of the given complex number using the specified {@link MathContext}. * * @param x the complex number to calculate the conjugate * @return the calculated {@link BigComplex} result * @see BigComplex#conjugate() */ public static BigComplex conjugate(this BigComplex x) { return(x.conjugate()); }
/** * Calculates the arc cotangens (inverted cotangens) of {@link BigComplex} x in the complex domain. * * <p>See: <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_complex_plane">Wikipedia: Inverse trigonometric functions (Extension to complex plane)</a></p> * * @param x the {@link BigComplex} to calculate the arc cotangens for * @param mathContext the {@link MathContext} used for the result * @return the calculated arc cotangens {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex acot(this BigComplex x, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return(log(x.add(I, mc).divide(x.subtract(I, mc), mc), mc).divide(I, mc).divide(TWO, mc).round(mathContext)); }
/** * Calculates the reciprocal of the given complex number using the specified {@link MathContext}. * * @param x the complex number to calculate the reciprocal * @param mathContext the {@link MathContext} used to calculate the result * @return the calculated {@link BigComplex} result * @see BigComplex#reciprocal(MathContext) */ public static BigComplex reciprocal(this BigComplex x, MathContext mathContext) { return(x.reciprocal(mathContext)); }
// // http://scipp.ucsc.edu/~haber/archives/physics116A10/arc_10.pdf /** * Calculates the tangens of {@link BigComplex} x in the complex domain. * * @param x the {@link BigComplex} to calculate the tangens for * @param mathContext the {@link MathContext} used for the result * @return the calculated tangens {@link BigComplex} with the precision specified in the <code>mathContext</code> */ public static BigComplex tan(this BigComplex x, MathContext mathContext) { MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode()); return(sin(x, mc).divide(cos(x, mc), mc).round(mathContext)); }