/** * Normalizes a column of the transition-probability matrix * * @param A Transition probability matrix to normalize, modified by side effect * @param j Column of the matrix to normalize */ protected static void normalizeTransitionMatrix(Matrix A, final int j) { double sum = 0.0; final int k = A.getNumRows(); for (int i = 0; i < k; i++) { final double value = A.getElement(i, j); if (value < 0.0) { throw new IllegalArgumentException("Transition Probabilities must be >= 0.0"); } sum += A.getElement(i, j); } if (sum <= 0.0) { sum = 1.0; } if (sum != 1.0) { for (int i = 0; i < k; i++) { A.setElement(i, j, A.getElement(i, j) / sum); } } }
public static Matrix plusInplace(Matrix mat, double etat) { int nrows = mat.getNumRows(); int ncols = mat.getNumColumns(); for (int r = 0; r < nrows; r++) { for (int c = 0; c < ncols; c++) { mat.setElement(r, c, mat.getElement(r, c) + etat); } } return mat; }
public static double absSum(Matrix mat) { double tot = 0; int nrows = mat.getNumRows(); int ncols = mat.getNumColumns(); for (int r = 0; r < nrows; r++) { for (int c = 0; c < ncols; c++) { tot += Math.abs(mat.getElement(r, c)); } } return tot; }
public static Matrix abs(Matrix mat) { Matrix ret = mat.clone(); int nrows = ret.getNumRows(); int ncols = ret.getNumColumns(); for (int r = 0; r < nrows; r++) { for (int c = 0; c < ncols; c++) { ret.setElement(r, c, Math.abs(mat.getElement(r, c))); } } return ret; }
public static Vector diag(Matrix mat) { Vector ret; if (mat.getNumColumns() > mat.getNumRows()) { ret = mat.getRow(0); } else { ret = mat.getColumn(0); } int rowcol = ret.getDimensionality(); for (int rc = 0; rc < rowcol; rc++) { ret.setElement(rc, mat.getElement(rc, rc)); } return ret; }