Matrix multiplication

Solved
Occasional Contributor
Posts: 5

Matrix multiplication

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 !

Accepted Solutions
Solution
‎04-26-2016 04:02 PM
SAS Super FREQ
Posts: 4,171

Re: Matrix multiplication

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;
``````

All Replies
Regular Contributor
Posts: 162

Re: Matrix multiplication

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?

Occasional Contributor
Posts: 5

Re: Matrix multiplication

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.

Regular Contributor
Posts: 162

Re: Matrix multiplication

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...

Regular Contributor
Posts: 162

Re: Matrix multiplication

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.

Occasional Contributor
Posts: 5

Thank you
Super User
Posts: 10,681

Re: Matrix multiplication

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;

```
SAS Super FREQ
Posts: 4,171

Re: Matrix multiplication

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.

Occasional Contributor
Posts: 5

Re: Matrix multiplication

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;

Solution
‎04-26-2016 04:02 PM
SAS Super FREQ
Posts: 4,171

Re: Matrix multiplication

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;
``````
Occasional Contributor
Posts: 5

Re: Matrix multiplication

Thank you Rick!
🔒 This topic is solved and locked.