X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?a=blobdiff_plain;f=ginac%2Fmul.cpp;h=75f74b4ba2cb7d06fc1ce969f4072ba7e4494618;hb=ae41c1e2347d2308a1019746b8d7e3feed02e9e6;hp=de5d03c03cf3a8d9fd08bb53c0c365a3334248cd;hpb=094911eb78cacb6f2877a70c9ac74766df58ccea;p=ginac.git diff --git a/ginac/mul.cpp b/ginac/mul.cpp index de5d03c0..75f74b4b 100644 --- a/ginac/mul.cpp +++ b/ginac/mul.cpp @@ -38,26 +38,14 @@ GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq) // default ctor, dctor, copy ctor assignment operator and helpers ////////// -// public - mul::mul() { debugmsg("mul default ctor",LOGLEVEL_CONSTRUCT); tinfo_key = TINFO_mul; } -// protected - -/** For use by copy ctor and assignment operator. */ -void mul::copy(const mul & other) -{ - inherited::copy(other); -} - -void mul::destroy(bool call_parent) -{ - if (call_parent) inherited::destroy(call_parent); -} +DEFAULT_COPY(mul) +DEFAULT_DESTROY(mul) ////////// // other ctors @@ -130,23 +118,7 @@ mul::mul(const ex & lh, const ex & mh, const ex & rh) // archiving ////////// -/** Construct object from archive_node. */ -mul::mul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) -{ - debugmsg("mul ctor from archive_node", LOGLEVEL_CONSTRUCT); -} - -/** Unarchive the object. */ -ex mul::unarchive(const archive_node &n, const lst &sym_lst) -{ - return (new mul(n, sym_lst))->setflag(status_flags::dynallocated); -} - -/** Archive the object. */ -void mul::archive(archive_node &n) const -{ - inherited::archive(n); -} +DEFAULT_ARCHIVING(mul) ////////// // functions overriding virtual functions from bases classes @@ -154,99 +126,112 @@ void mul::archive(archive_node &n) const // public -void mul::print(std::ostream & os, unsigned upper_precedence) const -{ - debugmsg("mul print",LOGLEVEL_PRINT); - if (precedence<=upper_precedence) os << "("; - bool first = true; - // first print the overall numeric coefficient: - numeric coeff = ex_to_numeric(overall_coeff); - if (coeff.csgn()==-1) os << '-'; - if (!coeff.is_equal(_num1()) && - !coeff.is_equal(_num_1())) { - if (coeff.is_rational()) { - if (coeff.is_negative()) - os << -coeff; - else - os << coeff; - } else { - if (coeff.csgn()==-1) - (-coeff).print(os, precedence); - else - coeff.print(os, precedence); +void mul::print(const print_context & c, unsigned level) const +{ + debugmsg("mul print", LOGLEVEL_PRINT); + + if (is_of_type(c, print_tree)) { + + inherited::print(c, level); + + } else if (is_of_type(c, print_csrc)) { + + c.s << "("; + + if (!overall_coeff.is_equal(_ex1())) { + overall_coeff.bp->print(c, precedence); + c.s << "*"; } - os << '*'; - } - // then proceed with the remaining factors: - for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { - if (!first) { - os << '*'; - } else { - first=false; + + // Print arguments, separated by "*" or "/" + epvector::const_iterator it = seq.begin(), itend = seq.end(); + while (it != itend) { + + // If the first argument is a negative integer power, it gets printed as "1.0/" + if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) { + if (is_of_type(c, print_csrc_cl_N)) + c.s << "recip("; + else + c.s << "1.0/"; + } + + // If the exponent is 1 or -1, it is left out + if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0) + it->rest.print(c, precedence); + else { + // Outer parens around ex needed for broken gcc-2.95 parser: + (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).print(c, level); + } + + // Separator is "/" for negative integer powers, "*" otherwise + ++it; + if (it != itend) { + if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) + c.s << "/"; + else + c.s << "*"; + } } - recombine_pair_to_ex(*cit).print(os,precedence); - } - if (precedence<=upper_precedence) os << ")"; -} -void mul::printraw(std::ostream & os) const -{ - debugmsg("mul printraw",LOGLEVEL_PRINT); + if (precedence <= level) + c.s << ")"; - os << "*("; - for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - os << "("; - (*it).rest.bp->printraw(os); - os << ","; - (*it).coeff.bp->printraw(os); - os << "),"; - } - os << ",hash=" << hashvalue << ",flags=" << flags; - os << ")"; -} + } else { -void mul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const -{ - debugmsg("mul print csrc", LOGLEVEL_PRINT); - if (precedence <= upper_precedence) - os << "("; + if (precedence <= level) { + if (is_of_type(c, print_latex)) + c.s << "{("; + else + c.s << "("; + } - if (!overall_coeff.is_equal(_ex1())) { - overall_coeff.bp->printcsrc(os,type,precedence); - os << "*"; - } - - // Print arguments, separated by "*" or "/" - epvector::const_iterator it = seq.begin(); - epvector::const_iterator itend = seq.end(); - while (it != itend) { - - // If the first argument is a negative integer power, it gets printed as "1.0/" - if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) { - if (type == csrc_types::ctype_cl_N) - os << "recip("; + bool first = true; + + // First print the overall numeric coefficient + numeric coeff = ex_to_numeric(overall_coeff); + if (coeff.csgn() == -1) + c.s << '-'; + if (!coeff.is_equal(_num1()) && + !coeff.is_equal(_num_1())) { + if (coeff.is_rational()) { + if (coeff.is_negative()) + (-coeff).print(c); + else + coeff.print(c); + } else { + if (coeff.csgn() == -1) + (-coeff).print(c, precedence); + else + coeff.print(c, precedence); + } + if (is_of_type(c, print_latex)) + c.s << ' '; else - os << "1.0/"; + c.s << '*'; } - // If the exponent is 1 or -1, it is left out - if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0) - it->rest.bp->printcsrc(os, type, precedence); - else - // outer parens around ex needed for broken gcc-2.95 parser: - (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence); + // Then proceed with the remaining factors + epvector::const_iterator it = seq.begin(), itend = seq.end(); + while (it != itend) { + if (!first) { + if (is_of_type(c, print_latex)) + c.s << ' '; + else + c.s << '*'; + } else { + first = false; + } + recombine_pair_to_ex(*it).print(c, precedence); + it++; + } - // Separator is "/" for negative integer powers, "*" otherwise - ++it; - if (it != itend) { - if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) - os << "/"; + if (precedence <= level) { + if (is_of_type(c, print_latex)) + c.s << ")}"; else - os << "*"; + c.s << ")"; } } - if (precedence <= upper_precedence) - os << ")"; } bool mul::info(unsigned inf) const @@ -275,7 +260,7 @@ bool mul::info(unsigned inf) const return inherited::info(inf); } -int mul::degree(const symbol & s) const +int mul::degree(const ex & s) const { int deg_sum = 0; for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { @@ -285,7 +270,7 @@ int mul::degree(const symbol & s) const return deg_sum; } -int mul::ldegree(const symbol & s) const +int mul::ldegree(const ex & s) const { int deg_sum = 0; for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { @@ -295,7 +280,7 @@ int mul::ldegree(const symbol & s) const return deg_sum; } -ex mul::coeff(const symbol & s, int n) const +ex mul::coeff(const ex & s, int n) const { exvector coeffseq; coeffseq.reserve(seq.size()+1); @@ -355,7 +340,7 @@ ex mul::eval(int level) const (!(ex_to_numeric((*cit).coeff).is_integer()))); GINAC_ASSERT(!(cit->is_canonical_numeric())); if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) - printtree(std::cerr,0); + print(print_tree(std::cerr)); GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)); /* for paranoia */ expair p = split_ex_to_pair(recombine_pair_to_ex(*cit)); @@ -420,7 +405,16 @@ ex mul::evalf(int level) const ex mul::simplify_ncmul(const exvector & v) const { - throw(std::logic_error("mul::simplify_ncmul() should never have been called!")); + if (seq.size()==0) { + return inherited::simplify_ncmul(v); + } + + // Find first noncommutative element and call its simplify_ncmul() + for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { + if (cit->rest.return_type() == return_types::noncommutative) + return cit->rest.simplify_ncmul(v); + } + return inherited::simplify_ncmul(v); } // protected @@ -647,6 +641,8 @@ ex mul::expand(unsigned options) const } ++cit; } + if (expanded_seqp) + delete expanded_seqp; if (is_ex_exactly_of_type(last_expanded,add)) { add const & finaladd = ex_to_add(last_expanded);