Post Reply 
(42S) Matrix Integer Powers
02-15-2015, 07:35 AM (This post was last modified: 06-15-2017 01:48 PM by Gene.)
Post: #1
(42S) Matrix Integer Powers
A surprising omission on the 42S is the lack of a matrix powering function.

These two programmes supply the lack.

IDN for positive integer input returns the identity matrix of input dimension.

0. { 26-Byte Prgm }
1. LBL “IDN”
2. ENTER
3. NEWMAT
4. EDIT
5. LBL 00
6. SIGN
7. →
8. FC? 76
9. ↓
10. FC? 76
11. GTO 00
12. EXITALL
13. END

For stack

Y: Matrix

X: Integer

MAT↑ returns the input matrix to input power.

0. { 61-Byte Prgm }
1. LBL “MAT↑”
2. X>0?
3. GTO 03
4. X<>Y
5. INVRT
6. X<>Y
7. ABS
8. LBL 03
9. STO 02
10. X<>Y
11. ENTER
12. DIM?
13. R↓
14. XEQ “IDN”
15. GTO 00
16. LBL 01
17. 2
18. MOD
19. X≠0?
20. GTO 02
21. LASTX
22. STO/ 02
23. R↑
24. RCL* ST X
25. R↑
26. RCL 02
27. GTO 01
28. LBL 02
29. STO- 02
30. R↓
31. RCL* ST Y
32. LBL 00
33. RCL 02
34. X≠0?
35. GTO 01
36. R↓
37. END
Find all posts by this user
Quote this message in a reply
08-04-2023, 02:45 PM
Post: #2
RE: (42S) Matrix Integer Powers
All in the stack, integrated IDN, and avoiding a multiplication by the unit matrix:

Code:
00 { 66-Byte Prgm }
01▸LBL "MPOW"
02 X≠0?
03 GTO 00
04 ×
05 EDIT
06▸LBL 02
07 SIGN
08 →
09 X=0?
10 I+
11 X=0?
12 GTO 02
13 EXITALL
14 RTN
15▸LBL 00
16 X>0?
17 GTO 00
18 +/-
19 X<>Y
20 INVRT
21 X<>Y
22▸LBL 00
23 SIGN
24 LASTX
25▸LBL 03 @ n R A
26 2
27 ÷
28 ENTER
29 FP
30 X=0?
31 GTO 00
32 R^
33 STO× ST T @ R := R*A;
34 R↓
35 X=Y?
36 GTO 01
37▸LBL 00
38 R^
39 STO× ST X @ A := A*A;
40 R↓
41 -
42 GTO 03
43▸LBL 01
44 X<> ST Z
45 END

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
08-05-2023, 03:22 PM
Post: #3
RE: (42S) Matrix Integer Powers
Very nicely done - & you forgot to say significantly faster. Bravo!
Find all posts by this user
Quote this message in a reply
08-06-2023, 08:02 AM
Post: #4
RE: (42S) Matrix Integer Powers
Actually, in this case I merely reworked your programs:
- I used your IDN, with shorter tests
- the exit test needs only to be performed when FP(n/2)=0, which was not the case in my original version ;-)
Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)