pythonnumpytransposesimpsons-rule

Numpy product of a vector and it's transpose


I have a N x N complex NumPy array U_0 and I have to do manipulation with it :

Simpson's rule

First, how can I increase the array with zero efficiently ? I can simply copy it into an np.zeros((2N-1, 2N-1)) but maybe you guys know a better method. Thanks to Alexander Riedel for answer this question with the solution of numpy.pad

Second, I tried with

b = np.array([1,2,3])

I saw on previous post to transpose a 1D vector you can do

b_T = b[..., None]
# or
b_T = np.atleast_2d(b).T

but when I try b_T.dot(b) I get shapes (3,1) and (3,) not aligned: 1 (dim 1) != 3 (dim 0). I don't know how to get b into a shape of (1,3) instead of (3,).

Thanks


Solution

  • You can use the expand_dims function to do what you want. The problem here is that numpy does not consider a shape (3, 1) and a (3, ) equivalent. Alternatively look into the matrix type

    Filling the array with zeros, as the commenters pointed out is also the answer to your first question. If that is not efficient enough, look into using sparse matrices from scipy, maybe they have the features you're looking for.