Syntax Problems with map() and apply()
|
05-23-2016, 12:01 PM
Post: #1
|
|||
|
|||
Syntax Problems with map() and apply()
I want to use the map() or apply() function to stream over a list or vector but I keep getting a syntax error, even when I enter the example from the User Guide or Help! Here is the first example from Help for apply():
apply( x->x^3, [1 2 3] ) results in a syntax error message. The cursor is left after the first occurrence of x. I use these functions in other languages all the time. I kind of think of solutions in terms of them - very useful! Please help me how to properly call them in HP PPL. Thanks! |
|||
05-23-2016, 02:02 PM
Post: #2
|
|||
|
|||
RE: Syntax Problems with map() and apply()
This is a CAS function that will operate as advertised from the CAS view (button below the Esc key). To use this or any other CAS command from the Home view try
Code:
HTH Remember kids, "In a democracy, you get the government you deserve." |
|||
05-23-2016, 02:22 PM
Post: #3
|
|||
|
|||
RE: Syntax Problems with map() and apply()
Try this format:
[HOME] command line entry: CAS.apply("x→x^3",[1,2,3]); [CAS] command line entry: apply((x)->x^3,[1,2,3]); -Dale- |
|||
05-24-2016, 08:07 PM
(This post was last modified: 05-24-2016 11:57 PM by mark4flies.)
Post: #4
|
|||
|
|||
RE: Syntax Problems with map() and apply()
Thanks so much for your help. It has been some time since I programmed a calculator. I used the HP-41 extensively through chemistry graduate school and beyond, and later the HP-42 and HP-48 through HP-50 as well. I am getting back to it now with the HP Prime, but it is a very different machine in so many ways.
I was using Mark Power's blog to help. One of his early posts about the Prime involved programming a card game. His approach to shuffling used explicit loops. I am used to other computer languages that support data structures such as lists and vectors with functions like apply() and map() so I thought I might apply one of my usual techniques for randomization. We try to 'vectorize' operations for the sake of concise code and algorithm performance. In the case of the card deck, you generate a vector of 52 uniformly random variables, sort them into another vector, then find their position in the original, random vector. The positions (index) becomes a random index. You essentially transfer the randomness to the positions. This way is usually much simpler and cheaper than other methods. I thought I would try it out. Here is my program, with your help, just for this aspect of the game: #cas f(l1,l2) := apply('x->POS(l1,x),l2); #end EXPORT RANDOMIZE() BEGIN L1 := MAKELIST(RANDOM(),X,1,52); L2 := SORT(L1); RETURN f(L1,L2); END; The RANDOMIZE program returned these timings using Eddie's TIMESHUFFLE() program to time the shuffling of a 52-card deck: HP Prime: 0.073s iPad Pro app: 0.011s HP Prime Virtual Calculator (Windows): 0.00389 HP Prime Virtual Calculator (Macintosh): 0.00384s I am surprised that the vectorization approach performed so poorly versus the explicit looping approach, but there you have it. For an 'apples to apples' comparison, it took only 0.02s for Mark's original solution. It took mine more than three times as long! So I have a lot to learn about this machine and programming it, but many thanks already for getting me started down the right path! |
|||
05-26-2016, 06:13 AM
Post: #5
|
|||
|
|||
RE: Syntax Problems with map() and apply()
Very interesting trick! Really like it, I'll have to remember it and use it sometime.
Perhaps the timing difference lies in the time required to search a list versus a vector. Could using MAKEMAT in place of MAKELIST change the results? Those familiar with the implementation could weigh in, or you could try the experiment and see. ~ Mark Remember kids, "In a democracy, you get the government you deserve." |
|||
05-26-2016, 12:30 PM
Post: #6
|
|||
|
|||
RE: Syntax Problems with map() and apply()
apply and map are CAS commands, if you are using them with HOME commands and from HOME programs you have to pay all the conversion penalties.
|
|||
05-27-2016, 04:21 AM
Post: #7
|
|||
|
|||
RE: Syntax Problems with map() and apply()
Alternative method for generating a shuffled deck of 52 cards:
rand(52,1,52)►L1 [Note: rand must be spelled in lowercase] One step. No looping. TEVAL reports that this method takes 0.001 seconds in Home. That's pretty fast. <0|ɸ|0> -Joe- |
|||
05-27-2016, 08:01 AM
Post: #8
|
|||
|
|||
RE: Syntax Problems with map() and apply()
(05-27-2016 04:21 AM)Joe Horn Wrote: Alternative method for generating a shuffled deck of 52 cards: thank you Joe, very nice! I wonder why rand() hasn't an item in the Catalog of the Prime (Help)... Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
05-27-2016, 10:23 AM
Post: #9
|
|||
|
|||
RE: Syntax Problems with map() and apply()
Perhaps you'd rather deal with a vector:
ranm(52,1,52)►M0 // Col Vector -or- rand(52,1,52)►M1; // Row vector There's a slight difference between them when stored in a list variable, (L0, etc.), instead of a matrix variable, (M0, etc.). But you can deal with them that way, too! -Dale- |
|||
05-27-2016, 01:39 PM
Post: #10
|
|||
|
|||
RE: Syntax Problems with map() and apply()
I never found the rand() function so I created my own. The built-in function is very nice.
BTW, what is the method for generating the pseudo-random numbers on the HP Prime? Is it still the linear congruential generator? The standard now is the Mersenne Twister. The HP Prime capability (memory and speed) lend itself to serious applications beyond education and having a superior, modern RNG would be a big step forward. Thanks again for the help with the original question. I'm learning a lot! |
|||
05-27-2016, 02:07 PM
Post: #11
|
|||
|
|||
RE: Syntax Problems with map() and apply()
The undocumented rand() function appears to return a shuffling of the items when used with three arguments as Joe introduced it. (It has other behavior with 0, 1, or 2 arguments). On the other hand, ranm() appears to use sampling with replacement, so it can't be used for shuffling.
|
|||
05-27-2016, 03:07 PM
Post: #12
|
|||
|
|||
RE: Syntax Problems with map() and apply()
I don't understand the "shuffling" distinction, do you mean the method of element (card) extraction?
L0:=MAKELIST(ranm(52,1,52),X,1,4); // Deck shuffled 4 times: { [[31],[12],[2],[37],[2],[45],[27],[26],[39],[43],[51],[22],[52],[12],[41],[50],[10],[20],[12],[2],[26],[31],[38],[46],[50],[13],[15],[34],[49],[18],[27],[21],[10],[8],[31],[13],[40],[21],[39],[22],[35],[47],[20],[10],[41],[24],[30],[5],[48],[19],[4],[38]], [[9],[21],[20],[20],[1],[18],[14],[37],[49],[51],[8],[44],[52],[44],[29],[13],[16],[22],[27],[21],[44],[11],[38],[41],[20],[13],[18],[20],[6],[29],[35],[52],[37],[13],[50],[31],[15],[16],[1],[2],[17],[26],[50],[26],[30],[39],[13],[40],[23],[34],[44],[35]], [[25],[42],[27],[10],[43],[6],[51],[8],[50],[51],[3],[1],[37],[32],[23],[6],[6],[14],[14],[14],[23],[35],[15],[29],[36],[18],[20],[4],[29],[19],[34],[12],[2],[43],[37],[34],[42],[4],[13],[23],[39],[31],[34],[12],[22],[28],[37],[17],[49],[18],[10],[15]], [[17],[40],[19],[51],[11],[10],[5],[43],[40],[18],[40],[44],[1],[23],[36],[38],[36],[38],[44],[6],[50],[25],[16],[20],[38],[32],[29],[22],[24],[37],[18],[15],[34],[34],[17],[27],[11],[42],[48],[43],[39],[51],[51],[5],[9],[42],[10],[11],[27],[6],[20],[42]] } L1:=MAKELIST(rand(52,1,52),X,1,4); // Deck shuffled 4 times: { [29,1,4,15,36,23,7,35,6,2,5,38,16,45,32,12,18,51,26,21,13,44,46,22,52,41,17,10,48,43,33,47,24,49,8,3,30,37,31,39,28,14,27,19,9,11,20,40,25,34,50,42], [8,20,38,41,49,34,47,31,10,19,11,17,48,9,3,2,30,16,28,7,26,32,51,43,5,42,45,37,52,4,33,27,13,12,50,22,44,46,35,1,36,25,23,24,14,21,15,29,39,40,6,18], [44,36,9,43,50,47,42,24,25,46,31,48,3,27,13,29,11,20,40,26,35,51,4,17,23,30,6,52,32,7,5,10,12,37,21,15,8,1,49,18,28,45,39,19,16,41,2,38,22,14,34,33], [46,9,8,50,15,23,39,6,36,33,42,22,51,24,31,17,40,16,52,18,38,30,48,45,2,27,32,7,47,43,13,35,25,26,49,19,41,28,14,1,34,29,44,5,37,21,12,10,11,4,20,3] } Element selection: L0(1,2,1) ==> 12 L1(1,2) ==> 1 Matrix, vector, and list representations enable a wide variety of additional toolbox commands, (for further processing). -Dale- |
|||
05-28-2016, 07:16 AM
Post: #13
|
|||
|
|||
RE: Syntax Problems with map() and apply()
On the Prime, rand is using the following congruence
Code: r = unsigned ((1664525*ulonglong(r)+1013904223)%(ulonglong(1)<<31)) |
|||
05-28-2016, 08:45 PM
Post: #14
|
|||
|
|||
RE: Syntax Problems with map() and apply()
I don't have documentation for rand() or ranm() so it is 'trial and error.' (I don't care about the difference between a row or column vector depending on how it is stored yet.) When I use simple cases of ranm(n,1,n), I see 'sample with replacement,' not shuffling. For example, i run ranm(5,1,5) and I get [4 5 2 3 5], so it is not shuffling but sampling with replacement.
I might be mis-using this function but I have no documentation and I am following the posted pattern of ranm(n,1,n) to experiment. |
|||
05-28-2016, 08:46 PM
Post: #15
|
|||
|
|||
RE: Syntax Problems with map() and apply()
Parisse, thanks for clarifying algorithm for RNG. Is 'inside Giac' the same as CAS?
|
|||
05-28-2016, 08:49 PM
Post: #16
|
|||
|
|||
RE: Syntax Problems with map() and apply()
DrD, in your example of four repeated shuffling, I see index 31 appear thrice:
[[31],[12],[2],[37],[2],[45],[27],[26],[39],[43],[51],[22],[52],[12],[41],[50],[10],[20],[12],[2],[26],[31],[38],[46],[50],[13],[15],[34],[49],[18],[27],[21],[10],[8],[31],[13],[40],[21],[39],[22],[35],[47],[20],[10],[41],[24],[30],[5],[48],[19],[4],[38]], So it is a case of sampling with replacement using ranm() and not shuffling as with rand(). |
|||
05-28-2016, 09:32 PM
Post: #17
|
|||
|
|||
RE: Syntax Problems with map() and apply()
ranm() see help description for randMat <== note spelling!
|
|||
05-29-2016, 06:00 AM
Post: #18
|
|||
|
|||
RE: Syntax Problems with map() and apply() | |||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)