/** * Wraps a linear solver of any type with a safe solver the ensures inputs are not modified */ public static <S extends Matrix, D extends Matrix> LinearSolver<S,D> safe(LinearSolver<S,D> solver ) { if( solver.modifiesA() || solver.modifiesB() ) { if( solver instanceof LinearSolverDense ) { return new LinearSolverSafe((LinearSolverDense)solver); } else if( solver instanceof LinearSolverSparse ) { return new LinearSolverSparseSafe((LinearSolverSparse)solver); } else { throw new IllegalArgumentException("Unknown solver type"); } } else { return solver; } }
@Override public void solve(T B, T X) { if( alg.modifiesB() ) { if( this.B == null ) { this.B = (T)B.copy(); } else { if( this.B.numRows != B.numRows || this.B.numCols != B.numCols ) { this.B.reshape(A.numRows,B.numCols,false); } this.B.set(B); } B = this.B; } alg.solve(B,X); }
@Override public void solve(T B, T X) { if( alg.modifiesB() ) { if( this.B == null ) { this.B = (T)B.copy(); } else { if( this.B.numRows != B.numRows || this.B.numCols != B.numCols ) { this.B.reshape(A.numRows,B.numCols,false); } this.B.set(B); } B = this.B; } alg.solve(B,X); }