HP Forums

Full Version: (42S) Matrix Integer Powers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
Very nicely done - & you forgot to say significantly faster. Bravo!
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
Reference URL's