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); }
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); }
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); }
bool Rational::operator==(const Rational& r) const { // todo: 1/2 == 2/4 return n==r.numerator() && d==r.denominator(); }
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; }