RPL mini-challenge: Create an anti-Identity Matrix
|
05-12-2018, 05:48 PM
(This post was last modified: 05-12-2018 09:50 PM by DavidM.)
Post: #1
|
|||
|
|||
RPL mini-challenge: Create an anti-Identity Matrix
Inspired by Ángel Martin's 41CL quiz, I am intrigued by the concept of the "anti-Identity" matrix. Not having a 41CL prevents me from participating in the quiz, but I thought I might experiment with an RPL version of the first part his challenge: creating the anti-Identity matrix.
John Keith came up with a very similar RPL program to my first attempt, which essentially creates the matrix by generating a matrix of 1s and subtracting its identity from it. This makes for a compact solution, but it strikes me that it's not very internally efficient. The result is good, of course, but the majority of computation to create it is unnecessary (there's lots of 1-0 going on to get the final matrix). This extra computation slows it down. So here's the challenge: Given an integer input in stack level 1 representing the dimension of the square matrix, create the anti-Identity form as quickly as possible (all 1s except for 0s on the diagonal). Rules: - To simplify the approach, you may assume that the input is a valid integer > 1. - Standard User RPL commands only for HP 49G and later systems (no SysRPL, Saturn, SYSEVALs, or third-party commands). - To make it more interesting on the 48-series systems, strategic SYSEVAL commands may be used if desired (provided they aren't used to simply execute an entire block of SysRPL or Saturn code). - Winners will be based on execution time of an input of 30, but the program should work properly with other inputs as well. - Execution time is defined as the time to complete the program on real hardware as opposed to an emulator. - Assume approximate mode for both constants and calculations on systems that support exact integers (49G-49g+-50g-48gII). |
|||
05-12-2018, 06:37 PM
(This post was last modified: 05-12-2018 06:38 PM by Gerald H.)
Post: #2
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
Processes 30 in 1.4 sec on 50g.
SIZE 80. CKSUM # 14A3h Code: « → S |
|||
05-12-2018, 07:33 PM
Post: #3
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
Basic concept, squeezed a byte here and there:
Code:
And element-by-element concept: Code:
I don't have timings or byte counts since I did it in newRPL. Somebody with a 50g in hand could measure? |
|||
05-12-2018, 08:04 PM
(This post was last modified: 05-12-2018 08:05 PM by Zaphod.)
Post: #4
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix | |||
05-12-2018, 08:44 PM
(This post was last modified: 05-12-2018 08:48 PM by 3298.)
Post: #5
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
There is a weird little command called RDM that's probably not that useful in many contexts, but here it can do some work.
The idea is that a matrix like this: Code: [ Code: \<< Code: \<< |
|||
05-12-2018, 09:07 PM
Post: #6
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
Very good, 3298.
Your programme is faster for reals, but if you change the programme to \<< 0 1 PICK3 NDUPN 1. + \->ARRY OVER 1. - NDUPN ROW\-> SWAP DUP 2. \->LIST RDM \>> slower for integer matrix than my programme. |
|||
05-12-2018, 09:45 PM
Post: #7
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
These are some great solutions so far, please keep them coming!
I should have foreseen the exact vs. approximate number issue and clarified it before this came up, but I think the appropriate standard in this case is to assume approximate mode for all constants and calculations. Though there are obviously exceptions, I still think that should help more implementations than it hurts. I'm open-minded on the topic, though. If the majority feel otherwise, please speak up. |
|||
05-12-2018, 10:00 PM
Post: #8
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
Nice thread!
Why I don't see any Axl? The idea if the identity matrix complemented, although slow, is neat. Wikis are great, Contribute :) |
|||
05-12-2018, 10:16 PM
Post: #9
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix | |||
05-13-2018, 02:29 AM
(This post was last modified: 05-13-2018 02:36 AM by Claudio L..)
Post: #10
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
(05-12-2018 08:04 PM)Zaphod Wrote: 1st version 5.5secs ishThanks for the timings. Ouch, very slow... (05-12-2018 10:00 PM)pier4r Wrote: Why I don't see any Axl? It's not going to be any faster, but perhaps more minimalist: Code:
EDIT: Here's a second attempt at an element-by-element method: Code:
|
|||
05-13-2018, 02:52 AM
(This post was last modified: 05-13-2018 03:06 AM by DavidM.)
Post: #11
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
(05-13-2018 02:29 AM)Claudio L. Wrote:(05-12-2018 08:04 PM)Zaphod Wrote: 1st version 5.5secs ishThanks for the timings. Ouch, very slow... Claudio, I've tested your 2nd program on my 50g with much better results: With exact integers (input, constants, and output): 1.62s With approximate numbers (input, constants, and output): 0.33s I'm not sure about the difference with Zaphod's timings. Perhaps he was using a different calculator? The above figures were the average of 10 iterations using 30 (or 30.) as the input, each iteration being prefaced by a GC that was excluded from the timing window. I did have to make one small change, though: dropping the tick marks around K in the FOR loop declaration. That's probably a subtle difference between New- and Old-RPL. Edit: We were posting at (nearly) the same time. The timings above are for the second program in your original post, not the most recent one you posted. I'll post an update after I run the most recent one under the same conditions. I think it will be slower, though. |
|||
05-13-2018, 03:11 AM
Post: #12
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
...and Claudio's most recent program (included here for clarity):
Code: « Exact mode: 21.03s Approx. mode: 4.42s |
|||
05-13-2018, 03:19 AM
Post: #13
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
(05-13-2018 02:29 AM)Claudio L. Wrote: It's not going to be any faster, but perhaps more minimalist: I really like that this works in NewRPL! Unfortunately, it doesn't in standard UserRPL -- NOT isn't recursive into the sublists, so it results in a "Bad Argument Type" error on a standard 50g. |
|||
05-13-2018, 06:00 AM
Post: #14
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
(05-13-2018 03:19 AM)DavidM Wrote:(05-13-2018 02:29 AM)Claudio L. Wrote: It's not going to be any faster, but perhaps more minimalist: How about this: << IDN << NOT >> MAP >> <0|ɸ|0> -Joe- |
|||
05-13-2018, 06:02 AM
Post: #15
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
(05-13-2018 02:52 AM)DavidM Wrote: Claudio, I've tested your 2nd program on my 50g with much better results: Quote:I'm not sure about the difference with Zaphod's timings. Perhaps he was using a different calculator? It was definitely a 50g , I'll check it again later , I did run it one or three times just for an average. Quote:I did have to make one small change, though: dropping the tick marks around K in the FOR loop declaration. That's probably a subtle difference between New- and Old-RPL. oh yeah I had to remove the single quotes too of course, I forgot to mention. |
|||
05-13-2018, 06:52 AM
Post: #16
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
Slightly faster than my 1st effort:
SIZE 59.5 CKSUM # A3AAh Code: « DUP 1. →LIST 1 CON 1. PICK3 |
|||
05-13-2018, 08:54 AM
(This post was last modified: 05-13-2018 09:27 AM by pier4r.)
Post: #17
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
First and foremost I would like to write some opinions.
I learned in some threads of this forum that variety is really nice and healthy, so I am really happy to see different solutions, and not only those that are "fast" (or in many other threads "short"). Also I like when a problem lead to solutions of related problems or topics that do not solve the original problem, but are nonetheless valuable. My first approach is already covered by 3298 in short: Make a constant matrix, then subtract the identity matrix. With fixed values just for testing Code:
This is 2.49 seconds on my 50g. Although the creation of the two matrix themselves is quite fast (0.6 seconds or so) so it is the subtraction that takes time. PS: I recently checked on youtube a video from the HHC 2011, about speed comparisons between the 15c and the 15c LE. Namir did them. Well it is an interesting approach that can be done for userRPL (or in general) too. Putting single commands in a loop, at least the most common ones, and see how much time they take. Although such endeavor would be quite time consuming, but highly informative. update1: now I am attemping to use AXL (I will also use some libraries to go out of the box and have a comparison, as I wrote. Variety is nice). Code:
Code:
Wikis are great, Contribute :) |
|||
05-13-2018, 09:22 AM
Post: #18
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
You need a NEG or a SWAP in there somewhere otherwise the resulting matrix has -1 (instead of 1) everywhere but on the diagonal.
Code: \<< \-> n \<< n IDN { n n } 1 CON SWAP - \>> \>> |
|||
05-13-2018, 09:51 AM
(This post was last modified: 05-13-2018 10:13 AM by pier4r.)
Post: #19
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
Playing around, not yet done. The problem is the construction. The matrix routines are blazing fast but then editing them is a pain (subtraction for example). So better to construct them right directly.
Still fixed values (adapting with variables shouldn't be that hard but will cost some additional time). Edit: oh this idea is very similar to the one from Claudio already. Code:
The code from claudio (post http://www.hpmuseum.org/forum/thread-107...l#pid97587) here is pretty fast. It is like mine, only he is better, he is creating all the 900 elements on the stack (I create 30 elements each time) and then using the array command to compact them in a matrix. Neat. I did not know! The part that I never like is that without comments one has to debug the code to understand what the code is doing. And that shouldn't happen. Code:
Code:
Wikis are great, Contribute :) |
|||
05-13-2018, 10:27 AM
(This post was last modified: 05-13-2018 10:32 AM by pier4r.)
Post: #20
|
|||
|
|||
RE: RPL mini-challenge: Create an anti-Identity Matrix
OK thanks to Claudio I discovered that \->ARRY is pretty flexible (I did not know, therefore I was in love with AXL) and thanks to 3298 with his idea (see, comments! I likely had ignored his code without comments) it gets even faster.
I'd say that the merit is 60% the observation of 3298, 25% the properly picked commands by Claudio, 15% my luck combining and testing things while ranting about comments. Fixed values, but not difficult to modify with variables. Code:
For example with absence of comments or discussions I - I speak only for myself - will never bother to use code that is difficult to decode without copying it and debugging it. Unless getting a similar solution by myself (or via google) is harder. I picked the code of Claudio only because I got a similar solution by myself, and therefore I could "decode" it, plus David commented on it that was 0.3 seconds and added interest on it. Otherwise I wouldn't have checked it. edit: next step. using additional list processing commands, like listExt. Wikis are great, Contribute :) |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)