HP48 programming: use values from stack in expression
|
11-30-2018, 02:47 PM
Post: #1
|
|||
|
|||
HP48 programming: use values from stack in expression
Hello all
I'm trying to write a simple program for the HP48. The program should take two values from the stack and put an expression back on the stack that contains these values. For example: take 2 and 3 and return '2+3'. With 2 and 3 as input, I tried << -> a b 'a+b' >> which returns 5 and << -> a b << 'a+b' >> >> which returns 'a+b'. And << -> a b << 'a+b' EVAL >> >> returns 5 again (of course)... Thanks for any help! Beat |
|||
11-30-2018, 09:14 PM
Post: #2
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
Code: « "'" ROT + "+" + SWAP + "'" + OBJ→ » Cheers Thomas |
|||
11-30-2018, 10:21 PM
Post: #3
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
Code: << -> A B << 'A+B' 'A' A = SUBST 'B' B = SUBST >> >> |
|||
12-01-2018, 05:05 AM
(This post was last modified: 12-06-2018 02:10 AM by Giuseppe Donnini.)
Post: #4
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
SUBST is not an HP-48 command.
Thomas Klemm's solution works, but you would have to rewrite the program for every single function you want to apply. Here's a more general solution: 1. For single-argument functions (like SIN, square root, etc.): Code: @ F1 : ( x {f} --> f(x) ) 2. For two-argument functions (like +, -, etc.): Code: @ F2 : ( x y {f} --> f(x,y) ) Following the same pattern, programs for three or more argument functions can easily be added. Note that these programs respect the symbolic result flag -3: if clear (default), they return symbolic results; if set, they return numeric results. |
|||
12-01-2018, 06:12 AM
Post: #5
|
|||
|
|||
RE: HP48 programming: use values from stack in expression | |||
12-01-2018, 02:06 PM
(This post was last modified: 12-01-2018 09:51 PM by Thomas Klemm.)
Post: #6
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
(12-01-2018 05:05 AM)Giuseppe Donnini Wrote: We don't need that step as EVAL takes care of the list: 'a' 'b' { + } EVAL 'a+b' Nice solution, by the way. I must admit I wasn't aware of neither ↑MATCH nor ↓MATCH. I couldn't come up with a simple way to create something like this map based on 2 and 3: Code: { { a 2 } { b 3 } } But that allowed us to extract the repeated code and use DOLIST: 'a+b' { { a 2 } { b 3 } } 1 « ↑MATCH DROP » DOLIST '2+3' Any suggestions on how to do that? Or then rather use STREAM: { 'a+b' { a 2 } { b 3 } } « ↑MATCH DROP » STREAM '2+3' Cheers Thomas |
|||
12-02-2018, 11:03 AM
Post: #7
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
Thank you for all the hints. At the moment the string solution with OBJ→ is sufficient for me, but I like the additional possibilities with lists and ↑MATCH... Thanks again!
|
|||
12-04-2018, 02:44 AM
(This post was last modified: 12-05-2018 01:15 PM by Giuseppe Donnini.)
Post: #8
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
But of course! Lists become procedure class objects when "evaluated"--as opposed to being merely "executed". Excellent suggestion, Thomas, thanks! I already changed my post accordingly.
For multiple variable substitutions, I use the following program which indeed relies on MATCH and works almost the same as your DOLIST example, except that it uses an explicit loop. Code: @ SUBST (version A) MATCH is a very powerful command, whose full potential I did not realize until studying Bill Wickes' "Insights" books. Because it uses MATCH, SUBST is in fact much more flexible than it at first appears. Actually, its level 1 argument allows the more general form: { { 'pattern1' 'replacement1' } ... { 'patternn' 'replacementn' } } of which { { name1 value1 } ... { namen valuen } } is only a small subset. You can therefore easily replace entire sub-expressions, as in the following example: ( 'COS(X)/SIN(X)+3*Y' { { 'COS(X)/SIN(X)' 'COT(X)' } { Y 5 } } --> 'COT(X)+3*5' ) The most powerful feature of MATCH, however, is that it allows you to use wild cards to target specific sub-expressions inside the main algebraic expression. Any name that begins with the ampersand character "&" is interpreted as a wild card. Suppose you have the following expression: '(SIN(X^2+1/Y))' and you want to replace X with 9, Y with 2, and then expand the sum according to the addition law for sines. This is easily done with the following level 1 argument: { { X 9 } { Y 2 } { 'SIN(&1+&2)' 'SIN(&1)*COS(&2)+COS(&1)*SIN(&2)' } } The result will be: 'SIN(9^2)*COS(1/2)+COS(9^2)*SIN(1/2)' Or, suppose you want to replace every square root in an expression with a cubic root. This simple level 1 argument: { { '\v/&1' 'XROOT(3,&1)' } } will carry out all the replacements. As a further refinement, you may specify an optional third algebraic in each list which, when evaluated, must return a user flag determining whether the replacement should take place or not. For example, while simplifying square roots of squares, you may want to make sure that the arguments of the square function are actually positive: { { '\v/(&1^2)' &1 '&1\>=0' } } Since the optional test argument is less often needed, and since one might prefer a more mathematical look and feel, here's an alternative version of SUBST, which uses a list of equations instead of a list of lists as its level 1 argument. For convenience, a single equation may also be entered without list delimiters. Code: @ SUBST (version B) The more elaborate constructs described above, involving entire expressions and wild cards, are also available in this version of SUBST, albeit with the following restrictions: 1. Since the list elements have the general form 'pattern=replacement', pattern and replacement may not be equations themselves. 2. The optional test argument is not available. |
|||
12-05-2018, 04:11 AM
Post: #9
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
That was an interesting read. I wasn't aware of these pattern matching capabilities of the HP-48.
Thanks a lot! Thomas |
|||
12-05-2018, 03:49 PM
Post: #10
|
|||
|
|||
RE: HP48 programming: use values from stack in expression
Another interesting use of MATCH is its combination with UDF (User Defined Function) versions of RPN-only commands, allowing you to select sub-expressions within larger expressions as arguments for those rewritten commands. In other words, it allows you to perform RULES-type operations right on the stack or inside programs. Here's an example with COLCT.
Code: @ NAME : SCOLCT |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)