src/main/java/com/osiris/maths/human/Matrix.java
/**
     * Copies the current matrix and performs gaussian elimination on it.
     * @return the matrix copy.
     */
Copies the current matrix and performs gaussian elimination on it.
@return the matrix copy.
Copies the current matrix and performs gaussian elimination on it . @return the matrix copy .
public Matrix copyWithGaussianElimination(boolean printSteps) throws NotQuadratic {
        if(!isQuadratic()) throw new NotQuadratic(this);
        Matrix copy = this.copy();
        if(printSteps) {
            System.out.println("PERFORM GAUSSIAN ELIMINATION ON COPY: ");
            System.out.println(copy);
            System.out.println("Before starting make sure element at(0,0) is == 1 and != 0.");
        }
        List<Variable> operationsToRevert = new ArrayList<>();
        copy.operationsToRevert = operationsToRevert;
        if(at(0, 0) == 0.0) {
            copy.rows.set(0, sumRowWith(0, 1.0));
            operationsToRevert.add(Variable.parse("+1.0"));
        }
        if(at(0,0) != 1.0) {
            copy.rows.set(0, divideRowWith(0, at(0, 0)));
            operationsToRevert.add(Variable.parse("/"+at(0, 0)));
        }
        if(printSteps) System.out.println(copy.asString());
        List<List<Double>> columns = copy.getColumns();
        for (int i = 0; i < columnsCount - 1; i++) {
            if(printSteps) System.out.println("Step "+i);
            if(copy.at(i, i) != 1.0){
                if(copy.at(i,i) == 0.0){
                    if(printSteps) System.out.print("at("+i+", "+i+") == "+ copy.at(i,i)+" != 1.0, but 0.0, thus "+copy.rows.get(i)+" + 1 = ");
                    List<Double> row = copy.sumRowWith(i, 1.0);
                    operationsToRevert.add(Variable.parse("+1.0"));
                    copy.rows.set(i, row);
                    if(printSteps) System.out.println(row);
                } else{
                    if(printSteps) System.out.print("at("+i+", "+i+") == "+ copy.at(i,i)+" != 1.0, thus "+copy.rows.get(i)+" / "+copy.at(i,i)+" = ");
                    List<Double> row = copy.divideRowWith(i, copy.at(i,i));
                    operationsToRevert.add(Variable.parse("/"+copy.at(i,i)));
                    copy.rows.set(i, row);
                    if(printSteps) System.out.println(row);
                }
            }
            for (int j = i + 1; j < columns.get(i).size(); j++) {
                if(printSteps) System.out.print(copy.rows.get(i)+" * "+Utils.invertNumber(columns.get(i).get(j))+" = ");
                List<Double> row = copy.multiplyRowWith(i, Utils.invertNumber(columns.get(i).get(j)));
                if(printSteps) {
                    System.out.println(row);
                    System.out.print(copy.rows.get(j)+" + "+row+" = ");
                }
                List<Double> result = copy.sumRowWith(j, row);
                if(printSteps) System.out.println(result);
                copy.rows.set(j, result);
            }
            columns = copy.getColumns();
            System.out.println("Result:");
            System.out.println(copy.asString());
        }
        return copy;
    }
public Matrix copyWithGaussianElimination ( boolean printSteps ) throws NotQuadratic { if ( ! isQuadratic ( ) ) throw new NotQuadratic ( this ) ; Matrix copy = this . copy ( ) ; if ( printSteps ) { System . out . println ( "PERFORM GAUSSIAN ELIMINATION ON COPY: " ) ; System . out . println ( copy ) ; System . out . println ( "Before starting make sure element at(0,0) is == 1 and != 0." ) ; } List < Variable > operationsToRevert = new ArrayList < > ( ) ; copy . operationsToRevert = operationsToRevert ; if ( at ( 0 , 0 ) == 0.0 ) { copy . rows . set ( 0 , sumRowWith ( 0 , 1.0 ) ) ; operationsToRevert . add ( Variable . parse ( "+1.0" ) ) ; } if ( at ( 0 , 0 ) != 1.0 ) { copy . rows . set ( 0 , divideRowWith ( 0 , at ( 0 , 0 ) ) ) ; operationsToRevert . add ( Variable . parse ( "/" + at ( 0 , 0 ) ) ) ; } if ( printSteps ) System . out . println ( copy . asString ( ) ) ; List < List < Double > > columns = copy . getColumns ( ) ; for ( int i = 0 ; i < columnsCount - 1 ; i ++ ) { if ( printSteps ) System . out . println ( "Step " + i ) ; if ( copy . at ( i , i ) != 1.0 ) { if ( copy . at ( i , i ) == 0.0 ) { if ( printSteps ) System . out . print ( "at(" + i + ", " + i + ") == " + copy . at ( i , i ) + " != 1.0, but 0.0, thus " + copy . rows . get ( i ) + " + 1 = " ) ; List < Double > row = copy . sumRowWith ( i , 1.0 ) ; operationsToRevert . add ( Variable . parse ( "+1.0" ) ) ; copy . rows . set ( i , row ) ; if ( printSteps ) System . out . println ( row ) ; } else { if ( printSteps ) System . out . print ( "at(" + i + ", " + i + ") == " + copy . at ( i , i ) + " != 1.0, thus " + copy . rows . get ( i ) + " / " + copy . at ( i , i ) + " = " ) ; List < Double > row = copy . divideRowWith ( i , copy . at ( i , i ) ) ; operationsToRevert . add ( Variable . parse ( "/" + copy . at ( i , i ) ) ) ; copy . rows . set ( i , row ) ; if ( printSteps ) System . out . println ( row ) ; } } for ( int j = i + 1 ; j < columns . get ( i ) . size ( ) ; j ++ ) { if ( printSteps ) System . out . print ( copy . rows . get ( i ) + " * " + Utils . invertNumber ( columns . get ( i ) . get ( j ) ) + " = " ) ; List < Double > row = copy . multiplyRowWith ( i , Utils . invertNumber ( columns . get ( i ) . get ( j ) ) ) ; if ( printSteps ) { System . out . println ( row ) ; System . out . print ( copy . rows . get ( j ) + " + " + row + " = " ) ; } List < Double > result = copy . sumRowWith ( j , row ) ; if ( printSteps ) System . out . println ( result ) ; copy . rows . set ( j , result ) ; } columns = copy . getColumns ( ) ; System . out . println ( "Result:" ) ; System . out . println ( copy . asString ( ) ) ; } return copy ; }
Copies the current matrix and performs gaussian elimination on it.
Copies the current matrix and performs gaussian elimination on it .
// These are operations done that need to be reverted at the end