%%HP: T(3)A(D)F(.);
\<< CLLCD
    @
    "HP 48G(X) RIEMANN SUMS" 2 DISP
    @
    "   Copyright (c) 1995"  4 DISP
    "     by Jack Courtney"  5 DISP
    "   All Rights Reserved" 6 DISP
    @
    @ Calculate a Riemann sum for an arbitrary function, over an arbitrary real interval, 
    @   by using an arbitrary number of subintervals.
    @
    @  Varieties of sums available: left hand
    @                               right hand
    @                               trapezoidal
    1 @ 1/0: plotting is on/off
    { "Left Hand"
      "Right Hand" 
      "Trapezoidal" 
    @ "Maximal"
    @ "Minimal" 
    }
    \-> plot choices
    \<< 0
        WHILE 0 ==
        REPEAT
            440 0.01 BEEP 
            "CHOOSE SUM TYPE" choices 1 CHOOSE
        END
        choices OVER POS 
        \-> choice
        \<< " Sum" +              @ Push the INFORM title onto the stack
            VARS 'EQ' POS         @ Push the list of INFORM rset values onto the stack
            IF 0 ==
            THEN {NOVAL}
            ELSE RCEQ 
            END
            VARS 'PPAR' POS       
            IF 0 ==
            THEN {NOVAL 10 NOVAL NOVAL}
            ELSE PPAR 3 GET
                 10
                 PPAR 1 GET RE
                 PPAR 2 GET RE
                 4 \->LIST
            END
            +
            \-> title rset       
            \<< 0
                WHILE 0 ==
                REPEAT
                    title                                                   @ Title
                    { { "EQ:" 	 "ENTER INTEGRAND"                  6 8 9 } 
                      {                                                   } @ Large entry field
                      { "Indep:" "ENTER INTEGRATION VARIABLE NAME"    6 9 } @ Small entry field
                      { "Subs:"  "ENTER INTEG. VAR. SAMPLE COUNT"       0 } @ Small entry field
                      { "H-Min:" "ENTER MIN. VALUE FOR INTEG. VAR."     0 } @ Small entry field
                      { "H-Max:" "ENTER MAX. VALUE FOR INTEG. VAR."     0 } @ Small entry field
                    }                                                                             
                    { 2 0 }						  @ {cols,tabs}
                    rset                          @ rset values
                    VARS 'RPAR' POS               @ init values
                    IF 0 == 
                    THEN rset
                    ELSE RPAR
                    END
                    440 0.01 BEEP 
                    IFERR
                        INFORM
                    THEN
                        "Illegal values in EQ, PPAR or RPAR. Please purge these vars." MSGBOX 
                        5 DROPN KILL
                    END
                    SWAP                          @ SAVE rc
                    DUP 'RPAR' STO                @ Output is RPAR
                    LIST\-> DROP
                    DUP2                          @ Test Hmin and Hmax
                    IF \>= 
                    THEN
                        CLEAR 0                   @ Change rc to 0
                        "ERROR: H-max >= H-min" MSGBOX
                    ELSE
                        DUP2 IF > THEN SWAP END XRNG  @ Set XRNG in PPAR
                        DROP INDEP                    @ Set Indep in PPAR
                        'EQ' STO                      @ Set EQ
                    END
                END
            \>>
            IF plot
            THEN
                ERASE
                FUNCTION
                AUTO
                @ Make certain that the horizontal axis 
                @ is within the viewing window.
                'PPAR' 1 GETI IM 3 ROLLD GETI IM 3 ROLLD DROP2 
                \-> y0 y1
                \<< y0 y1 *
                    IF 0 > 
                    THEN
                        IF y0 0 > 
                        THEN
                            0 y1 YRNG 
                        ELSE
                            y0 0 YRNG
                        END
                    END
                \>>
                DRAX
                DRAW
            END
            RPAR TAIL LIST\-> DROP                          @ x n a b
            IF 'choice==2' THEN SWAP END                    @ (RH sum?) Work from right to left
            \-> x n a b
            \<< 1 n 1 -                                     @ 1 n-1
                b a - n / \->NUM                            @ 1 n-1 h
                0 0                                         @ 1 n-1 h xL yL
                IFERR                                       @ Flag illegal values
                    a RCEQ x 3 PICK 2 \->LIST | \->NUM      @ 1 n-1 h xL yL xR yR
                    DUP TYPE IF 1 == THEN DOERR END         @ A complex value is an error
                THEN
                    7 DROPN
                    7 FREEZE
                    "Integrand is complex or undefined at " 
                    x \->STR + " = " + a \->STR + MSGBOX
                    KILL
                END
                CASE                                        @ Push the initial sum onto the stack
                    'choice==1 OR 
                     choice==2' THEN DUP END                @ xR yR sum
                    'choice==3' THEN DUP 
                                     IFERR 
                                         RCEQ x b 2 \->LIST | \->NUM
                                         DUP TYPE IF 1 == THEN DOERR END
                                     THEN
                                         8 DROPN
                                         7 FREEZE
                                         "Integrand is complex or undefined at "
                                         x \->STR + " = " + b \->STR + MSGBOX
                                         KILL
                                     END 
                                     + 2 / 
                                END
                @   'choice==4' THEN  END
                @   'choice==5' THEN  END
                    "ERROR!: Unknown sum type!" MSGBOX 7 DROPN KILL @ Flag an illegal choice
                END
                \-> h xL yL xR yR sum
                \<< START
                        IF plot
                        THEN
                            xR 'xL' STO
                            yR 'yL' STO
                        END
                        h 'xR' STO+                                        @ Update xR
                        IFERR
                            RCEQ x xR 2 \->LIST | \->NUM DUP 'yR' STO      @ Update yR
                            DUP TYPE IF 1 == THEN DOERR END                @ A complex value is an error
                        THEN
                            DROP
                            7 FREEZE
                            "Integrand is complex or undefined at " 
                            x \->STR + " = " + xR \->STR + MSGBOX
                            KILL
                        END                     
                        'sum' STO+                                         @ Update sum
                        IF plot 
                        THEN
                            CASE
                                '(choice==1) OR (choice==2)'
                                    THEN
                                        xL 0 R\->C xR yL R\->C BOX
                                    END
                                'choice==3'
                                    THEN
                                        xL yL R\->C xL 0 R\->C OVER LINE
                                        xR yR R\->C xR 0 R\->C OVER LINE LINE
                                    END
                            @   'choice==4' THEN  END
                            @   'choice==5' THEN  END
                            @   Error trap is unnecessary here, since earlier trap is sufficient.                                              
                            END
                        END
                    NEXT
                    sum h ABS * DUP
                    choices choice GET " Sum" + \->TAG      @ Push tagged result onto stack
                    SWAP
                    IF plot                                 @ Draw the last strip
                    THEN
                        xR 'xL' STO
                        yR 'yL' STO
                        b  'xR' STO
                        IFERR
                            RCEQ x xR 2 \->LIST | \->NUM
                            DUP TYPE IF 1 == THEN DOERR END
                        THEN
                            3 DROPN
                            7 FREEZE
                            "Integrand is complex or undefined at " 
                            x \->STR + " = " + xR \->STR + MSGBOX
                            KILL
                        END
                        'yR' STO
                        CASE
                            '(choice==1) OR (choice==2)'
                                THEN
                                    xL 0 R\->C xR yL R\->C BOX
                                END
                            'choice==3'
                                THEN
                                    xL yL R\->C xL 0 R\->C OVER LINE
                                    xR yR R\->C xR 0 R\->C OVER LINE LINE
                                END
                        @   'choice==4' THEN  END
                        @   'choice==5' THEN  END
                        @   Error trap is unnecessary here, since earlier trap is sufficient.                                              
                        END
                        2 FIX LABEL STD
                        7 FREEZE 
                        2 WAIT
                        "sum" \->TAG \->STR MSGBOX      
                    ELSE
                        DROP
                    END
                \>>
            \>>
        \>>
    \>>
\>>
