3 * The methods .print() are responsible for the nice default-output of
4 * objects. All related helper-functions go in here as well. */
7 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "expairseq.h"
39 #include "relational.h"
43 void ex::print(ostream & os, unsigned upper_precedence) const
45 debugmsg("ex print",LOGLEVEL_PRINT);
47 bp->print(os,upper_precedence);
50 void ex::dbgprint(void) const
52 debugmsg("ex dbgprint",LOGLEVEL_PRINT);
57 void basic::print(ostream & os, unsigned upper_precedence) const
59 debugmsg("basic print",LOGLEVEL_PRINT);
60 os << "[basic object]";
63 void basic::dbgprint(void) const
69 void symbol::print(ostream & os, unsigned upper_precedence) const
71 debugmsg("symbol print",LOGLEVEL_PRINT);
75 void constant::print(ostream & os, unsigned upper_precedence) const
77 debugmsg("constant print",LOGLEVEL_PRINT);
81 void power::print(ostream & os, unsigned upper_precedence) const
83 debugmsg("power print",LOGLEVEL_PRINT);
85 if (precedence<=upper_precedence) os << "(";
86 basis.print(os,precedence);
88 exponent.print(os,precedence);
89 if (precedence<=upper_precedence) os << ")";
91 os << "pow(" << basis << "," << exponent << ")";
95 void fail::print(ostream & os, unsigned upper_precedence) const
97 debugmsg("fail print",LOGLEVEL_PRINT);
101 void expairseq::printpair(ostream & os, expair const & p, unsigned upper_precedence) const
104 p.rest.bp->print(os,precedence);
106 p.coeff.bp->print(os,precedence);
111 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
112 unsigned upper_precedence) const
114 if (this_precedence<=upper_precedence) os << "(";
115 epvector::const_iterator itt,it,it_last,it_start;
118 it_start=seq.begin();
122 for (it=seq.begin(); it!=it_last; ++it) {
124 expair const & k = *itt;
125 printpair(os,*it, this_precedence);
126 if (((is_ex_of_type(k.rest, numeric)) && (k.coeff*k.rest > 0) ) || ((!is_ex_of_type(k.rest, numeric)) && (k.coeff >0))){
130 printpair(os,*it,this_precedence);
134 for (it = it_last ; it!=it_start; --it) {
135 if ((*it).rest.is_equal(exMINUSONE()) &&
136 (*it).coeff.is_equal(exONE())) {
139 printpair(os, *it,this_precedence);
143 printpair(os,*it,this_precedence);
146 clog << "Nobody expects the Spanish Inquisition: "
147 << "deliminator unknown!" << endl;
149 if (this_precedence<=upper_precedence) os << ")";
153 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
154 unsigned upper_precedence) const
156 if (this_precedence<=upper_precedence) os << "(";
157 epvector::const_iterator it,it_last;
160 for (it=seq.begin(); it!=it_last; ++it) {
161 printpair(os,*it,this_precedence);
164 printpair(os,*it,this_precedence);
165 if (!overall_coeff.is_equal(default_overall_coeff())) {
166 os << delim << overall_coeff;
168 if (this_precedence<=upper_precedence) os << ")";
171 void expairseq::print(ostream & os, unsigned upper_precedence) const
173 debugmsg("expairseq print",LOGLEVEL_PRINT);
175 printseq(os,',',precedence,upper_precedence);
179 void add::printpair(ostream & os, expair const & p, unsigned upper_precedence) const
194 void add::print(ostream & os, unsigned upper_precedence) const
196 debugmsg("add print",LOGLEVEL_PRINT);
197 if (precedence<=upper_precedence) os << "(";
199 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
201 if (cit->coeff > 0) os << '+';
205 if (cit->coeff == -1) {
208 if (cit->coeff != 1) {
209 (cit->coeff).print(os,precedence);
215 if (!overall_coeff.is_zero()) {
216 if (overall_coeff > 0) os << '+';
219 if (precedence<=upper_precedence) os << ")";
222 void mul::printpair(ostream & os, expair const & p, unsigned upper_precedence) const
225 if (p.coeff.compare(exONE())==0) {
226 p.rest.print(os,upper_precedence);
228 // outer parens around ex needed for broken gcc-2.95 parser:
229 (ex(power(p.rest,p.coeff))).print(os,upper_precedence);
234 void mul::print(ostream & os, unsigned upper_precedence) const
236 debugmsg("mul print",LOGLEVEL_PRINT);
237 if (precedence<=upper_precedence) os << "(";
239 if (!overall_coeff.is_equal(exONE())) {
240 overall_coeff.print(os,precedence);
243 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
249 recombine_pair_to_ex(*cit).print(os,precedence);
251 if (precedence<=upper_precedence) os << ")";
254 void ncmul::print(ostream & os, unsigned upper_precedence) const
256 debugmsg("ncmul print",LOGLEVEL_PRINT);
257 printseq(os,'(','%',')',precedence,upper_precedence);
260 /*void function::print(ostream & os, unsigned upper_precedence) const
262 * debugmsg("function print",LOGLEVEL_PRINT);
264 * printseq(os,'(',',',')',exprseq::precedence,function::precedence);
267 void series::print(ostream &os, unsigned upper_precedence) const
269 debugmsg("symbol print", LOGLEVEL_PRINT);
270 convert_to_poly().print(os, upper_precedence);
273 void relational::print(ostream & os, unsigned upper_precedence) const
275 debugmsg("relational print",LOGLEVEL_PRINT);
276 if (precedence<=upper_precedence) os << "(";
277 lh.print(os,precedence);
294 case greater_or_equal:
298 os << "(INVALID RELATIONAL OPERATOR)";
300 rh.print(os,precedence);
301 if (precedence<=upper_precedence) os << ")";
304 void matrix::print(ostream & os, unsigned upper_precedence) const
306 debugmsg("matrix print",LOGLEVEL_PRINT);
308 for (int r=0; r<row-1; ++r) {
310 for (int c=0; c<col-1; ++c) {
311 os << m[r*col+c] << ",";
313 os << m[col*(r+1)-1] << "]], ";
316 for (int c=0; c<col-1; ++c) {
317 os << m[(row-1)*col+c] << ",";
319 os << m[row*col-1] << "]] ]]";