esraMath
Class BLA

java.lang.Object
  extended byesraMath.BLA

public class BLA
extends java.lang.Object

Provides basic linear algebra routines such as vector additions, multiplications, inner, outer products, basic matrix operations such as transposition, inversion etc.

Version:
0.1, April 2005
Author:
Vincent Kraeutler and Mika Kastenholz

Field Summary
static double TOLERANCE
           
 
Constructor Summary
BLA()
           
 
Method Summary
static double[][][] add(double[][][] t1, double[][][] t2)
          same as the other add's, but for tensors of rank 3.
static double[][] add(double[][] m1, double[][] m2)
          add two matrices.
static double[] add(double[] aa, double[] bb)
          add two vectors.
static double angle(double[] i, double[] j)
          the (minimum) angle between two vectors.
static int[] append(int[] aa, int bb)
          appends an int at the end of an int[].
static int[] append(int[] aa, int[] bb)
          concatenates two int[]
static double[][] block(double[][] matrix, int x1, int y1, int x2, int y2)
           
static double[] circularPermutation(double[] vector, int offset)
          {x1, x2, -breakiterator , xn} -> {x(n - m), x(n - m + 1), breakiterator , x1, x2, breakiterator , x(n - m - 1)}
static int[][] combinations(int[] aa, int[] bb)
           
static double[] copy(double[] aa)
           
static double[][] copy(double[][] matrix)
           
static double cosab(double[] a, double[] b)
          the cos(angle) between two vectors.
static double[] cross(double[] r1, double[] r2)
          the cross product of two 3-vectors.
static double det3x3(double[][] a)
           
static double[][] diagonal(double[] diag)
          generate a diagonal matrix with the elements of diag on its diagonal.
static void diagonalizeSymmetric(double[][] matrix, double[] eigenvalues)
          public Method diagonalizeSymmetric diagonalizes a symmetric matrix by first reducing it to tridiagonal form (private void method: tred2) and then evaluating eigenvalues and eigenvectors using the QL algorithm (private void method tqli).
static double[][] diagonalMatmul(double[] vector, double[][] matrix)
          fuctionally equivalent to matmul(diagonal(vector), matrix) but better performance
static double distance(double[] i, double[] j)
          the distance between two vectors.
static double dot(double[] r1, double[] r2)
          the dot product of two vectors
static double[] flatten(double[][] matrix)
           
static double[][] flatten(double[][][] tensor)
           
static void floyd(int[][] matrix, int Nnodes)
          public Method floyd calculates the shortest path through an adjacency matrix.
static void heapsort(double[] values, int n, int[] key)
           
static double[] invert(double[] aa)
          {aa1, aa2, -breakiterator} -> { 1 / aa1, 1 / aa2, ...}
static double[][] invert(double[][] matrix)
          Naive and slow (but simple) matrix inversion routine ("Complete Gauss-Jordan").
static double[] matmul(double[][] mm, double[] vv)
          multiplication of an m x n matrix with an n-vector.
static double[][] matmul(double[][] m1, double[][] m2)
          multiplication of an n x m - matrix with a m x k-matrix, resulting in an n x k - matrix.
static double[] multiplyElements(double[] aa, double[] weights)
          element-wise multiplication of two vectors.
static double norm(double[] r)
          the euclidean norm of a vector
static double norm2(double[] r)
          the squared euclidean norm of a vector
static double[] normalize(double[] r)
          normalize a vector by scaling it by its inverse norm.
static double[][] outer(double[] r1, double[] r2)
          Compute the outer product of two vectors r1, r2.
static int[] range(int j)
           
static int[] range(int lower, int upper)
           
static double[] same(int ii, double value)
          initialize a vector of length ii with values value
static int[] same(int ii, int value)
          initialize a vector of length ii with values value
static double[][][] scale(double[][][] tt, double dd)
          Scale a 3rd order tensor by some scalar.
static double[][] scale(double[][] mm, double vv)
           
static double[] scale(double[] aa, double cc)
          multiply all elements of a vector by some fixed number.
static double[][] select(double[][] matrix, int[] selection)
          returns a selected number of rows of the matrix
static double[] select(double[] array, int[] selection)
          returns a selected part of the array
static int[] select(int[] array, int[] selection)
          returns a selected part of the array
static double[][] shift(double[][] positions, double[] change)
          Add a N-vector change to each row of the M x N - Matrix positions.
static void sort(double[][] mat, double[] eigenvalues)
           
static double sqrt_sum(double a, double b)
          Return the (a^2 + b^2)^(1/2) without over- or underflow
