Post Reply 
Python to FOCAL Compiler
10-06-2015, 08:19 PM (This post was last modified: 10-07-2015 01:41 AM by Gerson W. Barbosa.)
Post: #8
RE: Python to FOCAL Compiler
(10-06-2015 07:11 PM)Thomas Klemm Wrote:  [quote='Gerson W. Barbosa' pid='43596' dateline='1444147083']
I don't mean to hijack this thread either, but since you've talked about elliptic integrals and you appear to be in the mood, what about converting the fast algorithm presented by Hugh Steers here to RPN?

Here's a Python program based on Hugh's implementation:

Code:
def ellipse(a, b):
    u = 1
    v = b / a
    s = (1 + v**2)/2
    t = 1
    while 0 == 0:
        m = (u + v)/2
        if m == u:
            break
        w = (u - v)/2
        v = sqrt(u * v)
        u = m
        s = s - t * w**2
        t = t * 2
    return 2 * pi * a * s / u

And that's the generated FOCAL program using 113 bytes:

Code:
LBL "ELLIPSE"
STO 01 ; b
RDN
STO 00 ; a
RDN
1
STO 02 ; u
RDN
RCL 01 ; b
RCL 00 ; a
/
STO 03 ; v
RDN
1
RCL 03 ; v
2
Y↑X
+
2
/
STO 04 ; s
RDN
1
STO 05 ; t
RDN
LBL 00
0
0
X#Y?
GTO 02
RCL 02 ; u
RCL 03 ; v
+
2
/
STO 06 ; m
RDN
RCL 06 ; m
RCL 02 ; u
X#Y?
GTO 01
GTO 03 ; break
GTO 01
LBL 01
RCL 02 ; u
RCL 03 ; v
-
2
/
STO 07 ; w
RDN
RCL 02 ; u
RCL 03 ; v
*
SQRT
STO 03 ; v
RDN
RCL 06 ; m
STO 02 ; u
RDN
RCL 04 ; s
RCL 05 ; t
RCL 07 ; w
2
Y↑X
*
-
STO 04 ; s
RDN
RCL 05 ; t
2
*
STO 05 ; t
RDN
GTO 00
LBL 02
LBL 03
2
PI
*
RCL 00 ; a
*
RCL 04 ; s
*
RCL 02 ; u
/
RTN

But we can easily bring this down to 77 bytes:

Code:
LBL "ELLIPSE"
STO 01 ; b
RDN
STO 00 ; a
1
STO 02 ; u
STO 05 ; t
RCL 01 ; b
RCL 00 ; a
/
STO 03 ; v
X↑2
+
2
/
STO 04 ; s
LBL 00
RCL 02 ; u
RCL 03 ; v
+
2
/
STO 06 ; m
RCL 02 ; u
X=Y?
GTO 03 ; break
RCL 02 ; u
RCL 03 ; v
-
2
/
STO 07 ; w
RCL 02 ; u
RCL 03 ; v
*
SQRT
STO 03 ; v
RCL 06 ; m
STO 02 ; u
RCL 05 ; t
RCL 07 ; w
X↑2
*
ST- 04 ; s
2
ST* 05 ; t
GTO 00
LBL 03
2
PI
*
RCL 00 ; a
*
RCL 04 ; s
*
RCL 02 ; u
/
RTN

Thank you very much for the HP-41 programs!

As a compensation for my laziness, here is a 163-byte RPL stack-only version of the Pascal program, if you don't mind the off-topic content:

Code:

%%HP: T(3)A(D)F(.);
\<< OVER / DUP SQ 1. + 2. / 1. ROT 1.
  DO DUP2 DUP2 * \v/ UNROT - 2. / 4. ROLLD UNROT + 2. / 4. ROLL 4. ROLL DUP2 SQ * NEG 6. ROLL + ROT DUP + SWAP 5. ROLLD 4. ROLLD ABS
  UNTIL .00000000001 <
  END UNROT DROP2 / * DUP + \pi *
\>>

RPL masters might shorten this a bit and do more elegant stack manipulations. It's not an exact implementation of Hugh's algorithm because there is no break from RPL structures.


Best regards,

Gerson .

P.S.: Just tested your second program on my HP-41CV (because it's shorter). Very fast! (less than 5 seconds!). Thanks again!

Edited to add a missing s lest I am given a free grammar lesson :-)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Python to FOCAL Compiler - Thomas Klemm - 10-05-2015, 10:58 PM
RE: Python to FOCAL Compiler - Sukiari - 10-06-2015, 03:36 AM
RE: Python to FOCAL Compiler - Gerson W. Barbosa - 10-06-2015 08:19 PM
Java to FOCAL Compiler - Thomas Klemm - 10-07-2015, 04:38 PM



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