Having trouble with Random Matrix

Discuss your code, get questions answered

Having trouble with Random Matrix

Postby JohnShau » Sat Dec 10, 2011 1:20 pm

Hi Oliver,

I input the RPL program Random Matrix following the example given and named it RNDM. I entered 5,IDN and got the (5x5) idenity matrix same as in the tutorial. In My Vars I tapped RNDM, and the ND1 freezes. I compared my progam listing to the one in the tutorial and I didn't see any difference. So I decided to use the built-in RANM to help me. By using Array menu I found RANM; typed in 5,IDN and got the identity matrix, and it changes randomly with each tap of the RANM key. I thought I could look at the code by tapping 2nd Level and the RANM key but that didn't work. Do you have an idea why the ND1 freezes when I tap My Vars RNDM ?

Thanks,
John
JohnShau
 
Posts: 29
Joined: Thu Dec 08, 2011 6:30 am

Re: Having trouble with Random Matrix

Postby oliver » Sat Dec 10, 2011 4:25 pm

Hi John,

I just tried the code given in the User Functions tutorial, and it works for me. So, I'm suspecting you may have some kind of syntax error. Do you have all separating spaces in "0.5 - 18 *"?

Here's how to find out what's going on: Insert the command HALT as the first instruction in your program. If you now run the program, you will get a dialog for each step and see exactly what happens on the stack. (You don't have to overwrite RNDM for that, by the way; you can simply evaluate the program when it sits on the stack--this is convenient for debugging.)

Hope this helps.

Oliver

P.S. Unfortunately, I'm lagging what the documentation is concerned. This tutorial was written one and a half years ago. Since then the calculator has come a long way, and programmability in RPL+ is now available and much superior to old-school RPL. This specific RNDM example pretty much sucks, and really doesn't serve to make programming on ND1 look appealing... I will change that.

Here's a better implementation of RNDM:
Code: Select all
≪ [ [randomize 0.5 - 18 * 0 RND] MAP] MAP ≫

This is not only shorter, it runs (much) faster too.

http://naivedesign.com/ND1/Project_Euler.html is maybe the best source for example code right now. You can compare code there with implementations in many other languages on the Project Euler website. This doc will be updated with the release of v1.4, which makes various of these programs even smaller and faster.

P.P.S. About you not being able to see the code for RANM. Yes, built-in functions cannot be viewed and edited in the same way like user code. (Otherwise, it would be too easy to wreck the calculator.) There's ways to get at the code and even overwrite it with your own implementation, though. This is explained under "JavaScript API concepts".
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: Having trouble with Random Matrix

Postby JohnShau » Sat Dec 10, 2011 4:48 pm

Hi Oliver,

I got the Random Matrix example working on my iPad. Don't know what the problem is on the iPhone.

thanks,
john
JohnShau
 
Posts: 29
Joined: Thu Dec 08, 2011 6:30 am

Re: Having trouble with Random Matrix

Postby JohnShau » Sun Dec 11, 2011 4:27 am

Hi Oliver,

I just read your post of yesterday at 7:25 pm.
I did have the minus sign next to the 1 as "0.5 -18 *".
I fixed that and overwrote RNDM, and the program runs fine.
I like the HALT idea, but can't get it to work.
Does one put the HALT before the <<, the [1,1] or the DO?
Then does one tap Enter and rename? How does
one step through the program, etc ?
Do you have a link to a write up on debugging?

I'm still digesting all your helpful comments.

Thanks for the help.

John
JohnShau
 
Posts: 29
Joined: Thu Dec 08, 2011 6:30 am

Re: Having trouble with Random Matrix

Postby JohnShau » Sun Dec 11, 2011 6:13 am

Hi Oliver,

I got HALT to work. I edited RNDM and put HALT between << and [1,1]. I then Entered the new code and named it rndmH and STOred it. I then entered 3,IDN and tapped rndmH. This generated the instructions and result for each loop. Neat!

I'd still like to read more about basic programming and debugging. Please point me in the right direction. I'll continue working with your examples.

Thanks

John
JohnShau
 
Posts: 29
Joined: Thu Dec 08, 2011 6:30 am

Re: Having trouble with Random Matrix

Postby JohnShau » Sun Dec 11, 2011 9:29 am

Hi Oliver,

I have a few questions about RPL. In your post yesterday at 7:25 pm you were talking about using HALT and you said (You don't have to overwrite RNDM for that, by the way; you can simply evaluate the program when it sits on the stack--this is convenient for debugging) Please explain what you mean by I don't have to overwrite RNDM, and second, that I can evaluate the program when it sits on the stack.

Next, what are the special symbols that look like two greater-than and two less-than symbols close together? There's a small RPL program at the Wikipedia RPL site that displays if a number is greater than 1, less than 1, or equals 1. The code caught my eye because it uses several of these special symbols inside the code. I tried to program it and couldn't figure how to get these symbols inside ND1 code.

Thanks again.

John
JohnShau
 
Posts: 29
Joined: Thu Dec 08, 2011 6:30 am

Re: Having trouble with Random Matrix

Postby JohnShau » Sun Dec 11, 2011 11:55 am

Hi Oliver,

In my post today at 12:29 I asked you about the special symbols that look like << and >>. I found the set in the Program menu. I used them and was able to code the Wikipedia RPL "More, Less, One" IF statement example.

I'm slowly getting there :-)

Thanks

John
JohnShau
 
Posts: 29
Joined: Thu Dec 08, 2011 6:30 am

Re: Having trouble with Random Matrix

Postby oliver » Sun Dec 11, 2011 2:54 pm

Hi John,

Congrats on your first successes with RPL!

Ok, you found the ≪, ≫ symbols in the Program menu. These simply frame a RPL program. You find ≪ over the space key in the bottom left.
As the closing symbol is appended automatically when you enter code, the easy way to write an RPL program is to
- Enter ≪ from the keypad
- Double-tap the edit line to get it expanded if you plan to write a multi-line program
- Type your RPL commands
- Enter

Now, the RPL program will "sit on your stack". (Specifically, at position 1.)
You can now proceed to store it under a name, *or*, and that was the point I was trying to make, you can tap the ⌘ (EVAL) key.
The Eval key does various things, depending on the kind of object it's invoked on. Invoked on an RPL program, it will run the program. That is, you don't have to have a program stored in a variable to try it out. If you're in a edit/run cycle as you develop code, not having to store the program every time you do an edit comes in handy.

To edit an RPL program that's at position 1 on the stack, you tap EDIT (over the ENTER key), as I assume you found out.

(I hope you'll come to appreciate that those cryptic keys on the keypad are actually quite ergonomically placed, once you have gotten used to them. Also, sooner or later, try the Modern keyboard layout, it looks more cryptic, but it *is* better (plus, nine softkeys do make a nice difference).)

Most ND1 users come from an HP calc and know about all the RPL stuff. The documentation for the app is not very suitable for someone starting out with RPL programming, I'm afraid. However, you can google for the HP-50g User manual and go through a "proper" RPL learning steps, and study annotated code. You will find two downloadable folders with HP-28 and HP-50g example code under My Data | Downloadables.

ND1 introduces RPL+, which is, I think, an easier and better language, and I will invest time into writing more comprehensive documentation for it, as soon as there's time. (You may have noticed, there's an ambitious road map and ND1 is long not done. And features tend to come before documentation, I confess. CalcPad for the iPad (coming soon) will be a better learner's and coder's environment, fully using the bigger screen and optionally showing a function's documentation, as you select a function.)

HALT can appear anywhere within ≪ ≫. As soon as the program flow hits it, the calc switches from running silently to single-stepping with that dialog that shows you the stack and permits you to either advance a step, or Cancel and abort the program. There's not much more to it. And, sorry, there's presently no tutorial for debugging, I'm afraid. (The HP manuals will talk about HALT, too, though.)

Have fun on your RPL journey and don't let the intricacies of balancing the stack discourage you. RPL+, http://naivedesign.com/ND1/ND1_Reference__RPL+.html, allows you to assign and re-assign local variables with ease (the syntax is "=x", or whatever variable name you want), and this allows to eliminate much or all of the DUP, SWAP, and DROP instructions that can render traditional RPL program quite hard to read.

Cheers.

P.S. In v1.4, the example at hand can even more shortly be written in RPL+ like so
Code: Select all
≪ [ randomize 0.5 - 18 * 0 RND ] mmap ≫

mmap (a new command in v1.4) stands for "matrix map". It takes a matrix and a program and runs the program on every cell in the matrix.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: Having trouble with Random Matrix

Postby JohnShau » Mon Dec 12, 2011 10:50 am

Hi Oliver,

Thanks for the reply. Let me say, I appreciate what you do as administrator and teacher of the ND1. By way of background I was a research engineer with NASA from 1964 to 1996. I, like many that use this forum, have owned several HP programmables, and still use a 42S of years gone by. To say the least, I am impressed with the ND1 concept.

I'm trying to learn how to use the EVAL function to help write and debug RPL code. I've learned if I type a " ' " to begin a line of code on the edit line and follow it with executable code; e.g. " 2 + sin(45) ", then I can tap EVAL and the result will be put in stack position 1. Also I found that after typing the executable code I can tap ENTER and the code will be put on stack position 1; then if I tap EVAL the code is replaced with the value. Now here's my problem; I don't know how to use this new-found info :-) I've tried using the code beginning symbol like <<, followed by some code and this doesn't seem to work for me. I think a couple examples with a bit of code would help me understand what you are talking about.

Thanks

John
JohnShau
 
Posts: 29
Joined: Thu Dec 08, 2011 6:30 am

Re: Having trouble with Random Matrix

Postby oliver » Mon Dec 12, 2011 3:28 pm

Hi John,

Thank you for your kind words. They're appreciated.

Ok, what you typed in is an expression. Expressions follow normal algebraic syntax conventions. To EVAL an expression means to compute its result, as you discovered. An HP-42S doesn't know about expressions, but RPL machines do. They do regard them as a special data type, though, while continuing to follow post-fix syntax for everything else.

With an RPL program you need to type valid code following the "reverse" style of RPN/post-fix.
To replicate your example in RPL, try this:
Enter ≪ 45 sin 2 +
(Note, the commands appear just as you would type them into any RPN calc.)
Now EVAL this, and you should get the same result.

With algebraic expressions you're limited to producing a single output/result, and there're no "control structures" (=program flow commands) that you can use, such as FOR-loops or IF-conditionals. You have a large set of commands at your disposal and you're not constrained to producing a single result, or taking a single input.

Among the commands available in RPL is EVAL. And you can enter algebraic expressions, which will not be evaluated until you say so in code.
That is, you could also do this:
Enter ≪ '2+sin(45)' EVAL

And eval that. This is just as if you typed in '2+sin(45)', followed by a tap on EVAL.
If you don't use control structures, RPL is *exactly* like keystroke recording on HPs before the RPL series (which emcompasses the models HP-28, HP-48, -49, -50g).

RPL+ in v1.4 will permit for the first time the use of algebraic expressions in RPL without the tick marks (''). Such expressions will be evaluated immediately.
So, you'll be able to write ≪ 2+sin(45)
and, upon EVAL, this will give you the expected result. (Note, no ticks and no EVAL.)
This is noteworthy, because it allows you to mix the two notations, RPN's post-fix and algebraic's in-fix, on the same line of code. But I digress.

My recommendation remains to download and read the HP-28S or HP-50g manuals and play through the examples given there. They're good manuals.

Cheers.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron