Wow Daniel,
I am sincerely impressed at how you wrestled that one to the ground.
A trick I learned a while ago on these forums was the use of Sparse ($.)
toy e. _1
1 0 0 0
0 0 1 1
0 0 0 0
$. toy e. _1 NB. converts dense array to sparse form
0 0 │ 1
1 2 │ 1
1 3 │ 1
4 $. $. toy e. _1 NB. Dyadic 4 $. returns the indices of sparse form
0 0
1 2
1 3
Cheers, bob
> On Jul 11, 2019, at 4:20 PM, Daniel Eklund <[email protected]> wrote:
>
> Hi everyone,
>
> I’m looking for some newbie help. I feel I’ve come so far but I’ve run
> into something that is making me think I’m not really getting something
> fundamental.
>
> Rather than try to come up with a contrived example, I’ll just say outright
> that I’m trying to solve one of the project Euler questions (problem 44)
> and in my desire to use a particular J strategy (‘tabling’) I’m struggling
> to deduce how array indexing to recover the input pairs works best.
>
> From the question:
>
> A pentagonal number is Pn=n(3n−1)/2. The first ten pentagonal numbers are:
>
> 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, …
>
> The challenge is to find two pentagonal numbers that both added together
> and whose difference is also a pentagonal number.
>
> Producing pentagonals is easy enough:
>
> pentagonal =: [ * ( 1 -~ 3 * ])
>
> pentagonal >: i. 5
>
> 2 10 24 44 70
>
> My plan was then to table both the addition and subtraction
>
> +/~ pentagonal >: i. 5
>
> 4 12 26 46 72
>
> 12 20 34 54 80
>
> 26 34 48 68 94
>
> 46 54 68 88 114
>
> 72 80 94 114 140
>
> -/~ pentagonal >: i. 5
>
> 0 _8 _22 _42 _68
>
> 8 0 _14 _34 _60
>
> 22 14 0 _20 _46
>
> 42 34 20 0 _26
>
> 68 60 46 26 0
>
> And then fetch the values in the table that were also pentagonal. This
> seems like a sane strategy -- not entirely clever -- but I am running into
> something that makes me feel I am missing something essential: how to
> recover the numbers (horizontal row number and column number pair, and
> therefore input values) that induced the result.
>
> I create myself a toy example to try to understand, by creating a hardcoded
> matrix where I’m interested in those that have a certain value:
>
> ] toy =: 3 4 $ _1 2 12 9 32 23 _1 NB. _1 will be value of interest
>
> _1 2 12 9
>
> 32 23 _1 _1
>
> 2 12 9 32
>
> toy e. _1 NB. I am searching for _1 (a proxy for a truth function like
> "is_pentagonal")
>
> 1 0 0 0
>
> 0 0 1 1
>
> 0 0 0 0
>
> I cannot easily find the indices using I., because
>
> I. toy e. _1
>
> 0 0
>
> 2 3
>
> 0 0
>
> Has a zero in the first row which is semantically important, but
> zero-padding (semantically unimportant) in other locations.
>
> I realize I can ravel the answer and deduce (using the original shape) the
> indices
>
> I. , toy e. _1
>
> 0 6 7
>
> NB. The following is ugly, but works
>
> (<.@:%&4 ; 4&|)"0 I. , toy e. _1 NB. The 4 hardcoded is the length
> of each item
>
> ┌─┬─┐
>
> │0│0│
>
> ├─┼─┤
>
> │1│2│
>
> ├─┼─┤
>
> │1│3│
>
> └─┴─┘
>
> And voilà a table of items of row/column pairs that can be used to fetch
> the original inducing values.
>
> To get to the point: is there anything easier? I spent a long time on
> NuVoc looking at the “i.” family along with “{“. I feel like I might be
> missing out on something obvious, stipulating that there is probably
> another way to do this without tabling and trying to recover the inducing
> values.
>
> Is my desire, i.e. to table results and simultaneously keep pointers back
> to the original values (a matter of some hoop jumping) the smell of an
> anti-pattern in J ?
>
> Thanks for any input.
>
> Daniel
>
> PS. In my question I purposefully am ignoring the obvious symmetry of
>
> +/~ pentagonal >: i. 5
>
> As I am interested in the _general_ case of tabling two different arrays:
>
> ( pentagonal 20+i.6) +/ pentagonal >: i. 5
>
> 1182 1190 1204 1224 1250
>
> 1304 1312 1326 1346 1372
>
> 1432 1440 1454 1474 1500
>
> 1566 1574 1588 1608 1634
>
> 1706 1714 1728 1748 1774
>
> 1852 1860 1874 1894 1920
>
> And recovering their indices.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm