+void power::print(ostream & os, unsigned upper_precedence) const
+{
+ debugmsg("power print",LOGLEVEL_PRINT);
+ if (exponent.is_equal(_ex1_2())) {
+ os << "sqrt(" << basis << ")";
+ } else {
+ if (precedence<=upper_precedence) os << "(";
+ basis.print(os,precedence);
+ os << "^";
+ exponent.print(os,precedence);
+ if (precedence<=upper_precedence) os << ")";
+ }
+}
+
+void power::printraw(ostream & os) const
+{
+ debugmsg("power printraw",LOGLEVEL_PRINT);
+
+ os << "power(";
+ basis.printraw(os);
+ os << ",";
+ exponent.printraw(os);
+ os << ",hash=" << hashvalue << ",flags=" << flags << ")";
+}
+
+void power::printtree(ostream & os, unsigned indent) const
+{
+ debugmsg("power printtree",LOGLEVEL_PRINT);
+
+ os << string(indent,' ') << "power: "
+ << "hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
+ << ", flags=" << flags << endl;
+ basis.printtree(os,indent+delta_indent);
+ exponent.printtree(os,indent+delta_indent);
+}
+
+static void print_sym_pow(ostream & os, unsigned type, const symbol &x, int exp)
+{
+ // Optimal output of integer powers of symbols to aid compiler CSE
+ if (exp == 1) {
+ x.printcsrc(os, type, 0);
+ } else if (exp == 2) {
+ x.printcsrc(os, type, 0);
+ os << "*";
+ x.printcsrc(os, type, 0);
+ } else if (exp & 1) {
+ x.printcsrc(os, 0);
+ os << "*";
+ print_sym_pow(os, type, x, exp-1);
+ } else {
+ os << "(";
+ print_sym_pow(os, type, x, exp >> 1);
+ os << ")*(";
+ print_sym_pow(os, type, x, exp >> 1);
+ os << ")";
+ }
+}
+
+void power::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
+{
+ debugmsg("power print csrc", LOGLEVEL_PRINT);
+
+ // Integer powers of symbols are printed in a special, optimized way
+ if (exponent.info(info_flags::integer) &&
+ (is_ex_exactly_of_type(basis, symbol) ||
+ is_ex_exactly_of_type(basis, constant))) {
+ int exp = ex_to_numeric(exponent).to_int();
+ if (exp > 0)
+ os << "(";
+ else {
+ exp = -exp;
+ if (type == csrc_types::ctype_cl_N)
+ os << "recip(";
+ else
+ os << "1.0/(";
+ }
+ print_sym_pow(os, type, static_cast<const symbol &>(*basis.bp), exp);
+ os << ")";
+
+ // <expr>^-1 is printed as "1.0/<expr>" or with the recip() function of CLN
+ } else if (exponent.compare(_num_1()) == 0) {
+ if (type == csrc_types::ctype_cl_N)
+ os << "recip(";
+ else
+ os << "1.0/(";
+ basis.bp->printcsrc(os, type, 0);
+ os << ")";
+
+ // Otherwise, use the pow() or expt() (CLN) functions
+ } else {
+ if (type == csrc_types::ctype_cl_N)
+ os << "expt(";
+ else
+ os << "pow(";
+ basis.bp->printcsrc(os, type, 0);
+ os << ",";
+ exponent.bp->printcsrc(os, type, 0);
+ os << ")";
+ }
+}
+