def check_grad(f_df, xref, stepsize=<NUM_LIT>, tol=<NUM_LIT>, width=<NUM_LIT:15>, style='<STR_LIT>', out=sys.stdout):
CORRECT = u'<STR_LIT>'<EOL>INCORRECT = u'<STR_LIT>'<EOL>obj, grad = wrap(f_df, xref, size=<NUM_LIT:0>)<EOL>x0 = destruct(xref)<EOL>df = grad(x0)<EOL>out.write(tp.header(["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"], width=width, style=style) + "<STR_LIT:\n>")<EOL>out.flush()<EOL>def parse_error(number):<EOL><INDENT>failure = "<STR_LIT>"<EOL>passing = "<STR_LIT>"<EOL>warning = "<STR_LIT>"<EOL>end = "<STR_LIT>"<EOL>base = "<STR_LIT>"<EOL>if error < <NUM_LIT:0.1> * tol:<EOL><INDENT>return base.format(passing, error, end)<EOL><DEDENT>elif error < tol:<EOL><INDENT>return base.format(warning, error, end)<EOL><DEDENT>else:<EOL><INDENT>return base.format(failure, error, end)<EOL><DEDENT><DEDENT>num_errors = <NUM_LIT:0><EOL>for j in range(x0.size):<EOL><INDENT>dx = np.zeros(x0.size)<EOL>dx[j] = stepsize<EOL>df_approx = (obj(x0 + dx) - obj(x0 - dx)) / (<NUM_LIT:2> * stepsize)<EOL>df_analytic = df[j]<EOL>abs_error = np.linalg.norm(df_approx - df_analytic)<EOL>error = abs_error if np.allclose(abs_error, <NUM_LIT:0>) else abs_error /(np.linalg.norm(df_analytic) + np.linalg.norm(df_approx))<EOL>num_errors += error >= tol<EOL>errstr = CORRECT if error < tol else INCORRECT<EOL>out.write(tp.row([df_approx, df_analytic, parse_error(error) + '<STR_LIT:U+0020>' + errstr],<EOL>width=width, style=style) + "<STR_LIT:\n>")<EOL>out.flush()<EOL><DEDENT>out.write(tp.bottom(<NUM_LIT:3>, width=width, style=style) + "<STR_LIT:\n>")<EOL>return num_errors<EOL>
Compares the numerical gradient to the analytic gradient

Parameters
----------
f_df : function
    The analytic objective and gradient function to check

x0 : array_like
    Parameter values to check the gradient at

stepsize : float, optional
    Stepsize for the numerical gradient. Too big and this will poorly estimate the gradient.
    Too small and you will run into precision issues (default: 1e-6)

tol : float, optional
    Tolerance to use when coloring correct/incorrect gradients (default: 1e-5)

width : int, optional
    Width of the table columns (default: 15)

style : string, optional
    Style of the printed table, see tableprint for a list of styles (default: 'round')