def zinv(x, null=<NUM_LIT:0>):
if sps.issparse(x):<EOL><INDENT>if null != <NUM_LIT:0>: return zinv(x.toarray(), null=null)<EOL>x = x.copy()<EOL>x.data = zinv(x.data)<EOL>try: x.eliminate_zeros()<EOL>except Exception: pass<EOL>return x<EOL><DEDENT>else:<EOL><INDENT>x = np.asarray(x)<EOL>z = np.isclose(x, <NUM_LIT:0>)<EOL>r = np.logical_not(z) / (x + z)<EOL>if null == <NUM_LIT:0>: return r<EOL>r[z] = null<EOL>return r<EOL><DEDENT>
zinv(x) yields 1/x if x is not close to 0 and 0 otherwise. Automatically threads over arrays and
  supports sparse-arrays.

The optional argument null (default: 0) may be given to specify that zeros in the arary x should
instead be replaced with the given value. Note that if this value is not equal to 0, then any
sparse array passed to zinv must be reified.

The zinv function never raises an error due to divide-by-zero; if you desire this behavior, use
the inv function instead.