HP Forums

Full Version: Collaborative collection of useful userRPL snippets
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
The more I browse this forum or the net for userRPL solutions, the more I think that a library of "generic" programs would help. "generic" between quotes because what is generic may vary from use to user.

I know that ideally there is hpcalc.org, the general software library on this forum, wiki4hp and so on to collect such snippets, but it is also true that I saw this or that snippet always isolated, never a library to collect them in one place.

Hence this thread (that may fail, but it is worth trying). Another point of this thread is to solve problems that sounds generic enough to warrant a solution for this library. I saw some interesting questions posted on the facebook group (unfortunately those messages are hard to backup for the moment) that could be a nice input to assemble quick but effective little programs.

For example for me generic is a little command that computes the median of a list (or a column of a matrix, and so on). Although many people here will be able to write it on the spot, can often happen that someone wants to do something else and does not want to write first all the helper routines, even if they are small.

I already collected a couple of commands by myself, but not much. Those are in the hp calc torrent and I will upload them from time to time to the hpcalc.org and the wiki4hp. The more "doors" through which a person can get to a point, the better.

Now what I want to emphasize is that I see often something mentioned like "oh those are just 3-4 commands, they don't deserve to be in a library". Well in my opinion they do if they may help several users in several programs relieving them to know how to assemble those 3-4 commands. Furthermore those 3-4 commands may require some experience with the RPL library of this or that calculator, so they may be not that easy to replicate. They can be also used as examples of what one can do knowing the RPL library. Or they can be used as pointers for other commands that are similar.

One could pack them in an utility library and then that is, the library is available to everyone, in one place and hopefully indexed by the common search engines. Thus also the commands similar to those in the 1 minute marvels pdf are highly appreciated.

So I put the collected commands here: http://www.wiki4hp.com/doku.php?id=rpl:start
or here (when I cannot access wiki4hp) https://app.assembla.com/spaces/various-...grams.urpl
or here (if assembla closes the free accounts) https://de.osdn.net/users/pier4r/pf/vari...grams.urpl

you can go and edit everything (I did not spent a lot of time to organize them, first come the collection).

If you have any command to suggest, please do. Already a link to the source code is enough. I will report every addition here in the first post.

Changelog

Using wiki comments: http://www.wiki4hp.com/doku.php?id=rpl:s...=revisions

