ca.nengo.util
Class MU

java.lang.Object
  extended by ca.nengo.util.MU

public class MU
extends java.lang.Object

"Matrix Utilities". Utility methods related to matrices and vectors of floats. TODO: test

Author:
Bryan Tripp

Nested Class Summary
static class MU.MatrixExpander
          A tool for growing matrices (similar to java.util.List).
static class MU.VectorExpander
          A tool for growing vectors (similar to java.util.List).
 
Constructor Summary
MU()
           
 
Method Summary
static double[][] clone(double[][] matrix)
           
static float[][] clone(float[][] matrix)
           
static float[] convert(double[] vector)
           
static float[][] convert(double[][] matrix)
           
static double[] convert(float[] vector)
           
static double[][] convert(float[][] matrix)
           
static float[] copy(float[] vector, int start, int interval, int end)
           
static void copyInto(float[][] src, float[][] dest, int destRowPos, int destColPos, int length)
          Unlike System.arraycopy, this function copies the source matrix into the destination while preserving the original row length.
static float[][] diag(float[] entries)
           
static float[] diag(float[][] matrix)
           
static float[] difference(float[] X)
           
static float[][] difference(float[][] A, float[][] B)
           
static float[] difference(float[] X, float[] Y)
           
static float[][] I(int dimension)
           
static boolean isMatrix(float[][] matrix)
           
static float[] makeVector(float start, float increment, float end)
           
static float max(float[] vector)
           
static float max(float[][] matrix)
           
static float mean(float[] vector)
           
static float mean(float[][] matrix)
           
static float min(float[] vector)
           
static float min(float[][] matrix)
           
static float[] normalize(float[] vector)
           
static float[][] outerprod(float[] A, float[] B)
           
static float pnorm(float[] vector, int p)
           
static float[][] prod(float[][] A, float a)
           
static float[] prod(float[][] A, float[] X)
           
static float[][] prod(float[][] A, float[][] B)
           
static float[] prod(float[] X, float a)
           
static float prod(float[] X, float[] Y)
           
static float[][] prodElementwise(float[][] A, float[][] B)
           
static float[] prodElementwise(float[] A, float[] B)
           
static float[][] random(int rows, int cols, PDF pdf)
           
static int[] round(float[] vector)
           
static float[][] shape(float[][] matrix, int rows, int cols)
           
static float sum(float[] vector)
           
static float[][] sum(float[][] A, float[][] B)
           
static float[] sum(float[] X, float[] Y)
           
static float sumToIndex(float[] vector, int index)
           
static java.lang.String toString(float[][] matrix, int decimalPlaces)
          TODO: handle exponential notation
static float[][] transpose(float[][] matrix)
           
static float[][] uniform(int rows, int cols, float value)
           
static float variance(float[] vector, float mean)
           
static float[][] zero(int rows, int cols)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MU

public MU()
Method Detail

isMatrix

public static boolean isMatrix(float[][] matrix)
Parameters:
matrix - An array of arrays that is expected to be in matrix form
Returns:
True if all "rows" (ie array elements) have the same length

clone

public static float[][] clone(float[][] matrix)
Parameters:
matrix - Any matrix
Returns:
An identical but independent copy of the given matrix

clone

public static double[][] clone(double[][] matrix)
Parameters:
matrix - Any matrix
Returns:
An identical but independent copy of the given matrix

copyInto

public static void copyInto(float[][] src,
                            float[][] dest,
                            int destRowPos,
                            int destColPos,
                            int length)
Unlike System.arraycopy, this function copies the source matrix into the destination while preserving the original row length. It copies the full source.

Parameters:
src - - source matrix
dest - - destination matrix
destRowPos - - starting target row
destColPos - - starting target column position
length - - number of rows to copy

copy

public static float[] copy(float[] vector,
                           int start,
                           int interval,
                           int end)
Parameters:
vector - Vector to copy from
start - Index in vector from which to start copying
interval - Interval separating copied entries in source vector (ie skip over interval-1 entries)
end - Index in vector at which copying ends
Returns:
Values copied from source vector

prod

public static float[] prod(float[] X,
                           float a)
Parameters:
X - Any vector
a - Any scalar
Returns:
aX (each element of the vector multiplied by the scalar)

prod

public static float prod(float[] X,
                         float[] Y)
Parameters:
X - Any vector
Y - Any vector of the same length as X
Returns:
X'Y

prod

public static float[] prod(float[][] A,
                           float[] X)
Parameters:
A - Any matrix
X - Any vector with the same number of elements as there are columns in A
Returns:
AX

prod

public static float[][] prod(float[][] A,
                             float[][] B)
Parameters:
A - Any m x n matrix
B - Any n x p matrix
Returns:
Product of matrices

prodElementwise

public static float[] prodElementwise(float[] A,
                                      float[] B)
Parameters:
A - Any vector
B - Any vector the same length as A
Returns:
A(start:end) The identified subvector from A

prodElementwise

public static float[][] prodElementwise(float[][] A,
                                        float[][] B)
Parameters:
A - Any matrix
B - Any matrix the same dimensions as A
Returns:
A .* B

prod

public static float[][] prod(float[][] A,
                             float a)
Parameters:
A - Any matrix
a - Any scalar
Returns:
aA (each element of matrix multiplied by scalar)

outerprod

public static float[][] outerprod(float[] A,
                                  float[] B)
