genopt.algorithm.util.math
Class LinAlg

java.lang.Object
  extended by genopt.algorithm.util.math.LinAlg

public class LinAlg
extends java.lang.Object

Class with functions for linear algebra.

This project was carried out at:

and supported by

GenOpt Copyright (c) 1998-2011, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.

Version:
GenOpt(R) 3.1.0 (December 8, 2011)

Author:
Michael Wetter

Constructor Summary
LinAlg()
           
 
Method Summary
static double abs(double[] x)
          calculates the absoulte value of a vector
static double[][] add(double[][] A, double[][] B)
          adds 2 matrices: C = A + B;
Note:C has the dimension of A; B must not have the same dimension as A (non existing element are considered as 0 and elements that are not in A but in B are not taken into account by the summation)
static double[] add(double[] y, double[] x)
          adds 2 vectors: z = y + x
Note:If the dimension dx of x is bigger than the dimension dy of y, then only dx elements are added.
static double[][] fillDiagonal(double[][] A, double left, double diagonal, double right)
          fills the diagonal matrix element and sets all other elements to zero.
static double[] gaussElimination(double[][] A, double[] f)
          solves a vector x by a Gauss elimination of a NxN matrix with normalization and interchange of rows.
static double[] gaussEliminationTridiagonal(double[][] A, double[] f)
          solves a vector x by a Gauss elimination of a tridiagonal NxN matrix with normalization and interchange of rows.
static double[] getCenter(double[][] M)
          gets the center point of several points
static double[] getColumn(double[][] A, int i)
          gets a column of a matrix
static double[] getSub(double[] x, int startIndex, int number)
          gets a part of a vector
static void initialize(double[][] A, double v)
          initializes a rectangular matrix
static void initialize(double[] A, double v)
          initializes a vector
static double innerProduct(double[] a, double[] b)
          calculates the inner product (dot product): c[i] = sum(a[i] * b[i] , i = 0..N-1)
static double maxDiff(double[][] A, double[][] B)
          calculates the maximum magnitude of the difference between corresponding matrix element, defined as maxDiff = max(|A[i][j]-A[i][j]|) for all i,j
