0

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]
  • I do not understand what your code does. `A_extended[ch*(R + 1) + r, r:R]` is independent of `i` and thus it is overwritten for each loop iteration. This means only the last iteration is useful. – Jérôme Richard Mar 24 '22 at 21:36
  • @JérômeRichard sorry for that. When I pasted the code I did not realized that the `ch` variable was there instead of `i`. Now the code is righ, thank you for the advice. – Mateus Schneider Castilhos Mar 25 '22 at 12:35
  • 1
    In your case, the overhead of Numpy functions certainly causes the execution to be slow. I advise you to try to use Numba (or Cython with low-level views). – Jérôme Richard Mar 26 '22 at 18:47

0 Answers0