On 5/22/07, Terrence Brannon <[EMAIL PROTECTED]> wrote:
To be an array processing language, I am surprised to find the Pike code more
readable for this particular problem. Please spruce up my J code.

compose =: 4 : 0
a =. ((0 { x) * (0 { y))
b =. ((0 { x) * (1 { y)) + ((1 { x) * (3 { y))
c =. ((2 { x) * (0 { y)) + ((3 { x) * (2 { y))
d =. ((2 { x) * (1 { y)) + ((3 { x) * (3 { y))
a,b,c,d
)

I guess each of us has our own ways of approaching these
kinds of problems.  Here's how I might do it.

First off, I'd almost certainly use lists of those indices:
x1=: 0 0 2 2
x2=: 4 1 3 3

y1=: 0 1 0 1
y2=: 4 3 2 3

I used 4 for an index which selects 0.  Here's a first cut
at a solution which uses this approach:

  compose1=: (((x1{[) * (y1{])) + (x2{[) * y2{])&(,&0)

Note, however, that this assumes that x and y both have
length 4, but that I will not throw an error if this assumption
is violated -- if my assumption is violated, I would give a
wrong answer.  Does this matter for you?  If so, replace 4
with _1

Or, here's another approach (borrowing June Kim's num):

num=:".;._2

xInds=:num 0 :0
1 1 3 3
0 2 4 4
)

yInds=:num 0 :0
1 2 1 2
0 4 3 4
)

compose2=: ((xInds{[) +/@:* yInds{]) & (0&,)

Here, I renumbered the indices and used 0 to select
my 0s.  If you don't like that, you can replace 0&, with
,&0 and go back to the same indicing scheme I used
for compose 1

Here's yet another approach:

X=: +/ .*
L=: X ((i.4 4 4)e.4#.(2#0 2),.(i.4),.4$0 1) X ]
R=: X ((i.4 4 4)e.4#._ 1 3 3,.(i.4),._ 3 2 3) X ]
compose3=: L + R

A potential disadvantage of compose3 is that it assumes that its
left argument is rank 1.  But since your original is also not well
behaved on arguments with rank greater than 1, maybe that's not an
issue.  On the other hand, maybe compose3's treatement of
rank 0 arguments is a problem.  I'm also not sure about
timing (but if you're only working with rank 1 args, that's probably
not an issue for the definition of compose -- if timing for rank1
args becomes a problem you probably need to address that at
a different level of abstraction).

Or, lets say that you just want something as close as possible to
the pike expression (what does "readable" mean to you?).  For that
I might do something like:

compose4=:4 :0
 t=. ((0{x)*0{y),            ((0{x)*1{y)+(1{x)*3{y
 t, (((2{x)*0{y)+(3{x)*2{y), ((2{x)*1{y)+(3{x)*3{y
)

However, note that compose4, like your original, assumes that both
your arguments are rank 1.

Or, ...  there's plenty of other possibilities.  But what do you mean
by "readable"?

Thanks,

--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to