static double maxNorm(double[] u)
          calculates the Lmax norm of a vector, defined as Lmax(u) = (max(|u(i)|, i = 1..n)
static double maxNorm(double[][] A)
          calculates Lmax norm of a matrix
The max norm of a matrix is the maximum row sum, where the row sum is the sum of the magnitudes of the elements in a given row
static double[] multiply(double[][] A, double[] u)
          multiplicates a matrix with a vector: r = A * u;
(Returns the product of the row vector x and the rectangular array A)
static double[][] multiply(double[][] A, double[][] B)
          multiplicates a matrix with a matrix: C = A * B;
static double[] multiply(double[] u, double[][] A)
          multiplicates a vector with a matrix: r = u^T * A;
(Returns the product of the row vector x and the rectangular array A)
static double[] multiply(double s, double[] u)
          calculates S-multiplication : r = s * u;
static double[][] multiply(double s, double[][] A)
          calculates S-multiplication : B = s * A;
static double[] multiply(double s, int[] u)
          calculates S-multiplication : r = s * u; The computations are done in double.
static double oneNorm(double[] u)
          calculates L1 norm of a vector, defined as L1(u) = (sum(u(i), i = 1..n)
static double oneNorm(double[][] A)
          calculates L1 norm of a matrix
The one-norm of a matrix is the maximum column sum, where the column sum is the sum of the magnitudes of the elements in a given column.
static double[][] outerProduct(double[] a, double[] b)
          calculates the outer product (Tensor product): M[i][j] = a[i] * b[j]
static void print(double[] x)
          prints a vector to the output stream
static void print(double[][] A)
          prints a matrix to the output stream
static void print(double[] state, java.lang.String delimiter)
          reports a scalar and a vector to the output stream, where the entries are separated by the given delimiter
static void print(double time, double[] state)
          reports a scalar and a vector to the output stream
static void print(double time, double[] state, java.lang.String delimiter)
          reports a scalar and a vector to the output stream, where the entries are separated by the given delimiter
static void print(int[] x)
          prints a vector to the output stream
static double[][] setColumn(double[][] A, double[] x, int i)
          sets a column of a matrix
Note: the dimension of x can be smaller than the column length of A
static double[][] setRow(double[][] A, double[] x, int i)
          sets a row of a matrix
Note: the dimension of x can be smaller than the row length of A
static double[][] subtract(double[][] A, double[][] B)
          subtracts 2 matrices: C = A - B;
Note:C has the dimension of A; B must not have the same dimension as A (non existing element are considered as 0 and elements that are not in A but in B are not taken into account by the summation)
static double[] subtract(double[] y, double[] x)
          subtracts 2 vectors: z = y - x;
Note: If the dimension dx of x is bigger than the dimension dy of y, then only dx elements are subtracted.
static int[] subtract(int[] y, int[] x)
          subtracts 2 vectors: z = y - x;
Note: If the dimension dx of x is bigger than the dimension dy of y, then only dx elements are subtracted.
static double sumColumn(double[][] A, int i)
          returns the sum of the elements in the i-th column
static double sumRow(double[][] A, int i)
          returns the sum of the elements in the i-th row
static double twoNorm(double[] u)
          calculates the L2 norm of a vector, defined as L2(u) = (sum(u(i)**2, i = 1..n) ^ 0.5
static double twoNorm(double[] u, double h)
          calculates the L2 norm with a scaling factor of a vector, defined as L2(u) = (sum(h * u(i)**2, i = 1..n) ^ 0.5
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LinAlg

public LinAlg()
Method Detail

getCenter

public static double[] getCenter(double[][] M)
gets the center point of several points

Parameters:
M - Matrix where the row are the points from which the center has to be determined (the row has the first element number, i.e. row i: A[i][0...dim])
Returns:
the center point

maxDiff

public static double maxDiff(double[][] A,
                             double[][] B)
calculates the maximum magnitude of the difference between corresponding matrix element, defined as maxDiff = max(|A[i][j]-A[i][j]|) for all i,j

Parameters:
A - rectangular matrix
B - rectangular matrix of the same dimension like A
Returns:
the maximum magnitude of the difference between corresponding matrix element

abs

public static double abs(double[] x)
calculates the absoulte value of a vector

Parameters:
x - the vector
Returns:
the absolute value of x = ((sum(x(i)**2)**(1/2), i = 0..N-1)

getSub

public static double[] getSub(double[] x,
                              int startIndex,
                              int number)
gets a part of a vector

Parameters:
x - vector
startIndex - index of first element that will be returned
number - number of elements that will be returned
Returns:
i-th column A[k][i] where k = 0...n

getColumn

public static double[] getColumn(double[][] A,
                                 int i)
gets a column of a matrix

Parameters:
A - matrix
i - number of column
Returns:
i-th column A[k][i] where k = 0...n

initialize

public static void initialize(double[] A,
                              double v)
initializes a vector

Parameters:
A - vector
v - value to be set for all elements

initialize

public static void initialize(double[][] A,
                              double v)
initializes a rectangular matrix

Parameters:
A - matrix
v - value to be set for all elements

setColumn

public static double[][] setColumn(double[][] A,
                                   double[] x,
                                   int i)
sets a column of a matrix
Note: the dimension of x can be smaller than the column length of A

Parameters:
A - matrix
x - Vector to be set as the i-th column of A
i - column number where x has to be set
Returns:
matrix A where the i-th column is replaced by x

setRow

public static double[][] setRow(double[][] A,
                                double[] x,
                                int i)
sets a row of a matrix
Note: the dimension of x can be smaller than the row length of A

Parameters:
A - matrix
x - Vector to be set as the i-th row of A
i - row number where x has to be set
Returns:
matrix A where the i-th row is replaced by x

print

public static void print(double[][] A)
prints a matrix to the output stream

Parameters:
A - matrix to be printed

print

public static void print(double[] x)
prints a vector to the output stream

Parameters:
x - Vector to be printed

print

public static void print(int[] x)
prints a vector to the output stream

Parameters:
x - Vector to be printed

fillDiagonal

public static double[][] fillDiagonal(double[][] A,
                                      double left,
                                      double diagonal,
                                      double right)
fills the diagonal matrix element and sets all other elements to zero.

Parameters:
A - square matrix to be filled
left - Element (i-1, j)
diagonal - Element (i, j)
right - Element (i+1, j)
Returns:
Filled matrix

oneNorm

public static double oneNorm(double[][] A)
calculates L1 norm of a matrix
The one-norm of a matrix is the maximum column sum, where the column sum is the sum of the magnitudes of the elements in a given column.

Parameters:
A - matrix
Returns:
L1

maxNorm

public static double maxNorm(double[][] A)
calculates Lmax norm of a matrix
The max norm of a matrix is the maximum row sum, where the row sum is the sum of the magnitudes of the elements in a given row

Parameters:
A - matrix
Returns:
Lmax

oneNorm

public static double oneNorm(double[] u)
calculates L1 norm of a vector, defined as L1(u) = (sum(u(i), i = 1..n)

Parameters:
u - Vector
Returns:
L1

sumRow

public static double sumRow(double[][] A,
                            int i)
returns the sum of the elements in the i-th row

Parameters:
A - Matrix
i - Row number
Returns:
sum(A[i][j], j = 0..N-1)

sumColumn

public static double sumColumn(double[][] A,
                               int i)
returns the sum of the elements in the i-th column

Parameters:
A - Matrix
i - Column number
Returns:
sum(A[j][i], j = 0..N-1)

twoNorm

public static double twoNorm(double[] u)
calculates the L2 norm of a vector, defined as L2(u) = (sum(u(i)**2, i = 1..n) ^ 0.5

Parameters:
u - Vector
Returns:
L2

twoNorm

public static double twoNorm(double[] u,
                             double h)
calculates the L2 norm with a scaling factor of a vector, defined as L2(u) = (sum(h * u(i)**2, i = 1..n) ^ 0.5

Parameters:
u - Vector *param h Scaling factor
Returns:
L2

maxNorm

public static double maxNorm(double[] u)
calculates the Lmax norm of a vector, defined as Lmax(u) = (max(|u(i)|, i = 1..n)

Parameters:
u - Vector
Returns:
Lmax

subtract

public static double[] subtract(double[] y,
                                double[] x)
subtracts 2 vectors: z = y - x;
Note: If the dimension dx of x is bigger than the dimension dy of y, then only dx elements are subtracted.

Parameters:
x - Vector of size dx
y - Vector of size dy (dy >= dx)
Returns:
z Vector of size dx

subtract

public static int[] subtract(int[] y,
                             int[] x)
subtracts 2 vectors: z = y - x;
Note: If the dimension dx of x is bigger than the dimension dy of y, then only dx elements are subtracted.

Parameters:
x - Vector of size dx
y - Vector of size dy (dy >= dx)
Returns:
z Vector of size dx

add

public static double[] add(double[] y,
                           double[] x)
adds 2 vectors: z = y + x
Note:If the dimension dx of x is bigger than the dimension dy of y, then only dx elements are added.

Parameters:
x - Vector of size dx
y - Vector of size dy (dy >= dx)
Returns:
z Vector of size dx

add

public static double[][] add(double[][] A,
                             double[][] B)
adds 2 matrices: C = A + B;
Note:C has the dimension of A; B must not have the same dimension as A (non existing element are considered as 0 and elements that are not in A but in B are not taken into account by the summation)

Parameters:
A - Matrix of any dimension
B - Matrix of any dimension
Returns:
C Matrix of dimension A

subtract

public static double[][] subtract(double[][] A,
                                  double[][] B)
subtracts 2 matrices: C = A - B;
Note:C has the dimension of A; B must not have the same dimension as A (non existing element are considered as 0 and elements that are not in A but in B are not taken into account by the summation)

Parameters:
A - Matrix of any dimension
B - Matrix of any dimension
Returns:
C Matrix of dimension A

innerProduct

public static double innerProduct(double[] a,
                                  double[] b)
calculates the inner product (dot product): c[i] = sum(a[i] * b[i] , i = 0..N-1)

Parameters:
a - Array
b - Array
Returns:
c Inner procuct

outerProduct

public static double[][] outerProduct(double[] a,
                                      double[] b)
calculates the outer product (Tensor product): M[i][j] = a[i] * b[j]

Parameters:
a - Array
b - Array
Returns:
M Outer product of a and b

multiply

public static double[] multiply(double s,
                                double[] u)
calculates S-multiplication : r = s * u;

Parameters:
s - Scalar
u - Vector
Returns:
r Vector r = s * u;

multiply

public static double[] multiply(double s,
                                int[] u)
calculates S-multiplication : r = s * u; The computations are done in double.

Parameters:
s - Scalar
u - Vector
Returns:
r Vector r = s * u;

multiply

public static double[][] multiply(double s,
                                  double[][] A)
calculates S-multiplication : B = s * A;

Parameters:
s - Scalar
A - Matrix
Returns:
B Matrix B = s * A;

multiply

public static double[] multiply(double[] u,
                                double[][] A)
multiplicates a vector with a matrix: r = u^T * A;
(Returns the product of the row vector x and the rectangular array A)

Parameters:
u - Vector
A - Matrix
Returns:
r vector r = u^T * A;

multiply

public static double[] multiply(double[][] A,
                                double[] u)
multiplicates a matrix with a vector: r = A * u;
(Returns the product of the row vector x and the rectangular array A)

Parameters:
A - matrix
u - vector
Returns:
r vector r = A * u;

multiply

public static double[][] multiply(double[][] A,
                                  double[][] B)
multiplicates a matrix with a matrix: C = A * B;

Parameters:
A - n x m matrix
B - m x n matrix
Returns:
C m x m matrix

gaussElimination

public static double[] gaussElimination(double[][] A,
                                        double[] f)
solves a vector x by a Gauss elimination of a NxN matrix with normalization and interchange of rows.
Method solves the equation A*x=f for x.

Parameters:
A - Matrix
f - Array with solution of A*x=f
Returns:
x Array x = A**(-1) * f

gaussEliminationTridiagonal

public static double[] gaussEliminationTridiagonal(double[][] A,
                                                   double[] f)
solves a vector x by a Gauss elimination of a tridiagonal NxN matrix with normalization and interchange of rows.
Method solves the equation A*x=f for x.
Note: A has to be a NxN tridiagonal matrix where the entries are zero expect on the main diagonal (i=j) and the diagonals just above and below. The gaussian elimination method solves the equation in O(N) operations.

Parameters:
A - Matrix
f - Array with solution of A*x=f
Returns:
x Array x = A**(-1) * f

print

public static void print(double[] state,
                         java.lang.String delimiter)
reports a scalar and a vector to the output stream, where the entries are separated by the given delimiter

Parameters:
state - a vector
delimiter - a delimiter

print

public static void print(double time,
                         double[] state,
                         java.lang.String delimiter)
reports a scalar and a vector to the output stream, where the entries are separated by the given delimiter

Parameters:
time - a scalar
state - a vector
delimiter - a delimiter

print

public static void print(double time,
                         double[] state)
reports a scalar and a vector to the output stream

Parameters:
time - A scalar
state - A vector