edu.umbc.cs.maple.utils
Class JamaUtils

java.lang.Object
  extended by edu.umbc.cs.maple.utils.JamaUtils

public class JamaUtils
extends java.lang.Object

Various utility functions for the Jama matrix toolkit.

Copyright (c) 2008 Eric Eaton

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Version:
0.1
Author:
Eric Eaton (EricEaton@umbc.edu)
University of Maryland Baltimore County

Nested Class Summary
static class JamaUtils.Function
          Specifies a simple mathematical function.
 
Constructor Summary
JamaUtils()
           
 
Method Summary
static Jama.Matrix colsum(Jama.Matrix m)
          Computes the sum of each column of a matrix.
static double colsum(Jama.Matrix m, int col)
          Gets the sum of the specified column of the matrix.
static Jama.Matrix columnAppend(Jama.Matrix m, Jama.Matrix n)
          Appends additional columns to the first matrix.
static Jama.Matrix deleteCol(Jama.Matrix m, int col)
          Deletes a column from a matrix.
static Jama.Matrix deleteRow(Jama.Matrix m, int row)
          Deletes a row from a matrix.
static double dotproduct(Jama.Matrix m1, Jama.Matrix m2)
          Computes the dot product of two vectors.
protected static double gcv(Jama.Matrix A, Jama.Matrix b)
          Selects the regularization parameter by generalized cross-validation.
static Jama.Matrix getcol(Jama.Matrix m, int col)
          Gets the specified column of a matrix.
static Jama.Matrix getcolumns(Jama.Matrix m, int[] columns)
          Gets the specified columns of a matrix.
static double getMax(Jama.Matrix m)
          Gets the maximum value in a matrix.
static double getMin(Jama.Matrix m)
          Gets the minimum value in a matrix.
static Jama.Matrix getrow(Jama.Matrix m, int row)
          Gets the specified row of a matrix.
static Jama.Matrix getrows(Jama.Matrix m, int[] rows)
          Gets the specified rows of a matrix.
static boolean isColumnVector(Jama.Matrix m)
          Determines if a given matrix is a column vector, that is, it has only one column.
static boolean isRowVector(Jama.Matrix m)
          Determines if a given matrix is a row vector, that is, it has only one row.
static boolean isSymmetric(Jama.Matrix m)
          Determines whether a matrix is symmetric.
static Jama.Matrix loadSparseMatrix(java.io.File file)
           
static void main(java.lang.String[] args)
          Test function for regularized least squares.
static Jama.Matrix makeColumnVector(Jama.Matrix m)
          Transforms the given matrix into a column vector, that is, a matrix with one column.
static void makeMatrixSymmetric(Jama.Matrix m, JamaUtils.Function f)
          Makes a matrix symmetric by applying a function to symmetric elements.
static Jama.Matrix makeRowVector(Jama.Matrix m)
          Transforms the given matrix into a row vector, that is, a matrix with one row.
static Jama.Matrix normalize(Jama.Matrix m)
          Normalizes a matrix to make the elements sum to 1.
static Jama.Matrix ones(int numRows, int numCols)
          Make a matrix of ones.
static Jama.Matrix regLeastSquares(Jama.Matrix A, Jama.Matrix b)
          Performs least squares regression using Tikhonov regularization.
static Jama.Matrix regLeastSquares(Jama.Matrix A, Jama.Matrix b, double lambda)
          Performs least squares regression using Tikhonov regularization.
static Jama.Matrix regLeastSquares(Jama.Matrix A, Jama.Matrix b, Jama.Matrix regop)
          Performs least squares regression using Tikhonov regularization.
static double rmse(Jama.Matrix a, Jama.Matrix b)
          Computes the root mean squared error of two matrices
static Jama.Matrix rowAppend(Jama.Matrix m, Jama.Matrix n)
          Appends additional rows to the first matrix.
static Jama.Matrix rowsum(Jama.Matrix m)
          Computes the sum of each row of a matrix.
static double rowsum(Jama.Matrix m, int row)
          Gets the sum of the specified row of the matrix.
static void setcol(Jama.Matrix m, int col, double[] values)
          Sets the specified column of a matrix.
static void setcol(Jama.Matrix m, int col, Jama.Matrix values)
          Sets the specified column of a matrix.
static void setrow(Jama.Matrix m, int row, Jama.Matrix values)
          Sets the specified row of a matrix.
static double sum(Jama.Matrix m)
          Computes the sum the elements of a matrix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JamaUtils

public JamaUtils()
Method Detail

getcol

public static Jama.Matrix getcol(Jama.Matrix m,
                                 int col)
Gets the specified column of a matrix.

Parameters:
m - the matrix.
col - the column to get.
Returns:
the specified column of m.

getcolumns

public static Jama.Matrix getcolumns(Jama.Matrix m,
                                     int[] columns)
Gets the specified columns of a matrix.

Parameters:
m - the matrix
columns - the columns to get
Returns:
the matrix of the specified columns of m.

getrow

public static Jama.Matrix getrow(Jama.Matrix m,
                                 int row)
Gets the specified row of a matrix.

Parameters:
m - the matrix.
row - the row to get.
Returns:
the specified row of m.

getrows

public static Jama.Matrix getrows(Jama.Matrix m,
                                  int[] rows)
Gets the specified rows of a matrix.

Parameters:
m - the matrix
rows - the rows to get
Returns:
the matrix of the specified rows of m.

setrow

public static void setrow(Jama.Matrix m,
                          int row,
                          Jama.Matrix values)
Sets the specified row of a matrix. Modifies the passed matrix.

Parameters:
m - the matrix.
row - the row to modify.
values - the new values of the row.

setcol

public static void setcol(Jama.Matrix m,
                          int col,
                          Jama.Matrix values)
Sets the specified column of a matrix. Modifies the passed matrix.

Parameters:
m - the matrix.
col - the column to modify.
values - the new values of the column.

setcol

public static void setcol(Jama.Matrix m,
                          int col,
                          double[] values)
Sets the specified column of a matrix. Modifies the passed matrix.

Parameters:
m - the matrix.
col - the column to modify.
values - the new values of the column.

rowAppend

public static Jama.Matrix rowAppend(Jama.Matrix m,
                                    Jama.Matrix n)
Appends additional rows to the first matrix.

Parameters:
m - the first matrix.
n - the matrix to append containing additional rows.
Returns:
a matrix with all the rows of m then all the rows of n.

columnAppend

public static Jama.Matrix columnAppend(Jama.Matrix m,
                                       Jama.Matrix n)
Appends additional columns to the first matrix.

Parameters:
m - the first matrix.
n - the matrix to append containing additional columns.
Returns:
a matrix with all the columns of m then all the columns of n.

deleteRow

public static Jama.Matrix deleteRow(Jama.Matrix m,
                                    int row)
Deletes a row from a matrix. Does not change the passed matrix.

Parameters:
m - the matrix.
row - the row to delete.
Returns:
m with the specified row deleted.

deleteCol

public static Jama.Matrix deleteCol(Jama.Matrix m,
                                    int col)
Deletes a column from a matrix. Does not change the passed matrix.

Parameters:
m - the matrix.
col - the column to delete.
Returns:
m with the specified column deleted.

rowsum

public static double rowsum(Jama.Matrix m,
                            int row)
Gets the sum of the specified row of the matrix.

Parameters:
m - the matrix.
row - the row.
Returns:
the sum of m[row,*]

colsum

public static double colsum(Jama.Matrix m,
                            int col)
Gets the sum of the specified column of the matrix.

Parameters:
m - the matrix.
col - the column.
Returns:
the sum of m[*,col]

rowsum

public static Jama.Matrix rowsum(Jama.Matrix m)
Computes the sum of each row of a matrix.

Parameters:
m - the matrix.
Returns:
a column vector of the sum of each row of m.

colsum

public static Jama.Matrix colsum(Jama.Matrix m)
Computes the sum of each column of a matrix.

Parameters:
m - the matrix.
Returns:
a row vector of the sum of each column of m.

sum

public static double sum(Jama.Matrix m)
Computes the sum the elements of a matrix.

Parameters:
m - the matrix.
Returns:
the sum of the elements of the matrix

isRowVector

public static boolean isRowVector(Jama.Matrix m)
Determines if a given matrix is a row vector, that is, it has only one row.

Parameters:
m - the matrix.
Returns:
whether the given matrix is a row vector (whether it has only one row).

isColumnVector

public static boolean isColumnVector(Jama.Matrix m)
Determines if a given matrix is a column vector, that is, it has only one column.

Parameters:
m - the matrix.
Returns:
whether the given matrix is a column vector (whether it has only one column).

makeColumnVector

public static Jama.Matrix makeColumnVector(Jama.Matrix m)
Transforms the given matrix into a column vector, that is, a matrix with one column. The matrix must be a vector (row or column) to begin with.

Parameters:
m -
Returns:
m.transpose() if m is a row vector, m if m is a column vector.
Throws:
java.lang.IllegalArgumentException - if m is not a row vector or a column vector.

makeRowVector

public static Jama.Matrix makeRowVector(Jama.Matrix m)
Transforms the given matrix into a row vector, that is, a matrix with one row. The matrix must be a vector (row or column) to begin with.

