use of org.apache.ignite.ml.math.exceptions.math.CardinalityException in project ignite by apache.
the class LUDecomposition method solve.
/**
* @param b Matrix to solve using this decomposition.
* @return Solution matrix.
*/
public Matrix solve(Matrix b) {
final int m = pivot.size();
if (b.rowSize() != m)
throw new CardinalityException(b.rowSize(), m);
if (singular)
throw new SingularMatrixException();
final int nColB = b.columnSize();
// Apply permutations to b
final double[][] bp = new double[m][nColB];
for (int row = 0; row < m; row++) {
final double[] bpRow = bp[row];
final int pRow = (int) pivot.get(row);
for (int col = 0; col < nColB; col++) bpRow[col] = b.get(pRow, col);
}
// Solve LY = b
for (int col = 0; col < m; col++) {
final double[] bpCol = bp[col];
for (int i = col + 1; i < m; i++) {
final double[] bpI = bp[i];
final double luICol = lu.get(i, col);
for (int j = 0; j < nColB; j++) bpI[j] -= bpCol[j] * luICol;
}
}
// Solve UX = Y
for (int col = m - 1; col >= 0; col--) {
final double[] bpCol = bp[col];
final double luDiag = lu.getX(col, col);
for (int j = 0; j < nColB; j++) bpCol[j] /= luDiag;
for (int i = 0; i < col; i++) {
final double[] bpI = bp[i];
final double luICol = lu.get(i, col);
for (int j = 0; j < nColB; j++) bpI[j] -= bpCol[j] * luICol;
}
}
return b.like(b.rowSize(), b.columnSize()).assign(bp);
}
use of org.apache.ignite.ml.math.exceptions.math.CardinalityException in project ignite by apache.
the class Blas method gemv.
/**
* y := alpha * A * x + beta * y.
*
* @param alpha Alpha.
* @param a Matrix a.
* @param x Vector x.
* @param beta Beta.
* @param y Vector y.
*/
public static void gemv(double alpha, Matrix a, Vector x, double beta, Vector y) {
checkCardinality(a, x);
if (a.rowSize() != y.size())
throw new CardinalityException(a.columnSize(), y.size());
if (alpha == 0.0 && beta == 1.0)
return;
if (alpha == 0.0) {
scal(y, beta);
return;
}
double[] fA = a.getStorage().data();
double[] fX = x.getStorage().data();
double[] fY = y.getStorage().data();
nativeBlas.dgemv("N", a.rowSize(), a.columnSize(), alpha, fA, a.rowSize(), fX, 1, beta, fY, 1);
if (y instanceof SparseVector)
y.assign(fY);
}
Aggregations