everyone! I am pretty new on this platform and need your help in one thing. I am using sympy on jupyter notebook and I am programmig a dummy formula, root square, however the output for this symbol is not the root symbol, actually it is outputting sqrt instead.
I will give you guys an example
import sympy
In: sympy.sqrt(3)
Out :sqrt(3)
This is normal behavior for SymPy. I can't test it for now but try adding init_printing(use_unicode=True) in your code before calling the sqrt function.
Source :
https://docs.sympy.org/latest/tutorial/intro.html
Related
I have some code that currently returns the pylint error:
source.py:2:0: W0611: Unused sympy imported as sp (unused-import)
there is a clue: "Note: parse_expr() is available for you to use in this function and sympy has been imported into this question as sp" but I don't understand what to do.
It works fine in my WingIDE but is failing on the server I submit it. Any input on how to change the code to not have this error would be appreciated. Code is below :)
from sympy.parsing.sympy_parser import parse_expr
from sympy import Eq, solve
def num_intersections(expressions):
"""return a list of the number of times each expression
in this list intersects with every other expression."""
lst = []
for i in range(len(expressions)):
expressions[i] = parse_expr(expressions[i])
new_exp_lst = expressions
for expp in expressions:
intersection_cnt = 0
new_exp_lst.remove(expp)
for n_expp in new_exp_lst:
intersection_cnt += len(solve(Eq(expp, n_expp), list=True))
lst.append(intersection_cnt)
new_exp_lst.append(expp)
return lst
It look like your professor already imported sympy for you as sp and is doing some magic to append your code to its own file that he/she then lint with pylint. Use sp.x when you need sympy, i.e.
Instead of
from sympy.parsing.sympy_parser import parse_expr
from sympy import Eq, solve
Use:
import sympy as sp # Remove that before submitting
And then replace len(solve(Eq(expp, n_expp), list=True)) by len(sp.solve(sp.Eq(expp, n_expp), list=True)) (among others).
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.
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."
I am trying to use code which uses Bessel function zeros for other calculations. I noticed the following piece of code produces results that I consider unexpected.
import scipy
from scipy import special
scipy.special.jn_zeros(1,2)
I would expect the result from this call to be
array([0., 3.83170597])
instead of
array([3.83170597, 7.01558667])
Is there a reason a reason why the root at x=0.0 is not being returned?
From what I can see the roots are symmetric along the x-axis except for any found at the origin, but I do not think this would be enough of a reason to leave off the root completely.
The computer I am using has python version 2.7.10 installed and is using scipy version 0.19.0
P.S. the following function is what I am trying to find the zeros of
scipy.special.j1
It appears to be convention to not count the zero at zero, see for example ħere. Maybe it is considered redundant?
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)).