Nope, I didn't take my thinking far enough. Those last two lines
require some significant duct tape and glue if I want to "minimize"
recalculation.

But... one thing I could do is glue the ucnt and uval values together
(since they have the same length). And, then, when I calculate sndx, I
can glue that on top of the other two (since it also had the same
length). And, then, when I compute reps, I can glue that to the top of
the stack (since it is also the same length). This greatly simplifies
value passing, since I can now pick whichever of these four values I
want to work with. But that still leaves useg buried in the middle, so
I might as well make useg the same length as everything else and throw
it onto the stack.

This works, but it doesn't seem like it was a worthwhile exercise:

undx=: [: +/\ 2~:/\(,~ 1+{.)
ucvs=: (#/.~@] ,: {./.~) undx NB. uval: _1{  ucnt: _2{
useg=: ,~ 1,[: ((2 ~:/\ ]) , 0#~0<#)@:* 2 -/\ {: NB. useg: _3{
reps=: ,~ _2&{ * 1, 1+1}._3&{ NB. reps: _4{
sndx=: ,~ +/\@(_4&{) NB. sndx: _5{
segs=: _3&{@]`((_5{])-_2{])`(0#~(<_5 _1){])}~ NB. _6{
result=: <;.1 (_4&{) # _1&{
updowns=: (result~ segs)@sndx@reps@useg@ucvs

Oh well... at least it's harder to read. Or, something like that.

-- 
Raul

On Fri, May 21, 2021 at 9:01 PM Raul Miller <rauldmil...@gmail.com> wrote:
>
> After thinking about this for a little bit, I have decided that it
> should be *almost* straightforward to convert this definition to a
> fully tacit form.
>
> The simplest tactic would be to define tacit expressions for each of
> the local names in the definition, but that would result in
> significant repeat work.
>
> But we can use trains to calculate and save two values and of course a
> third value can be calculated inside the train if it's used only once.
> So, looking at the structure of the definition:
>
> labels=: ;:'y undx uval ucnt useg reps sndx segs result'
> reset=:{{
>   connections=:0$~2##labels
> }}
> depends=:{{
>   connections=:1 ({(labels i.;:x);(#labels)-.~labels i.;:y)} connections
> }}
>
>   reset''
>   'uval' depends ' undx {./. y'
>   'ucnt' depends ' #/.~ undx'
>   'useg' depends ' ((2 ~:/\ ]),0#~0<#) * 2 -/\ uval'
>   'reps' depends ' ucnt*1,1+useg'
>   'sndx' depends ' +/\reps'
>   'segs' depends ' (1,useg) (sndx-ucnt)} 0#~{:sndx'
>   'result' depends ' segs<;.1 reps#uval'
>
> We can also see that each value is used by at most two other values
> which is promising:
>    +/connections
> 1 2 2 2 2 2 1 1 0
>
> And, we can remind ourselves that each value depends on at most three
> other values, which is also promising:
>    +/"1 connections
> 0 0 2 1 1 2 1 3 3
>
> But those last two could use some further inspection:
>
> Looking at the computation for segs:
>    (labels i.;:'useg sndx ucnt') { +/connections
> 2 1 2
>
> sndx is only used once, so it could be computed inside a train. But,
> it appears twice in the expression, so we would need to rearrange
> things to eliminate that as a problem. Perhaps the simplest approach
> would be to replace those two uses with +/\reps and +/reps.
>
> Looking at the result, we see:
>    (labels i.;:'segs reps uval') { +/connections
> 1 2 2
>
> Here, segs itself is used only once.
>
> After this inspection, it seems like it should be completely
> straightforward to convert that definition of updowns to a fully tacit
> nested train representation.
>
> And, perhaps this way of thinking about things would be of interest to
> some others here in the forums?
>
>
> --
> Raul
>
> On Fri, May 21, 2021 at 8:23 AM Raul Miller <rauldmil...@gmail.com> wrote:
> >
> > Ok...
> >
> > I didn't feel like trying to make this tacit:
> >
> > updowns=:{{
> >   undx=. +/\ 2 ~:/\ (,~ 1+{.) y
> >   uval=. undx {./. y
> >   ucnt=. #/.~ undx
> >   useg=. ((2 ~:/\ ]),0#~0<#) * 2 -/\ uval
> >   reps=. ucnt*1,1+useg
> >   sndx=. +/\reps
> >   segs=. (1,useg) (sndx-ucnt)} 0#~{:sndx
> >   segs<;.1 reps#uval
> > }}
> >
> > This is of course quite a bit longer than your Foo, but also somewhat
> > faster on long sequences in my trivial tests.
> >
> > It's less space efficient, perhaps because I've retained all
> > intermediate results until the end.
> >
> > I hope this helps,
> >
> > --
> > Raul
> >
> >
> >
> >
> > On Fri, May 21, 2021 at 7:21 AM R.E. Boss <r.e.b...@outlook.com> wrote:
> > >
> > > Yep.
> > >
> > >
> > > R.E. Boss
> > >
> > >
> > > -----Original Message-----
> > > From: Programming <programming-boun...@forums.jsoftware.com> On Behalf Of 
> > > Raul Miller
> > > Sent: vrijdag 21 mei 2021 12:47
> > > To: Programming forum <programm...@jsoftware.com>
> > > Subject: Re: [Jprogramming] ups & downs
> > >
> > > Ok... so... this is correct behavior?
> > >
> > >    Foo 1 10 1 # 1 2 1
> > > +---------------------+---------------------+
> > > |1 2 2 2 2 2 2 2 2 2 2|2 2 2 2 2 2 2 2 2 2 1|
> > > +---------------------+---------------------+
> > >
> > > Thanks,
> > >
> > > --
> > > Raul
> > >
> > >
> > > On Fri, May 21, 2021 at 4:09 AM R.E. Boss <r.e.b...@outlook.com> wrote:
> > > >
> > > >   ud 2#1 2 1 2 1
> > > > +-------+-----+-----+-----+
> > > > |1 1 2 2|2 1 1|1 2 2|2 1 1|
> > > > +-------+-----+-----+-----+
> > > >   Foo 2#1 2 1 2 1
> > > > +-------+-------+-------+-------+
> > > > |1 1 2 2|2 2 1 1|1 1 2 2|2 2 1 1|
> > > > +-------+-------+-------+-------+
> > > >
> > > > the latter being the required, maximal length ups & downs.
> > > >
> > > > OTOH, Foo does not handle correct your first two values 4#1  and 3#1 2.
> > > > But they are rather trivial ones, being constant or only one up (or 
> > > > down), I can live with that. An easy check could filter that out.
> > > >
> > > >
> > > > R.E. Boss
> > > >
> > > >
> > > > -----Original Message-----
> > > > From: Programming <programming-boun...@forums.jsoftware.com> On Behalf 
> > > > Of Raul Miller
> > > > Sent: vrijdag 21 mei 2021 00:32
> > > > To: Programming forum <programm...@jsoftware.com>
> > > > Subject: Re: [Jprogramming] ups & downs
> > > >
> > > > Bugfix:
> > > >
> > > > reps=: 1+0,~0,2~:/\[:[^:(0~:[)/\.&|.^:2@:*2 -/\]
> > > > ud=: reps ((1,2 =/\ [ # #\@]) <;.1 #) ]
> > > >
> > > > This fixes a problem where the duplicate values appeared at the end of 
> > > > the sequence.
> > > >
> > > > Here are some values to test against:
> > > >    4#1
> > > >    3#1 2
> > > >    2#1 2 1 2 1
> > > >
> > > > I think I've dealt with all of these correctly (though of course, 
> > > > there's some ambiguity where a run of equal values appears at the edge 
> > > > of an up/down border). But let me know if I should be doing this 
> > > > differently.
> > > >
> > > > Thanks,
> > > >
> > > > --
> > > > Raul
> > > >
> > > > On Thu, May 20, 2021 at 12:17 PM Raul Miller <rauldmil...@gmail.com> 
> > > > wrote:
> > > > >
> > > > > Oh...  I see...
> > > > >
> > > > > I noticed that the ravel of the boxes did not match the original
> > > > > sequence and did not look close enough after that.
> > > > >
> > > > > Anyways, I guess here's how I would approach this:
> > > > >
> > > > > reps=: 1+0,~0,2~:/\[:[^:(0~:[)/\.@:*2 -/\]
> > > > > ud=: reps ((1,2 =/\ [ # #\@]) <;.1 #) ]
> > > > >
> > > > > Thanks,
> > > > >
> > > > > --
> > > > > Raul
> > > > >
> > > > > On Thu, May 20, 2021 at 10:42 AM R.E. Boss <r.e.b...@outlook.com> 
> > > > > wrote:
> > > > > >
> > > > > > I thought it was clear from the output (therefore I used ?. in the 
> > > > > > testcases) that the order of the input array should not be altered.
> > > > > > That was the constraint you were missing.
> > > > > >
> > > > > >
> > > > > > R.E. Boss
> > > > > >
> > > > > >
> > > > > > -----Original Message-----
> > > > > > From: Programming <programming-boun...@forums.jsoftware.com> On
> > > > > > Behalf Of Raul Miller
> > > > > > Sent: donderdag 20 mei 2021 16:33
> > > > > > To: Programming forum <programm...@jsoftware.com>
> > > > > > Subject: Re: [Jprogramming] ups & downs
> > > > > >
> > > > > > I could use some additional details.
> > > > > >
> > > > > > I mean... it seems to me that Foo=:<@/:~ would minimally satisfy 
> > > > > > the requirements which have been stated so far.
> > > > > >
> > > > > > But, presumably, there's some kind of "cost function" which we are 
> > > > > > trying to minimize, and presumably that has something to do with 
> > > > > > how pairs are handled?
> > > > > >
> > > > > > Or, perhaps, you have a constraint that no more than three of any 
> > > > > > given number may appear in any box, and I think I see a constraint 
> > > > > > that adjacent boxes must alternate their order. But perhaps there 
> > > > > > are also similar additional metrics or constraints?
> > > > > >
> > > > > > Thanks,
> > > > > >
> > > > > > --
> > > > > > Raul
> > > > > >
> > > > > > On Thu, May 20, 2021 at 6:43 AM R.E. Boss <r.e.b...@outlook.com> 
> > > > > > wrote:
> > > > > > >
> > > > > > > Given an array of numbers, like
> > > > > > >
> > > > > > >    ?.@#~20
> > > > > > > 14 16 8 6 5 8 6 16 16 19 13 12 3 1 9 12 17 0 9 5
> > > > > > >
> > > > > > > there is always a sequence of (non-strict) ups & downs, like
> > > > > > >
> > > > > > >    14 16;16 8 6 5;5 8;8 6;6 16 16 19;19 13 12 3 1; 1 9 12 17;17
> > > > > > > 0;0
> > > > > > > 9;9 5
> > > > > > > +-----+--------+---+---+----------+------------+---------+----+---+---+
> > > > > > > |14 16|16 8 6 5|5 8|8 6|6 16 16 19|19 13 12 3 1|1 9 12 17|17 0|0
> > > > > > > |9|9
> > > > > > > |5|
> > > > > > > +-----+--------+---+---+----------+------------+---------+----+---+---+
> > > > > > >
> > > > > > > Well, this is the way I prefer to have the ups & downs, where 
> > > > > > > they share their border terms.
> > > > > > > Which verb makes this transformation?
> > > > > > > Elegance, as always, is appreciated, correctness is a must, 
> > > > > > > efficiency a blessing.
> > > > > > >
> > > > > > > Some testcases:
> > > > > > >
> > > > > > >    Foo ?.@#~25
> > > > > > > +----+---+---+-------+-----+-----+----+---+---+----+-------+----+----+-------+----+----+----+----+------+---+
> > > > > > > |19 6|6 8|8 6|6 10 23|23 16|16 21|21 1|1 9|9 8|8 22|22 13 1|1
> > > > > > > |14|14
> > > > > > > |2|2 17 20|20 9|9 15|15 7|7 22|22 4 2|2 9|
> > > > > > > +----+---+---+-------+-----+-----+----+---+---+----+-------+----+----+-------+----+----+----+----+------+---+
> > > > > > >    Foo ?.@#~30
> > > > > > > +--------+------+----+--------+--------+---------+----+-----+-----+----+----+-----+-----+----+-------+-------+-------+--------+-----+-----+
> > > > > > > |24 26 28|28 6 5|5 28|28 16 16|16 16 26|26 19 3 2|2 13|13 11|11
> > > > > > > |19|19
> > > > > > > |2|2 27|27 10|10 19|19 5|5 17 27|27 19 7|7 24 28|28 27 14|14 22|22
> > > > > > > |2|21|
> > > > > > > +--------+------+----+--------+--------+---------+----+-----+-----+----+----+-----+-----+----+-------+-------+-------+--------+-----+-----+
> > > > > > >
> > > > > > >
> > > > > > > R.E. Boss
> > > > > > > ------------------------------------------------------------------
> > > > > > > ---- 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
> > > ----------------------------------------------------------------------
> > > 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

Reply via email to