static double[][] subtract(double[][] m1, double[][] m2)
          subtract two matrices.
static double[] subtract(double[] aa, double[] bb)
          subtract two vectors (bb - aa).
static double sum(double[] r)
           
static void swapColumns(double[][] mat, int i, int j)
           
static java.lang.String toString(double[][] mm)
           
static java.lang.String toString(double[][] mm, java.lang.String format)
           
static void tqli(double[][] matrix, double[] diagonal, double[] subdiagonal)
          QL algorithm with implicit shifts, to determine the eigenvalues and eigenvectors of a real, symmetric, tridiagonal matrix
static double[][] transpose(double[][] m1)
           
static int[][] transpose(int[][] m1)
           
private static void tred2(double[][] matrix, double[] diagonal, double[] offdiagonal)
          Householder reduction of a real, symmetric matrix (double mat[][])
static int[] unique(int[] aa)
          return the unique elements of the (sorted) array.
static double[] zeroes(int ii)
           
static double[][] zeroes(int ii, int jj)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOLERANCE

public static final double TOLERANCE
See Also:
Constant Field Values
Constructor Detail

BLA

public BLA()
Method Detail

range

public static int[] range(int lower,
                          int upper)
                   throws java.lang.IllegalArgumentException
Parameters:
lower - limit of the range (inclusive)
upper - limit of the range (exclusive)
Returns:
the range of integers [lower, upper)
Throws:
java.lang.IllegalArgumentException

range

public static int[] range(int j)
Parameters:
j - the upper limit of the range (exclusive).
Returns:
the range [0, j)

append

public static int[] append(int[] aa,
                           int[] bb)
concatenates two int[]

Parameters:
aa - the first one
bb - the second one
Returns:
the concatenated one

append

public static int[] append(int[] aa,
                           int bb)
appends an int at the end of an int[]. evil!! evil!! but handy!!

Parameters:
aa - the array
bb - the int to be appended
Returns:
the concatenated one

unique

public static int[] unique(int[] aa)
return the unique elements of the (sorted) array.

Parameters:
aa - the array
Returns:
the concatenated one

same

public static int[] same(int ii,
                         int value)
initialize a vector of length ii with values value

Parameters:
ii -
value -
Returns:
the new vector {value, value, ..., value}

same

public static double[] same(int ii,
                            double value)
initialize a vector of length ii with values value

Parameters:
ii -
value -
Returns:
the new vector {value, value, value, ..., value}

zeroes

public static double[] zeroes(int ii)
                       throws java.lang.IllegalArgumentException
Parameters:
ii - the size of the vector
Returns:
a zero-vector (double[] )of the appropriate size
Throws:
java.lang.IllegalArgumentException

circularPermutation

public static double[] circularPermutation(double[] vector,
                                           int offset)
{x1, x2, -breakiterator , xn} -> {x(n - m), x(n - m + 1), breakiterator , x1, x2, breakiterator , x(n - m - 1)}

Parameters:
vector -
offset -
Returns:
the permuted vector

add

public static double[] add(double[] aa,
                           double[] bb)
add two vectors.

Parameters:
aa -
bb -
Returns:
aa + bb
Throws:
java.lang.IllegalArgumentException

subtract

public static double[] subtract(double[] aa,
                                double[] bb)
                         throws java.lang.IllegalArgumentException
subtract two vectors (bb - aa).

Parameters:
aa -
bb -
Returns:
the difference of the two vectors (bb - aa)
Throws:
java.lang.IllegalArgumentException

multiplyElements

public static double[] multiplyElements(double[] aa,
                                        double[] weights)
                                 throws java.lang.IllegalArgumentException
element-wise multiplication of two vectors. {aa1, aa2, ...} * {ww1, ww2, ...} = {aa1 * ww1, aa2 * ww2, ...}

Parameters:
aa -
weights -
Returns:
{aa1 * ww1, aa2 * ww2, ...}
Throws:
java.lang.IllegalArgumentException

scale

public static double[] scale(double[] aa,
                             double cc)
multiply all elements of a vector by some fixed number. cc {aa1, aa2, ...} = {cc * aa1, cc * aa2, ...}

Parameters:
aa -
cc -
Returns:
teh new vector aa * cc

copy

public static double[] copy(double[] aa)
Parameters:
aa -
Returns:
a (inefficient) copy of aa

invert

public static double[] invert(double[] aa)
{aa1, aa2, -breakiterator} -> { 1 / aa1, 1 / aa2, ...}

Parameters:
aa -
Returns:
{1 / aa1, 1 / aa2, ...}

shift

