static int equal_repres(Term f1, Term f2) { if(CompoundName(f1)==CompoundName(f2) && (EqualTerms(CompoundArg1(f1),CompoundArg1(f2)) || EqualTerms(CompoundArg1(f1),CompoundArg2(f2))) ) return 1; return 0; }
int main() { static int table[MAXTERMS][MAXTERMS];//the table int covered[MAX_VARS+1][MAXTERMS];//checks to see if it was covered in the next level int m; int j,k,p; TERM tempTerm; int found; /*Initialize number of terms at each level m*/ for(m=0;m<MAX_VARS+1;m++) numTerms[m]=0; /*read input minterms from user*/ int in; printf("Enter the number of minterms to be entered ::"); scanf("%d",&in); printf("Enter the normal terms::\n"); int ind=0,trm; numTerms[0]=in; for(ind=0;ind<in;ind++) { scanf("%d",&trm); init(&terms[0][ind],trm); covered[0][ind]=FALSE; dont_care[0][ind]=FALSE; } printf("Enter the number of dont care terms::"); int dc; scanf("%d",&dc); numTerms[0]+=dc; int counter; for(counter=0;counter<dc;counter++) { scanf("%d",&trm); init(&terms[0][ind+counter],trm); covered[0][ind+counter]=FALSE; dont_care[0][ind+counter]=TRUE; } for(m=0;m<MAX_VARS;m++) for(j=0;j<numTerms[m];j++) for(k=j+1;k<numTerms[m];k++) { int both_dontcare=FALSE; if(combinable(terms[m][j],terms[m][k])) { covered[m][j]=TRUE; covered[m][k]=TRUE; if((dont_care[m][j]==TRUE)&&(dont_care[m][k]==TRUE)) both_dontcare=TRUE; combine(terms[m][j],terms[m][k],&tempTerm); found=FALSE; for(p=0;p<numTerms[m+1];p++) if (EqualTerms(terms[m+1][p],tempTerm)) found=TRUE; if(!found) { numTerms[m+1]=numTerms[m+1]+1; terms[m+1][numTerms[m+1]-1]=tempTerm; covered[m+1][numTerms[m+1]-1]=FALSE; if(both_dontcare) dont_care[m+1][numTerms[m+1]-1]=TRUE; else dont_care[m+1][numTerms[m+1]-1]=FALSE; } } } //creating the minterm table /*******************get the number of nonrepeating final prime implicants*****************/ int final_index=0,ii=0; for(m=0;m<MAX_VARS;m++) for(j=0;j<numTerms[m];j++) { if((!covered[m][j])&&(!dont_care[m][j])) { int flag=1; for(ii=0;ii<final_index;ii++) { if(EqualTerms(terms[m][j],prime_implicants[ii])) flag=0; } if(flag) { prime_implicants[final_index]=terms[m][j]; final_index++; } } } //and finally the table /*setting the table values according to the prime implicants*/ for(ii=0;ii<final_index;ii++) { for(j=0;j<prime_implicants[ii].nn;j++) table[ii][prime_implicants[ii].compterms[j]]=1; } for(ii=0;ii<MAXTERMS;ii++) { if(inDontCares(ii)) for(j=0;j<final_index;j++) table[j][ii]=0; } TERM essential[MAXTERMS]; int ess=0; //get the essential prime implicants while(TRUE) { int flag=1; for(ii=0;ii<MAXTERMS;ii++) { int count=0; int pos=0; for(j=0;j<final_index;j++) if(table[j][ii]==1) {count++;pos=j;} if(count==1) { //this is an essential prime implicant essential[ess]=prime_implicants[pos]; ess++; table[j][ii]=0; int c;//remove all other ones for(c=0;c<MAXTERMS;c++) if(table[pos][c]==1) makezeros(c,final_index,table);//set the col to zero flag=0; } } if(flag)//no more count=1 i.e no more ess prime im break; } print_solutions(0,table,final_index,essential,ess); }
void alg1_opt_let(Term a1) { List l,l1,l2,lm=ConsumeCompoundArg(a1,1); List nl=0; Label mylbl=NewLabel(); /*printf("%d -> ",ListLength(lm));*/ for(l=lm;l;l=ListTail(l)) { Term m1=ListFirst(l); int n,d,g; List ln=ConsumeCompoundArg(m1,3); List ld=ConsumeCompoundArg(m1,4); Term f1=0, f2=0, w=0; n=IntegerValue(CompoundArg1(m1)); d=IntegerValue(CompoundArg2(m1)); rpt1: for(l1=ld;l1;l1=ListTail(l1)) { int n=ListMember(ln,ListFirst(l1)); if(n) { ld=CutFromList(ld,l1); l1=ListNthList(ln,n); ln=CutFromList(ln,l1); goto rpt1; } if(CompoundArg2(ListFirst(l1))==A_SQRT2) { Term t=ListFirst(l1); ChangeList(l1,0); ld=CutFromList(ld,l1); ln=AppendFirst(ln,t); d*=2; goto rpt1; } } rpt2: for(l1=ln;l1;l1=ListTail(l1)) if(CompoundArg2(ListFirst(l1))==A_SQRT2) { List l2; for(l2=ListTail(l1);l2;l2=ListTail(l2)) if(CompoundArg2(ListFirst(l2))==A_SQRT2) { ln=CutFromList(ln,l1); ln=CutFromList(ln,l2); n*=2; goto rpt2; } } g=gcf(n,d); n/=g; d/=g; for(l1=ln;l1;l1=l1?ListTail(l1):0) { Term sp=ListFirst(l1); if(CompoundName(sp)==OPR_PARAMETER) continue; if(CompoundName(sp)==OPR_FIELD) { if(f1==0) f1=l1; else if(f2==0) f2=l1; else { ErrorInfo(1280); puts("bad let-sub: >2 prtc"); return; } continue; } if(CompoundName(sp)==OPR_WILD) { if(w) { ErrorInfo(1281); puts("bad let-sub: unk spec"); return; } w=l1; continue; } ErrorInfo(110); WriteTerm(sp);puts(" : bad let: unk stuff"); return; } if(f2==0) { ErrorInfo(112); puts("bad let-subs: not 2 prtc"); return; } l1=f1;f1=ListFirst(f1);ChangeList(l1,0);ln=CutFromList(ln,l1); l1=f2;f2=ListFirst(f2);ChangeList(l1,0);ln=CutFromList(ln,l1); if(w) {l1=w;w=ListFirst(w);ChangeList(l1,0);ln=CutFromList(ln,l1);} ln=SortedList(ln,prmcmp); ld=SortedList(ld,prmcmp); if((GetAtomProperty(CompoundArg2(f1),A_ANTI)==CompoundArg2(f1) || GetAtomProperty(CompoundArg2(f2),A_ANTI)==CompoundArg2(f2)) && strcmp(AtomValue(CompoundArg2(f1)),AtomValue(CompoundArg2(f2)))>0) { Term tmp=f1; f1=f2; f2=tmp; } if(ListLength(CompoundArg1(f1))==1 && ListLength(CompoundArg1(f1))==1) { Term i1=ListFirst(CompoundArg1(f1)); Term i2=ListFirst(CompoundArg1(f2)); if(CompoundName(CompoundArg1(i1))!=A_COLOR || CompoundName(CompoundArg1(i2))!=A_COLOR || CompoundArg2(i1)!=CompoundArg2(i2)) { ErrorInfo(233); puts("bad color stru in let-sub."); return; } SetCompoundArg(i1,2,mylbl); SetCompoundArg(i2,2,mylbl); } ln=AppendLast(ln,f1); ln=AppendLast(ln,f2); if(w) ln=AppendLast(ln,w); SetCompoundArg(m1,1,NewInteger(n)); SetCompoundArg(m1,2,NewInteger(d)); SetCompoundArg(m1,3,ln); SetCompoundArg(m1,4,ld); for(l1=nl;l1;l1=ListTail(l1)) { if(EqualTerms(CompoundArgN(ListFirst(l1),3),ln) && EqualTerms(CompoundArgN(ListFirst(l1),4),ld) && IntegerValue(CompoundArg2(ListFirst(l1)))*d>0) { Term om1=ListFirst(l1); int n1=IntegerValue(CompoundArg1(om1)); int d1=IntegerValue(CompoundArg2(om1)); int rn,rd,cc=0; if(d1<0) { d=-d;d1=-d1;cc=1; } rn=n*d1+n1*d; rd=d*d1; if(rn==0) { nl=CutFromList(nl,l1); break; } g=gcf(rn,rd); rn/=g; rd/=g; if(cc) rd=-rd; SetCompoundArg(om1,1,NewInteger(rn)); SetCompoundArg(om1,2,NewInteger(rd)); FreeAtomic(m1); break; } } if(l1==0) nl=AppendLast(nl,m1); } RemoveList(lm); /*DumpList(nl);*/ /*printf("%d\n",ListLength(nl));*/ SetCompoundArg(a1,1,nl); }
Term ProcLet(Term t, Term ind) { Term t1,nm,sub,kl=0; List il,ol,l1; int transf_fl=0; Atom anti1=0, anti2=0; ol=il=NewList(); t1=ConsumeCompoundArg(t,1); FreeAtomic(t); t1=ProcessAlias(t1); if(is_compound(t1) && CompoundArity(t1)==2 && CompoundName(t1)==OPR_COMMA) { Term a1,a2; a1=ConsumeCompoundArg(t1,1); a2=ConsumeCompoundArg(t1,2); FreeAtomic(t1); ProcLet(MakeCompound1(OPR_LET,a1),0); ProcLet(MakeCompound1(OPR_LET,a2),0); return 0; } if(!is_compound(t1) || CompoundArity(t1)!=2 || (CompoundName(t1)!=OPR_EQSIGN && CompoundName(t1)!=OPR_RARROW)) { ErrorInfo(325); printf("bad argument in let statement\n"); return 0; } if(CompoundName(t1)==OPR_EQSIGN && is_atom(CompoundArg1(t1)) && is_atom(CompoundArg2(t1)) && (CompoundArg2(t1)==A_GAMMA5 || GetAtomProperty(CompoundArg2(t1), A_GAMMA5))) { SetAtomProperty(CompoundArg1(t1),A_GAMMA5,NewInteger(1)); } if(CompoundName(t1)==OPR_RARROW) transf_fl=1; if(CompoundName(t1)==OPR_EQSIGN && is_atom(CompoundArg1(t1)) && is_compound(CompoundArg2(t1)) && CompoundName(CompoundArg2(t1))==A_ANTI && is_atom(CompoundArg1(CompoundArg2(t1)))) { anti1=CompoundArg1(t1); anti2=CompoundArg1(CompoundArg2(t1)); } nm=ConsumeCompoundArg(t1,1); sub=ConsumeCompoundArg(t1,2); FreeAtomic(t1); if(transf_fl) allow_transf_lets=0; if(is_compound(nm) && (CompoundName(nm)==OPR_USCORE || CompoundName(nm)==OPR_CARET)) nm=SplitIndices(nm,&il); if(!is_atom(nm)) { ErrorInfo(325); printf("bad left argument in let call\n"); FreeAtomic(sub); FreeAtomic(t1); return 0; } if(transf_fl) { if(!is_parameter(nm) && !is_particle(nm,NULL)) { ErrorInfo(728); printf("Unknown object '%s'.\n",AtomValue(nm)); return 0; } } if(GetAtomProperty(nm,A_KEEP_LETS)) { Term prp; kl=ExprTo1kl(CopyTerm(sub)); if(kl==0) return 0; prp=GetAtomProperty(nm,A_KEEP_LETS); SetCompoundArg(prp,1,kl); } sub=ExprTo1(sub); alg1_set_cos0(sub); if(sub==0) return 0; if(transf_fl) allow_transf_lets=1; t1=CopyTerm(CompoundArg2(sub)); if(is_empty_list(il)) { l1=t1; while(!is_empty_list(l1)) { Term t; t=CompoundArg2(ListFirst(l1)); if(!is_label(t)) { ErrorInfo(326); printf("unbalanced index '"); WriteTerm(t); printf("' in let statement\n"); FreeAtomic(sub); FreeAtomic(t1); FreeAtomic(ol); return 0; } ol=AppendLast(ol,t); l1=ListTail(l1); } /* mk_let(nm,sub,ol,t1); */ } else { List t2; t2=0; if(ListLength(il)!=ListLength(t1)) { ErrorInfo(327); printf("distinct indices number "); printf("in let statement.\n"); FreeAtomic(sub); FreeAtomic(t1); return 0; } l1=t1; while(!is_empty_list(l1)) { Term t; t=CompoundArg2(ListFirst(l1)); if(!is_atom(t) || !ListMember(il,t)) { ErrorInfo(326); printf("unbalanced index '"); WriteTerm(t); printf("' in let statement\n"); FreeAtomic(sub); FreeAtomic(t1); FreeAtomic(ol); return 0; } ol=AppendLast(ol,t); l1=ListTail(l1); } l1=il; while(!is_empty_list(l1)) { List l2; if(!ListMember(ol,ListFirst(l1))) { ErrorInfo(326); printf("unbalanced index '"); WriteTerm(t); printf("' in let statement\n"); FreeAtomic(sub); FreeAtomic(t1); FreeAtomic(ol); return 0; } for(l2=t1;l2;l2=ListTail(l2)) if(ListFirst(l1)==CompoundArg2(ListFirst(l2))) { t2=AppendLast(t2,ListFirst(l2)); break; } l1=ListTail(l1); } RemoveList(t1); t1=t2; FreeAtomic(ol); ol=il; } /* mk_let(nm,sub,ol,t1); */ l1=t1; while(!is_empty_list(l1)) { SetCompoundArg(ListFirst(l1),2,0); l1=ListTail(l1); } /*WriteTerm(sub); puts(""); getchar();*/ t=MakeCompound(OPR_LET,5); SetCompoundArg(t,1,sub); SetCompoundArg(t,2,ol); SetCompoundArg(t,3,alg1_inv_alg(sub)); if(transf_fl==0) { Term tt; tt=alg1_guess_mpl(sub); if(tt) { SetCompoundArg(t,4,NewInteger(1)); SetCompoundArg(t,5,tt); } else { int tp; tt=alg1_guess_mtr(sub, &tp); if(tt) { SetCompoundArg(t,4,NewInteger(tp)); SetCompoundArg(t,5,tt); } } ReportRedefined(nm,"let-substitution"); SetAtomProperty(nm,PROP_INDEX,t1); SetAtomProperty(nm,PROP_TYPE,t); alg1_let_cw(nm); if(anti1 && anti2) { SetAtomProperty(anti1,A_ANTI,anti2); SetAtomProperty(anti2,A_ANTI,anti1); } else if(only_prm(CompoundArg1(t))) SetAtomProperty(nm,A_ANTI,nm); return 0; } l1=GetAtomProperty(nm,PROP_INDEX); if(!EqualTerms(l1,t1)) { ErrorInfo(729); puts("transformed object has other indices types"); return 0; } if(GetAtomProperty(nm,OPR_LET)) { WarningInfo(0); printf("Warning: transformation rule for '%s' is redefined.\n", AtomValue(nm)); } SetAtomProperty(nm,OPR_LET,t); return 0; }
void alg2_common_t(Term a2) { List ct, l1,l2,l3,l4, ml, gct, vil=0; ct=NewList(); ml=CompoundArgN(a2,5); if(is_empty_list(ml) || (ListLength(ml)==1 && FAOutput==0) ) return; gct=CompoundArgN(ListFirst(ml),3); if(is_empty_list(gct)) return; if(FAOutput) { for(l1=CompoundArg1(a2);l1;l1=ListTail(l1)) if(CompoundName(CompoundArg2(ListFirst(l1)))==OPR_VECTOR || CompoundName(CompoundArg2(ListFirst(l1)))==OPR_SPINOR) vil=AppendLast(vil,ListFirst(CompoundArg1( CompoundArg2(ListFirst(l1))))); } l1=gct; while(!is_empty_list(l1)) { Term spt; spt=ListFirst(l1); if(FAOutput) { Term pr; if(CompoundName(spt)==A_MOMENT) goto cnt1; if((is_atom(CompoundArg1(spt))&& (pr=GetAtomProperty(CompoundArg1(spt),A_INFINITESIMAL)))) goto cnt1; if(CompoundName(spt)==OPR_SPECIAL) { l2=CompoundArg1(spt); if(l2==A_GAMMA||l2==A_GAMMA5||l2==A_GAMMAP||l2==A_GAMMAM) goto cnt1; } if(vil && CompoundName(spt)==OPR_SPECIAL && CompoundArg1(spt)==A_DELTA) { l2=CompoundArg2(spt); if(ListMember(vil,ListFirst(l2))) goto cnt1; if(ListMember(vil,ListFirst(ListTail(l2)))) goto cnt1; } } l2=ListTail(ml); while(!is_empty_list(l2)) { l3=CompoundArgN(ListFirst(l2),3); while(!is_empty_list(l3)) { if(EqualTerms(spt,ListFirst(l3))) break; l3=ListTail(l3); } if(is_empty_list(l3)) goto cnt1; l2=ListTail(l2); } ct=AppendLast(ct,CopyTerm(spt)); cnt1: l1=ListTail(l1); } if(is_empty_list(ct)) return; l1=ml; while(!is_empty_list(l1)) { Term m2; m2=ListFirst(l1); l2=ConsumeCompoundArg(m2,3); l3=ct; while(!is_empty_list(l3)) { Term rt; rt=ListFirst(l3); l4=l2; while(!is_empty_list(l4)) { if(EqualTerms(ListFirst(l4),rt)) { l2=CutFromList(l2,l4); goto cnt2; } l4=ListTail(l4); } puts("Internal error (a2ctf)."); cnt2: l3=ListTail(l3); } SetCompoundArg(m2,3,l2); l1=ListTail(l1); } SetCompoundArg(a2,4,ct); }