def dkron(A,dA,B,dB, operation='<STR_LIT>'):
if dA is None:<EOL><INDENT>dA_param_num = <NUM_LIT:0><EOL>dA = np.zeros((A.shape[<NUM_LIT:0>], A.shape[<NUM_LIT:1>],<NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>dA_param_num = dA.shape[<NUM_LIT:2>]<EOL><DEDENT>if dB is None:<EOL><INDENT>dB_param_num = <NUM_LIT:0><EOL>dB = np.zeros((B.shape[<NUM_LIT:0>], B.shape[<NUM_LIT:1>],<NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>dB_param_num = dB.shape[<NUM_LIT:2>]<EOL><DEDENT>dC = np.zeros((A.shape[<NUM_LIT:0>]*B.shape[<NUM_LIT:0>], A.shape[<NUM_LIT:1>]*B.shape[<NUM_LIT:1>], dA_param_num +  dB_param_num))<EOL>for k in range(dA_param_num):<EOL><INDENT>if operation == '<STR_LIT>':<EOL><INDENT>dC[:,:,k] = np.kron(dA[:,:,k],B);<EOL><DEDENT>else:<EOL><INDENT>dC[:,:,k] = np.kron(dA[:,:,k],np.eye( B.shape[<NUM_LIT:0>] ))<EOL><DEDENT><DEDENT>for k in range(dB_param_num):<EOL><INDENT>if operation == '<STR_LIT>':<EOL><INDENT>dC[:,:,dA_param_num+k] = np.kron(A,dB[:,:,k])<EOL><DEDENT>else:<EOL><INDENT>dC[:,:,dA_param_num+k] = np.kron(np.eye( A.shape[<NUM_LIT:0>] ),dB[:,:,k])<EOL><DEDENT><DEDENT>return dC<EOL>
Function computes the derivative of Kronecker product A*B
(or Kronecker sum A+B).

Input:
-----------------------

A: 2D matrix
    Some matrix
dA: 3D (or 2D matrix)
    Derivarives of A
B: 2D matrix
    Some matrix
dB: 3D (or 2D matrix)
    Derivarives of B

operation: str 'prod' or 'sum'
    Which operation is considered. If the operation is 'sum' it is assumed
    that A and are square matrices.s

Output:
    dC: 3D matrix
    Derivative of Kronecker product A*B (or Kronecker sum A+B)