## at.tuwien.ifs.somtoolbox.util Class VectorTools

```java.lang.Object
at.tuwien.ifs.somtoolbox.util.VectorTools
```

`public class VectorToolsextends Object`

This class provides several utility methods to operate on Vectors.

Version:
\$Id: VectorTools.java 3883 2010-11-02 17:13:23Z frank \$
Author:
Angela Roiger, Rudolf Mayer

Field Summary
`private static FieldPosition` `fp`

`static NumberFormat` `myFormat`

`private static String` `SPACING`

Constructor Summary
`VectorTools()`

Method Summary
`static double[]` ```add(double[] a, double[] b)```

`static int[]` ```add(int[] a, int[] b)```

`static int` ```calculateArrayOverlaps(int[] array1, int[] array2)```

`static int[]` `computeDefaultSize(int numInstances)`

`static int[]` ```computeDefaultSize(int numInstances, int elementsPerUnit)```

`static boolean[]` ```createBooleanArray(int dim, boolean initialValue)```

`static Point3d` ```crossProduct(Point3d a, Point3d b)```
Calculate the cross product of two 3-dimensional direction vectors.
`static void` `divByMax(cern.colt.matrix.DoubleMatrix2D matrix)`

`static double[][][]` ```divide(double[][][] data, double factor)```

`static double[]` ```divide(double[] a, double b)```

`static boolean` ```equals(double[][] distanceMatrix, double[][] distanceMatrixFromFile)```

`static double[]` ```findMiddle(double[] a, double[] b)```

`static double[]` `generateOneVector(int dim)`

`static cern.colt.matrix.DoubleMatrix1D` `generateRandomDoubleMatrix1D(int dim)`

`static cern.colt.matrix.DoubleMatrix1D` ```generateRandomDoubleMatrix1D(int dim, boolean generateNegativeNumbers)```

`static cern.colt.matrix.DoubleMatrix1D` ```generateRandomDoubleMatrix1D(Random rand, int dim, boolean generateNegativeNumbers)```

`static double[]` `generateRandomVector(int dim)`
Generates a random double[] with the given dimensionality, using a new random number generator.
`static double[]` ```generateRandomVector(int dim, boolean generateNegativeNumbers)```
Generates a random double[] with the given dimensionality, using a new random number generator.
`static double[]` ```generateRandomVector(Random rand, int dim)```
Generates a random double[] with the given dimensionality, using the given random number generator.
`static double[]` ```generateRandomVector(Random rand, int dim, boolean generateNegativeNumbers)```
Generates a random double[] with the given dimensionality, using the given random number generator.
`static double[]` ```generateVectorWithValue(int dim, int value)```

`static int` `getIndexOfMaxValue(double... array)`
Deprecated. not used anymore, marked for removal
`static int` `getIndexOfMaxValue(int... array)`
Gets the index of the max value in an array.
`private static int` ```getIndexOfValue(double[] array, double d, int skip)```
get the index of a given value in an array and skip the given number of indices to avoid duplicate indices in the results, i.e.
`private static int` ```getIndexOfValue(int[] array, int d)```
get the index of a given value in an array
`static int[]` ```getIndicesOfMaxValues(double[] array, double threshold)```
get the indices of the numberOfIndices largest values in an array
`static int[]` ```getIndicesOfMaxValues(double[] array, int numberOfIndices)```
get the max indices of the numberOfIndices largest values in an array
`static int[]` ```getIndicesOfMaxValues(int[] array, int numberOfIndices)```
get the max indices of the numberOfIndices largest values in an array
`static int[]` ```getIndicesOfMaxValuesOLD(int[] array, int numberOfIndices)```
Deprecated. FIXME this is not working marked for removal
`static int` `getMaxValue(int... array)`
Gets the index of the max value in an array.
`static int` ```getNumIndicesLargerThanThreshold(double[] array, double threshold)```
return the indices of all values larger than the given threshold
`static String` `getSpaces(int n)`

`static boolean` `isNullVector(double... vector)`

`static boolean` `isNullVector(int... vector)`

`static void` `main(String[] args)`
main method for testing purposes only.
`static double[]` `meanVector(double[][] a)`
Calculates the mean vector from the given array of vectors.
`static double` `median(double... values)`
Calculates the median of a vector.
`static double[]` `medianVector(double[][] a)`

`static int[]` ```mergeArrays(int[] directions1, int[] directions2)```
merges two direction arrays, i.e.
`static double[][][]` ```multiply(double[][][] data, double factor)```

