本文整理汇总了Python中numpy.ldexp函数的典型用法代码示例。如果您正苦于以下问题:Python ldexp函数的具体用法?Python ldexp怎么用?Python ldexp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ldexp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ldexp_invalid
def test_ldexp_invalid(self):
with pytest.raises(TypeError) as exc:
np.ldexp(3. * u.m, 4.)
# built-in TypeError, so can't check content of exception
with pytest.raises(TypeError) as exc:
np.ldexp(3., 4 * u.m)
assert "Cannot use ldexp" in exc.value.args[0]
示例2: _expm_product_helper
def _expm_product_helper(A, mu, iteration_stash, t, B):
# Estimate expm(t*M).dot(B).
# A = M - mu*I
# mu = mean(trace(M))
# The iteration stash helps to compute numbers of iterations to use.
# t is a scaling factor.
# B is the input matrix for the linear operator.
# Compute some input-dependent constants.
tol = np.ldexp(1, -53)
n0 = B.shape[1]
m, s = iteration_stash.fragment_3_1(n0, t)
#print('1-norm:', A.one_norm(), 't:', t, 'mu:', mu, 'n0:', n0, 'm:', m, 's:', s)
# Get the lapack function for computing matrix norms.
lange, = get_lapack_funcs(('lange',), (B,))
F = B
eta = np.exp(t*mu / float(s))
for i in range(s):
c1 = lange('i', B)
for j in range(m):
coeff = t / float(s*(j+1))
B = coeff * A.dot(B)
c2 = lange('i', B)
F = F + B
if c1 + c2 <= tol * lange('i', F):
break
c1 = c2
F = eta * F
B = F
return F
示例3: __init__
def __init__(self,
Q_primary, primary_distn, primary_to_part,
rate_on, rate_off):
"""
"""
# Store the inputs.
self.Q_primary = Q_primary
self.primary_distn = primary_distn
self.primary_to_part = primary_to_part
self.rate_on = rate_on
self.rate_off = rate_off
# Precompute some summaries which can be computed quickly
# and do not use much memory.
# Summaries that are slow or complicated to compute or which may use
# too much memory are computed on demand
# through an explicit function call.
self.nprimary = len(primary_to_part)
self.nparts = len(set(primary_to_part.values()))
self.ncompound = int(np.ldexp(self.nprimary, self.nparts))
self.tolerance_distn = get_tolerance_distn(rate_off, rate_on)
# Mark some attributes as un-initialized.
# These attributes are related to the compound distribution,
# and may be initialized later using init_compound().
self.Q_compound = None
self.compound_distn = None
self.compound_to_primary = None
self.compound_to_tolerances = None
示例4: test_half_ufuncs
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])
示例5: nextfloat
def nextfloat(fin):
"""Return (approximately) next float value (for f>0)."""
d = 2**-52
split = N.frexp(fin)
while True:
fout = N.ldexp(split[0] + d, split[1])
if fin != fout:
return fout
d *= 2
示例6: decode
def decode(self, i):
i = np.array(i)
i = 255 - i
sgn = i > 127
e = np.array(np.floor(i / 16.0) - 8 * sgn + 1, dtype=np.uint8)
f = i % 16
data = np.ldexp(f, e + 2)
e = MuLaw.etab[e-1]
data = MuLaw.iscale * (1 - 2 * sgn) * (e + data)
return data
示例7: test_roundtrip
def test_roundtrip(self, ftype, frac_vals, exp_vals):
for frac, exp in zip(frac_vals, exp_vals):
f = np.ldexp(frac, exp, dtype=ftype)
n, d = f.as_integer_ratio()
try:
# workaround for gh-9968
nf = np.longdouble(str(n))
df = np.longdouble(str(d))
except (OverflowError, RuntimeWarning):
# the values may not fit in any float type
pytest.skip("longdouble too small on this platform")
assert_equal(nf / df, f, "{}/{}".format(n, d))
示例8: from_npfloat
def from_npfloat(x, prec=113, rnd=round_fast):
"""Create a raw mpf from a numpy float, rounding if necessary.
If prec >= 113, the result is guaranteed to represent exactly the
same number as the input. If prec is not specified, use prec=113."""
y = float(x)
if x == y: # ldexp overflows for float16
return from_float(y, prec, rnd)
import numpy as np
if np.isfinite(x):
m, e = np.frexp(x)
return from_man_exp(int(np.ldexp(m, 113)), int(e-113), prec, rnd)
if np.isposinf(x): return finf
if np.isneginf(x): return fninf
return fnan
示例9: tiny_perturbation
def tiny_perturbation(arr):
fr = numpy.frexp(arr)
perturb = numpy.random.random(fr[0].shape) * 1e-12
return numpy.ldexp(fr[0]+perturb, fr[1])
示例10: test_ldexp_invalid
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))
示例11: test_ldexp_array
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)
示例12: test_ldexp_scalar
def test_ldexp_scalar(self):
assert np.ldexp(4. * u.m, 2) == 16. * u.m
示例13: ldexp_usecase
def ldexp_usecase(x, y, result):
np.ldexp(x, y, result)
示例14: n_ldexp
def n_ldexp(a, b):
"""safe ldexp"""
return np.ldexp(a, intify(b))
示例15: math
def math():
global a, b, c, z, r
print '###########################'
print '#'
print '# 数学相关'
print '#'
print '###########################'
### 三角函数 ###
print '三角形斜边:\n', np.hypot(3*np.ones((3, 3)), 4*np.ones((3, 3)))
print '弧度转角度1:\n', np.degrees(np.arange(12.)*np.pi/6)
print '弧度转角度2:\n', np.rad2deg(np.pi/2)
print '角度转弧度1:\n', np.radians(np.arange(12.)*30)
print '角度转弧度2:\n', np.deg2rad(180)
print '最大距离截断:\n', np.unwrap(np.linspace(0, np.pi, num=5))
### 取整 ###
print '根据位数取整:\n', np.around([0.37, 1.64], decimals=1)
print '四舍五入取整:\n', np.rint([0.37, 1.64])
print '向零取整1:\n', np.fix([-1.5, -0.8, 0.37, 1.64])
print '向零取整2:\n', np.trunc([-1.5, -0.8, 0.37, 1.64])
print '向下取整:\n', np.floor([0.37, 1.64])
print '向上取整:\n', np.ceil([0.37, 1.64])
### 四则运算 ###
print '乘积:\n', np.prod([[1.,2.],[3.,4.]], axis=1)
print '求和:\n', np.sum([[0, 1], [0, 5]], axis=0)
print '累计乘积:\n', np.cumprod([[1, 2], [3, 4]])
print '累计求和:\n', np.cumsum([[1, 2], [3, 4]])
print '求差:\n', np.diff([[1, 2], [3, 4]])
print '一维求差:\n', np.ediff1d([[1, 2], [3, 4]])
print '梯度:\n', np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float))
print '向量积(叉积):\n', np.cross([1, 2, 3], [4, 5, 6])
print '加法:\n', np.add(np.arange(9).reshape(3, 3), np.arange(3))
print '倒数:\n', np.reciprocal([1, 2., 3.33])
print '负数:\n', np.negative([1.,-1.])
print '乘法:\n', np.multiply(2.0, 4.0)
print '除法:\n', np.divide(5.0, 4.0)
print '除法带小数:\n', np.true_divide(5.0, 4.0)
print '除法向下取整:\n', np.floor_divide(5.0, 4.0)
print '指数:\n', np.power(np.arange(6), 3)
print '减法:\n', np.subtract(1.0, 4.0)
print '求余1:\n', np.mod([-3, -2, -1, 1, 2, 3], 2)
print '求余2:\n', np.fmod([-3, -2, -1, 1, 2, 3], 2)
print '求余3:\n', np.remainder([-3, -2, -1, 1, 2, 3], 2)
print '小数+整数:\n', np.modf([0, 3.5])
### 正负符号 ###
print '负数返回True:\n', np.signbit(np.array([1, -2.3, 2.1]))
# 小于0返回-1, 等于0返回0, 大于0返回1
print '正负符号:\n', np.sign([-5., 0, 4.5])
# 将x2的符号应用到x1上。
print '符号拷贝:\n', np.copysign([-1, 0, 1], -1.1)
# 分解公式:y = x1 * 2**x2
x1, x2 = np.frexp(np.arange(9))
print '指数分解:\n', x1, x2
# 构建公式:y = x1 * 2**x2,是分解公式的逆向操作
print '指数构建:\n', np.ldexp(x1, x2)
### 复数 ###
print '复数转角度:\n', np.angle([1.0, 1.0j, 1+1j])
print '返回实数部分:\n', np.real(np.array([1+2j, 3+4j, 5+6j]))
print '返回实数部分(虚数接近0):\n', np.real_if_close([2.1 + 4e-14j], tol=1000)
print '返回虚数部分:\n', np.imag(np.array([1+2j, 3+4j, 5+6j]))
print '返回复数共轭矩阵:\n', np.conj(np.eye(2) + 1j * np.eye(2))
### 杂项 ###
print '卷积:\n', np.convolve([1, 2, 3], [0, 1, 0.5])
# 根据指定的区间,小于区间最小值的赋值为最小值,大于区间最大值的赋值为最大值
print '截断:\n', np.clip(np.arange(10), 3, 6)
print '平方根:\n', np.sqrt([4, -1, -3+4J])
print '平方:\n', np.square([-1j, 1])
print '绝对值(含复数):\n', np.absolute([-1, -2])
print '绝对值2:\n', np.fabs([-1, -2])
print '最大值(含nan):\n', np.maximum([2, 3, np.nan], [1, 5, 2])
print '最小值(含nan):\n', np.minimum([2, 3, np.nan], [1, 5, 2])
print '最大值(忽略nan):\n', np.fmax([2, 3, np.nan], [1, 5, 2])
print '最小值(忽略nan):\n', np.fmin([2, 3, np.nan], [1, 5, 2])
# 在由xp和fp组成的坐标点中插值
print '线性插值:\n', np.interp([0, 1, 1.5, 2.72, 3.14], xp=[1, 2, 3], fp=[3, 2, 0])