public DoubleMatrix1D like1D(int size) { return new SparseDoubleMatrix1D(size); }
/** * Constructs a matrix with a copy of the given values. The values are * copied. So subsequent changes in <tt>values</tt> are not reflected in the * matrix, and vice-versa. * * @param values * The values to be filled into the new matrix. */ public SparseDoubleMatrix1D(double[] values) { this(values.length); assign(values); }
public double getQuick(int row, int column) { return elements[row].getQuick(column); }
private void factor() { int n = LU.rows(); for (int i = 1; i < n; ++i) { // Get row i SparseDoubleMatrix1D rowi = LU.viewRow(i); // Drop tolerance on current row double taui = DenseDoubleAlgebra.DEFAULT.norm(rowi, Norm.Two) * tau; for (int k = 0; k < i; ++k) { // Get row k SparseDoubleMatrix1D rowk = LU.viewRow(k); if (rowk.getQuick(k) == 0) throw new RuntimeException("Zero diagonal entry on row " + (k + 1) + " during ILU process"); double LUik = rowi.getQuick(k) / rowk.getQuick(k); // Check for small elimination entry if (Math.abs(LUik) <= taui) continue; // Traverse the sparse row k, reducing row i int rowUsed = (int) rowk.size(); for (int j = k + 1; j < rowUsed; ++j) rowi.setQuick(j, rowi.getQuick(j) - LUik * rowk.getQuick(j)); // The above has overwritten LUik, so remedy that rowi.setQuick(k, LUik); } // Store back into the LU matrix, dropping as needed gather(rowi, taui, i); } // System.out.println(LU.toString()); }
public DoubleMatrix1D vectorize() { SparseDoubleMatrix1D v = new SparseDoubleMatrix1D((int) size()); int idx = 0; for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { double elem = getQuick(r, c); v.setQuick(idx++, elem); } } return v; }
private DoubleMatrix1D upperSolve(DoubleMatrix1D b, DoubleMatrix1D x) { double[] bd = ((DenseDoubleMatrix1D) b).elements(); double[] xd = ((DenseDoubleMatrix1D) x).elements(); int rows = LU.rows(); for (int i = rows - 1; i >= 0; --i) { // Get row i SparseDoubleMatrix1D row = LU.viewRow(i); int used = (int) row.size(); // xi = (bi - sum[j>i] Uij * xj) / Uii double sum = 0; for (int j = i + 1; j < used; ++j) sum += row.getQuick(j) * xd[j]; xd[i] = (bd[i] - sum) / row.getQuick(i); } return x; }
public String toString() { StringBuilder builder = new StringBuilder(); builder.append("1 x ").append(size).append(" sparse matrix, nnz = ").append(cardinality()).append('\n'); for (int i = 0; i < size; i++) { double elem = getQuick(i); if (elem != 0) { builder.append('(').append(i).append(')').append('\t').append(elem).append('\n'); } } return builder.toString(); }
public void setQuick(int row, int column, double value) { elements[row].setQuick(column, value); }
public void trimToSize() { for (int r = 0; r < rows; r++) { elements[r].trimToSize(); } }
/** * Constructs a matrix view with a given number of parameters. * * @param size * the number of cells the matrix shall have. * @param elements * the cells. * @param offset * the index of the first element. * @param stride * the number of indexes between any two elements, i.e. * <tt>index(i+1)-index(i)</tt>. * @throws IllegalArgumentException * if <tt>size<0</tt>. */ protected SparseDoubleMatrix1D(int size, AbstractLongDoubleMap elements, int offset, int stride) { setUp(size, offset, stride); this.elements = elements; this.isNoView = false; }
private void factor() { int n = LU.rows(); for (int i = 1; i < n; ++i) { // Get row i SparseDoubleMatrix1D rowi = LU.viewRow(i); // Drop tolerance on current row double taui = DenseDoubleAlgebra.DEFAULT.norm(rowi, Norm.Two) * tau; for (int k = 0; k < i; ++k) { // Get row k SparseDoubleMatrix1D rowk = LU.viewRow(k); if (rowk.getQuick(k) == 0) throw new RuntimeException("Zero diagonal entry on row " + (k + 1) + " during ILU process"); double LUik = rowi.getQuick(k) / rowk.getQuick(k); // Check for small elimination entry if (Math.abs(LUik) <= taui) continue; // Traverse the sparse row k, reducing row i int rowUsed = (int) rowk.size(); for (int j = k + 1; j < rowUsed; ++j) rowi.setQuick(j, rowi.getQuick(j) - LUik * rowk.getQuick(j)); // The above has overwritten LUik, so remedy that rowi.setQuick(k, LUik); } // Store back into the LU matrix, dropping as needed gather(rowi, taui, i); } // System.out.println(LU.toString()); }
public DoubleMatrix1D vectorize() { SparseDoubleMatrix1D v = new SparseDoubleMatrix1D((int) size()); int idx = 0; for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { double elem = getQuick(r, c); v.setQuick(idx++, elem); } } return v; }
private DoubleMatrix1D upperSolve(DoubleMatrix1D b, DoubleMatrix1D x) { double[] bd = ((DenseDoubleMatrix1D) b).elements(); double[] xd = ((DenseDoubleMatrix1D) x).elements(); int rows = LU.rows(); for (int i = rows - 1; i >= 0; --i) { // Get row i SparseDoubleMatrix1D row = LU.viewRow(i); int used = (int) row.size(); // xi = (bi - sum[j>i] Uij * xj) / Uii double sum = 0; for (int j = i + 1; j < used; ++j) sum += row.getQuick(j) * xd[j]; xd[i] = (bd[i] - sum) / row.getQuick(i); } return x; }
public String toString() { StringBuilder builder = new StringBuilder(); builder.append("1 x ").append(size).append(" sparse matrix, nnz = ").append(cardinality()).append('\n'); for (int i = 0; i < size; i++) { double elem = getQuick(i); if (elem != 0) { builder.append('(').append(i).append(')').append('\t').append(elem).append('\n'); } } return builder.toString(); }
public void setQuick(int row, int column, double value) { elements[column].setQuick(row, value); }
public void trimToSize() { for (int c = 0; c < columns; c++) { elements[c].trimToSize(); } }
/** * Constructs a matrix view with a given number of parameters. * * @param size * the number of cells the matrix shall have. * @param elements * the cells. * @param offset * the index of the first element. * @param stride * the number of indexes between any two elements, i.e. * <tt>index(i+1)-index(i)</tt>. * @throws IllegalArgumentException * if <tt>size<0</tt>. */ protected SparseDoubleMatrix1D(int size, AbstractLongDoubleMap elements, int offset, int stride) { setUp(size, offset, stride); this.elements = elements; this.isNoView = false; }
protected DoubleMatrix1D like1D(int size, int offset, int stride) { return new SparseDoubleMatrix1D(size, this.elements, offset, stride); }