element-wise multiplication of matrices

198 views
Skip to first unread message

paramaniac

unread,
Jun 7, 2009, 2:12:50 PM6/7/09
to sage-support
Dear Sage community,

Is there a possibility/workaround in Sage to compute the element-wise
multiplication of two matrices? In Matlab there's the .* operator, but
Matlab is useless in my case since I need a symbolic result.

Thanks in advance for every help
Regards,
Lukas

EXAMPLE:

sigma, tau, beta = var('sigma tau beta')
A = matrix([[-1/tau, sigma/tau],[sigma/tau, -1/tau]])
B = matrix([[beta/tau, 0],[0, beta/tau]])
C = matrix([[1,0],[0,1]])
D = matrix([[0,0],[0,0]])
I = identity_matrix(2)
s, t = var('s t')
P = C*(s*I-A)^(-1)*B+D
P = P.simplify_rational()

RGA = P .* P.inverse().transpose()

Dan Drake

unread,
Jun 7, 2009, 7:53:12 PM6/7/09
to sage-s...@googlegroups.com
On Sun, 07 Jun 2009 at 11:12AM -0700, paramaniac wrote:
> Is there a possibility/workaround in Sage to compute the element-wise
> multiplication of two matrices? In Matlab there's the .* operator, but
> Matlab is useless in my case since I need a symbolic result.

There's no operator that I know of for that, but you can convert your
matrices to lists, multiply, and convert back:

sage: x,y,z,w = var('x y z w')
sage: a = matrix(SR, 2, 2, [x, y, z, w])
sage: b = matrix(SR, 2, 2, [1+x, 1+y, 1+z, 1+w])
sage: a.list()
[x, y, z, w]
sage: b.list()
[x + 1, y + 1, z + 1, w + 1]

Now make a list of corresponding pairs of entries with zip() and
multiply:

sage: [ x*y for x, y in zip(a.list(), b.list()) ]
[(x + 1)*x, (y + 1)*y, (z + 1)*z, (w + 1)*w]

...and make a matrix out of the new list:

sage: matrix(2, 2, [ x*y for x, y in zip(a.list(), b.list()) ])

[(x + 1)*x (y + 1)*y]
[(z + 1)*z (w + 1)*w]

You can easily put that sequence of steps into a function. You may need
to fiddle a bit with the rows and columns bits, and maybe add a ring
argument if you need to specify what ring the matrix should be over.

def componentwise_multiply(a, b, rows, cols):
return matrix(rows, cols, [x*y for x, y in zip(a.list(), b.list())])


Dan

--
--- Dan Drake <dr...@kaist.edu>
----- KAIST Department of Mathematical Sciences
------- http://mathsci.kaist.ac.kr/~drake

signature.asc

paramaniac

unread,
Jun 8, 2009, 3:17:15 AM6/8/09
to sage-support
Thank you for the fast response!

Regards,
Lukas

paramaniac

unread,
Jun 8, 2009, 3:17:31 AM6/8/09
to sage-support

Jason Grout

unread,
Jun 8, 2009, 7:49:18 AM6/8/09
to sage-s...@googlegroups.com

You can also turn this into a custom infix operator, if you want. That
would mean that your code would depend on a definition, but it could
make your function a lot easier to use. See
http://sagenb.org/home/pub/565 for an example using the above code.
This is based on the code developed in the thread
http://groups.google.com/group/sage-devel/browse_thread/thread/100de89e7d402134/fe89570b403344ae


(that code probably should get into Sage; it makes some calculations
very, very easy to write down...)

The trac ticket for incorporating this decorator is
http://trac.sagemath.org/sage_trac/ticket/6245

Thanks,

Jason

kcrisman

unread,
Jun 8, 2009, 11:24:54 AM6/8/09
to sage-support


> This is based on the code developed in the threadhttp://groups.google.com/group/sage-devel/browse_thread/thread/100de8...
>
> (that code probably should get into Sage; it makes some calculations
> very, very easy to write down...)
>
> The trac ticket for incorporating this decorator ishttp://trac.sagemath.org/sage_trac/ticket/6245

There are lots of good uses for something like this, e.g. I could have
used this for teaching Dirichlet products this past semester. If
something like this ever got into Sage, would it only be for "private"
use as detailed above, or could some custom infix operators
potentially become standard like the backslash, which only works for
things with the _backslash_ defined (presumably only matrix/vector
combos)?

- kcrisman

Jason Grout

unread,
Jun 8, 2009, 2:11:47 PM6/8/09
to sage-s...@googlegroups.com
kcrisman wrote:
>
>
>> This is based on the code developed in the threadhttp://groups.google.com/group/sage-devel/browse_thread/thread/100de8...
>>
>> (that code probably should get into Sage; it makes some calculations
>> very, very easy to write down...)
>>
>> The trac ticket for incorporating this decorator ishttp://trac.sagemath.org/sage_trac/ticket/6245
>
> There are lots of good uses for something like this, e.g. I could have
> used this for teaching Dirichlet products this past semester. If
> something like this ever got into Sage, would it only be for "private"


If you polish it up into a patch, I'll review it (so hopefully it gets
into 4.0.2)! Hmmm...or maybe I shouldn't review it, being one of the
authors...

> use as detailed above, or could some custom infix operators
> potentially become standard like the backslash, which only works for
> things with the _backslash_ defined (presumably only matrix/vector
> combos)?


Only time will tell. I think it's safe to say it would be available for
private use. Frankly, I'm a bit surprised that we have the backslash
operator. It's a bow to matlab (I assume), but there are lots and lots
of other matlab operators we don't have. Sage has (rightfully) been
very conservative about language additions.

However, I can see a sage.misc.infix module (or even specific modules,
like sage.graphs.infix) containing a bunch of nice infix operators, not
defined by default, but that someone could then import. That would help
standardize some things if people used them a lot.

Jason

--
Jason Grout

Reply all
Reply to author
Forward
0 new messages