turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-22-2016 08:51 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ambalath1

04-26-2016 02:30 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ambalath1

04-22-2016 10:19 AM

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?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanWakeling

04-22-2016 10:23 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ambalath1

04-22-2016 10:36 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanWakeling

04-22-2016 12:34 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanWakeling

04-22-2016 02:33 PM

Thank you

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ambalath1

04-23-2016 07:29 AM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

04-23-2016 07:36 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

04-26-2016 10:55 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ambalath1

04-26-2016 02:30 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

04-26-2016 04:03 PM

Thank you Rick!