The objective is to implement a Piecewise expression that gives 0 when n is even, and 1 when n is odd. One way to do it is using the floor function like below:
from sympy import *
from sympy.abc import n
f = Lambda((n,), Piecewise((0, Eq(n, floor(n / S(2)))),
(1, Eq(n, floor(n / S(2))+1))))
print(f(0))
print(f(1))
print(f(2))
print(f(3))
However, this returns the wrong output:
0
1
1
Piecewise()
The correct output should be:
0
1
0
1
Another way to achieve the same is to use:
from sympy import *
from sympy.abc import n
f = Lambda((n,), Piecewise((0, Eq((-1)**n, 1)),
(1, Eq((-1)**n, -1))))
print(f(0))
print(f(1))
print(f(2))
print(f(3))
and this returns the correct output. Is there a way to achieve this using the floor function in the original code?
A better way would be to use Mod, like
Piecewise((0, Eq(Mod(n, 2), 0)), (1, Eq(Mod(n, 2), 1)))
However, since your function coincides exactly with the definition of Mod, you can just use it directly
Mod(n, 2)
or equivalently
n % 2
Related
The three (real) roots of the polynomial x^3 - 3x + 1 sum up to 0. But sympy does not seem to be able to simplify this sum of roots:
>>> from sympy import *
>>> from sympy.abc import x
>>> rr = real_roots(x**3 -3*x + 1)
>>> sum(rr)
CRootOf(x**3 - 3*x + 1, 0) + CRootOf(x**3 - 3*x + 1, 1) + CRootOf(x**3 - 3*x + 1, 2)
The functions simplify and radsimp cannot simplify this expression. The minimal polynomial, however, is computed correctly:
>>> minimal_polymial(sum(rr))
_x
From this we can conclude that the sum is 0. Is there a direct way of making sympy simplify this sum of roots?
The following function computes the rational number equal to an algebraic term if possible:
import sympy as sp
# try to simplify an algebraic term to a rational number
def try_simplify_to_rational(expr):
try:
float(expr) # does the expression evaluate to a real number?
minPoly = sp.poly(sp.minimal_polynomial(expr))
print('minimal polynomial:', minPoly)
if len(minPoly.monoms()) == 1: # minPoly == x
return 0
if minPoly.degree() == 1: # minPoly == a*x + b
a,b = minPoly.coeffs()
return sp.Rational(-b, a)
except TypeError:
pass # expression does not evaluate to a real number
except sp.polys.polyerrors.NotAlgebraic:
pass # expression does not evaluate to an algebraic number
except Exception as exc:
print("unexpected exception:", str(exc))
print('simplification to rational number not successful')
return expr # simplification not successful
See the working example:
x = sp.symbols('x')
rr = sp.real_roots(x**3 - 3*x + 1)
# sum of roots equals (-1)*coefficient of x^2, here 0
print(sp.simplify(sum(rr)))
print(try_simplify_to_rational(sum(rr))) # -> 0
A more elaborate function computing also simple radical expressions is proposed in sympy issue #19726.
I have a non linear function with two variables, I want to solve the equation . But the solution itself is an equation. How do i evaluate the function at a certain point
CODE:
import sympy as sp
sp.init_printing()
x1,x2,y1,y2 = sp.symbols('x1,x2,y1,y2')
x1,y1=-2,3
f = sp.Eq((x1-x2)**2 + (y1-y2)**2,1)
a = sp.solve([f],(x2,y2))
now i want a a few solution pairs of the function 'a' .
Thanks in advance
:)
You have a single equation in two unknowns: pick a value for one and solve for the other. Here, we pick values for y2 and solve for x2 and pair each solution with the value of i. There is one solution for y2 = 2 and 4 and 2 solutions when it is 3
>>> [(j,i) for i in range(2,5) for j in sp.solve(f.subs(y2,i),x2)]
[(-2, 2), (-3, 3), (-1, 3), (-2, 4)]
Realizing that the equation represents a circle centered at (-2, 3) also permits one to use SymPy's Circle to give you an arbitrary Point in terms of a parameter:
>>> from sympy import Circle
>>> from sympy.abc import t
>>> Circle((-2,3),1).arbitrary_point(t)
Point2D(cos(t) - 2, sin(t) + 3)
Substitute a value for t to get the corresponding point
>>> _.subs(t,pi)
Point2D(-3, 3)
I want to solve the following equation for x with SymPy:
(Note that the equation can be simplified as mentioned in the comments, I copied it verbatim from an example in a legal document.)
According to my understanding, this translates to the following SymPy expression:
from sympy import Sum, solve
from sympy.abc import k, x
solve(350 - 18500 + Sum(182.94 * (1/(1+x)**(k/12)), (k, 1, 120)), x)
However, when I run this, the result is empty:
[]
What am I doing wrong?
solve probably shouldn't give [] but you will get better results from nsolve for this expression using a guess for x near 0:
>>> from sympy.abc import k, x
>>> from sympy import nsolve
eq = 350 - 18500 + Sum(182.94 * (1/(1+x)**(k/12)), (k, 1, 120))
>>> nsolve(eq, 0)
0.0397546543274819
>>> eq.subs(x,_).round(2)
0
I'm attempting to solve some simple Boolean satisfiability problems in Sympy. Here, I tried to solve a constraint that contains the Or logic operator:
from sympy import *
a,b = symbols("a b")
print(solve(Or(Eq(3, b*2), Eq(3, b*3))))
# In other words: (3 equals b*2) or (3 equals b*3)
# [1,3/2] was the answer that I expected
Surprisingly, this leads to an error instead:
TypeError: unsupported operand type(s) for -: 'Or' and 'int'
I can work around this problem using Piecewise, but this is much more verbose:
from sympy import *
a,b = symbols("a b")
print(solve(Piecewise((Eq(3, b*2),Eq(3, b*2)), (Eq(3, b*3),Eq(3, b*3)))))
#prints [1,3/2], as expected
Unfortunately, this work-around fails when I try to solve for two variables instead of one:
from sympy import *
a,b = symbols("a b")
print(solve([Eq(a,3+b),Piecewise((Eq(b,3),Eq(b,3)), (Eq(b,4),Eq(b,4)))]))
#AttributeError: 'BooleanTrue' object has no attribute 'n'
Is there a more reliable way to solve constraints like this one in Sympy?
To expand on zaq's answer, SymPy doesn't recognize logical operators in solve, but you can use the fact that
a*b = 0
is equivalent to
a = 0 OR b = 0
That is, multiply the two equations
solve((3 - 2*b)*(3 - 3*b), b)
As an additional note, if you wanted to use AND instead of OR, you can solve for a system. That is,
solve([eq1, eq2])
is equivalent to solving
eq1 = 0 AND eq2 = 0
Every equation can be expressed as something being equated to 0. For example, 3-2*b = 0 instead of 3 = 2*b. (In Sympy, you don't even have to write the =0 part, it's assumed.) Then you can simply multiply equations to express the OR logic:
>>> from sympy import *
>>> a,b = symbols("a b")
>>> solve((3-b*2)*(3-b*3))
[1, 3/2]
>>> solve([a-3-b, (3-b*2)*(3-b*3)])
[{b: 1, a: 4}, {b: 3/2, a: 9/2}]
As i've read in the sympy docs, the solve() command expects an equation to solve as being equal to zero.
As the equations i would like to solve are not in that form and in fact solving them for 0 is my purpose in using a library like sympy, is there a way to get around this?
What the docs are saying is that if you do something like
>>> solve(x**2 - 1, x)
Then solve is implicitly assuming that x**2 - 1 is equal to 0. If you wanted to solve x**2 - 1 = 2, then you could either subtract 2 from both sides, to get
>>> solve(x**2 - 1 - 2, x)
or you could use the Eq() class
>>> solve(Eq(x**2 - 1, 2), x)