本文整理汇总了Python中sympy.functions.sign函数的典型用法代码示例。如果您正苦于以下问题:Python sign函数的具体用法?Python sign怎么用?Python sign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sign函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ccode_sign
def test_ccode_sign():
expr1, ref1 = sign(x) * y, 'y*(((x) > 0) - ((x) < 0))'
expr2, ref2 = sign(cos(x)), '(((cos(x)) > 0) - ((cos(x)) < 0))'
expr3, ref3 = sign(2 * x + x**2) * x + x**2, 'pow(x, 2) + x*(((pow(x, 2) + 2*x) > 0) - ((pow(x, 2) + 2*x) < 0))'
assert ccode(expr1) == ref1
assert ccode(expr1, 'z') == 'z = %s;' % ref1
assert ccode(expr2) == ref2
assert ccode(expr3) == ref3
示例2: test_ccode_sign
def test_ccode_sign():
expr = sign(x) * y
assert ccode(expr) == 'y*(((x) > 0) - ((x) < 0))'
assert ccode(expr, 'z') == 'z = y*(((x) > 0) - ((x) < 0));'
assert ccode(sign(2 * x + x**2) * x + x**2) == \
'pow(x, 2) + x*(((pow(x, 2) + 2*x) > 0) - ((pow(x, 2) + 2*x) < 0))'
expr = sign(cos(x))
assert ccode(expr) == '(((cos(x)) > 0) - ((cos(x)) < 0))'
示例3: test_sign
def test_sign():
expr = sign(x) * y
assert rust_code(expr) == "y*x.signum()"
assert rust_code(expr, assign_to='r') == "r = y*x.signum();"
expr = sign(x + y) + 42
assert rust_code(expr) == "(x + y).signum() + 42"
assert rust_code(expr, assign_to='r') == "r = (x + y).signum() + 42;"
expr = sign(cos(x))
assert rust_code(expr) == "x.cos().signum()"
示例4: test_rcode_sgn
def test_rcode_sgn():
expr = sign(x) * y
assert rcode(expr) == 'y*sign(x)'
p = rcode(expr, 'z')
assert p == 'z = y*sign(x);'
p = rcode(sign(2 * x + x**2) * x + x**2)
assert p == "x^2 + x*sign(x^2 + 2*x)"
expr = sign(cos(x))
p = rcode(expr)
assert p == 'sign(cos(x))'
示例5: test_bounded
def test_bounded():
x, y = symbols('xy')
assert ask(x, Q.bounded) == False
assert ask(x, Q.bounded, Assume(x, Q.bounded)) == True
assert ask(x, Q.bounded, Assume(y, Q.bounded)) == False
assert ask(x, Q.bounded, Assume(x, Q.complex)) == False
assert ask(x+1, Q.bounded) == False
assert ask(x+1, Q.bounded, Assume(x, Q.bounded)) == True
assert ask(x+y, Q.bounded) == None
assert ask(x+y, Q.bounded, Assume(x, Q.bounded)) == False
assert ask(x+1, Q.bounded, Assume(x, Q.bounded) & \
Assume(y, Q.bounded)) == True
assert ask(2*x, Q.bounded) == False
assert ask(2*x, Q.bounded, Assume(x, Q.bounded)) == True
assert ask(x*y, Q.bounded) == None
assert ask(x*y, Q.bounded, Assume(x, Q.bounded)) == False
assert ask(x*y, Q.bounded, Assume(x, Q.bounded) & \
Assume(y, Q.bounded)) == True
assert ask(x**2, Q.bounded) == False
assert ask(2**x, Q.bounded) == False
assert ask(2**x, Q.bounded, Assume(x, Q.bounded)) == True
assert ask(x**x, Q.bounded) == False
assert ask(Rational(1,2) ** x, Q.bounded) == True
assert ask(x ** Rational(1,2), Q.bounded) == False
# sign function
assert ask(sign(x), Q.bounded) == True
assert ask(sign(x), Q.bounded, Assume(x, Q.bounded, False)) == True
# exponential functions
assert ask(log(x), Q.bounded) == False
assert ask(log(x), Q.bounded, Assume(x, Q.bounded)) == True
assert ask(exp(x), Q.bounded) == False
assert ask(exp(x), Q.bounded, Assume(x, Q.bounded)) == True
assert ask(exp(2), Q.bounded) == True
# trigonometric functions
assert ask(sin(x), Q.bounded) == True
assert ask(sin(x), Q.bounded, Assume(x, Q.bounded, False)) == True
assert ask(cos(x), Q.bounded) == True
assert ask(cos(x), Q.bounded, Assume(x, Q.bounded, False)) == True
assert ask(2*sin(x), Q.bounded) == True
assert ask(sin(x)**2, Q.bounded) == True
assert ask(cos(x)**2, Q.bounded) == True
assert ask(cos(x) + sin(x), Q.bounded) == True
示例6: test_bounded
def test_bounded():
x, y = symbols('x,y')
assert ask(Q.bounded(x)) == False
assert ask(Q.bounded(x), Q.bounded(x)) == True
assert ask(Q.bounded(x), Q.bounded(y)) == False
assert ask(Q.bounded(x), Q.complex(x)) == False
assert ask(Q.bounded(x+1)) == False
assert ask(Q.bounded(x+1), Q.bounded(x)) == True
assert ask(Q.bounded(x+y)) == None
assert ask(Q.bounded(x+y), Q.bounded(x)) == False
assert ask(Q.bounded(x+1), Q.bounded(x) & Q.bounded(y)) == True
assert ask(Q.bounded(2*x)) == False
assert ask(Q.bounded(2*x), Q.bounded(x)) == True
assert ask(Q.bounded(x*y)) == None
assert ask(Q.bounded(x*y), Q.bounded(x)) == False
assert ask(Q.bounded(x*y), Q.bounded(x) & Q.bounded(y)) == True
assert ask(Q.bounded(x**2)) == False
assert ask(Q.bounded(2**x)) == False
assert ask(Q.bounded(2**x), Q.bounded(x)) == True
assert ask(Q.bounded(x**x)) == False
assert ask(Q.bounded(Rational(1,2) ** x)) == None
assert ask(Q.bounded(Rational(1,2) ** x), Q.positive(x)) == True
assert ask(Q.bounded(Rational(1,2) ** x), Q.negative(x)) == False
assert ask(Q.bounded(sqrt(x))) == False
# sign function
assert ask(Q.bounded(sign(x))) == True
assert ask(Q.bounded(sign(x)), ~Q.bounded(x)) == True
# exponential functions
assert ask(Q.bounded(log(x))) == False
assert ask(Q.bounded(log(x)), Q.bounded(x)) == True
assert ask(Q.bounded(exp(x))) == False
assert ask(Q.bounded(exp(x)), Q.bounded(x)) == True
assert ask(Q.bounded(exp(2))) == True
# trigonometric functions
assert ask(Q.bounded(sin(x))) == True
assert ask(Q.bounded(sin(x)), ~Q.bounded(x)) == True
assert ask(Q.bounded(cos(x))) == True
assert ask(Q.bounded(cos(x)), ~Q.bounded(x)) == True
assert ask(Q.bounded(2*sin(x))) == True
assert ask(Q.bounded(sin(x)**2)) == True
assert ask(Q.bounded(cos(x)**2)) == True
assert ask(Q.bounded(cos(x) + sin(x))) == True
示例7: calc_limit
def calc_limit(a, b):
"""replace x with a, using subs if possible, otherwise limit
where sign of b is considered"""
wok = inverse_mapping.subs(x, a)
if wok is S.NaN or wok.is_bounded is False and a.is_bounded:
return limit(sign(b)*inverse_mapping, x, a)
return wok
示例8: refine_Pow
def refine_Pow(expr, assumptions):
"""
Handler for instances of Pow.
>>> from sympy import Symbol, Assume, Q
>>> from sympy.assumptions.refine import refine_Pow
>>> from sympy.abc import x
>>> refine_Pow((-1)**x, Assume(x, Q.real))
>>> refine_Pow((-1)**x, Assume(x, Q.even))
1
>>> refine_Pow((-1)**x, Assume(x, Q.odd))
-1
"""
from sympy.core import Pow, Rational
from sympy.functions import sign
if ask(expr.base, Q.real, assumptions):
if expr.base.is_number:
if ask(expr.exp, Q.even, assumptions):
return abs(expr.base) ** expr.exp
if ask(expr.exp, Q.odd, assumptions):
return sign(expr.base) * abs(expr.base) ** expr.exp
if isinstance(expr.exp, Rational):
if type(expr.base) is Pow:
return abs(expr.base.base) ** (expr.base.exp * expr.exp)
示例9: calc_limit
def calc_limit(a, b):
"""replace x with a, using subs if possible, otherwise limit
where sign of b is considered"""
wok = inverse_mapping.subs(x, a)
if not wok is S.NaN:
return wok
return limit(sign(b)*inverse_mapping, x, a)
示例10: denester
def denester(nested):
"""
Denests a list of expressions that contain nested square roots.
This method should not be called directly - use 'sqrtdenest' instead.
This algorithm is based on <http://www.almaden.ibm.com/cs/people/fagin/symb85.pdf>.
It is assumed that all of the elements of 'nested' share the same
bottom-level radicand. (This is stated in the paper, on page 177, in
the paragraph immediately preceding the algorithm.)
When evaluating all of the arguments in parallel, the bottom-level
radicand only needs to be denested once. This means that calling
denester with x arguments results in a recursive invocation with x+1
arguments; hence denester has polynomial complexity.
However, if the arguments were evaluated separately, each call would
result in two recursive invocations, and the algorithm would have
exponential complexity.
This is discussed in the paper in the middle paragraph of page 179.
"""
if all((n**2).is_Number for n in nested): #If none of the arguments are nested
for f in subsets(len(nested)): #Test subset 'f' of nested
p = prod(nested[i]**2 for i in range(len(f)) if f[i]).expand()
if 1 in f and f.count(1) > 1 and f[-1]:
p = -p
if sqrt(p).is_Number:
return sqrt(p), f #If we got a perfect square, return its square root.
return nested[-1], [0]*len(nested) #Otherwise, return the radicand from the previous invocation.
else:
a, b, r, R = Wild('a'), Wild('b'), Wild('r'), None
values = [expr.match(sqrt(a + b * sqrt(r))) for expr in nested]
if any(v is None for v in values): # this pattern is not recognized
return nested[-1], [0]*len(nested) #Otherwise, return the radicand from the previous invocation.
for v in values:
if r in v: #Since if b=0, r is not defined
if R is not None:
assert R == v[r] #All the 'r's should be the same.
else:
R = v[r]
if R is None:
return nested[-1], [0]*len(nested) #return the radicand from the previous invocation.
d, f = denester([sqrt((v[a]**2).expand()-(R*v[b]**2).expand()) for v in values] + [sqrt(R)])
if all(fi == 0 for fi in f):
v = values[-1]
return sqrt(v[a] + v[b]*d), f
else:
v = prod(nested[i]**2 for i in range(len(nested)) if f[i]).expand().match(a+b*sqrt(r))
if 1 in f and f.index(1) < len(nested) - 1 and f[len(nested)-1]:
v[a] = -1 * v[a]
v[b] = -1 * v[b]
if not f[len(nested)]: #Solution denests with square roots
vad = (v[a] + d).expand()
if not vad:
return nested[-1], [0]*len(nested) #Otherwise, return the radicand from the previous invocation.
return (sqrt(vad/2) + sign(v[b])*sqrt((v[b]**2*R/(2*vad)).expand())).expand(), f
else: #Solution requires a fourth root
FR, s = (R.expand()**Rational(1,4)), sqrt((v[b]*R).expand()+d)
return (s/(sqrt(2)*FR) + v[a]*FR/(sqrt(2)*s)).expand(), f
示例11: _calc_limit_1
def _calc_limit_1(F, a, b):
"""
replace d with a, using subs if possible, otherwise limit
where sign of b is considered
"""
wok = F.subs(d, a)
if wok is S.NaN or wok.is_bounded is False and a.is_bounded:
return limit(sign(b)*F, d, a)
return wok
示例12: p_parameter
def p_parameter(self):
"""P is a parameter of parabola.
Returns
=======
p : number or symbolic expression
Notes
=====
The absolute value of p is the focal length. The sign on p tells
which way the parabola faces. Vertical parabolas that open up
and horizontal that open right, give a positive value for p.
Vertical parabolas that open down and horizontal that open left,
give a negative value for p.
See Also
========
http://www.sparknotes.com/math/precalc/conicsections/section2.rhtml
Examples
========
>>> from sympy import Parabola, Point, Line
>>> p1 = Parabola(Point(0, 0), Line(Point(5, 8), Point(7, 8)))
>>> p1.p_parameter
-4
"""
if self.axis_of_symmetry.slope == 0:
x = self.directrix.coefficients[2]
p = sign(self.focus.args[0] + x)
else:
y = self.directrix.coefficients[2]
p = sign(self.focus.args[1] + y)
return p * self.focal_length
示例13: test_Function
def test_Function():
assert mcode(sin(x) ** cos(x)) == "sin(x).^cos(x)"
assert mcode(sign(x)) == "sign(x)"
assert mcode(exp(x)) == "exp(x)"
assert mcode(log(x)) == "log(x)"
assert mcode(factorial(x)) == "factorial(x)"
assert mcode(floor(x)) == "floor(x)"
assert mcode(atan2(y, x)) == "atan2(y, x)"
assert mcode(beta(x, y)) == 'beta(x, y)'
assert mcode(polylog(x, y)) == 'polylog(x, y)'
assert mcode(harmonic(x)) == 'harmonic(x)'
assert mcode(bernoulli(x)) == "bernoulli(x)"
assert mcode(bernoulli(x, y)) == "bernoulli(x, y)"
示例14: _sqrt_numeric_denest
def _sqrt_numeric_denest(a, b, r, d2):
"""Helper that denest expr = a + b*sqrt(r), with d2 = a**2 - b**2*r > 0
or returns None if not denested.
"""
from sympy.simplify.simplify import radsimp
depthr = sqrt_depth(r)
d = sqrt(d2)
vad = a + d
# sqrt_depth(res) <= sqrt_depth(vad) + 1
# sqrt_depth(expr) = depthr + 2
# there is denesting if sqrt_depth(vad)+1 < depthr + 2
# if vad**2 is Number there is a fourth root
if sqrt_depth(vad) < depthr + 1 or (vad**2).is_Rational:
vad1 = radsimp(1/vad)
return (sqrt(vad/2) + sign(b)*sqrt((b**2*r*vad1/2).expand())).expand()
示例15: test_PythonCodePrinter
def test_PythonCodePrinter():
prntr = PythonCodePrinter()
assert not prntr.module_imports
assert prntr.doprint(x**y) == 'x**y'
assert prntr.doprint(Mod(x, 2)) == 'x % 2'
assert prntr.doprint(And(x, y)) == 'x and y'
assert prntr.doprint(Or(x, y)) == 'x or y'
assert not prntr.module_imports
assert prntr.doprint(pi) == 'math.pi'
assert prntr.module_imports == {'math': {'pi'}}
assert prntr.doprint(acos(x)) == 'math.acos(x)'
assert prntr.doprint(Assignment(x, 2)) == 'x = 2'
assert prntr.doprint(Piecewise((1, Eq(x, 0)),
(2, x>6))) == '((1) if (x == 0) else (2) if (x > 6) else None)'
assert prntr.doprint(Piecewise((2, Le(x, 0)),
(3, Gt(x, 0)), evaluate=False)) == '((2) if (x <= 0) else'\
' (3) if (x > 0) else None)'
assert prntr.doprint(sign(x)) == '(0.0 if x == 0 else math.copysign(1, x))'