Parameters:
m -
Returns:
m.transpose() if m is a column vector, m if m is a row vector.
Throws:
java.lang.IllegalArgumentException - if m is not a row vector or a column vector.

dotproduct

public static double dotproduct(Jama.Matrix m1,
                                Jama.Matrix m2)
Computes the dot product of two vectors. Both must be either row or column vectors.

Parameters:
m1 -
m2 -
Returns:
the dot product of the two vectors.

isSymmetric

public static boolean isSymmetric(Jama.Matrix m)
Determines whether a matrix is symmetric.

Parameters:
m - the matrix.
Returns:
true if a is symmetric, false otherwise

makeMatrixSymmetric

public static void makeMatrixSymmetric(Jama.Matrix m,
                                       JamaUtils.Function f)
Makes a matrix symmetric by applying a function to symmetric elements.

Parameters:
m - the matrix to make symmetric
f - the function to apply

normalize

public static Jama.Matrix normalize(Jama.Matrix m)
Normalizes a matrix to make the elements sum to 1.

Parameters:
m - the matrix
Returns:
the normalized form of m with all elements summing to 1.

getMax

public static double getMax(Jama.Matrix m)
Gets the maximum value in a matrix.

Parameters:
m - the matrix
Returns:
the maximum value in m.

getMin

public static double getMin(Jama.Matrix m)
Gets the minimum value in a matrix.

Parameters:
m - the matrix
Returns:
the minimum value in m.

ones

public static Jama.Matrix ones(int numRows,
                               int numCols)
Make a matrix of ones.

Parameters:
numRows - the number of rows.
numCols - the number of columns.
Returns:
the numRows x numCols matrix of ones.

regLeastSquares

public static Jama.Matrix regLeastSquares(Jama.Matrix A,
                                          Jama.Matrix b)
Performs least squares regression using Tikhonov regularization. Solves the problem Ax = b for x using regularization: min || Ax - b ||^2 - lambda^2 || x ||^2 , which can be solved by x = inv(A' * A + lambda^2 * I) * A' * b; Uses the identity matrix as the regop, and estimates lambda using generalized cross-validation.

Parameters:
A - the data matrix (n x m).
b - the target function values (n x 1).

regLeastSquares

public static Jama.Matrix regLeastSquares(Jama.Matrix A,
                                          Jama.Matrix b,
                                          double lambda)
Performs least squares regression using Tikhonov regularization. Solves the problem Ax = b for x using regularization: min || Ax - b ||^2 - lambda^2 || x ||^2 , which can be solved by x = inv(A' * A + lambda^2 * I) * A' * b; Uses the identity matrix as the regularization operator.

Parameters:
A - the data matrix (n x m).
b - the target function values (n x 1).
lambda - the lambda values. If less than zero, it is estimated using generalized cross-validation.

regLeastSquares

public static Jama.Matrix regLeastSquares(Jama.Matrix A,
                                          Jama.Matrix b,
                                          Jama.Matrix regop)
Performs least squares regression using Tikhonov regularization. Solves the problem Ax = b for x using regularization: min || Ax - b ||^2 - || \sqrt(regop) x ||^2 , which can be solved by x = inv(A' * A + regop) * A' * b;

Parameters:
A - the data matrix (n x m).
b - the target function values (n x 1).
regop - the regularization operator (m x m). The default is to use the identity matrix as the regularization operator, so you probably don't want to use this verion of regLeastSquares() without a really good reason. Use regLeastSquares(Matrix A, Matrix b, double lambda) instead.

gcv

protected static double gcv(Jama.Matrix A,
                            Jama.Matrix b)
Selects the regularization parameter by generalized cross-validation. Given a matrix A and a data vector b, it returns a regularization parameter rpar chosen by generalized cross-validation using ridge regression. RSS G = --- T^2 where T = n - sum(f_i) is an effective number of degrees of freedom and f_i are the filter factors of the regularization method. The returned regularization parameter rpar is the ridge parameter of ridge regression. References: Hansen, P. C., 1998: Rank-Deficient and Discrete Ill-Posed Problems. SIAM Monogr. on Mathematical Modeling and Computation, SIAM. Wahba, G., 1990: Spline Models for Observational Data. CBMS-NSF Regional Conference Series in Applied Mathematics, Vol. 59, SIAM. Adapted from various routines in Per-Christian Hansen's regularization toolbox for matlab, then converted to java.


rmse

public static double rmse(Jama.Matrix a,
                          Jama.Matrix b)
Computes the root mean squared error of two matrices

Parameters:
a -
b -
Returns:
the RMSE of a and b

loadSparseMatrix

public static Jama.Matrix loadSparseMatrix(java.io.File file)

main

public static void main(java.lang.String[] args)
Test function for regularized least squares.