I have a function init_tensor()
which broadcasts a 2d matrix of dimension (N,N)
into 3d block matrix of dim (M,N,N)
so that there are M matrices of dimension NXN
:
def init_tensor(input_state, sample_size):
return np.broadcast_to(input_state, (sample_size,)+input_state.shape)
So for example if I want to create 3 (4x4) matrices then I could do:
init_tensor(np.eye(4, dtype=complex), 3)
Out[462]:
array([[[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
[0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
[0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
[0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]],
[[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
[0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
[0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
[0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]],
[[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
[0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
[0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
[0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]]])
The problem I have is that I have some arrays with dim (1,M) which I'd like to fill into the 3D array as its elements. For a simple case if M was 3 and I have:
lambda1 = [l11,l12,l13]
lambda2 = [l21,l22,l23]
lambda3 = [l31,l32,l33]
tau1 = [t11,t12,t13]
tau2 = [t21,t22,t23]
tau3 = [t31,t32,t33]
I'd like a vectorised way where I can fill them into the tensor such that it becomes:
array([[[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
[ t11, l11, 0.+0.j, 0.+0.j],
[ t21, 0.+0.j, l21, 0.+0.j],
[ t31, 0.+0.j, 0.+0.j, l31]],
[[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
[ t12, l12, 0.+0.j, 0.+0.j],
[ t22, 0.+0.j, l22, 0.+0.j],
[ t32, 0.+0.j, 0.+0.j, l32]],
[[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
[ t13, l13, 0.+0.j, 0.+0.j],
[ t23, 0.+0.j, l23, 0.+0.j],
[ t33, 0.+0.j, 0.+0.j, l33]]])
The depth of the tensor matrix will always be the same as the length of the 1D arrays, and the value of M
can vary between 1 to 100.