/** * Returns the U part of the matrix. This does only make sense after LU * decomposition. * * @return matrix with the U part of a matrix; * @see #LUDecomposition() */ public Matrix getU() throws Exception { int nr = numRows(); // num of rows int nc = numColumns(); // num of columns double[][] ud = new double[nr][nc]; for (int i = 0; i < nr; i++) { for (int j = i; j < nc; j++) { ud[i][j] = getElement(i, j); } } Matrix u = new Matrix(ud); return u; }
/** * Gets a row of the matrix and returns it as double array. * * @param index the row's index * @return an array of doubles */ public double[] getRow(int index) { double[] newRow = new double[this.numColumns()]; for (int i = 0; i < newRow.length; i++) { newRow[i] = getElement(index, i); } return newRow; }
/** * Gets a column of the matrix and returns it as a double array. * * @param index the column's index * @return an array of doubles */ public double[] getColumn(int index) { double[] newColumn = new double[this.numRows()]; for (int i = 0; i < newColumn.length; i++) { newColumn[i] = getElement(i, index); } return newColumn; }
Matrix M = new Matrix(m); int n = M.numRows(); double[][] V = new double[n][n]; double[] d = new double[n]; M.eigenvalueDecomposition(V, d); Matrix a = new Matrix(2, 3); Matrix b = new Matrix(3, 2); System.out.println("Number of columns for a: " + a.numColumns()); System.out.println("Number of rows for a: " + a.numRows()); a.setRow(0, first); a.setRow(1, second); b.setColumn(0, first); b.setColumn(1, second); System.out.println("a:\n " + a); System.out.println("b:\n " + b); System.out.println("a (0, 0): " + a.getElement(0, 0)); System.out.println("a transposed:\n " + a.transpose()); System.out.println("a * b:\n " + a.multiply(b)); Matrix r = new Matrix(3, 1); r.setColumn(0, response); System.out.println("r:\n " + r); System.out.println("Coefficients of regression of b on r: "); double[] coefficients = b.regression(r, 1.0e-8); for (double coefficient : coefficients) { System.out.print(coefficient + " "); coefficients = b.regression(r, weights, 1.0e-8); for (double coefficient : coefficients) {
int nr = numRows(); int nc = numColumns(); for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { if (j < i) { setElement(i, j, l.get(i, j)); } else { setElement(i, j, u.get(i, j));
if (!this.isSymmetric()) { throw new Exception("EigenvalueDecomposition: Matrix must be symmetric."); int nr = numRows(); int nc = numColumns(); for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) {
/** * Creates and returns a clone of this object. * * @return a clone of this instance. * @throws Exception if an error occurs */ @Override public Object clone() { try { return new Matrix(m_Matrix.getArrayCopy()); } catch (Exception e) { e.printStackTrace(); return null; } }
/** * converts the Matrix into a single line Matlab string: matrix is enclosed by * parentheses, rows are separated by semicolon and single cells by blanks, * e.g., [1 2; 3 4]. * * @return the matrix in Matlab single line format */ public String toMatlab() { return getMatrix().toMatlab(); }
/** * Sets a column of the matrix to the given column. Performs a deep copy. * * @param index the column's index * @param newColumn an array of doubles */ public final void setColumn(int index, double[] newColumn) { for (int i = 0; i < numRows(); i++) { m_Matrix.set(i, index, newColumn[i]); } }
Matrix M = new Matrix(m); int n = M.numRows(); double[][] V = new double[n][n]; double[] d = new double[n]; M.eigenvalueDecomposition(V, d); Matrix a = new Matrix(2, 3); Matrix b = new Matrix(3, 2); System.out.println("Number of columns for a: " + a.numColumns()); System.out.println("Number of rows for a: " + a.numRows()); a.setRow(0, first); a.setRow(1, second); b.setColumn(0, first); b.setColumn(1, second); System.out.println("a:\n " + a); System.out.println("b:\n " + b); System.out.println("a (0, 0): " + a.getElement(0, 0)); System.out.println("a transposed:\n " + a.transpose()); System.out.println("a * b:\n " + a.multiply(b)); Matrix r = new Matrix(3, 1); r.setColumn(0, response); System.out.println("r:\n " + r); System.out.println("Coefficients of regression of b on r: "); double[] coefficients = b.regression(r, 1.0e-8); for (double coefficient : coefficients) { System.out.print(coefficient + " "); coefficients = b.regression(r, weights, 1.0e-8); for (double coefficient : coefficients) {
int nr = numRows(); int nc = numColumns(); for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { if (j < i) { setElement(i, j, l.get(i, j)); } else { setElement(i, j, u.get(i, j));
if (!this.isSymmetric()) { throw new Exception("EigenvalueDecomposition: Matrix must be symmetric."); int nr = numRows(); int nc = numColumns(); for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) {
/** * Creates and returns a clone of this object. * * @return a clone of this instance. * @throws Exception if an error occurs */ @Override public Object clone() { try { return new Matrix(m_Matrix.getArrayCopy()); } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Gets a row of the matrix and returns it as double array. * * @param index the row's index * @return an array of doubles */ public double[] getRow(int index) { double[] newRow = new double[this.numColumns()]; for (int i = 0; i < newRow.length; i++) { newRow[i] = getElement(index, i); } return newRow; }
/** * Gets a column of the matrix and returns it as a double array. * * @param index the column's index * @return an array of doubles */ public double[] getColumn(int index) { double[] newColumn = new double[this.numRows()]; for (int i = 0; i < newColumn.length; i++) { newColumn[i] = getElement(i, index); } return newColumn; }
/** * Performs a (ridged) linear regression. * * @param y the dependent variable vector * @param ridge the ridge parameter * @return the coefficients * @throws IllegalArgumentException if not successful */ public final double[] regression(Matrix y, double ridge) { return getMatrix().regression(y.getMatrix(), ridge).getCoefficients(); }
/** * Sets a column of the matrix to the given column. Performs a deep copy. * * @param index the column's index * @param newColumn an array of doubles */ public final void setColumn(int index, double[] newColumn) { for (int i = 0; i < numRows(); i++) { m_Matrix.set(i, index, newColumn[i]); } }