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++ ){
35 if (!Double.isNaN(vals[i])){
39 subtotal = subtotal / vals.length;
44 * Returns the maximum value in the array.
47 public static double max(double[] vals) {
49 for (int i = 1; i < vals.length; i++)
50 m = Math.max(m, vals[i]);
55 * Returns the minimum value in the array.
58 public static double min(double[] vals) {
60 for (int i = 1; i < vals.length; i++)
61 m = Math.min(m, vals[i]);
66 * Returns the variance of the array of doubles
68 public static double variance(double[] vals) {
69 double mu = mean(vals);
72 for (int i = 0; i < vals.length; i++){
73 if (!Double.isNaN(vals[i])){
74 temp = (mu - vals[i]);
78 return sumsq / (vals.length);
82 * Returns the standard deviation of an array of doubles
84 public static double stdev(double[] vals) {
85 return Math.sqrt(variance(vals));
89 * Returns the RMS value of an array of doubles
91 public static double rms(double[] vals) {
92 double m = mean(vals);
93 double s = stdev(vals);
94 return Math.sqrt( m*m + s*s );
98 * Returns the integral of a given array calculated by the trapezoidal rule
99 * dt is the time step between each array value. Any NaN values are treated as zero
101 public static double trapz(double[] y, double dt){
102 double stop = (y.length -1) * dt;
104 if (y.length <= 1 || dt <= 0) return 0;
106 double[] x = range(0, stop, dt);
109 for (int i = 1; i < x.length; i++) {
110 double temp = (x[i] - x[i-1]) * (y[i] + y[i-1]);
111 if (!Double.isNaN(temp)){
119 * Returns the nearest value in an array to a given value
120 * Search starts from the lowest array index
122 public static double tnear(double[] range, double near, double start, double step){
123 double min = Double.POSITIVE_INFINITY;
126 //System.out.println("Nearest to "+near+" in range length "+range.length);
127 for (int i=0; i < range.length; i++){
128 double x = Math.abs(range[i] - near);
135 //System.out.println("Found nearest at i="+mini);
136 return start + (mini*step);
140 public static <T> T[] copyOf( T[] original, int length ) {
141 return copyOfRange(original,0,length);
145 * Implementation of java.util.Arrays.copyOfRange
147 * Since Froyo does not include this function it must be implemented here.
154 public static <T> T[] copyOfRange( T[] original, int start, int end ) {
156 if ( original == null ) {
157 throw new NullPointerException();
160 if ( start < 0 || start > original.length ) {
161 throw new ArrayIndexOutOfBoundsException();
165 throw new IllegalArgumentException();
168 T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
171 int stop = original.length < end ? original.length : end;
172 for ( int i = start; i < stop; i ++ ) {
173 if ( i < original.length ) {
174 result[index] = original[i];
183 public static double[] copyOf( double[] original, int length ) {
184 return copyOfRange(original,0,length);
187 public static double[] copyOfRange( double[] original, int start, int end ) {
189 if ( original == null ) {
190 throw new NullPointerException();
193 if ( start < 0 || start > original.length ) {
194 throw new ArrayIndexOutOfBoundsException();
198 throw new IllegalArgumentException();
201 double[] result = new double[(end-start)];
204 int stop = original.length < end ? original.length : end;
205 for ( int i = start; i < stop; i ++ ) {
206 if ( i < original.length ) {
207 result[index] = original[i];
216 public static byte[] copyOf( byte[] original, int length ) {
217 return copyOfRange(original,0,length);
220 public static byte[] copyOfRange( byte[] original, int start, int end ) {
222 if ( original == null ) {
223 throw new NullPointerException();
226 if ( start < 0 || start > original.length ) {
227 throw new ArrayIndexOutOfBoundsException();
231 throw new IllegalArgumentException();
234 byte[] result = new byte[(end-start)];
237 int stop = original.length < end ? original.length : end;
238 for ( int i = start; i < stop; i ++ ) {
239 if ( i < original.length ) {
240 result[index] = original[i];