Package org.goplanit.utils.arrays
Class ArrayUtils
- java.lang.Object
-
- org.goplanit.utils.arrays.ArrayUtils
-
public class ArrayUtils extends Object
General methods for arrays- Author:
- gman6028, markr
-
-
Constructor Summary
Constructors Constructor Description ArrayUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addTo(double[] destination, double[] addToDestination)
Add the values of a second array element-wise to the first arraystatic void
addTo(double[] destination, double[] addToDestination, int numberOfElements)
Add the values of a second array element-wise to the first arraystatic Object[]
addtoStart(Object elementToPrepend, Object[] theArray)
add an element to the start of the passed in array.static void
divideBy(double[] destination, double[] diviserArray, double divideByZeroResult)
Divide the values of the first array by the the second array (element-wise)static void
divideBy(double[] destination, double diviser, double divideByZeroResult)
divide each entry in array by given diviser.static void
divideBySum(double[] destination, int divideByZeroResult)
divide each entry in array by the sum of the entries.static double
dotProduct(double[] d1, double[] d2, int numberOfElements)
Return the dot product of two arrays The dot product is found by multiplying the elements of each array at each position together and then taking the sum of the multiplied values e.g.static double
getMaximum(double[] array)
find the maximum of an array of doublesstatic void
loopAll(double[][] array, BiConsumer<Integer,Integer> consumer)
Loop over array entries and apply consumerstatic void
loopAll(double[] array, Consumer<Integer> consumer)
Loop over array entries and apply consumerstatic <T> void
loopAll(T[] array, Consumer<T> consumer)
Loop over array entries and apply consumerstatic void
multiplyBy(double[] destination, double multiplicator)
multiply each entry in array by given multiplicator.static double
sumOf(double[] array)
sum of each entry in array
-
-
-
Method Detail
-
addTo
public static void addTo(double[] destination, double[] addToDestination, int numberOfElements)
Add the values of a second array element-wise to the first array- Parameters:
destination
- the array to be updatedaddToDestination
- array of values to be added to destination arraynumberOfElements
- number of elements in array to be updated
-
addTo
public static void addTo(double[] destination, double[] addToDestination)
Add the values of a second array element-wise to the first array- Parameters:
destination
- the array to be updatedaddToDestination
- array of values to be added to destination array
-
divideBy
public static void divideBy(double[] destination, double diviser, double divideByZeroResult)
divide each entry in array by given diviser. When divisor is zero, all entries are set to divideByZeroResult- Parameters:
destination
- array to apply todiviser
- to divide bydivideByZeroResult
- result if provided division value is zero
-
divideBy
public static void divideBy(double[] destination, double[] diviserArray, double divideByZeroResult)
Divide the values of the first array by the the second array (element-wise)- Parameters:
destination
- the array to be updateddiviserArray
- to divide by these valuesdivideByZeroResult
- to use in case the diviser is zero
-
multiplyBy
public static void multiplyBy(double[] destination, double multiplicator)
multiply each entry in array by given multiplicator.- Parameters:
destination
- array to apply tomultiplicator
- to multiply with
-
divideBySum
public static void divideBySum(double[] destination, int divideByZeroResult)
divide each entry in array by the sum of the entries. When divisor is zero, all entries are set to divideByZeroResult- Parameters:
destination
- array to apply todivideByZeroResult
- result if provided division value is zero
-
sumOf
public static double sumOf(double[] array)
sum of each entry in array- Parameters:
array
- to apply to- Returns:
- computed sum
-
dotProduct
public static double dotProduct(double[] d1, double[] d2, int numberOfElements)
Return the dot product of two arrays The dot product is found by multiplying the elements of each array at each position together and then taking the sum of the multiplied values e.g. a[1]*b[1]+ a[2]*b[2] + a[3]*b[3] etc- Parameters:
d1
- first array in the dot productd2
- second array in the dot productnumberOfElements
- number of elements in each array- Returns:
- the value of the dot product
-
addtoStart
public static Object[] addtoStart(Object elementToPrepend, Object[] theArray)
add an element to the start of the passed in array. Note that this involves a copy of the original array- Parameters:
elementToPrepend
- the element to add to starttheArray
- the array to prepend- Returns:
- the new array
-
getMaximum
public static double getMaximum(double[] array)
find the maximum of an array of doubles- Parameters:
array
- to check- Returns:
- maximum value
-
loopAll
public static void loopAll(double[][] array, BiConsumer<Integer,Integer> consumer)
Loop over array entries and apply consumer- Parameters:
array
- to apply consumer toconsumer
- to apply
-
loopAll
public static void loopAll(double[] array, Consumer<Integer> consumer)
Loop over array entries and apply consumer- Parameters:
array
- to apply consumer toconsumer
- to apply
-
loopAll
public static <T> void loopAll(T[] array, Consumer<T> consumer)
Loop over array entries and apply consumer- Type Parameters:
T
- type of array contents- Parameters:
array
- to apply consumer toconsumer
- to apply
-
-