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

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.

Related

Matplotlib can't find documented function set_cmap

I have the following code:
import matplotlib.pyplot as plt
plt.cm.set_cmap("Blues")
This gives me an error:
Traceback (most recent call last):
File ".\lorenz_explorer.py", line 12, in <module>
plt.cm.set_cmap("Blues")
AttributeError: module 'matplotlib.cm' has no attribute 'set_cmap'
My matplotlib version is 3.3.1, and the function certainly exists in the documentation for 3.3.1: Link
Then am I doing something wrong or is this a bug? Do I need to import matplotlib.cm separately or something along those lines?
As the documentation link you provide shows, the name of the function is matplotlib.pyplot.set_cmap, not matplotlib.pyplot.cm.set_cmap. So you can call it with plt.set_cmap("Blues").
In other words, the function is not part of the cm library, which is somewhat counter-intuitive.

cython float64 error although float32 specifically set

I am trying to implement user's #rkp solution to their own question of how to speed up sparse matrix multiplications with cython by using the pycuda library (please note this is their second solution in their post).
After installing pycuda, pymetis etc and running their exact same code (in IDLE Python 3.5.2) I am getting:
TypeError: 'numpy.float64' object cannot be interpreted as an integer
It turns out the (reproducible) part that produces this error is:
import numpy as np
import pycuda.autoinit
import pycuda.driver as drv
import pycuda.gpuarray as gpuarray
from pycuda.sparse.packeted import PacketedSpMV
from pycuda.tools import DeviceMemoryPool
from scipy.sparse import csr_matrix
COUNT = 100
N = 5000
P = 0.1
DTYPE = np.int32
#construct objects
np.random.seed(0)
a_dense = np.random.rand(N, N).astype(DTYPE)
a_dense[np.random.rand(N, N) >= P] = 0
a_sparse = csr_matrix(a_dense)
#PacketedSpMV produces the error
spmv = PacketedSpMV(a_sparse, is_symmetric=False, dtype=DTYPE)
And the full error:
Traceback (most recent call last):
File "C:/Users/svobodov/Desktop/data/tests/cython/t.py", line 23, in <module>
spmv = PacketedSpMV(a_sparse, is_symmetric=False, dtype=DTYPE)
File "C:\Python35\lib\site-packages\pycuda\sparse\packeted.py", line 185, in __init__
local_row_costs)
File "pkt_build_cython.pyx", line 22, in pycuda.sparse.pkt_build_cython.build_pkt_data_structure
TypeError: 'numpy.float64' object cannot be interpreted as an integer
I initially thought this to be the cython-related double-precision error but this is obviously something different as it is expecting specifically an integer rather than float32..
I tried tweaking the pkt_build_cython.pyx but without any success or confidence that I did it properly.
Any ideas on how to resolve this please?
As identified in comments, this was a result of a missing integer cast within an internal routine in the PyCUDA codebase.
The bug was actually fixed in 2018, so if you use any PyCUDA 2019 release, you should have the corrected code and this issue should not occur.

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

Access to mpmath module in sympy (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 *

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