summaryrefslogtreecommitdiffstats
path: root/org/madore/ephem/Comput.java
blob: 214d5e847281a65d7739f2852345fc7bf74e2f8e (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
package org.madore.ephem;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public final class Comput {

    public static final double degree = Math.PI/180;
    public static final double arcsecond = degree/3600;

    public static final double ipow(double x, int n) {
	if ( n == 0 )
	    return 1;
	else if ( n == 1 )
	    return x;
	else if ( n < 0 ) {
	    x = 1./x;
	    n = -n;
	}
	double y = x;
	int k = 1;
	double z = 1;
	int m = 0;
	while ( true ) {
	    if ( ( n & k ) != 0 ) {
		z *= y;
		m += k;
	    }
	    if ( m < n ) {
		y *= y;
		k *= 2;
	    } else
		return z;
	}
    }

    private static Double[] boxDoubleArray(double[] c) {
	Double[] boxed = new Double[c.length];
	for ( int i=0 ; i<boxed.length ; i++ )
	    boxed[i] = c[i];
	return boxed;
    }

    public static interface Function {
	public double v(double t);
    }

    public static final class Constant implements Function {
	double val;
	public Constant(double val) {
	    this.val = val;
	}
	public double v(double t) {
	    return val;
	}
    }

    public static final class Identity implements Function {
	public double v(double t) {
	    return t;
	}
    }

    public static class SumFunction implements Function {
	List<? extends Function> tms;
	public SumFunction(List<? extends Function> tms) {
	    this.tms = tms;
	}
	public SumFunction(Function[] tms) {
	    this(Arrays.asList(tms));
	}
	public double v(double t) {
	    double z = 0;
	    for ( int i=0 ; i<tms.size() ; i++ )
		z += tms.get(i).v(t);
	    return z;
	}	
    }

    public static class LinCombFunction implements Function {
	List<Double> c;
	List<Function> tms;
	public LinCombFunction(List<Double> c, List<Function> tms) {
	    if ( c.size() != tms.size() )
		throw new IllegalArgumentException();
	    this.c = c;
	    this.tms = tms;
	}
	public LinCombFunction(List<Double> c, Function[] tms) {
	    this(c, Arrays.asList(tms));
	}
	public LinCombFunction(Double[] c, List<Function> tms) {
	    this(Arrays.asList(c), tms);
	}
	public LinCombFunction(Double[] c, Function[] tms) {
	    this(Arrays.asList(c), Arrays.asList(tms));
	}
	public LinCombFunction(double[] c, List<Function> tms) {
	    this(boxDoubleArray(c), tms);
	}
	public LinCombFunction(double[] c, Function[] tms) {
	    this(boxDoubleArray(c), tms);
	}
	public double v(double t) {
	    double z = 0;
	    for ( int i=0 ; i<tms.size() ; i++ )
		z += c.get(i) * tms.get(i).v(t);
	    return z;
	}	
    }

    public static final class TimesPower implements Function {
	int deg;
	Function fn;
	public TimesPower(int deg, Function fn) {
	    this.deg = deg;
	    this.fn = fn;
	}
	public double v(double t) {
	    return ipow(t,deg) * fn.v(t);
	}
    }

    public static final class Polynomial implements Function {
	List<Double> c;
	public Polynomial(List<Double> c) {
	    this.c = c;
	}
	public Polynomial(Double[] c) {
	    this(Arrays.asList(c));
	}
	public Polynomial(double... c) {
	    this(boxDoubleArray(c));
	}
	public double v(double t) {
	    double z = 0;
	    double y = 1;
	    for ( int i=0 ; i<c.size() ; i++ ) {
		z += c.get(i) * y;
		y *= t;
	    }
	    return z;
	}	
    }

    public static final class PoissonTerm implements Function {
	// Returns c*t^d*cos(phi+om*t) = t^d*(a*cos(om*t) + b*sin(om*t))
	int deg;
	double c;
	double phi;	
	double om;
	PoissonTerm(int deg, double c, double phi, double om) {
	    this.c = c;
	    this.deg = deg;
	    this.phi = phi;
	    this.om = om;
	}
	public static PoissonTerm cphi(int deg, double c, double phi, double om) {
	    return new PoissonTerm(deg, c, phi, om);
	}
	public static PoissonTerm ab(int deg, double a, double b, double om) {
	    double c = Math.sqrt(a*a + b*b);
	    double phi = Math.atan2(-b, a);
	    return new PoissonTerm(deg, c, phi, om);
	}
	public double v(double t) {
	    return c * ipow(t, deg) * Math.cos(om*t + phi);
	}
    }

    public static class SumPoissonTerms extends SumFunction
	implements Function {
	List<? extends PoissonTerm> tms;
	public SumPoissonTerms(List<? extends PoissonTerm> tms) {
	    super(tms);
	    this.tms = tms;
	}
	public SumPoissonTerms(PoissonTerm[] tms) {
	    this(Arrays.asList(tms));
	}
	public double v(double t) {
	    double z = 0;
	    final int npow = 6;
	    List<Double> tpow = new ArrayList<Double>(npow);
	    for ( int d=0 ; d<npow ; d++ )
		tpow.add(ipow(t,d));
	    for ( int i=0 ; i<tms.size() ; i++ ) {
		PoissonTerm tm = tms.get(i);
		int deg = tm.deg;
		double tp = deg<npow ? tpow.get(deg) : ipow(t,deg);
		z += tm.c * tp * Math.cos(tm.om*t + tm.phi);
	    }
	    return z;
	}	
    }

}