symbolic matrix multiplication in sage

1,244 views
Skip to first unread message

alex

unread,
Mar 8, 2009, 2:43:37 PM3/8/09
to sage-support
How can i compute the matrix multiplication (product) of two symbolic
matrices in sage ?

I have tried:
A = maxima("matrix ([a, b], [c, d])")
AI= A.invert()

and
A * AI
gives
matrix([a*d/(a*d-b*c),-b^2/(a*d-b*c)],[-c^2/(a*d-b*c),a*d/(a*d-b*c)])

so * is not the matrix product.

I did not founf informations in any documentation !!

So: How can i compute the matrix multiplication (product) of two
symbolic matrices in sage ?

THX !

Justin C. Walker

unread,
Mar 8, 2009, 7:06:07 PM3/8/09
to sage-s...@googlegroups.com

On Mar 8, 2009, at 11:43 , alex wrote:

>
> How can i compute the matrix multiplication (product) of two symbolic
> matrices in sage ?

I'm not an expert in this part of Sage, but I think you need to get
more explicit about what you are doing. Real experts can correct me
where I err.

In Sage, there is a "symbolic ring" that supports all symbolic
computation. When you declare variables:

sage: var('x y z w')
(x, y, z, w)

these elements belong to that ring:

sage: x.parent()
Symbolic Ring

The name of the ring is 'SR':

sage: SR
Symbolic Ring

This seems to give you what you want:

sage: M = Matrix(SR,2,2,[x,y,z,w])
sage: M

[x y]
[z w]
sage: M*M

[y*z + x^2 x*y + w*y]
[x*z + w*z y*z + w^2]
sage: M^-1

[ w/(w*x - y*z) -y/(w*x - y*z)]
[-z/(w*x - y*z) x/(w*x - y*z)]

HTH,

Justin

--
Justin C. Walker, Curmudgeon at Large
Director
Institute for the Enhancement of the Director's income
-----------
--
They said it couldn't be done, but sometimes,
it doesn't work out that way.
- Casey Stengel
--

Robert Dodier

unread,
Mar 9, 2009, 12:58:50 AM3/9/09
to sage-support
alex wrote:

> How can i compute the matrix multiplication (product) of two symbolic
> matrices in sage ?
>
> I have tried:
> A = maxima("matrix ([a, b], [c, d])")
> AI= A.invert()
>
> and
> A * AI
> gives
> matrix([a*d/(a*d-b*c),-b^2/(a*d-b*c)],[-c^2/(a*d-b*c),a*d/(a*d-b*c)])
>
> so * is not the matrix product.

For the record, "." is the noncommutative product operator in Maxima
(while "*" is commutative). Dunno if it matters, hope this helps.

Robert Dodier

alex

unread,
Mar 9, 2009, 7:21:46 AM3/9/09
to sage-support
. do not work.
I have tried
A.AI
A . AI
and
maxima("A.AI")

please help me
thx

Simon King

unread,
Mar 9, 2009, 7:35:33 AM3/9/09
to sage-support
Dear Alex,

On Mar 9, 12:21 pm, alex <alessandro.bernardini.1...@gmail.com> wrote:
> . do not work.
> I have tried
> A.AI
> A . AI

This does not work because A and AI live in Sage, and in Sage the dot
does not mean multiplication.

A.AI means "look for an attribute named AI of the object A". Since A
has no attribute of that name, I thought you'd get an attribute error.
I am very surprised by the following:
sage: A.AI
AI

Can anyone explain this?

> maxima("A.AI")

This does not work, because A and AI live in Sage, not in Maxima.
There are underlying Maxima objects for A and AI, though. They have a
name that is automatically chosen; in my session, it is
sage: A.name()
'sage0'
sage: AI.name()
'sage1'

The Maxima objects corresponding to A and AI are obtainable under
these names:
sage: print maxima.eval('sage0')
matrix([a,b],[c,d])

Hence, for multiplying A and AI using Maxima's dot operator, you could
do:
sage: M = maxima(A.name()+'.'+AI.name())
sage: M
matrix([a*d/(a*d-b*c)-b*c/(a*d-b*c),0],[0,a*d/(a*d-b*c)-b*c/(a*d-
b*c)])

This is the correct result, since a*d/(a*d-b*c)-b*c/(a*d-b*c)
simplifies to 1.

What I wrote above is based on how Sage interfaces work in general.
Since I don't know about Maxima, I can not tell you how to ask Maxima
to simplify the coefficients of M.

Cheers,
Simon

David Joyner

unread,
Mar 9, 2009, 7:44:24 AM3/9/09
to sage-s...@googlegroups.com
On Sun, Mar 8, 2009 at 1:43 PM, alex
<alessandro.be...@gmail.com> wrote:
>
> How can i compute the matrix multiplication (product) of two symbolic
> matrices in sage ?
>
> I have tried:
> A = maxima("matrix ([a, b], [c, d])")
> AI= A.invert()
>
> and
> A * AI
> gives
> matrix([a*d/(a*d-b*c),-b^2/(a*d-b*c)],[-c^2/(a*d-b*c),a*d/(a*d-b*c)])


Do you want the following?

sage: a,b,c,d = var("a,b,c,d")
sage: A = matrix ([[a, b], [c, d]])
sage: AI = A.inverse()
sage: P = A*AI; P

[a*d/(a*d - b*c) - b*c/(a*d - b*c) 0]
[ 0 a*d/(a*d - b*c) - b*c/(a*d - b*c)]

Robert Bradshaw

unread,
Mar 9, 2009, 1:48:24 PM3/9/09
to sage-s...@googlegroups.com
On Mar 9, 2009, at 4:44 AM, David Joyner wrote:

> On Sun, Mar 8, 2009 at 1:43 PM, alex
> <alessandro.be...@gmail.com> wrote:
>>
>> How can i compute the matrix multiplication (product) of two symbolic
>> matrices in sage ?
>>
>> I have tried:
>> A = maxima("matrix ([a, b], [c, d])")
>> AI= A.invert()
>>
>> and
>> A * AI
>> gives
>> matrix([a*d/(a*d-b*c),-b^2/(a*d-b*c)],[-c^2/(a*d-b*c),a*d/(a*d-b*c)])
>
>
> Do you want the following?
>
> sage: a,b,c,d = var("a,b,c,d")
> sage: A = matrix ([[a, b], [c, d]])
> sage: AI = A.inverse()
> sage: P = A*AI; P
>
> [a*d/(a*d - b*c) - b*c/(a*d - b*c) 0]
> [ 0 a*d/(a*d - b*c) - b*c/(a*d - b*c)]

sage: P.simplify_rational()

[1 0]
[0 1]


Iwan Lappo-Danilewski

unread,
Mar 10, 2009, 6:50:19 AM3/10/09
to sage-support
Why does a Matrix not possess the function full_simpify. I.e. why does
P.full_simplify() not work?

alex

unread,
Mar 10, 2009, 9:28:56 AM3/10/09
to sage-support
yes, this is what i want !

BUT i cant compute the eigenvectors symbolically within SAGE.
So for example

A.eigenvectors()

gives the error:
AttributeError: 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_'
object has no attribute 'eigenvectors'

how can i now compute those eigenvectors within SAGE or with the
MAXIMA interface ?
Thank you very much !

___________________________________________________
On Mar 9, 6:48 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Jason Grout

unread,
Mar 10, 2009, 10:19:28 AM3/10/09
to sage-s...@googlegroups.com
alex wrote:
> yes, this is what i want !
>
> BUT i cant compute the eigenvectors symbolically within SAGE.
> So for example
>
> A.eigenvectors()
>
> gives the error:
> AttributeError: 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_'
> object has no attribute 'eigenvectors'
>
> how can i now compute those eigenvectors within SAGE or with the
> MAXIMA interface ?


Searching the google interface to sage-support for "symbolic
eigenvectors" yields this message from a few days ago. Right now,
calculating eigenvectors of symbolic matrices is a bit different than
other matrices; hopefully this will be corrected soon.


