Post Reply 
from hp50g expression 2 latex
06-11-2015, 08:13 PM (This post was last modified: 06-11-2015 09:53 PM by peacecalc.)
Post: #8
RE: from hp50g expression 2 latex
Hello 50g freaks,

as I tried to program for example the integral from RPL to TeX I failed with my serial approach. Further analysis shows me that there a two kind of functions such like "+" with the operands surround and like int(...) with the arguments one after the other.
Similar to TeX. So I tried a second time the recursive approach (by the way, there is no need anymore for the little programs with the MATCH command, so it is really faster).

I made two programs \->HP2TEX and LOOKUP and a nested list TXPAT for the patterns.
\->HP2TEX is the main program
LOOKUP is the helper for searching in TXPAT.

\->HP2TEX has some restrictions:

a) No conversion from caps to small letters
b) If LOOKUP find no pattern in TXPAT it returns 0, this case will not be captured (means you get an error the program abort).

First the content of TXPAT every sublist has same structure:
Let us say you have RPL XROT (Read please the bigger italc comment further on!) command:
This function has two arguments -> sublist has four entries:
{ "XROT" "\\sqrt[" "]{" "}" } the double backslash is necessary otherwise you get no backslash. A function with four arguments has six entries and so on.
The advantage you can add simply rules as many you like. The counting is is fix if you needn't an element you can take "" (kind of nullstring) that doesn't change the output string, but the program works properly.

Code:
%%HP: T(3)A(R)F(,);
{ { "+" "" "+" "" } 
{ "-" "" "-" "" } 
{ "*" "" "\\cdot " "" }
{ "/" "\\frac{" "}{" "}" } 
{ "\->K" "\\left(" "\\right)" } 
{ "\.S" "\\int_{" "}^{" "}" "d" "" } 
{ "\v/" "\\sqrt{" "}" } 
{ "XROT" "\\sqrt[" "]{" "}" } 
{ "^" "{" "}^{" "}" } }

EDIT: XROOT decomposes in a inverse order like other function with two arguments: XROOT(A;B) OBJ\-> you get: B A 2, XROOT instead of
A B 2, XROOT so the translation rule fails.

My workaround is, if you have an algebraic expression with XROOT you use a little program with MATCH to convert 'XROOT(A;B)' into 'XROT(A;B)' (this function mustn't exist, but 'XROT(A;B)' decomposes into A B 2, XROT, so the translation rule works.


Now the code for LOOKUP:

Code:
%%HP: T(3)A(R)F(,);
\<< TXPAT SIZE 1, + \-> SGN NU
  \<< 0,
      DO 
      DROP 'NU' DECR       @@Searching is from end
      UNTIL                    @@to the beginning of the
            IF 0, ==           @@list
            THEN DROP 0, 1,
            ELSE TXPAT NU GET 
                 DUP HEAD SGN ==
            END
      END
  \>>
\>>

And now the main program \->HP2TEX

Code:
%%HP: T(3)A(R)F(,);
\<< OBJ\->                          @@Destruct object
    IF OVER                         
    THEN \-> N F                    @@Store fname 
                                    @@and number of arguments
         \<< F N 1, + ROLLD         @@store fname before arguments
                                    @@on stack 
             1, N FOR J
                  IF DUP TYPE 9, == @@loop for analysing 
                                    @@every argument
                  THEN \->HP2TX     @@if algebraic object
                                    @@recursiv calling of
                                    @@\->HP2TX
                  END 

                  DUP               @@argument or last
                                    @@output of \->HP2TX     
                  IF TYPE DUP       @@local or global
                                    @@name, if true
                     6, == SWAP 
                     7, == OR
                  THEN \->STR DUP   @@convert to a string
                       SIZE 1, - 2, @@and remove algebraic 
                       SWAP SUB     @@signs
                  ELSE \->STR       @@numbers becomes 
                  END               @@a string

                  N ROLLD           @@Roll down next
                  NEXT              @@argument

             N 1, + ROLL DUP        @@self defined functions    
             IF TYPE DUP            @@are from type
                6, == SWAP          @@local or global       
                7, == OR            @@name 
             THEN \->STR DUP        @@so they need 
                 SIZE 1, - 2,       @@same procedure 
                 SWAP SUB           @@like above
             ELSE \->STR                
             END 

             LOOKUP \-> RULE        @@at that position
                                    @@stack line 1
                                    @@is the fname as
                                    @@a string
                                    @@lookup is a program
                                    @@which searches in
                                    @@the global variable
                                    @@texpat for a translation-
                                    @@pattern from RPL to
                                    @@TeX
                                    @@the rule found is
                                    @@stored in the local
                                    @@RULE        
                    \<< RULE TAIL   @@the first entry of RULE
                                    @@is the RPL fname, no need
                                    @@any more 
                        'RULE' STO 
                        RULE HEAD       @@next entry is the beginning
                                          @@of the TeX command 
                        1, N FOR J        @@loop for alternating compo-
                             RULE TAIL   @@sition of argument and
                             'RULE' STO  @@syntax of TeX command 
                             N J - 2, + ROLL 
                             + RULE HEAD +
                             NEXT
                    \>>
         \>>
  END

DUP 'OUT' STO                       @@display result as string and store
\>>                                 @@and store it in global OUT

input: '\.S(1;2;\->K(A+B)^3;B)'
output:\[\int_{1}^{2}{\left(A+B\right)}^{3}dB\]

the function \->K produces only the parenthesis in TeX (in RPL there is no need for this ;-)) )
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
from hp50g expression 2 latex - peacecalc - 05-23-2015, 01:35 PM
RE: from hp50g expression 2 latex - Gilles - 05-27-2015, 09:24 PM
RE: from hp50g expression 2 latex - peacecalc - 06-11-2015 08:13 PM
RE: from hp50g expression 2 latex - pier4r - 03-25-2017, 09:51 PM
RE: from hp50g expression 2 latex - pier4r - 02-19-2017, 07:59 PM



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