# sequence of matrix multiplications

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?

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

## 1 Answer

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 *)