1
$\begingroup$

My linear algebra book has questions that are are marked to use math software. There's probably some $50 guide on how to do everything but usually documentation has gotten me through. I've been kind of stumped by this though.

I'm doing some applications problems where I have to do something n times. Simple

x_1 = A.x_0,
x_2 = A.x_1,
...

I remember these as sequences in calc.

As it stands I am just doing x = A.x and repeating as many times as I need to, but this is obviously more work than needed if I knew the right tools.

Could you please tell me what would be this tool?

$\endgroup$
  • 2
    $\begingroup$ Lookup Nest and friends. $\endgroup$ – Spawn1701D Jun 26 '13 at 6:27
  • 1
    $\begingroup$ see this tutorial $\endgroup$ – amr Jun 26 '13 at 7:15
  • 1
    $\begingroup$ I doubt this was actual code that was posted, but if it was, the _ character is reserved and will cause you problems. $\endgroup$ – rcollyer Jun 26 '13 at 13:30
3
$\begingroup$

Using Nest[] is one option if you're applying general operations. For your specific example, you can also use MatrixPower[]:

a = RandomReal[1, {5, 5}];
x1 = RandomReal[1, 5];

x2 = a.x1;
x3 = a.x2;
x4 = a.x3;
x5 = a.x4;

Nest[Dot[a, #] &, x1, 4] == x5
(* True *)

MatrixPower[a, 4].x1 == x5
(* True *)

For larger matrices, MatrixPower[] is a bit faster:

a = RandomReal[1, {100, 100}];
x = RandomReal[1, 100];
x1 = x;

First@Timing[For[i = 1, i <= 500, i++, x = a.x;]]
First@Timing[nestx = Nest[Dot[a, #] &, x1, 500];]
First@Timing[powerx = MatrixPower[a, 500].x1;]
x == nestx == powerx
(* 6.860000 *)
(* 6.820000 *)
(* 3.580000 *)
(* True *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.