Subject: User Equation Library in User-RPL
From: jhmeyers@miu.edu (John H Meyers)
Date: May 29 1998
Message-ID: <6klpm1$jmo$5@news.iastate.edu>
Reply-To: jhmeyers@miu.edu
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Newsgroups: comp.sys.hp48

Here we go again:

o More features (well, is there anything now left out?)

o Uses more memory (ah, but you can delete anything you don't want :)

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>
-----------------------------------------------------------

%%HP: T(3); @ Multi-level User Equation Library (HP48G/GX)
@ Contributed by John H Meyers.  Version: 1998/05/28

@ Has features resembling the built-in Equation Library,
@ but allows you to supply the equations, etc.

@ This version adds units support, in that it allows you to supply
@ a list of initial values (including units) for all variables;
@ before starting the solver, it will use any such values
@ to create any variables which do not already exist.
@ You would normally supply zero values with units attached,
@ but you can also use this feature to supply any other constants
@ which the equation might utilize.

@ Also includes support for equation title and custom solver menus.

@ If you don't want some of the optional features, e.g. Vars, Pic, etc.,
@ you can delete their programs and custom menu entries, to save memory.

@ You may substitute a full-screen CHOOSE replacement, if you prefer.

@ The entire equation library presented here is a directory [e.g. EQLIB];
@ within this directory, you may create arbitrarily named subdirectories,
@ perhaps each pertaining to a separate topic, as in the built-in EQ LIB.

@ You may even create nested subdirectories, if you wish, or you may elect
@ to have no subdirectories (storing all equations in the main directory).

@ Within each subdirectory, you may create arbitrarily named equations;
@ names up to 14-15 characters will appear in full in the "choose" boxes
@ (longer names will fit if you use any full-screen CHOOSE replacement),
@ and may be mixed upper/lower case letters if you wish.

@ Press CST and then [Menu] or [Up^] to select topics and/or equations.
@ Press CST again, after selecting an equation, for all other features.

@ The correct solver is automatically selected for each equation or set.
@ Use right-shift VIEW to view multiple variable values at once.
@ See User's Guide Chapter 25 for other Multiple Equation Solver features.

@ A "custom" menu (invoked via the [CST] key) is provided,
@ resembling the built-in Equation Library menu:
@
@   [Solv]  Re-start the solver, using the current equation (or set).
@   [EQN]   Recall current equation (or set) to the stack.
@   [Vars]  Display meanings of current equation's variables (optional).
@   [Pic ]  Display current equation's picture, if any.
@   [Menu]  List current subdirectories and/or equations ("choose" box).
@   [Up^]   Up one level (and automatically display its menu).
@   ------  Press NXT for additional options -----------------
@   [MSOL]  Restart M.E.S. without re-initializing.
@   [MSop]  Toggle  M.E.S. option for use of right-shift with var keys.
@   [Purg]  Purge all extraneous variables created from solving equations
@          (purges only numeric and unit-valued variables, plus EQ & Mpar)
@   [CST]   Invoke any custom menu found in the HOME directory.

@ Store this complete directory in HOME, perhaps named 'EQLIB'

DIR @ Directory:  name <object>   name <object> ...

  Sample @ Sample sub-directory of equations:
    DIR
    @ Deeper sub-directories are permitted, if desired.
      Trip 'Dist=Rate*Time' @ Uses standard solver
      Lamp { 'V=I*R' 'W=V^2/R' } @ Uses Multiple-Equation solver
      Prog \<< P Q * 1 - \>> @ Programs may be used as equations
      Stop { }            @ Stops menu listing when encountered
      @ Optional units for Trip:
      TripINIT { 0_mi Dist 0_mph Rate 0_h Time } @ Value VarName ...
      @ Optional units for Lamp:
      LampINIT { 0_V V 0_W W 0_A I 0_\GW R } @ Special char. for Ohms
      @ Optional variable descriptions for Lamp:
      LampVARS { "V: Voltage" "W: Wattage" "I: Current" "R: Resistance" }
      @ Optional Multi-equation title and menu labels:
      LampMITM { "Lamp" { V W "" I R } }
      @ An optional picture may also be stored in LampPICT, etc.
    END

  Set2 DIR END @ Second sub-directory, etc.

  @ If you use a variable hider, you may hide the remaining variables:

  Stop { } @ Stops menu listing in this directory (even without hider).

  Solv @ [Re]start the solver, using the most recent equation (or set):
    \<< RCEQ DROP IFERR MINIT Mitm THEN 30 MENU ELSE MSOLVR END Init \>>
    @ Remove 'Init' and 'Mitm' if you delete the corresponding programs.

  Vars @ Display current equation's variable descriptions, if provided:
       @ (info list for equation 'xxx' must be stored in 'xxxVARS')
    \<< RCEQ DROP 'EQ' RCL DUP "" + "'" ROT + "VARS" + OBJ\->
        DUP VTYPE 5 == @ Is it a list?
        { RCL 1 CHOOSE { DROP } IFT } @ Display the list
        { DROP2 "No Info" DOERR } IFTE \>> @ No list provided

  Pic @ Display current equation's picture, if any
      @ (picture for equation 'xxx' must be stored in 'xxxPICT')
    \<< RCEQ DROP "'" 'EQ' RCL + PICT + OBJ\->
        DUP VTYPE 11 == @ Is it a grob?
        { RCL \->LCD 3 FREEZE } @ Or: { RCL PICT STO { } PVIEW }
        { DROP "No Pic" DOERR } IFTE \>> @ No picture provided

  Menu @ Catalog of directories and/or equations to choose from
       @ (added optional 'name' argument for initial item selection)
    \<< DEPTH { DUP TYPE 6 \=/ } 1 IFTE @ Current DIR or EQ name
        { IFERR RCEQ DROP ::EQ RCL THEN 0 END } IFT @ (if any)
      PATH DUP SIZE GET "" + { 5 8 9 15 } TVARS @ Added lines above
      'Stop' DUP2 + SWAP POS 1 - 1 SWAP SUB @ Stop at 'Stop' variable
      IF DUP SIZE THEN ROT OVER SWAP POS 1 MAX CHOOSE @ Was: THEN 1 CHOOSE
        { DUP VTYPE 15 == { EVAL Menu } { STEQ Solv } IFTE } IFT
      ELSE DROP2 DROP "No Items" DOERR END \>> @ Added DROP

  Purg @ Purge all extraneous variables created from solving equations
       @ (purges only numeric and unit-valued variables, plus EQ & Mpar)
    \<< { 0 13 } TVARS { EQ Mpar } + PURGE \>>

  Init @ Initialize equation variables, if a list is provided
       @ (init list for equation 'xxx' must be stored in 'xxxINIT')
    \<< RCEQ DROP "'" 'EQ' RCL + "INIT" + OBJ\->
        DUP VTYPE 5 == @ Is it a list?
        { RCL 2 :: \<< DUP TYPE 6 == @ Only for non-existent variables
          { DUP #185C7h SYSEVAL VTYPE 0 < { DUP2 STO } IFT } IFT
          @ SysRPL "DoHere:" [supported] ensures var is in current dir
        DROP2 \>> DOSUBS } { DROP } IFTE \>>

  Mitm @ Optional title and menu labels for Multiple Equation Solver
    @ (MITM arg list for equation 'xxx' must be stored in 'xxxMITM')
    @ See User's Guide Ch. 18 to customize single-equation SOLVR menu.
    \<< RCEQ DROP "'" 'EQ' RCL + "MITM" + OBJ\->
        DUP VTYPE 5 == @ Is it a list?
        { RCL OBJ\-> DROP MITM } { DROP } IFTE \>>

  CST @ Indirect pointer, protects CSTM from accidental MENU command.
    CSTM

  CSTM @ Custom menu for User Equation Library (Use it!)

    { @ Protect programs from being stored into:

      { Solv \<< Solv \>> } @ Solver

      { EQN RCEQ } @ Recall current equation

      { Vars \<< Vars \>> } @ Optional variable descriptions

      { Pic  \<< Pic  \>> } @ Optional picture

      { Menu \<< Menu \>> } @ Choose directory and/or equation

      @ Up one level (inserted PATH DUP SIZE GET for 'Menu' argument)
      { \|^ \<< PATH SIZE 2 > { PATH DUP SIZE GET UPDIR } IFT Menu \>> }
      @ Adjust the constant if this directory is at a lower level
      @ (to prevent going above the main directory level).

      @ ---- Next page (six labels) of CST menu ----

      MSOLVR @ Restart multiple equation solver without re-initializing

      @ With flag 63 clear, right-shift 'var' recalls variable.
      @ With flag 63 set, right-shift 'var' toggles MUSER/MCALC state.
      { MSopt \<< 63 DUP IF FS? THEN CF ELSE SF END MSOLVR \>> }

      { } @ Blank key

      { Purg \<< Purg 1 MENU \>> } @ Purge extraneous variables

      { } @ Blank key

      @ An optional feature, used by the author:
      { CST \<< { HOME CSTM } RCL TMENU \>> } @ Display CST[M] from HOME

    } @ End of custom menu CSTM

END @ Directory ends.






From: jhmeyers@miu.edu (John H Meyers)
Newsgroups: comp.sys.hp48
Subject: Re: User Equation Library in User-RPL
Date: 3 Jun 1998 07:06:10 GMT
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Lines: 51
Message-ID: <6l2sl2$rj2$1@news.iastate.edu>
References: <6klpm1$jmo$5@news.iastate.edu>
Reply-To: jhmeyers@miu.edu
NNTP-Posting-Host: vax1.mum.edu
Xref: republic.btigate.com comp.sys.hp48:11439

Here's an optional added bit of sophistication which mimics the
lesser-known behaviors of the built-in G/GX Equation Library;
of course, churning out more complex logic spoils the goal
of keeping everything extremely simple, but even Einstein said
"Everything should be as simple as possible -- but no simpler :)"

I can't help having the nagging feeling that there should exist an
unnamed library function already in the G/GX which can do this for us
without our having to bother; however, I haven't found it yet,
so here's a pure User-RPL solution to the problem:

%%HP: T(3); @ Re: Multi-level User Equation Library (HP48G/GX)

@ The following is a more complex optional replacement for the 'Init'
@ function (needed only if you want to pre-assign units to variables)
@ contained in the previously-posted User Equation Library directory.

@ Our original version never adjusted pre-existing variables;
@ this version (as well as the built-in Equation Library)
@ adjusts units as necessary in any pre-existing variables
@ which currently contain real or unit objects, but only if
@ the units needed by the new equation are incompatible.

@ This avoids errors caused by variables left-over from a previous
@ equation which have the same name but different meaning and units
@ in a new equation, or were not originally stored with any units.

@ Optionally replace the 'Init' program with the following:

       @ Initialize equation variables, if a list is provided
       @ (init list for equation 'xxx' must be stored in 'xxxINIT')
    \<< RCEQ DROP "'" 'EQ' RCL + "INIT" + OBJ\->
      DUP VTYPE 5 == @ Is it a list?
      { RCL 2 :: \<< DUP TYPE 6 == @ Value1 Name1, Value2 Name2, ...
        { DUP #185C7h SYSEVAL VTYPE 0 < { DUP2 STO } @ New variable.
  @ Was:{ DUP #185C7h SYSEVAL VTYPE 0 < { DUP2 STO } IFT } IFT
        @ SysRPL "DoHere:" [supported] ensures var is in current dir.
          { @ Additional new logic [ adds 110 bytes ]:
            { 0 13 } OVER VTYPE POS @ Examine existing real/unit var.
            { OVER UBASE 0 * OVER RCL UBASE 0 * SAME NOT @ Compatible?
              { DUP2 RCL UVAL SWAP @ Existing value, default units
                DUP TYPE { \->UNIT } { DROP } IFTE @ Unit vs. Real
                OVER STO @ Replace existing variable
              } IFT @ Compatible
            } IFT @ Existing Real/Unit
          } IFTE @ VType 0 <
        } IFT @ Type 6 ==
      DROP2 \>> DOSUBS } { DROP } IFTE \>> @ [ Original last line ]

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>