Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 3e96b8f

Browse files
committed
move to MonoSpline float
1 parent 5aa076d commit 3e96b8f

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

desktop/interpolationEngines/src/main/kotlin/curves/MonoSpline.kt

+33-33
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ package curves
2020
*
2121
*
2222
*/
23-
class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
24-
val timePoints: DoubleArray
25-
var mY: ArrayList<DoubleArray>
26-
var mTangent: ArrayList<DoubleArray>
23+
class MonoSpline(time: FloatArray, y: List<FloatArray>) {
24+
val timePoints: FloatArray
25+
var mY: ArrayList<FloatArray>
26+
var mTangent: ArrayList<FloatArray>
2727
private val mExtrapolate = true
28-
var mSlopeTemp: DoubleArray
29-
fun makeDoubleArray(a: Int, b: Int): ArrayList<DoubleArray> {
30-
val ret = ArrayList<DoubleArray>() //new double[a][b];
28+
var mSlopeTemp: FloatArray
29+
fun makeFloatArray(a: Int, b: Int): ArrayList<FloatArray> {
30+
val ret = ArrayList<FloatArray>() //new Float[a][b];
3131
for (i in 0 until a) {
32-
ret.add(DoubleArray(b))
32+
ret.add(FloatArray(b))
3333
}
3434
return ret
3535
}
3636

3737
init {
3838
val n = time.size
3939
val dim = y[0].size
40-
mSlopeTemp = DoubleArray(dim)
41-
val slope = makeDoubleArray(n - 1, dim) // could optimize this out
42-
val tangent = makeDoubleArray(n, dim)
40+
mSlopeTemp = FloatArray(dim)
41+
val slope = makeFloatArray(n - 1, dim) // could optimize this out
42+
val tangent = makeFloatArray(n, dim)
4343
for (j in 0 until dim) {
4444
for (i in 0 until n - 1) {
4545
val dt = time[i + 1] - time[i]
@@ -54,15 +54,15 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
5454
}
5555
for (i in 0 until n - 1) {
5656
for (j in 0 until dim) {
57-
if (slope[i][j] == 0.0) {
58-
tangent[i][j] = 0.0
59-
tangent[i + 1][j] = 0.0
57+
if (slope[i][j] == 0.0f) {
58+
tangent[i][j] = 0.0f
59+
tangent[i + 1][j] = 0.0f
6060
} else {
6161
val a = tangent[i][j] / slope[i][j]
6262
val b = tangent[i + 1][j] / slope[i][j]
63-
val h = Math.hypot(a, b)
63+
val h = Math.hypot(a.toDouble(), b.toDouble()).toFloat()
6464
if (h > 9.0) {
65-
val t = 3.0 / h
65+
val t = 3.0f / h
6666
tangent[i][j] = t * a * slope[i][j]
6767
tangent[i + 1][j] = t * b * slope[i][j]
6868
}
@@ -74,15 +74,15 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
7474
mTangent = tangent
7575
}
7676

77-
private fun copyData(y: List<DoubleArray>): ArrayList<DoubleArray> {
78-
val ret = ArrayList<DoubleArray>()
77+
private fun copyData(y: List<FloatArray>): ArrayList<FloatArray> {
78+
val ret = ArrayList<FloatArray>()
7979
for (array in y) {
8080
ret.add(array)
8181
}
8282
return ret
8383
}
8484

85-
fun getPos(t: Double, v: DoubleArray) {
85+
fun getPos(t: Float, v: FloatArray) {
8686
val n = timePoints.size
8787
val dim = mY[0].size
8888
if (mExtrapolate) {
@@ -135,7 +135,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
135135
}
136136
}
137137

138-
fun getPos(t: Double, v: FloatArray) {
138+
fun getPos2(t: Float, v: FloatArray) {
139139
val n = timePoints.size
140140
val dim = mY[0].size
141141
if (mExtrapolate) {
@@ -188,7 +188,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
188188
}
189189
}
190190

191-
fun getPos(t: Double, j: Int): Double {
191+
fun getPos(t: Float, j: Int): Float {
192192
val n = timePoints.size
193193
if (mExtrapolate) {
194194
if (t <= timePoints[0]) {
@@ -219,10 +219,10 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
219219
return interpolate(h, x, y1, y2, t1, t2)
220220
}
221221
}
222-
return 0.0 // should never reach here
222+
return 0.0f // should never reach here
223223
}
224224

225-
fun getSlope(time: Double, v: DoubleArray) {
225+
fun getSlope(time: Float, v: FloatArray) {
226226
var t = time
227227
val n = timePoints.size
228228
val dim = mY[0].size
@@ -248,7 +248,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
248248
return
249249
}
250250

251-
fun getSlope(time: Double, j: Int): Double {
251+
fun getSlope(time: Float, j: Int): Float {
252252
var t = time
253253
val n = timePoints.size
254254
if (t < timePoints[0]) {
@@ -267,21 +267,21 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
267267
return diff(h, x, y1, y2, t1, t2) / h
268268
}
269269
}
270-
return 0.0 // should never reach here
270+
return 0.0f // should never reach here
271271
}
272272

273273
companion object {
274274
/**
275275
* Cubic Hermite spline
276276
*/
277277
private fun interpolate(
278-
h: Double,
279-
x: Double,
280-
y1: Double,
281-
y2: Double,
282-
t1: Double,
283-
t2: Double
284-
): Double {
278+
h: Float,
279+
x: Float,
280+
y1: Float,
281+
y2: Float,
282+
t1: Float,
283+
t2: Float
284+
): Float {
285285
val x2 = x * x
286286
val x3 = x2 * x
287287
return (-2 * x3 * y2 + 3 * x2 * y2 + 2 * x3 * y1 - 3 * x2 * y1 + y1 + h * t2 * x3 + h * t1 * x3 - h * t2 * x2 - 2 * h * t1 * x2
@@ -291,7 +291,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
291291
/**
292292
* Cubic Hermite spline slope differentiated
293293
*/
294-
private fun diff(h: Double, x: Double, y1: Double, y2: Double, t1: Double, t2: Double): Double {
294+
private fun diff(h: Float, x: Float, y1: Float, y2: Float, t1: Float, t2: Float): Float {
295295
val x2 = x * x
296296
return -6 * x2 * y2 + 6 * x * y2 + 6 * x2 * y1 - 6 * x * y1 + 3 * h * t2 * x2 + 3 * h * t1 * x2 - 2 * h * t2 * x - 4 * h * t1 * x + h * t1
297297
}

desktop/interpolationEngines/src/test/kotlin/test/MonoSplineTest.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public void unit_test_framework_working() throws Exception {
3838

3939
@Test
4040
public void testCurveFit01() throws Exception {
41-
double[][] points = {
41+
float[][] points = {
4242
{0, 0}, {1, 1}, {2, 0}
4343
};
44-
double[] time = {
44+
float[] time = {
4545
0, 5, 10
4646
};
4747
MonoSpline spline = new MonoSpline( time, Arrays.asList(points));
@@ -55,26 +55,26 @@ public void testCurveFit01() throws Exception {
5555

5656
@Test
5757
public void testMonoSpline() throws Exception {
58-
double[][] points = {
58+
float[][] points = {
5959
{0, 0},
6060
{1, 1},
6161
{1, 0},
6262
{2, 0},
6363
{2, 0},
6464
{3, 0},
6565
};
66-
double[] time = {
66+
float[] time = {
6767
0,1,2,3,4,5
6868
};
6969
MonoSpline mspline = new MonoSpline( time, Arrays.asList(points));
7070

7171

7272
assertEquals(1.0, mspline.getPos(1, 0), 0.001);
73-
assertEquals(1.0, mspline.getPos(1.1, 0), 0.001);
74-
assertEquals(1.0, mspline.getPos(1.3, 0), 0.001);
75-
assertEquals(1.0, mspline.getPos(1.6, 0), 0.001);
76-
assertEquals(1.0, mspline.getPos(1.9, 0), 0.001);
77-
assertEquals(2.0, mspline.getPos(3.5, 0), 0.001);
73+
assertEquals(1.0, mspline.getPos(1.1f, 0), 0.001);
74+
assertEquals(1.0, mspline.getPos(1.3f, 0), 0.001);
75+
assertEquals(1.0, mspline.getPos(1.6f, 0), 0.001);
76+
assertEquals(1.0, mspline.getPos(1.9f, 0), 0.001);
77+
assertEquals(2.0, mspline.getPos(3.5f, 0), 0.001);
7878
String s = plotMonoSpline(mspline, 0, 0f, 5);
7979
String expect = "|*** | 0.0\n" +
8080
"| ** |\n" +
@@ -106,16 +106,16 @@ public void testSpline() throws Exception {
106106
{3, 3},
107107
};
108108

109-
Spline mspline = new Spline( Arrays.asList(points));
109+
Spline spline = new Spline( Arrays.asList(points));
110110

111111

112-
assertEquals(0.0, mspline.getPos(0, 0), 0.001);
113-
assertEquals(0.440, mspline.getPos(.1, 0), 0.001);
114-
assertEquals(1, mspline.getPos(.3, 0), 0.001);
115-
assertEquals(1.874, mspline.getPos(.6, 0), 0.001);
116-
assertEquals(2.56, mspline.getPos(.9, 0), 0.001);
117-
assertEquals(3, mspline.getPos(1, 0), 0.001);
118-
String s = plotSpline(mspline, 0, 0f, 1);
112+
assertEquals(0.0, spline.getPos(0, 0), 0.001);
113+
assertEquals(0.440, spline.getPos(.1, 0), 0.001);
114+
assertEquals(1, spline.getPos(.3, 0), 0.001);
115+
assertEquals(1.874, spline.getPos(.6, 0), 0.001);
116+
assertEquals(2.56, spline.getPos(.9, 0), 0.001);
117+
assertEquals(3, spline.getPos(1, 0), 0.001);
118+
String s = plotSpline(spline, 0, 0f, 1);
119119
String expect =
120120
"|*** | 0.0\n" +
121121
"| *** |\n" +

0 commit comments

Comments
 (0)