Using your examples:
x=:3 3 $i.9
y=:x+10
q=:2|x
q {"0 1 x,"0 y
0 11 2
13 4 15
6 17 8
We can express the selection using "Item Amend", the monadic form of u} ,
as others have noted:
q} x,:y
0 11 2
13 4 15
6 17 8
If you want to be able to calculate q on-the-fly, using tacit code,
here's a little helper utility:
mergeMask =: (&>) / (`>) } (@:,) (&<)
x (2|[) mergeMask y
0 11 2
13 4 15
6 17 8
It works on the same principles as x q} y , using gerund} to permit the
calculation of q from x and/or y at runtime. It's a bit convoluted
because the definition of gerund} doesn't readily support (i.e. actively
resists) accessing the monadic semantics of m} in a dyadic context, and
vice-versa. This limitation is imposed in the name of "consistency" [1],
but there's no syntactic requirement reason for it, and has frustrated
many of us on occasion, so I personally wish it were lifted.
Anyway, if you're interested, mergeMask basically joins x and y into a
single argument (i.e. a list of two boxes), which then can be processed by
} in a monadic context, giving us access to the semantics of (selection
indices) } (rank N+1 array of choices) . The trick is to build a gerund
argument to } which allows u (the merge mask calculator) to see x and y
separately, as it expects. This is accomplished by using the old
workhorse (&>)/ , which is the standard way of embedding a dyad in a
monad.
(2|[) mergeMask
(2 | [)&>/`>}@:,&<
Now, drum roll please....
x =: 3000 3000 $ i. 9
y =: x+10
q =: 2|x
ts =: 6!:2 , 7!:2@:]
10 ts 'q {"0 1 x,"0 y'
1.21649 4.02657e8
10 ts ' q} x,:y'
0.186725 6.71091e8
10 ts 'x ((2|[) {"0 1 ,"0) y'
1.22444 4.19434e8
10 ts 'x (2|[) mergeMask y'
0.155747 4.19439e8
So about an order of magnitude improvement in time using no additional
space.
-Dan
[1] In the "dictionary page for amend" thread from July 2006,
Roger said:
"The pattern is that the monadic case of gerund op
uses the monadic meaning of op while the dyadic case
uses the dyadic meaning.
In the case of gerund} , the dyadic case IS allowed,
but it is invoked via x gerund}y ."
http://www.jsoftware.com/pipermail/beta/2006-July/001528.html
----- Original Message ---------------
Subject: Re: [Jprogramming] Tacit J and indexed replacement
From: Erling Hellenäs <[email protected]>
Date: Mon, 07 Jul 2014 20:46:32 +0200
To: [email protected]
An example:
x=:3 3 $i.9
y=:x+10
q=:2|x
x
0 1 2
3 4 5
6 7 8
y
10 11 12
13 14 15
16 17 18
q
0 1 0
1 0 1
0 1 0
q {"0 1 x,"0 y
0 11 2
13 4 15
6 17 8
/Erling
On 2014-07-07 19:53, Erling Hellenäs wrote:
> It's obviously not possible to do any amendments in tacit code? It is
> also less elegant to pass these three parameters in the two arguments
> in tacit code? Any opinions about the use of From to do the same thing?
>
> NB. x and y are arrays of the same rank
> NB. q is a boolean, also of this rank
> NB. The expression merges x and y.
> NB. Where q is TRUE it picks from y, otherwise x
> NB. q {"0 1 x,"0 y
>
> If q is a vector and if we actually have a variable z of rank
> (+/q),}.$y we can easily create x from q#^:_1 [ z ? No use for any
> indexes?
>
> /Erling
>
> On 2014-07-07 12:33, Raul Miller wrote:
>> Another approach for this is:
>> (x*-.q)+y*q
>>
>> Sadly, that only works when x and y are numeric. Boxes and literals do
>> not have zero and 1 values (hypothetically "fill" could be zero, but
>> "1" is harder to rationalize.)
>>
>> A variant which uses amend might be:
>> (q#y) (I.q)} x
>>
>> This only works when x and y are rank 1, but you could also use
>> something like this for higher ranked arrays:
>> ($q)$ (q#&,y) (I.,q)} ,x
>>
>> (I hope I didn't make too many mistakes this time. I'm running without
>> any corrective support.)
>>
>> Thanks,
>>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm