1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.analysis.differentiation;
18
19 import java.lang.reflect.Array;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.hipparchus.CalculusFieldElement;
24 import org.hipparchus.Field;
25 import org.hipparchus.util.MathArrays;
26
27
28
29
30
31 public class FieldGradientField<T extends CalculusFieldElement<T>> implements Field<FieldGradient<T>> {
32
33
34 private static final Map<Field<?>, FieldGradientField<?>[]> CACHE = new HashMap<>();
35
36
37 private final FieldGradient<T> zero;
38
39
40 private final FieldGradient<T> one;
41
42
43 private final FDSFactory<T> factory;
44
45
46
47
48
49 private FieldGradientField(final Field<T> valueField, final int parameters) {
50 zero = new FieldGradient<>(valueField.getZero(), MathArrays.buildArray(valueField, parameters));
51 one = new FieldGradient<>(valueField.getOne(), MathArrays.buildArray(valueField, parameters));
52 factory = new FDSFactory<>(valueField, parameters, 1);
53 }
54
55
56
57
58
59
60
61 public static <T extends CalculusFieldElement<T>> FieldGradientField<T> getField(final Field<T> valueField, final int parameters) {
62
63 FieldGradientField<?>[] cachedFields;
64 synchronized (CACHE) {
65 cachedFields = CACHE.get(valueField);
66 if (cachedFields == null || cachedFields.length <= parameters) {
67 FieldGradientField<?>[] newCachedFields =
68 (FieldGradientField<?>[]) Array.newInstance(FieldGradientField.class, parameters + 1);
69 if (cachedFields != null) {
70
71 System.arraycopy(cachedFields, 0, newCachedFields, 0, cachedFields.length);
72 }
73 cachedFields = newCachedFields;
74 CACHE.put(valueField, cachedFields);
75 }
76 }
77
78 if (cachedFields[parameters] == null) {
79
80 cachedFields[parameters] = new FieldGradientField<>(valueField, parameters);
81 }
82
83 @SuppressWarnings("unchecked")
84 final FieldGradientField<T> tCached = (FieldGradientField<T>) cachedFields[parameters];
85 return tCached;
86
87 }
88
89
90 @Override
91 public FieldGradient<T> getOne() {
92 return one;
93 }
94
95
96 @Override
97 public FieldGradient<T> getZero() {
98 return zero;
99 }
100
101
102 @SuppressWarnings("unchecked")
103 @Override
104 public Class<FieldGradient<T>> getRuntimeClass() {
105 return (Class<FieldGradient<T>>) getZero().getClass();
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119 FDSFactory<T> getConversionFactory() {
120 return factory;
121 }
122
123
124 @Override
125 public boolean equals(final Object other) {
126 return this == other;
127 }
128
129
130 @Override
131 public int hashCode() {
132 return 0xcd3e92ee;
133 }
134
135 }