STBB
Class MAT

java.lang.Object
  extended bySTBB.MAT

public class MAT
extends java.lang.Object

This class provides many static methods for vector and matrix manipulation.

Version:
1.00
Author:
Cheng Guan Koay

Method Summary
static double[] abs(double[] v)
          returns absolute values componentwise.
static double[][] abs(double[][] v)
          Absolute value operation componentwise.
static double[][] add(double[][] m1, double m2)
          add a constant to a matrix
static double[][] add(double[][] m1, double[][] m2)
          Matrix addition
static double[] add(double[] m1, double m2)
          add a constant to a vector
static double[] add(double[] m1, double[] m2)
          vector addition
static double[][] addDiagonals(double[][] m1, double[] m2)
          return a new matrix of m1 with m2 added onto its diagonals
static double[] append(double[] a, double b)
          example: append({1,2,3},5)={1,2,3,5}
static double[] copy(double[] v)
          copy returns an exact replica of v.
static double[][] copy(double[][] m)
          copy returns an exact replica of m.
static double correlation(double[] v1, double[] v2)
          compute correlation between v1 and v2.
static double[][] diagonalMatrix(double[] v)
          get a diagonal matrix with v[] as the diagonal elements
static double[] divide(double[] m1, double[] m2)
          return m1/m2 componentwise
static double[] dot(double[][] m1, double[] v2)
          Matrix-Vector multiplication m1.v2
static double[][] dot(double[][] m1, double[][] m2)
          Matrix multiplication m1.m2
static double dot(double[] v1, double[] v2)
          Vector dot product v1.v2
static double[] drop(double[] components, int i)
          Drop the first i number of components i cannot be equal to the length of the vector.
static double[] flatten(double[][] a)
          example: flatten({{1,2,3},{4,5}}) = {1,2,3,4,5}
static double generalDotProduct(double[] v1, double[][] metric)
          v1^T.metric.v1 assuming metric is a square matrix
static double generalDotProduct(double[] v1, double[][] metric, double[] v2)
          v1^T.metric.v2
static double[] getDiagonals(double[][] m)
          get the diagonal elements from m
static double[][] identityMatrix(int dim)
          get an dim x dim identity matrix
static double[] join(double[] a, double[] b)
           join two arrays into one.
static double max(double[] data)
          find the maximum value in an array.
static double max(double[][] data)
          find the maximum value in a double array
static double max(double[][][] data)
          find the maximum value in data.
static double maxAbs(double[] v)
          find the maximum value of the absolute values of an array.
static double maxAbs(double[][] v)
          find the max value of the absolute values of a double array.
static double mean(double[] v)
          get the mean value of vector v.
static double min(double[] data)
          find the minimum value is data.
static double min(double[][] data)
          find the minimum value in a matrix.
static double min(double[][][] data)
          find the minimum value in data.
static double minAbs(double[] v)
          find the minimum value of the absolute values of an array v.
static double minAbs(double[][] v)
          find the minimum value of the absolute values of a double array.
static double[][] multiply(double[][] m1, double m2)
          Component-wise multiplication between a matrix and a number.
static double[][] multiply(double[][] m1, double[][] m2)
          Component-wise multiplication between two matrices.
static double[] multiply(double[] m1, double m2)
          Component-wise multiplication between a vector and a number.
static double[] multiply(double[] m1, double[] m2)
          Component-wise multiplication between two vectors.
static double norm(double[] v)
          vector norm
static double norm(double[][] m)
          Frobenius norm
static double[] normalize(double[] v)
          Obtain a normalized vector from v
static double[] ones(int num)
          get an array of one's with length num
static double[][] ones(int ro, int co)
          get a matrix of one's with dimensions ro x co
static double[][] outerProduct(double[] v1, double[] v2)
          outer product of two vectors.
static double[] prepend(double[] a, double b)
          example: prepend({1,2,3},5)={5,1,2,3}
static double[] random(int N)
          an array of N uniform random values ranging from -1.0 to 1.0);
static double[] random(int N, double a, double b)
          an array of N uniform random numbers ranging from a to b (a < b);
static double[][] removeDiagonals(double[][] mat)
          set the diagonal elements of mat to zero.
static double[] removeMean(double[] v)
          Obtain a vector with zero mean
static double[][] setDiagonals(double[][] m1, double[] m2)
          return a new matrix of m1 where the diagonal elements of m1 have been replaced by m2.
static void setValue(double[][] m, double v1)
          set m[i][j] = v1 for all i,j.
static void setValue(double[] v, double v1)
          set v[i] = v1 for all i.
static double[][] sortColumns(double[][] mat, int[] indexArray)
          return a matrix where the columns of the matrix is sorted according to the index array.
