Re: [sympy] Mixing matrix-matrix multiplication and elementwise multiplication

421 views
Skip to first unread message

Chris Smith

unread,
Oct 21, 2012, 4:31:10 AM10/21/12
to sy...@googlegroups.com
On Sun, Oct 21, 2012 at 1:58 PM, David Ketcheson <dke...@gmail.com> wrote:
> I would like to express formulas that involve both traditional matrix-matrix
> multiplication and elementwise multiplication. To be clear, these are the

The following is from the docs on Matrix Expressions:


>>> from sympy import MatrixSymbol, Matrix
>>> X = MatrixSymbol('X', 3, 3)
>>> Y = MatrixSymbol('Y', 3, 3)
>>> (X.T*X).I*Y
X^-1*X'^-1*Y

>>> Matrix(X)
[X_00, X_01, X_02]
[X_10, X_11, X_12]
[X_20, X_21, X_22]

>>> (X*Y)[1, 2]
X_10*Y_02 + X_11*Y_12 + X_12*Y_22

So yes, you can do abstract value-agnostic manipulations. Although
there is a matrix_multiply_elementwise function that operates on
regular matrices, I don't think that there is an implementation yet
for MatrixExpr.

Chris

David Ketcheson

unread,
Oct 21, 2012, 4:43:49 AM10/21/12
to sy...@googlegroups.com
Thanks.  But I don't want to specify the size of the matrices, and I don't want the result to be in terms of matrix elements; I want it to still appear as A*(B.*C) or something similar, as the purpose is generation of MATLAB or numpy code that won't index into the matrices.  I just need two different kinds of multiplication operators for non-commutative symbols.

David

Matthew Rocklin

unread,
Oct 21, 2012, 10:33:54 AM10/21/12
to sy...@googlegroups.com
Hi David, 

MatrixExprs represent matrices without explicit elements. I'm modifying the first half of Chris' example above

>>> from sympy import MatrixSymbol
>>> n = Symbol('n')
>>> X = MatrixSymbol('X', n, n)
>>> Y = MatrixSymbol('Y', n, n)
>>> (X.T*X).I*Y
X^-1*X'^-1*Y

The result here is an expression tree from which you could generate code. I'm actively working on this and would be interested in hearing more about what you're working on. 

MatrixExprs don't currently contain an elementwise product but it could be added if there was a need. I've added an issue for this topic here

I'm planning to take a bit of time to work on MatrixExprs this week. If you have some easy-to-implement feature requests this would be a good time. I'd be happy to see symbolic matrix expressions in wider use. I think that they're a good idea.

Other options: 
1. There is an old `Indexed` class in SymPy for representing and generating code for indexed expressions. I'm sure that this would support element-wise product. I hope that it would also support matrix multiply but I'm not sure what the state of the module is. Andy Terrel might have more information here. 
2. There is the Theano project which actively thinks about code generation for matrix and array expressions. They tend to target people who don't do their own code generation but they certainly have a language that can represent the sorts of operations for which you're asking. 

Best,
-Matthew

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/mcrYhSuqpukJ.

To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

Matthew Rocklin

unread,
Oct 25, 2012, 12:44:17 AM10/25/12
to sy...@googlegroups.com
Finally whipped up a simple Hadamard object in this branch

I don't have the .* syntax though. I haven't figured out what the ideal Python syntax is so I've left it without an operator. 

-----------------
A*(B.*C)

which should mean

\sum_j A_{ij} ( \sum_{p,q} B_{pq} * C_{pq} )_{jk}
-----------------

This can be done in this branch with the following code. There isn't any substantial functionality here other than giving you a valid container object. It's a simple PR. Should be in (I hope) in a couple days. 

A = MatrixSymbol('A', n, m)
B = MatrixSymbol('A', m, k)
C = MatrixSymbol('A', m, k)

A*Hadamard(B, C)
Reply all
Reply to author
Forward
0 new messages