1 package net.sf.openrocket.util;
3 import java.lang.reflect.Array;
5 public class ArrayUtils {
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.
11 public static double[] range(double start, double stop, double step){
13 int size = (int) Math.floor(((stop - start) / step)) + 1;
15 //System.out.println("Range from "+start+" to "+stop+" step "+step+" has length "+size);
17 double[] output = new double[size];
30 * Return the mean of an array
32 public static double mean(double[] vals){
34 for (int i = 0; i < vals.length; i++ ){
37 subtotal = subtotal / vals.length;
42 * Returns the maximum value in the array.
45 public static double max(double[] vals) {
47 for (int i = 1; i < vals.length; i++)
48 m = Math.max(m, vals[i]);
53 * Returns the minimum value in the array.
56 public static double min(double[] vals) {
58 for (int i = 1; i < vals.length; i++)
59 m = Math.min(m, vals[i]);
64 * Returns the variance of the array of doubles
66 public static double variance(double[] vals) {
67 double mu = mean(vals);
70 for (int i = 0; i < vals.length; i++){
71 temp = (mu - vals[i]);
74 return sumsq / (vals.length);
78 * Returns the standard deviation of an array of doubles
80 public static double stdev(double[] vals) {
81 return Math.sqrt(variance(vals));
85 * Returns the RMS value of an array of doubles
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 );
94 * Returns the integral of a given array calculated by the trapezoidal rule
95 * dt is the time step between each array value
97 public static double trapz(double[] y, double dt){
98 double stop = (y.length -1) * dt;
100 if (y.length <= 1 || dt <= 0) return 0;
102 double[] x = range(0, stop, dt);
105 for (int i = 1; i < x.length; i++) {
106 sum += (x[i] - x[i-1]) * (y[i] + y[i-1]);
112 * Returns the nearest value in an array to a given value
113 * Search starts from the lowest array index
115 public static double tnear(double[] range, double near, double start, double step){
116 double min = Double.POSITIVE_INFINITY;
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);
128 //System.out.println("Found nearest at i="+mini);
129 return start + (mini*step);
133 public static <T> T[] copyOf( T[] original, int length ) {
134 return copyOfRange(original,0,length);
138 * Implementation of java.util.Arrays.copyOfRange
140 * Since Froyo does not include this function it must be implemented here.
147 public static <T> T[] copyOfRange( T[] original, int start, int end ) {
149 if ( original == null ) {
150 throw new NullPointerException();
153 if ( start < 0 || start > original.length ) {
154 throw new ArrayIndexOutOfBoundsException();
158 throw new IllegalArgumentException();
161 T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
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];
176 public static double[] copyOf( double[] original, int length ) {
177 return copyOfRange(original,0,length);
180 public static double[] copyOfRange( double[] original, int start, int end ) {
182 if ( original == null ) {
183 throw new NullPointerException();
186 if ( start < 0 || start > original.length ) {
187 throw new ArrayIndexOutOfBoundsException();
191 throw new IllegalArgumentException();
194 double[] result = new double[(end-start)];
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];
209 public static byte[] copyOf( byte[] original, int length ) {
210 return copyOfRange(original,0,length);
213 public static byte[] copyOfRange( byte[] original, int start, int end ) {
215 if ( original == null ) {
216 throw new NullPointerException();
219 if ( start < 0 || start > original.length ) {
220 throw new ArrayIndexOutOfBoundsException();
224 throw new IllegalArgumentException();
227 byte[] result = new byte[(end-start)];
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];