BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ambalath1
Calcite | Level 5

 

I have a n*n matrix A and I would like to calculate A2=A*A , A3=A2*A, A4=A3*A ... A60=A59*A. Can any one help me writing a do loop to perform this task ? I have never used IML before. I do have a piece of code to complete the same task in R.

 

Thank you !

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

As I said when you asked this question on my blog, initialize the result matrix (P) to the identity matrix. Then loop, each time multiplying the current value of P times the original matrix.  The other computations just "go along for the ride."

 

proc iml;
X = {1 2 3,
    -1 0 1,
     2 1 0};
V = {-1, 0, 1};

m = nrow(V);
P = I(m);
do i = 1 to 10;
   P = P*X;
   C = P*V;
   D = V+C;
   F = D*m;
end;
/* at end of loop, P = X**10 */
print P C D F;

View solution in original post

10 REPLIES 10
IanWakeling
Barite | Level 11

You could use the matrix power operator ** to achieve this.   For example:

 

  a = 1.01 # i(3);
  b = a**60;
  print a, b;

Or is it important to retain all the lower powers of a?

 

ambalath1
Calcite | Level 5

Hi IanWakeling,

 

I would have to use all the 60 matrices for some other calculations. Having said, I need A2, A3.. A60 to perform one more computation.

IanWakeling
Barite | Level 11

Unless n is very large, and efficiency is important,  then I would be tempted to calculate the powers as and when you need them.  i.e.

 

c   =   a**10   +   a**20;

 

It is is much more difficult if  you want to save the whole sequence of matrices A1, A2,A3, etc...

IanWakeling
Barite | Level 11

If you want to store all the matrices, then I think you will have to stack them together inside one large matrix. This complicates things as you need to keep track of where in the larger matrix, the individual matrices are stored.  Here is an example:

 

   n = 3;
   maxp = 60;
   ap = j(maxp#n, n); /* large matrix in which to keep all the powers of a */
   ri = shape(1:(maxp#n), maxp); /* row index matrix for sub-matrices in ap */

   a = 1.01 # i(n); /* define the matrix a */

   ap[ ri[ 1, ], ] = a;  /* write matrix a to ap */
   do i = 2 to maxp;     /* write powers to ap */
     ap[ ri[ i, ], ] = ap [ ri[ i-1, ], ] * a; 
   end;

   c = ap[ ri[ 3, ], ];  /* set c to to a**3 */

Hope that helps.

 

ambalath1
Calcite | Level 5
Thank you
Ksharp
Super User
Do You want create many Matrix A2-A60 to represent that calculation?
proc iml;
call randseed(1234);
A=j(4,4);
call randgen(A,'uniform');
x={'A'}+left(char(2:60));
B=A;
do i=1 to 59;
 B=B*A;
 call valset(x[i],B);
end;




print A;
do i=1 to 59;
 temp=value(x[i]);
 label=x[i];
 print temp[l=label];
end;
quit;



Rick_SAS
SAS Super FREQ

The use of the VALSET and VALUE functions are described in the article "Indirect assignment."  However, it is rare to need to use this technique. Almost always, you can avoid indirect assignment and use the matrices inside the DO loop without saving them into 60 different named matrices.

 

What are you trying to acheive? That would help us know what programming technique to suggest.

ambalath1
Calcite | Level 5

Thank you Rick,

 

This is what I am trying to accomplish

I have two matrices
P=mXm
V=mX1
and a saclar value m

I am looking for a sas iml code to do the following

Run a loop for 10 times say, to get the final result of F.
where P=P at the befining for second run it should be p=P*P, and at the end (for 10) is should be
p=p*p*p*p*p*p*p*p*p*p

C=P*v;
D=V+C;
F=D*m;

 

 

Rick_SAS
SAS Super FREQ

As I said when you asked this question on my blog, initialize the result matrix (P) to the identity matrix. Then loop, each time multiplying the current value of P times the original matrix.  The other computations just "go along for the ride."

 

proc iml;
X = {1 2 3,
    -1 0 1,
     2 1 0};
V = {-1, 0, 1};

m = nrow(V);
P = I(m);
do i = 1 to 10;
   P = P*X;
   C = P*V;
   D = V+C;
   F = D*m;
end;
/* at end of loop, P = X**10 */
print P C D F;
ambalath1
Calcite | Level 5
Thank you Rick!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 10 replies
  • 1585 views
  • 0 likes
  • 4 in conversation