def _bisect(value_and_gradients_function, initial_args, f_lim):
def _loop_cond(curr):<EOL><INDENT>return ~tf.reduce_all(input_tensor=curr.stopped)<EOL><DEDENT>def _loop_body(curr):<EOL><INDENT>"""<STR_LIT>"""<EOL>mid = value_and_gradients_function((curr.left.x + curr.right.x) / <NUM_LIT:2>)<EOL>failed = (curr.failed | ~is_finite(mid) |<EOL>tf.equal(mid.x, curr.left.x) | tf.equal(mid.x, curr.right.x))<EOL>to_update = ~(curr.stopped | failed)<EOL>update_left = (mid.df < <NUM_LIT:0>) & (mid.f <= f_lim)<EOL>left = val_where(to_update & update_left, mid, curr.left)<EOL>right = val_where(to_update & ~update_left, mid, curr.right)<EOL>stopped = curr.stopped | failed | (right.df >= <NUM_LIT:0>)<EOL>return [_IntermediateResult(<EOL>iteration=curr.iteration,<EOL>stopped=stopped,<EOL>failed=failed,<EOL>num_evals=curr.num_evals + <NUM_LIT:1>,<EOL>left=left,<EOL>right=right)]<EOL><DEDENT>return tf.while_loop(<EOL>cond=_loop_cond, body=_loop_body, loop_vars=[initial_args])[<NUM_LIT:0>]<EOL>
Actual implementation of bisect given initial_args in a _BracketResult.