« first day (760 days earlier)      last day (1900 days later) » 

3:59 AM
0
Q: Arrays in J: indexing from one into another

teyvkUsing J I am trying to do something similar to the following example shown on page 128 of Mastering Dyalog Apl by Bernard Legrand (2009). I have not been able to find a direct conversion of this code into J, which is what I want. Here's the example. Quoting... BHCodes ← 83 12 12 83 43 66 50 81 ...

 
 
5 hours later…
9:25 AM
any clever way to sum diagonals of an array? can't seem to think of anything good
 
@dzaima Do you want the first sum to consist of only the corner?
 
@Adám yeah
 
@dzaima Which diagonals? /// or \\\?
 
@Adám /// but you can always add a
or
 
⍞←(¯1↓1⊥⍳∘≢⌽0∘×,⊢)3 3⍴⍳9
 
9:29 AM
@Adám 7 12 15 8 3
 
@dzaima ^ is \\\ from left to right. \\\ right to left is
⍞←(¯1↓1⊥⍳∘≢⌽0∘×,⍉)3 3⍴⍳9
 
@Adám 3 8 15 12 7
 
@Adám oh cool that's what i was trying right now. it'll take a bit of modification to work on non-square inputs though
 
⎕←(1↓¯1↓1⊥⍳∘≢⌽0∘×,⊢)⊃⎕←⊂3 4⍴⍳12
 
@Adám
┌──────────┐
│1  2  3  4│
│5  6  7  8│
│9 10 11 12│
└──────────┘
9 15 18 21 11 4
 
9:34 AM
@dzaima ^ non-square
 
1↓(-⍨/⍴)↓ though
 
@nathanrogers Anything that can be delivered as a dll should be usable with ⎕NA. Anything dll with a .NET interface should be usable with ⎕USING.
@dzaima OK, but you're on the right track.
 
i guess time to add dyadic to my apl
 
@dzaima Yeah, despite it not being as general as J's, it is really practical sometimes.
 
ngn
@dzaima short, fast, or just clever?
 
9:40 AM
@ngn APLy, "in-spirit"
 
ngn
:D
@Adám 0∘× -> ≠⍨
 
@ngn Yes as per , I wasn't trying to golf, and 0∘× is way more obvious.
 
ngn
@dzaima the apl-y way is to add zeroes, like in Adam's solution, and then reshape to a slightly narrower matrix
⍞←((+\0 ¯1+⍴)⍴+\∘⍴↑⊢)3 4⍴⍳100 ⍝ and then of course +⌿
 
@ngn
 
@ngn comments + bot = bad
 
ngn
9:47 AM
⍞←((+\0 ¯1+⍴)⍴+\∘⍴↑⊢)3 4⍴⍳12
 
@ngn 1 2 3  4  0  0
 
ngn
not what i expected... anyway, here's a tio link
 
@ngn will force a one-line output
 
ngn
another, maybe less aply, way:
⎕←{(,+/↑⍳⍴⍵)⊢⌸,⍵}3 4⍴⍳12
 
@ngn
 1  0  0
 2  5  0
 3  6  9
 4  7 10
 8 11  0
12  0  0
 
ngn
9:52 AM
and then +/
 
@ngn depends on the input being in order. is what I was currently using, with ugly variable modification in ⍺⍺
from an "APLy" solution I'd expect them to work for both </⍴ and >/⍴, which the previous ones don't
 
ngn
@dzaima what do you mean "in order"?
 
@ngn try ⌽3 4⍴⍳12 as an input
ignoring in ⍺⍺ of is always a sign of bugs if the argument isn't sorted
 
ngn
⎕←+/{(,+/↑⍳⍴⍵)⊢⌸,⍵}⌽3 4⍴⍳12
 
@ngn
4 11 21 18 15 9
 
ngn
10:02 AM
@dzaima looks fine to me. it sums the "/" diagonals, by the way
 
oohh you're using dyadically
 
ngn
10:19 AM
⎕←+⌿↑{0,⍵}⍀↓⌽3 4⍴⍳12
 
@ngn
4 11 21 18 15 9
 
ngn
solution using "scan" :)
 
@ngn a.k.a. "an ngn solution".
 
ngn
@Adám must live up to my reputation of a scan abuser :)
 
ngn
10:40 AM
@ktye i find the use of parse trees in tetradic ! and ? very strange
 
@ngn i guess that was made for supporting qsql from k
I think about putting it in the array indexes for tables:
T[rows;cols;group-by;accumulate]
 
ngn
10:57 AM
@ktye i'm not sure i understand this. is it like "select cols from T where id in rows group by ..." and then reduce using "accumulate"?
 
@ngn yes, and the result is a table again. If you leave out group-by and accumulate, than it's just normal indexing.
 
ngn
@ktye and the keys in the resulting table are whatever group-by returns? will the reduction be performed within each group separately?
 
the reduction would be within each group. If the reduction returns the same number as the original (e.g. scan), that's an implicit ungroup.
Group may be a list of keys, or a dict mapping the new column name to a gouping function.
@ngn Similar for accumulate: If nothing is grouped, that the complete table is used. The result get's the original column name, or a new name, if the accumulate function is stored in a dict.
 
ngn
@ktye "implicit ungroup" sounds like something that might happen by accident
 
@ngn I have no concept for a grouped table without accumulate. This makes sure the result is always a table.
 
11:51 AM
so my options for summing diagonals are this strange thing, or an O(n^2) scan. how about built-ins for getting diagonals?
 
12:09 PM
@dzaima what about dyadic transpose with repetitions? or is that only for the main diagonal?
 
@ktye 1 1⍉ gives only the main diagonal
 
ngn
12:40 PM
@dzaima how often do your users need that? if the answer is "very", you should implement it :)
 
 
1 hour later…
1:47 PM
@dzaima J has /. called "Oblique".
 
2:22 PM
@Adám oh cool :D
i guess it would make sense for it to be a monadic operator
 

« first day (760 days earlier)      last day (1900 days later) »