aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ch/obermuhlner/math/big/BigComplexMath.java
blob: a73d9bccddd5471255609c0022c8a9914409b32c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package ch.obermuhlner.math.big;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.List;

import static ch.obermuhlner.math.big.BigComplex.I;

/**
 * Provides advanced functions operating on {@link BigComplex}s.
 */
public class BigComplexMath {

	private static final BigDecimal TWO = BigDecimal.valueOf(2);

	/**
	 * 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(BigComplex x, MathContext mathContext) {
		return x.reciprocal(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(BigComplex x) {
		return x.conjugate();
	}

	/**
	 * Calculates the absolute value (also known as magnitude, length or radius) of the given complex number using the specified {@link MathContext}.
	 *
	 * @param x the complex number to calculate the absolute value
	 * @param mathContext the {@link MathContext} used to calculate the result
	 * @return the calculated {@link BigComplex} result
	 * @see BigComplex#abs(MathContext)
	 */
	public static BigDecimal abs(BigComplex x, MathContext mathContext) {
		return x.abs(mathContext);
	}

	/**
	 * Calculates the square of the absolute value (also known as magnitude, length or radius) of the given complex number using the specified {@link MathContext}.
	 *
	 * @param x the complex number to calculate the square of the absolute value
	 * @param mathContext the {@link MathContext} used to calculate the result
	 * @return the calculated {@link BigComplex} result
	 * @see BigComplex#absSquare(MathContext)
	 */
	public static BigDecimal absSquare(BigComplex x, MathContext mathContext) {
		return x.absSquare(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(BigComplex x, MathContext mathContext) {
		return x.angle(mathContext);
	}

	/**
	 * Calculates the factorial of the specified {@link BigComplex}.
	 *
	 * <p>This implementation uses
	 * <a href="https://en.wikipedia.org/wiki/Spouge%27s_approximation">Spouge's approximation</a>
	 * to calculate the factorial for non-integer values.</p>
	 *
	 * <p>This involves calculating a series of constants that depend on the desired precision.
	 * Since this constant calculation is quite expensive (especially for higher precisions),
	 * the constants for a specific precision will be cached
	 * and subsequent calls to this method with the same precision will be much faster.</p>
	 *
	 * <p>It is therefore recommended to do one call to this method with the standard precision of your application during the startup phase
	 * and to avoid calling it with many different precisions.</p>
	 *
	 * <p>See: <a href="https://en.wikipedia.org/wiki/Factorial#Extension_of_factorial_to_non-integer_values_of_argument">Wikipedia: Factorial - Extension of factorial to non-integer values of argument</a></p>
	 *
	 * @param x the {@link BigComplex}
	 * @param mathContext the {@link MathContext} used for the result
	 * @return the factorial {@link BigComplex}
	 * @throws ArithmeticException if x is a negative integer value (-1, -2, -3, ...)
	 * @see BigDecimalMath#factorial(BigDecimal, MathContext)
	 * @see #gamma(BigComplex, MathContext)
	 */
	public static BigComplex factorial(BigComplex x, MathContext mathContext) {
		if (x.isReal() && BigDecimalMath.isIntValue(x.re)) {
			return BigComplex.valueOf(BigDecimalMath.factorial(x.re.intValueExact()).round(mathContext));
		}

		// https://en.wikipedia.org/wiki/Spouge%27s_approximation
		MathContext mc = new MathContext(mathContext.getPrecision() * 2, mathContext.getRoundingMode());

		int a = mathContext.getPrecision() * 13 / 10;
		List<BigDecimal> constants = BigDecimalMath.getSpougeFactorialConstants(a);

		BigDecimal bigA = BigDecimal.valueOf(a);

		boolean negative = false;
		BigComplex factor = BigComplex.valueOf(constants.get(0));
		for (int k = 1; k < a; k++) {
			BigDecimal bigK = BigDecimal.valueOf(k);
			factor = factor.add(BigComplex.valueOf(constants.get(k)).divide(x.add(bigK), mc), mc);
			negative = !negative;
		}

		BigComplex result = pow(x.add(bigA, mc), x.add(BigDecimal.valueOf(0.5), mc), mc);
		result = result.multiply(exp(x.negate().subtract(bigA, mc), mc), mc);
		result = result.multiply(factor, mc);

		return result.round(mathContext);
	}

	/**
	 * Calculates the gamma function of the specified {@link BigComplex}.
	 *
	 * <p>This implementation uses {@link #factorial(BigComplex, MathContext)} internally,
	 * therefore the performance implications described there apply also for this method.
	 *
	 * <p>See: <a href="https://en.wikipedia.org/wiki/Gamma_function">Wikipedia: Gamma function</a></p>
	 *
	 * @param x the {@link BigComplex}
	 * @param mathContext the {@link MathContext} used for the result
	 * @return the gamma {@link BigComplex}
	 * @throws ArithmeticException if x-1 is a negative integer value (-1, -2, -3, ...)
	 * @see BigDecimalMath#gamma(BigDecimal, MathContext)
	 * @see #factorial(BigComplex, MathContext)
	 */
	public static BigComplex gamma(BigComplex x, MathContext mathContext) {
		return factorial(x.subtract(BigComplex.ONE), 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(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 sine (sinus) of {@link BigComplex} x in the complex domain.
	 *
	 * <p>See: <a href="https://en.wikipedia.org/wiki/Sine#Sine_with_a_complex_argument">Wikipedia: Sine (Sine with a complex argument)</a></p>
	 *
	 * @param x the {@link BigComplex} to calculate the sine for
	 * @param mathContext the {@link MathContext} used for the result
	 * @return the calculated sine {@link BigComplex} with the precision specified in the <code>mathContext</code>
	 */
	public static BigComplex sin(BigComplex x, MathContext mathContext) {
		MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
		
		return BigComplex.valueOf(
				BigDecimalMath.sin(x.re, mc).multiply(BigDecimalMath.cosh(x.im, mc), mc).round(mathContext),
				BigDecimalMath.cos(x.re, mc).multiply(BigDecimalMath.sinh(x.im, mc), mc).round(mathContext));
	}

	/**
	 * 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(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));
	}
	
	// 
	// 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(BigComplex x, MathContext mathContext) {
		MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());

		return sin(x, mc).divide(cos(x, mc), mc).round(mathContext);
	}

	/**
	 * Calculates the arc tangens (inverted tangens) 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 tangens for
	 * @param mathContext the {@link MathContext} used for the result
	 * @return the calculated arc tangens {@link BigComplex} with the precision specified in the <code>mathContext</code>
	 */
	public static BigComplex atan(BigComplex x, MathContext mathContext) {
		MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());

		return log(I.subtract(x, mc).divide(I.add(x, mc), mc), mc).divide(I, mc).divide(TWO, mc).round(mathContext);
	}

	/**
	 * 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(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 arc sine (inverted sine) 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 sine for
	 * @param mathContext the {@link MathContext} used for the result
	 * @return the calculated arc sine {@link BigComplex} with the precision specified in the <code>mathContext</code>
	 */
	public static BigComplex asin(BigComplex x, MathContext mathContext) {
		MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());

		return I.negate().multiply(log(I.multiply(x, mc).add(sqrt(BigComplex.ONE.subtract(x.multiply(x, mc), mc), mc), mc), mc), mc).round(mathContext);
	}

	/**
	 * 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(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 square root of {@link BigComplex} x in the complex domain (sqrt 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(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);
	}

	/**
	 * 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(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 <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(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);
	}

	/**
	 * 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(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 {@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(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 {@link BigDecimal} n'th root of {@link BigComplex} x (<sup>n</sup>sqrt 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 BigDecimal} 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(BigComplex x, BigDecimal n, MathContext mathContext) {
		MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());

		return pow(x, BigDecimal.ONE.divide(n, mc), mc).round(mathContext);
	}

	/**
	 * Calculates the {@link BigComplex} n'th root of {@link BigComplex} x (<sup>n</sup>sqrt 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(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);
	}
	
	// TODO add root() for the k'th root - https://math.stackexchange.com/questions/322481/principal-nth-root-of-a-complex-number 
}