I get:
   (-~&(^&3%3:)/ -: *:qromb_nr_) 1 2
|domain error
|   PolyInterpY=:PolyInterpY    (j-1)}~u trapzdNext ab

But the locale is right, which is all I was concerned with.

That said, there's another issue - we are working with an anonymous
verb, so we must manually set the locale back to what it was before we
exit. (The automatic locale switching only happens with named verbs.)

Meanwhile... conjunctions are a bit trickier.

However, you can write an adverb or conjunction which returns an
adverb or a conjunction. The adverb which returns an adverb is
probably the easiest to reason about syntactically (because adverbs
and conjunctions have a long left scope). Anyways, you can use this to
bring in arbitrarily many arguments. (Though you do need to think
about how you terminate the mess, if you are getting overly
ambitious.)

Still, you wind up building a gerund to pass multiple verbs where they
need to be. Which is basically what you were suggesting, and I think
that that's all that's needed here.

So, for example (being a bit verbose, but this is a verbose issue...):

conj_example_=: 2 :0
  u`v conj_implementation_example_ (coname '')
)

conj_implementation_example_=: 2 :0
  return_locale=. coname''
  cocurrent v
  '`u v'=. u
  NB. code goes here
  return_value=. expression y NB. fix this expression
  cocurrent return_locale
  return_value
)

I hope this helps,

-- 
Raul




