Effectively calculating the muddy product of the transition matrices (m * m) * (n * n) to obtain the (mn * mn) matrix

Consider the input matrices X and Y of the forms (m, m) and (n, n), respectively. As a conclusion, we need to give a matrix of the form (mn, mn) so that it multiplies the corresponding entries in two matrices. These two matrices X and Y are transition matrices. The following example can be used to illustrate the desired conclusion. Here X is a 3 * 3 matrix, and Y is a 2 * 2 matrix.

Matrix X -------------- x1 x2 x3 x1| abc x2| def x3| ghi Matrix Y -------------- y1 y2 y1| jk y2| lm Matrix Z (Output) ---------------------------------------- x1y1 x1y2 x2y1 x2y2 x3y1 x3y2 x1y1| aj ak bj bk cj ck x1y2| al am bl bm cl cm x2y1| dj dk ej ek fj fk . . 

Below is the non-vectorization that I wrote for this task:

 def transition_multiply(X,Y): num_rows_X=len(X) num_rows_Y=len(Y) out=[] count=0 for i in range(num_rows_X): for j in range(num_rows_Y): out.append([]) for x in X[i]: for y in Y[j]: out[count].append(x*y) count+=1 return out X=[[1,2,3],[2,3,4],[3,4,5]] Y=[[2,4],[1,2]] import numpy print transition_multiply(numpy.array(X),numpy.array(Y)) 

I get the required output, but I understand that the non-vectorized version will be very slow. What would be the best way to vectorize this calculation using Numpy.

Those who are interested in why this calculation is necessary. This is necessary to create a transition matrix from a factorial hidden Markov model from composite transition matrices.

+6
source share
1 answer

This is a Kronecker product , see numpy documentation here .

+8
source

Source: https://habr.com/ru/post/944097/


All Articles