-
Notifications
You must be signed in to change notification settings - Fork 1
/
PolarCoordinates.java
332 lines (280 loc) · 12.3 KB
/
PolarCoordinates.java
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
package eu.hoefel.coordinates;
import java.util.Collections;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import eu.hoefel.coordinates.axes.Axes;
import eu.hoefel.coordinates.axes.Axis;
import eu.hoefel.coordinates.tensors.TensorIndexType;
import eu.hoefel.coordinates.tensors.TensorTransformation;
import eu.hoefel.unit.Unit;
import eu.hoefel.unit.Units;
import eu.hoefel.unit.si.SiBaseUnit;
import eu.hoefel.unit.si.SiDerivedUnit;
/**
* Polar coordinates determine a point P by measuring its distance from a
* reference point R, denoted radius <i>ρ</i>, and the polar angle <i>φ</i>
* between the connection PR and another specific vector (typically an axis of
* the system).
* <p>
* They are defined as follows:<br>
* {@code x = ρ cos(φ)}<br>
* {@code y = ρ sin(φ)}<br>
* <p>
* There are two extensions of the 2D polar coordinates to 3D:
* {@link CylindricalCoordinates} and {@link SphericalCoordinates}.
*
* @param axes the axes defining the coordinate system, not null
*
* @author Udo Hoefel
*/
@CoordinateSystemSymbols({"polar", "pol"})
public final record PolarCoordinates(NavigableSet<Axis> axes) implements CoordinateSystem {
/** The default axes. */
public static final NavigableSet<Axis> DEFAULT_AXES = Axes.of(
new Axis(0, SiBaseUnit.METER, "ρ"),
new Axis(1, SiDerivedUnit.RADIAN, "φ"));
/**
* The consumer useful for checking the arguments in
* {@link #PolarCoordinates(Object...)}.
*/
private static final Consumer<Object[]> ARG_CHECK = args -> Axes.DEFAULT_ARG_CHECK.accept("Polar", args);
/** Constructs a new polar coordinate system. */
public PolarCoordinates {
Objects.requireNonNull(axes, "Axes may not be null. "
+ "Use the DEFAULT_AXES or the empty constructor to get a reasonable default.");
if (!Units.convertible(Axis.fromSet(axes, 1).unit(), Units.EMPTY_UNIT)) {
throw new IllegalArgumentException("The unit of dimension 1 (%s) needs to be effectively dimensionless."
.formatted(Axis.fromSet(axes, 1).unit()));
}
}
/**
* Constructs a new polar coordinate system.
*
* @param args the arguments, must be either {@link Axes} or {@link Axis}, which
* will take precedence over the {@link #DEFAULT_AXES} if given. If
* no arguments are given, the default axes will be used.
*/
public PolarCoordinates(Object... args) {
this(Axes.fromArgs(DEFAULT_AXES, ARG_CHECK, args));
}
/**
* Validates the position, i.e. it throws an exception if a dimension of the
* position is out of range.
*
* @param position the position to validate
* @throw IllegalArgumentException if the assumptions about the dimensionality
* or the valid range of any dimension of the input are violated.
*/
private void validatePosition(double[] position) {
Objects.requireNonNull(position);
if (position.length > dimension()) {
throw new IllegalArgumentException(
"The given dimensionality exceeds the maximum supported dimensionality (%d vs %d)"
.formatted(position.length, dimension()));
}
if (position[0] < 0) {
throw new IllegalArgumentException("position[0] needs to be above 0, but is " + position[0]);
}
if (position[1] < 0 || position[1] > 2*Math.PI) {
throw new IllegalArgumentException("position[1] needs to be between 0 and 2*Math.PI, but is " + position[1]);
}
}
@Override
public int dimension() {
return 2;
}
@Override
public boolean isOrthogonal() {
return true;
}
@Override
public NavigableMap<Integer, Unit> toBaseUnits() {
NavigableMap<Integer, Unit> map = new TreeMap<>();
map.put(0, axis(0).unit());
map.put(1, axis(0).unit());
return Collections.unmodifiableNavigableMap(map);
}
@Override
public double[] toBasePoint(double[] position) {
validatePosition(position);
double[] pointInBase = new double[2];
pointInBase[0] = position[0]*Math.cos(position[1]);
pointInBase[1] = position[0]*Math.sin(position[1]);
return pointInBase;
}
@Override
public double[] fromBasePoint(double[] position) {
double[] pointInCurrentSystem = new double[2];
pointInCurrentSystem[0] = Math.pow((Math.pow(position[0],2)+Math.pow(position[1],2)),0.5);
pointInCurrentSystem[1] = Math.atan2(position[1],position[0]);
return pointInCurrentSystem;
}
@Override
public double[] toBaseVector(double[] position, double[] vector) {
validatePosition(position);
double[] vectorInBaseSys = new double[vector.length];
vectorInBaseSys[0] = Math.cos(position[1])*vector[0]+Math.sin(position[1])*vector[1];
vectorInBaseSys[1] = -1*position[0]*Math.sin(position[1])*vector[0]+position[0]*Math.cos(position[1])*vector[1];
return vectorInBaseSys;
}
@Override
public double[] fromBaseVector(double[] position, double[] vector) {
double[] vectorInCurrentSys = new double[vector.length];
vectorInCurrentSys[0] = position[0]*Math.pow((Math.pow(position[0],2)+Math.pow(position[1],2)),-1.0/2)*vector[0]-1*position[1]*Math.pow((Math.pow(position[0],2)+Math.pow(position[1],2)),-1)*vector[1];
vectorInCurrentSys[1] = position[1]*Math.pow((Math.pow(position[0],2)+Math.pow(position[1],2)),-1.0/2)*vector[0]+position[0]*Math.pow((Math.pow(position[0],2)+Math.pow(position[1],2)),-1)*vector[1];
return vectorInCurrentSys;
}
@Override
public double metricCoefficient(double[] position, TensorTransformation behavior, int i, int j) {
validatePosition(position);
if (i < 0 || j < 0) {
throw new IllegalArgumentException(("Metric coefficient not available for i=%d, j=%d "
+ "(too low dimension, only 2 dimensions [0,1] are supported for polar coordinates)")
.formatted(i, j));
} else if (i > 1 || j > 1) {
throw new IllegalArgumentException(("Metric coefficient not available for i=%d, j=%d "
+ "(too high dimension, only 2 dimensions [0,1] are supported for polar coordinates)")
.formatted(i, j));
}
if (behavior instanceof TensorIndexType tit) {
return switch (tit) {
case COVARIANT -> {
if (i == 0 && j == 0) yield 1;
if (i == 1 && j == 1) yield Math.pow(position[0],2);
yield 0;
}
case CONTRAVARIANT -> {
if (i == 0 && j == 0) yield 1;
if (i == 1 && j == 1) yield Math.pow(position[0],-2);
yield 0;
}
};
}
// Fall back to metric tensor (which might fall back to this method) for mixed behavior
Function<double[], double[][]> metricTensor = pos -> metricTensor(pos, TensorIndexType.COVARIANT);
return TensorIndexType.COVARIANT.transform(this, metricTensor, behavior).apply(position)[i][j];
}
@Override
public double[][] metricTensor(double[] position, TensorTransformation behavior) {
validatePosition(position);
int dim = 2;
double[][] g = new double[dim][dim];
// Note that we skip all elements that are zero anyways
g[0][0] = metricCoefficient(position, behavior, 0, 0);
g[1][1] = metricCoefficient(position, behavior, 1, 1);
return g;
}
@Override
public double jacobianDeterminant(double[] position) {
validatePosition(position);
return position[0];
}
@Override
public double christoffelSymbol1stKind(double[] position, int i, int j, int k) {
validatePosition(position);
int dim = position.length;
if (i < 0 || j < 0 || k < 0 || i >= dim || j >= dim || k >= dim) {
throw new IllegalArgumentException(
"i, j and k may not be <0 or exceed %d, but they were i=%d, j=%d and k=%d"
.formatted(dim, i, j, k));
}
if (i == 0 && j == 1 && k == 1) return position[0];
if (i == 1 && j == 0 && k == 1) return position[0];
if (i == 1 && j == 1 && k == 0) return -position[0];
return 0;
}
@Override
public double christoffelSymbol2ndKind(double[] position, int m, int i, int j) {
validatePosition(position);
int dim = position.length;
if (m < 0 || i < 0 || j < 0 || m >= dim || i >= dim || j >= dim) {
throw new IllegalArgumentException(
"m, i and j may not be <0 or exceed %d, but they were m=%d, i=%d and j=%d"
.formatted(dim, m, i, j));
}
if (m == 0 && i == 1 && j == 1) return -position[0];
if (m == 1 && i == 0 && j == 1) return 1/position[0];
if (m == 1 && i == 1 && j == 0) return 1/position[0];
return 0;
}
@Override
public double riemannTensor(double[] position, int mu, int nu, int rho, int sigma) {
validatePosition(position);
int dim = position.length;
if (mu < 0 || nu < 0 || rho < 0 || sigma < 0 || mu >= dim || nu >= dim || rho >= dim || sigma >= dim) {
throw new IllegalArgumentException(
"mu, nu, rho and sigma may not be <0 or exceed %d, but they were mu=%d, nu=%d, rho=%d and sigma=%d"
.formatted(dim, mu, nu, rho, sigma));
}
return 0;
}
@Override
public boolean isFlat() {
return true;
}
@Override
public double ricciTensor(double[] position, int mu, int nu) {
validatePosition(position);
int dim = position.length;
if (mu < 0 || nu < 0 || mu >= dim || nu >= dim) {
throw new IllegalArgumentException(
"mu and nu may not be <0 or exceed %d, but they were mu=%d and nu=%d".formatted(dim, mu, nu));
}
return 0;
}
@Override
public double ricciScalar(double[] position) {
validatePosition(position);
return 0;
}
@Override
public <T> double magnitude(double[] position, TensorTransformation transformation, Function<double[], T> tensorfield) {
validatePosition(position);
return CoordinateSystem.super.magnitude(position, transformation, tensorfield);
}
@Override
public double ds(double[] position, int i, double dui) {
validatePosition(position);
return CoordinateSystem.super.ds(position, i, dui);
}
@Override
public double dA(double[] position, int i, int j, double dui, double duj) {
validatePosition(position);
return CoordinateSystem.super.dA(position, i, j, dui, duj);
}
@Override
public double dV(double[] position, double... du) {
validatePosition(position);
return CoordinateSystem.super.dV(position, du);
}
@Override
public double dot(double[] position, TensorIndexType behavior, double[] v1, double[] v2) {
validatePosition(position);
return CoordinateSystem.super.dot(position, behavior, v1, v2);
}
@Override
public double[] cross(double[] position, TensorIndexType behavior, double[] v1, double[] v2, double[]... vn) {
validatePosition(position);
return CoordinateSystem.super.cross(position, behavior, v1, v2, vn);
}
@Override
public <T> T div(double[] position, TensorTransformation componentBehavior, Function<double[], T[]> field) {
validatePosition(position);
return CoordinateSystem.super.div(position, componentBehavior, field);
}
@Override
public <T> T[] grad(double[] position, TensorTransformation componentBehavior, Function<double[], T> field) {
validatePosition(position);
return CoordinateSystem.super.grad(position, componentBehavior, field);
}
@Override
public double[] curl(double[] position, TensorTransformation componentBehavior, UnaryOperator<double[]> vectorfield) {
validatePosition(position);
return CoordinateSystem.super.curl(position, componentBehavior, vectorfield);
}
}