Package org.nd4j.common.util
Class MathUtils
- java.lang.Object
-
- org.nd4j.common.util.MathUtils
-
public class MathUtils extends Object
-
-
Constructor Summary
Constructors Constructor Description MathUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static double
adjustedrSquared(double rSquared, int numRegressors, int numDataPoints)
This calculates the adjusted r^2 including degrees of freedom.static double
bernoullis(double n, double k, double successProb)
This will return the bernoulli trial for the given event.static int
binomial(org.apache.commons.math3.random.RandomGenerator rng, int n, double p)
Generates a binomial distributed number using the given rngstatic int
clamp(int value, int min, int max)
Clamps the value to a discrete valuestatic double
combination(double n, double r)
This returns the combination of n choose rstatic List<double[]>
coordSplit(double[] vector)
This returns the coordinate split in a list of coordinates such that the values for ret[0] are the x values and ret[1] are the y valuesstatic List<double[]>
coordSplit(List<Double> vector)
This returns the coordinate split in a list of coordinates such that the values for ret[0] are the x values and ret[1] are the y valuesstatic double
correlation(double[] residuals, double[] targetAttribute)
Returns the correlation coefficient of two double vectors.static double
determinationCoefficient(double[] y1, double[] y2, int n)
This returns the determination coefficient of two vectors given a lengthstatic int
discretize(double value, double min, double max, int binCount)
Discretize the given valuestatic int
distanceFinderZValue(double[] vector)
This will translate a vector in to an equivalent integerstatic double
entropy(double[] vector)
This returns the entropy (information gain, or uncertainty of a random variable): -sum(x*log(x))static double
errorFor(double actual, double prediction)
static double
euclideanDistance(double[] p, double[] q)
This returns the euclidean distance of two vectors sum(i=1,n) (q_i - p_i)^2static double
euclideanDistance(float[] p, float[] q)
This returns the euclidean distance of two vectors sum(i=1,n) (q_i - p_i)^2static double
factorial(double n)
This will return the factorial of the given number n.static double[]
fromString(String data, String separator)
This will take a given string and separator and convert it to an equivalent double array.static double[]
generateUniform(int l)
This will generate a series of uniformally distributed numbers between l timesstatic boolean
gr(double a, double b)
Tests if a is greater than b.static int
hashCode(double value)
hashCode method, taken from Java 1.8 Double.hashCode(double) methodstatic double
hypotenuse(double a, double b)
sqrt(a^2 + b^2) without under/overflow.static double
idf(double totalDocs, double numTimesWordAppearedInADocument)
Inverse document frequency: the total docs divided by the number of times the word appeared in a documentstatic double
information(double[] probabilities)
This returns the entropy for a given vector of probabilities.static int
kroneckerDelta(double i, double j)
This returns the kronecker delta of two doubles.static double
log2(double a)
Returns the logarithm of a for base 2.static double[]
logs2probs(double[] a)
Converts an array containing the natural logarithms of probabilities stored in a vector back into probabilities.static double
manhattanDistance(double[] p, double[] q)
This will calculate the Manhattan distance between two sets of points.static double
max(double[] doubles)
static int
maxIndex(double[] doubles)
Returns index of maximum element in a given array of doubles.static double
mean(double[] vector)
Computes the mean for an array of doubles.static double[]
mergeCoords(double[] x, double[] y)
This will merge the coordinates of the given coordinate system.static List<Double>
mergeCoords(List<Double> x, List<Double> y)
This will merge the coordinates of the given coordinate system.static double
min(double[] doubles)
static long
nextPowOf2(long v)
static void
normalize(double[] doubles, double sum)
Normalizes the doubles in the array using the given value.static double
normalize(double val, double min, double max)
Normalize a value (val - min) / (max - min)static double[]
normalizeToOne(double[] doubles)
static List<List<Double>>
partitionVariable(List<Double> arr, int chunk)
This will partition the given whole variable data applyTransformToDestination in to the specified chunk number.static double
permutation(double n, double r)
This returns the permutation of n choose r.static double
pow(double base, double exponent)
static int
probRound(double value, Random rand)
Rounds a double to the next nearest integer value in a probabilistic fashion (e.g.static double
probToLogOdds(double prob)
Returns the log-odds for a given probability.static double
randomDoubleBetween(double begin, double end)
static float
randomFloatBetween(float begin, float end)
static int
randomNumberBetween(double begin, double end)
Generates a random integer between the specified numbersstatic int
randomNumberBetween(double begin, double end, double anchor)
Generates a random integer between the specified numbersstatic int
randomNumberBetween(double begin, double end, org.apache.commons.math3.random.RandomGenerator rng)
Generates a random integer between the specified numbersstatic double
rootMeansSquaredError(double[] real, double[] predicted)
This returns the root mean squared error of two data setsstatic int
round(double value)
Rounds a double to the next nearest integer value.static double
roundDouble(double value, int afterDecimalPoint)
Rounds a double to the given number of decimal places.static float
roundFloat(float value, int afterDecimalPoint)
Rounds a double to the given number of decimal places.static double[]
sampleDoublesInInterval(double[][] doubles, int l)
static void
shuffleArray(int[] array, long rngSeed)
Shuffle the array elements using the specified RNG seed.static void
shuffleArray(int[] array, Random rng)
Shuffle the array elements using the specified Random instance Uses Fisher Yates shuffle internally: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithmstatic void
shuffleArraySubset(int[] array, int shuffleFirst, Random rng)
Shuffle the first N elements of the array using the specified Random instance.
If shuffleFirst < array.length, only the elements 0 to shuffleFirst-1 are modified; values at indices shuffleFirst to array.length-1 are not changed.static double
sigmoid(double x)
1 / 1 + exp(-x)double
slope(double x1, double x2, double y1, double y2)
This returns the slope of the given points.static boolean
sm(double a, double b)
Tests if a is smaller than b.static double
squaredLoss(double[] x, double[] y, double w_0, double w_1)
This will return the squared loss of the given pointsstatic double
ssError(double[] predictedValues, double[] targetAttribute)
How much of the variance is NOT explained by the regressionstatic double
ssReg(double[] residuals, double[] targetAttribute)
How much of the variance is explained by the regressionstatic double
ssTotal(double[] residuals, double[] targetAttribute)
Total variance in target attributestatic double
stringSimilarity(String... strings)
Calculate string similarity with tfidf weights relative to each character frequency and how many times a character appears in a given stringstatic double
sum(double[] nums)
This returns the sum of the given array.static double
sumOfMeanDifferences(double[] vector, double[] vector2)
Used for calculating top part of simple regression for beta 1static double
sumOfMeanDifferencesOnePoint(double[] vector)
Used for calculating top part of simple regression for beta 1static double
sumOfProducts(double[]... nums)
This returns the sum of products for the given numbers.static double
sumOfSquares(double[] vector)
This returns the sum of squares for the given vector.static double
tf(int count)
Term frequency: 1+ log10(count)static double
tfidf(double td, double idf)
Return td * idfstatic double
times(double[] nums)
This returns the product of all numbers in the given array.static int
toDecimal(String binary)
This will convert the given binary string to a decimal based integerstatic double
uniform(Random rng, double min, double max)
Generate a uniform random number from the given rngstatic double
vectorLength(double[] vector)
Returns the vector length (sqrt(sum(x_i))static double
w_0(double[] x, double[] y, int n)
static double
w_1(double[] x, double[] y, int n)
static double[]
weightsFor(double[] vector)
This returns the minimized loss values for a given vector.static double[]
weightsFor(List<Double> vector)
This returns the minimized loss values for a given vector.static double[]
xVals(double[] vector)
This returns the x values of the given vector.static double[]
yVals(double[] vector)
This returns the odd indexed values for the given vector
-
-
-
Field Detail
-
log2
public static final double log2
The natural logarithm of 2.
-
SMALL
public static final double SMALL
The small deviation allowed in double comparisons.- See Also:
- Constant Field Values
-
-
Method Detail
-
pow
public static double pow(double base, double exponent)
-
normalize
public static double normalize(double val, double min, double max)
Normalize a value (val - min) / (max - min)- Parameters:
val
- value to normalizemax
- max valuemin
- min value- Returns:
- the normalized value
-
clamp
public static int clamp(int value, int min, int max)
Clamps the value to a discrete value- Parameters:
value
- the value to clampmin
- min for the probability distributionmax
- max for the probability distribution- Returns:
- the discrete value
-
discretize
public static int discretize(double value, double min, double max, int binCount)
Discretize the given value- Parameters:
value
- the value to discretizemin
- the min of the distributionmax
- the max of the distributionbinCount
- the number of bins- Returns:
- the discretized value
-
nextPowOf2
public static long nextPowOf2(long v)
- Parameters:
v
- the number to getFromOrigin the next power of 2 for- Returns:
- the next power of 2 for the passed in value
-
binomial
public static int binomial(org.apache.commons.math3.random.RandomGenerator rng, int n, double p)
Generates a binomial distributed number using the given rng- Parameters:
rng
-n
-p
-- Returns:
-
uniform
public static double uniform(Random rng, double min, double max)
Generate a uniform random number from the given rng- Parameters:
rng
- the rng to usemin
- the min nummax
- the max num- Returns:
- a number uniformly distributed between min and max
-
correlation
public static double correlation(double[] residuals, double[] targetAttribute)
Returns the correlation coefficient of two double vectors.- Parameters:
residuals
- residualstargetAttribute
- target attribute vector- Returns:
- the correlation coefficient or r
-
sigmoid
public static double sigmoid(double x)
1 / 1 + exp(-x)- Parameters:
x
-- Returns:
-
ssReg
public static double ssReg(double[] residuals, double[] targetAttribute)
How much of the variance is explained by the regression- Parameters:
residuals
- errortargetAttribute
- data for target attribute- Returns:
- the sum squares of regression
-
ssError
public static double ssError(double[] predictedValues, double[] targetAttribute)
How much of the variance is NOT explained by the regression- Parameters:
predictedValues
- predicted valuestargetAttribute
- data for target attribute- Returns:
- the sum squares of regression
-
stringSimilarity
public static double stringSimilarity(String... strings)
Calculate string similarity with tfidf weights relative to each character frequency and how many times a character appears in a given string- Parameters:
strings
- the strings to calculate similarity for- Returns:
- the cosine similarity between the strings
-
vectorLength
public static double vectorLength(double[] vector)
Returns the vector length (sqrt(sum(x_i))- Parameters:
vector
- the vector to return the vector length for- Returns:
- the vector length of the passed in array
-
idf
public static double idf(double totalDocs, double numTimesWordAppearedInADocument)
Inverse document frequency: the total docs divided by the number of times the word appeared in a document- Parameters:
totalDocs
- the total documents for the data applyTransformToDestinationnumTimesWordAppearedInADocument
- the number of times the word occurred in a document- Returns:
- log(10) (totalDocs/numTImesWordAppearedInADocument)
-
tf
public static double tf(int count)
Term frequency: 1+ log10(count)- Parameters:
count
- the count of a word or character in a given string or document- Returns:
- 1+ log(10) count
-
tfidf
public static double tfidf(double td, double idf)
Return td * idf- Parameters:
td
- the term frequency (assumed calculated)idf
- inverse document frequency (assumed calculated)- Returns:
- td * idf
-
ssTotal
public static double ssTotal(double[] residuals, double[] targetAttribute)
Total variance in target attribute- Parameters:
residuals
- errortargetAttribute
- data for target attribute- Returns:
- Total variance in target attribute
-
sum
public static double sum(double[] nums)
This returns the sum of the given array.- Parameters:
nums
- the array of numbers to sum- Returns:
- the sum of the given array
-
mergeCoords
public static double[] mergeCoords(double[] x, double[] y)
This will merge the coordinates of the given coordinate system.- Parameters:
x
- the x coordinatesy
- the y coordinates- Returns:
- a vector such that each (x,y) pair is at ret[i],ret[i+1]
-
mergeCoords
public static List<Double> mergeCoords(List<Double> x, List<Double> y)
This will merge the coordinates of the given coordinate system.- Parameters:
x
- the x coordinatesy
- the y coordinates- Returns:
- a vector such that each (x,y) pair is at ret[i],ret[i+1]
-
weightsFor
public static double[] weightsFor(List<Double> vector)
This returns the minimized loss values for a given vector. It is assumed that the x, y pairs are at vector[i], vector[i+1]- Parameters:
vector
- the vector of numbers to getFromOrigin the weights for- Returns:
- a double array with w_0 and w_1 are the associated indices.
-
squaredLoss
public static double squaredLoss(double[] x, double[] y, double w_0, double w_1)
This will return the squared loss of the given points- Parameters:
x
- the x coordinates to usey
- the y coordinates to usew_0
- the first weightw_1
- the second weight- Returns:
- the squared loss of the given points
-
w_1
public static double w_1(double[] x, double[] y, int n)
-
w_0
public static double w_0(double[] x, double[] y, int n)
-
weightsFor
public static double[] weightsFor(double[] vector)
This returns the minimized loss values for a given vector. It is assumed that the x, y pairs are at vector[i], vector[i+1]- Parameters:
vector
- the vector of numbers to getFromOrigin the weights for- Returns:
- a double array with w_0 and w_1 are the associated indices.
-
errorFor
public static double errorFor(double actual, double prediction)
-
sumOfMeanDifferences
public static double sumOfMeanDifferences(double[] vector, double[] vector2)
Used for calculating top part of simple regression for beta 1- Parameters:
vector
- the x coordinatesvector2
- the y coordinates- Returns:
- the sum of mean differences for the input vectors
-
sumOfMeanDifferencesOnePoint
public static double sumOfMeanDifferencesOnePoint(double[] vector)
Used for calculating top part of simple regression for beta 1- Parameters:
vector
- the x coordinates- Returns:
- the sum of mean differences for the input vectors
-
times
public static double times(double[] nums)
This returns the product of all numbers in the given array.- Parameters:
nums
- the numbers to multiply over- Returns:
- the product of all numbers in the array, or 0 if the length is or or nums i null
-
sumOfProducts
public static double sumOfProducts(double[]... nums)
This returns the sum of products for the given numbers.- Parameters:
nums
- the sum of products for the give numbers- Returns:
- the sum of products for the given numbers
-
coordSplit
public static List<double[]> coordSplit(double[] vector)
This returns the coordinate split in a list of coordinates such that the values for ret[0] are the x values and ret[1] are the y values- Parameters:
vector
- the vector to split with x and y values/- Returns:
- a coordinate split for the given vector of values. if null, is passed in null is returned
-
partitionVariable
public static List<List<Double>> partitionVariable(List<Double> arr, int chunk)
This will partition the given whole variable data applyTransformToDestination in to the specified chunk number.- Parameters:
arr
- the data applyTransformToDestination to pass inchunk
- the number to separate by- Returns:
- a partition data applyTransformToDestination relative to the passed in chunk number
-
coordSplit
public static List<double[]> coordSplit(List<Double> vector)
This returns the coordinate split in a list of coordinates such that the values for ret[0] are the x values and ret[1] are the y values- Parameters:
vector
- the vector to split with x and y values Note that the list will be more stable due to the size operator. The array version will have extraneous values if not monitored properly.- Returns:
- a coordinate split for the given vector of values. if null, is passed in null is returned
-
xVals
public static double[] xVals(double[] vector)
This returns the x values of the given vector. These are assumed to be the even values of the vector.- Parameters:
vector
- the vector to getFromOrigin the values for- Returns:
- the x values of the given vector
-
yVals
public static double[] yVals(double[] vector)
This returns the odd indexed values for the given vector- Parameters:
vector
- the odd indexed values of rht egiven vector- Returns:
- the y values of the given vector
-
sumOfSquares
public static double sumOfSquares(double[] vector)
This returns the sum of squares for the given vector.- Parameters:
vector
- the vector to obtain the sum of squares for- Returns:
- the sum of squares for this vector
-
determinationCoefficient
public static double determinationCoefficient(double[] y1, double[] y2, int n)
This returns the determination coefficient of two vectors given a length- Parameters:
y1
- the first vectory2
- the second vectorn
- the length of both vectors- Returns:
- the determination coefficient or r^2
-
log2
public static double log2(double a)
Returns the logarithm of a for base 2.- Parameters:
a
- a double- Returns:
- the logarithm for base 2
-
rootMeansSquaredError
public static double rootMeansSquaredError(double[] real, double[] predicted)
This returns the root mean squared error of two data sets- Parameters:
real
- the realComponent valuespredicted
- the predicted values- Returns:
- the root means squared error for two data sets
-
entropy
public static double entropy(double[] vector)
This returns the entropy (information gain, or uncertainty of a random variable): -sum(x*log(x))- Parameters:
vector
- the vector of values to getFromOrigin the entropy for- Returns:
- the entropy of the given vector
-
kroneckerDelta
public static int kroneckerDelta(double i, double j)
This returns the kronecker delta of two doubles.- Parameters:
i
- the first number to comparej
- the second number to compare- Returns:
- 1 if they are equal, 0 otherwise
-
adjustedrSquared
public static double adjustedrSquared(double rSquared, int numRegressors, int numDataPoints)
This calculates the adjusted r^2 including degrees of freedom. Also known as calculating "strength" of a regression- Parameters:
rSquared
- the r squared value to calculatenumRegressors
- number of variablesnumDataPoints
- size of the data applyTransformToDestination- Returns:
- an adjusted r^2 for degrees of freedom
-
normalizeToOne
public static double[] normalizeToOne(double[] doubles)
-
min
public static double min(double[] doubles)
-
max
public static double max(double[] doubles)
-
normalize
public static void normalize(double[] doubles, double sum)
Normalizes the doubles in the array using the given value.- Parameters:
doubles
- the array of doublesum
- the value by which the doubles are to be normalized- Throws:
IllegalArgumentException
- if sum is zero or NaN
-
logs2probs
public static double[] logs2probs(double[] a)
Converts an array containing the natural logarithms of probabilities stored in a vector back into probabilities. The probabilities are assumed to sum to one.- Parameters:
a
- an array holding the natural logarithms of the probabilities- Returns:
- the converted array
-
information
public static double information(double[] probabilities)
This returns the entropy for a given vector of probabilities.- Parameters:
probabilities
- the probabilities to getFromOrigin the entropy for- Returns:
- the entropy of the given probabilities.
-
maxIndex
public static int maxIndex(double[] doubles)
Returns index of maximum element in a given array of doubles. First maximum is returned.- Parameters:
doubles
- the array of doubles- Returns:
- the index of the maximum element
-
factorial
public static double factorial(double n)
This will return the factorial of the given number n.- Parameters:
n
- the number to getFromOrigin the factorial for- Returns:
- the factorial for this number
-
probToLogOdds
public static double probToLogOdds(double prob)
Returns the log-odds for a given probability.- Parameters:
prob
- the probability- Returns:
- the log-odds after the probability has been mapped to [Utils.SMALL, 1-Utils.SMALL]
-
round
public static int round(double value)
Rounds a double to the next nearest integer value. The JDK version of it doesn't work properly.- Parameters:
value
- the double value- Returns:
- the resulting integer value
-
permutation
public static double permutation(double n, double r)
This returns the permutation of n choose r.- Parameters:
n
- the n to chooser
- the number of elements to choose- Returns:
- the permutation of these numbers
-
combination
public static double combination(double n, double r)
This returns the combination of n choose r- Parameters:
n
- the number of elements overallr
- the number of elements to choose- Returns:
- the amount of possible combinations for this applyTransformToDestination of elements
-
hypotenuse
public static double hypotenuse(double a, double b)
sqrt(a^2 + b^2) without under/overflow.
-
probRound
public static int probRound(double value, Random rand)
Rounds a double to the next nearest integer value in a probabilistic fashion (e.g. 0.8 has a 20% chance of being rounded down to 0 and a 80% chance of being rounded up to 1). In the limit, the average of the rounded numbers generated by this procedure should converge to the original double.- Parameters:
value
- the double valuerand
- the random number generator- Returns:
- the resulting integer value
-
roundDouble
public static double roundDouble(double value, int afterDecimalPoint)
Rounds a double to the given number of decimal places.- Parameters:
value
- the double valueafterDecimalPoint
- the number of digits after the decimal point- Returns:
- the double rounded to the given precision
-
roundFloat
public static float roundFloat(float value, int afterDecimalPoint)
Rounds a double to the given number of decimal places.- Parameters:
value
- the double valueafterDecimalPoint
- the number of digits after the decimal point- Returns:
- the double rounded to the given precision
-
bernoullis
public static double bernoullis(double n, double k, double successProb)
This will return the bernoulli trial for the given event. A bernoulli trial is a mechanism for detecting the probability of a given event occurring k times in n independent trials- Parameters:
n
- the number of trialsk
- the number of times the target event occurssuccessProb
- the probability of the event happening- Returns:
- the probability of the given event occurring k times.
-
sm
public static boolean sm(double a, double b)
Tests if a is smaller than b.- Parameters:
a
- a doubleb
- a double
-
gr
public static boolean gr(double a, double b)
Tests if a is greater than b.- Parameters:
a
- a doubleb
- a double
-
fromString
public static double[] fromString(String data, String separator)
This will take a given string and separator and convert it to an equivalent double array.- Parameters:
data
- the data to separateseparator
- the separator to use- Returns:
- the new double array based on the given data
-
mean
public static double mean(double[] vector)
Computes the mean for an array of doubles.- Parameters:
vector
- the array- Returns:
- the mean
-
toDecimal
public static int toDecimal(String binary)
This will convert the given binary string to a decimal based integer- Parameters:
binary
- the binary string to convert- Returns:
- an equivalent base 10 number
-
distanceFinderZValue
public static int distanceFinderZValue(double[] vector)
This will translate a vector in to an equivalent integer- Parameters:
vector
- the vector to translate- Returns:
- a z value such that the value is the interleaved lsd to msd for each double in the vector
-
euclideanDistance
public static double euclideanDistance(double[] p, double[] q)
This returns the euclidean distance of two vectors sum(i=1,n) (q_i - p_i)^2- Parameters:
p
- the first vectorq
- the second vector- Returns:
- the euclidean distance between two vectors
-
euclideanDistance
public static double euclideanDistance(float[] p, float[] q)
This returns the euclidean distance of two vectors sum(i=1,n) (q_i - p_i)^2- Parameters:
p
- the first vectorq
- the second vector- Returns:
- the euclidean distance between two vectors
-
generateUniform
public static double[] generateUniform(int l)
This will generate a series of uniformally distributed numbers between l times- Parameters:
l
- the number of numbers to generate- Returns:
- l uniformally generated numbers
-
manhattanDistance
public static double manhattanDistance(double[] p, double[] q)
This will calculate the Manhattan distance between two sets of points. The Manhattan distance is equivalent to: 1_sum_n |p_i - q_i|- Parameters:
p
- the first point vectorq
- the second point vector- Returns:
- the Manhattan distance between two object
-
sampleDoublesInInterval
public static double[] sampleDoublesInInterval(double[][] doubles, int l)
-
randomNumberBetween
public static int randomNumberBetween(double begin, double end, double anchor)
Generates a random integer between the specified numbers- Parameters:
begin
- the begin of the intervalend
- the end of the intervalanchor
- the base number (assuming to be generated from an external rng)- Returns:
- an int between begin and end
-
randomNumberBetween
public static int randomNumberBetween(double begin, double end)
Generates a random integer between the specified numbers- Parameters:
begin
- the begin of the intervalend
- the end of the interval- Returns:
- an int between begin and end
-
randomNumberBetween
public static int randomNumberBetween(double begin, double end, org.apache.commons.math3.random.RandomGenerator rng)
Generates a random integer between the specified numbers- Parameters:
begin
- the begin of the intervalend
- the end of the interval- Returns:
- an int between begin and end
-
randomFloatBetween
public static float randomFloatBetween(float begin, float end)
-
randomDoubleBetween
public static double randomDoubleBetween(double begin, double end)
-
slope
public double slope(double x1, double x2, double y1, double y2)
This returns the slope of the given points.- Parameters:
x1
- the first x to usex2
- the end x to usey1
- the begin y to usey2
- the end y to use- Returns:
- the slope of the given points
-
shuffleArray
public static void shuffleArray(int[] array, long rngSeed)
Shuffle the array elements using the specified RNG seed. Uses Fisher Yates shuffle internally: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm- Parameters:
array
- Array to shufflerngSeed
- RNG seed to use for shuffling
-
shuffleArray
public static void shuffleArray(int[] array, Random rng)
Shuffle the array elements using the specified Random instance Uses Fisher Yates shuffle internally: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm- Parameters:
array
- Array to shufflerng
- Random instance to use for shuffling
-
shuffleArraySubset
public static void shuffleArraySubset(int[] array, int shuffleFirst, Random rng)
Shuffle the first N elements of the array using the specified Random instance.
If shuffleFirst < array.length, only the elements 0 to shuffleFirst-1 are modified; values at indices shuffleFirst to array.length-1 are not changed. Uses Fisher Yates shuffle internally: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm- Parameters:
array
- Array to shuffle first N elements ofrng
- Random instance to use for shuffling
-
hashCode
public static int hashCode(double value)
hashCode method, taken from Java 1.8 Double.hashCode(double) method- Parameters:
value
- Double value to hash- Returns:
- Hash code for the double value
-
-