I'd like to know if someone knows a vectoring or more efficient way (reducing time and memory consuming) of creating an upper triangular matrix, which the rows are delayed versions (shifted arrays) of the first one. I've already seen this topic but is there another way? I'll appreciate so much!
Let's introduce the problem: suppose my matrix A is like (I'm using numpy.array
to create it):
[[ 5 6 7 8 9 10 11 12]
[ 4 5 6 7 8 9 10 11]
[ 3 4 5 6 7 8 9 10]
[ 2 3 4 5 6 7 8 9]
[ 1 2 3 4 5 6 7 8]]
And I want a new matrix with more R=4 columns, A_extended, like:
[[ 1 2 3 4 | 5 6 7 8 9 10 11 12]
[ 0 1 2 3 | 4 5 6 7 8 9 10 11]
[ 0 0 1 2 | 3 4 5 6 7 8 9 10]
[ 0 0 0 1 | 2 3 4 5 6 7 8 9]
[ 0 0 0 0 | 1 2 3 4 5 6 7 8]]
So, I need to create an upper triangular matrix and after to stack it to the original matrix. The stacking is not necessary the problem, but if there are another ways more efficient of stacking as well, I'll thank you.
Obs.: I'm assuming that A in the example above is R+1=5 rows of a bigger matrix, let's say: (M,Dr) = B.shape, which in real context, Dr is in the order of 10^6 and M=m(R+1) (m and R are integers), which m ranges from 32 to 128 and R from 8 to 16.
My code:
A_extended = np.hstack((np.zeros((m*(R+1), R)), B))
for i in range(m)):
# A = B[i*(R+1):(i+1)*(R+1), :]
temp_array = B[i*(R+1) + R, R:2*R]
for r in range(R + 1):
A_extended[i*(R + 1) + r, r:R] = temp_array[:R-r]