-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLegrendePolynomial.java
More file actions
115 lines (99 loc) · 3.98 KB
/
Copy pathLegrendePolynomial.java
File metadata and controls
115 lines (99 loc) · 3.98 KB
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
package core.tools.functiongenerators;
import core.config.Settings;
import core.functions.GeneralFunction;
import core.functions.binary.Pow;
import core.functions.commutative.Product;
import core.functions.commutative.Sum;
import core.functions.endpoint.Constant;
import core.functions.endpoint.Variable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static java.lang.Math.*;
import static core.tools.MiscTools.*;
/**
* The methods in {@link LegrendePolynomial} deal with
* <a href="https://en.wikipedia.org/wiki/Legendre_polynomials">Legendre Polynomials</a>.
* <p><b>Note:</b> The class name "LegrendePolynomial" contains a historical typo
* (correct spelling: "LegendrePolynomial"). The name is kept unchanged for API compatibility.
*/
public class LegrendePolynomial {
/**
* Thread-safe cache using ConcurrentHashMap.
* A plain HashMap would cause race conditions under concurrent access
* (multiple threads calling makeLegrendePolynomial() simultaneously).
*/
private static final Map<Integer, GeneralFunction> cache = new ConcurrentHashMap<>();
private static final String defaultVariable = "\\var";
private static GeneralFunction makeLegrendePolynomial(int n) {
if (cache.containsKey(n))
return cache.get(n);
int p = (n + 1) / 2;
GeneralFunction[] sum = new GeneralFunction[n - p + 1];
for (int m = p; m <= n; m++) {
sum[m - p] = new Product(new Constant(constant(n, m)), new Pow(new Constant(2 * m - n), new Variable(defaultVariable))).simplify();
}
GeneralFunction polynomial = new Sum(sum).simplify();
if (Settings.cacheLegrendePolynomials)
cache.put(n, polynomial);
return polynomial;
}
/**
* Returns the nth Legrende polynomial with the default variable defined with {@link Settings#singleVariableDefault}
* @param n the nth polynomial
* @return nth Legrende polynomial
*/
public static GeneralFunction legrendePolynomial(int n) {
return legrendePolynomial(n, Settings.singleVariableDefault);
}
/**
* Returns the nth Legrende polynomial with a variable of the specified String
* @param n the nth polynomial
* @param variable the String of the {@link Variable} of the polynomial
* @return nth Legrende polynomial
*/
public static GeneralFunction legrendePolynomial(int n, String variable) {
Map<String, Variable> substitution = new HashMap<>();
substitution.put(defaultVariable, new Variable(variable));
return makeLegrendePolynomial(n).substituteVariables(substitution);
}
/**
* Returns the nth normalized Legrende polynomial with the default variable defined with {@link Settings#singleVariableDefault}
* @param n the nth polynomial
* @return nth normalized Legrende polynomial
*/
public static GeneralFunction normalLegrendePolynomial(int n) {
return new Product(new Constant(1 / normalizingConstantDouble(n)), legrendePolynomial(n)).simplify();
}
/**
* Returns the nth normalized Legrende polynomial with a variable of the specified String
* @param n the nth polynomial
* @param variable the String of the {@link Variable} of the polynomial
* @return nth normalized Legrende polynomial
*/
public static GeneralFunction normalLegrendePolynomial(int n, String variable) {
return new Product(new Constant(1 / normalizingConstantDouble(n)), legrendePolynomial(n, variable)).simplify();
}
/**
* The normalizing constant of the nth Legrende polynomial
* ie If you wanted to normalize, you would do {@code 1/N * L_n}
* @param n the nth polynomial
* @return normalizing constant of the nth Legrende polynomial
*/
public static GeneralFunction normalizingConstant(int n) {
return new Constant(normalizingConstantDouble(n));
}
private static double normalizingConstantDouble(int n) {
return 1 / sqrt(0.5 * (2 * n + 1));
}
private static double constant(int n, int m) {
double mult = 1;
for (int i = 0; i < n; i++) {
mult *= ((2 * m - i) / 2.);
}
mult /= factorial(m);
mult /= factorial(n - m);
mult *= 1 - 2 * ((n - m) % 2); // (-1)^(n-m)
return mult;
}
}