Python sympy module problems - python

I met some problems when I using sympy to solve mathematical problems, my code is like this,
eq1=Eq(0, -sympy.sqrt(x**2) + sympy.sqrt((x + 4)**2))
sympy.solve(eq1,x)
the answer for this equation ought to be -2, but I got an empty list instead

eq1=Eq(0, -sympy.sqrt(x**2) + sympy.sqrt((x + 4)**2))
sympy.solveset(eq1,x)
this should work
OUTPUT
{-2}
btw sympy got an exelent website that would have given you the answer, along with a very intuitive shell inside the browser https://docs.sympy.org/latest/tutorial/solvers.html
here is what sympy has to say on the matter:
"Please note that there is another function called solve which can also be used to solve equations. The syntax is solve(equations, variables) However, it is recommended to use solveset instead."

Related

Invalid Syntax error in python, what is the proper way to write f(x)= 6x*(1-x)?

So i'm very new to Python and i'm not used to the syntax yet. This is probably a simple problem but then again, i'm new so I don't know what is the proper way to write this. I'm supposed to integrate f(x)= 6x*(1-x) with intervals [0,1] but I get a syntax error when I write the code. I tried changing the parenthesis but still got the same error. Again, I know this is probably very simple but i'm still trying to get used to Python so help will be very appreciated.
from sympy import Symbol
from sympy import integrate
x=Symbol('x')
print (integrate((6x)*(1-x))
f=((6x)*(1-x))
print(quad(f,0,1))
Changing what you currently have to:
from sympy import Symbol
from sympy import integrate
x = sympy.Symbol('x')
print(sympy.integrate( (6*x)* (1-x) ))
f = ((6*x)*(1-x))
print(sympy.quad(f,0,1))
Fixes your errors. For sympy, you need to write n*y, not ny, and you also had some missing parenthesis.

Using div() on multivariate polynomials in Sympy: incorrect remainder?

I want to compute remainders in Python for multivariate polynomials and I found that div() from sympy should do the trick (I also need sympy for Gröbner computations). But the problem I keep finding is that it seems that div() only checks the leading term for division because
q, r = div(x**2 + y, 2*x)
gives r=y, while
q, r = div(x**2 + y, 2*y)
gives r=x**2+y.
I want to do things like Ideal Membership, hence finding the remainder of some polynomial f on division by G={g_1,...,g_s}, where by above I now cannot rely upon div().
Whilst working with Sage I don't get this problem (using (x^2+y)%y gives x^2), but I'm more familier with Python and prefer to do this via Python.
Can someone please tell me if I'm doing something wrong? Or does someone know a better function to use for remainders?
I found a good alternative: reduced(x**2+y, [2*y]) gives the desired ([1/2], x^2).

Solving equations involving ceilings

I would like to solve the equations of the following type in Python. Please let me know if there are any relevant libraries or methods to solve it. ceil(x) denotes the ceiling function.
ceil(x/7)+3*ceil(x/12) = x
This is just a random example, not sure if there is a solution to this.
I tried looking into Sympy library of Python but I can't find a way to use it for my case.
Any help is appreciated. Thanks!
I tried
from sympy import ceiling
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
print(solve(ceiling(x/7) + 3*ceiling(x/12) - x, x))
which ended up in
No algorithms are implemented to solve equation -x + 3*ceiling(x/12) + ceiling(x/7)
The only thing I can think of right now is a graphical solution.

Sympy: Expanding sum that involves Kets from its quantum module

Today I started using sympy and its quantum module to implement some basic calculations in Bra-Ket notation.
Executing the code:
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
from sympy import *
from sympy.abc import k
print Sum(Ket(k),(k,0,5))
yields the expected result, that is, Sum(|k>, (k, 0, 5)) is printed.
Now I'd like to expand the sum and therefore write:
print Sum(Ket(k),(k,0,5)).doit()
However, this doesn't give the correct result, but prints out 6*|k> which obviously is not the desired output. Apparently, the program doesn't recognize Ket(k) as depending on the index k.
How could I work around or solve this issue?
Looks like a bug. You can probably work around it by doing the sum outside of sympy, with standard python functions like sum(Ket(i) for i in range(6)).

I keep on having problems with syntax and calculating the roots of the quadratic equation

Hey I am having problems with calculating the roots of the quadratic equation with the quadratic formula, using python's complex number functionality.
When I try
>>> if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
else:
I get the error message
SyntaxError: invalid syntax
Then, when I try instead
>>> if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
break
I get the error
SyntaxError: 'break' outside loop
I am trying to put:
else:
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
under it but it won't work.
Any help please?
Not sure I understand your problem, but it looks like you are not properly indenting -- Python uses white space to mark blocks, so the above should look like:
if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
else:
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
although that doesn't make sense because x1 and x2 are calculated the same in both branches, and j is not defined in the else branch... so maybe what you really want is
if root<0:
root=abs(complex(root))
j=complex(0,1)
x1=(-b+j+sqrt(root))/2*a
x2=(-b-j+sqrt(root))/2*a
Part of my confusion is the prompt: enter code here -- this is not a standard Python prompt so either you changed your prompt or you are using some other program with your Python. At any rate, hope this helps.
try importing the complex math module, available online in a number of forms. I believe there is an implementation for complex numbers in the standard python distributions, as well as in numpy/scipy. You can also try working out the real and complex components of your roots separately (by including a test based on the value of the discriminant). Also, your if and elif tests are identical.
As #bythenumbers points out, your if and elif conditions are the same. Also, are you getting exceptions or the wrong values? Also also, where you have j+sqrt(root), do you perhaps mean j*sqrt(root)?

Categories

Resources