This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Assigning numpy array to Function

+1 vote

Hello

I compute eigenvectors using scipy which return a numpy array. I try to copy this to a function as below

vals, vecs = la.eigs(A, k=5, M=M, sigma=sigma, which='LM', ncv=50, tol=1.0e-6)

up = Function(X)
up.vector()[freeinds] = np.real(vecs[:,0])

freeinds is a numpy array of indices for free dofs. Linear algebra backend is ublas and I am running latest mac binary.

This gives following error

Traceback (most recent call last): File "eig_scipy.py", line 131, in

up.vector()[freeinds] = np.real(vecs[:,0]) File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/cpp/la.py",
line 1255, in setitem
_set_vector_items_array_of_float(self, indices, values) RuntimeError: expected a contiguous 1D numpy array of numbers

I have checked that arrays are of correct dimension. I cannot figure out what could be the problem.

asked Dec 21, 2014 by praveen FEniCS User (2,760 points)
edited Dec 21, 2014 by praveen

1 Answer

+1 vote

It was a small mistake. I had to just do

up.vector()[freeinds] = np.array(np.real(vecs[:,0]))
answered Dec 22, 2014 by praveen FEniCS User (2,760 points)
...