static A jtrdns(J jt,F f){A za,z;I n;size_t r,tr=0; GA(za,LIT,n=1024,1,0); clearerr(f); while(!feof(f) && (r=fread(CAV(za)+tr,sizeof(C),n-tr,f))){ tr+=r; if(tr==(U)n){RZ(za=ext(0,za));n*=2;} } if(tr==(U)n)z=za; else {GA(z,LIT,tr,1,0); MC(CAV(z),CAV(za),tr);} R z; } /* read entire file stream (non-seekable) */
static DF2(con2){A h,*hv,*x,z;V*sv; PREF2(con2); sv=VAV(self); h=sv->h; hv=AAV(h); GA(z,BOX,AN(h),AR(h),AS(h)); x=AAV(z); DO(AN(h), RZ(*x++=(VAV(*hv)->f2)(a,w,*hv)); ++hv); R ope(z); }
void MultilevelGraph::writeGML(std::ostream &os) { GraphAttributes GA(*m_G); exportAttributesSimple(GA); GraphIO::writeGML(GA, os); }
//TSP -> NN -> Generations( g, ForkJoin ( n, GA -> 2-OPT ) ) -> TSP' void Pipeline2(tsp_class& tsp_instance, unsigned int number_of_tasks, unsigned int number_of_generations) { #pragma region "PipelineConfiguration" auto a = Args<General_args_type>(make_General_args(number_of_generations, number_of_tasks)); auto sa = Args<SA_args_type>(); auto aco = Args<ACO_args_type>(); auto ga = Args<GA_args_type>(make_GA_args(1000, 10, 5, 50000, 10, 0.9)); const char* pipeline_description = "TSP -> NN -> Generations( g, ForkJoin ( n, GA -> 2-OPT ) ) -> TSP'"; display_args(pipeline_description, a, sa, aco, ga); auto g = a[0].number_of_iterations_or_generations; auto n = a[0].number_of_tasks_in_parallel; auto _TSP = TSP(just(tsp_instance)); auto _DisplayInput = Display("TSP INPUT", DisplayFlags::All); auto _NN = Measure(NN(), Display("NEAREST NEIGHBOUR", DisplayFlags::EmitMathematicaGraphPlot)); auto _GA_2OPT = Chain(GA(ga[0].population_size, ga[0].mutation_percentage, ga[0].group_size, ga[0].number_of_generations, ga[0].nearby_cities, ga[0].nearby_cities_percentage), _2OPT()); auto _ForkJoin = [](unsigned int n, TSP::transformer_type map_fun){ return Measure(ForkJoin(n, map_fun)); }; auto _DisplayOutput = Display("TSP OUTPUT", DisplayFlags::EmitMathematicaGraphPlot); #pragma endregion //TSP -> NN -> Generations( g, ForkJoin ( n, GA -> 2-OPT ) ) -> TSP' auto result = _TSP .map(_DisplayInput) .map(_NN) .map(Generations(g, _ForkJoin(n, _GA_2OPT))) .map(_DisplayOutput); }
static F1(jtvtokens){A t,*y,z;I n,*s;TA*x; RZ(t=tokens(vs(w))); n=AN(t); y=AAV(t); jt->tmonad=1; GA(z,BOX,WTA*(5+n),2,0); s=AS(z); *s++=5+n; *s=WTA; x=(TA*)AV(z); x->a=mark; x->t=0; ++x; DO(n, x->a=t=*y++; x->t=0; ++x; if(t==xnam||jt->dotnames&&t==xdot)jt->tmonad=0;);
int main(int argc, char **argv) { srand((unsigned int)time(NULL)); // 乱数初期化 GA(); return 0; }
A jtfxeachv(J jt,I r,A w){A*wv,x,z,*zv;I n,wd; RZ(w); n=AN(w); wv=AAV(w); wd=(I)w*ARELATIVE(w); ASSERT(r>=AR(w),EVRANK); ASSERT(n,EVLENGTH); ASSERT(BOX&AT(w),EVDOMAIN); GA(z,BOX,n,AR(w),AS(w)); zv=AAV(z); DO(n, RZ(zv[i]=x=fx(WVR(i))); ASSERT(VERB&AT(x),EVDOMAIN););
static A jtvaspc(J jt,A a,A w,C id,VF ado,I cv,I t,I zt,I af,I acr,I wf,I wcr,I f,I r){A q;I*as,*v,*ws; as=AS(a); ws=AS(w); GA(q,INT,f+r,1,0); v=AV(q); if(r>acr){ICPY(v,wf+ws,r); RZ(a=irs2(vec(INT,r-acr,acr+v),a,0L,1L,0L,jtreshape));} if(r>wcr){ICPY(v,af+as,r); RZ(w=irs2(vec(INT,r-wcr,wcr+v),w,0L,1L,0L,jtreshape));} R vasp(a,w,id,ado,cv,t,zt,af,r,wf,r,f,r); } /* prefix agreement on cells */
static A jtcants(J jt,A a,A w,A z){A a1,q,y;B*b,*c;I*u,wr,zr;P*wp,*zp; RZ(a&&w&&z); RZ(a=grade1(a)); wr=AR(w); wp=PAV(w); a1=SPA(wp,a); zr=AR(z); zp=PAV(z); ASSERT(wr==zr,EVNONCE); RZ(b=bfi(wr,a1,1)); GA(q,B01,wr,1,0); c=BAV(q); u=AV(a); DO(wr, c[i]=b[u[i]];);
F1(bool){A b,h;I j,*v; RZ(w); if(VERB&AT(w))R ADERIV(CBOOL, basis1,0L, 0L,0L,0L); RZ(w=vi(w)); v=AV(w); DO(AN(w), j=*v++; ASSERT(-16<=j&&j<16,EVINDEX)); GA(b,BOOL,64,2,0); *AS(b)=16; *(1+AS(b))=4; MC(AV(b),booltab,64L); RZ(h=cant2(apv(AR(w),0L,1L),from(w,b))); R fdef(CBOOL,VERB, bool1,bool2, w,0L,h, 0L, RMAXL,0L,0L); }
int main(){ srand(time(NULL)); PROTAIN * prot = allocProtain(); initProtain(prot); readProtain(prot); GA(prot); return 0; }
static A jtipprep(J jt,A a,A w,I zt,I*pm,I*pn,I*pp){A z=mark;I*as,ar,ar1,m,mn,n,p,*ws,wr,wr1; ar=AR(a); as=AS(a); ar1=ar?ar-1:0; RE(*pm=m=prod(ar1, as)); wr=AR(w); ws=AS(w); wr1=wr?wr-1:0; RE(*pn=n=prod(wr1,1+ws)); RE(mn=mult(m,n)); *pp=p=ar?*(as+ar1):wr?*ws:1; ASSERT(!(ar&&wr)||p==*ws,EVLENGTH); GA(z,zt,mn,ar1+wr1,0); ICPY(AS(z), as,ar1); ICPY(AS(z)+ar1,1+ws,wr1); R z; } /* argument validation & result for an inner product */
// Analysis for inner product // a,w are arguments // zt is type of result // *pm is # 1-cells of a // *pn is # atoms in an item of w // *pp is number of inner-product muladds // (in each, an atom of a multiplies an item of w) static A jtipprep(J jt,A a,A w,I zt,I*pm,I*pn,I*pp){A z=mark;I*as,ar,ar1,m,mn,n,p,*ws,wr,wr1; ar=AR(a); as=AS(a); ar1=ar?ar-1:0; RE(*pm=m=prod(ar1,as)); // m=# 1-cells of a. It could overflow, if there are no atoms wr=AR(w); ws=AS(w); wr1=wr?wr-1:0; RE(*pn=n=prod(wr1,1+ws)); RE(mn=mult(m,n)); // n=#atoms in 1-cell of w; mn = #atoms in result *pp=p=ar?*(as+ar1):wr?*ws:1; // if a is an array, the length of a 1-cell; otherwise, the number of items of w ASSERT(!(ar&&wr)||p==*ws,EVLENGTH); GA(z,zt,mn,ar1+wr1,0); // allocate result area ICPY(AS(z), as,ar1); // Set shape: 1-frame of a followed by shape of item of w ICPY(AS(z)+ar1,1+ws,wr1); R z; } /* argument validation & result for an inner product */
int main() { //srand((unsigned)time(NULL)); srand(SEED); population_init(); GA(); system("pause"); return 0; }
static SF(jtsortb){A z;B up,*u,*v;I i,s; GA(z,AT(w),AN(w),AR(w),AS(w)); v=BAV(z); up=1==jt->compgt; u=BAV(w); for(i=0;i<m;++i){ s=bsum(n,u); if(up){memset(v,C0,n-s); memset(v+n-s,C1,s );} else {memset(v,C1,s ); memset(v+s, C0,n-s);} u+=n; v+=n; } R z; } /* w grade"1 w on boolean */
static F2(jtebarmat){A ya,yw,z;B b,*zv;C*au,*av,*u,*v,*v0,*wu,*wv;I*as,c,i,k,m,n,r,s,si,sj,t,*ws; RZ(a&&w); as=AS(a); av=CAV(a); ws=AS(w); v=v0=wv=CAV(w); si=as[0]; m=1+ws[0]-si; sj=as[1]; n=1+ws[1]-sj; t=AT(w); k=bp(t); c=ws[1]; r=k*c; s=k*sj; GA(z,B01,AN(w),2,ws); zv=BAV(z); memset(zv,C0,AN(z)); if(t&B01+LIT+INT||0==jt->ct&&t&FL+CMPX) for(i=0;i<m;++i){ DO(n, u=av; b=0; DO(si, if(b=memcmp(u,v,s))break; u+=s; v+=r;); v=v0+=k; zv[i]=!b;);
static A jtmakename(J jt,C*s){A z;I m;NM*zv; m=strlen(s); GA(z,NAME,m,1,0); zv=NAV(z); memcpy(zv->s,s,m); *(m+zv->s)=0; zv->m =(UC)m; zv->sn =0; zv->e =0; zv->flag=NMDOT; zv->hash=NMHASH(m,s); ACX(z); R z; }
static B jtpdef(J jt,C id,I t,AF f1,AF f2,I m,I l,I r){A z;V*v; GA(z,t,1,0,0); ACX(z); v=VAV(z); v->f1=f1?f1:jtdomainerr1; /* monad C function */ v->f2=f2?f2:jtdomainerr2; /* dyad C function */ v->mr=m; /* monadic rank */ v->lr=l; /* left rank */ v->rr=r; /* right rank */ v->fdep=1; /* function depth */ v->id=id; /* spelling */ pst[(UC)id]=z; /* other fields are zeroed in ga() */ R 1; }
static DF1(reduce){PROLOG;DECLF;A y,z;C*u,*v;I c,k,m,old,t; RZ(w); m=IC(w); if(!m)R df1(w,iden(fs)); RZ(z=tail(w)); if(1==m)R z; t=AT(w); c=AN(z); GA(y,t,c,AR(z),AS(z)); u=CAV(y); k=SZT(t,c); v=CAV(w)+k*(m-1); old=tbase+ttop; DO(m-1, MC(u,v-=k,k); RZ(z=f2(y,z,fs)); gc(z,old)); EPILOG(z); }
B jtsymext(J jt,B b){A x,y;I j,m,n,s[2],*v,xn,yn;L*u; if(b){y=jt->symp; j=((MS*)y-1)->j; n=*AS(y); yn=AN(y);} else { j=12; n=1; yn=0; } m=msize[1+j]; /* new size in bytes */ m-=sizeof(MS)+SZI*(AH+2); /* less array overhead */ m/=symcol*SZI; /* new # rows */ s[0]=m; s[1]=symcol; xn=m*symcol; /* new pool array shape */ GA(x,INT,xn,2,s); v=AV(x); /* new pool array */ if(b)ICPY(v,AV(y),yn); /* copy old data to new array */ memset(v+yn,C0,SZI*(xn-yn)); /* 0 unused area for safety */ u=n+(L*)v; j=1+n; DO(m-n-1, u++->next=j++;); /* build free list extension */
static F1(lparen) { A z; C*v; I n; RZ(w); n=AN(w); GA(z,CHAR,2+n,1,0); v=CAV(z); *v='('; *(v+n+1)=')'; MC(1+v,AV(w),n); R z; }
static DF1(jtbasis1){DECLF;A z;D*x;I j;V*v; PREF1(jtbasis1); RZ(w=vi(w)); switch(*AV(w)){ case 0: GA(z,FL,3,1,0); x=DAV(z); v=VAV(fs); j=v->mr; x[0]=j<=-RMAX?-inf:j>=RMAX?inf:j; j=v->lr; x[1]=j<=-RMAX?-inf:j>=RMAX?inf:j; j=v->rr; x[2]=j<=-RMAX?-inf:j>=RMAX?inf:j; R pcvt(INT,z); case -1: R lrep(inv (fs)); case 1: R lrep(iden(fs)); default: ASSERT(0,EVDOMAIN); }}
I*deba(A *stack,I i){DC d; I k,*sp,*spx; GA(d,INT,2*i+DSZ,1L,0); d->t=DCPARS; d->lnk=sitop; sitop=d; /* link new debug token array to top of si stack */ d->ln=i; /* tokens in debug token array */ d->n=i-1; /* assumed error at end of token array */ sp=DSZX+(I*)d; memcpy(sp,stack,i*sizeof(A*)); spx=sp=i+sp; k=0; while(i--)*spx++=k++; R sp; }
DC jtdeba(J jt,C t,A x,A y,A fs){A q;DC d; GA(q,LIT,sizeof(DST),1,0); d=(DC)AV(q); memset(d,C0,sizeof(DST)); d->dctype=t; d->dclnk=jt->sitop; jt->sitop=d; switch(t){ case DCPARSE: d->dcy=y; break; case DCSCRIPT: d->dcy=y; d->dcm=(I)fs; break; case DCCALL: d->dcx=x; d->dcy=y; d->dcf=fs; d->dca=jt->curname; d->dcm=NAV(jt->curname)->m; d->dcn=(I)jt->cursymb; d->dcstop=-2; if(jt->dbss==SSSTEPINTO){d->dcss=SSSTEPINTO; jt->dbssd=d; jt->dbss=0;} } R d; } /* create new top of si stack */
static A jtmerge1(J jt,A w,A ind){A z;B*b;C*wc,*zc;D*wd,*zd;I c,it,j,k,m,r,*s,t,*u,*wi,*zi; RZ(w&&ind); r=MAX(0,AR(w)-1); s=1+AS(w); t=AT(w); k=bp(t); m=IC(w); c=aii(w); ASSERT(!(t&SPARSE),EVNONCE); ASSERT(r==AR(ind),EVRANK); ASSERT(!ICMP(s,AS(ind),r),EVLENGTH); GA(z,t,c,r,s); if(!(AT(ind)&B01+INT))RZ(ind=cvt(INT,ind)); it=AT(ind); u=AV(ind); b=(B*)u; ASSERT(!c||1<m||!(it&B01),EVINDEX); ASSERT(!c||1!=m||!memchr(b,C1,c),EVINDEX); zi=AV(z); zc=(C*)zi; zd=(D*)zc; wi=AV(w); wc=(C*)wi; wd=(D*)wc; switch(MCASE(it,k)){ case MCASE(B01,sizeof(C)): DO(c, *zc++=wc[*b++?i+c:i];); break; case MCASE(B01,sizeof(I)): DO(c, *zi++=wi[*b++?i+c:i];); break;
A jtrd(J jt,F f,I j,I n){A z;C*x;I p=0;size_t q=1; RZ(f); if(0>n){if(j<0) n=-j; else n=fsize(f)-j;} #if !SY_WINCE {INT64 v; v= j+((0>j)?fsize(f):0); fsetpos(f,(fpos_t*)&v);} #else fseek(f,(long)(0>j?1+j:j),0>j?SEEK_END:SEEK_SET); #endif clearerr(f); GA(z,LIT,n,1,0); x=CAV(z); while(q&&n>p){ p+=q=fread(p+x,sizeof(C),(size_t)(n-p),f); if(ferror(f))R jerrno(); } R z; } /* read file f for n bytes at j */
A jtstcreate(J jt,C k,I p,I n,C*u){A g,*pv,x,y;C s[20];I m,*nv;L*v; GA(g,SYMB,ptab[p],1,0); RZ(v=symnew(AV(g))); v->flag|=LINFO; v->sn=jt->symindex++; switch(k){ case 0: /* named locale */ RZ(x=nfs(n,u)); LOCNAME(g)=x; LOCPATH(g)=ra(1==n&&'z'==*u?vec(BOX,0L,0L):zpath); symbis(x,g,jt->stloc); break; case 1: /* numbered locale */ ASSERT(0<=jt->stmax,EVLOCALE); sprintf(s,FMTI,n); RZ(x=nfs(strlen(s),s)); LOCNAME(g)=x; LOCPATH(g)=ra(zpath); ++jt->stused; m=AN(jt->stnum); if(m<jt->stused){ x=ext(1,jt->stnum); y=ext(1,jt->stptr); RZ(x&&y); jt->stnum=x; jt->stptr=y; nv=m+AV(jt->stnum); pv=m+AAV(jt->stptr); DO(AN(x)-m, *nv++=-1; *pv++=0;); }
// Derived verb for f//. y static DF1(jtobqfslash){A y,z;B b=0,p;C er,id,*wv;I c,d,k,m,m1,mn,n,n1,r,*s,wt; RZ(w); r=AR(w); s=AS(w); wt=AT(w); wv=CAV(w); if(!(AN(w)&&1<r&&DENSE&wt))R oblique(w,self); // revert to default if rank<2, empty, or sparse y=VAV(self)->f; y=VAV(y)->f; id=vaid(y); m=s[0]; m1=m-1; n=s[1]; n1=n-1; mn=m*n; d=m+n-1; PROD(c,r-2,2+s); if(1==m||1==n){GA(z,wt,AN(w),r-1,1+s); *AS(z)=d; MC(AV(z),wv,AN(w)*bp(wt)); R z;} if(wt&FL+CMPX)NAN0; if(1==c)switch(OBQCASE(CTTZ(wt),id)){ case OBQCASE(B01X, CNE ): OBQLOOP(B,B,wt,x=*u, x^=*u ); break; case OBQCASE(B01X, CEQ ): OBQLOOP(B,B,wt,x=*u, x=x==*u ); break; case OBQCASE(B01X, CMAX ): case OBQCASE(B01X, CPLUSDOT): OBQLOOP(B,B,wt,x=*u, x|=*u ); break; case OBQCASE(B01X, CMIN ): case OBQCASE(B01X, CSTAR ): case OBQCASE(B01X, CSTARDOT): OBQLOOP(B,B,wt,x=*u, x&=*u ); break; case OBQCASE(B01X, CLT ): OBQLOOP(B,B,wt,x=*u, x=*u< x ); break; case OBQCASE(B01X, CLE ): OBQLOOP(B,B,wt,x=*u, x=*u<=x ); break; case OBQCASE(B01X, CGT ): OBQLOOP(B,B,wt,x=*u, x=*u> x ); break; case OBQCASE(B01X, CGE ): OBQLOOP(B,B,wt,x=*u, x=*u>=x ); break; case OBQCASE(B01X, CPLUS ): OBQLOOP(B,I,INT,x=*u, x+=*u ); break; case OBQCASE(SBTX, CMAX ): OBQLOOP(SB,SB,wt,x=*u, x=SBGT(x,*u)?x:*u ); break; case OBQCASE(SBTX, CMIN ): OBQLOOP(SB,SB,wt,x=*u, x=SBLT(x,*u)?x:*u ); break; case OBQCASE(FLX, CMAX ): OBQLOOP(D,D,wt,x=*u, x=MAX(x,*u) ); break; case OBQCASE(FLX, CMIN ): OBQLOOP(D,D,wt,x=*u, x=MIN(x,*u) ); break; case OBQCASE(FLX, CPLUS ): OBQLOOP(D,D,wt,x=*u, x+=*u ); break; case OBQCASE(CMPXX,CPLUS ): OBQLOOP(Z,Z,wt,x=*u, x=zplus(x,*u)); break; case OBQCASE(XNUMX,CMAX ): OBQLOOP(X,X,wt,x=*u, x=1==xcompare(x,*u)? x:*u); break; case OBQCASE(XNUMX,CMIN ): OBQLOOP(X,X,wt,x=*u, x=1==xcompare(x,*u)?*u: x); break; case OBQCASE(XNUMX,CPLUS ): OBQLOOP(X,X,wt,x=*u, x=xplus(x,*u)); break; case OBQCASE(RATX, CMAX ): OBQLOOP(Q,Q,wt,x=*u, x=1==QCOMP(x,*u)? x:*u); break; case OBQCASE(RATX, CMIN ): OBQLOOP(Q,Q,wt,x=*u, x=1==QCOMP(x,*u)?*u: x); break; case OBQCASE(RATX, CPLUS ): OBQLOOP(Q,Q,wt,x=*u, x=qplus(x,*u)); break; case OBQCASE(INTX, CBW0001 ): OBQLOOP(I,I,wt,x=*u, x&=*u ); break; case OBQCASE(INTX, CBW0110 ): OBQLOOP(I,I,wt,x=*u, x^=*u ); break; case OBQCASE(INTX, CBW0111 ): OBQLOOP(I,I,wt,x=*u, x|=*u ); break; case OBQCASE(INTX, CMAX ): OBQLOOP(I,I,wt,x=*u, x=MAX(x,*u) ); break; case OBQCASE(INTX, CMIN ): OBQLOOP(I,I,wt,x=*u, x=MIN(x,*u) ); break; case OBQCASE(INTX, CPLUS ): er=0; OBQLOOP(I,I,wt,x=*u, {p=0>x; x+=*u; BOV(p==0>*u&&p!=0>x);}); if(er>=EWOV)OBQLOOP(I,D,FL,x=(D)*u, x+=*u); }
static DF2(xd){PROLOG;DECLFG;A f,*line,loc=local,name,seq,z=0;B b;DC dv; I i=0,n,old; b=a&&w; f=*(b+AAV(sv->h)); line=AAV(f); n=nline=AN(f); ASSERT(n,EVDOMAIN); GA(local,SYMB,twprimes[0],1,0); symbis(scnm(CALPHA),a,local); symbis(scnm(COMEGA),w,local); RZ(dv=debadd(DCDEFN)); dv->p=sv->s; drun=0; old=tbase+ttop; ra(self); for(;0<=i&&i<n;i++){ tpop(old); dv->ln=i; dv->n=1+(0!=a); z=parse(ca(line[i])); if(!debugb&&!z) break; } if(debugb&&!z){z=tpush(qpopres); qpopres=0;} z=car(z); ++AC(local); fa(local); local=loc; asgn=0; fa(self); debz(); if(!z) jsignal(EVRESULT); EPILOG(z); }
static SF(jtsortb2){A z;B up;I i,ii,j,p,yv[4];US*v,*wv,x,zz[4]; GA(z,AT(w),AN(w),AR(w),AS(w)); v=(US*)AV(z); wv=(US*)AV(w); p=4; up=1==jt->compgt; DO(p, yv[i]=0;);