public static double[][] shift(double[][] positions,
                               double[] change)
Add a N-vector change to each row of the M x N - Matrix positions.

Parameters:
positions -
change -
Returns:
a new vector positions + change

cosab

public static double cosab(double[] a,
                           double[] b)
the cos(angle) between two vectors.

Parameters:
a -
b -
Returns:
cos(angle)

angle

public static final double angle(double[] i,
                                 double[] j)
the (minimum) angle between two vectors. remark: typically, the norm of a vector is just a tiny bit bigger than one due to roundoff errors. in this case (i.e. when determining the angle between a vector and itself), the current java implementation of Math.acos returns NaN. We try to handle that gracefully, by setting cosines which are in the range (1, 1 + eps) to 1 before performing the acos operation.

Parameters:
i -
j -
Returns:
the angle (radians)

distance

public static final double distance(double[] i,
                                    double[] j)
the distance between two vectors.

Parameters:
i -
j -
Returns:
the distance

dot

public static final double dot(double[] r1,
                               double[] r2)
                        throws java.lang.IllegalArgumentException
the dot product of two vectors

Parameters:
r1 -
r2 -
Returns:
r1 . r2
Throws:
java.lang.IllegalArgumentException

norm2

public static final double norm2(double[] r)
the squared euclidean norm of a vector

Parameters:
r -
Returns:
dot(r, r)

norm

public static final double norm(double[] r)
the euclidean norm of a vector

Parameters:
r -
Returns:
Math.sqrt(norm2(r))

normalize

public static final double[] normalize(double[] r)
normalize a vector by scaling it by its inverse norm. if the vector is the origin (i.e. the zero-vector), a copy of the origin is returned.

Parameters:
r -
Returns:
the normalized vector

outer

public static final double[][] outer(double[] r1,
                                     double[] r2)
                              throws java.lang.IllegalArgumentException
Compute the outer product of two vectors r1, r2. The vectors must have the same length.

Parameters:
r1 - Vector
r2 - Vector
Returns:
double[r1.length][r1.length], the outer product matrix
Throws:
java.lang.IllegalArgumentException

cross

public static final double[] cross(double[] r1,
                                   double[] r2)
