Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Lookup Nest and friends. – Spawn1701D Jun 26 '13 at 6:27
see this tutorial – 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
up vote 3 down vote accepted

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 *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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