jhplot.math
Class ArrayMath
- java.lang.Object
-
- jhplot.math.ArrayMath
-
public final class ArrayMath extends java.lang.Object
Arrays are faster than object, so this class is here to take full advantage of arrays without encapsulation. All methods are safe, that is, they create copies of arrays whenever necessary This makes for slower methods, but the programmer can always define his own methods optimized for performance.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description static double[]
abs(double[] v)
Takes the absolute value of each component of an array.static double[][]
abs(double[][] v)
Takes the absolute value of each component of an array.static int[]
abs(int[] v)
Takes the absolute value of each component of an array.static int[][]
abs(int[][] v)
Takes the absolute value of each component of an array.static double[]
add(double[] w, double a)
Adds a scalar to every element in the array.static double[]
add(double[] a, double[] b)
Adds the two arrays together (componentwise).static double[]
add(double[] w, double a, double[] v, int p)
Adds to an array w, a times v where a is a scalar.static int[]
add(int[] w, double a, int[] v, int p)
Adds to an array w, a times v where a is a scalar.static int[]
add(int[] w, int a)
Adds a scalar to every element in the array.static int[]
add(int[] a, int[] b)
Adds the two arrays together (componentwise).static double[]
copy(double[] array)
Returns a copy of the array.static double[][]
copy(double[][] v)
Returns a copy of the array.static float[]
copy(float[] array)
Returns a copy of the array.static int[]
copy(int[] array)
Returns a copy of the array.static int[][]
copy(int[][] v)
Returns a copy of the array.static long[]
copy(long[] array)
Returns a copy of the array.static double
correlation(double[] v1, double[] v2)
Computes the (linear) correlation between two arrays.static double
correlation(int[] v1, int[] v2)
Computes the (linear) correlation between two arrays.static double
covariance(double[] v1, double[] v2)
Computes the covariance.static double
covariance(int[] v1, int[] v2)
Computes the covariance.static boolean
equals(int[] a, int[] b)
Deprecated.Use java.util.Arrays instead.static double[]
extract(int k0, int k1, double[] invect)
Extracts a sub-array (will invert the resulting array if k0 > k1).static int[]
extract(int k0, int k1, int[] invect)
Extracts a sub-array (will invert the resulting array if k0 > k1).static double
getPowerN(double[] data, int n)
Sums all components then puts to power n.static double[]
invert(double[] v)
Inverts an array from left to right.static int[]
invert(int[] v)
Inverts an array from left to right.static boolean
isSquare(double[][] a)
static boolean
isSquare(float[][] a)
static boolean
isSquare(int[][] a)
static boolean
isSquare(long[][] a)
static boolean
isSquare(java.lang.Object[][] a)
static double
mass(double[] v)
Returns the sum of the elements of the array.static int
mass(int[] v)
Returns the sum of the elements of the array.static double
max(double[] v)
Returns the maximum of an array.static double
max(double[][] v)
Returns the maximum of an array.static int
max(int[] v)
Returns the maximum of an array.static int
max(int[][] v)
Returns the maximum of an array.static double
maxIndex(double[] value)
returns the index of the greatest element from the given arraystatic double
mean(double[] v)
Computes the mean.static double
mean(int[] v)
Computes the mean.static double
median(double[] v)
Computes the median of an array.static int
median(int[] v)
Computes the median of an array.static double
min(double[] v)
Returns the minimum of an array.static double
min(double[][] v)
Returns the minimum of an array.static int
min(int[] v)
Returns the minimum of an array.static int
min(int[][] v)
Returns the minimum of an array.static double
minIndex(double[] value)
returns the index of the smallest element from the given arraystatic double
norm(double[] data)
Computes the L2 norm of an array (Euclidean norm or "length").static double
norm(int[] data)
Computes the L2 norm of an array (Euclidean norm or "length").static double[]
normalize(double[] v)
Renormalizes the array so that its L2 norm is 1 (up to computational errors).static double[]
padding(int n0, int pos, double[] v)
Fills in with zero to get to the desired length; original array with be at the specified position.static int[]
padding(int n0, int pos, int[] v)
Fills in with zeroes to get to the specified length; original array with be at the specified positionstatic double
percentile(double[] v, double p)
Gives the percentile of an arraystatic double
percentile(int[] v, double p)
Gives the percentile of an array.static void
print(double[] v)
Prints an array to screen.static void
print(double[][] v)
Prints an array to screen.static void
print(int[] v)
Prints an array to screen.static void
print(int[][] v)
Prints an array to screen.static double[]
range(double b)
Generates an array going for 0 to b with steps of size 1.static double
range(double[] population)
returns the difference between the max and the min elementstatic double[]
range(double a, double b)
Generates an array going for a to b inclusively with steps of size 1.static double[]
range(double a, double b, double step)
Generates an array going for a to b with steps of size step.static int[]
range(int b)
Generates an array going for 0 to b with steps of size 1.static int[]
range(int a, int b)
Generates an array going for a to b with steps of size 1.static int[]
range(int a, int b, int step)
Generates an array going for a to b with steps of size step.static double[]
scalarMultiply(double a, double[] v)
Multiplies every component of an array by a scalar.static double[]
scalarMultiply(double a, int[] v)
Multiplies every component of an array by a scalar.static double[]
scalarMultiplyFast(double a, double[] v)
Fast scalar multiplication for sparse arrays.static double[]
scalarMultiplyFast(double a, int[] v)
Fast scalar multiplication for sparse arrays.static double
scalarProduct(double[][] w0, double[][] w1)
Computes the scalar product of two array as if they were matrices.static double
scalarProduct(double[] w0, double[] w1)
Computes the scalar product of two array as if they were vectors.static double
scalarProduct(int[][] w0, int[][] w1)
Computes the scalar product of two array as if they were matrices.static int
scalarProduct(int[] w0, int[] w1)
Computes the scalar product of two array as if they were vectors.static double[]
setLengthFromBeginning(double[] data, int length)
Sets an array to the specified length scraping or padding the end if necessary.static int[]
setLengthFromBeginning(int[] data, int length)
Sets an array to the specified length scraping or padding the end if necessary.static double[]
setLengthFromEnd(double[] data, int length)
Sets an array to the specified length scraping or padding the beginning if necessary.static int[]
setLengthFromEnd(int[] data, int length)
Sets an array to the specified length scraping or padding the beginning if necessary.static double
softmax(int index, double[] x)
produces a double between 0 and 1 based on the given array x and the index that must be between (0 and x.length) For further information see also: http://www-ccs.ucsd.edu/matlab/toolbox/nnet/softmax.htmlstatic double[]
sortMaxToMin(double[] v)
Returns a sorted array from the maximum to the minimum value.static int[]
sortMaxToMin(int[] v)
Returns a sorted array from the maximum to the minimum value.static double[]
sortMinToMax(double[] v)
Returns a sorted array from the minimum to the maximum value.static int[]
sortMinToMax(int[] v)
Returns a sorted array from the minimum to the maximum value.static double
standardDeviation(double[] v)
Computes the (bias-corrected sample) standard deviation of an array.static double
standardDeviation(int[] v)
Returns the (bias-corrected sample) standard deviation of an array.static double[]
subtract(double[] a, double[] b)
Subtracts the two arrays together (componentwise)static int[]
subtract(int[] a, int[] b)
Subtracts the two arrays together (componentwise).static double
sumSquares(double[] data)
Sums the squares of all components; also called the energy of the array.static double
sumSquares(double[][] data)
Sums the squares of all components; also called the energy of the array.static int
sumSquares(int[] data)
Sums the squares of all components; also called the energy of the array.static int
sumSquares(int[][] data)
Sums the squares of all components; also called the energy of the array.static double[]
toDouble(float[] v)
static double[][]
toDouble(float[][] v)
static double[]
toDouble(int[] v)
static double[][]
toDouble(int[][] v)
static double[]
toDouble(long[] v)
static double[][]
toDouble(long[][] v)
static float[]
toFloat(double[] v)
static float[][]
toFloat(double[][] v)
static float[]
toFloat(int[] v)
static float[][]
toFloat(int[][] v)
static float[]
toFloat(long[] v)
static float[][]
toFloat(long[][] v)
static int[]
toInt(double[] v)
static int[][]
toInt(double[][] v)
static int[]
toInt(float[] v)
static int[][]
toInt(float[][] v)
static int[]
toInt(long[] v)
static int[][]
toInt(long[][] v)
static long[]
toLong(double[] v)
static long[][]
toLong(double[][] v)
static long[]
toLong(float[] v)
static long[][]
toLong(float[][] v)
static long[]
toLong(int[] v)
static long[][]
toLong(int[][] v)
static java.lang.String
toString(double[] array)
Returns a comma delimited string representing the value of the array.static java.lang.String
toString(double[][] array)
Returns a comma delimited string representing the value of the array.static java.lang.String
toString(int[] array)
Returns a comma delimited string representing the value of the array.static java.lang.String
toString(int[][] array)
Returns a comma delimited string representing the value of the array.static double[][]
transpose(double[][] M)
Takes the transpose of an array (like the matrix operation).static int[][]
transpose(int[][] M)
Takes the transpose of an array (like the matrix operation).static double
variance(double[] v)
Computes the (bias-corrected sample) variance.static double
variance(int[] v)
Computes the (bias-corrected sample) variance.
-
-
-
Method Detail
-
copy
public static int[] copy(int[] array)
Returns a copy of the array.
-
copy
public static long[] copy(long[] array)
Returns a copy of the array.
-
copy
public static float[] copy(float[] array)
Returns a copy of the array.
-
copy
public static double[] copy(double[] array)
Returns a copy of the array.
-
toFloat
public static float[] toFloat(double[] v)
-
toFloat
public static float[] toFloat(long[] v)
-
toFloat
public static float[] toFloat(int[] v)
-
isSquare
public static boolean isSquare(double[][] a)
-
isSquare
public static boolean isSquare(float[][] a)
-
isSquare
public static boolean isSquare(long[][] a)
-
isSquare
public static boolean isSquare(int[][] a)
-
isSquare
public static boolean isSquare(java.lang.Object[][] a)
-
toFloat
public static float[][] toFloat(double[][] v)
-
toFloat
public static float[][] toFloat(long[][] v)
-
toFloat
public static float[][] toFloat(int[][] v)
-
toDouble
public static double[] toDouble(float[] v)
-
toDouble
public static double[] toDouble(long[] v)
-
toDouble
public static double[] toDouble(int[] v)
-
toDouble
public static double[][] toDouble(float[][] v)
-
toDouble
public static double[][] toDouble(long[][] v)
-
toDouble
public static double[][] toDouble(int[][] v)
-
toLong
public static long[] toLong(double[] v)
-
toLong
public static long[] toLong(float[] v)
-
toLong
public static long[] toLong(int[] v)
-
toLong
public static long[][] toLong(float[][] v)
-
toLong
public static long[][] toLong(double[][] v)
-
toLong
public static long[][] toLong(int[][] v)
-
toInt
public static int[] toInt(double[] v)
-
toInt
public static int[] toInt(float[] v)
-
toInt
public static int[] toInt(long[] v)
-
toInt
public static int[][] toInt(float[][] v)
-
toInt
public static int[][] toInt(double[][] v)
-
toInt
public static int[][] toInt(long[][] v)
-
normalize
public static double[] normalize(double[] v)
Renormalizes the array so that its L2 norm is 1 (up to computational errors).
-
setLengthFromEnd
public static double[] setLengthFromEnd(double[] data, int length)
Sets an array to the specified length scraping or padding the beginning if necessary.
-
setLengthFromBeginning
public static double[] setLengthFromBeginning(double[] data, int length)
Sets an array to the specified length scraping or padding the end if necessary.
-
copy
public static double[][] copy(double[][] v)
Returns a copy of the array.
-
variance
public static double variance(double[] v)
Computes the (bias-corrected sample) variance.
-
covariance
public static double covariance(double[] v1, double[] v2)
Computes the covariance.
-
correlation
public static double correlation(double[] v1, double[] v2)
Computes the (linear) correlation between two arrays. Squaring this result and multiply by 100 gives you the percentage of correlation.
-
mean
public static double mean(double[] v)
Computes the mean.
-
standardDeviation
public static double standardDeviation(double[] v)
Computes the (bias-corrected sample) standard deviation of an array.
-
sortMinToMax
public static double[] sortMinToMax(double[] v)
Returns a sorted array from the minimum to the maximum value.
-
sortMaxToMin
public static double[] sortMaxToMin(double[] v)
Returns a sorted array from the maximum to the minimum value.
-
percentile
public static double percentile(double[] v, double p)
Gives the percentile of an array- Parameters:
p
- percentile, must be between 0 and 1- Throws:
java.lang.IllegalArgumentException
- if parameter. p is not between 0 and 1.
-
median
public static double median(double[] v)
Computes the median of an array. If the array contains an even number of elements, then the mean of the lower and upper medians is returned.
-
abs
public static double[] abs(double[] v)
Takes the absolute value of each component of an array.
-
abs
public static double[][] abs(double[][] v)
Takes the absolute value of each component of an array.
-
max
public static double max(double[] v)
Returns the maximum of an array.
-
max
public static double max(double[][] v)
Returns the maximum of an array.
-
maxIndex
public static double maxIndex(double[] value)
returns the index of the greatest element from the given array
-
min
public static double min(double[] v)
Returns the minimum of an array.
-
min
public static double min(double[][] v)
Returns the minimum of an array.
-
minIndex
public static double minIndex(double[] value)
returns the index of the smallest element from the given array
-
norm
public static double norm(double[] data)
Computes the L2 norm of an array (Euclidean norm or "length").
-
sumSquares
public static double sumSquares(double[] data)
Sums the squares of all components; also called the energy of the array.
-
sumSquares
public static double sumSquares(double[][] data)
Sums the squares of all components; also called the energy of the array.
-
getPowerN
public static double getPowerN(double[] data, int n)
Sums all components then puts to power n. This is the direct computation of the polynomial expansion
-
scalarProduct
public static double scalarProduct(double[] w0, double[] w1)
Computes the scalar product of two array as if they were vectors.- Throws:
java.lang.IllegalArgumentException
- if the don't have the same length
-
scalarProduct
public static double scalarProduct(double[][] w0, double[][] w1)
Computes the scalar product of two array as if they were matrices.- Throws:
java.lang.IllegalArgumentException
- if the don't have the same length
-
extract
public static double[] extract(int k0, int k1, double[] invect)
Extracts a sub-array (will invert the resulting array if k0 > k1).- Parameters:
k0
- location of the first componentk1
- location of the last component
-
invert
public static double[] invert(double[] v)
Inverts an array from left to right.
-
padding
public static double[] padding(int n0, int pos, double[] v)
Fills in with zero to get to the desired length; original array with be at the specified position.- Parameters:
n0
- length of the new array.pos
- position of the old array.
-
add
public static double[] add(double[] w, double a, double[] v, int p)
Adds to an array w, a times v where a is a scalar. Since v can be smaller then w, we must specified the position at which v will be added.- Parameters:
a
- scalar.p
- position.
-
add
public static double[] add(double[] w, double a)
Adds a scalar to every element in the array.
-
transpose
public static double[][] transpose(double[][] M)
Takes the transpose of an array (like the matrix operation).- Throws:
java.lang.IllegalArgumentException
- if the array is not a matrix
-
range
public static double[] range(double a, double b, double step)
Generates an array going for a to b with steps of size step. If it can't get to the value b in a finite number of steps, it gets as close as possible (a can be larger or smaller than b)- Parameters:
step
- size of steps, must be positive.a
- first value of array.b
- last value of array.- Throws:
java.lang.IllegalArgumentException
- if step is negative of if a=b.
-
range
public static double[] range(double a, double b)
Generates an array going for a to b inclusively with steps of size 1. a can be smaller or larger than b.
-
range
public static double range(double[] population)
returns the difference between the max and the min element
-
softmax
public static double softmax(int index, double[] x)
produces a double between 0 and 1 based on the given array x and the index that must be between (0 and x.length) For further information see also: http://www-ccs.ucsd.edu/matlab/toolbox/nnet/softmax.html
-
range
public static double[] range(double b)
Generates an array going for 0 to b with steps of size 1. 0 can be smaller or larger than b.
-
add
public static double[] add(double[] a, double[] b)
Adds the two arrays together (componentwise).- Throws:
java.lang.IllegalArgumentException
- if the two arrays don't have the same length.
-
subtract
public static double[] subtract(double[] a, double[] b)
Subtracts the two arrays together (componentwise)- Throws:
java.lang.IllegalArgumentException
- if the two arrays don't have the same length.
-
scalarMultiply
public static double[] scalarMultiply(double a, double[] v)
Multiplies every component of an array by a scalar.
-
toString
public static java.lang.String toString(double[] array)
Returns a comma delimited string representing the value of the array.
-
toString
public static java.lang.String toString(double[][] array)
Returns a comma delimited string representing the value of the array.
-
mass
public static double mass(double[] v)
Returns the sum of the elements of the array.
-
scalarMultiplyFast
public static double[] scalarMultiplyFast(double a, double[] v)
Fast scalar multiplication for sparse arrays.
-
print
public static void print(double[] v)
Prints an array to screen.
-
print
public static void print(double[][] v)
Prints an array to screen.
-
setLengthFromEnd
public static int[] setLengthFromEnd(int[] data, int length)
Sets an array to the specified length scraping or padding the beginning if necessary.
-
setLengthFromBeginning
public static int[] setLengthFromBeginning(int[] data, int length)
Sets an array to the specified length scraping or padding the end if necessary.
-
copy
public static int[][] copy(int[][] v)
Returns a copy of the array.
-
variance
public static double variance(int[] v)
Computes the (bias-corrected sample) variance.
-
covariance
public static double covariance(int[] v1, int[] v2)
Computes the covariance.
-
correlation
public static double correlation(int[] v1, int[] v2)
Computes the (linear) correlation between two arrays. Squaring this result and multiply by 100 gives you the percentage of correlation.
-
mean
public static double mean(int[] v)
Computes the mean.
-
standardDeviation
public static double standardDeviation(int[] v)
Returns the (bias-corrected sample) standard deviation of an array.
-
sortMinToMax
public static int[] sortMinToMax(int[] v)
Returns a sorted array from the minimum to the maximum value.
-
sortMaxToMin
public static int[] sortMaxToMin(int[] v)
Returns a sorted array from the maximum to the minimum value.
-
toString
public static java.lang.String toString(int[] array)
Returns a comma delimited string representing the value of the array.
-
toString
public static java.lang.String toString(int[][] array)
Returns a comma delimited string representing the value of the array.
-
percentile
public static double percentile(int[] v, double p)
Gives the percentile of an array.- Parameters:
p
- percentile, must be between 0 and 1- Throws:
java.lang.IllegalArgumentException
- if parameter p is not between 0 and 1.
-
median
public static int median(int[] v)
Computes the median of an array. If the array contains an even number of elements, the lower median is returned.
-
equals
public static boolean equals(int[] a, int[] b)
Deprecated. Use java.util.Arrays instead.Use java.util.Arrays instead. Check if two arrays are equal.
-
abs
public static int[] abs(int[] v)
Takes the absolute value of each component of an array.
-
abs
public static int[][] abs(int[][] v)
Takes the absolute value of each component of an array.
-
max
public static int max(int[] v)
Returns the maximum of an array.
-
max
public static int max(int[][] v)
Returns the maximum of an array.
-
min
public static int min(int[] v)
Returns the minimum of an array.
-
min
public static int min(int[][] v)
Returns the minimum of an array.
-
norm
public static double norm(int[] data)
Computes the L2 norm of an array (Euclidean norm or "length").
-
sumSquares
public static int sumSquares(int[] data)
Sums the squares of all components; also called the energy of the array.
-
sumSquares
public static int sumSquares(int[][] data)
Sums the squares of all components; also called the energy of the array.
-
scalarProduct
public static int scalarProduct(int[] w0, int[] w1)
Computes the scalar product of two array as if they were vectors.- Throws:
java.lang.IllegalArgumentException
- if the don't have the same length.
-
scalarProduct
public static double scalarProduct(int[][] w0, int[][] w1)
Computes the scalar product of two array as if they were matrices.- Throws:
java.lang.IllegalArgumentException
- if the don't have the same length.
-
extract
public static int[] extract(int k0, int k1, int[] invect)
Extracts a sub-array (will invert the resulting array if k0 > k1).- Parameters:
k0
- location of the first component.k1
- location of the last component.
-
invert
public static int[] invert(int[] v)
Inverts an array from left to right.
-
padding
public static int[] padding(int n0, int pos, int[] v)
Fills in with zeroes to get to the specified length; original array with be at the specified position- Parameters:
n0
- length of the new array.pos
- position of the old array.
-
add
public static int[] add(int[] w, double a, int[] v, int p)
Adds to an array w, a times v where a is a scalar. Since v can be smaller then w, we must specified the position at which v will be added.- Parameters:
a
- scalar.p
- position.w
- longer array.v
- shorter array.- Throws:
java.lang.IllegalArgumentException
- if the second array is not shorter than the first one.
-
add
public static int[] add(int[] w, int a)
Adds a scalar to every element in the array.
-
range
public static int[] range(int a, int b)
Generates an array going for a to b with steps of size 1. a can be smaller or larger than b.
-
range
public static int[] range(int b)
Generates an array going for 0 to b with steps of size 1. 0 can be smaller or larger than b.
-
range
public static int[] range(int a, int b, int step)
Generates an array going for a to b with steps of size step. If it can't get to the value b in a finite number of steps, it gets as close as possible (a can be larger or smaller than b)- Parameters:
step
- size of steps, must be positivea
- first value of arrayb
- last value of array- Throws:
java.lang.IllegalArgumentException
- if step is negative or if a=b.
-
transpose
public static int[][] transpose(int[][] M)
Takes the transpose of an array (like the matrix operation).- Throws:
java.lang.IllegalArgumentException
- if the array is not a matrix
-
add
public static int[] add(int[] a, int[] b)
Adds the two arrays together (componentwise).- Throws:
java.lang.IllegalArgumentException
- if the two arrays don't have the same length.
-
subtract
public static int[] subtract(int[] a, int[] b)
Subtracts the two arrays together (componentwise).- Throws:
java.lang.IllegalArgumentException
- if the two arrays don't have the same length.
-
mass
public static int mass(int[] v)
Returns the sum of the elements of the array.
-
scalarMultiply
public static double[] scalarMultiply(double a, int[] v)
Multiplies every component of an array by a scalar.
-
scalarMultiplyFast
public static double[] scalarMultiplyFast(double a, int[] v)
Fast scalar multiplication for sparse arrays.
-
print
public static void print(int[] v)
Prints an array to screen.
-
print
public static void print(int[][] v)
Prints an array to screen.
-
-
DMelt 3.0 © DataMelt by jWork.ORG