sympy error - AttributeError: sqrt - python

I am new to Python, and I keep getting the following error
..., line 27, in <module>
eq=(p**2+2)/p/sqrt(p**2+4)
AttributeError: sqrt
I tried to add math.sqrt or numpy.sqrt but neither of these work. Does anyone know where I'm going wrong?
My code is:
from numpy import *
from matplotlib import *
from pylab import *
from sympy import Symbol
from sympy.solvers import solve
p=Symbol('p')
eq=(p**2+2)/p/sqrt(p**2+4)
solve(eq,1.34,set=True)

sqrt is defined in the math module, import it this way. This should remove the errors!
from math import sqrt

You are using a sympy symbol: either you wanted to do numerical sqrt (in which case use numpy.sqrt on an actual number) or you wanted symbolic sqrt (in which case use sympy.sqrt). Each of the imports replaces the definition of sqrt in the current namespace, from math, sympy or numpy. It's best to be explicit and not use "import *".
I suspect from the line which follows, you want sympy.sqrt here.

Related

How to edit code to remove pylint error "unused sympy imported as sp"

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).

Calculation going to infinity

I'm trying to calculate the gamma function of a number and it is going to infinity. When I use maple (for instance) it returns the correct answer (2.57e1133 (yes, it's huge)). I tried to use DECIMAL but 0 success. Is there a solution? Thanks in advance.
the code
import scipy as sp
from scipy.special import gamma
from decimal import Decimal
from scipy import special
def teste(k):
Bk2 = Decimal(gamma((1/(2*k))+(3/4)))
return Bk2
print(teste(0.001))
Result
Infinity
I used gammaln to prevent overflow, then I cast the result to Decimal. Lastly, I use the exponential function to recover the intended result.
import scipy as sp
from scipy.special import gammaln
from decimal import Decimal
from scipy import special
from numpy import exp
def teste(k):
Bk2 = exp(Decimal(gammaln((1/(2*k))+(3/4))))
return Bk2
print(teste(0.001))

Gaussian Function in Python 2.7

I need help making a program that calculates the Gaussian function f(x)=1/(sqrt(2*pi)s)*exp[-.5*((x-m)/s)**2] when m=0, s=2, and x=1.
Would it be just:
def Gaussian(m,s,x):
return 1/(sqrt(2*pi)s)*exp[-.5*((x-m)/s)**2]
print Gaussian(0,2,1)
I think you are missing from math import sqrt, from math import exp and from math import pi unless you didn't show it in your code.

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)).

Should I use scipy.pi, numpy.pi, or math.pi?

In a project using SciPy and NumPy, should I use scipy.pi, numpy.pi, or math.pi?
>>> import math
>>> import numpy as np
>>> import scipy
>>> math.pi == np.pi == scipy.pi
True
So it doesn't matter, they are all the same value.
The only reason all three modules provide a pi value is so if you are using just one of the three modules, you can conveniently have access to pi without having to import another module. They're not providing different values for pi.
One thing to note is that not all libraries will use the same meaning for pi, of course, so it never hurts to know what you're using. For example, the symbolic math library Sympy's representation of pi is not the same as math and numpy:
import math
import numpy
import scipy
import sympy
print(math.pi == numpy.pi)
> True
print(math.pi == scipy.pi)
> True
print(math.pi == sympy.pi)
> False
If we look its source code, scipy.pi is precisely math.pi; in fact, it's defined as
import math as _math
pi = _math.pi
In their source codes, math.pi is defined to be equal to 3.14159265358979323846 and numpy.pi is defined to be equal to 3.141592653589793238462643383279502884; both are well above the 15 digit accuracy of a float in Python, so it doesn't matter which one you use.
That said, if you're not already using numpy or scipy, importing them just for np.pi or scipy.pi would add unnecessary dependency while math is a Python standard library, so there's not dependency issues. For example, for pi in tensorflow code in python, one could use tf.constant(math.pi).

Categories

Resources