本文整理汇总了Python中sympy.functions.elementary.complexes.sign函数的典型用法代码示例。如果您正苦于以下问题:Python sign函数的具体用法?Python sign怎么用?Python sign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sign函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: eval
def eval(cls, n, m, z=None):
if z is not None:
n, z, m = n, m, z
k = 2 * z / pi
if n == S.Zero:
return elliptic_f(z, m)
elif n == S.One:
return elliptic_f(z, m) + (sqrt(1 - m * sin(z) ** 2) * tan(z) - elliptic_e(z, m)) / (1 - m)
elif k.is_integer:
return k * elliptic_pi(n, m)
elif m == S.Zero:
return atanh(sqrt(n - 1) * tan(z)) / sqrt(n - 1)
elif n == m:
return elliptic_f(z, n) - elliptic_pi(1, z, n) + tan(z) / sqrt(1 - n * sin(z) ** 2)
elif n in (S.Infinity, S.NegativeInfinity):
return S.Zero
elif m in (S.Infinity, S.NegativeInfinity):
return S.Zero
elif z.could_extract_minus_sign():
return -elliptic_pi(n, -z, m)
else:
if n == S.Zero:
return elliptic_k(m)
elif n == S.One:
return S.ComplexInfinity
elif m == S.Zero:
return pi / (2 * sqrt(1 - n))
elif m == S.One:
return -S.Infinity / sign(n - 1)
elif n == m:
return elliptic_e(n) / (1 - n)
elif n in (S.Infinity, S.NegativeInfinity):
return S.Zero
elif m in (S.Infinity, S.NegativeInfinity):
return S.Zero
示例2: _updated_range
def _updated_range(r, first):
st = sign(r.step)*step
if r.start.is_finite:
rv = Range(first, r.stop, st)
else:
rv = Range(r.start, first + st, st)
return rv
示例3: _eval_rewrite_as_sign
def _eval_rewrite_as_sign(self, arg):
"""Represents the Heaviside function in the form of sign function.
Examples
========
>>> from sympy import Heaviside, Symbol, sign
>>> x = Symbol('x', real=True)
>>> Heaviside(x).rewrite(sign)
sign(x)/2 + 1/2
>>> Heaviside(x - 2).rewrite(sign)
sign(x - 2)/2 + 1/2
>>> Heaviside(x**2 - 2*x + 1).rewrite(sign)
sign(x**2 - 2*x + 1)/2 + 1/2
>>> y = Symbol('y')
>>> Heaviside(y).rewrite(sign)
Heaviside(y)
>>> Heaviside(y**2 - 2*y + 1).rewrite(sign)
Heaviside(y**2 - 2*y + 1)
See Also
========
sign
"""
if arg.is_real:
return (sign(arg)+1)/2
示例4: real_root
def real_root(arg, n=None, evaluate=None):
"""Return the real nth-root of arg if possible. If n is omitted then
all instances of (-n)**(1/odd) will be changed to -n**(1/odd); this
will only create a real root of a principal root -- the presence of
other factors may cause the result to not be real.
The parameter evaluate determines if the expression should be evaluated.
If None, its value is taken from global_evaluate.
Examples
========
>>> from sympy import root, real_root, Rational
>>> from sympy.abc import x, n
>>> real_root(-8, 3)
-2
>>> root(-8, 3)
2*(-1)**(1/3)
>>> real_root(_)
-2
If one creates a non-principal root and applies real_root, the
result will not be real (so use with caution):
>>> root(-8, 3, 2)
-2*(-1)**(2/3)
>>> real_root(_)
-2*(-1)**(2/3)
See Also
========
sympy.polys.rootoftools.rootof
sympy.core.power.integer_nthroot
root, sqrt
"""
from sympy.functions.elementary.complexes import Abs, im, sign
from sympy.functions.elementary.piecewise import Piecewise
if n is not None:
return Piecewise(
(root(arg, n, evaluate=evaluate), Or(Eq(n, S.One), Eq(n, S.NegativeOne))),
(Mul(sign(arg), root(Abs(arg), n, evaluate=evaluate), evaluate=evaluate),
And(Eq(im(arg), S.Zero), Eq(Mod(n, 2), S.One))),
(root(arg, n, evaluate=evaluate), True))
rv = sympify(arg)
n1pow = Transform(lambda x: -(-x.base)**x.exp,
lambda x:
x.is_Pow and
x.base.is_negative and
x.exp.is_Rational and
x.exp.p == 1 and x.exp.q % 2)
return rv.xreplace(n1pow)
示例5: _first_finite_point
def _first_finite_point(r1, c):
if c == r1.start:
return c
# st is the signed step we need to take to
# get from c to r1.start
st = sign(r1.start - c)*step
# use Range to calculate the first point:
# we want to get as close as possible to
# r1.start; the Range will not be null since
# it will at least contain c
s1 = Range(c, r1.start + st, st)[-1]
if s1 == r1.start:
pass
else:
# if we didn't hit r1.start then, if the
# sign of st didn't match the sign of r1.step
# we are off by one and s1 is not in r1
if sign(r1.step) != sign(st):
s1 -= st
if s1 not in r1:
return
return s1
示例6: _eval_rewrite_as_sign
def _eval_rewrite_as_sign(self, arg, H0=None):
"""Represents the Heaviside function in the form of sign function.
The value of the second argument of Heaviside must specify Heaviside(0)
= 1/2 for rewritting as sign to be strictly equivalent. For easier
usage, we also allow this rewriting when Heaviside(0) is undefined.
Examples
========
>>> from sympy import Heaviside, Symbol, sign
>>> x = Symbol('x', real=True)
>>> Heaviside(x).rewrite(sign)
sign(x)/2 + 1/2
>>> Heaviside(x, 0).rewrite(sign)
Heaviside(x, 0)
>>> Heaviside(x - 2).rewrite(sign)
sign(x - 2)/2 + 1/2
>>> Heaviside(x**2 - 2*x + 1).rewrite(sign)
sign(x**2 - 2*x + 1)/2 + 1/2
>>> y = Symbol('y')
>>> Heaviside(y).rewrite(sign)
Heaviside(y)
>>> Heaviside(y**2 - 2*y + 1).rewrite(sign)
Heaviside(y**2 - 2*y + 1)
See Also
========
sign
"""
if arg.is_real:
if H0 is None or H0 == S.Half:
return (sign(arg)+1)/2
示例7: _eval_power
def _eval_power(self, e):
if e.is_Rational and self.is_number:
from sympy.core.evalf import pure_complex
from sympy.core.mul import _unevaluated_Mul
from sympy.core.exprtools import factor_terms
from sympy.core.function import expand_multinomial
from sympy.functions.elementary.complexes import sign
from sympy.functions.elementary.miscellaneous import sqrt
ri = pure_complex(self)
if ri:
r, i = ri
if e.q == 2:
D = sqrt(r**2 + i**2)
if D.is_Rational:
# (r, i, D) is a Pythagorean triple
root = sqrt(factor_terms((D - r)/2))**e.p
return root*expand_multinomial((
# principle value
(D + r)/abs(i) + sign(i)*S.ImaginaryUnit)**e.p)
elif e == -1:
return _unevaluated_Mul(
r - i*S.ImaginaryUnit,
1/(r**2 + i**2))
示例8: _eval_rewrite_as_sign
def _eval_rewrite_as_sign(self, arg):
if arg.is_real:
return (sign(arg)+1)/2
示例9: test_sympy__functions__elementary__complexes__sign
def test_sympy__functions__elementary__complexes__sign():
from sympy.functions.elementary.complexes import sign
assert _test_args(sign(x))
示例10: _intersect
def _intersect(self, other):
from sympy.functions.elementary.integers import ceiling, floor
from sympy.functions.elementary.complexes import sign
if other is S.Naturals:
return self._intersect(Interval(1, S.Infinity))
if other is S.Integers:
return self
if other.is_Interval:
if not all(i.is_number for i in other.args[:2]):
return
# In case of null Range, return an EmptySet.
if self.size == 0:
return S.EmptySet
# trim down to self's size, and represent
# as a Range with step 1.
start = ceiling(max(other.inf, self.inf))
if start not in other:
start += 1
end = floor(min(other.sup, self.sup))
if end not in other:
end -= 1
return self.intersect(Range(start, end + 1))
if isinstance(other, Range):
from sympy.solvers.diophantine import diop_linear
from sympy.core.numbers import ilcm
# non-overlap quick exits
if not other:
return S.EmptySet
if not self:
return S.EmptySet
if other.sup < self.inf:
return S.EmptySet
if other.inf > self.sup:
return S.EmptySet
# work with finite end at the start
r1 = self
if r1.start.is_infinite:
r1 = r1.reversed
r2 = other
if r2.start.is_infinite:
r2 = r2.reversed
# this equation represents the values of the Range;
# it's a linear equation
eq = lambda r, i: r.start + i*r.step
# we want to know when the two equations might
# have integer solutions so we use the diophantine
# solver
a, b = diop_linear(eq(r1, Dummy()) - eq(r2, Dummy()))
# check for no solution
no_solution = a is None and b is None
if no_solution:
return S.EmptySet
# there is a solution
# -------------------
# find the coincident point, c
a0 = a.as_coeff_Add()[0]
c = eq(r1, a0)
# find the first point, if possible, in each range
# since c may not be that point
def _first_finite_point(r1, c):
if c == r1.start:
return c
# st is the signed step we need to take to
# get from c to r1.start
st = sign(r1.start - c)*step
# use Range to calculate the first point:
# we want to get as close as possible to
# r1.start; the Range will not be null since
# it will at least contain c
s1 = Range(c, r1.start + st, st)[-1]
if s1 == r1.start:
pass
else:
# if we didn't hit r1.start then, if the
# sign of st didn't match the sign of r1.step
# we are off by one and s1 is not in r1
if sign(r1.step) != sign(st):
s1 -= st
if s1 not in r1:
return
return s1
# calculate the step size of the new Range
step = abs(ilcm(r1.step, r2.step))
s1 = _first_finite_point(r1, c)
if s1 is None:
#.........这里部分代码省略.........