static double[][] sortRows(double[][] mat, int[] indexArray)
          sort rows
static double[] square(double[] v)
          square the components
static double standardDeviation(double[] v)
          sample standard deviation returns 0 if v.length==1.
static double[][] subtract(double[][] m1, double m2)
          return m1 - m2
static double[][] subtract(double[][] m1, double[][] m2)
          return m1 - m2
static double[] subtract(double[] m1, double m2)
          return m1 - m2
static double[] subtract(double[] m1, double[] m2)
          return m1 - m2
static double sum(double[] v)
          get the sum of all the elements of v using Kahan summation.
static double sumOfSquares(double[] v)
          return v[0]*v[0]+...+v[n-1][n-1] where n is the length of v
static double[] take(double[] components, int i)
          Take the first i number of components i can be equal to the length of the vector.
static java.lang.String toString(double[] a)
          This method is for printing out elements of a on the screen.
static java.lang.String toString(double[][] a)
          This method is for printing out elements of a on the screen.
static java.lang.String toString(int[] a)
          This method is for printing out elements of a on the screen.
static java.lang.String toString(int[][] a)
          This method is for printing out elements of a on the screen.
static double trace(double[][] m)
          get the sum of the diagonal elements of m
static double[][] transpose(double[][] m)
          matrix transposition
static double variance(double[] v)
          compute sample variance returns 0 if v.length==1.
static double[] zeroes(int num)
          get an array of zero's with length num
static double[][] zeroes(int ro, int co)
          get a matrix of zero's with dimensions ro x co
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

abs

public static double[] abs(double[] v)
returns absolute values componentwise.


random

public static double[] random(int N,
                              double a,
                              double b)
an array of N uniform random numbers ranging from a to b (a < b);

Assumption: N>0 and (a


random

public static double[] random(int N)
an array of N uniform random values ranging from -1.0 to 1.0);


abs

public static double[][] abs(double[][] v)
Absolute value operation componentwise.


removeDiagonals

public static double[][] removeDiagonals(double[][] mat)
set the diagonal elements of mat to zero.


setValue

public static void setValue(double[] v,
                            double v1)
set v[i] = v1 for all i.


setValue

public static void setValue(double[][] m,
                            double v1)
set m[i][j] = v1 for all i,j.


addDiagonals

public static double[][] addDiagonals(double[][] m1,
                                      double[] m2)
return a new matrix of m1 with m2 added onto its diagonals


setDiagonals

public static double[][] setDiagonals(double[][] m1,
                                      double[] m2)
return a new matrix of m1 where the diagonal elements of m1 have been replaced by m2.


add

public static double[] add(double[] m1,
                           double m2)
add a constant to a vector


add

public static double[] add(double[] m1,
                           double[] m2)
vector addition


add

public static double[][] add(double[][] m1,
                             double m2)
add a constant to a matrix


add

public static double[][] add(double[][] m1,
                             double[][] m2)
Matrix addition


divide

public static double[] divide(double[] m1,
                              double[] m2)
return m1/m2 componentwise

Parameters:
m1 - type double[]
m2 - type double[]

subtract

public static double[] subtract(double[] m1,
                                double m2)
return m1 - m2

Parameters:
m1 - type double[]
m2 - type double

subtract

public static double[] subtract(double[] m1,
                                double[] m2)
return m1 - m2

Parameters:
m1 - type double[]
m2 - type double[]

subtract

public static double[][] subtract(double[][] m1,
                                  double m2)
return m1 - m2

Parameters:
m1 - type double[][]
m2 - type double

subtract

public static double[][] subtract(double[][] m1,
                                  double[][] m2)
return m1 - m2

Parameters:
m1 - type double[][]
m2 - type double[][]

multiply

public static double[] multiply(double[] m1,
                                double m2)
Component-wise multiplication between a vector and a number.


multiply

public static double[] multiply(double[] m1,
                                double[] m2)
Component-wise multiplication between two vectors. m1 * m2


multiply

public static double[][] multiply(double[][] m1,
                                  double m2)
Component-wise multiplication between a matrix and a number.


multiply

public static double[][] multiply(double[][] m1,
                                  double[][] m2)
Component-wise multiplication between two matrices. m1 * m2


dot

public static double[][] dot(double[][] m1,
                             double[][] m2)
Matrix multiplication m1.m2


dot

public static double[] dot(double[][] m1,
                           double[] v2)
Matrix-Vector multiplication m1.v2


dot

public static double dot(double[] v1,
                         double[] v2)
Vector dot product v1.v2


ones

public static double[] ones(int num)
get an array of one's with length num


zeroes

