tensor products and/or multidimensional matrix multiplication

67 views
Skip to first unread message

Andre Walker-Loud

unread,
Oct 17, 2016, 8:49:09 PM10/17/16
to sympy
Greetings,

I am using sympy for the first time, and attempting to do matrix multiplication like the following

sum_{l,m} U[i,l] G[l,m] D[j,m]

I want to do regular matrix multiplication, but only on one of the two indices for the objects U and D, in my case for a known G (I want to put numerical entries for the matrix G).  I was hoping to have this print out a symbolic expression.  I found sympy.MatrixSymbol to create a symbolic representation for U and D, but it seems I need some functionality from sympy.tensor class to handle this type of multiplication.

Does anyone have, or could anyone write down a simple example doing such a multiplication?  Let me make a simple specific example.
Let 

G = sympy.Matrix([[0,1], [-1,0]]) #
# [ 0  1 ]
# [ -1 0 ]

So, in the above example, leaving i,j arbitrary, this would result in

U[i,0] D[j,1] - U[i,1] D[j,0]

I also want to multiply this by a G[i,j], but knowing how to do the above I think will be sufficient for the complete multiplication.

Thanks,
Andre

Aaron Meurer

unread,
Oct 18, 2016, 5:51:03 PM10/18/16
to sy...@googlegroups.com
Hopefully something in the tensor module could help you here (I'll
defer that to someone more knowledgeable). Otherwise you'd need
something like MatrixFunction (which isn't implemented yet,
https://github.com/sympy/sympy/issues/5855), i.e., to represent the
matrices as functions of i and j.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/b51fda12-c828-439b-bc14-51915ff6996d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Andre Walker-Loud

unread,
Oct 19, 2016, 2:25:46 PM10/19/16
to sympy
Hi Aaron,

Yes, hopefully someone familiar with the tensor module will chime in.
I am trying to liberate myself from Mathematica, and this is one of the last type of things I do with Mathematica.

Regards,
Andre

Francesco Bonazzi

unread,
Oct 20, 2016, 5:23:14 AM10/20/16
to sympy
Maybe this way: define a supporting Gsym for the symbolic version of the matrix G:

 In [1]: U = MatrixSymbol("U", 2, 2)

In [2]: D = MatrixSymbol("D", 2, 2)

In [3]: G = Matrix([[0, 1], [-1, 0]])

In [4]: Gsym = MatrixSymbol("Gsym", 2, 2)


Now create your matrix multiplication expression:

In [5]: expr = Sum(U[i, l]*Gsym[l, m]*D[j, m], (l, 0, 1), (m, 0, 1))

In [6]: expr
Out[6]:
 
1     1                            
 ___   ___                            
 
                                 
 
      U[i, l]⋅Gsym[l, m]⋅D[j, m]
 
                               
 
                                 
 
‾‾‾   ‾‾‾                            
m
= 0 l = 0                          

In [7]: expr.doit()
Out[7]: U[i, 0]⋅Gsym₀₀⋅D[j, 0] + U[i, 0]⋅Gsym₀₁⋅D[j, 1] + U[i, 1]⋅Gsym₁₀⋅D[j, 0] + U[i, 1]⋅Gsym₁₁⋅D[j, 1]


Now create a replacement dictionary (replacing symbols in Gsym with their numeric values):

In [8]: repl_dict = {Gsym[i]: G[i] for i in range(len(G))}

In [9]: repl_dict
Out[9]: {Gsym₀₀: 0, Gsym₀₁: 1, Gsym₁₀: -1, Gsym₁₁: 0}


Perform the replacement:

In [10]: expr.doit().subs(repl_dict)
Out[10]: U[i, 0]⋅D[j, 1] - U[i, 1]⋅D[j, 0]


Unfortunately the matrix module still needs some fixes, but I hope that you can use this trick to overcome the current limitations.

Francesco Bonazzi

unread,
Oct 20, 2016, 7:33:41 AM10/20/16
to sympy
Got the inspiration for this fix:

https://github.com/sympy/sympy/pull/11748

Reply all
Reply to author
Forward
0 new messages