Algebraic to RPN
02-26-2017, 02:59 PM (This post was last modified: 03-02-2018 01:38 PM by compsystems.)
Post: #5
 compsystems Senior Member Posts: 1,342 Joined: Dec 2013
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 -1 >= AND NOT      THEN "EXPRESSION " EXPRESSION + " CONTAINS ONLY " + NPARTS + " PARTS" +      ELSE        IF XPART 0 ==        THEN OPERATOR        ELSE          IF XPART -1 ==          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, oper, partn, parts, partJ, expr2;      rpnStr:="";      oper:="";      partn:="";      if (type(expr1) <> DOM_SYMBOLIC) then        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_SYMBOLIC) then          partn:=string(expr2);        else          partn:=convToRPN(expr2);        end;        if (partJ > 2) then           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, oper, partn, parts, partJ, expr2;            rpnStr:="";      oper:="";      partn:="";      if (type(expr1) <> DOM_SYMBOLIC) then        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_SYMBOLIC) then          partn:=string(expr2);        else          partn:=convToRPN2(expr2);        end;        if (partJ > 1) then           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
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 2partJ:=1;expr2:=part(expr1,partJ); //returns xpartn:=string(expr2); //returns "x"rpnStr:=rpnStr + partn; // returns "x"partJ:=2;expr2:=part(expr1,partJ); //returns 3partn:=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^3; rpnStr:=""; oper:=""; partn:=""; oper:=part(expr1,0); parts:=part(expr1); partJ:=1; expr2:=part(expr1,partJ); partn:=string(expr2); rpnStr:=rpnStr + partn; partJ:=2; expr2:=part(expr1,partJ); partn:=string(expr2); rpnStr:=rpnStr + " ";rpnStr:=rpnStr + partn; rpnStr:=rpnStr + " " + oper;

returns

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

 « Next Oldest | Next Newest »

 Messages In This Thread Algebraic to RPN - Han - 02-24-2017, 09:27 PM RE: Algebraic to RPN - ggauny@live.fr - 02-25-2017, 12:19 PM RE: Algebraic to RPN - compsystems - 02-25-2017, 02:05 PM RE: Algebraic to RPN - Han - 02-25-2017, 03:15 PM RE: Algebraic to RPN - compsystems - 02-26-2017 02:59 PM RE: Algebraic to RPN - tcab - 07-29-2018, 07:46 AM RE: Algebraic to RPN - Joe Horn - 07-29-2018, 12:10 PM RE: Algebraic to RPN - Komanguy - 07-28-2018, 08:33 PM

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