public static double[] zeroes(int num)
get an array of zero's with length num


ones

public static double[][] ones(int ro,
                              int co)
get a matrix of one's with dimensions ro x co


zeroes

public static double[][] zeroes(int ro,
                                int co)
get a matrix of zero's with dimensions ro x co


identityMatrix

public static double[][] identityMatrix(int dim)
get an dim x dim identity matrix


diagonalMatrix

public static double[][] diagonalMatrix(double[] v)
get a diagonal matrix with v[] as the diagonal elements


getDiagonals

public static double[] getDiagonals(double[][] m)
get the diagonal elements from m


trace

public static double trace(double[][] m)
get the sum of the diagonal elements of m


transpose

public static double[][] transpose(double[][] m)
matrix transposition


generalDotProduct

public static double generalDotProduct(double[] v1,
                                       double[][] metric,
                                       double[] v2)
v1^T.metric.v2


generalDotProduct

public static double generalDotProduct(double[] v1,
                                       double[][] metric)
v1^T.metric.v1 assuming metric is a square matrix


max

public static double max(double[] data)
find the maximum value in an array.


max

public static double max(double[][] data)
find the maximum value in a double array


max

public static double max(double[][][] data)
find the maximum value in data.


min

public static double min(double[] data)
find the minimum value is data.


min

public static double min(double[][] data)
find the minimum value in a matrix.


min

public static double min(double[][][] data)
find the minimum value in data.


sum

public static double sum(double[] v)
get the sum of all the elements of v using Kahan summation.


join

public static double[] join(double[] a,
                            double[] b)

join two arrays into one.

Example: a={1,2,3}; b={4,5};

join(a,b) returns {1,2,3,4,5};


prepend

public static double[] prepend(double[] a,
                               double b)
example: prepend({1,2,3},5)={5,1,2,3}


append

public static double[] append(double[] a,
                              double b)
example: append({1,2,3},5)={1,2,3,5}


outerProduct

public static double[][] outerProduct(double[] v1,
                                      double[] v2)
outer product of two vectors.

returns v1.v2^T


flatten

public static double[] flatten(double[][] a)
example: flatten({{1,2,3},{4,5}}) = {1,2,3,4,5}


norm

public static double norm(double[][] m)
Frobenius norm


norm

public static double norm(double[] v)
vector norm


sumOfSquares

public static double sumOfSquares(double[] v)
return v[0]*v[0]+...+v[n-1][n-1] where n is the length of v


mean

public static double mean(double[] v)
get the mean value of vector v.


normalize

public static double[] normalize(double[] v)
Obtain a normalized vector from v

Returns:
double[]

removeMean

public static double[] removeMean(double[] v)
Obtain a vector with zero mean


correlation

public static double correlation(double[] v1,
                                 double[] v2)
compute correlation between v1 and v2.


take

public static double[] take(double[] components,
                            int i)
Take the first i number of components i can be equal to the length of the vector. If i is negative then this method gives the last i elements of vector

Returns:
double[].

drop

public static double[] drop(double[] components,
                            int i)
Drop the first i number of components i cannot be equal to the length of the vector. If i is negative then this method drop the last i elements of vector

Returns:
double[].

square

public static double[] square(double[] v)
square the components

returns {v[0]*v[0], v[1]*v[1], ... }


variance

public static double variance(double[] v)
compute sample variance returns 0 if v.length==1.


standardDeviation

public static double standardDeviation(double[] v)
sample standard deviation returns 0 if v.length==1.


copy

public static double[] copy(double[] v)
copy returns an exact replica of v.


copy

public static double[][] copy(double[][] m)
copy returns an exact replica of m.


toString

public static java.lang.String toString(double[][] a)
This method is for printing out elements of a on the screen.


toString

public static java.lang.String toString(int[][] a)
This method is for printing out elements of a on the screen.


toString

public static java.lang.String toString(double[] a)
This method is for printing out elements of a on the screen.


toString

public static java.lang.String toString(int[] a)
This method is for printing out elements of a on the screen.


sortColumns

public static double[][] sortColumns(double[][] mat,
                                     int[] indexArray)
return a matrix where the columns of the matrix is sorted according to the index array.

Notice that the return type is double[][].


sortRows

public static double[][] sortRows(double[][] mat,
                                  int[] indexArray)
sort rows


minAbs

public static double minAbs(double[] v)
find the minimum value of the absolute values of an array v.


maxAbs

public static double maxAbs(double[] v)
find the maximum value of the absolute values of an array.


minAbs

public static double minAbs(double[][] v)
find the minimum value of the absolute values of a double array.


maxAbs

public static double maxAbs(double[][] v)
find the max value of the absolute values of a double array.