package util.matrix;
import cern.colt.function.DoubleFunction;
import cern.colt.matrix.DoubleMatrix1D;
import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.impl.DenseDoubleMatrix1D;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
import cern.colt.matrix.linalg.Algebra;
import cern.colt.matrix.linalg.EigenvalueDecomposition;
import cern.jet.math.Functions;
public class ColtMatrixs {
private final static Algebra algebra = Algebra.DEFAULT;
public static DoubleMatrix2D create2DEmptyMatrix(int rows, int colums) {
return new DenseDoubleMatrix2D(rows, colums);
}
public static DoubleMatrix2D create2DMatrix(final double[][] elements) {
return new DenseDoubleMatrix2D(elements);
}
public static DoubleMatrix2D create2D1RowMatrix(final double[] elements,
int columns) {
DoubleMatrix2D matrix1d = new DenseDoubleMatrix2D(1, columns);
for (int i = 0; i < columns; i++) {
matrix1d.set(0, i, elements[i]);
}
return matrix1d;
}
public static DoubleMatrix2D create2D1ColumnMatrix(final double[] elements,
int rows) {
DoubleMatrix2D matrix1d = new DenseDoubleMatrix2D(rows, 1);
for (int i = 0; i < rows; i++) {
matrix1d.set(i, 0, elements[i]);
}
return matrix1d;
}
public static void set2DMatrixColumn(DoubleMatrix2D matrix2d, int column,
final DoubleMatrix2D vector) {
try {
matrix2d.viewColumn(column).assign(vector.viewColumn(0));
} catch (Exception e) {
matrix2d.viewColumn(column).assign(transpose(vector).viewColumn(0));
}
}
public static void set2DMatrixRow(DoubleMatrix2D matrix2d, int row,
final DoubleMatrix2D vector) {
try {
matrix2d.viewRow(row).assign(vector.viewRow(0));
} catch (Exception e) {
matrix2d.viewRow(row).assign(transpose(vector).viewRow(0));
}
}
public static void set2DMatrixColumn(DoubleMatrix2D matrix2d, int column,
final DoubleMatrix1D vector) {
matrix2d.viewColumn(column).assign(vector);
}
public static void set2DMatrixRow(DoubleMatrix2D matrix2d, int row,
final DoubleMatrix1D vector) {
matrix2d.viewRow(row).assign(vector);
}
// Shouldn't change original matrix
public static DoubleMatrix2D dotMultiply(final DoubleMatrix2D matrix1,
final DoubleMatrix2D matrix2) {
int rows = matrix1.rows();
int colums = matrix1.columns();
DoubleMatrix2D matrix2d = new DenseDoubleMatrix2D(rows, colums);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < colums; j++)
matrix2d.set(i, j, matrix1.get(i, j) * matrix2.get(i, j));
}
return matrix2d;
}
public static DoubleMatrix2D multiply(final DoubleMatrix2D matrix,
double val) {
int rows = matrix.rows();
int colums = matrix.columns();
DoubleMatrix2D matrix2d = new DenseDoubleMatrix2D(rows, colums);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < colums; j++)
matrix2d.set(i, j, matrix.get(i, j) * val);
}
return matrix2d;
}
public static DoubleMatrix2D multiply(final DoubleMatrix2D matrix2d,
final DoubleMatrix2D matrix2d2) {
return algebra.mult(matrix2d, matrix2d2);
}
public static DoubleMatrix2D add(final DoubleMatrix2D matrix2d, double val) {
DoubleMatrix2D result = matrix2d.copy();
return result.assign(
constantMatrix(matrix2d.rows(), matrix2d.columns(), val),
Functions.plus);
}
public static DoubleMatrix1D add(final DoubleMatrix1D matrix, double val) {
DoubleMatrix1D result = matrix.copy();
return result.assign(new DoubleFunction(){
@Override
public double apply(double element) {
// TODO Auto-generated method stub
return (element + val);
}
});
}
public static DoubleMatrix2D invert(final DoubleMatrix2D m) {
return algebra.inverse(m);
}
public static DoubleMatrix2D leftDivide(final DoubleMatrix2D lMatrix,
final DoubleMatrix2D rMatrix) {
return algebra.mult(invert(lMatrix), rMatrix);
}
public static DoubleMatrix1D rightDiagDivide(double divisor,
final DoubleMatrix1D matrix) {
int size = matrix.size();
DoubleMatrix1D resultMatrix = new DenseDoubleMatrix1D(size);
double element;
for (int i = 0; i < size; i++) {
element = matrix.get(i);
if (element == 0) {
System.out.println("Cannot divide 0, skip this element!");
continue;
}
resultMatrix.set(i, divisor / element);
}
return resultMatrix;
}
public static DoubleMatrix2D constantMatrix(int rows, int columns,
double val) {
return new DenseDoubleMatrix2D(rows, columns).assign(val);
}
public static DoubleMatrix2D transpose(DoubleMatrix2D matrix) {
return matrix.viewDice();
}
// solve the normal system of equations and find fitted ellipsoid parameters
public static DoubleMatrix2D solveEquationMatrix(final DoubleMatrix2D matrix) {
return leftDivide(
multiply(transpose(matrix), matrix),
multiply(transpose(matrix), constantMatrix(matrix.rows(), 1, 1)));
}
// form the algebraic form of the ellipsoid
public static DoubleMatrix2D generateEllipsoid(final DoubleMatrix2D matrix) {
return new DenseDoubleMatrix2D(new double[][] {
{ matrix.get(0, 0), matrix.get(3, 0), matrix.get(4, 0) },
{ matrix.get(3, 0), matrix.get(1, 0), matrix.get(5, 0) },
{ matrix.get(4, 0), matrix.get(5, 0), matrix.get(2, 0) } });
}
public static DoubleMatrix2D select(final DoubleMatrix2D matrix, int rows,
int columns) {
return algebra.subMatrix(matrix, 0, rows - 1, 0, columns - 1);
}
public static DoubleMatrix2D getEigValueMatrix2d(final DoubleMatrix2D matrix) {
return new EigenvalueDecomposition(matrix).getD();
}
public static DoubleMatrix2D getEigVectorMatrix2d(
final DoubleMatrix2D matrix) {
return new EigenvalueDecomposition(matrix).getV();
}
public static DoubleMatrix1D diag(final DoubleMatrix2D matrix) {
int diagCount = matrix.rows() < matrix.columns() ? matrix.rows()
: matrix.columns();
DoubleMatrix1D diagResult = new DenseDoubleMatrix1D(diagCount);
for (int i = 0; i < diagCount; i++) {
diagResult.set(i, matrix.get(i, i));
}
return diagResult;
}
public static DoubleMatrix2D sqrtMatrix(final DoubleMatrix2D matrix) {
DoubleMatrix2D result = matrix.copy();
return result.assign(Functions.sqrt);
}
public static DoubleMatrix1D sqrtMatrix(final DoubleMatrix1D matrix) {
DoubleMatrix1D result = matrix.copy();
return result.assign(Functions.sqrt);
}
// public static void main(String[] args) {
// DoubleMatrix2D matrix = new DenseDoubleMatrix2D(new double[][] {
// { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
//
// DoubleMatrix2D matrix2 = new DenseDoubleMatrix2D(new double[][] {
// { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } });
//
// DoubleMatrix1D matrix3 = new DenseDoubleMatrix1D(new double[] { 2, 3,
// 4, 5 });
//
// DoubleMatrix2D matrixA = new DenseDoubleMatrix2D(new double[][] {
// { 1, 2 }, { 2, 1 } });
//
// DoubleMatrix2D matrixB = new DenseDoubleMatrix2D(new double[][] {
// { 5, 8 }, { 4, 7 } });
//
// Double[][] values = new Double[][] {
// { new Double(1.0), new Double(1.0), new Double(2.0) },
// { new Double(1.0), new Double(2.0), new Double(2.0) },
// { new Double(2.0), new Double(2.0), new Double(2.0) } };
//
// System.out.println(ColtMatrixs.add(matrix, 2));
// System.out.println(ColtMatrixs.constantMatrix(3, 3, 1));
// System.out.println(ColtMatrixs.create2DMatrix(values, 3, 3));
// System.out.println(ColtMatrixs.diag(matrix));
// System.out.println(ColtMatrixs.dotMultiply(matrix, matrix2));
//
// System.out.println(multiply(matrix, 2));
// System.err.println(matrix);
// System.out.println(multiply(matrix, matrix));
// System.err.println(matrix);
// System.out.println(ColtMatrixs.sqrtMatrix(matrix));
// dotMultiply(matrix, matrix2);
// System.out.println(matrix);
// System.out.println(diag(matrix));
// System.out.println(select(matrix, 2, 2));
// System.out.println(constantMatrix(3,4,2));
// System.out.println(invert(matrix3));
// System.out.println(transpose(matrix3));
// System.out.println(leftDivide(matrixA, matrixB));
}