本文整理汇总了Python中numpy.ldexp方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.ldexp方法的具体用法?Python numpy.ldexp怎么用?Python numpy.ldexp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.ldexp方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clear_fuss
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def clear_fuss(ar, fuss_binary_bits=7):
"""Clears trailing `fuss_binary_bits` of mantissa of a floating number"""
x = np.asanyarray(ar)
if np.iscomplexobj(x):
return clear_fuss(x.real) + 1j * clear_fuss(x.imag)
significant_binary_bits = np.finfo(x.dtype).nmant
x_mant, x_exp = np.frexp(x)
f = 2.0**(significant_binary_bits - fuss_binary_bits)
x_mant *= f
np.rint(x_mant, out=x_mant)
x_mant /= f
return np.ldexp(x_mant, x_exp)
# XXX: This function should be available through numpy.testing
示例2: _roots
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def _roots(p):
"""Modified version of NumPy's roots function.
NumPy's roots uses the companion matrix method, which divides by
p[0]. This can causes overflows/underflows. Instead form a
modified companion matrix that is scaled by 2^c * p[0], where the
exponent c is chosen to balance the magnitudes of the
coefficients. Since scaling the matrix just scales the
eigenvalues, we can remove the scaling at the end.
Scaling by a power of 2 is chosen to avoid rounding errors.
"""
_, e = np.frexp(p)
# Balance the most extreme exponents e_max and e_min by solving
# the equation
#
# |c + e_max| = |c + e_min|.
#
# Round the exponent to an integer to avoid rounding errors.
c = int(-0.5 * (np.max(e) + np.min(e)))
p = np.ldexp(p, c)
A = np.diag(np.full(p.size - 2, p[0]), k=-1)
A[0,:] = -p[1:]
eigenvalues = np.linalg.eigvals(A)
return eigenvalues / p[0]
示例3: helper_ldexp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def helper_ldexp(f, unit1, unit2):
if unit2 is not None:
raise TypeError("Cannot use ldexp with a quantity "
"as second argument.")
else:
return [None, None], _d(unit1)
示例4: test_ldexp_scalar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def test_ldexp_scalar(self):
assert np.ldexp(4. * u.m, 2) == 16. * u.m
示例5: test_ldexp_array
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def test_ldexp_array(self):
assert np.all(np.ldexp(np.array([1., 2., 3.]) * u.m, [3, 2, 1])
== np.array([8., 8., 6.]) * u.m)
示例6: test_ldexp_invalid
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def test_ldexp_invalid(self):
with pytest.raises(TypeError):
np.ldexp(3. * u.m, 4.)
with pytest.raises(TypeError):
np.ldexp(3., u.Quantity(4, u.m, dtype=int))
示例7: _hdr_read
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def _hdr_read(filename, use_imageio=False):
"""Read hdr file.
.. TODO:
* Support axis other than -Y +X
"""
if use_imageio:
return imageio.imread(filename, **kwargs)
with open(filename, "rb") as f:
MAGIC = f.readline().strip()
assert MAGIC == b'#?RADIANCE', "Wrong header found in {}".format(filename)
comments = b""
while comments[:6] != b"FORMAT":
comments = f.readline().strip()
assert comments[:3] != b"-Y ", "Could not find data format"
assert comments == b'FORMAT=32-bit_rle_rgbe', "Format not supported"
while comments[:3] != b"-Y ":
comments = f.readline().strip()
_, height, _, width = comments.decode("ascii").split(" ")
height, width = int(height), int(width)
rgbe = np.fromfile(f, dtype=np.uint8).reshape((height, width, 4))
rgb = np.empty((height, width, 3), dtype=np.float)
rgb[...,0] = np.ldexp(rgbe[...,0], rgbe[...,3].astype('int') - 128)
rgb[...,1] = np.ldexp(rgbe[...,1], rgbe[...,3].astype('int') - 128)
rgb[...,2] = np.ldexp(rgbe[...,2], rgbe[...,3].astype('int') - 128)
# TODO: This will rescale all the values to be in [0, 1]. Find a way to retrieve the original values.
rgb /= rgb.max()
return rgb
示例8: test_ldexp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def test_ldexp():
m = te.var("m",)
A = te.placeholder((m,), name='A')
B = te.placeholder((m,), name='B', dtype="int32")
C = te.compute((m,), lambda *i: tvm.tir.ldexp(A(*i), B(*i)), name='C')
s = te.create_schedule(C.op)
f = tvm.build(s, [A, B, C], "llvm")
ctx = tvm.cpu(0)
n = 10
a = tvm.nd.array(np.random.uniform(0, 1, size=n).astype(A.dtype), ctx)
b = tvm.nd.array(np.random.randint(0, 5, size=n).astype(B.dtype), ctx)
c = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), ctx)
f(a, b, c)
tvm.testing.assert_allclose(
c.asnumpy(), np.ldexp(a.asnumpy(), b.asnumpy()), atol=1e-5, rtol=1e-5)
示例9: ldexp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
def ldexp(x1, x2, out=None, where=None, **kwargs):
"""
Returns x1 * 2**x2, element-wise.
The mantissas `x1` and twos exponents `x2` are used to construct
floating point numbers ``x1 * 2**x2``.
Parameters
----------
x1 : array_like
Tensor of multipliers.
x2 : array_like, int
Tensor of twos exponents.
out : Tensor, None, or tuple of Tensor and None, optional
A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or `None`,
a freshly-allocated tensor is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values
of False indicate to leave the value in the output alone.
**kwargs
Returns
-------
y : Tensor or scalar
The result of ``x1 * 2**x2``.
See Also
--------
frexp : Return (y1, y2) from ``x = y1 * 2**y2``, inverse to `ldexp`.
Notes
-----
Complex dtypes are not supported, they will raise a TypeError.
`ldexp` is useful as the inverse of `frexp`, if used by itself it is
more clear to simply use the expression ``x1 * 2**x2``.
Examples
--------
>>> import mars.tensor as mt
>>> mt.ldexp(5, mt.arange(4)).execute()
array([ 5., 10., 20., 40.], dtype=float32)
>>> x = mt.arange(6)
>>> mt.ldexp(*mt.frexp(x)).execute()
array([ 0., 1., 2., 3., 4., 5.])
"""
x2_dtype = astensor(x2).dtype
casting = kwargs.get('casting', 'safe')
if not np.can_cast(x2_dtype, np.int64, casting=casting):
raise TypeError("ufunc 'ldexp' not supported for the input types, "
"and the inputs could not be safely coerced to any supported types "
"according to the casting rule ''{0}''".format(casting))
op = TensorLdexp(**kwargs)
return op(x1, x2, out=out, where=where)
示例10: test_half_ufuncs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
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])
示例11: test_half_ufuncs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ldexp [as 别名]
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.divmod(a, b), ([0, 0, 2, 1, 0], [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.positive(b), b)
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])