Token TokenEvalContext::evalDivide(const Token& lexpr, const Token& rexpr) const { Token left = evalToken(lexpr); Token right = evalToken(rexpr); if (left.isInt()) { int value = left.intValue(); if (value == 0) throw DivisionByZeroException(); if (right.isInt()) return Token::newInt(left.srcpos(), left.bitwidth(), value / right.intValue()); else if (right.isFloat()) return Token(left.srcpos(), kFloat, double(value) / right.floatValue()); else if (right.isRational()) return Token(left.srcpos(), kRational, Rational(value, 1) / right.rationalValue()); else throw BadExpressionException(fromInt(__LINE__)); } else if (left.isFloat()) { double value = left.floatValue(); if (value == 0) throw DivisionByZeroException(); if (right.isInt()) return Token(left.srcpos(), kFloat, value / double(right.intValue())); else if (right.isFloat()) return Token(left.srcpos(), kFloat, value / right.floatValue()); else if (right.isRational()) return Token(left.srcpos(), kFloat, value / right.rationalValue().toFloat()); else throw BadExpressionException(fromInt(__LINE__)); } else if (left.isRational()) { Rational value = left.rationalValue(); if (value.numerator() == 0) throw DivisionByZeroException(); if (right.isInt()) return Token(left.srcpos(), kRational, value / Rational(right.intValue(), 1)); else if (right.isFloat()) return Token(left.srcpos(), kFloat, value.toFloat() / right.floatValue()); else if (right.isRational()) return Token(left.srcpos(), kRational, value / right.rationalValue()); else throw BadExpressionException(fromInt(__LINE__)); } throw BadExpressionException(fromInt(__LINE__)); }
Expression * Trigonometry::shallowReduceDirectFunction(Expression * e, Context& context, Expression::AngleUnit angleUnit) { assert(e->type() == Expression::Type::Sine || e->type() == Expression::Type::Cosine || e->type() == Expression::Type::Tangent); Expression * lookup = Trigonometry::table(e->operand(0), e->type(), context, angleUnit); if (lookup != nullptr) { return e->replaceWith(lookup, true); } Expression::Type correspondingType = e->type() == Expression::Type::Cosine ? Expression::Type::ArcCosine : (e->type() == Expression::Type::Sine ? Expression::Type::ArcSine : Expression::Type::ArcTangent); if (e->operand(0)->type() == correspondingType) { float trigoOp = e->operand(0)->operand(0)->approximateToScalar<float>(context, angleUnit); if (e->type() == Expression::Type::Tangent || (trigoOp >= -1.0f && trigoOp <= 1.0f)) { return e->replaceWith(e->editableOperand(0)->editableOperand(0), true); } } if (e->operand(0)->sign() == Expression::Sign::Negative) { Expression * op = e->editableOperand(0); Expression * newOp = op->setSign(Expression::Sign::Positive, context, angleUnit); newOp->shallowReduce(context, angleUnit); if (e->type() == Expression::Type::Cosine) { return e->shallowReduce(context, angleUnit); } else { Multiplication * m = new Multiplication(new Rational(-1), e->clone(), false); m->editableOperand(1)->shallowReduce(context, angleUnit); return e->replaceWith(m, true)->shallowReduce(context, angleUnit); } } if ((angleUnit == Expression::AngleUnit::Radian && e->operand(0)->type() == Expression::Type::Multiplication && e->operand(0)->numberOfOperands() == 2 && e->operand(0)->operand(1)->type() == Expression::Type::Symbol && static_cast<const Symbol *>(e->operand(0)->operand(1))->name() == Ion::Charset::SmallPi && e->operand(0)->operand(0)->type() == Expression::Type::Rational) || (angleUnit == Expression::AngleUnit::Degree && e->operand(0)->type() == Expression::Type::Rational)) { Rational * r = angleUnit == Expression::AngleUnit::Radian ? static_cast<Rational *>(e->editableOperand(0)->editableOperand(0)) : static_cast<Rational *>(e->editableOperand(0)); int unaryCoefficient = 1; // store 1 or -1 // Replace argument in [0, Pi/2[ or [0, 90[ Integer divisor = angleUnit == Expression::AngleUnit::Radian ? r->denominator() : Integer::Multiplication(r->denominator(), Integer(90)); Integer dividand = angleUnit == Expression::AngleUnit::Radian ? Integer::Addition(r->numerator(), r->numerator()) : r->numerator(); if (divisor.isLowerThan(dividand)) { Integer piDivisor = angleUnit == Expression::AngleUnit::Radian ? r->denominator() : Integer::Multiplication(r->denominator(), Integer(180)); IntegerDivision div = Integer::Division(r->numerator(), piDivisor); dividand = angleUnit == Expression::AngleUnit::Radian ? Integer::Addition(div.remainder, div.remainder) : div.remainder; if (divisor.isLowerThan(dividand)) { div.remainder = Integer::Subtraction(piDivisor, div.remainder); if (e->type() == Expression::Type::Cosine || e->type() == Expression::Type::Tangent) { unaryCoefficient *= -1; } } Rational * newR = new Rational(div.remainder, r->denominator()); Expression * rationalParent = angleUnit == Expression::AngleUnit::Radian ? e->editableOperand(0) : e; rationalParent->replaceOperand(r, newR, true); e->editableOperand(0)->shallowReduce(context, angleUnit); if (Integer::Division(div.quotient, Integer(2)).remainder.isOne() && e->type() != Expression::Type::Tangent) { unaryCoefficient *= -1; } Expression * simplifiedCosine = e->shallowReduce(context, angleUnit); // recursive Multiplication * m = new Multiplication(new Rational(unaryCoefficient), simplifiedCosine->clone(), false); return simplifiedCosine->replaceWith(m, true)->shallowReduce(context, angleUnit); } assert(r->sign() == Expression::Sign::Positive); assert(!divisor.isLowerThan(dividand)); } return e; }
Rational Rational::Power(const Rational & i, const Integer & j) { Integer absJ = j; absJ.setNegative(false); Integer newNumerator = Integer::Power(i.numerator(), absJ); Integer newDenominator = Integer::Power(i.denominator(), absJ); if (j.isNegative()) { return Rational(newDenominator, newNumerator); } return Rational(newNumerator, newDenominator); }
double INTERN_MP_FLOAT::to_double(const Root_of_2<MP_Float> &x) { typedef MP_Float RT; typedef Quotient<RT> FT; typedef CGAL::Rational_traits< FT > Rational; Rational r; const RT r1 = r.numerator(x.alpha()); const RT d1 = r.denominator(x.alpha()); if(x.is_rational()) { std::pair<double, int> n = to_double_exp(r1); std::pair<double, int> d = to_double_exp(d1); double scale = std::ldexp(1.0, n.second - d.second); return (n.first / d.first) * scale; } const RT r2 = r.numerator(x.beta()); const RT d2 = r.denominator(x.beta()); const RT r3 = r.numerator(x.gamma()); const RT d3 = r.denominator(x.gamma()); std::pair<double, int> n1 = to_double_exp(r1); std::pair<double, int> v1 = to_double_exp(d1); double scale1 = std::ldexp(1.0, n1.second - v1.second); std::pair<double, int> n2 = to_double_exp(r2); std::pair<double, int> v2 = to_double_exp(d2); double scale2 = std::ldexp(1.0, n2.second - v2.second); std::pair<double, int> n3 = to_double_exp(r3); std::pair<double, int> v3 = to_double_exp(d3); double scale3 = std::ldexp(1.0, n3.second - v3.second); return ((n1.first / v1.first) * scale1) + ((n2.first / v2.first) * scale2) * std::sqrt((n3.first / v3.first) * scale3); }
SuperInstance* Merger::getSuperInstance(Instance* src, Instance* dst, list<Connection*>* connections ){ // Superinstance name stringstream id; id << "merger"; id << index++; //Get property of instances Actor* srcAct = src->getActor(); MoC* srcMoC = srcAct->getMoC(); Actor* dstAct = dst->getActor(); MoC* dstMoC = dstAct->getMoC(); Pattern* srcPattern = ((CSDFMoC*)srcMoC)->getOutputPattern(); Pattern* dstPattern = ((CSDFMoC*)dstMoC)->getInputPattern(); map<Port*, Port*>* internPorts = new map<Port*, Port*>(); // Calculate rate and set internal ports Rational rate; list<Connection*>::iterator it; for (it = connections->begin(); it != connections->end(); it++){ Connection* connection = *it; // Get ports of the connection Port* srcPort = connection->getSourcePort(); Port* dstPort = connection->getDestinationPort(); // Get corresponding port in actor Port* srcActPort = srcAct->getOutput(srcPort->getName()); Port* dstActPort = dstAct->getInput(dstPort->getName()); // Verify that rate of the two instances are consistent Rational compareRate = getRational(srcPattern->getNumTokens(srcActPort), dstPattern->getNumTokens(dstActPort)); if ( rate == 0){ rate = compareRate; }else if (rate != compareRate){ // This two instances can't be merged return NULL; } // Set internal ports of each instances srcPort->setInternal(true); dstPort->setInternal(true); internPorts->insert(pair<Port*, Port*>(srcPort, dstPort)); } return new SuperInstance(Context, id.str() , src, rate.numerator(), dst, rate.denominator(), internPorts); }
Token TokenEvalContext::evalExponent(const Token& lexpr, const Token& rexpr) const { Token left = evalToken(lexpr); Token right = evalToken(rexpr); if (left.isInt()) { int value = left.intValue(); if (right.isInt()) return Token(left.srcpos(), kInt, herschel::exponent(value, right.intValue())); else throw BadExpressionException(fromInt(__LINE__)); } else if (left.isFloat()) { double value = left.floatValue(); if (value == 0) throw DivisionByZeroException(); if (right.isInt()) return Token(left.srcpos(), kFloat, herschel::exponent(value, right.intValue())); else throw BadExpressionException(fromInt(__LINE__)); } else if (left.isRational()) { Rational value = left.rationalValue(); if (value.numerator() == 0) throw DivisionByZeroException(); if (right.isInt()) return Token(left.srcpos(), kRational, value.exponent(right.intValue())); else throw BadExpressionException(fromInt(__LINE__)); } throw BadExpressionException(fromInt(__LINE__)); }
int main( int argc, char** argv ) { // Command line options bool help_mode_enabled{ false }; scalar end_time_override{ -1.0 }; unsigned output_frequency{ 0 }; std::string serialized_file_name; // Attempt to load command line options if( !parseCommandLineOptions( &argc, &argv, help_mode_enabled, end_time_override, output_frequency, serialized_file_name ) ) { return EXIT_FAILURE; } // If the user requested help, print help and exit if( help_mode_enabled ) { printUsage( argv[0] ); return EXIT_SUCCESS; } // Check for impossible combinations of options #ifdef USE_HDF5 if( g_output_forces && g_output_dir_name.empty() ) { std::cerr << "Impulse output requires an output directory." << std::endl; return EXIT_FAILURE; } #endif #ifdef USE_PYTHON // Initialize the Python interpreter Py_SetProgramName( argv[0] ); Py_Initialize(); // Initialize a callback that will close down the interpreter atexit( exitCleanup ); // Allow subsequent Python commands to use the sys module PythonTools::pythonCommand( "import sys" ); // Prevent Python from intercepting the interrupt signal PythonTools::pythonCommand( "import signal" ); PythonTools::pythonCommand( "signal.signal( signal.SIGINT, signal.SIG_DFL )" ); // Initialize the callbacks PythonScripting::initializeCallbacks(); #endif if( !serialized_file_name.empty() ) { if( deserializeSystem( serialized_file_name ) == EXIT_FAILURE ) { return EXIT_FAILURE; } return executeSimLoop(); } // The user must provide the path to an xml scene file if( argc != optind + 1 ) { std::cerr << "Invalid arguments. Must provide a single xml scene file name." << std::endl; return EXIT_FAILURE; } // Attempt to load the user-provided scene if( !loadXMLScene( std::string{ argv[optind] } ) ) { return EXIT_FAILURE; } // Override the default end time with the requested one, if provided if( end_time_override > 0.0 ) { g_end_time = end_time_override; } // Compute the data output rate assert( g_dt.positive() ); // If the user provided an output frequency if( output_frequency != 0 ) { const Rational<std::intmax_t> potential_steps_per_frame{ std::intmax_t( 1 ) / ( g_dt * std::intmax_t( output_frequency ) ) }; if( !potential_steps_per_frame.isInteger() ) { std::cerr << "Timestep and output frequency do not yield an integer number of timesteps for data output. Exiting." << std::endl; return EXIT_FAILURE; } g_steps_per_save = unsigned( potential_steps_per_frame.numerator() ); } // Otherwise default to dumping every frame else { g_steps_per_save = 1; } assert( g_end_time > 0.0 ); g_save_number_width = MathUtilities::computeNumDigits( 1 + unsigned( ceil( g_end_time / scalar( g_dt ) ) ) / g_steps_per_save ); printCompileInfo( std::cout ); std::cout << "Geometry count: " << g_sim.state().ngeo() << std::endl; std::cout << "Body count: " << g_sim.state().nbodies() << std::endl; // If there are any intitial collisions, warn the user { std::map<std::string,unsigned> collision_counts; std::map<std::string,scalar> collision_depths; std::map<std::string,scalar> overlap_volumes; g_sim.computeNumberOfCollisions( collision_counts, collision_depths, overlap_volumes ); assert( collision_counts.size() == collision_depths.size() ); assert( collision_counts.size() == overlap_volumes.size() ); if( !collision_counts.empty() ) { std::cout << "Warning, initial collisions detected (name : count : total_depth : total_volume):" << std::endl; } for( const auto& count_pair : collision_counts ) { const std::string& constraint_name{ count_pair.first }; const unsigned& constraint_count{ count_pair.second }; assert( collision_depths.find( constraint_name ) != collision_depths.cend() ); const scalar& constraint_depth{ collision_depths[constraint_name] }; const scalar& constraint_volume{ overlap_volumes[constraint_name] }; std::string depth_string; if( !std::isnan( constraint_depth ) ) { depth_string = StringUtilities::convertToString( constraint_depth ); } else { depth_string = "depth_computation_not_supported"; } std::string volume_string; if( !std::isnan( constraint_volume ) ) { volume_string = StringUtilities::convertToString( constraint_volume ); } else { volume_string = "volume_computation_not_supported"; } std::cout << " " << constraint_name << " : " << constraint_count << " : " << depth_string << " : " << volume_string << std::endl; } } if( g_end_time == SCALAR_INFINITY ) { std::cout << "No end time specified. Simulation will run indefinitely." << std::endl; } //scalar total_volume = 0.0; //for( int bdy_idx = 0; bdy_idx < g_sim.state().nbodies(); ++bdy_idx ) //{ // total_volume += g_sim.state().getGeometryOfBody( bdy_idx ).volume(); //} //std::cout << "Total volume: " << total_volume << std::endl; return executeSimLoop(); }
Rational operator / ( const Rational & lhs, const Rational & rhs ) { return Rational(lhs.numerator() * rhs.denominator(), lhs.denominator() * rhs.numerator()); }
const Rational<T> operator*(const Rational<T>& lhs, const Rational<T>& rhs) { return Rational<T>(lhs.numerator() * rhs.numerator(), lhs.denominator() * lhs.denominator()); }
const Rational<T> doMultiply (const Rational<T>& lhs, const Rational<T>& rhs) { return Rational<T>(lhs.numerator()*rhs.numerator(), lhs.denominator()*rhs.denominator()); }
int Rational::NaturalOrder(const Rational & i, const Rational & j) { Integer i1 = Integer::Multiplication(i.numerator(), j.denominator()); Integer i2 = Integer::Multiplication(i.denominator(), j.numerator()); return Integer::NaturalOrder(i1, i2); }
bool Rational::operator==(const Rational& r) const { // todo: 1/2 == 2/4 return n==r.numerator() && d==r.denominator(); }
Rational Rational::Multiplication(const Rational & i, const Rational & j) { Integer newNumerator = Integer::Multiplication(i.numerator(), j.numerator()); Integer newDenominator = Integer::Multiplication(i.denominator(), j.denominator()); return Rational(newNumerator, newDenominator); }
int main() { using namespace std; using numeric::Rational; Rational a { 1, 3 }; Rational b { 3, 2 }; cout << "Rational Number Class - Test Program\n" "------------------------------------\n" << endl; Rational c = a + b; cout << a.numerator() << '/' << a.denominator() << " + " << b.numerator() << '/' << b.denominator() << " = " << c.numerator() << '/' << c.denominator() << endl; Rational d = a * c; cout << a.numerator() << '/' << a.denominator() << " * " << c.numerator() << '/' << c.denominator() << " = " << d.numerator() << '/' << d.denominator() << endl; Rational e = d - b; cout << d.numerator() << '/' << d.denominator() << " - " << b.numerator() << '/' << b.denominator() << " = " << e.numerator() << '/' << e.denominator() << endl; Rational f = e / a; cout << e.numerator() << '/' << e.denominator() << " / " << a.numerator() << '/' << a.denominator() << " = " << f.numerator() << '/' << f.denominator() << endl; cout.setf(ios::boolalpha); cout << a.numerator() << '/' << a.denominator() << " == " << b.numerator() << '/' << b.denominator() << " ? " << (a == b) << endl; cout << c.numerator() << '/' << c.denominator() << " Positive? " << (c > Rational::ZERO) << endl; cout << e.numerator() << '/' << e.denominator() << " Negative? " << (e < Rational::ZERO) << endl; return 0; }
double to_double(const Rational& a) { return double(a.numerator())/double(a.denumerator()); }
void operator<<(ostream& os, const Rational& n) { os << n.numerator() << "/" << n.denumerator() << endl; }
bool operator==(Rational a, Rational b) { if (b.numerator() == a.numerator() && b.denumerator() == a.denumerator()) return true; return false; }
Rational operator*(const Rational& a, const Rational& b) { int n = a.numerator()*b.numerator(); int d = a.denumerator()*b.denumerator(); return Rational {n,d}; }