1 package org.catacomb.numeric.function;
2
3 import org.catacomb.report.E;
4
5
6 public class HoldLastScalarFunction implements ScalarFunction {
7
8 int ntv;
9 double[] times;
10 double[] values;
11
12 int itv;
13
14
15 public HoldLastScalarFunction(double t, double v) {
16 times = new double[4];
17 values = new double[4];
18 times[0] = t;
19 values[0] = v;
20 ntv = 1;
21 }
22
23
24 public void addTransition(double t, double v) {
25 if (t < times[ntv-1]) {
26 E.missing("cant add prior transition");
27 } else {
28 if (ntv == times.length) {
29 double[] tt = new double[2 * times.length];
30 double[] vv = new double[2 * values.length];
31 for (int i = 0; i < ntv; i++) {
32 tt[i] = times[i];
33 vv[i] = values[i];
34 }
35 times = tt;
36 values = vv;
37 }
38
39 times[ntv] = t;
40 values[ntv] =v;
41 ntv += 1;
42 }
43 }
44
45
46 public void fixate() {
47 addTransition(1.e99, values[ntv-1]);
48
49 itv = 0;
50
51
52
53
54
55
56
57 }
58
59
60
61 public double getScalar(double t) {
62 if (t >= times[itv] && t < times[itv+1]) {
63
64
65 } else if (t < times[itv]) {
66 itv = 0;
67 while (itv < ntv-1 && t >= times[itv+1]) {
68 itv++;
69 }
70
71 } else {
72 while (itv < ntv-1 && t >= times[itv+1]) {
73 itv++;
74
75 }
76 }
77
78 return values[itv];
79 }
80
81
82
83
84
85 }