Hi,
R. E. Boss’s thread gave me an idea for an interesting problem:
Given two vectors v and s of the same length, find the permutation w of v such
that
((s=x)#w) -: /:~(s=x)#v
for any scalar x in s.
I have two solutions; the first is more obvious and relies on the key adverb
and boxing, whereas the second (more elegant IMO) uses no boxing and relies on
the grade verb, much like progressive dyadic index and company:
r=: /:@/:
csk=: r@[ { <@/:~/. ;@/: ~.@[
prm=: (] {~ r@:{~ i. r@[) /:
csr=: prm { ]
csk uses key and can be generalized to apply operations other than sorting to
each class in v.
csr is faster though (on my phone in j701 at least), and uses prm which
generates the permutation corresponding to the desired operation first.
Here is an example where s -: -*v, so elements in the result must match the
sign of the corresponding element in v:
v
4 3 _2 _5 1 _4 _3 0 _1 2
-s
1 1 _1 _1 1 _1 _1 0 _1 1
s srt v
4 9 3 5 1 6 2 7 8 0
s csr v
1 2 _5 _4 3 _3 _2 0 _1 4
I’d be curious to see what elegant solutions the forum can come up with!
Cheers,
Louis
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm