de.unibi.techfak.misc
Class MathTools

java.lang.Object
  extended by de.unibi.techfak.misc.MathTools

public class MathTools
extends java.lang.Object

Class for holding methods with mathmatical background, such as matrix operations, list sorting, and so on.


Field Summary
private static long serialVersionUID
          The seriel version number of this class.
 
Constructor Summary
MathTools()
           
 
Method Summary
static double[][] array2matrix(double[] arr, int rows, int cols)
          Creates a matrix of cols columns and rows rows and fills it with the given array.
static double[] arraycpy(double[] dest, double[] src)
          Copies the 'src' array into 'dest'.
static int[] arraycpy(int[] dest, int[] src)
          Copies the 'src' array into 'dest'.
static int dist(int one, int two)
          Calculates and returnes the distance between the two int values.
static int[] extendArray(int before, int[] array, int after)
          The given array is changed in its size.
static double[][] matrixadd(double[][] dest, double[][] add)
          Adds two double matrices, the first matrix is overwritten (addition is in-place) and returned.
static double[][] matrixclone(double[][] m)
          Clones a matrix of doubles deeply.
static double[][] matrixclr(double[][] m)
          The matrix entries are set to zero.
static double[][] matrixcpy(double[][] dest, double[][] src)
          Copies the 'src' matrix into 'dest'.
static int[][] matrixcpy(int[][] dest, int[][] src)
          Copies the 'src' matrix into 'dest'.
static java.lang.Double[][] matrixCut(java.lang.Double[][] d, int rows, int cols)
          A matrix of Doubles is cutted or pumped up to the new Dimension.
static double matrixdiff(double[][] m1, double[][] m2)
          The distances between two values of the same coordinates are summed-up.
static java.lang.Double[][] matrixNew(double[][] d, double nullV)
          Creates a new matrix of Doubles from a matrix of doubles.
static double[][] matrixNew(java.lang.Double[][] D, double nullV, boolean ignoreNullRows)
          Creates a new matrix of doubles from a matrix of Doubles.
static void matrixprint(double[][] m, java.io.PrintStream out)
          Prints a matrix 'm' to a given PrintStream.
static double[][] matrixset(double[][] m, double value)
          All entries of the matrix 'm' are set to 'value'.
static java.util.LinkedList mergeLists(java.util.LinkedList[] al, boolean destroy, boolean storeOrigin, boolean permitDouble)
          Merges all lists in the array to one linked list.
static double round(double nr, int fig)
          Rounds a given number with the number of figures after the decimal point.
static double round2(double nr, int fig)
          Rounds a given number with the number of significant figures.
static int signum(int n)
          Tests and returns the signum of an integer value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
The seriel version number of this class. Starts with 1000L, every new method increases this number.

See Also:
Constant Field Values
Constructor Detail

MathTools

public MathTools()
Method Detail

matrixclone

public static double[][] matrixclone(double[][] m)
Clones a matrix of doubles deeply.

Parameters:
m - The matrix to be cloned.
Returns:
The cloned matrix.

matrixadd

public static double[][] matrixadd(double[][] dest,
                                   double[][] add)
Adds two double matrices, the first matrix is overwritten (addition is in-place) and returned. It is an error, if the dest matrix is null or if the destination matrix is greater in size than the add matrix. Otherwise it is allowed, that the add matrix is greater than the dest matrix, because the dest matrix should be covered fully by the add matrix.

Parameters:
dest - The destination matrix to get overwritten.
add - The matrix to be added to the destination matrix.
Returns:
The overwritten 'dest' matrix, null in case of error

matrixset

public static double[][] matrixset(double[][] m,
                                   double value)
All entries of the matrix 'm' are set to 'value'.

Parameters:
m - The matrix to get overwritten.
value - The value to overwrite every matrix entry.
Returns:
A pointer to the matrix 'm'

matrixclr

public static double[][] matrixclr(double[][] m)
The matrix entries are set to zero. This is equivalent to the call: matrixset( m, 0.0 );

Parameters:
m - The matrix to get cleared.
Returns:
A pointer to the matrix 'm'
See Also:
matrixset(double[][], double)

matrixcpy

public static double[][] matrixcpy(double[][] dest,
                                   double[][] src)
Copies the 'src' matrix into 'dest'. Overwrites 'dest'. The matrices need not to be of same size. The lesser dimensions are always taken. Example: imagine 'src' to be of [8,9] and 'dest' of [5,10]. Then the [5,9]-part of 'src' is only copied into 'dest'.

Parameters:
dest - The destination matrix to be overwritten.
src - The source matrix.
Returns:
A pointer to the 'dest' matrix, null in case of error.

matrixcpy

public static int[][] matrixcpy(int[][] dest,
                                int[][] src)
Copies the 'src' matrix into 'dest'. Overwrites 'dest'. The matrices need not to be of same size. The lesser dimensions are always taken. Example: imagine 'src' to be of [8,9] and 'dest' of [5,10]. Then the [5,9]-part of 'src' is only copied into 'dest'.

Parameters:
dest - The destination matrix to be overwritten.
src - The source matrix.
Returns:
A pointer to the 'dest' matrix, null in case of error.

matrixdiff

public static double matrixdiff(double[][] m1,
                                double[][] m2)
The distances between two values of the same coordinates are summed-up. If the matrices are not of the same size, the lesser value is taken for both rows and columns.
Note, that this method can be used for check of identity.

Parameters:
m1 - The first matrix.
m2 - The second matrix.
Returns:
The sum of all distances

matrixprint

public static void matrixprint(double[][] m,
                               java.io.PrintStream out)
Prints a matrix 'm' to a given PrintStream.

Parameters:
m - The matrix to be printed.
out - The PrintStream to write to.

mergeLists

public static java.util.LinkedList mergeLists(java.util.LinkedList[] al,
                                              boolean destroy,
                                              boolean storeOrigin,
                                              boolean permitDouble)
                                       throws java.lang.ClassCastException
Merges all lists in the array to one linked list.
There are 3 boolean fields which specify the merge behavior. By setting destroy = true the given lists are destroyed and are empty after merging, otherwise the lists are untouched. Note, that in either case the objects are not cloned, what means that unless the source list is not destroyed both lists refer to the same elements.
By setting storeOrigin = true the result list holds befor every value an Integer in range [0, l.length-1] which specifys the list where the value comes from. Setting permitDouble = true let the method copy only the first of repeatedly occuring values regardless whether they occur in the same list or in different lists.
This method works in O( k*k*n ), where k is the number of lists in the array and n is the length of the lists.
It is permitted, that lists in the array can be null or empty. null values in the lists will not copied to the result list. After merging with destroy = true the lists consists of only null-entries.

Parameters:
al - An array of LinkedLists to merge.
destroy - Whether or not to clear the original lists.
storeOrigin - Whether or not storing the origin of the value.
permitDouble - Whether or not permitting the same values.
Returns:
Returns a newly generated LinkedList with the elements of the given lists.
Throws:
java.lang.ClassCastException - - if one element is not comparable to the other ones. The source lists might be changed in that case.

matrixNew

public static double[][] matrixNew(java.lang.Double[][] D,
                                   double nullV,
                                   boolean ignoreNullRows)
Creates a new matrix of doubles from a matrix of Doubles. Entries of null are translated to nullV, e.g. Double.NaN.

Parameters:
D - The matrix of Doubles.
nullV - The double representative of null.
ignoreNullRows - Ignores complete rows with only null values.
Returns:
The newly created matrix.

matrixNew

public static java.lang.Double[][] matrixNew(double[][] d,
                                             double nullV)
Creates a new matrix of Doubles from a matrix of doubles. Entries, that are equal to nullV are translated to null.

Parameters:
d - The matrix of doubles.
nullV - The double representative of null.
Returns:
The newly created matrix.

matrixCut

public static java.lang.Double[][] matrixCut(java.lang.Double[][] d,
                                             int rows,
                                             int cols)
A matrix of Doubles is cutted or pumped up to the new Dimension. A new matrix of Doubles is always generated.

Parameters:
d - The matrix of Doubles.
rows - The new number of rows.
cols - The new number of cols.
Returns:
The newly created matrix.

arraycpy

public static double[] arraycpy(double[] dest,
                                double[] src)
Copies the 'src' array into 'dest'. Overwrites 'dest'. The arrays need not to be of same size. The lesser dimension is taken.

Parameters:
dest - The destination array to be overwritten.
src - The source array.
Returns:
A pointer to the 'dest' array, null in case of error.

arraycpy

public static int[] arraycpy(int[] dest,
                             int[] src)
Copies the 'src' array into 'dest'. Overwrites 'dest'. The arrays need not to be of same size. The lesser dimension is taken.

Parameters:
dest - The destination array to be overwritten.
src - The source array.
Returns:
A pointer to 'dest', null in case of error.

round

public static double round(double nr,
                           int fig)
Rounds a given number with the number of figures after the decimal point. Example: round(1.4156, 3) yields 1.416. If the double number is Infinity or NaN or zero it is not changed at all.

Parameters:
nr - The number to be rounded.
fig - The precision of the rounding.
Returns:
A double representing the rounded number.

round2

public static double round2(double nr,
                            int fig)
Rounds a given number with the number of significant figures. Example: round2(1.4156e-14, 3) yields 1.42e-14. If the double number is Infinity or NaN or zero it is not changed at all.

Parameters:
nr - The number to be rounded.
fig - The number of significant figures.
Returns:
A double representing the rounded number.

extendArray

public static int[] extendArray(int before,
                                int[] array,
                                int after)
The given array is changed in its size. After creating the new array, the content of the old will be copied to the correct position. The newly created leading and trailing positions are filled with the first or last number of the old array, respectively.
Example: array=[1,2,3,4,5,6,7], before=2, after=5 leads to newarray=[1,1,1,2,3,4,5,6,7,7,7,7,7,7]. Negative values for before and after are permitted. before=-2 and after=0 would yield newarray=[3,4,5,6,7].
If before and after are zero, the array is cloned.

Parameters:
before - The number of integers to preced the array with.
array - The array to be extended.
after - The number of integers appent to the array.
Returns:
A new array which contains the old one and newly created positions. It is null returned, if the array is null or less then one position in length.

signum

public static int signum(int n)
Tests and returns the signum of an integer value. The double and float pendants are defined in class Math.

Parameters:
n - The integer to process the signum function on.
Returns:
-1, iff n is negative, zero, iff n is zero, in all other cases 1.

array2matrix

public static double[][] array2matrix(double[] arr,
                                      int rows,
                                      int cols)
Creates a matrix of cols columns and rows rows and fills it with the given array. E.g. arr={1,2,3} cols=2 and rows=3 would yield
 ( 1 2 )
 ( 3 0 )
 ( 0 0 )
 
.

Parameters:
arr - The array to fill the matrix with.
rows - The number of rows in the new matrix.
cols - The number of columns in the new matrix.
Returns:
The new matrix, or null if rows or cols are less than 1.

dist

public static int dist(int one,
                       int two)
Calculates and returnes the distance between the two int values. If the distance is greater or equal Integer.MAX_VALUE this value is returned.

Parameters:
one - The first value for distance calculation.
two - The second value for distance calculation.
Returns:
The distance between the two values given.