- 2018-03-09. Added weighted and harmonic average of lists, converting examples from the 50g user guide (see page 8-15 and subsequent ones) .
Refactored the order of programs and information. Added the program row vector to column vector, column vector to row vector, list to row vector
- 2018.03.03. Started the attempt to slowly build a library of useful snippets.
So here some questions (rephrased) that I find interesting from the facebook group (see also http://www.hpmuseum.org/forum/thread-9679.html )

TODOs
- How do you sort a matrix by column (define a sort metric that for you makes sense) or by row?

- by extension, any other matrix/vector operations that you find useful not present in the base RPL library?
- Added weighted and harmonic average of lists, converting examples from the 50g user guide (see page 8-15 and subsequent ones) .
- Refactored the order of programs and information. Added the program "row vector to column vector" (page 9-20 hp50g user manual). Same with column vector to vector and list to vector.

http://www.wiki4hp.com/doku.php?id=rpl:start

--
It is interesting that useful code is spread through the manuals, not only for pre PDF hp models (that is: those that came with the printed manuals). One would presume that with newer models useful code would be written in a way that is easy to copy and paste on the calculator. Instead it is exposed as procedure. The exposition is useful to understand better the algorithm, but it is not immediate to think it as a program.

So I am going to randomly check the 50g user guide, then maybe the 50g AUR. If I am not mistaken in the first part of the AUR there may be some useful code as well, like percentiles or something similar. I won't go through the entire AUR though, unless I end up casually on interesting snippets.

As usual, contribute! This is the reason the code is on the wiki, so we can all edit it.

--

Another thought that crossed my mind while testing the snippets was whether such statistical operations on lists, could be way faster when done in sysRPL a la David listExt, or the internal RPL functions are already pretty quick with whatever input size.
- added info about RDM. Added programs exposed in the 50g user guide to transform n lists of equal size in a matrix, using the lists as columns or as rows.

Link, always the same: http://www.wiki4hp.com/doku.php?id=rpl:start&do=

As usual, be welcomed to contribute! Programs, or refactoring (if it adds in usability), comments, links, fixes, examples, info, etc...

One can also contribute uploading the file from time to time to hpcalc.org (I'll do it too).
- Added functions related to random output, like selecting 'N' random numbers without repetition
- many other little changes. See http://www.wiki4hp.com/doku.php?id=rpl:s...=revisions

Code: http://www.wiki4hp.com/doku.php?id=rpl:start&do=

Suggestions and additions are always welcome.
While searching for this and that topic, as I remember I saw some RPL algorithms exposed here related to the topic I am searching, I observed that:

- In this http://www.hpmuseum.org/forum/thread-7955.html thread I asked something 2-3 times, until I finally remember it. (check for the double @@ comments) Damn me. This shows quite a patience from the community.
- There are plenty of RPL gems already in this new forum, but either one has a very poignant keyword/sentence, or the chances to find them with a generic keyword/sentence are very low. One has to read through the entire forum to find them.

Anyway, added some commands to the collection from Joe Horn and DavidM. As usual, contributions are welcomed.

http://www.wiki4hp.com/doku.php?id=rpl:s...=revisions
Unless I'm missing the point of the collection, I'd suggest changing the "randInt" entry from
Code:
randInt
  \<<
    RAND * 1 + IP
  \>>
to
Code:
randInt
  \<<
    RAND * CEIL
  \>>
Done! http://www.wiki4hp.com/doku.php?id=rpl:s...=revisions

Are you not registered on the wiki? You could do it on your own and report the change here if you want. Otherwise people have to wait until I have time to fix/add the suggestion.
I just had a quick look and noticed that the angle between vectors snippet is using the arccosine of the dot product. This has some numeric issues. Better is this formula:

\[angle\_between(\,\vec v_1, \vec v_2) =
\begin{cases}
\pi - 2 \, arcsin \frac{\mathopen|-\vec v_2 - \vec v_1\mathclose|}{2}, &\quad \vec v_1 \boldsymbol{\cdot} \vec v_2 < 0 \\
2 \, arcsin \frac{\mathopen|\,\vec v_2 - \vec v_1\mathclose|}{2}, &\quad\text{otherwise.} \\
\end{cases}
\]
Interesting bit Paul! As you can see from the library, I trusted the 50g user guide. I am going to insert your fix when I have time (or you can).

May I ask you to explain also why it is better? For reference. Thanks!
It comes down to taking an arccosine of a number near to ±1. The result is close to zero but you are also likely to lose up to half the possible digits. Remember that there are more numbers close to zero than any other number in floating point arithmetic and that cosine is very flat when it takes a value of ±1.

For fun, try plotting: \( arccos(1-x) \) and \( 2 \, arcsin(\sqrt{\frac{x}{2}}) \) for very small \( x > 0 \). These two are mathematically equivalent.

There is also a worse possibility here, where the number rounds to > 1 or < -1 causing an error.
Hello Paul,

why you don't like to use that expression for finding the angle between to vectors?

\[ angle between(\vec{v}_1,\vec{v}_2)= arccos\left(\frac{\vec{v}_1 \cdot \vec{v}_2}{|\vec{v}_1 || \vec{v}_2|}\right) \].

It is brutal force... but in RPL it is easy to manage:

Code:
<< \-> v1 v2 
    << v1 v2 DOT v1 ABS / v2 ABS / ACOS >>
>>

Sorry I forgot if the angle is greater than 90 degrees (or pi/2) then take 180 degrees minus the result above (or pi - result).
Double sorry, I've now understand your display task in your last post.
As I noted, using arccosine results in a significant loss of accuracy for some input. The alternative formula doesn't.

Pauli
Although I did not fixed the angle between vector routine yet (I added a link though) the collection is proceeding, very slowly. At this pace it may include what is useful and available __today__ in the public domain - assuming it will be still available - in some 30 years. Any contribution is appreciated. Therefore the code is on a wiki, everyone can go there and edit it leaving a comment for the change.

changelog: http://www.wiki4hp.com/doku.php?id=rpl:s...=revisions
http://www.wiki4hp.com/ is at the moment down for me. This would hurt a lot as I assumed it was a stable site (maybe it is just an hiccup, I hope so) to put effort on it over time.

Is it down also for you?
(11-17-2018 11:35 PM)pier4r Wrote: [ -> ]http://www.wiki4hp.com/ is at the moment down for me. This would hurt a lot as I assumed it was a stable site (maybe it is just an hiccup, I hope so) to put effort on it over time.

Is it down also for you?

Ok for me (in NY, USA) on 181117 at 1849hrs
(11-17-2018 11:35 PM)pier4r Wrote: [ -> ]http://www.wiki4hp.com/ is at the moment down for me. This would hurt a lot as I assumed it was a stable site (maybe it is just an hiccup, I hope so) to put effort on it over time.

Is it down also for you?

Also OK for me (Spain, 08:00 AM)

V.
Ok works for me again. Still it is a point to ask the admin if we can include every now and then a dump of that wiki as well.

Thanks for your reports!
please consider this snippet, for an arbitrary base logarithm:

Code:
logyx: LOG SWAP LOG /

It uses the fact that logb(x) (log x to base b) is == loga(x) / loga(b), where a is any base.

I won't add to the wiki myself, since I don't quite get the syntax, and haven't tried it on my 50g yet, only as the equivalent RPN on my DM42.

thanks.
(01-12-2019 09:43 PM)cdmackay Wrote: [ -> ]please consider this snippet, for an arbitrary base logarithm:

Code:
logyx: LOG SWAP LOG /

It uses the fact that logb(x) (log x to base b) is == loga(x) / loga(b), where a is any base.

I'd guess that LOG uses LN internally, so to make it just that little bit more accurate and faster I'd probably use:

Code:
\<< LN SWAP LN / \>>

The CORDIC algorithms that I know get you the natural logarithm of a number, not the decimal logarithm. Internally, the machine probably calculates \( \frac {ln(x)} {ln(10)} \) with \( ln(10) \) stored as a constant in ROM. At least that's how my floating point library does it Smile

To use this as a programmable function in its own right then use local variables:

Code:
\<< \-> b x \<< x LN b LN / \>> \>>

Store it as "LOGBX" and you can invoke it as LOGBX(2,8) for example, which should return 3.

Need to analyse the function at some point? Make it an algebraic:

Code:
\<< \-> b x 'LN(x)/LN(b)' \>>

If you now want to derive that function, you can (on a 48GX):

Code:
'LOGBX(B,X)' 'X' ∂

That gets you:

Code:
'1/X/LN(B)'
Pages: 1 2
Reference URL's