Big update to custom expression feature.
[debian/openrocket] / core / src / net / sf / openrocket / util / ArrayUtils.java
index 67209f323ea4a0db5b5fa014cfdfab96c15744c4..59290b12d9c8fd48401436ad62bf93dffc2330f4 100644 (file)
@@ -4,6 +4,132 @@ import java.lang.reflect.Array;
 
 public class ArrayUtils {
 
+       /**
+        * Returns a double array with values from start to end with given step.
+        * Starts exactly at start and stops at the step before stop is reached. 
+        */
+       public static double[] range(double start, double stop, double step){
+               
+               int size = (int) Math.floor(((stop - start) / step));
+               
+               //System.out.println("Range from "+start+" to "+stop+" step "+step+" has length "+size);
+               
+               double[] output = new double[size];
+               int i = 0;
+               double x = start;
+               while (i<size){
+                       output[i] = x;
+                       x = x+step;
+                       i++;
+               }
+               
+               return output;
+       }
+
+       /**
+        * Return the mean of an array
+        */
+       public static double mean(double[] vals){
+               double subtotal = 0;
+               for (int i = 0; i < vals.length; i++ ){
+                       subtotal += vals[i];
+               }
+               subtotal = subtotal / vals.length;
+               return subtotal;
+       }
+
+       /**
+        * Returns the maximum value in the array.
+        */
+
+       public static double max(double[] vals) {
+               double m = vals[0];
+               for (int i = 1; i < vals.length; i++)
+                       m = Math.max(m, vals[i]);
+               return m;
+       }
+
+       /**
+        * Returns the minimum value in the array.
+        */
+
+       public static double min(double[] vals) {
+               double m = vals[0];
+               for (int i = 1; i < vals.length; i++)
+                       m = Math.min(m, vals[i]);
+               return m;
+       }
+
+       /**
+        * Returns the variance of the array of doubles
+        */
+       public static double variance(double[] vals) {
+               double mu = mean(vals);
+               double sumsq = 0.0;
+               double temp = 0;
+               for (int i = 0; i < vals.length; i++){
+                       temp = (mu - vals[i]);
+                       sumsq += temp*temp;
+               }
+               return sumsq / (vals.length);
+       }
+
+       /**
+        * Returns the standard deviation of an array of doubles
+        */
+       public static double stdev(double[] vals) {
+               return Math.sqrt(variance(vals));
+       }
+       
+       /**
+        * Returns the RMS value of an array of doubles 
+        */
+       public static double rms(double[] vals) {
+               double m = mean(vals);
+               double s = stdev(vals);
+               return Math.sqrt( m*m + s*s );
+       }
+       
+       /**
+        * Returns the integral of a given array calculated by the trapezoidal rule
+        * dt is the time step between each array value
+        */
+       public static double trapz(double[] y, double dt){
+               double stop = y.length * dt;
+               
+               if (y.length <= 1 || dt <= 0) return 0;
+               
+               double[] x = range(0, stop, dt);
+           
+           double sum = 0.0;
+           for (int i = 1; i < x.length; i++) {
+               sum += (x[i] - x[i-1]) * (y[i] + y[i-1]);
+           }
+           return sum * 0.5;
+       }
+       
+       /**
+        * Returns the nearest value in an array to a given value
+        * Search starts from the lowest array index
+        */
+       public static double tnear(double[] range, double near, double start, double step){
+               double min = Double.POSITIVE_INFINITY;
+               int mini = 0;
+               
+               //System.out.println("Nearest to "+near+" in range length "+range.length);
+               for (int i=0; i < range.length; i++){
+                       double x = Math.abs(range[i] - near);
+                       if (x < min){
+                               min = x;
+                               mini = i;
+                       }
+               }
+               
+               //System.out.println("Found nearest at i="+mini);               
+               return start + (mini*step);
+       }
+       
+       
        public static <T> T[] copyOf( T[] original, int length ) {
                return copyOfRange(original,0,length);
        }
@@ -114,3 +240,4 @@ public class ArrayUtils {
        }
 
 }
+