The second loop can be eliminated by creating a 2D array by tiling the self.x array with the np.tile method. The removal of elements in de 1D array with np.delete can than be replaced with a boolean mask (unfortunately this results in a 1D array which needs to be reshaped).
The calculation of the 2D array can be done outside the first loop, as can be the ...
Okay, so there are a lot of problems here. I think the best way to address them is in stages. I want to show you how to go through your own code critically and address problems in it as you write.
Before I begin, for the benefit of someone who will review your code, it is helpful to include all the imports you used so that the person doing the review can ...
For really large sparse matrices, convert numpy dense to
These store only the non-zeros
(well, 2 ints + 1 double, 24 bytes):
S = scipy.sparse.csr_matrix( C ) # dense to sparse
print( "S: %s %d non-0" % (S.shape, S.nnz) )
S *= 30
S.data += 1 # increment only the non-0
# Dense = S.toarray() # sparse to dense
This seems to so what you want, but is specific to the example and code you gave.
There are two pairs of subarrays in arr and two different sets of indices and data to add to the subarrays. So there are four combinations. These get figured out by the values of i, j, and 'k'.
Because the data to be added is sparse, I'm going to use scipy.sparse.coo_matrix() ...