Access to mpmath module in sympy (python) - python

I am new to sympy and still naive about python.... I wanted to solve a trigonometric equation, to find its zeroes. (Once I have syntax, then I will use a more complex function.)
I cannot find the right syntax yet. Here is what I tried at the iPython console in Spyder (Python 2.7):
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve(sin(x), x)
I got this error:
Traceback (most recent call last):
File "", line 1, in
solve(sin(x), x)
NameError: name 'sin' is not defined
OK, so I need to have the correct reference to the sine function.
According to the sympy documentation, I thought this was in mpath, but this did not work:
from mpmath import *
Traceback (most recent call last):
File "<ipython-input-7-8dcdd12d9679>", line 1, in <module>
from mpmath import *
ImportError: No module named mpmath
How do I load/access mpmath or some other way to get the sine function?

This fixed it:
from sympy import sin

To access mpmath do this
from sympy.mpmath import *

Related

Python IDLE doesn't recognize numpy functions

A very simple thing - I downloaded python 3.8 and installed numpy. Upon making a very basic program that uses a numpy function, I get an error. I captured all the info that I think is relevant for now:
Traceback (most recent call last):
File "C:/Python/numpytest.py", line 6, in <module>
a=sigmoid(2)
File "C:/Python/numpytest.py", line 4, in sigmoid
return 1/(1+exp(-x))
NameError: name 'exp' is not defined
I'm guessing it isn't even importing numpy but no idea why.
Use np.exp(x) to access Numpy's exp() function.
Otherwise, import Numpy as:
from numpy import *
to use exp() without any prefix.
For example,
import numpy as np
def sigmoid(x):
return 1/(1+np.exp(-x))
a=sigmoid(2)
print(a)
You need to call the np class then the exp function
Also, it is better to copy the text rather than take a picture of it.

Scipy - Error while using spherical Bessel functions

I'm trying to draw plots in Python with Scipy module. According to http://docs.scipy.org/doc/scipy/reference/special.html I wrote code with scipy.special.spherical_jn(n,x,0):
import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sp
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
def odrazTE(a,o,d):
temp1 = sp.spherical_jn[1,a,0]
temp2 = 1
return abs(temp1/temp2)**2
t = np.arange(0.001, 2, 0.001)
plt.plot(t,odrazTE(t,t,1),label='TE1')
plt.show()
While I'm compiling the program, all I get is this error:
Traceback (most recent call last):
File "standing-sphere.py", line 33, in <module>
plt.plot(t,odrazTE(t,t,1),label='TE1')
File "standing-sphere.py", line 15, in odrazTE
temp1 = sp.spherical_jn[1,a,0]
AttributeError: 'module' object has no attribute 'spherical_jn'
There is way how to do it with regular Bessel function and relationship between Bessel and spherical Bessel function, but I don't like this solution because of derivative of sph.bess. function that I need too.
Is there any chance I have set something wrongly and it can be fixed to scipy.special.spherical_jn work?
scipy.special.spherical_jn was added in scipy version 0.18.0, which was released on July 25, 2016. My guess is you are using an older version of scipy. To check, run
import scipy
print(scipy.__version__)

What happens to my scipy.sparse.linalg.eigs?

I use python 2.7.8 with the Anaconda distribution and I have problems with scipy.
Let A be a sparse matrix; I want to calculate its eigenvalues but if I write:
import scipy
scipy.sparse.linalg.eigs(A)
I get the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
What is the problem? (The version of scipy is 0.15.1)
Does this work for you?
from scipy import sparse
import scipy.sparse.linalg as sp_linalg
B = np.random.rand(10,10)
A_dense = np.dot(B.T, B)
A_sparse = sparse.lil_matrix(A_dense)
sp_linalg.eigs(A_sparse, 3)
It seems that you have to explicitly import the submodules. scipy does not load those per default.

Python SymPy solving linear system

I have Python 3.3.3 and SymPy 0.7.4.1, both in x64 version and installed locally on my computer. I am using PSPad as a configured editor for Python scripting.
When using imports from library sympy in a module which should solve a set of three linear equations:
from sympy import Matrix, solve_linear_system
from sympy.abc import x, y, z
def main():
system = Matrix (((3,2,-1,1) ,(2,-2,4,-2),(-1,0.5,-1,0)))
print(solve_linear_system(system, x, y,z))
if __name__ == "__main__":
main()
The editor PSPad console output returns the following:
traceback (most recent call last): File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\linear system solve SYMPY.py", line 1, in <module>
from sympy import Matrix,solve_linear_system File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\sympy.py", line 2, in <module>
from sympy import var,Eq,solve ImportError: cannot import name var
Process completed, Exit Code 1.
Execution time: 00:00.134
Actually, I am wondering myself heavily about those issues:
Why, when typing the same thing, without an object def main(), and entered line by line in IDLE, everything is solved correctly, as: {x: 1.00000000000000, y: -2.00000000000000, z: -2.00000000000000}
Why, my PSPad file with object, having the same computation lines, doesn't work and returns errors?
In fact, I would like to use SymPy in normal python code and get computed results in a list or printed in console( .. as in IDLE). Just in order to avoid some annoying line-to-line IDLE manipulations, what should my code file look like?
The problem seems to be that you have created a file named sympy.py, which has the same name as the sympy module.
Hence, in the from sympy import ... statement, your sympy.py is acting as the sympy module.
Try renaming the file to something else, like sympy_programming_test.py and let know if it works.

AttributeError: 'module' object (scipy) has no attribute *** Why does this error occur?

In scipy, the error occurs quite often.
>>> import scipy
>>> scipy.integrate.trapz(gyroSeries, timeSeries)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'integrate'
>>>
I figure out how to solve this problem by doing the following:
>>>
>>> import scipy.integrate
>>> scipy.integrate.trapz(gyroSeries, timeSeries)
>>> 1.2
My question:
Why does the error occur?
Why would that fix the error?
Most possibly because scipy is a library (package) that contains modules and to import a specific module from the scipy library, you need to specify it and import the module itself. As it's a separate module (sub-package), once you import it, it's attributes are available to you by using the regular scipy.module.attribute
In order to fix the error, add the following line at the top of your script
from scipy import integrate
Just simply use
import scipy.constants as spc
and then
C = spc.c #speed of light m/s
pi = spc.pi

Categories