Here's my take on it:
NB.* rpl1z: replace 1 zero with 1 and _1.
rpl1z=: 13 : '(wh{.y),"1]1 _1,"(0 1) y}.~>:wh=. y i. 0'"1
NB.* flatten: elide first dimension if greater than matrix.
flatten=: 13 : '(,:,:)`,:`]`(,/) @. (0 1 2 i.[:#$)y'
NB.* rplZs: replace each zero with both 1 and _1.
rplZs=: 13 : 'flatten^:_ rpl1z^:(0+/ .=y)] y'"1
NB.* replaceZerosDHM: replace each zero in each row with 1 and _1
replaceZerosDHM=: 13 : '>,&.>/rplZs&.><"1 y'
It avoids too much boxing, though Mr. Boss's version performs much better:
replaceZerosREB=: 13 : '>;<@,@:{"1 (<1 _1)(0([EMAIL PROTECTED] <@#: I.@,@:=)
y)}<"0 y'
$MM=: _1 _1 _1 0 1 1 1{~?1000 8$7
1000 8
(replaceZerosDHM-:replaceZerosREB) MM
1
6!:2 'replaceZerosDHM MM'
0.090929434
6!:2 'replaceZerosREB MM'
0.012734579
My version doesn't scale as well either:
$MM=: _1 _1 _1 0 1 1 1{~?10000 8$7
10000 8
(replaceZerosDHM-:replaceZerosREB) MM
1
6!:2 'replaceZerosREB MM'
0.20282271
6!:2 'replaceZerosDHM MM'
6.1287549
On 3/9/08, Geoff Canyon <[EMAIL PROTECTED]> wrote:
>
> Ack, posted the next-to-last version. Here's the correct one:
>
> replaceZeroes =: (;@:;@:([([:<(((_1 1 (] i.&0)}"0 1 ])`])@.(# =
> i.&0)))\]))^:_
>
> Then
>
> _2 replaceZeroes 0 0
>
> _1 _1 _1 1 1 _1 1 1
>
>
> ...which is the desired result. The left argument determines how many
> elements there are in a sub-list.
>
> Still, it looks like your code is (far) better than mine. I wasn't
> familiar with catalogue before this, but it fits perfectly here.
>
> thanks!
>
>
> Geoff
>
>
>
>
> On Mar 9, 2008, at 3:15 PM, R.E. Boss wrote:
>
> > replaceZeroes 0 0
> > _1 _1 1 _1 _1 _1 1 1 _1 1 1 _1 _1 1 1 1
> >
> >> ;<@,@:{"1 (<1 _1)(0([EMAIL PROTECTED] <@#: I.@,@:=) M)}<"0 M=:0 0
> > 1 1
> > 1 _1
> > _1 1
> > _1 _1
> >
> > Apparently replaceZeroes generates too much.
> >
> >
> > R.E. Boss
> >
> >
> >> -----Oorspronkelijk bericht-----
> >> Van: [EMAIL PROTECTED] [mailto:programming-
> >> [EMAIL PROTECTED] Namens R.E. Boss
> >> Verzonden: zondag 9 maart 2008 23:11
> >> Aan: 'Programming forum'
> >> Onderwerp: RE: [Jprogramming] Turning 0 into _1 1 in a list
> >>
> >> Oops, last one should be
> >>
> >>> ;<@,@:{"1 (<1 _1)(0([EMAIL PROTECTED] <@#: I.@,@:=) M)}<"0 M
> >> 1 _1 1 1
> >> 1 _1 _1 1
> >> 1 1 _1 1
> >> 1 1 1 _1
> >> 1 1 _1 _1
> >> _1 1 1 _1
> >> _1 1 _1 _1
> >>
> >>
> >> R.E. Boss
> >>
> >>
> >>> -----Oorspronkelijk bericht-----
> >>> Van: [EMAIL PROTECTED] [mailto:programming-
> >>> [EMAIL PROTECTED] Namens R.E. Boss
> >>> Verzonden: zondag 9 maart 2008 22:57
> >>> Aan: 'Programming forum'
> >>> Onderwerp: RE: [Jprogramming] Turning 0 into _1 1 in a list
> >>>
> >>> [M=:1 _1 0 1,1 1 _1 1,:0 1 0 _1
> >>> 1 _1 0 1
> >>> 1 1 _1 1
> >>> 0 1 0 _1
> >>>
> >>> 0([EMAIL PROTECTED] <@#: I.@,@:=) M
> >>> +---+---+---+
> >>> |0 2|2 0|2 2|
> >>> +---+---+---+
> >>> NB. coordinates of zeroes
> >>>
> >>> (<1 _1)(0([EMAIL PROTECTED] <@#: I.@,@:=) M)}<"0 M
> >>> +----+--+----+--+
> >>> |1 |_1|1 _1|1 |
> >>> +----+--+----+--+
> >>> |1 |1 |_1 |1 |
> >>> +----+--+----+--+
> >>> |1 _1|1 |1 _1|_1|
> >>> +----+--+----+--+
> >>> NB. replace boxed zeroes by 1 _1
> >>>
> >>> <@{"1 (<1 _1)(0([EMAIL PROTECTED] <@#: I.@,@:=) M)}<"0 M
> >>> +--------------------+----------+----------------------+
> >>> |+--------+---------+|+--------+|+---------+----------+|
> >>> ||1 _1 1 1|1 _1 _1 1|||1 1 _1 1|||1 1 1 _1 |1 1 _1 _1 ||
> >>> |+--------+---------+|+--------+|+---------+----------+|
> >>> | | ||_1 1 1 _1|_1 1 _1 _1||
> >>> | | |+---------+----------+|
> >>> +--------------------+----------+----------------------+
> >>> NB. use catalogue to make all combinations
> >>>
> >>>> ,;<@{"1 (<1 _1)(0([EMAIL PROTECTED] <@#: I.@,@:=) M)}<"0 M
> >>> 1 _1 1 1
> >>> 1 _1 _1 1
> >>> 1 1 _1 1
> >>> 1 1 _1 1
> >>> 1 1 1 _1
> >>> 1 1 _1 _1
> >>> _1 1 1 _1
> >>> _1 1 _1 _1
> >>>
> >>>
> >>> replaceZeroes M
> >>> 1 _1 _1 1 1 _1 1 1 1 1 _1 1 _1 1 _1 _1 _1 1 1 _1 1 1 _1 _1 1 1 1 _1
> >>>
> >>>
> >>> R.E. Boss
> >>>
> >>>
> >>>> -----Oorspronkelijk bericht-----
> >>>> Van: [EMAIL PROTECTED] [mailto:programming-
> >>>> [EMAIL PROTECTED] Namens Geoff Canyon
> >>>> Verzonden: zondag 9 maart 2008 21:59
> >>>> Aan: Programming forum
> >>>> Onderwerp: Re: [Jprogramming] Turning 0 into _1 1 in a list
> >>>>
> >>>> I think the eventual solution I came up with was somewhat similar
> >>>> to
> >>>> this. Here's my solution:
> >>>>
> >>>> replaceZeroes =: (;@:;@:(_4([:<(((_1 1 (] i.&0)}"0 1 ])`])@.(# =
> >>>> i.&0)))\]))^:_
> >>>>
> >>>> I spent some time trying to figure out how to apply the replacement
> >>>> repeatedly before boxing and then unbox at the end, to minimize
> >> boxing/
> >>>> unboxing, but I couldn't get that to work. So in the above, the
> >> boxing/
> >>>> unboxing happens for each replacement. This isn't so bad in my
> >>>> particular case since no list has more than three zeroes in it.
> >>>> If the
> >>>> lists had many zeroes, the whole thing would come apart, since I'm
> >>>> starting with 512 lists of 27 elements, so a bunch of zeroes would
> >>>> quickly result in millions of lists. As it is, I end up with less
> >>>> than
> >>>> a thousand.
> >>>>
> >>>> Still I wonder that there seems to be no way to avoid boxing/
> >>>> unboxing?
> >>>> If I have a list and I want to transform that list, with the
> >>>> subsets I
> >>>> take potentially changing in length, I can't do that without boxes?
> >>>>
> >>>> Say I have a list like this: 1 2 3 4 5 6 7 8
> >>>>
> >>>> And I want the result to be a list that a duplicate for each
> >>>> prime in
> >>>> the list:
> >>>>
> >>>> 1 2 2 3 3 4 5 5 6 7 7 8
> >>>>
> >>>> Are boxes the only/best way?
> >>>>
> >>>> regards,
> >>>>
> >>>> Geoff
> >>>>
> >>>> On Mar 4, 2008, at 5:17 AM, Raul Miller wrote:
> >>>>
> >>>>> I have some comments in addition to R.E.Boss's comments
> >>>>> (and using his M)
> >>>>> M=:1 _1 0 1,1 1 _1 1,:0 1 0 _1
> >>>>>
> >>>>> A simple approach to supressing fill uses box (<) followed
> >>>>> by raze (;). In other words:
> >>>>>
> >>>>> <@(_1 1 (] i.&0)}"0 1 ])`<@.(# = i.&0)"1 M
> >>>>
> ----------------------------------------------------------------------
> >>>> 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
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm