25 Oct 2013 19:50

Symbolic matrix power

Michele Minelli micheleminelli1 at gmail.com
Fri Oct 25 07:39:41 CDT 2013 wrote:

>Hello everyone, >is there a way to compute the k-th power of a >matrix .in a symbolic way? >For example if the matrix is [2, 0, 0], [0, 2, 0],[-1, 0, 3] it should >return [2^k, 0, 0], [0, 2^k, 0], [2^k-3^k, 0, 3^k]. >Thank you. >Michele

Do

(%i1) A:matrix([2, 0, 0], [0, 2, 0], [-1, 0, 3])\$
(%i3) integer_pow(x) := block ([k], declare (k, integer), x^k)\$
(%i4) mat_function (integer_pow, A);
(%o4) matrix([2^k,0,0],[0,2^k,0],[2^k-3^k,0,3^k])

best

Aleksas D
_______________________________________________
Maxima mailing list
Maxima <at> math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima
25 Oct 2013 20:26

Re: Symbolic matrix power

On 10/25/2013 07:50 PM, Aleksas Domarkas wrote:
> (%i1) A:matrix([2, 0, 0], [0, 2, 0], [-1, 0, 3])\$
> (%i3) integer_pow(x) := block ([k], declare (k, integer), x^k)\$
> (%i4) mat_function (integer_pow, A);
> (%o4) matrix([2^k,0,0],[0,2^k,0],[2^k-3^k,0,3^k])
Hi list,
I tried the following

[user <at> host ~]\$ rmaxima
Maxima 5.30.0 http://maxima.sourceforge.net
using Lisp SBCL 1.1.2-1.fc18
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
STYLE-WARNING: redefining MAXIMA::\$FILE_TYPE in DEFUN
(%i1) display2d : false;

(%o1) false
(%i2) A : matrix([a,b],[c,d]);

(%o2) matrix([a,b],[c,d])

(%o3) "/home/user/opt/maxima/share/maxima/5.30.0/share/contrib/diag.mac"
(%i4)  integer_pow(x) := block ([k], declare (k, integer), x^k);

(%o4) integer_pow(x):=block([k],declare(k,integer),x^k)
(%i5) mat_function (integer_pow, A);

