char *dd_MLGetStrForNumber(mytype x) { /* This is to make a string of rational expression for GMP rational number x. It does nothing (return NULL) if x is double. */ char *sd=NULL,*sn=NULL,*st=NULL; #if defined GMPRATIONAL mpz_t zn,zd; int len; mpz_init(zn); mpz_init(zd); mpq_canonicalize(x); mpq_get_num(zn,x); mpq_get_den(zd,x); if (mpz_sgn(zn)==0){ st=(char *)malloc(2); strcpy(st," 0"); } else if (mpz_cmp_ui(zd,1U)==0){ st=mpz_get_str(st,10,zn); } else { sn=mpz_get_str(sn,10,zn);sd=mpz_get_str(sd,10,zd); len=strlen(sn)+strlen(sd)+2; st=(char *)malloc(len); strcpy(st,sn); strcat(st,"/");strcat(st,sd); } mpz_clear(zn); mpz_clear(zd); if (sd!=NULL) free(sd); if (sn!=NULL) free(sn); #else /* do nothing */ #endif /* printf("String for Number =%s\n",st); */ return st; }
/** * Output the first n best rational approximations of rational q */ void cpt_rat_approx(mpq_t *res, mpq_srcptr q, unsigned int *n) { mpz_t pnm1,pnm2,qnm1,qnm2,pn,qn,a,num,den; mpz_init_set_si(pnm1,1); mpz_init_set_si(pnm2,0); mpz_init_set_si(qnm1,0); mpz_init_set_si(qnm2,1); mpz_init(pn); mpz_init(qn); mpz_init(a); mpz_init(num); mpz_init(den); mpq_get_num(num,q); mpq_get_den(den,q); for(unsigned int i = 0; i < *n; i++) { if(mpz_cmp_si(den,0) == 0) { *n = i; break; } __rat_approx_step(res[i], num, den, a, pnm2, pnm1, qnm2, qnm1, pn, qn); } mpz_clear(pnm1); mpz_clear(pnm2); mpz_clear(qnm1); mpz_clear(qnm2); mpz_clear(pn); mpz_clear(qn); mpz_clear(a); mpz_clear(num); mpz_clear(den); }
static void dumpSkip(mpq_t *t, const symbol_t *scan) { static int initialized = 0; static mpq_t dt; static int de; static int nu; static mpz_t zde; static mpz_t znu; int rnd; if (! initialized) { mpq_init(dt); mpz_init(zde); mpz_init(znu); initialized = 1; } if (mpq_equal(*t, scan->start)) { return; } while (dump_tuplet_current != NO_ID) { /* stop */ fprintf(lily_out, " }"); tuplet_pop(&dump_tuplet_current); } if (mpq_cmp(*t, scan->start) > 0) { fprintf(stderr, "Uh oh -- start time "); mpq_out_str(stderr, 10, scan->start); fprintf(stderr, " is too low, should be "); mpq_out_str(stderr, 10, *t); fprintf(stderr, " -- is my voice analysis correct?\n"); return; } mpq_sub(dt, scan->start, *t); mpq_get_num(znu, dt); mpq_get_den(zde, dt); nu = mpz_get_ui(znu); de = mpz_get_ui(zde); rnd = nu / de; if (rnd != 0) { fprintf(lily_out, " s1*%d", rnd); nu -= rnd * de; } if (! is_two_pow(de)) { fprintf(stderr, "Uh oh -- skip now already a tuplet %d/%d??\n", nu, de); fprintf(lily_out, " s1*%d/%d", nu, de); } else { if (nu != 0) { fprintf(lily_out, " s%d*%d", de, nu); } } last_dumped_symbol = &sym_any_skip; mpq_set(*t, scan->start); VPRINTF(" skip to t = "); VPRINT_MPQ(*t); }
Integer Rational::GetNumerator() { Integer numerator; mpz_t numer; mpz_init(numer); mpq_get_num(numer, number); numerator = numer; return numerator; }
var Pretty(Kernel& k, const Rational& x) { mpq_canonicalize(const_cast<mpq_ptr>(x.mpq)); Integer *a = new Integer, *b = new Integer; mpq_get_num(a->mpz, x.mpq); mpq_get_den(b->mpz, x.mpq); if(mpz_cmp_ui(b->mpz, 1) == 0) return Pretty(k, Z(a)); if(mpz_sgn(a->mpz) < 0) { mpz_neg(a->mpz, a->mpz); return nV::tuple($.Minus, nV::tuple(SYS(Divide), a, b)); }
void sccwriter::write_code( Expr* code, std::ostream& os, int ind, const char* retModStr, int opts ) { std::string retModString; std::string incString; if ( retModStr ) { retModString = std::string( retModStr ); retModString.append( " = " ); incString = std::string( retModStr ); incString.append( "->inc();" ); } switch( code->getclass() ) { case INT_EXPR: { indent( os, ind ); os << retModString.c_str(); os << "new IntExpr( " << mpz_get_si( ((IntExpr*)code)->n ) << " );" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } break; case RAT_EXPR: { mpz_t num, den; mpz_init(num); mpz_init(den); mpq_get_num( num, ((RatExpr*)code)->n ); mpq_get_den( den, ((RatExpr*)code)->n ); indent( os, ind ); os << retModString.c_str(); os << "new RatExpr( " << mpz_get_si( num ) << ", " << mpz_get_si( den ) << " );" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } break; case SYMS_EXPR: { //if it is a variable, simply write it to buffer if( is_var( ((SymSExpr*)code)->s ) ) { indent( os, ind ); os << retModString.c_str(); write_variable( ((SymSExpr*)code)->s.c_str(), os ); os << ";" << std::endl; } else //else must look at symbol lookup table { std::string var; get_var_name( ((SymSExpr*)code)->s, var ); indent( os, ind ); os << retModString.c_str() << "e_" << var.c_str() << ";" << std::endl; add_global_sym( var ); } indent( os, ind ); os << incString.c_str() << std::endl; } break; default: switch( code->getop() ) { case APP: { //collect the arguments std::vector< Expr* > argVector; code->collect_args( argVector ); //write the arguments std::vector< std::string > args; for( int a=0; a<(int)argVector.size(); a++ ) { std::string expr; write_expr( argVector[a], os, ind, expr ); args.push_back( expr ); } //write_args( (CExpr*)code, os, ind, 1, args ); Expr* hd = code->get_head(); //map to a program in the case that it is a program if( hd->getop()==PROG && get_prog_index_by_expr( hd )!=-1 ) { indent( os, ind ); os << retModString << "f_" << progNames[ get_prog_index_by_expr( hd ) ].c_str() << "( "; for( int a=0; a<(int)args.size(); a++ ) { os << args[a].c_str(); if( a!=(int)( args.size()-1 ) ){ os << ", "; } } os << " );" << std::endl; for( int a=0; a<(int)args.size(); a++ ) { write_dec( args[a], os, ind ); } } else { #ifdef USE_FLAT_APP std::string expr; write_expr( hd, os, ind, expr ); indent( os, ind ); os << retModString << "new CExpr( APP, "; os << expr.c_str() << ", "; for( int a=0; a<(int)args.size(); a++ ) { os << args[a].c_str(); if( a!=(int)( args.size()-1 ) ){ os << ", "; } } os << " );" << std::endl; #else std::string expr; write_expr( hd, os, ind, expr ); indent( os, ind ); os << retModString; for( int a=0; a<(int)args.size(); a++ ) { os << "new CExpr( APP, "; } os << expr.c_str() << ", "; for( int a=0; a<(int)args.size(); a++ ) { os << args[a].c_str(); os << " )"; if( a!=(int)( args.size()-1 ) ){ os << ", "; } } os << ";" << std::endl; #endif //indent( os, ind ); //os << expr.c_str() << "->dec();" << std::endl; } } break; case MATCH: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[0], os, ind, expr ); //get the head std::ostringstream sshd; sshd << "e" << exprCount; exprCount++; indent( os, ind ); os << "Expr* " << sshd.str().c_str() << " = " << expr.c_str() << "->followDefs()->get_head();" << std::endl; //write the arguments std::vector< std::string > args; write_args( (CExpr*)code, os, ind, 1, args ); bool encounterDefault = false; //now make an if statement corresponding to the match int a = 0; while( ((CExpr*)code)->kids[a+1] ) { indent( os, ind ); if( a!=0 ){ os << "}else"; } if( ((CExpr*)code)->kids[a+1]->getop()!=CASE ){ encounterDefault = true; os << "{" << std::endl; //write the body of the case write_code( ((CExpr*)code)->kids[a+1], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; }else{ if( a!=0 ) os << " "; os << "if( " << sshd.str().c_str() << "==" << args[a].c_str() << " ){" << std::endl; //collect args from the variable in the code std::ostringstream ssargs; ssargs << "args" << argsCount; argsCount++; #ifndef USE_FLAT_APP indent( os, ind+1 ); os << "std::vector< Expr* > " << ssargs.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << expr.c_str() << "->followDefs()->collect_args( " << ssargs.str().c_str() << " );" << std::endl; #endif //set the variables defined in the pattern equal to the arguments std::vector< Expr* > caseArgs; ((CExpr*)((CExpr*)code)->kids[a+1])->kids[0]->collect_args( caseArgs ); for( int b=0; b<(int)caseArgs.size(); b++ ) { indent( os, ind+1 ); os << "Expr* "; write_variable( ((SymSExpr*)caseArgs[b])->s.c_str(), os ); #ifdef USE_FLAT_APP os << " = ((CExpr*)" << expr.c_str() << "->followDefs())->kids[" << b+1 << "];" << std::endl; #else os << " = " << ssargs.str().c_str() << "[" << b << "];" << std::endl; #endif vars.push_back( ((SymSExpr*)caseArgs[b])->s ); } //write the body of the case write_code( ((CExpr*)code)->kids[a+1], os, ind+1, retModStr, opt_write_case_body ); } a++; } if( !encounterDefault ) { indent( os, ind ); os << "}else{" << std::endl; indent( os, ind + 1 ); os << "std::cout << \"Could not find match for expression in function f_"; os << progNames[currProgram].c_str() << " \";" << std::endl; indent( os, ind + 1 ); os << sshd.str().c_str() << "->print( std::cout );" << std::endl; indent( os, ind + 1 ); os << "std::cout << std::endl;" << std::endl; indent( os, ind + 1 ); os << "exit( 1 );" << std::endl; indent( os, ind ); os << "}" << std::endl; } write_dec( expr, os, ind ); for( int a=0; a<(int)args.size(); a++ ) { write_dec( args[a], os, ind ); } } break; case CASE: if( opts&opt_write_case_body ) { write_code( ((CExpr*)code)->kids[1], os, ind, retModStr ); } else { write_code( ((CExpr*)code)->kids[0]->get_head(), os, ind, retModStr ); } break; case DO: { //write each of the children in sequence int counter = 0; while( ((CExpr*)code)->kids[counter] ) { if( ((CExpr*)code)->kids[counter+1]==NULL ) { write_code( ((CExpr*)code)->kids[counter], os, ind, retModStr ); } else { std::string expr; write_expr( ((CExpr*)code)->kids[counter], os, ind, expr ); //clean up memory write_dec( expr, os, ind ); } counter++; } } break; case LET: { indent( os, ind ); os << "Expr* "; write_variable( ((SymSExpr*)((CExpr*)code)->kids[0])->s, os ); os << ";" << std::endl; std::ostringstream ss; write_variable( ((SymSExpr*)((CExpr*)code)->kids[0])->s, ss ); write_code( ((CExpr*)code)->kids[1], os, ind, ss.str().c_str() ); //add it to the variables vars.push_back( ((SymSExpr*)((CExpr*)code)->kids[0])->s ); write_code( ((CExpr*)code)->kids[2], os, ind, retModStr ); //clean up memory indent( os, ind ); write_variable( ((SymSExpr*)((CExpr*)code)->kids[0])->s, os ); os << "->dec();" << std::endl; } break; case FAIL: { indent( os, ind ); os << retModString.c_str() << "NULL;" << std::endl; } break; #ifndef MARKVAR_32 case MARKVAR: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[0], os, ind, expr, opt_write_check_sym_expr ); //set the mark on the expression indent( os, ind ); os << "if (" << expr.c_str() << "->followDefs()->getmark())" << std::endl; indent( os, ind+1 ); os << expr.c_str() << "->followDefs()->clearmark();" << std::endl; indent( os, ind ); os << "else" << std::endl; indent( os, ind+1 ); os << expr.c_str() << "->followDefs()->setmark();" << std::endl; //write the return if necessary if( retModStr!=NULL ){ indent( os, ind ); os << retModString.c_str() << expr.c_str() << ";" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } write_dec( expr, os, ind ); } break; case IFMARKED: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[0], os, ind, expr, opt_write_check_sym_expr ); //if mark is set, write code for kids[1] indent( os, ind ); os << "if (" << expr.c_str() << "->followDefs()->getmark()){" << std::endl; write_code( ((CExpr*)code)->kids[1], os, ind+1, retModStr ); //else write code for kids[2] indent( os, ind ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr, os, ind ); } break; #else case MARKVAR: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[1], os, ind, expr, opt_write_check_sym_expr ); //set the mark on the expression indent( os, ind ); os << "if ( ((SymExpr*)" << expr.c_str() << "->followDefs())->getmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << "))" << std::endl; indent( os, ind+1 ); os << "((SymExpr*)" << expr.c_str() << "->followDefs())->clearmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << ");" << std::endl; indent( os, ind ); os << "else" << std::endl; indent( os, ind+1 ); os << "((SymExpr*)" << expr.c_str() << "->followDefs())->setmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << ");" << std::endl; //write the return if necessary if( retModStr!=NULL ){ indent( os, ind ); os << retModString.c_str() << expr.c_str() << ";" << std::endl; indent( os, ind ); os << incString.c_str() << std::endl; } write_dec( expr, os, ind ); } break; case COMPARE: { std::string expr1, expr2; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1, opt_write_check_sym_expr ); write_expr( ((CExpr*)code)->kids[1], os, ind, expr2, opt_write_check_sym_expr ); indent( os, ind ); os << "if( ((SymExpr*)" << expr1.c_str() << ")->followDefs() < ((SymExpr*)" << expr2.c_str() << ")->followDefs() ){" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+1, retModStr ); indent( os, ind ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[3], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); write_dec( expr2, os, ind ); } break; case IFMARKED: { //calculate the value for the expression std::string expr; write_expr( ((CExpr*)code)->kids[1], os, ind, expr, opt_write_check_sym_expr ); //if mark is set, write code for kids[1] indent( os, ind ); os << "if ( ((SymExpr*)" << expr.c_str() << "->followDefs())->getmark("; os << ((IntExpr*)((CExpr*)code)->kids[0])->get_num() << ")){" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+1, retModStr ); //else write code for kids[2] indent( os, ind ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[3], os, ind+1, retModStr ); indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr, os, ind ); } break; #endif case ADD: case MUL: case DIV: { //calculate the value for the first expression std::string expr1; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1 ); //calculate the value for the second expression std::string expr2; write_expr( ((CExpr*)code)->kids[1], os, ind, expr2 ); std::ostringstream ss; ss << "rnum" << rnumCount; rnumCount++; indent( os, ind ); os << "if( " << expr1.c_str() << "->followDefs()->getclass()==INT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpz_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpz_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpz_"; if( code->getop()==ADD ) os << "add"; else os << "mul"; os << "( " << ss.str().c_str() << ", ((IntExpr*)" << expr1.c_str() << "->followDefs())->n, ((IntExpr*)" << expr2.c_str() << "->followDefs())->n);" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new IntExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}else if( " << expr1.c_str() << "->followDefs()->getclass()==RAT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpq_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpq_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpq_"; if( code->getop()==ADD ) os << "add"; else if( code->getop()==MUL ) os << "mul"; else os << "div"; os << "( " << ss.str().c_str() << ", ((RatExpr*)" << expr1.c_str() << "->followDefs())->n, ((RatExpr*)" << expr2.c_str() << "->followDefs())->n);" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new RatExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); write_dec( expr2, os, ind ); } break; case NEG: { //calculate the value for the first expression std::string expr1; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1 ); std::ostringstream ss; ss << "rnum" << rnumCount; rnumCount++; indent( os, ind ); os << "if( " << expr1.c_str() << "->followDefs()->getclass()==INT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpz_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpz_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpz_neg( " << ss.str().c_str() << ", ((IntExpr*)" << expr1.c_str() << "->followDefs())->n );" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new IntExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}else if( " << expr1.c_str() << "->followDefs()->getclass()==RAT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "mpq_t " << ss.str().c_str() << ";" << std::endl; indent( os, ind+1 ); os << "mpq_init(" << ss.str().c_str() << ");" << std::endl; indent( os, ind+1 ); os << "mpq_neg( " << ss.str().c_str() << ", ((RatExpr*)" << expr1.c_str() << "->followDefs())->n );" << std::endl; indent( os, ind+1 ); os << retModString.c_str() << "new RatExpr(" << ss.str().c_str() << ");" << std::endl; indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); } break; case IFNEG: case IFZERO: { std::string expr1; write_expr( ((CExpr*)code)->kids[0], os, ind, expr1 ); indent( os, ind ); os << "if( " << expr1.c_str() << "->followDefs()->getclass()==INT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "if( mpz_sgn( ((IntExpr *)" << expr1.c_str() << "->followDefs())->n ) "; if( code->getop()==IFNEG ) os << "<"; else os << "=="; os << " 0 ){" << std::endl; write_code( ((CExpr*)code)->kids[1], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}" << std::endl; indent( os, ind ); os << "}else if( " << expr1.c_str() << "->followDefs()->getclass()==RAT_EXPR ){" << std::endl; indent( os, ind+1 ); os << "if( mpq_sgn( ((RatExpr *)" << expr1.c_str() << "->followDefs())->n ) "; if( code->getop()==IFNEG ) os << "<"; else os << "=="; os << " 0 ){" << std::endl; write_code( ((CExpr*)code)->kids[1], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}else{" << std::endl; write_code( ((CExpr*)code)->kids[2], os, ind+2, retModStr ); indent( os, ind+1 ); os << "}" << std::endl; indent( os, ind ); os << "}" << std::endl; //clean up memory write_dec( expr1, os, ind ); } break; case RUN:/*?*/break; case PI:/*?*/break; case LAM:/*?*/break; case TYPE:/*?*/break; case KIND:/*?*/break; case ASCRIBE:/*?*/break; case MPZ:/*?*/break; case PROG:/*?*/break; case PROGVARS:/*?*/break; case PAT:/*?*/break; } break; } }