Package dev.nm.number
Class DoubleUtils
- java.lang.Object
-
- dev.nm.number.DoubleUtils
-
public final class DoubleUtils extends Object
These are the utility functions to manipulatedoubleandint.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDoubleUtils.ifelseReturn a value with the same shape astestwhich is filled with elements selected from eitheryesornodepending on whether the element of test istrueorfalse.static classDoubleUtils.RoundingSchemethe available schemes to round a numberstatic interfaceDoubleUtils.whichDecide whether x satisfies thebooleantest.
-
Field Summary
Fields Modifier and Type Field Description static StringCSV_SEPARATORThe default separator for CSV file parsing.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleabsoluteError(double x1, double x0)Compute the absolute difference betweenx1andx0.static doublebound(double value, double lower, double upper)Bounds a given value by a given range.static double[]collection2DoubleArray(Collection<? extends Number> numbers)Convert a collection of numbers to adoublearray.static int[]collection2IntArray(Collection<Integer> integers)Convert a collection ofIntegers to anintarray.static long[]collection2LongArray(Collection<Long> integers)Convert a collection ofLongs to alongarray.static intcompare(double d1, double d2, double epsilon)Compares twodoubles up to a precision.static double[]concat(double[]... arr)Concatenate an array of arrays into one array.static double[][]copy2D(double[][] raw)Copies a 2D array.static double[]cumsum(double[] arr)Gets the cumulative sums of the elements in an array.static int[]cumsum(int[] arr)Gets the cumulative sums of the elements in an array.static double[]diff(double[] arr)Gets the first differences of an array.static double[][]diff(double[][] arr)Gets the first differences of an array of vectors.static double[][]diff(double[][] arr, int lag, int order)Gets the lagged and iterated differences of vectors.static double[]diff(double[] arr, int lag, int order)Gets the lagged and iterated differences.static int[]doubleArray2intArray(double... arr)Convert adoublearray to anintarray, rounding down if necessary.static List<Double>doubleArray2List(double... arr)Convert adoublearray to a list.static doubledoubleMod(double x, double y)Double modulo, analogous to x % y when x and y are ints.static booleanequal(double[][] d1, double[][] d2, double epsilon)Check if two 2D arrays,double[][], are close enough, hence equal, entry-by-entry.static booleanequal(double[] d1, double[] d2, double epsilon)Check if twodoublearrays are close enough, hence equal, entry-by-entry.static booleanequal(double d1, double d2, double epsilon)Check if twodoubles are close enough, hence equal.static booleanequal(int[] d1, int[] d2)Check if twointarrays,int[], are equal, entry-by-entry.static double[]foreach(double[] doubles, UnivariateRealFunction f)Apply a univariate function f to each element in an array.static booleanhasDuplicate(double[] arr, double epsilon)Check if adoublearray contains any duplicates.static booleanhasZero(double[] d, double epsilon)Check if adoublearray has any 0.static double[]ifelse(double[] arr, DoubleUtils.ifelse selection)Return a value with the same shape astestwhich is filled with elements selected from eitheryesornodepending on whether the element of test istrueorfalse.static double[]intArray2doubleArray(int... arr)Convert anintarray to adoublearray.static List<Integer>intArray2List(int[] arr)Convert anintarray to a list.static booleanisAllZeros(double[] d, double epsilon)Check if adoublearray contains only 0s, entry-by-entry.static booleanisNegative(double d, double epsilon)Check ifdis negative.static booleanisNumber(double x)Check if adoubleis a number, i.e., it is not∞orNaN.static booleanisPositive(double d, double epsilon)Check ifdis positive.static booleanisPow2(int n)Check if an integer is a power of 2.static booleanisZero(double d, double epsilon)Check ifdis zero.static voidleftShift(double... arr)Perform a in-memory left-shift (by 1 cell} to an array.static voidleftShift(double[] arr, int k)Perform a in-memory right-shift (bykcells} to an array.static double[]leftShiftCopy(double... arr)Get a left shifted (by 1 cell) copy of an array.static double[]leftShiftCopy(double[] arr, int k)Get a left shifted (bykcells) copy of an array.static intmaxIndex(boolean moveOnTies, int from, int to, double... doubles)Get the index of the maximum of the values, skippingDouble.NaN.static intmaxIndex(double... doubles)Get the index of the maximum of the values, skippingDouble.NaN.static intminIndex(boolean moveOnTies, int from, int to, double... doubles)Get the index of the minimum of the values, skippingDouble.NaN.static intminIndex(double... doubles)Get the index of the minimum of the values, skippingDouble.NaN.static double[]noNaN(double[] doubles)Remove theNaNfrom an array.static int[]order(double[] arr)Returns a permutation which rearranges its first argument into ascending or descending order.static int[]order(double[] arr, boolean ascending)Returns a permutation which rearranges its first argument into ascending or descending order.static double[]readCSV1d(InputStream stream)Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray.static double[]readCSV1d(InputStream stream, boolean hasIndex, boolean hasHeading)Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray.static double[]readCSV1d(InputStream stream, boolean hasIndex, boolean hasHeading, String separator)Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray, with a given separator which overrides thedefault separator.static double[]readCSV1d(String fileName)Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray.static double[]readCSV1d(String fileName, boolean hasIndex, boolean hasHeading)Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray.static double[]readCSV1d(String fileName, boolean hasIndex, boolean hasHeading, String separator)Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray, with a given separator which overrides thedefault separator.static double[][]readCSV2d(InputStream stream)Read a multi-column CSV stream (output bywrite.csvfrom R) into a 2-Ddoublearray.static double[][]readCSV2d(InputStream stream, boolean hasIndex, boolean hasHeading)Read a multi-column CSV stream (output bywrite.csvfrom R) into a 2-Ddoublearray.static double[][]readCSV2d(InputStream stream, boolean hasIndex, boolean hasHeading, String separator)Read a multi-column CSV stream (output bywrite.csvfrom R) into a 2-Ddoublearray, with a given separator which overrides thedefault separator.static double[][]readCSV2d(String fileName)Read a multi-column CSV file (output bywrite.csvfrom R) into a 2-Ddoublearray.static double[][]readCSV2d(String fileName, boolean hasIndex, boolean hasHeading)Read a multi-column CSV file (output bywrite.csvfrom R) into a 2-Ddoublearray.static double[][]readCSV2d(String fileName, boolean hasIndex, boolean hasHeading, String separator)Read a multi-column CSV file (output bywrite.csvfrom R) into a 2-Ddoublearray, with a given separator which overrides thedefault separator.static doublerelativeError(double x1, double x0)Compute the relative error for {x1, x0}.static double[]rep(double value, int times)Generates an array ofdoubles of repeated values.static int[]rep(int value, int times)Generates an array ofints of repeated values.static voidreverse(double... arr)Reverse adoublearray.static voidreverse(int... arr)Reverse anintarray.static double[]reverseCopy(double... arr)Get a reversed copy of adoublearray.static int[]reverseCopy(int... arr)Get a reversed copy of aintarray.static voidreverseRange(double[] array, int fromIndex, int toIndex)Reverses a range of elements in an array.static voidrightShift(double... arr)Perform a in-memory right-shift (by 1 cell} to an array.static voidrightShift(double[] arr, int k)Perform a in-memory right-shift (bykcells} to an array.static double[]rightShiftCopy(double... arr)Get a right shifted (by 1 cell) copy of an array.static double[]rightShiftCopy(double[] arr, int k)Get a right shifted (bykcells) copy of an array.static doubleround(double d, int scale)Round a number to the precision specified.static doubleround(double d, DoubleUtils.RoundingScheme scheme)Round up or down a number to an integer.static double[]select(double[] arr, DoubleUtils.which test)Select the array elements which satisfy thebooleantest.static int[]select(int[] arr, DoubleUtils.which test)Select the array elements which satisfy thebooleantest.static double[]seq(double from, double to, double inc)Generates a sequence ofdoubles fromfromup totowith incrementsinc.static double[]seq(double from, double to, int n)Generate a sequence ofnequi-spaceddoublevalues, fromstarttoend(inclusive).static double[]seq(int n, double start, double inc)Generate a sequence ofdoublevalues with a given start value and a given constant increment.static int[]seq(int from, int to)Generates a sequence ofints fromfromup totowith increments 1.static int[]seq(int from, int to, int inc)Generates a sequence ofints fromfromup totowith incrementsinc.static int[]shellsort(double... arr)Sort an array using Shell sort.static double[]subarray(double[] arr, int[] indices)Get a sub-array of the original array with the given indices.static int[]subarray(int[] arr, int[] indices)Get a sub-array of the original array with the given indices.static double[]toPrimitive(Double[] arr)Convert aDoublearray to a primitivedoublearray.static StringtoString(double... arr)Print out numbers to a string.static StringtoString(double[][] arr)Print out a 2D array,double[][]to a string.static int[]which(double[] arr, DoubleUtils.which test)Get the indices of the array elements which satisfy thebooleantest.static int[]which(int[] arr, DoubleUtils.which test)Get the indices of the array elements which satisfy thebooleantest.
-
-
-
Field Detail
-
CSV_SEPARATOR
public static final String CSV_SEPARATOR
The default separator for CSV file parsing.- See Also:
- Constant Field Values
-
-
Method Detail
-
compare
public static int compare(double d1, double d2, double epsilon)Compares twodoubles up to a precision. This implementation is preferred toDouble.compare(double, double)because our implementation returns 0 (equality), i.e.,DoubleUtils.compare(0.0, -0.0)returns0;Double.compare(0.0, -0.0)returns1.- Parameters:
d1- adoubled2- adoubleepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
- 0 if
d1is close enough tod2; 1 if d1 > d2; -1 if d1 ≤ d2
-
isZero
public static boolean isZero(double d, double epsilon)Check ifdis zero.- Parameters:
d- adoubleepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueifdis close enough to 0
-
isPositive
public static boolean isPositive(double d, double epsilon)Check ifdis positive.- Parameters:
d- adoubleepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueifdis bigger than 0 byepsilon
-
isNegative
public static boolean isNegative(double d, double epsilon)Check ifdis negative.- Parameters:
d- adoubleepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueifdis smaller than 0 byepsilon
-
equal
public static boolean equal(double d1, double d2, double epsilon)Check if twodoubles are close enough, hence equal.- Parameters:
d1- adoubled2- adoubleepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueifd1is close enough tod2,falseotherwise
-
equal
public static boolean equal(double[] d1, double[] d2, double epsilon)Check if twodoublearrays are close enough, hence equal, entry-by-entry.- Parameters:
d1- adouble[]d2- adouble[]epsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueif all entries ind1are close enough to all entries ind2,falseotherwise
-
equal
public static boolean equal(double[][] d1, double[][] d2, double epsilon)Check if two 2D arrays,double[][], are close enough, hence equal, entry-by-entry.- Parameters:
d1- adouble[][]d2- adouble[][]epsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueif all entries ind1are close enough to all entries ind2,falseotherwise
-
equal
public static boolean equal(int[] d1, int[] d2)Check if twointarrays,int[], are equal, entry-by-entry.- Parameters:
d1- anintarrayd2- anintarray- Returns:
trueif all entries ind1are the same as all entries ind2,falseotherwise
-
maxIndex
public static int maxIndex(boolean moveOnTies, int from, int to, double... doubles)Get the index of the maximum of the values, skippingDouble.NaN.- Parameters:
moveOnTies-trueif prefer the later one on tiesfrom- the initial index of the range to be consideredto- 1 after the last index of the range to be considereddoubles- an array,double[]- Returns:
- the index of the biggest number
-
maxIndex
public static int maxIndex(double... doubles)
Get the index of the maximum of the values, skippingDouble.NaN.- Parameters:
doubles- an array,double[]- Returns:
- the index of the biggest number
-
minIndex
public static int minIndex(boolean moveOnTies, int from, int to, double... doubles)Get the index of the minimum of the values, skippingDouble.NaN.- Parameters:
moveOnTies-trueif prefer the later one on tiesfrom- the initial index of the range to be consideredto- 1 after the last index of the range to be considereddoubles- an array,double[]- Returns:
- the index of the smallest number
-
minIndex
public static int minIndex(double... doubles)
Get the index of the minimum of the values, skippingDouble.NaN.- Parameters:
doubles- an array,double[]- Returns:
- the index of the smallest number
-
foreach
public static double[] foreach(double[] doubles, UnivariateRealFunction f)Apply a univariate function f to each element in an array. For more complicated cases, use BruteForce.- Parameters:
doubles- an array,double[]f- a function to be applied to each element- Returns:
- the function outputs
-
concat
public static double[] concat(double[]... arr)
Concatenate an array of arrays into one array. The concatenated array is not sorted.- Parameters:
arr- an array,double[]- Returns:
- the concatenated array
-
reverse
public static void reverse(double... arr)
Reverse adoublearray.- Parameters:
arr- an array,double[]
-
reverseRange
public static void reverseRange(double[] array, int fromIndex, int toIndex)Reverses a range of elements in an array.- Parameters:
array- the arrayfromIndex- the index of the first element (inclusive)toIndex- the index of the last element (exclusive)
-
reverseCopy
public static double[] reverseCopy(double... arr)
Get a reversed copy of adoublearray.- Parameters:
arr- an array,double[]- Returns:
- the reversed copy
-
reverse
public static void reverse(int... arr)
Reverse anintarray.- Parameters:
arr- an array,int[]
-
reverseCopy
public static int[] reverseCopy(int... arr)
Get a reversed copy of aintarray.- Parameters:
arr- an array,int[]- Returns:
- the reversed copy
-
leftShiftCopy
public static double[] leftShiftCopy(double... arr)
Get a left shifted (by 1 cell) copy of an array. The rightmost element is filled by a 0.- Parameters:
arr- an array,double[]- Returns:
- a left shifted array
-
leftShiftCopy
public static double[] leftShiftCopy(double[] arr, int k)Get a left shifted (bykcells) copy of an array. The rightmost elements are filled by zeros.- Parameters:
arr- an array,double[]k- the number of cells to be shifted- Returns:
- a left shifted array
-
leftShift
public static void leftShift(double... arr)
Perform a in-memory left-shift (by 1 cell} to an array. The leftmost element is filled by a zero. No temporary array will be created during the operation.- Parameters:
arr- an array,double[]
-
leftShift
public static void leftShift(double[] arr, int k)Perform a in-memory right-shift (bykcells} to an array. The leftmost elements are filled by zeros. No temporary array will be created during the operation.- Parameters:
arr- an array,double[]k- the number of cells to be shifted
-
rightShiftCopy
public static double[] rightShiftCopy(double... arr)
Get a right shifted (by 1 cell) copy of an array. The leftmost element is filled by a 0.- Parameters:
arr- an array,double[]- Returns:
- a right shifted array
-
rightShiftCopy
public static double[] rightShiftCopy(double[] arr, int k)Get a right shifted (bykcells) copy of an array. The leftmost elements are filled by zeros.- Parameters:
arr- an array,double[]k- the number of cells to be shifted- Returns:
- a right shifted array
-
rightShift
public static void rightShift(double... arr)
Perform a in-memory right-shift (by 1 cell} to an array. The leftmost element is filled by a zero. No temporary array will be created during the operation.- Parameters:
arr- an array,double[]
-
rightShift
public static void rightShift(double[] arr, int k)Perform a in-memory right-shift (bykcells} to an array. The leftmost elements are filled by zeros. No temporary array will be created during the operation.- Parameters:
arr- an array,double[]k- the number of cells to be shifted
-
shellsort
public static int[] shellsort(double... arr)
Sort an array using Shell sort.- Parameters:
arr- an array,double[]- Returns:
- the order of the original array; side effect: the input
arris sorted in ascending order - See Also:
- Wikipedia: Shell sort
-
isAllZeros
public static boolean isAllZeros(double[] d, double epsilon)Check if adoublearray contains only 0s, entry-by-entry.- Parameters:
d- adoublearrayepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueif all entries indare close enough to 0,falseotherwise
-
hasZero
public static boolean hasZero(double[] d, double epsilon)Check if adoublearray has any 0.- Parameters:
d- adoublearrayepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueif an entry indare close enough to 0,falseotherwise
-
isNumber
public static boolean isNumber(double x)
Check if adoubleis a number, i.e., it is not∞orNaN.- Parameters:
x- adouble- Returns:
trueif x is not∞orNaN
-
isPow2
public static boolean isPow2(int n)
Check if an integer is a power of 2.- Parameters:
n- an integer- Returns:
trueif n is a power of 2.
-
hasDuplicate
public static boolean hasDuplicate(double[] arr, double epsilon)Check if adoublearray contains any duplicates.- Parameters:
arr- adoublearrayepsilon- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
trueif there is a duplicate
-
doubleArray2intArray
public static int[] doubleArray2intArray(double... arr)
Convert adoublearray to anintarray, rounding down if necessary.- Parameters:
arr- adoublearray- Returns:
- an
intarray
-
intArray2doubleArray
public static double[] intArray2doubleArray(int... arr)
Convert anintarray to adoublearray.- Parameters:
arr- anintarray- Returns:
- a
doublearray
-
collection2DoubleArray
public static double[] collection2DoubleArray(Collection<? extends Number> numbers)
Convert a collection of numbers to adoublearray.- Parameters:
numbers- the collection of numbers- Returns:
- a
doublearray containing the numbers in the input collection
-
doubleArray2List
public static List<Double> doubleArray2List(double... arr)
Convert adoublearray to a list.- Parameters:
arr- adoublearray- Returns:
- a list of the numbers
-
collection2IntArray
public static int[] collection2IntArray(Collection<Integer> integers)
Convert a collection ofIntegers to anintarray.- Parameters:
integers- a collection of integers- Returns:
- an
intarray of the integers
-
collection2LongArray
public static long[] collection2LongArray(Collection<Long> integers)
Convert a collection ofLongs to alongarray.- Parameters:
integers- a collection of long integers- Returns:
- a
longarray of the long integers
-
intArray2List
public static List<Integer> intArray2List(int[] arr)
Convert anintarray to a list.- Parameters:
arr- anintarray- Returns:
- a list of the integers
-
toPrimitive
public static double[] toPrimitive(Double[] arr)
Convert aDoublearray to a primitivedoublearray.Double.NaNis used when an inputDoubleinstance isnull.- Parameters:
arr- aDoublearray- Returns:
- the primitive array
-
round
public static double round(double d, DoubleUtils.RoundingScheme scheme)Round up or down a number to an integer.- Parameters:
d- a numberscheme- the rounding scheme- Returns:
- a near integer in
double
-
round
public static double round(double d, int scale)Round a number to the precision specified.- Parameters:
d- a numberscale- the number of decimal points- Returns:
- an approximation of the number
-
absoluteError
public static double absoluteError(double x1, double x0)Compute the absolute difference betweenx1andx0. This function is symmetric.ε = | x1 - x0 |
- Parameters:
x1-x1x0-x0- Returns:
- the absolute error
-
relativeError
public static double relativeError(double x1, double x0)Compute the relative error for {x1, x0}. This function is asymmetric.ε = | (x1 - x0) / x0 | = | x1/x0 - 1 |
- Parameters:
x1-x1x0-x0- Returns:
- the relative error
-
toString
public static String toString(double... arr)
Print out numbers to a string.- Parameters:
arr- adoublearray- Returns:
- a
Stringrepresentation of the numbers
-
toString
public static String toString(double[][] arr)
Print out a 2D array,double[][]to a string.- Parameters:
arr- adouble[][]- Returns:
- the string representation of the array
-
rep
public static double[] rep(double value, int times)Generates an array ofdoubles of repeated values.- Parameters:
value- the repeated valuetimes- the length of the array- Returns:
- an array of repetitions of value
-
rep
public static int[] rep(int value, int times)Generates an array ofints of repeated values.- Parameters:
value- the repeated valuetimes- the length of the array- Returns:
- an array of repetitions of value
-
seq
public static double[] seq(int n, double start, double inc)Generate a sequence ofdoublevalues with a given start value and a given constant increment.- Parameters:
n- the total number of valuesstart- the first valueinc- the increment- Returns:
- the sequence
-
seq
public static double[] seq(double from, double to, double inc)Generates a sequence ofdoubles fromfromup totowith incrementsinc. The last number in the sequence is smaller than or equal totofor positiveinc. The last number in the sequence is bigger than or equal totofor negativeinc.- Parameters:
from- the first number in the sequenceto- the bound of the sequenceinc- the increment- Returns:
- a sequence of
doubles
-
seq
public static double[] seq(double from, double to, int n)Generate a sequence ofnequi-spaceddoublevalues, fromstarttoend(inclusive).- Parameters:
from- the first valueto- the last valuen- the total number of values- Returns:
- an array of
nequi-spaced values
-
seq
public static int[] seq(int from, int to, int inc)Generates a sequence ofints fromfromup totowith incrementsinc. The last number in the sequence is smaller than or equal totofor positiveinc. The last number in the sequence is bigger than or equal totofor negativeinc.- Parameters:
from- the first number in the sequenceto- the bound of the sequenceinc- the increment- Returns:
- a sequence of
ints
-
seq
public static int[] seq(int from, int to)Generates a sequence ofints fromfromup totowith increments 1. That is, [from, to], inclusively.- Parameters:
from- the first number in the sequenceto- the bound of the sequence- Returns:
- a sequence of
ints
-
diff
public static double[] diff(double[] arr, int lag, int order)Gets the lagged and iterated differences.- Parameters:
arr- an array,double[]lag- an integer indicating which lag to useorder- an integer indicating the order of the difference. This is the number of timesdiffis applied to the data. E.g.,diff(x, 1, 2) = diff(diff(x, 1, 1), 1, 1).- Returns:
- the lagged and iterated differences
-
diff
public static double[] diff(double[] arr)
Gets the first differences of an array.- Parameters:
arr- an array,double[]- Returns:
- the first differences
-
diff
public static double[][] diff(double[][] arr, int lag, int order)Gets the lagged and iterated differences of vectors.- Parameters:
arr- adouble[][]; row view; must not be jaggedlag- an integer indicating which lag to useorder- an integer indicating the order of the difference. This is the number of timesdiffis applied to the data. E.g.,diff(x, 1, 2) = diff(diff(x, 1, 1), 1, 1).- Returns:
- the lagged and iterated differences
-
diff
public static double[][] diff(double[][] arr)
Gets the first differences of an array of vectors.- Parameters:
arr- adouble[][]; row view; must not be jagged- Returns:
- the first differences
-
cumsum
public static double[] cumsum(double[] arr)
Gets the cumulative sums of the elements in an array.- Parameters:
arr- an array,double[]- Returns:
cumsum
-
cumsum
public static int[] cumsum(int[] arr)
Gets the cumulative sums of the elements in an array.- Parameters:
arr- an array,int[]- Returns:
cumsum
-
ifelse
public static double[] ifelse(double[] arr, DoubleUtils.ifelse selection)Return a value with the same shape astestwhich is filled with elements selected from eitheryesornodepending on whether the element of test istrueorfalse.- Parameters:
arr- an arrayselection- thebooleantest to decidetrueorfalseand return values- Returns:
- "yes" or "no" return values
-
which
public static int[] which(double[] arr, DoubleUtils.which test)Get the indices of the array elements which satisfy thebooleantest.- Parameters:
arr- an array.double[]test- thebooleantest to decidetrueorfalse(which to select)- Returns:
- the indices of the satisfying elements
-
which
public static int[] which(int[] arr, DoubleUtils.which test)Get the indices of the array elements which satisfy thebooleantest.- Parameters:
arr- an array.int[]test- thebooleantest to decidetrueorfalse(which to select)- Returns:
- the indices of the satisfying elements
-
select
public static double[] select(double[] arr, DoubleUtils.which test)Select the array elements which satisfy thebooleantest. R does not have a 'select' function. The R-equivalent isarr[which(...]}.- Parameters:
arr- an array,double[]test- thebooleantest to determine which elements to be selected- Returns:
- the satisfying elements
-
select
public static int[] select(int[] arr, DoubleUtils.which test)Select the array elements which satisfy thebooleantest. R does not have a 'select' function. The R-equivalent isarr[which(...]}.- Parameters:
arr- an array,int[]test- thebooleantest to determine which elements to be selected- Returns:
- the satisfying elements
-
noNaN
public static double[] noNaN(double[] doubles)
Remove theNaNfrom an array.- Parameters:
doubles- an array,double[]- Returns:
- the same array but with NaNs removed
-
subarray
public static double[] subarray(double[] arr, int[] indices)Get a sub-array of the original array with the given indices. The R-equivalent isarr[indices].- Parameters:
arr- an array,double[]indices- an array of indices to select- Returns:
arr[indices].
-
subarray
public static int[] subarray(int[] arr, int[] indices)Get a sub-array of the original array with the given indices. The R-equivalent isarr[indices].- Parameters:
arr- an array,int[]indices- an array of indices to select- Returns:
arr[indices].
-
order
public static int[] order(double[] arr, boolean ascending)Returns a permutation which rearranges its first argument into ascending or descending order. The R-equivalent isorder(...}.- Parameters:
arr- an array,int[]ascending-trueif arranging elements in ascending order; false if descending order- Returns:
- the order of the original array
-
order
public static int[] order(double[] arr)
Returns a permutation which rearranges its first argument into ascending or descending order. The R-equivalent isorder(...}.- Parameters:
arr- an array,int[]- Returns:
- the order of the original array
-
readCSV2d
public static double[][] readCSV2d(String fileName) throws IOException
Read a multi-column CSV file (output bywrite.csvfrom R) into a 2-Ddoublearray. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use thedefault separator.- Parameters:
fileName- the name of the CSV file- Returns:
- the numbers in the CSV file as a
double[][] - Throws:
IOException- when error has occurred while reading the file
-
readCSV2d
public static double[][] readCSV2d(String fileName, boolean hasIndex, boolean hasHeading) throws IOException
Read a multi-column CSV file (output bywrite.csvfrom R) into a 2-Ddoublearray. Thedefault separatoris used.- Parameters:
fileName- the name of the CSV filehasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading row- Returns:
- the numbers in the CSV file as a
double[][] - Throws:
IOException- when error has occurred while reading the file
-
readCSV2d
public static double[][] readCSV2d(String fileName, boolean hasIndex, boolean hasHeading, String separator) throws IOException
Read a multi-column CSV file (output bywrite.csvfrom R) into a 2-Ddoublearray, with a given separator which overrides thedefault separator.- Parameters:
fileName- the name of the CSV filehasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading rowseparator- the separator- Returns:
- the numbers in the CSV file as a
double[][] - Throws:
IOException- when error has occurred while reading the file
-
readCSV2d
public static double[][] readCSV2d(InputStream stream) throws IOException
Read a multi-column CSV stream (output bywrite.csvfrom R) into a 2-Ddoublearray. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use thedefault separator.- Parameters:
stream- the CSV input stream- Returns:
- the numbers in the CSV file as a
double[][] - Throws:
IOException- when error has occurred while reading the file
-
readCSV2d
public static double[][] readCSV2d(InputStream stream, boolean hasIndex, boolean hasHeading) throws IOException
Read a multi-column CSV stream (output bywrite.csvfrom R) into a 2-Ddoublearray. Thedefault separatoris used.- Parameters:
stream- the CSV input streamhasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading row- Returns:
- the numbers in the CSV file as a
double[][] - Throws:
IOException- when error has occurred while reading the file
-
readCSV2d
public static double[][] readCSV2d(InputStream stream, boolean hasIndex, boolean hasHeading, String separator) throws IOException
Read a multi-column CSV stream (output bywrite.csvfrom R) into a 2-Ddoublearray, with a given separator which overrides thedefault separator.- Parameters:
stream- the CSV input streamhasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading rowseparator- the separator- Returns:
- the numbers in the CSV file as a
double[][] - Throws:
IOException- when error has occurred while reading the file
-
readCSV1d
public static double[] readCSV1d(String fileName) throws IOException
Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use thedefault separator.- Parameters:
fileName- the name of the CSV file- Returns:
- the numbers in the CSV file as a
double[] - Throws:
IOException- when error has occurred while reading the file
-
readCSV1d
public static double[] readCSV1d(String fileName, boolean hasIndex, boolean hasHeading) throws IOException
Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray. Thedefault separatoris used.- Parameters:
fileName- the name of the CSV filehasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading row- Returns:
- the numbers in the CSV file as a
double[] - Throws:
IOException- when error has occurred while reading the file
-
readCSV1d
public static double[] readCSV1d(String fileName, boolean hasIndex, boolean hasHeading, String separator) throws IOException
Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray, with a given separator which overrides thedefault separator.- Parameters:
fileName- the name of the CSV filehasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading rowseparator- the separator- Returns:
- the numbers in the CSV file as a
double[] - Throws:
IOException- when error has occurred while reading the file
-
readCSV1d
public static double[] readCSV1d(InputStream stream) throws IOException
Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray. By default, the CSV file contains both the index column (as the first column) and the heading row (as the first row), and use thedefault separator.- Parameters:
stream- the CSV input stream- Returns:
- the numbers in the CSV file as a
double[] - Throws:
IOException- when error has occurred while reading the file
-
readCSV1d
public static double[] readCSV1d(InputStream stream, boolean hasIndex, boolean hasHeading) throws IOException
Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray. Thedefault separatoris used.- Parameters:
stream- the CSV input streamhasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading row- Returns:
- the numbers in the CSV file as a
double[] - Throws:
IOException- when error has occurred while reading the file
-
readCSV1d
public static double[] readCSV1d(InputStream stream, boolean hasIndex, boolean hasHeading, String separator) throws IOException
Read a single-column CSV file (output bywrite.csvfrom R) into a 1-Ddoublearray, with a given separator which overrides thedefault separator.- Parameters:
stream- the CSV input streamhasIndex- whether or not the file contains an index columnhasHeading- whether or not the file contains a heading rowseparator- the separator- Returns:
- the numbers in the CSV file as a
double[] - Throws:
IOException- when error has occurred while reading the file
-
copy2D
public static double[][] copy2D(double[][] raw)
Copies a 2D array.- Parameters:
raw- a 2D array- Returns:
- a copy of the a 2D array
-
doubleMod
public static double doubleMod(double x, double y)Double modulo, analogous to x % y when x and y are ints.- Parameters:
x- a doubley- a double- Returns:
- x % y
-
bound
public static double bound(double value, double lower, double upper)Bounds a given value by a given range. This is equivalent toMath.min(Math.max(value, lower), upper)- Parameters:
value- the original valuelower- the lower bound of the rangeupper- the upper bound of the range- Returns:
- the bounded value
-
-