(42S) Matrix Integer Powers - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (42S) Matrix Integer Powers (/thread-3096.html) |
(42S) Matrix Integer Powers - Gerald H - 02-15-2015 07:35 AM 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 RE: (42S) Matrix Integer Powers - Werner - 08-04-2023 02:45 PM All in the stack, integrated IDN, and avoiding a multiplication by the unit matrix: Code: 00 { 66-Byte Prgm } Cheers, Werner RE: (42S) Matrix Integer Powers - Gerald H - 08-05-2023 03:22 PM Very nicely done - & you forgot to say significantly faster. Bravo! RE: (42S) Matrix Integer Powers - Werner - 08-06-2023 08:02 AM 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 |