package jsat.linear;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import jsat.utils.ModifiableCountDownLatch;
import static jsat.utils.SystemInfo.*;
/**
* Generic class with some pre-implemented methods for a Matrix object.
* Throughout the documentation, the object that has its method called on will
* be denoted as A. So if you have code that looks like
*
* {@code Matrix gramHat = gram.subtract(Matrix.eye(gram.rows()));}
*
* Then {@code gram} would be the matrix A in the documentation.
*
* Matrices will use a capital letter, vectors a bold lower case letter,
* and scalars a normal lower case letter.
*
*
* @author Edward Rafff
*/
public abstract class Matrix implements Cloneable, Serializable
{
private static final long serialVersionUID = 6888360415978051714L;
/**
* Creates a new Matrix that stores the result of {@code A+B}
* @param B the matrix to add this this
* @return {@code A+B}
*/
public Matrix add(Matrix B)
{
Matrix toReturn = getThisSideMatrix(B);
toReturn.mutableAdd(1.0, B);
return toReturn;
}
/**
* Creates a new Matrix that stores the result of {@code A+B}
* @param B the matrix to add this this
* @param threadPool the source of threads to do computation in parallel
* @return {@code A+B}
*/
public Matrix add(Matrix B, ExecutorService threadPool)
{
Matrix toReturn = getThisSideMatrix(B);
toReturn.mutableAdd(1.0, B, threadPool);
return toReturn;
}
/**
* Creates a new Matrix that stores the result of {@code A+c}
* @param c the scalar to add to each value in this
* @return {@code A+c}
*/
public Matrix add(double c)
{
Matrix toReturn = getThisSideMatrix(null);
toReturn.mutableAdd(c);
return toReturn;
}
/**
* Creates a new Matrix that stores the result of {@code A+c}
* @param c the scalar to add to each value in this
* @param threadPool the source of threads to do computation in parallel
* @return {@code A+B}
*/
public Matrix add(double c, ExecutorService threadPool)
{
Matrix toReturn = getThisSideMatrix(null);
toReturn.mutableAdd(c, threadPool);
return toReturn;
}
/**
* Alters the current matrix to store the value A+B
* @param B the matrix to add this this
*/
public void mutableAdd(Matrix B)
{
this.mutableAdd(1.0, B);
}
/**
* Alters the current matrix to store the value A+c*B
* @param c the scalar constant to multiple B by
* @param B the matrix to add to this
*/
abstract public void mutableAdd(double c, Matrix B);
/**
* Alters the current matrix to store the value A+B
* @param B the matrix to add to this
* @param threadpool the source of threads to do computation in parallel
*/
public void mutableAdd(Matrix B, ExecutorService threadpool)
{
this.mutableAdd(1.0, B, threadpool);
}
/**
* Alters the current matrix to store the value A+c*B
* @param c the scalar constant to multiple B by
* @param B the matrix to add to this
* @param threadPool the source of threads to do computation in parallel
*/
abstract public void mutableAdd(double c, Matrix B, ExecutorService threadPool);
/**
* Alters the current matrix to store the value A+c
* @param c the scalar constant to add to this
*/
abstract public void mutableAdd(double c);
/**
* Alters the current matrix to store the value A+c
* @param c the scalar constant to add to this
* @param threadPool the source of threads to do computation in parallel
*/
abstract public void mutableAdd(double c, ExecutorService threadPool);
/**
* Indicates whether or not this matrix can be mutated. If
* {@code false}, any method that contains "mutate" will not work.
*
* By default, this returns {@code true}
*
* @return {@code true} if the matrix supports being altered, {@code false}
* other wise.
*/
public boolean canBeMutated()
{
return true;
}
/**
* Returns an appropriate matrix to use for some operation A op B,
* where {@code A = this }
* @param B the other matrix, may be null
* @return a matrix that can be mutated to take the place of A
*/
private Matrix getThisSideMatrix(Matrix B)
{
if(this.canBeMutated())
return this.clone();
else//so far, only other option in JSAT is a dense matrix
{
DenseMatrix dm = new DenseMatrix(rows(), cols());
dm.mutableAdd(this);
return dm;
}
}
/**
* Creates a new Matrix that stores the result of A-B
* @param B the matrix to subtract from this.
* @return a new matrix equal to A-B
*/
public Matrix subtract(Matrix B)
{
Matrix toReturn = getThisSideMatrix(B);
toReturn.mutableSubtract(1.0, B);
return toReturn;
}
/**
* Creates a new Matrix that stores the result of A-B
* @param B the matrix to subtract from this.
* @param threadPool the source of threads to do computation in parallel
* @return a new matrix equal to A-B
*/
public Matrix subtract(Matrix B, ExecutorService threadPool)
{
Matrix toReturn = getThisSideMatrix(B);
toReturn.mutableSubtract(1.0, B, threadPool);
return toReturn;
}
/**
* Creates a new Matrix that stores the result of A-c
* @param c the scalar constant to subtract from this
* @return a new matrix equal to A-B
*/
public Matrix subtract(double c)
{
Matrix toReturn = getThisSideMatrix(null);
toReturn.mutableSubtract(c);
return toReturn;
}
/**
* Creates a new Matrix that stores the result of A-c
* @param c the scalar constant to subtract from this
* @param threadPool the source of threads to do computation in parallel
* @return a new matrix equal to A-B
*/
public Matrix subtract(double c, ExecutorService threadPool)
{
Matrix toReturn = getThisSideMatrix(null);
toReturn.mutableSubtract(c, threadPool);
return toReturn;
}
/**
* Alters the current matrix to store A-B
* @param B the matrix to subtract from this.
*/
public void mutableSubtract(Matrix B)
{
this.mutableSubtract(1.0, B);
}
/**
* Alters the current matrix to store A-c*B
* @param c the scalar constant to multiply B by
* @param B the matrix to subtract from this.
*/
public void mutableSubtract(double c, Matrix B)
{
mutableAdd(-c, B);
}
/**
* Alters the current matrix to store A-B
* @param B the matrix to subtract from this.
* @param threadpool the source of threads to do computation in parallel
*/
public void mutableSubtract(Matrix B, ExecutorService threadpool)
{
this.mutableSubtract(1.0, B, threadpool);
}
/**
* Alters the current matrix to store A-c*B
* @param c the scalar constant to multiply B by
* @param B the matrix to subtract from this.
* @param threadPool the source of threads to do computation in parallel
*/
public void mutableSubtract(double c, Matrix B, ExecutorService threadPool)
{
mutableAdd(-c, B, threadPool);
}
/**
* Alters the current matrix to store A-c
* @param c the scalar constant to subtract from this
*/
public void mutableSubtract(double c)
{
mutableAdd(-c);
}
/**
* Alters the current matrix to store A-c
* @param c the scalar constant to subtract from this
* @param threadPool the source of threads to do computation in parallel
*/
public void mutableSubtract(double c, ExecutorService threadPool)
{
mutableAdd(-c, threadPool);
}
/**
* If this matrix is Am x n, and b has a length of n, and c has a length of m,
* then this will mutate c to store c = c + A*b*z
* @param b the vector to be treated as a colum vector
* @param z the constant to multiply the A*b value by.
* @param c where to place the result by addition
* @throws ArithmeticException if the dimensions of A, b, or c do not all agree
*/
abstract public void multiply(Vec b, double z, Vec c);
/**
* Creates a new vector that is equal to A*b
* @param b the vector to multiply by
* @return a new vector A*b
*/
public Vec multiply(Vec b)
{
DenseVector result = new DenseVector(rows());
multiply(b, 1.0, result);
return result;
}
/**
* Creates a new matrix that stores A*B
* @param B the matrix to multiply by
* @return a new matrix A*B
*/
public Matrix multiply(Matrix B)
{
Matrix C = new DenseMatrix(this.rows(), B.cols());
multiply(B, C);
return C;
}
/**
* Creates a new matrix that stores A*B
* @param B the matrix to multiply by
* @param threadPool the source of threads to do computation in parallel
* @return a new matrix A*B
*/
public Matrix multiply(Matrix B, ExecutorService threadPool)
{
Matrix C = new DenseMatrix(this.rows(), B.cols());
multiply(B, C, threadPool);
return C;
}
/**
* Alters the matrix C to be equal to C = C+A*B
* @param B the matrix to multiply this with
* @param C the matrix to add the result to
*/
abstract public void multiply(Matrix B, Matrix C);
/**
* Alters the matrix C to be equal to C = C+A*B
* @param B the matrix to multiply this with
* @param C the matrix to add the result to
* @param threadPool the source of threads to do computation in parallel
*/
abstract public void multiply(Matrix B, Matrix C, ExecutorService threadPool);
/**
* Alters the matrix C to be equal to C = C+A*BT
* @param B the matrix to multiply this with
* @param C the matrix to add the result to
*/
abstract public void multiplyTranspose(final Matrix B, final Matrix C);
/**
* Returns the new matrix C that is C = A*BT
* @param B the matrix to multiply by the transpose of
* @return the result C
*/
public Matrix multiplyTranspose(final Matrix B)
{
Matrix C = new DenseMatrix(this.rows(), B.rows());
multiplyTranspose(B, C);
return C;
}
/**
* Alters the matrix C to be equal to C = C+A*BT
* @param B the matrix to multiply this with
* @param C the matrix to add the result to
* @param threadPool the source of threads to do computation in parallel
*/
abstract public void multiplyTranspose(final Matrix B, final Matrix C, ExecutorService threadPool);
/**
* Returns the new matrix C that is C = A*BT
* @param B the matrix to multiply by the transpose of
* @param threadPool the source of threads to do computation in parallel
* @return the result C
*/
public Matrix multiplyTranspose(final Matrix B, ExecutorService threadPool)
{
Matrix C = new DenseMatrix(this.rows(), B.rows());
multiplyTranspose(B, C, threadPool);
return C;
}
/**
* Creates a new Matrix that stores A*c
* @param c the scalar constant to multiply by
* @return a new vector A*c
*/
public Matrix multiply(double c)
{
Matrix toReturn = getThisSideMatrix(null);
toReturn.mutableMultiply(c);
return toReturn;
}
/**
* Creates a new Matrix that stores A*c
* @param c the scalar constant to multiply by
* @param threadPool the source of threads to do computation in parallel
* @return a new matrix equal to A*c
*/
public Matrix multiply(double c, ExecutorService threadPool)
{
Matrix toReturn = getThisSideMatrix(null);
toReturn.mutableMultiply(c, threadPool);
return toReturn;
}
/**
* Alters the current matrix to be equal to A*c
* @param c the scalar constant to multiply by
*/
abstract public void mutableMultiply(double c);
/**
* Alters the current matrix to be equal to A*c
* @param c the scalar constant to multiply by
* @param threadPool the source of threads to do computation in parallel
*/
abstract public void mutableMultiply(double c, ExecutorService threadPool);
abstract public Matrix[] lup();
abstract public Matrix[] lup(ExecutorService threadPool);
abstract public Matrix[] qr();
abstract public Matrix[] qr(ExecutorService threadPool);
/**
* This method alters the size of a matrix, either adding or subtracting
* rows from the internal structure of the matrix. Every resize call may
* cause a new allocation internally, and should not be called for excessive
* changing of a matrix. All added rows/ columns will have values of zero.
* If a row / column is removed, it is always the bottom/right most row /
* column removed. Values of the removed rows / columns will be lost.
*
* @param newRows the new number of rows, must be positive
* @param newCols the new number of columns, must be positive.
*/
abstract public void changeSize(int newRows, int newCols);
/**
* Transposes the current matrix in place, altering its value.
* Only valid for square matrices
*/
abstract public void mutableTranspose();
/**
* Returns a new matrix that is the transpose of this matrix.
* @return a new matrix A'
*/
public Matrix transpose()
{
Matrix toReturn = new DenseMatrix(cols(), rows());
this.transpose(toReturn);
return toReturn;
}
/**
* Overwrites the values stored in matrix C to store the value of
* A'
* @param C the matrix to store the transpose of the current matrix
* @throws ArithmeticException if the dimensions of C do not match
* the dimensions of this'
*/
abstract public void transpose(Matrix C);
/**
* Creates a new matrix equal to A'*B, or the same result as
*
* A.{@link #transpose() transpose()}.{@link #multiply(jsat.linear.Matrix) multiply(B)}
*
*
* @param B the other Matrix
* @return a new matrix equal to A'*B
*/
public Matrix transposeMultiply(Matrix B)
{
Matrix C = new DenseMatrix(this.cols(), B.cols());
transposeMultiply(B, C);
return C;
}
/**
* Alters the matrix C so that C = C + A'*B
* @param B the matrix to multiply by
* @param C the matrix to add the result to
*/
abstract public void transposeMultiply(Matrix B, Matrix C);
/**
* Computes the result matrix of A'*B, or the same result as
*
* A.{@link #transpose() transpose()}.{@link #multiply(jsat.linear.Matrix) multiply(B)}
*
*
* @param B the matrix to multiply by
* @param threadPool the source of threads to do computation in parallel
* @return a new matrix equal to A'*B
*/
public Matrix transposeMultiply(Matrix B, ExecutorService threadPool)
{
Matrix C = new DenseMatrix(this.cols(), B.cols());
transposeMultiply(B, C, threadPool);
return C;
}
/**
* Alters the matrix C so that C = C + A'*B
* @param B the matrix to multiply by
* @param C the matrix to place the results in
* @param threadPool the source of threads to do computation in parallel
*/
abstract public void transposeMultiply(Matrix B, Matrix C, ExecutorService threadPool);
/**
* Alters the vector x to be equal to x = x + A'*b*c
*
* @param c the scalar constant to multiply by
* @param b the vector to multiply by
* @param x the vector the add the result to
*/
abstract public void transposeMultiply(double c, Vec b, Vec x);
/**
* Creates a new vector equal to x = A'*b*c
* @param c the scalar constant to multiply by
* @param b the vector to multiply by
* @return the new vector equal to A'*b*c
*/
public Vec transposeMultiply(double c, Vec b)
{
DenseVector toReturns = new DenseVector(this.cols());
this.transposeMultiply(c, b, toReturns);
return toReturns;
}
/**
* Returns the value stored at at the matrix position Ai,j
* @param i the row, starting from 0
* @param j the column, starting from 0
* @return the value at Ai,j
*/
abstract public double get(int i, int j);
/**
* Sets the value stored at at the matrix position Ai,j
* @param i the row, starting from 0
* @param j the column, starting from 0
* @param value the value to place at Ai,j
*/
abstract public void set(int i, int j, double value);
/**
* Alters the current matrix at index (i,j) to be equal to
* Ai,j = Ai,j + value
* @param i the row, starting from 0
* @param j the column, starting from 0
* @param value the value to add to the matrix coordinate
*/
public void increment(int i, int j, double value)
{
if(Double.isNaN(value) || Double.isInfinite(value))
throw new ArithmeticException("Can not add a value " + value);
set(i, j, get(i, j)+value);
}
/**
* Returns the number of rows stored in this matrix
* @return the number of rows stored in this matrix
*/
abstract public int rows();
/**
* Returns the number of columns stored in this matrix
* @return the number of columns stored in this matrix
*/
abstract public int cols();
/**
* Returns {@code true} if the matrix is sparse, {@code false} otherwise
* @return {@code true} if the matrix is sparse, {@code false} otherwise
*/
abstract public boolean isSparce();
/**
* Returns the number of non zero values stored in this matrix. This is
* mostly useful for sparse matrices.
*
* @return the number of non zero values stored in this matrix.
*/
public long nnz()
{
return ((long)rows())*cols();
}
/**
* Returns {@code true} if the matrix is square, meaning it has the same
* number of {@link #rows() rows} and {@link #cols() columns}.
* @return {@code true} if this matrix is square, {@code false} if it is
* rectangular.
*/
public boolean isSquare()
{
return rows() == cols();
}
/**
* Alters the current matrix by swapping the values stored in two different
* rows.
* @param r1 the first row to swap
* @param r2 the second row to swap
*/
abstract public void swapRows(int r1, int r2);
/**
* Creates a vector that has a copy of the values in column j of this
* matrix. Altering it will not effect the values in this matrix
* @param j the column to copy
* @return a clone of the column as a {@link Vec}
*/
public Vec getColumn(int j)
{
if(j < 0 || j >= cols())
throw new ArithmeticException("Column was not a valid value " + j + " not in [0," + (cols()-1) + "]");
DenseVector c = new DenseVector(rows());
for(int i =0; i < rows(); i++)
c.set(i, get(i, j));
return c;
}
/**
* Obtains a vector that is backed by this, at very little memory
* cost. Mutations to this vector will alter the values stored in the
* matrix, and vice versa.
*
* @param j the column to obtain a view of
* @return a vector backed by the specified row of the matrix
*/
public Vec getColumnView(final int j)
{
final Matrix M = this;
return new Vec()
{
/**
*
*/
private static final long serialVersionUID = 7107290189250645384L;
@Override
public int length()
{
return rows();
}
@Override
public double get(int index)
{
return M.get(index, j);
}
@Override
public void set(int index, double val)
{
M.set(index, j, val);
}
@Override
public boolean isSparse()
{
return M.isSparce();
}
@Override
public Vec clone()
{
if(M.isSparce())
return new SparseVector(this);
else
return new DenseVector(this);
}
};
}
/**
* Creates a vector that has a copy of the values in row i of this
* matrix. Altering it will not effect the values in this matrix.
* @param r the row to copy
* @return a clone of the row as a {@link Vec}
*/
public Vec getRow(int r)
{
if(r < 0 || r >= rows())
throw new ArithmeticException("Row was not a valid value " + r + " not in [0," + (rows()-1) + "]");
DenseVector c = new DenseVector(cols());
for(int j =0; j < cols(); j++)
c.set(j, get(r, j));
return c;
}
/**
* Obtains a vector that is backed by this, at very little memory
* cost. Mutations to this vector will alter the values stored in the
* matrix, and vice versa.
*
* @param r the row to obtain a view of
* @return a vector backed by the specified row of the matrix
*/
public Vec getRowView(final int r)
{
final Matrix M = this;
return new Vec()
{
/**
*
*/
private static final long serialVersionUID = 8484494698777822563L;
@Override
public int length()
{
return M.cols();
}
@Override
public double get(int index)
{
return M.get(r, index);
}
@Override
public void set(int index, double val)
{
M.set(r, index, val);
}
@Override
public boolean isSparse()
{
return M.isSparce();
}
@Override
public Vec clone()
{
if(M.isSparce())
return new SparseVector(this);
else
return new DenseVector(this);
}
};
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(rows()*cols());
sb.append("[");
for(int i = 0; i < rows(); i++)
{
sb.append(get(i, 0));
for(int j = 1; j < cols(); j++)
{
sb.append(", ").append(get(i, j));
}
sb.append(";");
}
sb.append("]");
return sb.toString();
}
/**
* Convenience method that will return {@code true} only if the two input
* matrices have the exact same dimensions.
* @param A the first matrix
* @param B the second matrix
* @return {@code true} if they have the exact same dimensions,
* {@code false} otherwise.
*/
public static boolean sameDimensions(Matrix A, Matrix B)
{
return A.rows() == B.rows() && A.cols() == B.cols();
}
/**
* Convenience method that will return {@code true} only if the two input
* matrices have dimensions compatible for multiplying A*B
* @param A the first matrix
* @param B the second matrix
* @return {@code true} if they have dimensions allowing multiplication,
* {@code false} otherwise.
*/
public static boolean canMultiply(Matrix A, Matrix B)
{
return A.cols() == B.rows();
}
@Override
public boolean equals(Object obj)
{
if(obj == null || !(obj instanceof Matrix))
return false;
Matrix that = (Matrix) obj;
if(this.rows() != that.rows() || this.cols() != that.cols())
return false;
for(int i = 0; i < rows(); i++)
for(int j = 0; j < cols(); j++)
if(this.get(i, j) != that.get(i, j))
return false;
return true;
}
/**
* Performs the same as {@link #equals(java.lang.Object) }, but allows a
* leniency in the differences between matrix values. This is useful for
* when some amount of numerical error is expected
*
* @param obj the other matrix
* @param range the max acceptable difference between two cell values
* @return {@code true} if the difference between the values of each pair of
* matrix elements are less than or equal to range
*/
public boolean equals(Object obj, double range)
{
if(obj == null || !(obj instanceof Matrix))
return false;
Matrix that = (Matrix) obj;
if(this.rows() != that.rows() || this.cols() != that.cols())
return false;
for(int i = 0; i < rows(); i++)
for(int j = 0; j < cols(); j++)
if(Math.abs(this.get(i, j)-that.get(i, j)) > range)
return false;
return true;
}
/**
* Alters the current matrix so that all values are equal to zero.
*/
abstract public void zeroOut();
/**
* Copes the values of this matrix into the other matrix of the same dimensions
* @param other the matrix to overwrite the values of
*/
public void copyTo(Matrix other)
{
if (this.rows() != other.rows() || this.cols() != other.cols())
throw new ArithmeticException("Matrices are not of the same dimension");
for(int i = 0; i < rows(); i++)
this.getRowView(i).copyTo(other.getRowView(i));
}
/**
* Alters row i of this matrix, such that
* A[i,:] = A[i,:] + c*b
* @param i the index of the row to update
* @param c the scalar constant to multiply the vector by
* @param b the vector to add to the specified row
*/
public void updateRow(int i, double c, Vec b)
{
if(b.length() != this.cols())
throw new ArithmeticException("vector is not of the same column length");
if (b.isSparse())
for (IndexValue iv : b)
this.increment(i, iv.getIndex(), c * iv.getValue());
else
for (int j = 0; j < b.length(); j++)
this.increment(i, j, c * b.get(j));
}
/**
* Alters the matrix A such that,
* A = A + c * x * y'
*
* @param A the matrix to update
* @param x the first vector
* @param y the second vector
* @param c the scalar constant to multiply the outer product by
* @throws ArithmeticException if the vector dimensions are not compatible
* with the matrix A
*/
public static void OuterProductUpdate(Matrix A, Vec x, Vec y, double c)
{
if (x.length() != A.rows() || y.length() != A.cols())
throw new ArithmeticException("Matrix dimensions do not agree with outer product");
if (x.isSparse())
for (IndexValue iv : x)
A.updateRow(iv.getIndex(), iv.getValue() * c, y);
else
for (int i = 0; i < x.length(); i++)
{
double rowCosnt = c * x.get(i);
A.updateRow(i, rowCosnt, y);
}
}
/**
* Alters the matrix A such that,
* A = A + c * x * y'
*
* @param A the matrix to update
* @param x the first vector
* @param y the second vector
* @param c the scalar constant to multiply the outer product by
* @param threadpool the source of threads to do computation in parallel
*/
public static void OuterProductUpdate(final Matrix A, final Vec x, final Vec y, final double c, ExecutorService threadpool)
{
if(x.length() != A.rows() || y.length() != A.cols())
throw new ArithmeticException("Matrix dimensions do not agree with outer product");
if (x.isSparse())
{
final ModifiableCountDownLatch mcdl = new ModifiableCountDownLatch(1);
for (final IndexValue iv : x)
{
mcdl.countUp();
threadpool.submit(new Runnable()
{
@Override
public void run()
{
A.updateRow(iv.getIndex(), iv.getValue() * c, y);
mcdl.countDown();
}
});
}
mcdl.countDown();
try
{
mcdl.await();
}
catch (InterruptedException ex)
{
Logger.getLogger(Matrix.class.getName()).log(Level.SEVERE, null, ex);
}
}
else
{
final CountDownLatch latch = new CountDownLatch(LogicalCores);
for(int id = 0; id < LogicalCores; id++)
{
final int threadID = id;
threadpool.submit(new Runnable()
{
@Override
public void run()
{
for(int i = threadID; i < x.length(); i+=LogicalCores)
{
double rowCosnt = c*x.get(i);
A.updateRow(i, rowCosnt, y);
}
latch.countDown();
}
});
}
try
{
latch.await();
}
catch (InterruptedException ex)
{
Logger.getLogger(Matrix.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* Creates a new dense identity matrix with k rows and columns.
* @param k the number of rows / columns
* @return a new dense identity matrix Ik
*/
public static DenseMatrix eye(int k)
{
DenseMatrix eye = new DenseMatrix(k, k);
for(int i = 0; i < k; i++ )
eye.set(i, i, 1);
return eye;
}
/**
* Creates a new dense matrix filled with random values from
* {@link Random#nextDouble() }
*
* @param rows the number of rows for the matrix
* @param cols the number of columns for the matrix
* @param rand the source of randomness
* @return a new dense matrix full of random values
*/
public static DenseMatrix random(int rows, int cols, Random rand)
{
DenseMatrix m = new DenseMatrix(rows, cols);
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
m.set(i, j, rand.nextDouble());
return m;
}
/**
* Returns a new dense square matrix such that the main diagonal contains
* the values given in a
* @param a the diagonal values of a matrix
* @return the diagonal matrix represent by a
*/
public static Matrix diag(Vec a)
{
DenseMatrix A = new DenseMatrix(a.length(), a.length());
for(Iterator iter = a.getNonZeroIterator(); iter.hasNext();)
{
IndexValue iv = iter.next();
A.set(iv.getIndex(), iv.getIndex(), iv.getValue());
}
return A;
}
/**
* Alters the matrix A so that it contains the result of A
* times a sparse matrix represented by only its diagonal values or
* A = A*diag(b). This is equivalent to the code
*
* A = A{@link #multiply(jsat.linear.Matrix) .multiply}
* ({@link #diag(jsat.linear.Vec) diag}(b))
*
* @param A the square matrix to update
* @param b the diagonal value vector
*/
public static void diagMult(Matrix A, Vec b)
{
if(A.cols() != b.length())
throw new ArithmeticException("Could not multiply, matrix dimensions must agree");
for(int i = 0; i < A.rows(); i++)
RowColumnOps.multRow(A, i, b);
}
/**
* Alters the matrix A so that it contains the result of
* sparse matrix represented by only its diagonal values times A or
* A = diag(b)*A. This is equivalent to the code
*
* b{@link Vec#multiply(jsat.linear.Matrix) .multiply}
* ({@link #diag(jsat.linear.Vec) diag}(A))
*
* @param b the diagonal value vector
* @param A the square matrix to update
*/
public static void diagMult(Vec b, Matrix A)
{
if(A.rows() != b.length())
throw new ArithmeticException("Could not multiply, matrix dimensions must agree");
for(int i = 0; i < A.rows(); i++)
RowColumnOps.multRow(A, i, b.get(i));
}
/**
* Checks to see if the given input is approximately symmetric. Rounding
* errors may cause the computation of a matrix to come out non symmetric,
* where |a[i,h] - a[j, i]| < eps. Despite these errors, it may be
* preferred to treat the matrix as perfectly symmetric regardless.
*
* @param A the input matrix
* @param eps the maximum tolerable difference between two entries
* @return {@code true} if the matrix is approximately symmetric
*/
public static boolean isSymmetric(Matrix A, double eps)
{
if(!A.isSquare())
return false;
for(int i = 0; i < A.rows(); i++)
for(int j = i+1; j < A.cols(); j++)
if( Math.abs(A.get(i, j)-A.get(j, i)) > eps)
return false;
return true;
}
/**
* Checks to see if the given input is a perfectly symmetric matrix
* @param A the input matrix
* @return {@code true} if it is perfectly symmetric.
*/
public static boolean isSymmetric(Matrix A)
{
return isSymmetric(A, 0.0);
}
/**
* Creates a new square matrix that is a pascal matrix. The pascal matrix of
* size n is n by n and symmetric.
*
* @param size the number of rows and columns for the matrix
* @return a pascal matrix of the desired size
*/
public static Matrix pascal(int size)
{
if(size <= 0 )
throw new ArithmeticException();
DenseMatrix P = new DenseMatrix(size, size);
RowColumnOps.fillRow(P, 0, 0, size, 1.0);
RowColumnOps.fillCol(P, 0, 0, size, 1.0);
for(int i = 1; i < size; i++)
for(int j = 1; j < size; j++)
P.set(i, j, P.get(i-1, j) + P.get(i, j-1));
return P;
}
@Override
abstract public Matrix clone();
}
Ads help maintain this website.