/** * max * @param values values in array * @return max */ public static double max( double[] values ) { return max(values, 0, values.length); }
/** * Mean * * @return mean */ public double mean() { return Dbl.mean(values, end); }
/** * Calculate Median * @param values values * @return median */ public static double median(double[] values) { return median(values, 0, values.length); }
public static double[] insert( final double[] array, final int fromIndex, final double[] values ) { if ( fromIndex >= array.length ) { return add( array, values ); } final int index = calculateIndex( array, fromIndex ); //Object newArray = Array.newInstance(array.getClass().getComponentType(), array.length+1); double[] newArray = new double[ array.length + values.length ]; if ( index != 0 ) { /* Copy up to the length in the array before the index. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, 0, newArray, 0, index ); } boolean lastIndex = index == array.length - 1; int toIndex = index + values.length; int remainingIndex = newArray.length - toIndex; if ( lastIndex ) { /* Copy the area after the insert. Make sure we don't write over the end. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, index, newArray, index + values.length, remainingIndex ); } else { /* Copy the area after the insert. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, index, newArray, index + values.length, remainingIndex ); } for ( int i = index, j = 0; i < toIndex; i++, j++ ) { newArray[ i ] = values[ j ]; } return newArray; }
/** * Sum * @param values values in int * @return sum */ public static double sum( double[] values ) { return sum( values, 0, values.length); }
/** * min * * @return min */ public double min() { return Dbl.min(values, end); }
/** * Reduce by functional support for int arrays. * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> double reduceBy( final double[] array, T object ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object ); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object); MethodHandle methodHandle = callSite.dynamicInvoker(); try { double sum = 0; for ( double v : array ) { sum = (double) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return Exceptions.handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object); } }
public static double[] slc( double[] array, int startIndex, int endIndex ) { final int start = calculateIndex( array, startIndex ); final int end = calculateEndIndex(array, endIndex); final int newLength = end - start; if ( newLength < 0 ) { throw new ArrayIndexOutOfBoundsException( String.format( "start index %d, end index %d, length %d", startIndex, endIndex, array.length ) ); } double[] newArray = new double[ newLength ]; System.arraycopy( array, start, newArray, 0, newLength ); return newArray; }
public static double idx( final double[] array, final int index ) { final int i = calculateIndex( array, index ); return array[ i ]; }
public static double[] slcEnd( final double[] array, final int endIndex ) { final int end = calculateEndIndex(array, endIndex); final int newLength = end; // + (endIndex < 0 ? 1 : 0); if ( newLength < 0 ) { throw new ArrayIndexOutOfBoundsException( String.format( "start index %d, length %d", endIndex, array.length ) ); } double[] newArray = new double[ newLength ]; System.arraycopy( array, 0, newArray, 0, newLength ); return newArray; }
public static double[] sliceOf( double[] array, int startIndex, int endIndex ) { return slc(array, startIndex, endIndex); }
public static double[] insert( final double[] array, final int fromIndex, final double[] values ) { if ( fromIndex >= array.length ) { return add( array, values ); } final int index = calculateIndex( array, fromIndex ); //Object newArray = Array.newInstance(array.getClass().getComponentType(), array.length+1); double[] newArray = new double[ array.length + values.length ]; if ( index != 0 ) { /* Copy up to the length in the array before the index. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, 0, newArray, 0, index ); } boolean lastIndex = index == array.length - 1; int toIndex = index + values.length; int remainingIndex = newArray.length - toIndex; if ( lastIndex ) { /* Copy the area after the insert. Make sure we don't write over the end. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, index, newArray, index + values.length, remainingIndex ); } else { /* Copy the area after the insert. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, index, newArray, index + values.length, remainingIndex ); } for ( int i = index, j = 0; i < toIndex; i++, j++ ) { newArray[ i ] = values[ j ]; } return newArray; }
/** * Sum * @param values values in int * @return sum */ public static double sum( double[] values ) { return sum( values, 0, values.length); }
/** * Min * @param values values in int * @param length length * @return min */ public static double min( double[] values, int length ) { return min(values, 0, length); }
/** * Reduce By * @param array array of items to reduce by * @param length where to end in the array * @param object function * @return reduction */ public static double reduceBy( final double[] array, int length, Object object ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, length, object ); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object ); MethodHandle methodHandle = callSite.dynamicInvoker(); try { double sum = 0; for (int index=0; index < length; index++) { double v = array[index]; sum = (double) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return Exceptions.handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, length, object ); } }
public static double[] slc( double[] array, int startIndex, int endIndex ) { final int start = calculateIndex( array, startIndex ); final int end = calculateEndIndex(array, endIndex); final int newLength = end - start; if ( newLength < 0 ) { throw new ArrayIndexOutOfBoundsException( String.format( "start index %d, end index %d, length %d", startIndex, endIndex, array.length ) ); } double[] newArray = new double[ newLength ]; System.arraycopy( array, start, newArray, 0, newLength ); return newArray; }
public static double idx( final double[] array, final int index ) { final int i = calculateIndex( array, index ); return array[ i ]; }
public static double[] slcEnd( final double[] array, final int endIndex ) { final int end = calculateEndIndex(array, endIndex); final int newLength = end; // + (endIndex < 0 ? 1 : 0); if ( newLength < 0 ) { throw new ArrayIndexOutOfBoundsException( String.format( "start index %d, length %d", endIndex, array.length ) ); } double[] newArray = new double[ newLength ]; System.arraycopy( array, 0, newArray, 0, newLength ); return newArray; }
public static double[] sliceOf( double[] array, int startIndex, int endIndex ) { return slc(array, startIndex, endIndex); }
public static double[] insert( final double[] array, final int idx, final double v ) { Exceptions.requireNonNull( array ); if ( idx >= array.length ) { return add( array, v ); } final int index = calculateIndex( array, idx ); //Object newArray = Array.newInstance(array.getClass().getComponentType(), array.length+1); double[] newArray = new double[ array.length + 1 ]; if ( index != 0 ) { /* Copy up to the length in the array before the index. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, 0, newArray, 0, index ); } boolean lastIndex = index == array.length - 1; int remainingIndex = array.length - index; if ( lastIndex ) { /* Copy the area after the insert. Make sure we don't write over the end. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, index, newArray, index + 1, remainingIndex ); } else { /* Copy the area after the insert. */ /* src sbegin dst dbegin length of copy */ System.arraycopy( array, index, newArray, index + 1, remainingIndex ); } newArray[ index ] = v; return newArray; }