I have an nxN numpy array where N is around 500_000. Depending on the use case of the program, n can be 1 or, e.g., 1000. I'd like to avoid flooding my code with if clauses on the shape of the array every time I use it. Also, I need the first dimension of this array (n) to match that of other arrays in the code base.
So, for the n=1 case, I initially decided to allocate an nxN array, populate the first row and replicate the data with:
my_array = np.repeat(np.reshape(my_array[0,:],(1,my_array.shape[1])),1000,axis=0)
However, this is rather wasteful and takes up 9 GiB of memory. I'd like to have a 1xN array with the data and a 1000xN pointer array (if such thing exist in python) pointing to the 1xN array.
Any ideas? Thanks
Update:
The solution with np.broadcast_to() worked brilliantly. Indeed, no need to create the 2D array, which is what I was looking for.