On Wed, Apr 5, 2017 at 2:29 AM, Xiao-Yong Jin <[email protected]> wrote:
> Hi Pascal, Henry, and Raul,
>
> First, thank you for giving me suggestions.  Allow me to reply you all in 
> this one message.
>
> Raul, 'qromb' does a numerical integration, so
>    (-~&(^&3%3:)/ -: *:qromb) 1 2
> returns 1, meaning integrating *: from 1 to 2 is the same as 2-&(^&3%3:)1 or 
> 2-&(*:d._1)1
> I got your code to work by putting back 'u' in those adverbs.
> But the code changes the current locale to nr after calling qromb_nr_ once, 
> and I don't see an easy
> way to get back to the calling locale.
> I also don't like rewriting the adverb into a conjunction.  What would I do 
> with a conjunction?
> Maybe I can rewrite it into a verb accepting gerunds.  I'll try looking into 
> this direction.
>
> Pascal, it really takes some time to wrap my head around your code.  I want 
> to have a generic method
> that would work for both adverb and conjunction, and have an interface that 
> works like a normal J adverb
> or conjunction.  Inspired by your verb and ti, I found the following work 
> around satisfying.
>
> NB.for a simple object/locale, with a monadic adverb a_t_ waiting to be 
> wrapped
>    a_t_=:1 : 'echo (u y);(5!:1<''u'');''a_t_'';y;(coname'''')label_.d'
>    d_t_=:'d_t_'
> NB.create an interface adverb aa_t_ for a_t_ and hide the data d_t_ via a 
> verb av_t_
>    aa_t_=:1 : '(cofullname&.>u`'''') av_t_ y'
>    av_t_=:4 : 'x`:6 a y'
>
> Henry, I see the complication.
> Assuming all names are fully qualified with locales (otherwise we stick the 
> current locale to it like cofullname does)
> What about letting 'v_lv_ a_la_' executes: switch to la, run 'v_lv_ a', 
> switch back?  Similar behavior for conjunctions.  Does the locale has to be 
> tied to anonymous verbs?
> I may have some misunderstanding, but my impression of the execution model is 
> that 'v_lv_ y' executes
> 1. get the value of 'y'
> 2. switch to 'lv'
> 3. get the value of 'v_lv_'
> 4. run 'v_lv_ y' by following the definition of 'v_lv_'
> 5. switch back
>
>
>
>
>> On Apr 4, 2017, at 9:01 PM, 'Pascal Jasmin' via Programming 
>> <[email protected]> wrote:
>>
>>
>>> should the anonymous verb created by
>>
>>
>> verb adv_locale_
>>
>> automatically be executed in (locale)?
>>
>>
>> The existing behaviour can be leveraged with duck typing in mind.
>>
>> One easy workaround I didn't get too deep in is based on this magic fork
>>
>> inl_z_ =: ((cocurrent@] ".@] [)"1 0 boxopen)
>>
>>
>> executes string x in  every locale y
>>
>> A conjunction version lets you access local parameters,
>>
>> lr_z_ =: 3 : '5!:5 < ''y'''
>> inlC_z_ =: 2 : ' (([: <^:(0=L.) v"_) inl~ m ,"1 '' '',"1 lr@:]) :  (([: 
>> <^:(0=L.) v"_) inl~ (lr@:[),"1 '' '' ,"1 m ,"1 '' '',"1 lr@:] )'
>>
>> inlA =: 1 : 'u inlC  (18!:5 '''')'
>>
>> ab_far_ =: 1 : (':';'x + a u y')
>> a_far_ =: 3
>>
>> a_base_ =: 33
>>
>> a ' + ab ' inlA_far_ 3
>> 39
>>
>> b =: 4 : 'x + a + y'
>>
>> 2 ' b_base_ ab ' inlA_far_ 3
>> 41
>>
>> in jpp, this can be
>>
>>
>> 2  ( b_base_ ab  inlC.: far) 3
>> ________________________________
>> From: Henry Rich <[email protected]>
>> To: [email protected]
>> Sent: Tuesday, April 4, 2017 8:12 PM
>> Subject: Re: [Jprogramming] locales with adverbs and conjunctions?
>>
>>
>>
>> I have been thinking about this & I don't see a better solution than
>> Raul's.  @Raul: think about putting something in NuVoc explaining this.
>>
>> I thought at first: should the anonymous verb created by
>>
>> verb adv_locale_
>>
>> automatically be executed in (locale)?  That would solve the immediate
>> problem.
>>
>> But it leaves us with the responsibility of defining a locale for every
>> anonymous verb.  What locale should we assign to:
>>
>> (V0 V1_locale_ V2)
>>
>> (V0_locale0_ V1_locale1_ V2_locale2_)
>>
>> (V0_locale0_ V1)
>>
>> ?
>>
>> I haven't been able to come up with something I would be willing to
>> suggest to Roger & Ken.
>>
>> Unless someone does, I guess we will leave it as is.  The locale suffix
>> gives an easy way to specify the locale of a verb - 98% of the cases -
>> and for the rest you need Raul's Device.
>>
>> Henry Rich
>>
>>
>>
>> On 4/4/2017 4:03 AM, Raul Miller wrote:
>>> I am not sure how to test this (I do not know what values to use for f
>>> a and b in 'f qromb a b' and do not feel like studying your code
>>> enough to deduce such values), but I think this is what you are asking
>>> for:
>>>
>>> ---------- 8< ------ cut here ------ 8< ----------
>>>
>>> coclass'nr'
>>>
>>> qromb=:1 :0
>>>   qromb_implementation (coname '')
>>> )
>>>
>>> qromb_implementation=:2 :0
>>> NB.Returns the integral of the function from a to b.  NR P.166 Ch.4.3
>>> NB.Uses Romberg's method of order 2*K, where, e.g. K=2 is Simpson's rule.
>>> NB.u            function
>>> NB.y            a,b,eps integration bounds(a,b), and accuracy(eps,
>>> default 1e_10)
>>> NB.eps is the fraction error from extrapolation error estimate
>>> NB.PolyInterpX stores successive trapezoidal relative stepsizes
>>> NB.PolyInterpY stores their approximations
>>> NB.K is the number of points used in the extrapolation
>>>         cocurrent v
>>>         ab=.2{.y [ eps=.2{y,1e_10
>>>         PolyInterpY=:20#0 [ PolyInterpX=:21#0 [ PolyInterpM=:K=.5
>>>         PolyInterpX=:PolyInterpX 0}~1
>>>         TrapzdNextN=:0
>>>         j=.1 while.j<:#PolyInterpX do.
>>>                 PolyInterpY=:PolyInterpY(j-1)}~u trapzdNext ab
>>>                 if.j>:K do.'ss dy'=.0 polyInterpRawinterp j-K
>>> if.(|dy)<:eps*|ss do.ss return.end.end.
>>> NB.This is key.  The factor 0.25 allows h^2 extrapolation.  See NR
>>> equation 4.2.1.
>>>                 PolyInterpX=:PolyInterpX j}~0.25*PolyInterpX{~j-1
>>>         j=.>:j end.
>>>         'Too many steps in routine qromb'assert 0
>>> )
>>>
>>> polyInterpRawinterp=:4 :0
>>> NB.Polynomial interpolation.  NR P.119 Ch.3.2
>>> NB.x            the point of interpolation
>>> NB.y            j       subrange j+i.PolyInterpM is used for the 
>>> interpolation
>>> NB.Must initialize
>>> NB.PolyInterpM=:5
>>> NB.PolyInterpX=:21#0
>>> NB.PolyInterpY=:20#0
>>>         j=.y
>>>         dif=.|x-j{PolyInterpX
>>>         i=.0 while.i<PolyInterpM do.
>>>                 if.dif>dift=.|x-PolyInterpX{~j+i do.ns=.i [ dif=.dift end.
>>>         i=.>:i end.
>>>         d=.c=.PolyInterpY ];.0~ j,:PolyInterpM
>>>         ns=.<:ns [ y=.PolyInterpY{~j+ns
>>>         m=.1 while.m<PolyInterpM do.
>>>                 i=.0 while.i<PolyInterpM-m do.
>>>                         ho=.x-~PolyInterpX{~j+i
>>>                         hp=.x-~PolyInterpX{~j+i+m
>>>                         w=.(c{~i+1)-i{d
>>>                         'PolyInterp error'assert 0~:den=.ho-hp
>>>                         den=.w%den
>>>                         d=.d i}~hp*den
>>>                         c=.c i}~ho*den
>>>                 i=.>:i end.
>>>                 if.(PolyInterpM-m)>2*ns+1 do.dy=.c{~ns+1
>>>                 else.ns=.<:ns [ dy=.ns{d
>>>                 end.
>>>                 y=.y+dy
>>>         m=.>:m end.
>>>         y,dy
>>> )
>>>
>>> trapzdNext=:1 :0
>>>         trapzdNext_implementation (coname '')
>>> )
>>>
>>> trapzdNext_implementation=:2 :0
>>> NB.Returns the nth stage of refinement of the extended trapezoidal
>>> rule.  NR P.163 Ch.4.2
>>> NB.u            function                must accept list
>>> NB.y            a,b     range
>>> NB.Must initialize TrapzdNextN=:0 before using.
>>>         cocurrent v
>>>         ba=.-~/y
>>>         TrapzdNextN=:>:TrapzdNextN
>>>         if.1=TrapzdNextN do.TrapzdNextS=:-:ba*+/u y return.
>>>         
>>> else.TrapzdNextS=:-:TrapzdNextS+ba*t%~+/u({.y)+(0.5+i.t)*ba%t=.2^TrapzdNextN-2
>>> return.
>>>         end.
>>> )
>>>
>>> ---------- 8< ------ cut here ------ 8< ----------
>>>
>>> This is a rather bulky example, if there are problems with this
>>> approach it might be better to define a more concise (and complete -
>>> with a test case which illustrates the problem) example?
>>>
>>> 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to