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 step before stop is reached.
11 public static double[] range(double start, double stop, double step){
14 if ( stop <= start ) {
17 size = (int) Math.ceil(((stop - start) / step));
20 //System.out.println("Range from "+start+" to "+stop+" step "+step+" has length "+size);
22 double[] output = new double[size];
35 * Return the mean of an array
37 public static double mean(double[] vals){
39 for (int i = 0; i < vals.length; i++ ){
42 subtotal = subtotal / vals.length;
47 * Returns the maximum value in the array.
50 public static double max(double[] vals) {
52 for (int i = 1; i < vals.length; i++)
53 m = Math.max(m, vals[i]);
58 * Returns the minimum value in the array.
61 public static double min(double[] vals) {
63 for (int i = 1; i < vals.length; i++)
64 m = Math.min(m, vals[i]);
69 * Returns the variance of the array of doubles
71 public static double variance(double[] vals) {
72 double mu = mean(vals);
75 for (int i = 0; i < vals.length; i++){
76 temp = (mu - vals[i]);
79 return sumsq / (vals.length);
83 * Returns the standard deviation of an array of doubles
85 public static double stdev(double[] vals) {
86 return Math.sqrt(variance(vals));
90 * Returns the RMS value of an array of doubles
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 );
99 * Returns the integral of a given array calculated by the trapezoidal rule
100 * dt is the time step between each array value
102 public static double trapz(double[] y, double dt){
103 double stop = y.length * dt;
105 if (y.length <= 1 || dt <= 0) return 0;
107 double[] x = range(0, stop, dt);
110 for (int i = 1; i < x.length; i++) {
111 sum += (x[i] - x[i-1]) * (y[i] + y[i-1]);
117 * Returns the nearest value in an array to a given value
118 * Search starts from the lowest array index
120 public static double tnear(double[] range, double near, double start, double step){
121 double min = Double.POSITIVE_INFINITY;
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);
133 //System.out.println("Found nearest at i="+mini);
134 return start + (mini*step);
138 public static <T> T[] copyOf( T[] original, int length ) {
139 return copyOfRange(original,0,length);
143 * Implementation of java.util.Arrays.copyOfRange
145 * Since Froyo does not include this function it must be implemented here.
152 public static <T> T[] copyOfRange( T[] original, int start, int end ) {
154 if ( original == null ) {
155 throw new NullPointerException();
158 if ( start < 0 || start > original.length ) {
159 throw new ArrayIndexOutOfBoundsException();
163 throw new IllegalArgumentException();
166 T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
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];
181 public static double[] copyOf( double[] original, int length ) {
182 return copyOfRange(original,0,length);
185 public static double[] copyOfRange( double[] original, int start, int end ) {
187 if ( original == null ) {
188 throw new NullPointerException();
191 if ( start < 0 || start > original.length ) {
192 throw new ArrayIndexOutOfBoundsException();
196 throw new IllegalArgumentException();
199 double[] result = new double[(end-start)];
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];
214 public static byte[] copyOf( byte[] original, int length ) {
215 return copyOfRange(original,0,length);
218 public static byte[] copyOfRange( byte[] original, int start, int end ) {
220 if ( original == null ) {
221 throw new NullPointerException();
224 if ( start < 0 || start > original.length ) {
225 throw new ArrayIndexOutOfBoundsException();
229 throw new IllegalArgumentException();
232 byte[] result = new byte[(end-start)];
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];