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 manipulatedouble
andint
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DoubleUtils.ifelse
Return a value with the same shape astest
which is filled with elements selected from eitheryes
orno
depending on whether the element of test istrue
orfalse
.static class
DoubleUtils.RoundingScheme
the available schemes to round a numberstatic interface
DoubleUtils.which
Decide whether x satisfies theboolean
test.
-
Field Summary
Fields Modifier and Type Field Description static String
CSV_SEPARATOR
The default separator for CSV file parsing.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
absoluteError(double x1, double x0)
Compute the absolute difference betweenx1
andx0
.static double
bound(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 adouble
array.static int[]
collection2IntArray(Collection<Integer> integers)
Convert a collection ofInteger
s to anint
array.static long[]
collection2LongArray(Collection<Long> integers)
Convert a collection ofLong
s to along
array.static int
compare(double d1, double d2, double epsilon)
Compares twodouble
s 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 adouble
array to anint
array, rounding down if necessary.static List<Double>
doubleArray2List(double... arr)
Convert adouble
array to a list.static double
doubleMod(double x, double y)
Double modulo, analogous to x % y when x and y are ints.static boolean
equal(double[][] d1, double[][] d2, double epsilon)
Check if two 2D arrays,double[][]
, are close enough, hence equal, entry-by-entry.static boolean
equal(double[] d1, double[] d2, double epsilon)
Check if twodouble
arrays are close enough, hence equal, entry-by-entry.static boolean
equal(double d1, double d2, double epsilon)
Check if twodouble
s are close enough, hence equal.static boolean
equal(int[] d1, int[] d2)
Check if twoint
arrays,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 boolean
hasDuplicate(double[] arr, double epsilon)
Check if adouble
array contains any duplicates.static boolean
hasZero(double[] d, double epsilon)
Check if adouble
array has any 0.static double[]
ifelse(double[] arr, DoubleUtils.ifelse selection)
Return a value with the same shape astest
which is filled with elements selected from eitheryes
orno
depending on whether the element of test istrue
orfalse
.static double[]
intArray2doubleArray(int... arr)
Convert anint
array to adouble
array.static List<Integer>
intArray2List(int[] arr)
Convert anint
array to a list.static boolean
isAllZeros(double[] d, double epsilon)
Check if adouble
array contains only 0s, entry-by-entry.static boolean
isNegative(double d, double epsilon)
Check ifd
is negative.static boolean
isNumber(double x)
Check if adouble
is a number, i.e., it is not∞
orNaN
.static boolean
isPositive(double d, double epsilon)
Check ifd
is positive.static boolean
isPow2(int n)
Check if an integer is a power of 2.static boolean
isZero(double d, double epsilon)
Check ifd
is zero.static void
leftShift(double... arr)
Perform a in-memory left-shift (by 1 cell} to an array.static void
leftShift(double[] arr, int k)
Perform a in-memory right-shift (byk
cells} 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 (byk
cells) copy of an array.static int
maxIndex(boolean moveOnTies, int from, int to, double... doubles)
Get the index of the maximum of the values, skippingDouble.NaN
.static int
maxIndex(double... doubles)
Get the index of the maximum of the values, skippingDouble.NaN
.static int
minIndex(boolean moveOnTies, int from, int to, double... doubles)
Get the index of the minimum of the values, skippingDouble.NaN
.static int
minIndex(double... doubles)
Get the index of the minimum of the values, skippingDouble.NaN
.static double[]
noNaN(double[] doubles)
Remove theNaN
from 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.csv
from R) into a 1-Ddouble
array.static double[]
readCSV1d(InputStream stream, boolean hasIndex, boolean hasHeading)
Read a single-column CSV file (output bywrite.csv
from R) into a 1-Ddouble
array.static double[]
readCSV1d(InputStream stream, boolean hasIndex, boolean hasHeading, String separator)
Read a single-column CSV file (output bywrite.csv
from R) into a 1-Ddouble
array, with a given separator which overrides thedefault separator
.static double[]
readCSV1d(String fileName)
Read a single-column CSV file (output bywrite.csv
from R) into a 1-Ddouble
array.static double[]
readCSV1d(String fileName, boolean hasIndex, boolean hasHeading)
Read a single-column CSV file (output bywrite.csv
from R) into a 1-Ddouble
array.static double[]
readCSV1d(String fileName, boolean hasIndex, boolean hasHeading, String separator)
Read a single-column CSV file (output bywrite.csv
from R) into a 1-Ddouble
array, with a given separator which overrides thedefault separator
.static double[][]
readCSV2d(InputStream stream)
Read a multi-column CSV stream (output bywrite.csv
from R) into a 2-Ddouble
array.static double[][]
readCSV2d(InputStream stream, boolean hasIndex, boolean hasHeading)
Read a multi-column CSV stream (output bywrite.csv
from R) into a 2-Ddouble
array.static double[][]
readCSV2d(InputStream stream, boolean hasIndex, boolean hasHeading, String separator)
Read a multi-column CSV stream (output bywrite.csv
from R) into a 2-Ddouble
array, with a given separator which overrides thedefault separator
.static double[][]
readCSV2d(String fileName)
Read a multi-column CSV file (output bywrite.csv
from R) into a 2-Ddouble
array.static double[][]
readCSV2d(String fileName, boolean hasIndex, boolean hasHeading)
Read a multi-column CSV file (output bywrite.csv
from R) into a 2-Ddouble
array.static double[][]
readCSV2d(String fileName, boolean hasIndex, boolean hasHeading, String separator)
Read a multi-column CSV file (output bywrite.csv
from R) into a 2-Ddouble
array, with a given separator which overrides thedefault separator
.static double
relativeError(double x1, double x0)
Compute the relative error for {x1, x0}.static double[]
rep(double value, int times)
Generates an array ofdouble
s of repeated values.static int[]
rep(int value, int times)
Generates an array ofint
s of repeated values.static void
reverse(double... arr)
Reverse adouble
array.static void
reverse(int... arr)
Reverse anint
array.static double[]
reverseCopy(double... arr)
Get a reversed copy of adouble
array.static int[]
reverseCopy(int... arr)
Get a reversed copy of aint
array.static void
reverseRange(double[] array, int fromIndex, int toIndex)
Reverses a range of elements in an array.static void
rightShift(double... arr)
Perform a in-memory right-shift (by 1 cell} to an array.static void
rightShift(double[] arr, int k)
Perform a in-memory right-shift (byk
cells} 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 (byk
cells) copy of an array.static double
round(double d, int scale)
Round a number to the precision specified.static double
round(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 theboolean
test.static int[]
select(int[] arr, DoubleUtils.which test)
Select the array elements which satisfy theboolean
test.static double[]
seq(double from, double to, double inc)
Generates a sequence ofdouble
s fromfrom
up toto
with incrementsinc
.static double[]
seq(double from, double to, int n)
Generate a sequence ofn
equi-spaceddouble
values, fromstart
toend
(inclusive).static double[]
seq(int n, double start, double inc)
Generate a sequence ofdouble
values with a given start value and a given constant increment.static int[]
seq(int from, int to)
Generates a sequence ofint
s fromfrom
up toto
with increments 1.static int[]
seq(int from, int to, int inc)
Generates a sequence ofint
s fromfrom
up toto
with 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 aDouble
array to a primitivedouble
array.static String
toString(double... arr)
Print out numbers to a string.static String
toString(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 theboolean
test.static int[]
which(int[] arr, DoubleUtils.which test)
Get the indices of the array elements which satisfy theboolean
test.
-
-
-
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 twodouble
s 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
- adouble
d2
- adouble
epsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
- 0 if
d1
is close enough tod2
; 1 if d1 > d2; -1 if d1 ≤ d2
-
isZero
public static boolean isZero(double d, double epsilon)
Check ifd
is zero.- Parameters:
d
- adouble
epsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
ifd
is close enough to 0
-
isPositive
public static boolean isPositive(double d, double epsilon)
Check ifd
is positive.- Parameters:
d
- adouble
epsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
ifd
is bigger than 0 byepsilon
-
isNegative
public static boolean isNegative(double d, double epsilon)
Check ifd
is negative.- Parameters:
d
- adouble
epsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
ifd
is smaller than 0 byepsilon
-
equal
public static boolean equal(double d1, double d2, double epsilon)
Check if twodouble
s are close enough, hence equal.- Parameters:
d1
- adouble
d2
- adouble
epsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
ifd1
is close enough tod2
,false
otherwise
-
equal
public static boolean equal(double[] d1, double[] d2, double epsilon)
Check if twodouble
arrays 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:
true
if all entries ind1
are close enough to all entries ind2
,false
otherwise
-
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:
true
if all entries ind1
are close enough to all entries ind2
,false
otherwise
-
equal
public static boolean equal(int[] d1, int[] d2)
Check if twoint
arrays,int[]
, are equal, entry-by-entry.- Parameters:
d1
- anint
arrayd2
- anint
array- Returns:
true
if all entries ind1
are the same as all entries ind2
,false
otherwise
-
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
-true
if 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
-true
if 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 adouble
array.- 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 adouble
array.- Parameters:
arr
- an array,double[]
- Returns:
- the reversed copy
-
reverse
public static void reverse(int... arr)
Reverse anint
array.- Parameters:
arr
- an array,int[]
-
reverseCopy
public static int[] reverseCopy(int... arr)
Get a reversed copy of aint
array.- 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 (byk
cells) 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 (byk
cells} 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 (byk
cells) 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 (byk
cells} 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
arr
is sorted in ascending order - See Also:
- Wikipedia: Shell sort
-
isAllZeros
public static boolean isAllZeros(double[] d, double epsilon)
Check if adouble
array contains only 0s, entry-by-entry.- Parameters:
d
- adouble
arrayepsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
if all entries ind
are close enough to 0,false
otherwise
-
hasZero
public static boolean hasZero(double[] d, double epsilon)
Check if adouble
array has any 0.- Parameters:
d
- adouble
arrayepsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
if an entry ind
are close enough to 0,false
otherwise
-
isNumber
public static boolean isNumber(double x)
Check if adouble
is a number, i.e., it is not∞
orNaN
.- Parameters:
x
- adouble
- Returns:
true
if 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:
true
if n is a power of 2.
-
hasDuplicate
public static boolean hasDuplicate(double[] arr, double epsilon)
Check if adouble
array contains any duplicates.- Parameters:
arr
- adouble
arrayepsilon
- a precision parameter: when a number |x| ≤ ε, it is considered 0- Returns:
true
if there is a duplicate
-
doubleArray2intArray
public static int[] doubleArray2intArray(double... arr)
Convert adouble
array to anint
array, rounding down if necessary.- Parameters:
arr
- adouble
array- Returns:
- an
int
array
-
intArray2doubleArray
public static double[] intArray2doubleArray(int... arr)
Convert anint
array to adouble
array.- Parameters:
arr
- anint
array- Returns:
- a
double
array
-
collection2DoubleArray
public static double[] collection2DoubleArray(Collection<? extends Number> numbers)
Convert a collection of numbers to adouble
array.- Parameters:
numbers
- the collection of numbers- Returns:
- a
double
array containing the numbers in the input collection
-
doubleArray2List
public static List<Double> doubleArray2List(double... arr)
Convert adouble
array to a list.- Parameters:
arr
- adouble
array- Returns:
- a list of the numbers
-
collection2IntArray
public static int[] collection2IntArray(Collection<Integer> integers)
Convert a collection ofInteger
s to anint
array.- Parameters:
integers
- a collection of integers- Returns:
- an
int
array of the integers
-
collection2LongArray
public static long[] collection2LongArray(Collection<Long> integers)
Convert a collection ofLong
s to along
array.- Parameters:
integers
- a collection of long integers- Returns:
- a
long
array of the long integers
-
intArray2List
public static List<Integer> intArray2List(int[] arr)
Convert anint
array to a list.- Parameters:
arr
- anint
array- Returns:
- a list of the integers
-
toPrimitive
public static double[] toPrimitive(Double[] arr)
Convert aDouble
array to a primitivedouble
array.Double.NaN
is used when an inputDouble
instance isnull
.- Parameters:
arr
- aDouble
array- 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 betweenx1
andx0
. This function is symmetric.ε = | x1 - x0 |
- Parameters:
x1
-x1
x0
-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
-x1
x0
-x0
- Returns:
- the relative error
-
toString
public static String toString(double... arr)
Print out numbers to a string.- Parameters:
arr
- adouble
array- Returns:
- a
String
representation 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 ofdouble
s 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 ofint
s 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 ofdouble
values 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 ofdouble
s fromfrom
up toto
with incrementsinc
. The last number in the sequence is smaller than or equal toto
for positiveinc
. The last number in the sequence is bigger than or equal toto
for negativeinc
.- Parameters:
from
- the first number in the sequenceto
- the bound of the sequenceinc
- the increment- Returns:
- a sequence of
double
s
-
seq
public static double[] seq(double from, double to, int n)
Generate a sequence ofn
equi-spaceddouble
values, fromstart
toend
(inclusive).- Parameters:
from
- the first valueto
- the last valuen
- the total number of values- Returns:
- an array of
n
equi-spaced values
-
seq
public static int[] seq(int from, int to, int inc)
Generates a sequence ofint
s fromfrom
up toto
with incrementsinc
. The last number in the sequence is smaller than or equal toto
for positiveinc
. The last number in the sequence is bigger than or equal toto
for negativeinc
.- Parameters:
from
- the first number in the sequenceto
- the bound of the sequenceinc
- the increment- Returns:
- a sequence of
int
s
-
seq
public static int[] seq(int from, int to)
Generates a sequence ofint
s fromfrom
up toto
with increments 1. That is, [from, to], inclusively.- Parameters:
from
- the first number in the sequenceto
- the bound of the sequence- Returns:
- a sequence of
int
s
-
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 timesdiff
is 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 timesdiff
is 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 astest
which is filled with elements selected from eitheryes
orno
depending on whether the element of test istrue
orfalse
.- Parameters:
arr
- an arrayselection
- theboolean
test to decidetrue
orfalse
and 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 theboolean
test.- Parameters:
arr
- an array.double[]
test
- theboolean
test to decidetrue
orfalse
(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 theboolean
test.- Parameters:
arr
- an array.int[]
test
- theboolean
test to decidetrue
orfalse
(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 theboolean
test. R does not have a 'select' function. The R-equivalent isarr[which(...
]}.- Parameters:
arr
- an array,double[]
test
- theboolean
test 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 theboolean
test. R does not have a 'select' function. The R-equivalent isarr[which(...
]}.- Parameters:
arr
- an array,int[]
test
- theboolean
test to determine which elements to be selected- Returns:
- the satisfying elements
-
noNaN
public static double[] noNaN(double[] doubles)
Remove theNaN
from 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
-true
if 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.csv
from R) into a 2-Ddouble
array. 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.csv
from R) into a 2-Ddouble
array. Thedefault separator
is 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.csv
from R) into a 2-Ddouble
array, 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.csv
from R) into a 2-Ddouble
array. 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.csv
from R) into a 2-Ddouble
array. Thedefault separator
is 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.csv
from R) into a 2-Ddouble
array, 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.csv
from R) into a 1-Ddouble
array. 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.csv
from R) into a 1-Ddouble
array. Thedefault separator
is 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.csv
from R) into a 1-Ddouble
array, 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.csv
from R) into a 1-Ddouble
array. 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.csv
from R) into a 1-Ddouble
array. Thedefault separator
is 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.csv
from R) into a 1-Ddouble
array, 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
-
-