Parameters:
A - Any vector
B - Any vector
Returns:
A*B (matrix with the outer product of A and B)

sum

public static float[][] sum(float[][] A,
                            float[][] B)
Parameters:
A - Any m x n matrix
B - Any m x n matrix
Returns:
The element-wise sum of the given matrices

difference

public static float[][] difference(float[][] A,
                                   float[][] B)
Parameters:
A - Any m x n matrix
B - Any m x n matrix
Returns:
The element-wise difference of the given matrices (A-B)

sum

public static float[] sum(float[] X,
                          float[] Y)
Parameters:
X - Any vector
Y - Any vector same length as vector X
Returns:
X+Y (element-wise sum)

difference

public static float[] difference(float[] X,
                                 float[] Y)
Parameters:
X - Any vector
Y - Any vector same length as vector X
Returns:
X-Y (element-wise difference)

difference

public static float[] difference(float[] X)
Parameters:
X - Any vector
Returns:
X(2:end) - X(1:end-1)

shape

public static float[][] shape(float[][] matrix,
                              int rows,
                              int cols)
Parameters:
matrix - An array of float arrays (normally a matrix but can have rows of different length)
rows - Desired number of rows
cols - Desired number of columns
Returns:
Matrix with requested numbers of rows and columns drawn from the given matrix, and padded with zeros if there are not enough values in the original matrix

transpose

public static float[][] transpose(float[][] matrix)
Parameters:
matrix - Any matrix
Returns:
The transpose of the matrix

diag

public static float[][] diag(float[] entries)
Parameters:
entries - A list of diagonal entries
Returns:
A square diagonal matrix with given entries on the diagonal

diag

public static float[] diag(float[][] matrix)
Parameters:
matrix - Any matrix
Returns:
Diagonal entries

I

public static float[][] I(int dimension)
Parameters:
dimension - # of rows/columns
Returns:
Identity matrix of specified dimension

zero

public static float[][] zero(int rows,
                             int cols)
Parameters:
rows - Number of rows in the requested matrix
cols - Number of columns in the requested matrix
Returns:
Matrix of zeroes with the given dimensions

uniform

public static float[][] uniform(int rows,
                                int cols,
                                float value)
Parameters:
rows - Number of rows in the requested matrix
cols - Number of columns in the requested matrix
value - Value of each element
Returns:
Matrix with the given dimensions where each entry is the given value

random

public static float[][] random(int rows,
                               int cols,
                               PDF pdf)
Parameters:
rows - Number of rows in the requested matrix
cols - Number of columns in the requested matrix
pdf - One-dimensional PDF from which each element is drawn
Returns:
Matrix with the given dimensions where each entry is randomly drawn from the given PDF

convert

public static double[][] convert(float[][] matrix)
Parameters:
matrix - Any float matrix
Returns:
Duplicate with each element cast to double

convert

public static double[] convert(float[] vector)
Parameters:
vector - Any float vector
Returns:
Duplicate with each element cast to double

convert

public static float[][] convert(double[][] matrix)
Parameters:
matrix - Any double matrix
Returns:
Duplicate with each element cast to float

convert

public static float[] convert(double[] vector)
Parameters:
vector - Any double vector
Returns:
Duplicate with each element cast to float

min

public static float min(float[] vector)
Parameters:
vector - Any vector
Returns:
Minimum of elements

max

public static float max(float[] vector)
Parameters:
vector - Any vector
Returns:
Minimum of elements

min

public static float min(float[][] matrix)
Parameters:
matrix - Any matrix
Returns:
Minimum of elements

max

public static float max(float[][] matrix)
Parameters:
matrix - Any matrix
Returns:
Minimum of elements

sum

public static float sum(float[] vector)
Parameters:
vector - Any vector
Returns:
Sum of elements

sumToIndex

public static float sumToIndex(float[] vector,
                               int index)
Parameters:
vector - Any vector
index - Index of last element to include in sum
Returns:
Sum of elements

mean

public static float mean(float[] vector)
Parameters:
vector - Any vector
Returns:
Mean of vector elements

mean

public static float mean(float[][] matrix)
Parameters:
matrix - Any matrix
Returns:
Mean of matrix elements

variance

public static float variance(float[] vector,
                             float mean)
Parameters:
vector - Any vector
mean - Value around which to take variance, eg MU.mean(vector) or some pre-defined value
Returns:
Bias-corrected variance of vector elements around the given values

normalize

public static float[] normalize(float[] vector)
Parameters:
vector - Any vector
Returns:
The vector normalized to 2-norm of 1

pnorm

public static float pnorm(float[] vector,
                          int p)
Parameters:
vector - Any vector
p - Degree of p-norm (use -1 for infinity)
Returns:
The p-norm of the vector

toString

public static java.lang.String toString(float[][] matrix,
                                        int decimalPlaces)
TODO: handle exponential notation

Parameters:
matrix - Any matrix
decimalPlaces - number of decimal places to display for float values
Returns:
String representation of matrix with one row per line

makeVector

public static float[] makeVector(float start,
                                 float increment,
                                 float end)
Parameters:
start - Value of first element in vector
increment - Increment between adjacent elements
end - Value of last element in vector
Returns:
A vector with elements evenly incremented from start to end

round

public static int[] round(float[] vector)
Parameters:
vector - A vector
Returns:
Elements rounded to nearest integer