Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. public Matrix getCoMatrix() throws MatrixException {
  2.         if (!isSquared()) {
  3.             return null;
  4.         }
  5.         Matrix m = new Matrix(getNumRows(), getNumColumns());
  6.         for (int i = 0; i < getNumRows(); i++) {
  7.             for (int j = 0; j < getNumColumns(); j++) {
  8.                 Matrix subMatrix = this.getSubmatrix(i,j);
  9.                 m.setElement(i, j, changeSign(i) * changeSign(j) * subMatrix.getDeterminant());
  10.             }
  11.         }
  12.  
  13.         return m;
  14.     }
  15.  
  16. public Matrix getSubmatrix(int line, int col) throws MatrixException {
  17.         try {
  18.             Matrix m = new Matrix(getNumColumns() - 1, getNumColumns() - 1);
  19.  
  20.             int r = -1;
  21.  
  22.             for (int i = 0; i < getNumRows(); i++) {
  23.                 if (i == line)
  24.                     continue;
  25.  
  26.                 r++;
  27.                 int c = -1;
  28.  
  29.                 for (int j = 0; j < getNumColumns(); j++) {
  30.                     if (j == col)
  31.                         continue;
  32.  
  33.                     m.setElement(r, ++c, getElement(i, j));
  34.                 }
  35.             }
  36.  
  37.             return m;
  38.         } catch (IndexOutOfBoundsException e) {
  39.             throw new MatrixException("Invalid line or column parameter");
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement