The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

UserRPL local variables not available to algebraics?
Message #1 Posted by John R. Graham on 28 Mar 2012, 8:27 p.m.

I can't imagine that this topic hasn't been covered, but I couldn't find it.

I don't usually do algebraic notation, so maybe I'm missing a nuance here. I was helping my college age son write a program where he wanted to evaluate an algebraic expression stored in a global variable against stack supplied variables. Simple, I thought. Just define local variables that match variables in the algebraic. However, local variables don't appear to be available to an algebraic. I can store the named variables in globals with STO and the algebraic can access them, but not if they're local.

Is it possible to define local variables in such a way that the algebraics can see them? Compiled local variables don't appear to work either.

- John

      
Re: UserRPL local variables not available to algebraics?
Message #2 Posted by Paul Dale on 28 Mar 2012, 8:36 p.m.,
in response to message #1 by John R. Graham

At a guess the local variables would have to exist at the point the algebraic object is defined for them to be referenced correctly.

In the absence of this, they'll be interpreted as global variables.

- Pauli

            
Re: UserRPL local variables not available to algebraics?
Message #3 Posted by Gerry Schultz on 28 Mar 2012, 8:47 p.m.,
in response to message #2 by Paul Dale

I thought local variables were created at the time the program is run and only last as long as the program is executing. So if the values are located on the stack, I would think you would write the program to use the values on the stack or re-write the program to prompt for the values of the newly declared local variables when executing the program.

Am I missing something?

Gerry

                  
Re: UserRPL local variables not available to algebraics?
Message #4 Posted by Paul Dale on 28 Mar 2012, 8:53 p.m.,
in response to message #3 by Gerry Schultz

Locals are created when a program is run and destroyed when it finishes.

What happens if you put a STOP in such a program?

What about executing something like -> a b << 'a*b+4' 'EQN' STO >> from the command prompt?

There are ways to have an active input and local variables defined.

- Pauli

                        
Re: UserRPL local variables not available to algebraics?
Message #5 Posted by Luiz C. Vieira (Brazil) on 28 Mar 2012, 10:44 p.m.,
in response to message #4 by Paul Dale

Hi.

Depending on the calculator you use, given a and b in the stack you'll have only the evaluated value stored in 'EQ' (e.g.: HP28C/S).

The equivalent to STOP in this case would be HALT, IIRC.

Cheers.

Luiz (Brazil)

      
Re: UserRPL local variables not available to algebraics?
Message #6 Posted by Luiz C. Vieira (Brazil) on 28 Mar 2012, 10:41 p.m.,
in response to message #1 by John R. Graham

Hi.

Can you post the command line contents you used?

Cheers.

Luiz (Brazil)

            
Re: UserRPL local variables not available to algebraics?
Message #7 Posted by John R. Graham on 29 Mar 2012, 7:44 a.m.,
in response to message #6 by Luiz C. Vieira (Brazil)

Hi, Luiz. Something funny going on here. The calculator in question is a 50g, by the way.

I have a global variable named 'Eqn' with value '2*X^2+9*X+7'. The program I am trying is as follows:

<< -> X
  << Eqn EVAL
  >>
>>
The EVAL doesn't touch the quoted algebraic, leaving it on the stack unmodified. Here's the funny thing, though. If I include the same equation literally in the program, like so

<< -> X
  << '2*X^2+9*X+7' EVAL
  >>
>>
then it works properly, evaluating the algebraic. In addition, if I create a global variable X, the much less elegant
<< 'X' STO 
  Eqn EVAL
  'X' PURGE
>>
works. This seems to hinge on the absence of the inner << >> program quotes. Restoring them seems to mask the local variable X from the algebraic in some very specialized circumstances.

- John

Edited: 29 Mar 2012, 11:07 a.m. after one or more responses were posted

                  
Re: UserRPL local variables not available to algebraics?
Message #8 Posted by Alexander Oestert on 29 Mar 2012, 8:28 a.m.,
in response to message #7 by John R. Graham

See below the different ways how to make this work in a program.

You might want to try an entirely different approach:

By pressing Eqn in the VAR menu put the algebraic expression on the stack. Then press the silver down arrow, by that entering the expression in the EQW writer. Then [right shift] [TOOL] (the orange pipe operator|). You will get the = right to your expression with a blinking cursor arrow. Enter X on its left side and the value you want to evaluate the expression with on its right side, say X=5. Press ENTER. Press EVAL. Press EVAL again. The numerical result is on the stack.

                  
Re: UserRPL local variables not available to algebraics?
Message #9 Posted by Kiyoshi Akima on 29 Mar 2012, 12:40 p.m.,
in response to message #7 by John R. Graham

