我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用numpy.frexp()。
def test_ufunc_override_out(self): # 2016-01-29: NUMPY_UFUNC_DISABLED return class A(object): def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): return kwargs class B(object): def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): return kwargs a = A() b = B() res0 = np.multiply(a, b, 'out_arg') res1 = np.multiply(a, b, out='out_arg') res2 = np.multiply(2, b, 'out_arg') res3 = np.multiply(3, b, out='out_arg') res4 = np.multiply(a, 4, 'out_arg') res5 = np.multiply(a, 5, out='out_arg') assert_equal(res0['out'], 'out_arg') assert_equal(res1['out'], 'out_arg') assert_equal(res2['out'], 'out_arg') assert_equal(res3['out'], 'out_arg') assert_equal(res4['out'], 'out_arg') assert_equal(res5['out'], 'out_arg') # ufuncs with multiple output modf and frexp. res6 = np.modf(a, 'out0', 'out1') res7 = np.frexp(a, 'out0', 'out1') assert_equal(res6['out'][0], 'out0') assert_equal(res6['out'][1], 'out1') assert_equal(res7['out'][0], 'out0') assert_equal(res7['out'][1], 'out1')
def test_frexp(self, dtype): numpy_a = numpy.array([-300, -20, -10, -1, 0, 1, 10, 20, 300], dtype=dtype) numpy_b, numpy_c = numpy.frexp(numpy_a) cupy_a = cupy.array(numpy_a) cupy_b, cupy_c = cupy.frexp(cupy_a) testing.assert_allclose(cupy_b, numpy_b) testing.assert_array_equal(cupy_c, numpy_c)
def log2int(x): "Fast integer part of the base-2 logarithm; mostly useful for powers of two but works for any real nonzero input" return np.frexp(x)[1] - 1
def test_ufunc_override_out(self): # Temporarily disable __numpy_ufunc__ for 1.10; see gh-5844 return class A(object): def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): return kwargs class B(object): def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): return kwargs a = A() b = B() res0 = np.multiply(a, b, 'out_arg') res1 = np.multiply(a, b, out='out_arg') res2 = np.multiply(2, b, 'out_arg') res3 = np.multiply(3, b, out='out_arg') res4 = np.multiply(a, 4, 'out_arg') res5 = np.multiply(a, 5, out='out_arg') assert_equal(res0['out'], 'out_arg') assert_equal(res1['out'], 'out_arg') assert_equal(res2['out'], 'out_arg') assert_equal(res3['out'], 'out_arg') assert_equal(res4['out'], 'out_arg') assert_equal(res5['out'], 'out_arg') # ufuncs with multiple output modf and frexp. res6 = np.modf(a, 'out0', 'out1') res7 = np.frexp(a, 'out0', 'out1') assert_equal(res6['out'][0], 'out0') assert_equal(res6['out'][1], 'out1') assert_equal(res7['out'][0], 'out0') assert_equal(res7['out'][1], 'out1')
def test_out_subok(self): for subok in (True, False): a = np.array(0.5) o = np.empty(()) r = np.add(a, 2, o, subok=subok) assert_(r is o) r = np.add(a, 2, out=o, subok=subok) assert_(r is o) r = np.add(a, 2, out=(o,), subok=subok) assert_(r is o) d = np.array(5.7) o1 = np.empty(()) o2 = np.empty((), dtype=np.int32) r1, r2 = np.frexp(d, o1, None, subok=subok) assert_(r1 is o1) r1, r2 = np.frexp(d, None, o2, subok=subok) assert_(r2 is o2) r1, r2 = np.frexp(d, o1, o2, subok=subok) assert_(r1 is o1) assert_(r2 is o2) r1, r2 = np.frexp(d, out=(o1, None), subok=subok) assert_(r1 is o1) r1, r2 = np.frexp(d, out=(None, o2), subok=subok) assert_(r2 is o2) r1, r2 = np.frexp(d, out=(o1, o2), subok=subok) assert_(r1 is o1) assert_(r2 is o2) with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', DeprecationWarning) r1, r2 = np.frexp(d, out=o1, subok=subok) assert_(r1 is o1) assert_(w[0].category is DeprecationWarning) assert_raises(ValueError, np.add, a, 2, o, o, subok=subok) assert_raises(ValueError, np.add, a, 2, o, out=o, subok=subok) assert_raises(ValueError, np.add, a, 2, None, out=o, subok=subok) assert_raises(ValueError, np.add, a, 2, out=(o, o), subok=subok) assert_raises(ValueError, np.add, a, 2, out=(), subok=subok) assert_raises(TypeError, np.add, a, 2, [], subok=subok) assert_raises(TypeError, np.add, a, 2, out=[], subok=subok) assert_raises(TypeError, np.add, a, 2, out=([],), subok=subok) o.flags.writeable = False assert_raises(ValueError, np.add, a, 2, o, subok=subok) assert_raises(ValueError, np.add, a, 2, out=o, subok=subok) assert_raises(ValueError, np.add, a, 2, out=(o,), subok=subok)
def test_half_ufuncs(self): """Test the various ufuncs""" a = np.array([0, 1, 2, 4, 2], dtype=float16) b = np.array([-2, 5, 1, 4, 3], dtype=float16) c = np.array([0, -1, -np.inf, np.nan, 6], dtype=float16) assert_equal(np.add(a, b), [-2, 6, 3, 8, 5]) assert_equal(np.subtract(a, b), [2, -4, 1, 0, -1]) assert_equal(np.multiply(a, b), [0, 5, 2, 16, 6]) assert_equal(np.divide(a, b), [0, 0.199951171875, 2, 1, 0.66650390625]) assert_equal(np.equal(a, b), [False, False, False, True, False]) assert_equal(np.not_equal(a, b), [True, True, True, False, True]) assert_equal(np.less(a, b), [False, True, False, False, True]) assert_equal(np.less_equal(a, b), [False, True, False, True, True]) assert_equal(np.greater(a, b), [True, False, True, False, False]) assert_equal(np.greater_equal(a, b), [True, False, True, True, False]) assert_equal(np.logical_and(a, b), [False, True, True, True, True]) assert_equal(np.logical_or(a, b), [True, True, True, True, True]) assert_equal(np.logical_xor(a, b), [True, False, False, False, False]) assert_equal(np.logical_not(a), [True, False, False, False, False]) assert_equal(np.isnan(c), [False, False, False, True, False]) assert_equal(np.isinf(c), [False, False, True, False, False]) assert_equal(np.isfinite(c), [True, True, False, False, True]) assert_equal(np.signbit(b), [True, False, False, False, False]) assert_equal(np.copysign(b, a), [2, 5, 1, 4, 3]) assert_equal(np.maximum(a, b), [0, 5, 2, 4, 3]) x = np.maximum(b, c) assert_(np.isnan(x[3])) x[3] = 0 assert_equal(x, [0, 5, 1, 0, 6]) assert_equal(np.minimum(a, b), [-2, 1, 1, 4, 2]) x = np.minimum(b, c) assert_(np.isnan(x[3])) x[3] = 0 assert_equal(x, [-2, -1, -np.inf, 0, 3]) assert_equal(np.fmax(a, b), [0, 5, 2, 4, 3]) assert_equal(np.fmax(b, c), [0, 5, 1, 4, 6]) assert_equal(np.fmin(a, b), [-2, 1, 1, 4, 2]) assert_equal(np.fmin(b, c), [-2, -1, -np.inf, 4, 3]) assert_equal(np.floor_divide(a, b), [0, 0, 2, 1, 0]) assert_equal(np.remainder(a, b), [0, 1, 0, 0, 2]) assert_equal(np.square(b), [4, 25, 1, 16, 9]) assert_equal(np.reciprocal(b), [-0.5, 0.199951171875, 1, 0.25, 0.333251953125]) assert_equal(np.ones_like(b), [1, 1, 1, 1, 1]) assert_equal(np.conjugate(b), b) assert_equal(np.absolute(b), [2, 5, 1, 4, 3]) assert_equal(np.negative(b), [2, -5, -1, -4, -3]) assert_equal(np.sign(b), [-1, 1, 1, 1, 1]) assert_equal(np.modf(b), ([0, 0, 0, 0, 0], b)) assert_equal(np.frexp(b), ([-0.5, 0.625, 0.5, 0.5, 0.75], [2, 3, 1, 3, 2])) assert_equal(np.ldexp(b, [0, 1, 2, 4, 2]), [-2, 10, 4, 64, 12])