use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class QRDecompositionExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println(">>> QR decomposition example started.");
Matrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
System.out.println("\n>>> Input matrix:");
Tracer.showAscii(m);
QRDecomposition dec = new QRDecomposition(m);
System.out.println("\n>>> Value for full rank in decomposition: [" + dec.hasFullRank() + "].");
Matrix q = dec.getQ();
Matrix r = dec.getR();
System.out.println("\n>>> Orthogonal matrix Q:");
Tracer.showAscii(q);
System.out.println("\n>>> Upper triangular matrix R:");
Tracer.showAscii(r);
Matrix qSafeCp = safeCopy(q);
Matrix identity = qSafeCp.times(qSafeCp.transpose());
System.out.println("\n>>> Identity matrix obtained from Q:");
Tracer.showAscii(identity);
Matrix recomposed = qSafeCp.times(r);
System.out.println("\n>>> Recomposed input matrix:");
Tracer.showAscii(recomposed);
Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10));
System.out.println("\n>>> Solved matrix:");
Tracer.showAscii(sol);
dec.destroy();
System.out.println("\n>>> QR decomposition example completed.");
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class MatrixExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Basic Matrix API usage example started.");
System.out.println("\n>>> Creating a matrix to be transposed.");
double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
Matrix m = new DenseLocalOnHeapMatrix(data);
Matrix transposed = m.transpose();
System.out.println(">>> Matrix: ");
MatrixExampleUtil.print(m);
System.out.println(">>> Transposed matrix: ");
MatrixExampleUtil.print(transposed);
MatrixExampleUtil.verifyTransposition(m, transposed);
System.out.println("\n>>> Creating matrices to be multiplied.");
double[][] data1 = new double[][] { { 1, 2 }, { 3, 4 } };
double[][] data2 = new double[][] { { 5, 6 }, { 7, 8 } };
Matrix m1 = new DenseLocalOnHeapMatrix(data1);
Matrix m2 = new DenseLocalOnHeapMatrix(data2);
Matrix mult = m1.times(m2);
System.out.println(">>> First matrix: ");
MatrixExampleUtil.print(m1);
System.out.println(">>> Second matrix: ");
MatrixExampleUtil.print(m2);
System.out.println(">>> Matrix product: ");
MatrixExampleUtil.print(mult);
System.out.println("\n>>> Calculating matrices determinants.");
double det1 = m1.determinant();
double det2 = m2.determinant();
double detMult = mult.determinant();
boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d;
System.out.println(">>> First matrix determinant: [" + det1 + "].");
System.out.println(">>> Second matrix determinant: [" + det2 + "].");
System.out.println(">>> Matrix product determinant: [" + detMult + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "].");
System.out.println("Determinant of product matrix [" + detMult + "] should be equal to product of determinants [" + (det1 * det2) + "].");
System.out.println("\n>>> Basic Matrix API usage example completed.");
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class CholeskyDecompositionExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println(">>> Cholesky decomposition example started.");
// Let's compute a Cholesky decomposition of Hermitian matrix m:
// m = l l^{*}, where
// l is a lower triangular matrix
// l^{*} is its conjugate transpose
DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
System.out.println("\n>>> Matrix m for decomposition: ");
Tracer.showAscii(m);
// This decomposition is useful when dealing with systems of linear equations of the form
// m x = b where m is a Hermitian matrix.
// For such systems Cholesky decomposition provides
// more effective method of solving compared to LU decomposition.
// Suppose we want to solve system
// m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs
// as a matrix of the form
// (b1, b2, ..., bm)
// to the method Cholesky::solve which returns solutions in the form
// (sol1, sol2, ..., solm)
CholeskyDecomposition dec = new CholeskyDecomposition(m);
System.out.println("\n>>> Made decomposition m = l * l^{*}.");
System.out.println(">>> Matrix l is ");
Tracer.showAscii(dec.getL());
System.out.println(">>> Matrix l^{*} is ");
Tracer.showAscii(dec.getLT());
Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } }).transpose();
System.out.println("\n>>> Solving systems of linear equations of the form m x = b for various bs represented by columns of matrix");
Tracer.showAscii(bs);
Matrix sol = dec.solve(bs);
System.out.println("\n>>> List of solutions: ");
for (int i = 0; i < sol.columnSize(); i++) Tracer.showAscii(sol.viewColumn(i));
System.out.println("\n>>> Cholesky decomposition example completed.");
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class IgniteSingularValueDecompositionBenchmark method runSingularValueDecomposition.
/**
* Based on SingularValueDecompositionTest#basicTest.
*/
private void runSingularValueDecomposition() {
Matrix m = new DenseLocalOnHeapMatrix(new DataChanger.Scale().mutate(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } }));
SingularValueDecomposition dec = new SingularValueDecomposition(m);
Matrix s = dec.getS();
Matrix u = dec.getU();
Matrix v = dec.getV();
u.times(s).times(v.transpose());
dec.destroy();
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class IgniteAbstractMatrixMulBenchmark method test.
/**
* {@inheritDoc}
*/
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
final double scale = DataChanger.next();
// Create IgniteThread, we may want to work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), this.getClass().getSimpleName(), new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
Matrix m1, m2, m3, m4, m5, m6;
Matrix m7 = times(m1 = createAndFill(dataSquare, scale), m2 = createAndFill(dataSquare, scale));
Matrix m8 = times(m3 = createAndFill(dataRect1, scale), m4 = createAndFill(dataRect2, scale));
Matrix m9 = times(m5 = createAndFill(dataRect2, scale), m6 = createAndFill(dataRect1, scale));
m1.destroy();
m2.destroy();
m3.destroy();
m4.destroy();
m5.destroy();
m6.destroy();
m7.destroy();
m8.destroy();
m9.destroy();
}
});
igniteThread.start();
igniteThread.join();
return true;
}
Aggregations