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