简体   繁体   中英

TypeError: 'dia_matrix' object has no attribute '__getitem__' - Python

I'm currently trying to dot product a row from a diagonal matrix formed by scipy.sparse.diags function in "Poisson Stiffness" below, but i am having trouble selecting the row and am getting the following error:

TypeError: 'dia_matrix' object has no attribute '__getitem__'

The following is my code

def Poisson_Stiffness(x0):
    """Finds the Poisson equation stiffness matrix with any non uniform mesh x0"""

    x0 = np.array(x0)
    N = len(x0) - 1 # The amount of elements; x0, x1, ..., xN

    h = x0[1:] - x0[:-1]

    a = np.zeros(N+1)
    a[0] = 1 #BOUNDARY CONDITIONS
    a[1:-1] = 1/h[1:] + 1/h[:-1]
    a[-1] = 1/h[-1]
    a[N] = 1 #BOUNDARY CONDITIONS

    b = -1/h
    b[0] = 0 #BOUNDARY CONDITIONS

    c = -1/h
    c[N-1] = 0 #BOUNDARY CONDITIONS: DIRICHLET

    data = [a.tolist(), b.tolist(), c.tolist()]
    Positions = [0, 1, -1]
    Stiffness_Matrix = diags(data, Positions, (N+1,N+1))

    return Stiffness_Matrix


def Error_Indicators(Uh,U_mesh,Z,Z_mesh,f):
    """Take in U, Interpolate to same mesh as Z then solve for eta"""

    u_inter = interp1d(U_mesh,Uh) #Interpolation of old mesh
    U2 = u_inter(Z_mesh) #New function u for the new mesh to use in 

    Bz = Poisson_Stiffness(Z_mesh)

    eta = np.empty(len(Z_mesh))
    for i in range(len(Z_mesh)):
        eta[i] = (f[i] - np.dot(Bz[i,:],U2[:]))*z[i]

    return eta

The error is specifically coming from the following line of code:

eta[i] = (f[i] - np.dot(Bz[i,:],U2[:]))*z[i]

It is the Bz matrix that is causing the error, being created using scipy.sparse.diags and will not allow me to call the row.

Bz = Poisson_Stiffness(np.linspace(0,1,40))
print Bz[0,0]

This code will also produce the same error.

Any help is very much appreciated Thanks

sparse.dia_matrix apparently does not support indexing. The way around that would be to convert it to another format. For calculation purposes tocsr() would be appropriate.

The documentation for dia_matrix is limited, though I think the code is visible Python. I'd have to double check, but I think it is a matrix construction tool, not a fully developed working format.

In [97]: M=sparse.dia_matrix(np.ones((3,4)),[0,-1,2])

In [98]: M[1,:]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-98-a74bcb661a8d> in <module>()
----> 1 M[1,:]

TypeError: 'dia_matrix' object is not subscriptable

In [99]: M.tocsr()[1,:]
Out[99]: 
<1x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Row format>

A dia_matrix stores its information in a .data and .offsets attributes, which are simple modifications of the input parameters. They aren't ammenable for 2d indexing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM