Beispiel #1
0
void StrPrinter::bvisit(const Add &x) {
    std::ostringstream o;
    bool first = true;
    std::map<RCP<const Basic>, RCP<const Number>,
            RCPBasicKeyLessCmp> dict(x.dict_.begin(), x.dict_.end());

    if (neq(*(x.coef_), *zero)) {
        o << this->apply(x.coef_);
        first = false;
    }
    for (auto &p: dict) {
        std::string t;
        if (eq(*(p.second), *one)) {
            t = this->apply(p.first);
        } else if(eq(*(p.second), *minus_one)) {
            t = "-" + parenthesizeLT(p.first, PrecedenceEnum::Mul);
        } else {
            t = parenthesizeLT(p.second, PrecedenceEnum::Mul) + "*" + parenthesizeLT(p.first, PrecedenceEnum::Mul);
        }

        if (!first) {
            if (t[0] == '-') {
                o << " - " << t.substr(1);
            } else {
                o << " + " << t;
            }
        } else {
            o << t;
            first = false;
        }
    }
    str_ = o.str();
}
Beispiel #2
0
void StrPrinter::bvisit(const Mul &x) {
    std::ostringstream o, o2;
    bool num = false;
    unsigned den = 0;
    std::map<RCP<const Basic>, RCP<const Basic>,
            RCPBasicKeyLessCmp> dict(x.dict_.begin(), x.dict_.end());

    if (eq(*(x.coef_), *minus_one)) {
        o << "-";
    } else if (neq(*(x.coef_), *one)) {
        o << parenthesizeLT(x.coef_, PrecedenceEnum::Mul) << "*";
        num = true;
    }

    for (auto &p: dict) {
        if ((is_a<Integer>(*p.second) &&
             rcp_static_cast<const Integer>(p.second)->is_negative()) ||
            (is_a<Rational>(*p.second) &&
             rcp_static_cast<const Rational>(p.second)->is_negative())) {
            if(eq(*(p.second), *minus_one)) {
                o2 << parenthesizeLT(p.first, PrecedenceEnum::Mul);
            } else {
                o2 << parenthesizeLE(p.first, PrecedenceEnum::Pow);
                o2 << "**";
                o2 << parenthesizeLE(neg(p.second), PrecedenceEnum::Pow);
            }
            o2 << "*";
            den++;
        } else {
            if(eq(*(p.second), *one)) {
                o << parenthesizeLT(p.first, PrecedenceEnum::Mul);
            } else {
                o << parenthesizeLE(p.first, PrecedenceEnum::Pow);
                o << "**";
                o << parenthesizeLE(p.second, PrecedenceEnum::Pow);
            }
            o << "*";
            num = true;
        }
    }

    if (!num) {
        o << "1*";
    }

    std::string s = o.str();
    s = s.substr(0, s.size() - 1);

    if (den != 0) {
        std::string s2 = o2.str();
        s2 = s2.substr(0, s2.size() - 1);
        if (den > 1) {
            str_ = s + "/(" + s2 + ")";
        } else {
            str_ = s + "/" + s2;
        }
    } else {
        str_ = s;
    }
}
Beispiel #3
0
// UnivariatePolynomial printing, tests taken from SymPy and printing ensures
// that there is compatibility
void StrPrinter::bvisit(const UnivariatePolynomial &x)
{
    std::ostringstream s;
    // bool variable needed to take care of cases like -5, -x, -3*x etc.
    bool first = true;
    // we iterate over the map in reverse order so that highest degree gets
    // printed first
    for (auto it = x.get_dict().rbegin(); it != x.get_dict().rend(); ++it) {
        std::string t;
        // if exponent is 0, then print only coefficient
        if (it->first == 0) {
            if (first) {
                s << it->second;
            } else {
                t = parenthesizeLT(it->second.get_basic(), PrecedenceEnum::Mul);
                if (t[0] == '-') {
                    s << " - " << t.substr(1);
                } else {
                    s << " + " << t;
                }
            }
            first = false;
            continue;
        }
        // if the coefficient of a term is +1 or -1
        if (it->second == 1 or it->second == -1) {
            // in cases of -x, print -x
            // in cases of x**2 - x, print - x
            if (first) {
                if (it->second == -1)
                    s << "-";
            } else {
                s << " " << _print_sign(static_cast<const Integer &>(
                                            *it->second.get_basic())
                                            .as_mpz())
                  << " ";
            }
        }
        // same logic is followed as above
        else {
            // in cases of -2*x, print -2*x
            // in cases of x**2 - 2*x, print - 2*x
            if (first) {
                s << parenthesizeLT(it->second.get_basic(), PrecedenceEnum::Mul)
                  << "*";
            } else {
                t = parenthesizeLT(it->second.get_basic(), PrecedenceEnum::Mul);
                if (t[0] == '-') {
                    s << " - " << t.substr(1);
                } else {
                    s << " + " << t;
                }
                s << "*";
            }
        }
        s << x.get_var()->get_name();
        // if exponent is not 1, print the exponent;
        if (it->first > 1) {
            s << "**" << it->first;
        } else if (it->first < 0) {
            s << "**(" << it->first << ")";
        }
        // corner cases of only first term handled successfully, switch the bool
        first = false;
    }
    if (x.get_dict().size() == 0)
        s << "0";
    str_ = s.str();
}