7d373096a3cae4930e878b4f47de9521f0137628
[debian/openrocket] / core / src / net / sf / openrocket / util / ArrayUtils.java
1 package net.sf.openrocket.util;
2
3 import java.lang.reflect.Array;
4
5 public class ArrayUtils {
6
7         /**
8          * Returns a double array with values from start to end with given step.
9          * Starts exactly at start and stops at the multiple of step <= stop. 
10          */
11         public static double[] range(double start, double stop, double step){
12                 
13                 int size = (int) Math.floor(((stop - start) / step)) + 1;
14                 
15                 //System.out.println("Range from "+start+" to "+stop+" step "+step+" has length "+size);
16                 
17                 double[] output = new double[size];
18                 int i = 0;
19                 double x = start;
20                 while (i<size){
21                         output[i] = x;
22                         x = x+step;
23                         i++;
24                 }
25                 
26                 return output;
27         }
28
29         /**
30          * Return the mean of an array
31          */
32         public static double mean(double[] vals){
33                 double subtotal = 0;
34                 for (int i = 0; i < vals.length; i++ ){
35                         subtotal += vals[i];
36                 }
37                 subtotal = subtotal / vals.length;
38                 return subtotal;
39         }
40
41         /**
42          * Returns the maximum value in the array.
43          */
44
45         public static double max(double[] vals) {
46                 double m = vals[0];
47                 for (int i = 1; i < vals.length; i++)
48                         m = Math.max(m, vals[i]);
49                 return m;
50         }
51
52         /**
53          * Returns the minimum value in the array.
54          */
55
56         public static double min(double[] vals) {
57                 double m = vals[0];
58                 for (int i = 1; i < vals.length; i++)
59                         m = Math.min(m, vals[i]);
60                 return m;
61         }
62
63         /**
64          * Returns the variance of the array of doubles
65          */
66         public static double variance(double[] vals) {
67                 double mu = mean(vals);
68                 double sumsq = 0.0;
69                 double temp = 0;
70                 for (int i = 0; i < vals.length; i++){
71                         temp = (mu - vals[i]);
72                         sumsq += temp*temp;
73                 }
74                 return sumsq / (vals.length);
75         }
76
77         /**
78          * Returns the standard deviation of an array of doubles
79          */
80         public static double stdev(double[] vals) {
81                 return Math.sqrt(variance(vals));
82         }
83         
84         /**
85          * Returns the RMS value of an array of doubles 
86          */
87         public static double rms(double[] vals) {
88                 double m = mean(vals);
89                 double s = stdev(vals);
90                 return Math.sqrt( m*m + s*s );
91         }
92         
93         /**
94          * Returns the integral of a given array calculated by the trapezoidal rule
95          * dt is the time step between each array value
96          */
97         public static double trapz(double[] y, double dt){
98                 double stop = (y.length -1) * dt;
99                 
100                 if (y.length <= 1 || dt <= 0) return 0;
101                 
102                 double[] x = range(0, stop, dt);
103             
104             double sum = 0.0;
105             for (int i = 1; i < x.length; i++) {
106                 sum += (x[i] - x[i-1]) * (y[i] + y[i-1]);
107             }
108             return sum * 0.5;
109         }
110         
111         /**
112          * Returns the nearest value in an array to a given value
113          * Search starts from the lowest array index
114          */
115         public static double tnear(double[] range, double near, double start, double step){
116                 double min = Double.POSITIVE_INFINITY;
117                 int mini = 0;
118                 
119                 //System.out.println("Nearest to "+near+" in range length "+range.length);
120                 for (int i=0; i < range.length; i++){
121                         double x = Math.abs(range[i] - near);
122                         if (x < min){
123                                 min = x;
124                                 mini = i;
125                         }
126                 }
127                 
128                 //System.out.println("Found nearest at i="+mini);               
129                 return start + (mini*step);
130         }
131         
132         
133         public static <T> T[] copyOf( T[] original, int length ) {
134                 return copyOfRange(original,0,length);
135         }
136         
137         /**
138          * Implementation of java.util.Arrays.copyOfRange
139          * 
140          * Since Froyo does not include this function it must be implemented here.
141          * 
142          * @param original
143          * @param start
144          * @param end
145          * @return
146          */
147         public static <T> T[] copyOfRange( T[] original, int start, int end ) {
148                 
149                 if ( original == null ) {
150                         throw new NullPointerException();
151                 }
152                 
153                 if ( start < 0 || start > original.length ) {
154                         throw new ArrayIndexOutOfBoundsException();
155                 }
156                 
157                 if ( start > end ) {
158                         throw new IllegalArgumentException();
159                 }
160                 
161                 T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
162                 
163                 int index = 0;
164                 int stop = original.length < end ? original.length : end;
165                 for ( int i = start; i < stop; i ++ ) {
166                         if ( i < original.length ) {
167                                 result[index] = original[i];
168                         }
169                         index++;
170                 }
171                 
172                 return result;
173                 
174         }
175
176         public static double[] copyOf( double[] original, int length ) {
177                 return copyOfRange(original,0,length);
178         }
179         
180         public static double[] copyOfRange( double[] original, int start, int end ) {
181                 
182                 if ( original == null ) {
183                         throw new NullPointerException();
184                 }
185                 
186                 if ( start < 0 || start > original.length ) {
187                         throw new ArrayIndexOutOfBoundsException();
188                 }
189                 
190                 if ( start > end ) {
191                         throw new IllegalArgumentException();
192                 }
193                 
194                 double[] result = new double[(end-start)];
195                 
196                 int index = 0;
197                 int stop = original.length < end ? original.length : end;
198                 for ( int i = start; i < stop; i ++ ) {
199                         if ( i < original.length ) {
200                                 result[index] = original[i];
201                         }
202                         index++;
203                 }
204                 
205                 return result;
206                 
207         }
208
209         public static byte[] copyOf( byte[] original, int length ) {
210                 return copyOfRange(original,0,length);
211         }
212         
213         public static byte[] copyOfRange( byte[] original, int start, int end ) {
214                 
215                 if ( original == null ) {
216                         throw new NullPointerException();
217                 }
218                 
219                 if ( start < 0 || start > original.length ) {
220                         throw new ArrayIndexOutOfBoundsException();
221                 }
222                 
223                 if ( start > end ) {
224                         throw new IllegalArgumentException();
225                 }
226                 
227                 byte[] result = new byte[(end-start)];
228                 
229                 int index = 0;
230                 int stop = original.length < end ? original.length : end;
231                 for ( int i = start; i < stop; i ++ ) {
232                         if ( i < original.length ) {
233                                 result[index] = original[i];
234                         }
235                         index++;
236                 }
237                 
238                 return result;
239                 
240         }
241
242 }
243