Skip to content

Commit d625253

Browse files
committed
Support boxed y in D.
1 parent 062c00d commit d625253

2 files changed

Lines changed: 384 additions & 21 deletions

File tree

‎jsrc/cad.c‎

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ static A jtadres(J jt,A f){A x,nm;
118118
ASSERTN(x!=0,EVVALUE,nm); ASSERTN(AT(x)&VERB,EVDOMAIN,nm);
119119
R ADPLAINV(x)?x:f;
120120
}
121-
ASSERTN(QCWORD(x=syrd(nm,jt->locsyms))!=0,EVVALUE,nm); // ordinary lookup
121+
// ordinary lookup (unquote in sc.c)
122+
if(!(NAV(nm)->flag&(NMLOC|NMILOC))&&!(AR(jt->locsyms)&ARHASACV)){
123+
if((x=jtsyrd1((J)((I)jt+NAV(nm)->m),NAV(nm)->s,NAV(nm)->hash,jt->global))!=0)x=SETNAMEDFAOWED(x);
124+
}else x=syrd(nm,jt->locsyms);
125+
ASSERTN(QCWORD(x)!=0,EVVALUE,nm);
122126
if(ISFAOWED(x))tpush(QCWORD(x)); x=QCWORD(x);
123127
ASSERTN(AT(x)&VERB,EVDOMAIN,nm); // the name's part of speech changed since the reference was made
124128
R ADPLAINV(x)?x:f;
@@ -719,15 +723,15 @@ static C jtadinneridb(J jt,A f){V*v; A g,h,t; C c=0; ADLOC s1[ADMAXLOC],s2[ADMAX
719723
// get verb's signed ranks, for the monad and for the left and right arguments of the dyad.
720724
static I jtadrankmb(J jt,A f){V*v=FAV(f);
721725
if(ISDCAP(f))R jtadrankm(jt,DCAPU(f));
722-
R v->id==CQQ?(I)v->localuse.lu1.srank[0]:(I)v->mr;
726+
R v->id==CQQ&&!(v->flag&VGERL)?(I)v->localuse.lu1.srank[0]:(I)v->mr; // gerund"n has rank _
723727
}
724728
static I jtadranklb(J jt,A f){V*v=FAV(f);
725729
if(ISDCAP(f))R jtadrankl(jt,DCAPU(f));
726-
R v->id==CQQ?(I)v->localuse.lu1.srank[1]:lr(f);
730+
R v->id==CQQ&&!(v->flag&VGERL)?(I)v->localuse.lu1.srank[1]:lr(f);
727731
}
728732
static I jtadrankrb(J jt,A f){V*v=FAV(f);
729733
if(ISDCAP(f))R jtadrankr(jt,DCAPU(f));
730-
R v->id==CQQ?(I)v->localuse.lu1.srank[2]:rr(f);
734+
R v->id==CQQ&&!(v->flag&VGERL)?(I)v->localuse.lu1.srank[2]:rr(f);
731735
}
732736

733737
// classifying predicates see through names in the right locale
@@ -830,12 +834,13 @@ static A jtadwinidx(J jt,B out,I x,I n,I a){A W; I s,l,L,*wv;
830834
static DF1(jtadcellm){DF12IP;
831835
V*v=FAV(self); A f=v->fgh[0],*dv=AAV(v->fgh[1]); A p,t,pz,tz;
832836
I k=IAV(C(dv[2]))[0], mode=IAV(C(dv[2]))[1], i=IAV(w)[0]; A P=C(dv[0]),T=C(dv[1]);
837+
B b=!!(AT(P)&BOX); t=ADZ; // boxes have no tangent (their contents carry it)
833838
switch(mode){
834-
case 0: RZ(p=from(w,P)); if(AT(P)&BOX)t=ADZ; else RZ(t=from(w,T)); break;
835-
case 1: RZ(p=take(sc(i+1),P)); RZ(t=admap2(ds(CTAKE),sc(i+1),0,T,TR(T))); break;
836-
case 2: RZ(p=drop(sc(i),P)); RZ(t=admap2(ds(CDROP),sc(i),0,T,TR(T))); break;
839+
case 0: RZ(p=from(w,P)); if(!b)RZ(t=from(w,T)); break;
840+
case 1: RZ(p=take(sc(i+1),P)); if(!b)RZ(t=admap2(ds(CTAKE),sc(i+1),0,T,TR(T))); break;
841+
case 2: RZ(p=drop(sc(i),P)); if(!b)RZ(t=admap2(ds(CDROP),sc(i),0,T,TR(T))); break;
837842
default: {A ix; RZ(ix=jtadwincell(jt,mode==4,IAV(C(dv[2]))[2],AS(P)[0],i));
838-
RZ(p=from(ix,P)); RZ(t=admap2(ds(CFROM),ix,1,T,TR(T))); break;}
843+
RZ(p=from(ix,P)); if(!b)RZ(t=admap2(ds(CFROM),ix,1,T,TR(T))); break;}
839844
}
840845
RZ(jtadfwd1(jt,f,p,t,k,&pz,&tz));
841846
RZ(tz=admat(tz,pz)); RZ(tz=adexpand(tz,pz));
@@ -1021,8 +1026,9 @@ static I jtadagendaf(J jt,A f,A x,A Tx,A y,A Ty,I k,A*pz,A*tz){A s,gb,gk,inv,*fv
10211026
}
10221027
// u\ y (mode 1) and u\. y (mode 2) for nonlinear u: apply u to each prefix/suffix (the cell loop, jtadcellm)
10231028
static I jtadscan(J jt,A f,I mode,A P,A T,I k,A*pz,A*tz){A d,cv,z,g; I n;
1024-
RZ(T=admat(T,P));
1025-
if(!AR(P)){RZ(P=ravel(P)); RZ(T=reshape(over(sc(k),sc(1)),T));}
1029+
B b=!!(AT(P)&BOX); // boxes: no tangent
1030+
if(!b)RZ(T=admat(T,P));
1031+
if(!AR(P)){RZ(P=ravel(P)); if(!b)RZ(T=reshape(over(sc(k),sc(1)),T));}
10261032
n=AS(P)[0];
10271033
if(!n){
10281034
RZ((z=AO1(mode==1?bslash(f):bsdot(f),P)));
@@ -1061,7 +1067,7 @@ static I jtadinfixf(J jt,A f,B out,A x,A P,A T,I k,A zg,A*pz,A*tz){A z,W,b,tb; I
10611067
#define ADSUFFOLD(f) (AT(f)&VERB&&FAV(f)->id==CSLASH&&FAV(f)->fgh[0]&&AT(FAV(f)->fgh[0])&VERB)
10621068
// computee g/\. y and its tangent in forward mode
10631069
static I jtadsuffold(J jt,A f,A P,A T,I k,A*pz,A*tz){A g=FAV(f)->fgh[0],zb,a,ta; I n=AS(P)[0];
1064-
RZ(T=admat(T,P));
1070+
if(!(AT(P)&BOX))RZ(T=admat(T,P)); // boxes have no tangent
10651071
A tsb,*ts; RZ(zb=jtadboxlist(jt,n));
10661072
GATV0(tsb,INT,n,1); ts=(A*)IAV(tsb); // the tangents of the steps, until they are boxed
10671073
RZ(a=from(sc(n-1),P)); RZ(ta=jtaditem(jt,T,n-1)); adboxset(zb,n-1,a); ts[n-1]=ta;
@@ -1147,7 +1153,7 @@ static I jtadfoldx(J jt,struct foldstatus*fi,A f,A x,A Tx,A y,A Ty,I k,B rec,A*p
11471153
if(zs&0b10000)break; // 1 Z: the last step
11481154
if(cnt>0&&!--cnt)break;
11491155
}
1150-
if(0){zstop: if(zs&0b00001)R 0; RESETERR;} // _3 Z: (the fold limit) is an error _2 Z: ends the fold with the results so far
1156+
if(0){zstop: if(zs&0b00001)R 0; RESETERR;} // _3 Z: (the fold limit) is an error _2 Z: ends the fold with the results so far
11511157
ASSERT(nz,EVNORESULT);
11521158
if(!mult){*pz=z; *tz=tzz; R 1;}
11531159
AN(zb)=AS(zb)[0]=nz;
@@ -1303,6 +1309,8 @@ static B adboxwalk(A f){
13031309
switch(FAV(f)->id){
13041310
case CAT: case CATCO: case CAMP: case CAMPCO: case CFORK: case CHOOK: case CTILDE: case CQQ:
13051311
case CUNDER: case CUNDCO: case CPOWOP: case CSLASH: R 1;
1312+
ADFOLDCASES: R 1;
1313+
case CADVERSE: case CCOLON: case CMCAP: case COBVERSE: R 1;
13061314
case CATDOT: R ADISAGENDA(FAV(f));
13071315
default: R 0;
13081316
}
@@ -1833,11 +1841,10 @@ static A jtadsolvek(J jt,A c,A y,A z){A t; I n=AS(y)[0],k=AS(c)[0];
18331841
static A jtadselx(J jt,C id,A x,A y){A c,d;
18341842
PUSHCCT(1.0) c=AO2(ds(id==CMIN?CLE:CGE),x,y); d=c?AO2(ds(id==CMIN?CLT:CGT),x,y):0; POPCCT
18351843
RZ(d);
1836-
if(AT(c)&AT(d)&B01&&AN(c)==AN(d)){A z; B*cv=BAV(c),*dv=BAV(d);
1844+
B*cv=BAV(c),*dv=BAV(d);
18371845
if(!memcmp(cv,dv,AN(c)))R c; // no tie: c, boolean
1838-
GATV(z,FL,AN(c),AR(c),AS(c)); D*zv=DAV(z); DO(AN(c), zv[i]=0.5*(cv[i]+dv[i]);) R z; // a constant: one pass
1839-
}
1840-
R tymes(scf(0.5),plus(c,d));
1846+
A z; GATV(z,FL,AN(c),AR(c),AS(c)); D*zv=DAV(z);
1847+
DO(AN(c), zv[i]=0.5*(cv[i]+dv[i]);) R z;
18411848
}
18421849

18431850
// x -. y
@@ -3585,9 +3592,9 @@ static B adpsethas(ADPSET*s,A x){I lo=0,hi=s->n-1;
35853592
static I jtadboxchk(J jt,A z,ADPSET*s){
35863593
STACKCHKOFL
35873594
DO(AN(z), A c=C(ADBOXE(z,i));
3588-
if(!c||adpsethas(s,c)||!ISZ(adtget(c))||ACISPERM(AC(c)))continue;
3595+
if(!c||adpsethas(s,c)||ACISPERM(AC(c)))continue; // held in the arguments (traced or not) or a constant
35893596
if(AT(c)&BOX){RZ(jtadboxchk(jt,c,s)); continue;}
3590-
if(AT(c)&NUMERIC&&AN(c)){jtadrefusefinal(jt); R 0;} // number copied from traced data into a box: not supported
3597+
if(AT(c)&NUMERIC&&AN(c)){jtadrefusefinal(jt); R 0;} // number made while f ran is refused
35913598
)
35923599
R 1;
35933600
}
@@ -3596,7 +3603,7 @@ static I jtadboxchk(J jt,A z,ADPSET*s){
35963603
static A jtadnorule(J jt,A f,A z,A x,A y){ADPSET s={0,0,0}; I r;
35973604
if(!z)R 0;
35983605
if(z==x||z==y)R z;
3599-
if(AT(z)&BOX?0:!(AT(z)&NUMERIC)||!AN(z)||!ISZ(adtget(z)))R z; // text (not followed), empty or traced (passed through)
3606+
if(AT(z)&BOX?0:!(AT(z)&NUMERIC)||!AN(z))R z; // text (not followed) or empty
36003607
if((x&&!jtadpsetadd(jt,&s,x))||!jtadpsetadd(jt,&s,y)||!jtadpsetverb(jt,&s,f)){free(s.v); RE(0); ASSERT(0,EVWSFULL);}
36013608
qsort(s.v,s.n,sizeof(A),adpcmp);
36023609
if(AT(z)&BOX)r=jtadboxchk(jt,z,&s);
@@ -3756,8 +3763,74 @@ static A jtadnegcellv(J jt,A self,B dyad,ADLOC*ls,I*nls){A f,v; I n=IAV(FAV(self
37563763
R fdef(0,CDCAPDOT,VERB,jtdcap1cell,jtdcap2cell,v,0,0,VFLAGNONE,RMAX,RMAX,RMAX);
37573764
}
37583765

3766+
// u D.n where y is array of boxes. Each box holds array of numbers (any shape, empty of any type allowed).
3767+
// The result gives one block per box:
3768+
// for n=1 it has the shape of y and box i holds ($u y),$>i{y
3769+
// for n=2 it has shape ($y),$y and box (i,j) holds ($u y),($>i{y),$>j{y (Hessian in blocks) and so on.
3770+
// D. is called from C, so an error from u or D.'s refusal is reported at the user's sentence as for unboxed y.
3771+
static const C adboxpos[]="(((+/\\ - ])&.,)@:(*/&>) +&.> i.&.>)@:($&.>)"; // positions
3772+
static const C adboxpack[]="; ,&.> , y"; // list
3773+
static const C adboxunp[]="[ {&.> <@]"; // boxes rebuilt
3774+
static const C adboxcut[]=":\n({\"(_,m)&y)@<&.> { m # < x"; // blocks
3775+
// Check the contents of boxed y
3776+
static I jtadboxychk(J jt,A w){A*wv=AAV(w);
3777+
DO(AN(w), A c=C(wv[i]);
3778+
if(!AN(c))continue; // empty (nothing to differentiate)
3779+
ASSERT(!(AT(c)&BOX),EVNONCE);
3780+
ASSERT(AT(c)&NUMERIC,EVDOMAIN);
3781+
)
3782+
R 1;
3783+
}
3784+
// The boxes of y with their contents in floating point, as an unboxed y is converted.
3785+
static A jtadboxyfl(J jt,A w){A*wv=AAV(w),z; I n=AN(w); B any=0;
3786+
DO(n, if(!(AT(C(wv[i]))&FL))any=1;);
3787+
if(!any)R w;
3788+
RZ(z=jtadboxlist(jt,n));
3789+
DO(n, A c=C(wv[i]);
3790+
if(!(AT(c)&FL))RZ(c=cvt(FL,c));
3791+
adboxset(z,i,c);
3792+
);
3793+
R reshape(shape(w),z);
3794+
}
3795+
// One cell of [x] u D.n y with y boxed. self is per-cell verb made in jtadboxed: fgh[0] is u, fgh[1] is n,
3796+
// fgh[2] a list of boxes holding verbs.
3797+
static A jtadboxcell(J jt,A a,A w,A self,B dyad){A u=FAV(self)->fgh[0],n=FAV(self)->fgh[1],*hv=AAV(FAV(self)->fgh[2]),p,f,d,dv,z;
3798+
df1(p,w,hv[0]); RZ(p); // positions
3799+
df1(f,w,hv[1]); RZ(f); // list
3800+
RZ(d=jtadmkdcap(jt,atop(dyad?amp(a,u):u,hv[2]),n)); // monad u@unpack D.n or dyad x&u@unpack D.n
3801+
df2(dv,p,f,d); RZ(dv); // derivative: shape ($u y),n#N
3802+
df2(z,p,dv,hv[3]); // blocks
3803+
R z;
3804+
}
3805+
static DF1(jtadbox1){DF12IP;PROLOG(0);A z; RZ(z=jtadboxcell(jt,0,w,self,0)); EPILOG(z);}
3806+
static DF2(jtadbox2){DF12IP;PROLOG(0);A z; RZ(z=jtadboxcell(jt,a,w,self,1)); EPILOG(z);}
3807+
// [x] u D.n y with y boxed. self is u D.n. Cells of y are taken at u's rank, as for unboxed y.
3808+
static A jtadboxed(J jt,A self,A a,A w,B dyad){A u=FAV(self)->fgh[0],n=FAV(self)->fgh[1],hv,v,b=0,z=0; I lc=0,rc; B neg; ADLOC ls[ADMAXLOC]; I nls=0;
3809+
RZ(jtadboxychk(jt,w)); RZ(w=jtadboxyfl(jt,w));
3810+
if(dyad){
3811+
I sl=adrankl(u),sr=adrankr(u); lc=efr(AR(a),sl); rc=efr(AR(w),sr); neg=sl<0||sr<0;
3812+
}else{
3813+
I sr=adrankm(u); rc=efr(AR(w),sr); neg=sr<0;
3814+
}
3815+
if(neg)RZ(u=jtadnegv(jt,u,dyad,ls,&nls)); // rank is applied below, so cells run u without its negative rank
3816+
if((hv=jtadboxlist(jt,4))){ // J verbs, made once
3817+
if((v=eval((C*)adboxpos)))adboxset(hv,0,v);
3818+
if(v&&(v=colon(num(3),cstr((C*)adboxpack))))adboxset(hv,1,v);
3819+
if(v&&(v=eval((C*)adboxunp)))adboxset(hv,2,v);
3820+
if(v&&(v=colon(num(1),cstr((C*)adboxcut)))&&(df1(v,n,v),v))adboxset(hv,3,v);
3821+
if(v)b=fdef(0,CDCAPDOT,VERB,jtadbox1,jtadbox2,u,n,hv,VFLAGNONE,RMAX,RMAX,RMAX); // one cell
3822+
}
3823+
if(b&&(dyad?lc<AR(a)||rc<AR(w):rc<AR(w)))b=qq(b,dyad?v2(lc,rc):sc(rc)); // several cells: b at u's rank
3824+
if(b){ // run b
3825+
if(dyad)df2(z,a,w,b);
3826+
else df1(z,w,b);
3827+
}
3828+
jtadlocexitn(jt,ls,nls);
3829+
R z;
3830+
}
37593831
static DF1(jtdcap1){DF12IP;PROLOG(0);A u,z;
37603832
if(ISSPARSE(AT(w)))RZ(w=denseit(w));
3833+
if(AT(w)&BOX){RZ(z=jtadboxed(jt,self,0,w,0)); EPILOG(z);} // y boxed: one block per box
37613834
u=DCAPU(self); // u for n=1, u D.(n-1) for n>1
37623835
I s=adrankm(u), c=efr(AR(w),s); // effective (cell) rank
37633836
if(adatomic1(u)){ // u provably works atom by atom
@@ -3776,6 +3849,7 @@ static DF1(jtdcap1){DF12IP;PROLOG(0);A u,z;
37763849
}
37773850
static DF2(jtdcap2){DF12IP;PROLOG(0);A u,z;
37783851
if(ISSPARSE(AT(a)))RZ(a=denseit(a)); if(ISSPARSE(AT(w)))RZ(w=denseit(w));
3852+
if(AT(w)&BOX){RZ(z=jtadboxed(jt,self,a,w,1)); EPILOG(z);} // y boxed: one block per box
37793853
u=DCAPU(self);
37803854
I sl=adrankl(u), sr=adrankr(u);
37813855
I lc=efr(AR(a),sl), rc=efr(AR(w),sr); // effective (cell) ranks

0 commit comments

Comments
 (0)