`static double[]` ```multiply(double[][] matrix, double[] vec)```

`static double[]` ```multiply(double[] a, double b)```

`static double[]` ```multiply(double[] a, double[] b)```

`static double[]` ```multiply(double[] vec, double[][] matrix)```

`static void` `normalise(cern.colt.matrix.DoubleMatrix2D matrix)`
Normalises a matrix.
`static double[]` `normaliseByLength(double... vector)`
Normalises vector elements by the length of the vector, i.e.
`static double[]` ```normaliseByLength(double[] vector, double length)```

`static InputDatum` `normaliseByLength(InputDatum datum)`

`static InputDatum` ```normaliseByLength(InputDatum datum, double length)```

`static double[]` `normaliseVectorToUnitLength(double... vector)`
Calculates vector normalised to unit length.
`static String` `printMatrix(double[][] matrix)`

`static String` ```printMatrix(double[][] m, int d, int w)```
Method for printing a double float matrix
Based on ER Harold, "Java I/O", O'Reilly, around p.
`static String` `printMatrix(int[][] matrix)`

`static String` `printMatrix(Object[][] matrix)`

`static String` ```printMatrixComparison(double[][] matrix, double[][] otherMatrix)```

`static String` ```printVect(double[] m, int d, int w)```
Method printVect for printing a double float vector
Based on ER Harold, "Java I/O", O'Reilly, around p.
`static StringBuffer` `printVector(double... vector)`

`static StringBuffer` `printVector(int... vector)`

`static StringBuffer` ```printVector(int[] vector, String spacing)```

`static StringBuffer` `printVector(Object... vector)`

`static StringBuffer` ```printVector(Object[] vector, String spacing)```

`static double[]` ```removeIndex(double[] array, int indexToRemove)```
Deprecated. use `ArrayUtils.remove(double[], int)` instead
`static int[]` ```removeIndex(int[] array, int indexToRemove)```
Remove an index from an array, it's as simple as that.
`static HashMap<Object,Integer>` `reverseHashMap(Map<Integer,Object> map)`
Creates a `Map` with an inversed mapping of the original map, i.e.
`static double[]` ```slice(double[][] array, int i)```

`static double` `standard_deviation(double... a)`

`static double[]` ```subtract(double[] a, double[] b)```

`static int[]` ```subtract(int[] a, int[] b)```

`static int` `sum(int... array)`

`static double[][]` `transpose(double[][] matrix)`