(%o5) matrix([matrix([(sqrt(d^2-2*a*d+4*b*c+a^2)-d-a)^k* ...

Where %o5 consists of a long long expression
But can you see it the two occurrences of "matrix"?

Andre
27 Oct 2013 10:43

"matrix([matrix([" returned with mat_function (Was: Re: Symbolic matrix power)

This is a repost, so it won't get lost.

On 10/25/2013 08:26 PM, andre maute wrote:
> On 10/25/2013 07:50 PM, Aleksas Domarkas wrote:
>> (%i1) A:matrix([2, 0, 0], [0, 2, 0], [-1, 0, 3])\$
>> (%i3) integer_pow(x) := block ([k], declare (k, integer), x^k)\$
>> (%i4) mat_function (integer_pow, A);
>> (%o4) matrix([2^k,0,0],[0,2^k,0],[2^k-3^k,0,3^k])
> Hi list,
> I tried the following
>
> [user <at> host ~]\$ rmaxima
> Maxima 5.30.0 http://maxima.sourceforge.net
> using Lisp SBCL 1.1.2-1.fc18
> Dedicated to the memory of William Schelter.
> The function bug_report() provides bug reporting information.
> STYLE-WARNING: redefining MAXIMA::\$FILE_TYPE in DEFUN
> (%i1) display2d : false;
>
> (%o1) false
> (%i2) A : matrix([a,b],[c,d]);
>
> (%o2) matrix([a,b],[c,d])
>
> (%o3) "/home/user/opt/maxima/share/maxima/5.30.0/share/contrib/diag.mac"
> (%i4) integer_pow(x) := block ([k], declare (k, integer), x^k);
>
> (%o4) integer_pow(x):=block([k],declare(k,integer),x^k)
> (%i5) mat_function (integer_pow, A);
>
> (%o5) matrix([matrix([(sqrt(d^2-2*a*d+4*b*c+a^2)-d-a)^k* ...
>
> Where %o5 consists of a long long expression
> But can you see it the two occurrences of "matrix"?
>
> Andre
>
> _______________________________________________
> Maxima mailing list
> Maxima <at> math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
27 Oct 2013 10:58

Re: "matrix([matrix([" returned with mat_function (Was: Re: Symbolic matrix power)

Actually there are more than two occurrences: scrolling through the
expression I counted three of them. Probably the matrix is defined by
use of submatrices, but mine is just a guess.
Regards,

Michele M.

Il 27/10/2013 10:43, andre maute ha scritto:
> This is a repost, so it won't get lost.
>
> On 10/25/2013 08:26 PM, andre maute wrote:
>> On 10/25/2013 07:50 PM, Aleksas Domarkas wrote:
>>> (%i1) A:matrix([2, 0, 0], [0, 2, 0], [-1, 0, 3])\$
>>> (%i3) integer_pow(x) := block ([k], declare (k, integer), x^k)\$
>>> (%i4) mat_function (integer_pow, A);
>>> (%o4) matrix([2^k,0,0],[0,2^k,0],[2^k-3^k,0,3^k])
>> Hi list,
>> I tried the following
>>
>> [user <at> host ~]\$ rmaxima
>> Maxima 5.30.0 http://maxima.sourceforge.net
>> using Lisp SBCL 1.1.2-1.fc18
>> Dedicated to the memory of William Schelter.
>> The function bug_report() provides bug reporting information.
>> STYLE-WARNING: redefining MAXIMA::\$FILE_TYPE in DEFUN
>> (%i1) display2d : false;
>>
>> (%o1) false
>> (%i2) A : matrix([a,b],[c,d]);
>>
>> (%o2) matrix([a,b],[c,d])
>>
>> (%o3) "/home/user/opt/maxima/share/maxima/5.30.0/share/contrib/diag.mac"
>> (%i4) integer_pow(x) := block ([k], declare (k, integer), x^k);
>>
>> (%o4) integer_pow(x):=block([k],declare(k,integer),x^k)
>> (%i5) mat_function (integer_pow, A);
>>
>> (%o5) matrix([matrix([(sqrt(d^2-2*a*d+4*b*c+a^2)-d-a)^k* ...
>>
>> Where %o5 consists of a long long expression
>> But can you see it the two occurrences of "matrix"?
>>
>> Andre
>>
>> _______________________________________________
>> Maxima mailing list
>> Maxima <at> math.utexas.edu
>> http://www.math.utexas.edu/mailman/listinfo/maxima
>>
>
> _______________________________________________
> Maxima mailing list
> Maxima <at> math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
28 Oct 2013 19:25

Re: "matrix([matrix([" returned with mat_function (Was: Re: Symbolic matrix power)

> On 10/25/2013 08:26 PM, andre maute wrote:

>> (%i2) A : matrix([a,b],[c,d]);
>>
>> (%o2) matrix([a,b],[c,d])
>>
>> (%o3) "/home/user/opt/maxima/share/maxima/5.30.0/share/contrib/diag.mac"
>> (%i4) integer_pow(x) := block ([k], declare (k, integer), x^k);
>>
>> (%o4) integer_pow(x):=block([k],declare(k,integer),x^k)
>> (%i5) mat_function (integer_pow, A);
>>
>> (%o5) matrix([matrix([(sqrt(d^2-2*a*d+4*b*c+a^2)-d-a)^k* ...

This is due to Maxima's dynamic binding policy -- there is a local
variable in diag_mode_matrix named 'a' which is bound to the value of
the user variable 'A'. Try it again w/ A : matrix([b, c], [d, e]) --
I think you'll get a different result.

This particular bug could be fixed by changing 'a' to 'a%' or something
to reduce the chance of a name collision, but it doesn't seem worth the
trouble -- every local variable really needs the same treatment, and
anyway the problem will reappear if someone uses an "unlikely" name.
The only way to really fix it is to make local variables lexical instead
of dynamic -- I'm in favor of that, for the record.

best

Robert Dodier
28 Oct 2013 19:47

Re: "matrix([matrix([" returned with mat_function (Was: Re: Symbolic matrix power)

On 10/28/2013 07:25 PM, Robert Dodier wrote:
>> On 10/25/2013 08:26 PM, andre maute wrote:
>>> (%i2) A : matrix([a,b],[c,d]);
>>>
>>> (%o2) matrix([a,b],[c,d])
>>>
>>> (%o3) "/home/user/opt/maxima/share/maxima/5.30.0/share/contrib/diag.mac"
>>> (%i4) integer_pow(x) := block ([k], declare (k, integer), x^k);
>>>
>>> (%o4) integer_pow(x):=block([k],declare(k,integer),x^k)
>>> (%i5) mat_function (integer_pow, A);
>>>
>>> (%o5) matrix([matrix([(sqrt(d^2-2*a*d+4*b*c+a^2)-d-a)^k* ...
> This is due to Maxima's dynamic binding policy -- there is a local
> variable in diag_mode_matrix named 'a' which is bound to the value of
> the user variable 'A'. Try it again w/ A : matrix([b, c], [d, e]) --
> I think you'll get a different result.

And here is the next problem with trigreduce

\$ rmaxima
Maxima 5.30.0 http://maxima.sourceforge.net
using Lisp SBCL 1.1.2-1.fc18
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
STYLE-WARNING: redefining MAXIMA::\$FILE_TYPE in DEFUN
(%i1) display2d : false;

(%o1) false
(%i2) A : matrix([q,p],[r,s]);

(%o2) matrix([q,p],[r,s])

(%o3) "/home/user/opt/maxima/share/maxima/5.30.0/share/contrib/diag.mac"
(%i4) integer_pow(x) := block ([k], declare (k, integer), x^k);

(%o4) integer_pow(x):=block([k],declare(k,integer),x^k)
(%i5) h : mat_function(f,A)\$

(%i6) trigreduce(h);

Maxima encountered a Lisp error:

The value 0 is not of type LIST.

Automatically continuing.
To enable the Lisp debugger set *debugger-hook* to nil.
(%i7)
28 Oct 2013 20:15

Re: "matrix([matrix([" returned with mat_function (Was: Re: Symbolic matrix power)

It is true that lexical scope rules (or changing the variable name) would fix this.

But that only matters if an object is being evaluated where it shouldn't be. And sure enough, applying the 'matrix' function incorrectly re-evaluates its arguments. Here is a simple example:

q: '[[x]]\$
x: 3\$
apply('matrix,q) => matrix([3])     !!!!  should be matrix([x])

This extra evaluation is a bug in 'apply'.

-s

On Mon, Oct 28, 2013 at 2:25 PM, Robert Dodier wrote:
> On 10/25/2013 08:26 PM, andre maute wrote:

>> (%i2) A : matrix([a,b],[c,d]);
>>
>> (%o2) matrix([a,b],[c,d])
>>
>> (%o3) "/home/user/opt/maxima/share/maxima/5.30.0/share/contrib/diag.mac"
>> (%i4) integer_pow(x) := block ([k], declare (k, integer), x^k);
>>
>> (%o4) integer_pow(x):=block([k],declare(k,integer),x^k)
>> (%i5) mat_function (integer_pow, A);
>>
>> (%o5) matrix([matrix([(sqrt(d^2-2*a*d+4*b*c+a^2)-d-a)^k* ...

This is due to Maxima's dynamic binding policy -- there is a local
variable in diag_mode_matrix named 'a' which is bound to the value of
the user variable 'A'. Try it again w/ A : matrix([b, c], [d, e]) --
I think you'll get a different result.

This particular bug could be fixed by changing 'a' to 'a%' or something
to reduce the chance of a name collision, but it doesn't seem worth the
trouble -- every local variable really needs the same treatment, and
anyway the problem will reappear if someone uses an "unlikely" name.
The only way to really fix it is to make local variables lexical instead
of dynamic -- I'm in favor of that, for the record.

best

Robert Dodier

_______________________________________________
Maxima mailing list
Maxima <at> math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima

_______________________________________________
Maxima mailing list
Maxima <at> math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima
25 Oct 2013 20:38

Re: Symbolic matrix power

Thank you very much Aleksas! Your method seems to work flawlessly. I did not understand exactly what you have done but I willi try to read the doc.
Regards,

Michele Minelli

Il 25/10/2013 19:50, Aleksas Domarkas ha scritto:
Michele Minelli micheleminelli1 at gmail.com
Fri Oct 25 07:39:41 CDT 2013 wrote:

>Hello everyone, >is there a way to compute the k-th power of a >matrix .in a symbolic way? >For example if the matrix is [2, 0, 0], [0, 2, 0],[-1, 0, 3] it should >return [2^k, 0, 0], [0, 2^k, 0], [2^k-3^k, 0, 3^k]. >Thank you. >Michele

Do

(%i1) A:matrix([2, 0, 0], [0, 2, 0], [-1, 0, 3])\$
(%i3) integer_pow(x) := block ([k], declare (k, integer), x^k)\$
(%i4) mat_function (integer_pow, A);
(%o4) matrix([2^k,0,0],[0,2^k,0],[2^k-3^k,0,3^k])

best

Aleksas D

_______________________________________________ Maxima mailing list Maxima <at> math.utexas.edu http://www.math.utexas.edu/mailman/listinfo/maxima

_______________________________________________
Maxima mailing list
Maxima <at> math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima

Gmane