Post Reply 
Algebraic to RPN
02-24-2017, 09:27 PM (This post was last modified: 02-25-2017 03:10 PM by Han.)
Post: #1
Algebraic to RPN
The code below takes an algebraic expression and converts it to an RPN string. The program itself is not all that useful (in my opinion) but it hopefully provides to those interested in programming expressions a bit of insight into how expressions are built.

For example, in the CAS view, type:

CRPN(x^2-4*x)

to obtain "x 2 ^ 4 x * - +"

EDIT: Joe Horn pointed out to me that the "+" appeared extraneous -- which was also my initial reaction. As it turns out, the CAS treats subtraction as a sequence of negation followed by addition. So the "-" operator is actually the negation operator, which can be verified by typing f:=neg(x) in the CAS view, and extracting the operation using f[1]. I never really gave this much thought, but it is neat to see how expressions are handled on a lower level.

Code:
#pragma mode( separator(.,;) integer(h32) )
#cas
CRPN(f):=
begin
  local rpn, op, par, n, j, g;

  rpn:="";
  op:="";
  par:="";
  if (type(f) <> DOM_SYMBOLIC) then
    rpn:=string(f);
    return(rpn);
  end;

  op:=string(f[1]);
  n:=size(op);
  op:=MID(op,2,n-2);
  n:=dim(f) + 1;

  for j from 2 to n do
    g:=f[j];
    if (type(g) <> DOM_SYMBOLIC) then
      par:=string(g);
    else
      par:=CRPN(g);
    end;
    if (j > 2) then rpn:=rpn + " "; end;
    rpn:=rpn + par;
    if ((j > 2) or (n == 2)) then
      rpn:=rpn + " " + op;
    end;
  end;

  return(rpn);
end;
#end

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-25-2017, 12:19 PM
Post: #2
RE: Algebraic to RPN
Hello Han,

I appriciate many your program. I have the same from Wiiliam C WICKES from
HP 48 INSIGHTS (part II page 534) from 1992.

I was failure to translate to HP PRIME and you give me !

Thank again.

Gérard.
Find all posts by this user
Quote this message in a reply
02-25-2017, 02:05 PM
Post: #3
RE: Algebraic to RPN
extract parts and operators with [] acts as the PART command, please create a second version with PART cmd

PHP Code:
(x^2-4*x)[1→ '+'
(x^2-4*x)[2→ x^2
(x^2-4*x)[3→ -4*x

part
(x^2-4*x→ 2
part
(x^2-4*x,1→ x^2
part
(x^2-4*x,2→ -4*x
part
(x^2-4*x,0→ "+" 
Find all posts by this user
Quote this message in a reply
02-25-2017, 03:15 PM
Post: #4
RE: Algebraic to RPN
(02-25-2017 02:05 PM)compsystems Wrote:  extract parts and operators with [] acts as the PART command, please create a second version with PART cmd

PHP Code:
(x^2-4*x)[1→ '+'
(x^2-4*x)[2→ x^2
(x^2-4*x)[3→ -4*x

part
(x^2-4*x→ 2
part
(x^2-4*x,1→ x^2
part
(x^2-4*x,2→ -4*x
part
(x^2-4*x,0→ "+" 

I am not sure why you would want a version using the part command, but here:

Code:
#pragma mode( separator(.,;) integer(h32) )
#cas
CRPN(f):=
begin
  local rpn, op, par, n, j, g;

  rpn:="";
  op:="";
  par:="";
  if (type(f) <> DOM_SYMBOLIC) then
    rpn:=string(f);
    return(rpn);
  end;

  op:=string(part(f,0));
  n:=size(op);
  op:=MID(op,2,n-2);
  n:=part(f);

  for j from 1 to n do
    g:=part(f,j);
    if (type(g) <> DOM_SYMBOLIC) then
      par:=string(g);
    else
      par:=CRPN(g);
    end;
    if (j > 1) then rpn:=rpn + " "; end;
    rpn:=rpn + par;
    if ((j > 1) or (n == 1)) then
      rpn:=rpn + " " + op;
    end;
  end;

  return(rpn);
end;
#end

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-26-2017, 02:59 PM (This post was last modified: 03-02-2018 01:38 PM by compsystems.)
Post: #5
RE: Algebraic to RPN
userrpl

PHP Code:
« 0 0 { } -> EXPRESSION XPART NPARTS OPERATOR OBJECTS
  « EXPRESSION
    IFERR OBJ
->
    
THEN ->STR 'OPERATOR' STO 0 'NPARTS' STO OPERATOR 1 ->LIST 'OBJECTS' STO
    
ELSE ->STR 'OPERATOR' STO 'NPARTS' STO NPARTS ->LIST 'OBJECTS' STO NPARTS OPERATOR OBJECTS 3 ->LIST DROP
    END
      
IF XPART NPARTS <= XPART ->= AND NOT
      THEN 
"EXPRESSION " EXPRESSION " CONTAINS ONLY " NPARTS " PARTS" +
      ELSE
        IF 
XPART 0 ==
        
THEN OPERATOR
        
ELSE
          IF 
XPART -==
          
THEN NPARTS
          
ELSE OBJECTS XPART GET
          END
        END
      END
  »
»
'PART' STO 


Hi. The code with PART CMD, is simplified to the following, also rename some variables to auto-document the code.

PHP Code:
#pragma mode( separator(.,;) integer(h32) )
#cas
    
convToRPN(expr1):=
    
begin
      local rpnStr
operpartnpartspartJexpr2;

      
rpnStr:="";
      
oper:="";
      
partn:="";
      if (
type(expr1) <> DOM_SYMBOLICthen
        rpnStr
:=string(expr1);
        return(
rpnStr);
      
end;
      
expr1:=(x^2-4*x);
      
oper:=string(expr1[1]);  
      
parts:=size(oper);  
      
oper:=MID(oper,2,parts-2);  
      
parts:=dim(expr1) + 1;

      for 
partJ from 2 to parts do
        
expr2:=expr1[partJ];
        if (
type(expr2) <> DOM_SYMBOLICthen
          partn
:=string(expr2);
        else
          
partn:=convToRPN(expr2);
        
end;
        if (
partJ 2then 
          rpnStr
:=rpnStr " "
        
end;
        
rpnStr:=rpnStr partn;
        if ((
partJ 2) or (parts == 2)) then
          rpnStr
:=rpnStr " " oper;
        
end;
      
end;

      return(
rpnStr);
    
end;
#end

//(x^2-4*x)[1] -> '+'
//(x^2-4*x)[2] -> x^2
//(x^2-4*x)[3] -> -4*x


#pragma mode( separator(.,;) integer(h32) )
#cas
    
convToRPN2(expr1):=
    
begin
      local rpnStr
operpartnpartspartJexpr2;
      
      
rpnStr:="";
      
oper:="";
      
partn:="";
      if (
type(expr1) <> DOM_SYMBOLICthen
        rpnStr
:=string(expr1);
        return(
rpnStr);
      
end;
      
oper:=part(expr1,0);
      
parts:=part(expr1);

      for 
partJ from 1 to parts do
        
expr2:=part(expr1,partJ);
        if (
type(expr2) <> DOM_SYMBOLICthen
          partn
:=string(expr2);
        else
          
partn:=convToRPN2(expr2);
        
end;
        if (
partJ 1then 
          rpnStr
:=rpnStr " "
        
end;
        
rpnStr:=rpnStr partn;
        if ((
partJ 1) or (parts == 1)) then
          rpnStr
:=rpnStr " " oper;
        
end;
      
end;

      return(
rpnStr);
    
end;
#end 

convToRPN2(x^3); returns "x 3 ^"


///////////////

Running step by step convToRPN2(x^3);, for those who want to see the conversion of x^3 to RPN
more info on RPN
https://en.wikipedia.org/wiki/Reverse_Polish_notation

PHP Code:
expr1:=x^3;
rpnStr:="";
oper:="";
partn:="";
oper:=part(expr1,0); //returns "^"
parts:=part(expr1); //returns 2
partJ:=1;
expr2:=part(expr1,partJ); //returns x
partn:=string(expr2); //returns "x"
rpnStr:=rpnStr partn// returns "x"
partJ:=2;
expr2:=part(expr1,partJ); //returns 3
partn:=string(expr2); //returns "3"
rpnStr:=rpnStr " "//returns "x "
rpnStr:=rpnStr partn//returns "x 3"
rpnStr:=rpnStr " " oper//returns "x 3 ^" 

Or in a list of steps, without comments to copy and paste them in the history view

PHP Code:
expr1:=x^3rpnStr:=""oper:=""partn:=""oper:=part(expr1,0); parts:=part(expr1); partJ:=1expr2:=part(expr1,partJ); partn:=string(expr2); rpnStr:=rpnStr partnpartJ:=2expr2:=part(expr1,partJ); partn:=string(expr2); rpnStr:=rpnStr " ";rpnStr:=rpnStr partnrpnStr:=rpnStr " " oper

returns

PHP Code:
x^3,"","","","^",2,1,x,"x","x",2,3,"3","x ","x 3","x 3 ^"

[Image: hp_prime_convertToRPN_image00.png]
Find all posts by this user
Quote this message in a reply
07-28-2018, 08:33 PM
Post: #6
RE: Algebraic to RPN
Thanks for this useful program.

I’m learning lots of things.

Guy R. KOMAN, hp 50G, hp Prime Rev. C
Find all posts by this user
Quote this message in a reply
07-29-2018, 07:46 AM
Post: #7
RE: Algebraic to RPN
@compsystems - nice skin on your virtual Prime - is that available for download anywhere?
Find all posts by this user
Quote this message in a reply
07-29-2018, 12:10 PM
Post: #8
RE: Algebraic to RPN
(07-29-2018 07:46 AM)tcab Wrote:  @compsystems - nice skin on your virtual Prime - is that available for download anywhere?

It's here: http://www.hpmuseum.org/forum/thread-762...l#pid66998

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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