`static double` `vectorLength(double... vector)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### SPACING

`private static final String SPACING`
Constant Field Values

### myFormat

`public static final NumberFormat myFormat`

### fp

`private static final FieldPosition fp`
Constructor Detail

### VectorTools

`public VectorTools()`
Method Detail

### subtract

```public static double[] subtract(double[] a,
double[] b)```

### subtract

```public static int[] subtract(int[] a,
int[] b)```

```public static double[] add(double[] a,
double[] b)```

```public static int[] add(int[] a,
int[] b)```

### multiply

```public static double[] multiply(double[] a,
double[] b)
throws MetricException```
Throws:
`MetricException`

### multiply

```public static double[] multiply(double[] a,
double b)```

### divide

```public static double[] divide(double[] a,
double b)```

### generateRandomVector

`public static double[] generateRandomVector(int dim)`
Generates a random double[] with the given dimensionality, using a new random number generator.

Parameters:
`dim` - the dimensionality of the double[]
Returns:
a random double[]

### generateRandomVector

```public static double[] generateRandomVector(Random rand,
int dim)```
Generates a random double[] with the given dimensionality, using the given random number generator.

Parameters:
`rand` - a random number generator
`dim` - the dimensionality of the double[]
Returns:
a random double[]

### generateRandomVector

```public static double[] generateRandomVector(int dim,
boolean generateNegativeNumbers)```
Generates a random double[] with the given dimensionality, using a new random number generator.

Parameters:
`dim` - the dimensionality of the double[]
`generateNegativeNumbers` - whether to generate negative numbers or not.
Returns:
a random double[]

### generateRandomVector

```public static double[] generateRandomVector(Random rand,
int dim,
boolean generateNegativeNumbers)```
Generates a random double[] with the given dimensionality, using the given random number generator.

Parameters:
`rand` - a random number generator
`dim` - the dimensionality of the double[]
`generateNegativeNumbers` - whether to generate negative numbers or not.
Returns:
a random double[]

### generateRandomDoubleMatrix1D

```public static cern.colt.matrix.DoubleMatrix1D generateRandomDoubleMatrix1D(Random rand,
int dim,
boolean generateNegativeNumbers)```

### generateRandomDoubleMatrix1D

```public static cern.colt.matrix.DoubleMatrix1D generateRandomDoubleMatrix1D(int dim,
boolean generateNegativeNumbers)```

### generateRandomDoubleMatrix1D

`public static cern.colt.matrix.DoubleMatrix1D generateRandomDoubleMatrix1D(int dim)`

### normalise

`public static void normalise(cern.colt.matrix.DoubleMatrix2D matrix)`
Normalises a matrix.

Parameters:
`matrix` - the matrix to normalise.

### divByMax

`public static void divByMax(cern.colt.matrix.DoubleMatrix2D matrix)`

### normaliseByLength

`public static double[] normaliseByLength(double... vector)`
Normalises vector elements by the length of the vector, i.e. the length of the vector will become 1.

### vectorLength

`public static double vectorLength(double... vector)`

### normaliseByLength

```public static double[] normaliseByLength(double[] vector,
double length)```

### normaliseByLength

`public static InputDatum normaliseByLength(InputDatum datum)`

### normaliseByLength

```public static InputDatum normaliseByLength(InputDatum datum,
double length)```

### median

`public static double median(double... values)`
Calculates the median of a vector.

Parameters:
`values` - the vector elements
Returns:
the median value

### standard_deviation

`public static double standard_deviation(double... a)`
Parameters:
`a` - an array
Returns:
the standard deviation

### createBooleanArray

```public static boolean[] createBooleanArray(int dim,
boolean initialValue)```

### reverseHashMap

`public static HashMap<Object,Integer> reverseHashMap(Map<Integer,Object> map)`
Creates a `Map` with an inversed mapping of the original map, i.e. a valu e-> key mapping.

### meanVector

`public static double[] meanVector(double[][] a)`
Calculates the mean vector from the given array of vectors.

### medianVector

`public static double[] medianVector(double[][] a)`

### printMatrix

`public static String printMatrix(double[][] matrix)`

### printMatrix

`public static String printMatrix(Object[][] matrix)`

### printMatrix

`public static String printMatrix(int[][] matrix)`

### printMatrix

```public static String printMatrix(double[][] m,
int d,
int w)```
Method for printing a double float matrix
Based on ER Harold, "Java I/O", O'Reilly, around p. 473.

Parameters:
`m` - input matrix values, double
`d` - display precision, number of decimal places
`w` - display precision, total width of floating value

### printVect

```public static String printVect(double[] m,
int d,
int w)```
Method printVect for printing a double float vector
Based on ER Harold, "Java I/O", O'Reilly, around p. 473.

Parameters:
`m` - input vector of length m.length
`d` - display precision, number of decimal places
`w` - display precision, total width of floating value

### getSpaces

`public static String getSpaces(int n)`

### printVector

`public static StringBuffer printVector(double... vector)`

### printVector

`public static StringBuffer printVector(int... vector)`

### printVector

```public static StringBuffer printVector(int[] vector,
String spacing)```

### printVector

`public static StringBuffer printVector(Object... vector)`

### printVector

```public static StringBuffer printVector(Object[] vector,
String spacing)```

### printMatrixComparison

```public static String printMatrixComparison(double[][] matrix,
double[][] otherMatrix)```

### equals

```public static boolean equals(double[][] distanceMatrix,
double[][] distanceMatrixFromFile)```

### generateOneVector

`public static double[] generateOneVector(int dim)`

### generateVectorWithValue

```public static double[] generateVectorWithValue(int dim,
int value)```

### transpose

`public static double[][] transpose(double[][] matrix)`

### main

`public static void main(String[] args)`
main method for testing purposes only.

### multiply

```public static double[] multiply(double[][] matrix,
double[] vec)```

### multiply

```public static double[] multiply(double[] vec,
double[][] matrix)```

### getIndexOfMaxValue

`public static int getIndexOfMaxValue(int... array)`
Gets the index of the max value in an array.

Parameters:
`array` - the input array
Returns:
index of the maximum value in the array

### getMaxValue

`public static int getMaxValue(int... array)`
Gets the index of the max value in an array.

Parameters:
`array` - the input array
Returns:
index of the maximum value in the array

### getIndexOfMaxValue

```@Deprecated
public static int getIndexOfMaxValue(double... array)```
Deprecated. not used anymore, marked for removal

Gets the index of the max value in an array (for a double array his time).

Parameters:
`array` - the input array
Returns:
index of the maximum value in the array

### getIndicesOfMaxValues

```public static int[] getIndicesOfMaxValues(double[] array,
int numberOfIndices)```
get the max indices of the numberOfIndices largest values in an array

Parameters:
`array` - the input array
`numberOfIndices` - the desired number of indices to get in return
Returns:
indices of largest values in the array

### getIndicesOfMaxValues

```public static int[] getIndicesOfMaxValues(int[] array,
int numberOfIndices)```
get the max indices of the numberOfIndices largest values in an array

Parameters:
`array` - the input array
`numberOfIndices` - how many
Returns:
the indices

### getIndicesOfMaxValues

```public static int[] getIndicesOfMaxValues(double[] array,
double threshold)```
get the indices of the numberOfIndices largest values in an array

Parameters:
`array` - input
`threshold` - value to compare to
Returns:
indices of all values larger than the given threshold

### getNumIndicesLargerThanThreshold

```public static int getNumIndicesLargerThanThreshold(double[] array,
double threshold)```
return the indices of all values larger than the given threshold

Parameters:
`array` - input
`threshold` - to compare to
Returns:
number of indices containing a value larger than the threshold

### getIndexOfValue

```private static int getIndexOfValue(double[] array,
double d,
int skip)```
get the index of a given value in an array and skip the given number of indices to avoid duplicate indices in the results, i.e. if the array is [12, 12, 5, 2] we will return 0 if skip = 0 and 1 if skip = 1

Parameters:
`array` - the input array
`d` - the value to search for
Returns:
the index

### getIndexOfValue

```private static int getIndexOfValue(int[] array,
int d)```
get the index of a given value in an array

Parameters:
`array` - the input array
`d` - the value to search for
Returns:
the index

### getIndicesOfMaxValuesOLD

```@Deprecated
public static int[] getIndicesOfMaxValuesOLD(int[] array,
int numberOfIndices)```
Deprecated. FIXME this is not working marked for removal

Ever needed a certain number of indices with maximum values sorted in descending order? Well, here it comes.

Parameters:
`array` - an array of doubles
`numberOfIndices` - the number of max values you want the indices of
Returns:
the indices of these max values in the given array

### removeIndex

```@Deprecated
public static double[] removeIndex(double[] array,
int indexToRemove)```
Deprecated. use `ArrayUtils.remove(double[], int)` instead

Remove an index from an array, it's as simple as that.

Parameters:
`array` - the input array
`indexToRemove` - the index to remove
Returns:
an array of size array.length - 1 FIXME how about templates for this one?

### removeIndex

```public static int[] removeIndex(int[] array,
int indexToRemove)```
Remove an index from an array, it's as simple as that.

Parameters:
`array` - the input array
`indexToRemove` - index to remove
Returns:
an array of size array.length - 1 FIXME how about templates for this one?

### sum

`public static int sum(int... array)`

### calculateArrayOverlaps

```public static int calculateArrayOverlaps(int[] array1,
int[] array2)```

### mergeArrays

```public static int[] mergeArrays(int[] directions1,
int[] directions2)```
merges two direction arrays, i.e. {1, 0, 0} and {0, 0, 1} become {1, 0, 1

Parameters:
`directions1` - direction one
`directions2` - direction two
Returns:
the merged direction

### multiply

```public static double[][][] multiply(double[][][] data,
double factor)```

### divide

```public static double[][][] divide(double[][][] data,
double factor)```

### isNullVector

`public static boolean isNullVector(double... vector)`

### isNullVector

`public static boolean isNullVector(int... vector)`

### normaliseVectorToUnitLength

`public static double[] normaliseVectorToUnitLength(double... vector)`
Calculates vector normalised to unit length.

Parameters:
`vector` - Vector to be normalised
Returns:
The normalised vector.

### crossProduct

```public static Point3d crossProduct(Point3d a,
Point3d b)```
Calculate the cross product of two 3-dimensional direction vectors. This is needed to check whether two lines are parallel or not. At first you gotta get two direction vectors by subtracting x, y, and z values. Then you can calculate the cross product. If the cross product = [0 0 0] you found your parallel lines

Parameters:
`a` - first direction vector
`b` - second direction vector
Returns:
cross product of a and b

### computeDefaultSize

`public static int[] computeDefaultSize(int numInstances)`

### computeDefaultSize

```public static int[] computeDefaultSize(int numInstances,
int elementsPerUnit)```

### slice

```public static double[] slice(double[][] array,
int i)```

### findMiddle

```public static double[] findMiddle(double[] a,
double[] b)```