http://groups.google.com/group/sage-support/browse_thread/thread/4370a886918b0f14/a7578c228b204558?lnk=gst&q=symbolic+eigenvectors#a7578c228b204558

Thanks,

Jason

Jason Grout

unread,
Mar 10, 2009, 10:27:10 AM3/10/09
to sage-s...@googlegroups.com
Iwan Lappo-Danilewski wrote:
> Why does a Matrix not possess the function full_simpify. I.e. why does
> P.full_simplify() not work?
>

It's probably because no one has written it yet. I think it'd be great
to have. We welcome any patches to do that.

You can do the same thing using the apply_map function, which applies a
function to each entry of a matrix.

sage: var('a,b,c,d')
(a, b, c, d)
sage: A=matrix([[sin(a+b), sin(c+d)],[cos(a+d),cos(b+d)]])
sage: A

[sin(b + a) sin(d + c)]
[cos(d + a) cos(d + b)]
sage: B=A.apply_map(lambda x: x.full_simplify())
sage: B

[cos(a)*sin(b) + sin(a)*cos(b) cos(c)*sin(d) + sin(c)*cos(d)]
[cos(a)*cos(d) - sin(a)*sin(d) cos(b)*cos(d) - sin(b)*sin(d)]

Jason

alex

unread,
Mar 10, 2009, 11:05:34 AM3/10/09
to sage-support
How can I collect the eigenvectors to form an eigenvector matrix ?

I have tried all matrix commands and I always get some error !

Sorry,
THX

alex

unread,
Mar 10, 2009, 12:13:46 PM3/10/09
to sage-support
This seems to work:
given a symbolic matrix P of dimension 2x2 do:

EVECP = maxima(P).eigenvectors()
EVEC1 = EVECP.part(2)
(first eigenvector)
EVEC2 = EVECP.part(3)
(second eigenvector)
EIGENVECT = (matrix(SR, 2,2, [EVEC1.sage(), EVEC2.sage()])).transpose
() (matrix of eigenvectors)

for the inverse:
EIGENVECTINV = EIGENVECT.inverse()
(EIGENVECTINV * EIGENVECT).simplify_rational()
gives identity matrix.

the eigenvalues of P with multiplicity (!) can be read with
EVECP.part(1)

Is this correct ?

Is there a way to symbolically compute the matrix exponential
directly ?
THX !

Jason Grout

unread,
Mar 10, 2009, 3:00:55 PM3/10/09
to sage-s...@googlegroups.com
alex wrote:
> This seems to work:
> given a symbolic matrix P of dimension 2x2 do:
>
> EVECP = maxima(P).eigenvectors()
> EVEC1 = EVECP.part(2)
> (first eigenvector)
> EVEC2 = EVECP.part(3)
> (second eigenvector)
> EIGENVECT = (matrix(SR, 2,2, [EVEC1.sage(), EVEC2.sage()])).transpose
> () (matrix of eigenvectors)
>
> for the inverse:
> EIGENVECTINV = EIGENVECT.inverse()
> (EIGENVECTINV * EIGENVECT).simplify_rational()
> gives identity matrix.
>
> the eigenvalues of P with multiplicity (!) can be read with
> EVECP.part(1)
>
> Is this correct ?
>
> Is there a way to symbolically compute the matrix exponential
> directly ?
> THX !


There should be a matrix exponential function:

sage: var('a,b,c,d')
(a, b, c, d)

sage: A=matrix([[a,0],[0,d]])
sage: A.exp()

[e^a 0]
[ 0 e^d]
sage: A=matrix([[a,2],[0,d]])
sage: A.exp()

[ e^a (2*e^d - 2*e^a)/(d - a)]
[ 0 e^d]


Jason

alex

unread,
Mar 11, 2009, 12:01:23 PM3/11/09
to sage-support
Thank you very much, for the useful information !

Where can I find a complete and good documentation about Sage, better
then the tutorial or the reference ?

Thank you again.
Reply all
Reply to author
Forward
0 new messages