HP Forums
48: Moving average with DOSUBS - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: 48: Moving average with DOSUBS (/thread-4138.html)



48: Moving average with DOSUBS - Dave Britten - 06-12-2015 01:27 PM

I need to whip up a quick moving-average program. This doesn't work:

Code:
AVG:
\<< DUP \GSLIST SWAP SIZE / \>>

MAVG:
\<< \-> L N
  \<< L N \<< N \->LIST AVG \>> DOSUBS \>>
\>>

And it's fairly obvious why: DOSUBS doesn't like the local name 'N' being present in the program it's being passed.

What's the trick to modifying a program on the stack? I figure I should be able to recall the value of N, prepend it to \<< ->LIST AVG \>>, and have a program that DOSUBS will take. You can't simply concatenate programs with +, and while it works with lists, DOSUBS won't accept the function stored as a list.


RE: 48: Moving average with DOSUBS - Werner - 06-12-2015 02:55 PM

? But it does work, and DOSUBS has no issues with local variables being in the executable object..
{ 1 2 3 } 2 MAVG returns { 1.5 2.5 } as it should
BTW you don't need to include L in your local environment

Code:
MAVG:
\<< \-> N
  \<< N \<< N \->LIST AVG \>> DOSUBS \>>
\>>

Cheers, Werner


RE: 48: Moving average with DOSUBS - Dave Britten - 06-12-2015 03:03 PM

(06-12-2015 02:55 PM)Werner Wrote:  ? But it does work, and DOSUBS has no issues with local variables being in the executable object..
{ 1 2 3 } 2 MAVG returns { 1.5 2.5 } as it should
BTW you don't need to include L in your local environment

Code:
MAVG:
\<< \-> N
  \<< N \<< N \->LIST AVG \>> DOSUBS \>>
\>>

Cheers, Werner

Alright, that's weird... At first, I was getting "DOSUBS Error: Invalid User Function". Not sure what bizarre typo was leading to that.

Anyway, if anybody knows how to modify programs and convert them to/from lists, I'd still find that useful.


RE: 48: Moving average with DOSUBS - John R. Graham - 06-12-2015 03:26 PM

(06-12-2015 03:03 PM)Dave Britten Wrote:  Anyway, if anybody knows how to modify programs and convert them to/from lists, I'd still find that useful.
Convert the program to a string with →STR, modify to your heart's content, and then convert back to a program with OBJ→. This has the advantage that it forces a recompile of the program so that your changes are guaranteed to take effect.

Edit: I think I'm mis-remembering the compiling thing, which only applies to algebraics.

- John


RE: 48: Moving average with DOSUBS - Dave Britten - 06-12-2015 04:03 PM

(06-12-2015 03:26 PM)John R. Graham Wrote:  
(06-12-2015 03:03 PM)Dave Britten Wrote:  Anyway, if anybody knows how to modify programs and convert them to/from lists, I'd still find that useful.
Convert the program to a string with →STR, modify to your heart's content, and then convert back to a program with OBJ→. This has the advantage that it forces a recompile of the program so that your changes are guaranteed to take effect.

Edit: I think I'm mis-remembering the compiling thing, which only applies to algebraics.

- John

That should work in most cases, including this one, but if the program has any non-UserRPL stuff in it, e.g. takeovers, externals, etc., then recompiling it from the string representation will probably either fail with an error, or store a broken program.

I could swear I saw some method to build/modify a program on the fly without doing any string conversions. Can't remember where I saw it, though. It may have been an algebraic-to-RPN converter, unless I'm imagining the existence of that too.


RE: 48: Moving average with DOSUBS - John R. Graham - 06-12-2015 04:31 PM

Interesting. If you (re-)locate such a feature, I'd love to hear about it.

- John


RE: 48: Moving average with DOSUBS - Joe Horn - 06-12-2015 05:50 PM

By converting programs to lists and back again, are you referring to the →LST and →PRG command in library 256? If you execute 256.02 MENU, you'll see →LST on the F5 key. Pressing it with a program on the stack will turn it into a list, which can easily be exploded to the stack via OBJ→, or manipulated like any other list. Then press NXT to see →PRG on the F1 key. It takes any list and turns it into a program.

If that's not what you were looking for, ignore this posting.


RE: 48: Moving average with DOSUBS - John R. Graham - 06-12-2015 06:09 PM

Wow. Those descriptions are so terse in the AUR that I hadn't realized what they did. Thank you.

- John


RE: 48: Moving average with DOSUBS - Dave Britten - 06-12-2015 06:23 PM

(06-12-2015 05:50 PM)Joe Horn Wrote:  By converting programs to lists and back again, are you referring to the →LST and →PRG command in library 256? If you execute 256.02 MENU, you'll see →LST on the F5 key. Pressing it with a program on the stack will turn it into a list, which can easily be exploded to the stack via OBJ→, or manipulated like any other list. Then press NXT to see →PRG on the F1 key. It takes any list and turns it into a program.

If that's not what you were looking for, ignore this posting.

That sounds like what I was thinking of.

Now, how do I do that on a 48, which doesn't have menu 256?


RE: 48: Moving average with DOSUBS - Joe Horn - 06-12-2015 09:56 PM

(06-12-2015 06:23 PM)Dave Britten Wrote:  That sounds like what I was thinking of. Now, how do I do that on a 48, which doesn't have menu 256?

Gotta use System RPL, but that can be faked with SYSEVAL in a User RPL program. Delightfully, the SRPL command CHANGETYPE has remained stable at ROM address #05AB3h in every RPL model, from 48SX version A, through the 48GX version R, through the 50g version 2.15. So the following little routine will convert any program into a list or vice versa, and leave any other input unchanged. It's non-optimized for ease of human reading... if such a thing is possible in RPL. Be sure to get the hex numbers exactly right, or it'll do Bad Things.

'P~L' (Program/List Toggler) for HP 48/49/50, all versions
<< CASE
DUP TYPE 8 ==
THEN #2A74h #5A03h SYSEVAL #5AB3h SYSEVAL END
DUP TYPE 5 ==
THEN #2D9Dh #5A03h SYSEVAL #5AB3h SYSEVAL END
END >>



RE: 48: Moving average with DOSUBS - Dave Britten - 06-12-2015 10:54 PM

(06-12-2015 09:56 PM)Joe Horn Wrote:  
(06-12-2015 06:23 PM)Dave Britten Wrote:  That sounds like what I was thinking of. Now, how do I do that on a 48, which doesn't have menu 256?

Gotta use System RPL, but that can be faked with SYSEVAL in a User RPL program. Delightfully, the SRPL command CHANGETYPE has remained stable at ROM address #05AB3h in every RPL model, from 48SX version A, through the 48GX version R, through the 50g version 2.15. So the following little routine will convert any program into a list or vice versa, and leave any other input unchanged. It's non-optimized for ease of human reading... if such a thing is possible in RPL. Be sure to get the hex numbers exactly right, or it'll do Bad Things.

'P~L' (Program/List Toggler) for HP 48/49/50, all versions
<< CASE
DUP TYPE 8 ==
THEN #2A74h #5A03h SYSEVAL #5AB3h SYSEVAL END
DUP TYPE 5 ==
THEN #2D9Dh #5A03h SYSEVAL #5AB3h SYSEVAL END
END >>

Sweet, thanks. I figured there might be a couple of SYSEVALs that would do that.