Try it this way:

'2*<-X^2+9*<-X+7'
 and
<<
 -> <-X
 <<
  Eqn EVAL
 >>
>>

In your approach the local variable X is not defined at the point Eqn is defined. This is what compiled local variables are for.

                  
Re: UserRPL local variables not available to algebraics?
Message #10 Posted by Luiz C. Vieira (Brazil) on 29 Mar 2012, 1:38 p.m.,
in response to message #7 by John R. Graham

HI.

In brief, just about the first part (I'm in a hurry now, will post more later), try ->NUM instead, like this:

<< -> X
  << Eqn ->NUM
  >>
>>
It forces a numeric result. One of the EVAL rules is to keep a variable name once it is evaluated. I believe it will work the way you want. The second part is correct, once another EVAL rule is to replace variables for their contents when an algebraic is evaluated. Bill Wickes' 'HP48 Insights' has great material about this.

Cheers.

Luiz (Brazil)

Edited: 29 Mar 2012, 1:42 p.m.

      
Re: UserRPL local variables not available to algebraics?
Message #11 Posted by Kiyoshi Akima on 29 Mar 2012, 1:56 a.m.,
in response to message #1 by John R. Graham

<<
 -> a b
 <<
  'a+b' EVAL
 >>
>>
This program does the expected thing on the 50g, adding the first two elements on the stack.

Edited: 29 Mar 2012, 2:21 a.m.

            
Re: UserRPL local variables not available to algebraics?
Message #12 Posted by C.Ret on 29 Mar 2012, 3:31 a.m.,
in response to message #11 by Kiyoshi Akima

Quote:
<<
 -> a b
 <<
  'a+b' EVAL
 >>
>>

The following syntax will do it as well :

 <<  -> a b 'a+b' >>

In HP28C/S manual it is call analytical user's function. Compare to your program strucure code, this can be formaly derivate or integrate as any other built-in analytical function of the system.

      
Re: UserRPL local variables not available to algebraics?
Message #13 Posted by Alexander Oestert on 29 Mar 2012, 2:06 a.m.,
in response to message #1 by John R. Graham

Assume you STO the following in 'ALG'

<<'A*B' >>

and this in 'TST'

<< A STO B STO << ALG >> EVAL ->NUM {A B} PURGE >>

then you put say 5 and 6 on stack lines 1: and 2: and press TST from the VAR menu, you will have

30.

on line 1: of the stack.

The program uses A STO B STO instead of -> A B to make A and B global variables in order for 'ALG' to be able to see them. The PURGE is necessary because any subsequent run of TST could not STO A and B again when they already exist. The algebraic expression STOed in 'ALG' however continues to exist after the program is run.

Edited: 29 Mar 2012, 2:16 a.m.

            
Re: UserRPL local variables not available to algebraics?
Message #14 Posted by Marcus von Cube, Germany on 29 Mar 2012, 2:20 a.m.,
in response to message #13 by Alexander Oestert

Quote:
The PURGE is necessary because any subsequent run of TST could not STO A and B again when they already exist.
Wouldn't 'A' STO 'B' STO solve this dilemma?
                  
Re: UserRPL local variables not available to algebraics?
Message #15 Posted by Alexander Oestert on 29 Mar 2012, 2:33 a.m.,
in response to message #14 by Marcus von Cube, Germany

Quote:

Wouldn't 'A' STO 'B' STO solve this dilemma?


Yes and no. Doing it your way would make a subsequent program run possible as A and B could be used again without being PURGEd before. But after the program run A and B would still exist in the VAR menu as they are global variables. John asked for local variables, so I assumed he wanted them to not exist beyond the scope of the program. My way is the closest I could get to that.

Edited: 29 Mar 2012, 2:33 a.m.

                        
Re: UserRPL local variables not available to algebraics?
Message #16 Posted by Paul Dale on 29 Mar 2012, 2:42 a.m.,
in response to message #15 by Alexander Oestert

Try this instead:

<< 0 0 -> A B << 'A*B' >> 'ALG' STO >> EVAL

Local variables in an algebraic expression.

The way to use them would then be in a program:

<< -> A B << ALG EVAL >> >>

- Pauli

                              
Re: UserRPL local variables not available to algebraics?
Message #17 Posted by Alexander Oestert on 29 Mar 2012, 2:47 a.m.,
in response to message #16 by Paul Dale

Very nice and elegant!

OT When will the WP-34s have RPL and an algebraic mode? ;-)

      
Re: UserRPL local variables not available to algebraics?
Message #18 Posted by cyrille de brebisson on 29 Mar 2012, 6:56 a.m.,
in response to message #1 by John R. Graham

Quote:
I don't usually do algebraic notation, so maybe I'm missing a nuance here. I was helping my college age son write a program where he wanted to evaluate an algebraic expression stored in a global variable against stack supplied variables. Simple, I thought. Just define local variables that match variables in the algebraic. However, local variables don't appear to be available to an algebraic. I can store the named variables in globals with STO and the algebraic can access them, but not if they're local.

hello

yes, but what you need to understand is that when an object is compiled (when you press ENTER on something), names will be compiled as local variables ONLY if the local exists at that time...

so, doing: 'A+B' 'C' STO followed by calling C EVAL in a program when A and/or B are local variable will cause A+B to still refer to the GLOBAL variables as local A/B did not exist at that point...

also, doing: 2 'A' STO << 0 -> A << 'A' 1 -> A << EVAL >> >> will return 1 because when 'A' was compiled, a local variable A DID exist and 'A' will evaluate a local, in this case the A containing 1... doing: 2 'A' STO << 0 -> A << 'A' 1 -> A << >> >> EVAL will err as there is no more local A when 'A' is evaluated...

cyrille

            
Re: UserRPL local variables not available to algebraics?
Message #19 Posted by Dave Britten on 29 Mar 2012, 7:39 a.m.,
in response to message #18 by cyrille de brebisson

This is the most important (and most confusing) part, since there's no easy visual indication of how it was compiled. You can use TYPE to determine if a name is local or global, though (I forget the return codes, but it's different for the two).

            
Re: UserRPL local variables not available to algebraics?
Message #20 Posted by John R. Graham on 29 Mar 2012, 8:07 a.m.,
in response to message #18 by cyrille de brebisson

Cyrille,

Thanks. This is exactly consistent with the symptoms I'm seeing (see my reply to Luis above). If I create the proper local variable context, do you know how I can force a recompile of the algebraic expression?

- John

                  
Re: UserRPL local variables not available to algebraics?
Message #21 Posted by Paul Dale on 29 Mar 2012, 8:12 a.m.,
in response to message #20 by John R. Graham

You must create the expression inside a context that has the local variables defined. You might be able to convert the expression to a string, create the context and then convert it back.

- Pauli

                        
Re: UserRPL local variables not available to algebraics?
Message #22 Posted by Marcus von Cube, Germany on 29 Mar 2012, 9:48 a.m.,
in response to message #21 by Paul Dale

Shouldn't something like

'A+B' 'EQ' STO

<< -> A B << EQ ->STR OBJ-> EVAL >> >>

work?

                        
Re: UserRPL local variables not available to algebraics?
Message #23 Posted by John R. Graham on 29 Mar 2012, 9:50 a.m.,
in response to message #21 by Paul Dale

Okay, good suggestion! The following

<<
  -> X
  << Eqn ->STR OBJ-> EVAL
  >>
>>
works just fine. Understanding the precompilation nuance makes all the difference. It's pretty abstruse, though.

Thanks very much to Cyrille, Pauli, and Marcus.

- John

Edited: 29 Mar 2012, 10:10 a.m.

            
Re: UserRPL local variables not available to algebraics?
Message #24 Posted by cyrille de brebisson on 31 Mar 2012, 2:29 a.m.,
in response to message #18 by cyrille de brebisson

Actually, there is something that you can do...

If my memory serves me well (and I do not have a 50 around me for testing), ANY variable that starts with a forward? arrow is ALWAYS compiled as a LOCAL..

so you should be able to do '>A+>B' << 1 2 > >A >B << EVAL >> >> and it should work....

I am just not 100% sure that the magic chr is that arrow....

cyrille

                  
Re: UserRPL local variables not available to algebraics?
Message #25 Posted by Luiz C. Vieira (Brazil) on 31 Mar 2012, 2:47 a.m.,
in response to message #24 by cyrille de brebisson

HI.

