Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Matrix getCoMatrix() throws MatrixException {
- if (!isSquared()) {
- return null;
- }
- Matrix m = new Matrix(getNumRows(), getNumColumns());
- for (int i = 0; i < getNumRows(); i++) {
- for (int j = 0; j < getNumColumns(); j++) {
- Matrix subMatrix = this.getSubmatrix(i,j);
- m.setElement(i, j, changeSign(i) * changeSign(j) * subMatrix.getDeterminant());
- }
- }
- return m;
- }
- public Matrix getSubmatrix(int line, int col) throws MatrixException {
- try {
- Matrix m = new Matrix(getNumColumns() - 1, getNumColumns() - 1);
- int r = -1;
- for (int i = 0; i < getNumRows(); i++) {
- if (i == line)
- continue;
- r++;
- int c = -1;
- for (int j = 0; j < getNumColumns(); j++) {
- if (j == col)
- continue;
- m.setElement(r, ++c, getElement(i, j));
- }
- }
- return m;
- } catch (IndexOutOfBoundsException e) {
- throw new MatrixException("Invalid line or column parameter");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement