/** * Sets the selected rows of <code>A</code> equal zero, and puts * <code>diagonal</code> on the diagonal of those rows. Useful for enforcing * boundary conditions */ public static void zeroRows(Matrix A, double diagonal, int... row) { // Sort the rows int[] rowS = row.clone(); Arrays.sort(rowS); for (MatrixEntry e : A) { int j = java.util.Arrays.binarySearch(rowS, e.row()); if (j >= 0) { // Found if (e.row() == e.column()) // Diagonal e.set(diagonal); else // Off diagonal e.set(0); } } // Ensure the diagonal is set. This is necessary in case of missing // rows if (diagonal != 0) for (int rowI : row) A.set(rowI, rowI, diagonal); }
/** * Finds the number of non-zero entries on each column */ public static int[] columnBandwidth(Matrix A) { int[] nz = new int[A.numColumns()]; for (MatrixEntry e : A) nz[e.column()]++; return nz; }
/** * Finds the number of non-zero entries on each row */ public static int[] rowBandwidth(Matrix A) { int[] nz = new int[A.numRows()]; for (MatrixEntry e : A) nz[e.row()]++; return nz; }
/** * Returns the number of non-zero entries in the given matrix */ public static int cardinality(Matrix A) { int nz = 0; for (MatrixEntry e : A) if (e.get() != 0) nz++; return nz; }
public Matrix zero() { for (MatrixEntry e : this) e.set(0); return this; }
/** * Sets the selected columns of <code>A</code> equal zero, and puts * <code>diagonal</code> on the diagonal of those columns. Useful for * enforcing boundary conditions */ public static void zeroColumns(Matrix A, double diagonal, int... column) { // Sort the columns int[] columnS = column.clone(); Arrays.sort(columnS); for (MatrixEntry e : A) { int j = java.util.Arrays.binarySearch(columnS, e.column()); if (j >= 0) { // Found if (e.row() == e.column()) // Diagonal e.set(diagonal); else // Off diagonal e.set(0); } } // Ensure the diagonal is set. This is necessary in case of missing // columns if (diagonal != 0) for (int columnI : column) A.set(columnI, columnI, diagonal); }
/** * Finds the number of non-zero entries on each column */ public static int[] columnBandwidth(Matrix A) { int[] nz = new int[A.numColumns()]; for (MatrixEntry e : A) nz[e.column()]++; return nz; }
/** * Finds the number of non-zero entries on each row */ public static int[] rowBandwidth(Matrix A) { int[] nz = new int[A.numRows()]; for (MatrixEntry e : A) nz[e.row()]++; return nz; }
/** * Returns the number of non-zero entries in the given matrix */ public static int cardinality(Matrix A) { int nz = 0; for (MatrixEntry e : A) if (e.get() != 0) nz++; return nz; }
public Matrix zero() { for (MatrixEntry e : this) e.set(0); return this; }