Hi Gil,
I corrected some bad logic for small values and also done some time-optimizing
For convergency test 1e-16, i.e. about 16 correct digits:
DM42 on USB calculates the example in 0.67 s
DM42 on battery 1.76 s
Actually setting convergency test to 1e-12, and R03=8, I can run calculations in 0.6 s on USB power and get 12 correct digits
I think it is worth the increased program size.
Code:
@ Matrix exponential, square matrix
@ calculation with argument reduction and series expansion
@ convergence check uses RNRM difference between iterations,
@ uses R00-R03
@ R00: counter, number of iterations
@ R01: convergence limit, set at line 37, typical R01=1e-16 to R01=1e-32
@ R02: number of halvings and squarings used for argument reduction
@ R03: number of initial iterations before convergence check (saves time),
@ suggest R03=10 when R01=1e-16
@ suggest R03=20 when R01=1e-32
@ Optional CF 24, CF 25 for overflow check
00 { 121-Byte Prgm }
01▸LBL "MEXP"
02 FUNC 11 @ 1 argument required, square matrix
03 L4STK
04 10
05 STO 03
06 CLX
07 2
08 STO 00
09 RCL ST Y @ calculate and determine any argument reduction
10 FNRM
11 LN
12 RCL 00
13 LN
14 ÷
15 IP
16 3 @ reduce the argument a little more
17 +
18 1
19 STO 01
20 +
21 X<0?
22 CLX @ if negative, don't use
23 STO 02
24 Y↑X
25 ÷ @ reduce argument
26 ENTER
27 ENTER
28 EDIT @ add the identity matrix directly
29 ↑
30▸LBL 01
31 ↓
32 RCL+ 01
33 →
34 FC? 77
35 GTO 01
36 EXITALL
37 1ᴇ-16 @ convergence crtiteria
38 STO 01
39 R↓
40▸LBL 02 @ main iteration
41 X<>Y
42 RCL÷ 00
43 ISG 00
44 NOP
45 RCL× ST Z
46 X<>Y
47 RCL+ ST Y
48 DSE 03
49 GTO 02
50 STO- ST L
51 LASTX
52 RNRM
53 RCL- 01
54 R↓
55 0<? ST T @ convergence test
56 GTO 02 @ repeat
57 0=? 02 @ skip if no argument reduction
58 GTO 04
59▸LBL 03 @ argument back squaring
60 STO× ST X
61 DSE 02
62 GTO 03
63▸LBL 04
64 FNRM @ check for /force "out of Range" error on DM42 v. Free 3.0.15
65 LASTX @ delete line 64,65 for later revisions
66 END
Best regards
Gjermund