def cg(f_Ax, b, cg_iters=<NUM_LIT:10>, callback=None, verbose=False, residual_tol=<NUM_LIT>):
p = b.copy()<EOL>r = b.copy()<EOL>x = np.zeros_like(b)<EOL>rdotr = r.dot(r)<EOL>fmtstr =  "<STR_LIT>"<EOL>titlestr =  "<STR_LIT>"<EOL>if verbose: print(titlestr % ("<STR_LIT>", "<STR_LIT>", "<STR_LIT>"))<EOL>for i in range(cg_iters):<EOL><INDENT>if callback is not None:<EOL><INDENT>callback(x)<EOL><DEDENT>if verbose: print(fmtstr % (i, rdotr, np.linalg.norm(x)))<EOL>z = f_Ax(p)<EOL>v = rdotr / p.dot(z)<EOL>x += v*p<EOL>r -= v*z<EOL>newrdotr = r.dot(r)<EOL>mu = newrdotr/rdotr<EOL>p = r + mu*p<EOL>rdotr = newrdotr<EOL>if rdotr < residual_tol:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if callback is not None:<EOL><INDENT>callback(x)<EOL><DEDENT>if verbose: print(fmtstr % (i+<NUM_LIT:1>, rdotr, np.linalg.norm(x)))  <EOL>return x<EOL>
Demmel p 312