Wouldn't it be the left-arrow? Like this:

'<-A + <-B' << 1 2 -> <-A <-B << EVAL >> >>
At least this 'left-arrow' being used to define/create compiled variables is what I remember reading about at James Donnelly's "The HP48 Handbook".

Cheers.

Luiz (Brazil)

Edited: 31 Mar 2012, 2:48 a.m.

                  
Re: UserRPL local variables not available to algebraics?
Message #26 Posted by Paul Dale on 31 Mar 2012, 2:59 a.m.,
in response to message #24 by cyrille de brebisson

If this is true, I've learned something. Pity my 48SX and 49G are both dead and my 28S has no battery door so I can't test.

- Pauli

                        
Re: UserRPL local variables not available to algebraics?
Message #27 Posted by Luiz C. Vieira (Brazil) on 31 Mar 2012, 7:45 a.m.,
in response to message #26 by Paul Dale

Hi, Paul.

I checked my October 1994, 2nd. Edition volume, p. 46, and the ID for compiled local variables is indeed the preceding left-arrow character. I am not sure about after which model it was implemented, the cover picture of Donnelly's book has an HP48GX. Do not know if the 48S/SX or the 28C/S had such feature.

Cheers.

Luiz (Brazil)

                              
Re: UserRPL local variables not available to algebraics?
Message #28 Posted by Gilles Carpentier on 5 Apr 2012, 6:56 a.m.,
in response to message #27 by Luiz C. Vieira (Brazil)

I tried on my old 48sx but I can not remember how to enter such a char on a SX (back arrow) . There is no CHARS menus on the 48SX ...

                                    
Re: UserRPL local variables not available to algebraics?
Message #29 Posted by Paul Berger (Canada) on 5 Apr 2012, 7:24 a.m.,
in response to message #28 by Gilles Carpentier

To get left arrow it alpha R-shift(blue) cursor left key, same as on a 48G.

                                    
Re: UserRPL local variables not available to algebraics?
Message #30 Posted by Alexander Oestert on 5 Apr 2012, 8:14 a.m.,
in response to message #28 by Gilles Carpentier

As far as I can tell from the manual of the 48sx and an actual side-by-side test of the 48s and the 48g, the 48s doesn't have local compiled variables available, the 48g does.

EDIT for clarification

Edited: 5 Apr 2012, 1:55 p.m. after one or more responses were posted

                                          
Re: UserRPL local variables not available to algebraics?
Message #31 Posted by Gilles Carpentier on 5 Apr 2012, 12:35 p.m.,
in response to message #30 by Alexander Oestert

Just tested : It works on my 49 but not on my 48SX

      
Re: UserRPL local variables not available to algebraics?
Message #32 Posted by Eric Smith on 29 Mar 2012, 2:51 p.m.,
in response to message #1 by John R. Graham

The key point is that the local variable has to be in scope at the time that any reference to it is compiled. You can't do something like this:

'a+b' 'foo' STO

<< -> a b foo >> 'bar' STO

3 7 bar

because at the time you define foo, the compiler has no reason to expect a and b to be local variables, and compiles them as globals.

            
Re: UserRPL local variables not available to algebraics?
Message #33 Posted by Gilles Carpentier on 29 Mar 2012, 3:42 p.m.,
in response to message #32 by Eric Smith

Note that this way works (replace the <- by arrows) :

'<-a+<-b' 'foo' STO

« -> <-a <-b « foo EVAL » » 'bar' STO

3 7 bar

but it's awfull in my opinion and i don't see any interest of that.

      
Re: UserRPL local variables not available to algebraics?
Message #34 Posted by Gilles Carpentier on 29 Mar 2012, 3:07 p.m.,
in response to message #1 by John R. Graham

Hi John

another way could be :

«
  -> X
  « Eqn {X , 'X'} | EVAL »
»

Works fine

I don't know what you want to do with the global Eqn, but why not something like this :

'Eqn(X)=2*X^2+9*X+7' DEFINE 

Then 5 Eqn or 'Eqn(5)' EVAL

does the job

or if you want to keep Eqn exactly as '2*X^2+9*X+7'

'2*X^2+9*X+7' 'Eqn' STO 
'F(X)'Eqn = DEFINE 

Then 5 F or 'F(5)' EVAL

or 'Y' F or 'Y^2+9' F or what you want if you need to work in symbolic way

Edited: 29 Mar 2012, 4:31 p.m.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall