matrix^0, matrix.exp() and latex output of matrices

Richard B. Kreckel kreckel at thep.physik.uni-mainz.de
Fri Mar 8 18:33:53 CET 2002


Hi,

[Alexander, something seems to be wrong with your Email.  Majordomo got
some notifications about delivery failure.  Could you please check.]

On Thu, 7 Mar 2002, Alexander Heide wrote:
> Here are some remarks and questions about matrices in GiNaC.
> 
> Have a look at this code:
> 
> #include <iostream>
> #include <ginac/ginac.h>
> using namespace std;
> using namespace GiNaC;
> 
> int main()
> {
>         matrix A(2, 2, lst(0, 1, 1, 0));
>         A.print(print_latex(std::cout)); cout << endl;
>         cout << evalm(exp(A)) << endl;
>         cout << pow(A, 0).evalm() << endl;
>         cout << A.pow(0) << endl;
>         return 0;
> }
> 
> The programm prints out:
> 
> [[0,1],[1,0]]
> exp([[0,1],[1,0]])
> 1
> 
> and then runs forever (or until someone presses CTRL-C or turns off the
> computer or whatever).

Thanks for pointing us to this bug, see below...

> To have a nice matrix in LaTeX I would expect something like
> 
> \left(\begin{array}{ccc}0&1\\1&0\end{array}\right)

This was still unimplemented.  Code to accomplish this is now in CVS.

> in the first line of the output. In the second line the expression keeps
> unevaluated because of a lacking method
> 
> matrix matrix::exp(void) const;

Hmm, what would you expect?  Do you have a handy formula for computing
exponential matrices?  For diagonal matrices, this is easy.  It is also
easy for matrices that turn out to be nilpotent or for which A^2 is the
unit matrix, but in the general (maybe even symbolic) case...

> The third line should be
> 
> [[1,0],[0,1]]

Should it, really?  Then, should A-A be the zero nxm matrix, too?  This is
tricky, because it calls for an exceptional treatment in the power class
(and in the add class).

> as well as the the fourth line.

Indeed, this was a bug, it's fixed now in CVS.  (matrix::pow() was
intended for use by the matrix evaluator evalm() where this problem
couldn't appear since the anonymous evaluator first evaluated it to
1.  Still it should be fine as a user-callable function.)

> And finally I have a (probably stupid) question: To construct an identity
> 2 x 2 matrix, I want to use
> 
> ex I = diag_matrix((lst(1, 1));
> 
> (More convenient would be a function like 'ex idenity_matrix(unsigned n)')

Okay, there is a function 'ex unit_matrix(unsigned n)' in CVS now.

> Now I want to add E to another matrix A, for instance
> 
> A = matrix(2, 2, lst(1, 0, 0, 1));
> 
> This works: evalm(A + E)
> This works not: A.add(E), because E ist not a matrix object.
> 
> My question is: How con I convert E to a matrix object?

ex_to<matrix>(E)

> I've consulted the tutorial, but I couldn't find an answer to this
> question.

Section 5.1 explains this.

Regards
    -richy.
-- 
Richard B. Kreckel
<Richard.Kreckel at Uni-Mainz.DE>
<http://wwwthep.physik.uni-mainz.de/~kreckel/>





More information about the GiNaC-list mailing list