use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class OffHeapMatrixExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Off-heap 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 DenseLocalOffHeapMatrix(data.length, data[0].length);
m.assign(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 DenseLocalOffHeapMatrix(data1.length, data1[0].length);
Matrix m2 = new DenseLocalOffHeapMatrix(data2.length, data2[0].length);
m1.assign(data1);
m2.assign(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>>> Off-heap matrix API usage example completed.");
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class SparseMatrixExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Sparse 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 SparseLocalOnHeapMatrix(data.length, data[0].length);
m.assign(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 SparseLocalOnHeapMatrix(data1.length, data1[0].length);
Matrix m2 = new SparseLocalOnHeapMatrix(data2.length, data2[0].length);
m1.assign(data1);
m2.assign(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>>> Sparse matrix API usage example completed.");
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class LUDecompositionExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println(">>> LU decomposition example started.");
// Let's compute a LU decomposition for some (n x n) matrix m:
// m = p l u, where
// p is an (n x n) is a row-permutation matrix
// l is a (n x n) lower triangular matrix
// u is a (n x n) upper triangular matrix
DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 1.0d, -1.0d }, { 1.0d, -2.0d, 3.0d }, { 2.0d, 3.0d, 1.0d } });
System.out.println("\n>>> Matrix m for decomposition: ");
Tracer.showAscii(m);
// This decomposition is useful when dealing with systems of linear equations.
// (see https://en.wikipedia.org/wiki/LU_decomposition)
// suppose we want to solve system
// m x = b for various bs. Then after we computed LU decomposition, we can feed various bs
// as a matrix of the form
// (b1, b2, ..., bm)
// to the method LUDecomposition::solve which returns solutions in the form
// (sol1, sol2, ..., solm)
LUDecomposition dec = new LUDecomposition(m);
System.out.println("\n>>> Made decomposition.");
System.out.println(">>> Matrix getL is ");
Tracer.showAscii(dec.getL());
System.out.println(">>> Matrix getU is ");
Tracer.showAscii(dec.getU());
System.out.println(">>> Matrix getP is ");
Tracer.showAscii(dec.getP());
Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } });
System.out.println("\n>>> Matrix to solve: ");
Tracer.showAscii(bs);
Matrix sol = dec.solve(bs.transpose());
System.out.println("\n>>> List of solutions: ");
for (int i = 0; i < sol.columnSize(); i++) Tracer.showAscii(sol.viewColumn(i));
System.out.println("\n>>> LU decomposition example completed.");
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class OLSMultipleLinearRegressionTest method testHat.
/**
* Test hat matrix computation
*/
@Test
public void testHat() {
/*
* This example is from "The Hat Matrix in Regression and ANOVA",
* David C. Hoaglin and Roy E. Welsch,
* The American Statistician, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
*
*/
double[] design = new double[] { 11.14, .499, 11.1, 12.74, .558, 8.9, 13.13, .604, 8.8, 11.51, .441, 8.9, 12.38, .550, 8.8, 12.60, .528, 9.9, 11.13, .418, 10.7, 11.7, .480, 10.5, 11.02, .406, 10.5, 11.41, .467, 10.7 };
int nobs = 10;
int nvars = 2;
// Estimate the model
OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression();
mdl.newSampleData(design, nobs, nvars, new DenseLocalOnHeapMatrix());
Matrix hat = mdl.calculateHat();
// Reference data is upper half of symmetric hat matrix
double[] refData = new double[] { .418, -.002, .079, -.274, -.046, .181, .128, .222, .050, .242, .242, .292, .136, .243, .128, -.041, .033, -.035, .004, .417, -.019, .273, .187, -.126, .044, -.153, .004, .604, .197, -.038, .168, -.022, .275, -.028, .252, .111, -.030, .019, -.010, -.010, .148, .042, .117, .012, .111, .262, .145, .277, .174, .154, .120, .168, .315, .148, .187 };
// Check against reference data and verify symmetry
int k = 0;
for (int i = 0; i < 10; i++) {
for (int j = i; j < 10; j++) {
Assert.assertEquals(refData[k], hat.getX(i, j), 10e-3);
Assert.assertEquals(hat.getX(i, j), hat.getX(j, i), 10e-12);
k++;
}
}
/*
* Verify that residuals computed using the hat matrix are close to
* what we get from direct computation, i.e. r = (I - H) y
*/
double[] residuals = mdl.estimateResiduals();
Matrix id = MatrixUtil.identityLike(hat, 10);
double[] hatResiduals = id.minus(hat).times(mdl.getY()).getStorage().data();
TestUtils.assertEquals(residuals, hatResiduals, 10e-12);
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class VectorToMatrixTest method testLikeMatrix.
/** */
@Test
public void testLikeMatrix() {
consumeSampleVectors((v, desc) -> {
if (!availableForTesting(v))
return;
final Matrix matrix = v.likeMatrix(1, 1);
Class<? extends Vector> key = v.getClass();
Class<? extends Matrix> expMatrixType = typesMap.get(key);
assertNotNull("Expect non-null matrix for " + key.getSimpleName() + " in " + desc, matrix);
Class<? extends Matrix> actualMatrixType = matrix.getClass();
assertTrue("Expected matrix type " + expMatrixType.getSimpleName() + " should be assignable from actual type " + actualMatrixType.getSimpleName() + " in " + desc, expMatrixType.isAssignableFrom(actualMatrixType));
for (int rows : new int[] { 1, 2 }) for (int cols : new int[] { 1, 2 }) {
final Matrix actualMatrix = v.likeMatrix(rows, cols);
String details = "rows " + rows + " cols " + cols;
assertNotNull("Expect non-null matrix for " + details + " in " + desc, actualMatrix);
assertEquals("Unexpected number of rows in " + desc, rows, actualMatrix.rowSize());
assertEquals("Unexpected number of cols in " + desc, cols, actualMatrix.columnSize());
}
});
}
Aggregations