the cross product of two 3-vectors.
maybe replace this with mathml? [\mathord{\buildrel{\lower3pt\hbox{$\scriptscriptstyle\rightharpoonup$}} \over r} _{cross_{ij} } = \mathord{\buildrel{\lower3pt\hbox{$\scriptscriptstyle\rightharpoonup$}} \over r} _i \times \mathord{\buildrel{\lower3pt\hbox{$\scriptscriptstyle\rightharpoonup$}} \over r} _j

Parameters:
r1 - Vector (double[3]) 1 for the cross product calculation
r2 - Vector (double[3]) 2 for the cross product calculation
Returns:
double[3], the cross product vector

combinations

public static final int[][] combinations(int[] aa,
                                         int[] bb)

select

public static final int[] select(int[] array,
                                 int[] selection)
returns a selected part of the array

Parameters:
array -
selection -
Returns:
the new array

select

public static final double[] select(double[] array,
                                    int[] selection)
returns a selected part of the array

Parameters:
array -
selection -
Returns:
the new array

sum

public static final double sum(double[] r)
Parameters:
r -
Returns:
the sum of elements contained in the vector

zeroes

public static final double[][] zeroes(int ii,
                                      int jj)
Parameters:
ii -
jj -
Returns:
a zero-filled matrix of size ii x jj

diagonal

public static final double[][] diagonal(double[] diag)
generate a diagonal matrix with the elements of diag on its diagonal.

Parameters:
diag -
Returns:
the diagonal matrix

flatten

public static final double[] flatten(double[][] matrix)
Parameters:
matrix -
Returns:
a vector

flatten

public static final double[][] flatten(double[][][] tensor)
Parameters:
tensor - a rank3 tensor
Returns:
a vector

add

public static final double[][] add(double[][] m1,
                                   double[][] m2)
add two matrices.

Parameters:
m1 -
m2 -
Returns:
the sum matrix

add

public static final double[][][] add(double[][][] t1,
                                     double[][][] t2)
same as the other add's, but for tensors of rank 3.

Parameters:
t1 -
t2 -
Returns:
the sum

scale

public static final double[][][] scale(double[][][] tt,
                                       double dd)
Scale a 3rd order tensor by some scalar.

Parameters:
tt -
dd -
Returns:
the scaled tensor

subtract

public static final double[][] subtract(double[][] m1,
                                        double[][] m2)
subtract two matrices.

Parameters:
m1 -
m2 -
Returns:
m2 - m1

transpose

public static final int[][] transpose(int[][] m1)
Parameters:
m1 -
Returns:
the transpose of m1

transpose

public static final double[][] transpose(double[][] m1)
Parameters:
m1 -
Returns:
the transpose of m1

det3x3

public static final double det3x3(double[][] a)
Parameters:
a -
Returns:
the determinant of a 3x3 matrix

matmul

public static final double[][] matmul(double[][] m1,
                                      double[][] m2)
multiplication of an n x m - matrix with a m x k-matrix, resulting in an n x k - matrix.

Parameters:
m1 -
m2 -
Returns:
m1 . m2

diagonalMatmul

public static final double[][] diagonalMatmul(double[] vector,
                                              double[][] matrix)
fuctionally equivalent to matmul(diagonal(vector), matrix) but better performance

Parameters:
vector -
matrix -
Returns:
matmul(diagonal(vector), matrix)

matmul

public static final double[] matmul(double[][] mm,
                                    double[] vv)
multiplication of an m x n matrix with an n-vector.

Parameters:
mm -
vv -
Returns:
mm . vv

scale

public static final double[][] scale(double[][] mm,
                                     double vv)
Parameters:
mm -
vv -
Returns:
the new matrix mm * vv

copy

public static final double[][] copy(double[][] matrix)
Parameters:
matrix -
Returns:
a deep copy of matrix

select

public static final double[][] select(double[][] matrix,
                                      int[] selection)
returns a selected number of rows of the matrix

Parameters:
matrix -
selection -
Returns:
the new array

block

public static final double[][] block(double[][] matrix,
                                     int x1,
                                     int y1,
                                     int x2,
                                     int y2)
Parameters:
matrix -
x1 -
y1 -
x2 -
y2 -
Returns:
part of matrix ranging from x1 to x2 (exclusive) and y1 to y2 (exclusive)

invert

public static double[][] invert(double[][] matrix)
Naive and slow (but simple) matrix inversion routine ("Complete Gauss-Jordan").

Parameters:
matrix -
Returns:
inverse

diagonalizeSymmetric

public static void diagonalizeSymmetric(double[][] matrix,
                                        double[] eigenvalues)
public Method diagonalizeSymmetric diagonalizes a symmetric matrix by first reducing it to tridiagonal form (private void method: tred2) and then evaluating eigenvalues and eigenvectors using the QL algorithm (private void method tqli). (see: Numerical Recipes in C. 2nd edition. page 469ff.)

Parameters:
matrix - symmetric matrix double[][]
eigenvalues - an array of eigenvalues double[]

tred2

private static void tred2(double[][] matrix,
                          double[] diagonal,
                          double[] offdiagonal)
Householder reduction of a real, symmetric matrix (double mat[][])

Parameters:
matrix - real, symmetric matrix double[][], replaced by its orthogonal matrix upon return
diagonal - returns the diagonal elements of the tridiagonal matrix
offdiagonal - returns the offdiagonal elements of the tridiagonal matrix

tqli

public static void tqli(double[][] matrix,
                        double[] diagonal,
                        double[] subdiagonal)
QL algorithm with implicit shifts, to determine the eigenvalues and eigenvectors of a real, symmetric, tridiagonal matrix

Parameters:
matrix - reduced matrix from householder reduction routine tred2
diagonal - diagonal elements of the tridiagonal matrix; replaced the eigenvalues upon return.
subdiagonal - subdiagonal elements of the tridiagonal matrix

sqrt_sum

public static double sqrt_sum(double a,
                              double b)
Return the (a^2 + b^2)^(1/2) without over- or underflow

Parameters:
a -
b -
Returns:
(a^2 + b^2)^(1/2)

sort

public static void sort(double[][] mat,
                        double[] eigenvalues)

swapColumns

public static void swapColumns(double[][] mat,
                               int i,
                               int j)

floyd

public static void floyd(int[][] matrix,
                         int Nnodes)
public Method floyd calculates the shortest path through an adjacency matrix. COMMUNICATIONS OF THE ACM 5 (6): 345-345 1962
This is of O(n^3), so watch out!

Parameters:
matrix - A double[Nnodes][Nnodes] adjacency matrix containing some distances.
Nnodes - number of nodes in the matrix
Returns:
the original matrix now containing the shortest path distances

heapsort

public static void heapsort(double[] values,
                            int n,
                            int[] key)

toString

public static java.lang.String toString(double[][] mm,
                                        java.lang.String format)

toString

public static java.lang.String toString(double[][] mm)