Domanda Sparse Matrix

AnabelaJorge

Utente Iron
26 Ottobre 2020
1
1
0
10
Ultima modifica da un moderatore:
Salve ho un problema a sviluppare una definizione per la moltiplicazione fra matrici sparse nella classe SparseMatrix. Ovviamente senza usare i metodi. Questo è quello che ho scritto, ma non mi restituisce il risultato voluto. Avendo a disposizione la classe_MatrixElement

Python:
def __mul__(self, B):
        if self.numCols() != B.numRows():
            return("Le matrici non sono compatibili per essere moltiplicate.")

        N=SparseMatrix(self.numRows(), B.numCols())

        for elemento in self._elementList :
            dupElement=self._MatrixElement(elemento._row, elemento._col, elemento._value)
            N._elementList.append(dupElement)

        for elemento in B._elementList :
            valore=N[elemento._col,elemento._value]
            N[elemento._col,elemento._value]=valore

            for r in range(self.numCols()):
                for c in range(elemento._col):
                    for k in range(elemento._row):
                        N[r][c]+=self._elementList[r][k]*elemento[k][c]
        return N