3 * The methods .print() are responsible for the nice default-output of
4 * objects. All related helper-functions go in here as well.
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "expairseq.h"
38 #include "relational.h"
42 void ex::print(ostream & os, unsigned upper_precedence) const
44 debugmsg("ex print",LOGLEVEL_PRINT);
46 bp->print(os,upper_precedence);
49 void ex::dbgprint(void) const
51 debugmsg("ex dbgprint",LOGLEVEL_PRINT);
56 void basic::print(ostream & os, unsigned upper_precedence) const
58 debugmsg("basic print",LOGLEVEL_PRINT);
59 os << "[basic object]";
62 void basic::dbgprint(void) const
68 void symbol::print(ostream & os, unsigned upper_precedence) const
70 debugmsg("symbol print",LOGLEVEL_PRINT);
74 void constant::print(ostream & os, unsigned upper_precedence) const
76 debugmsg("constant print",LOGLEVEL_PRINT);
80 void power::print(ostream & os, unsigned upper_precedence) const
82 debugmsg("power print",LOGLEVEL_PRINT);
84 if (precedence<=upper_precedence) os << "(";
85 basis.print(os,precedence);
87 exponent.print(os,precedence);
88 if (precedence<=upper_precedence) os << ")";
90 os << "pow(" << basis << "," << exponent << ")";
94 void fail::print(ostream & os, unsigned upper_precedence) const
96 debugmsg("fail print",LOGLEVEL_PRINT);
100 void expairseq::printpair(ostream & os, expair const & p, unsigned upper_precedence) const
103 p.rest.bp->print(os,precedence);
105 p.coeff.bp->print(os,precedence);
110 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
111 unsigned upper_precedence) const
113 if (this_precedence<=upper_precedence) os << "(";
114 epvector::const_iterator itt,it,it_last,it_start;
117 it_start=seq.begin();
121 for (it=seq.begin(); it!=it_last; ++it) {
123 expair const & k = *itt;
124 printpair(os,*it, this_precedence);
125 if (((is_ex_of_type(k.rest, numeric)) && (k.coeff*k.rest > 0) ) || ((!is_ex_of_type(k.rest, numeric)) && (k.coeff >0))){
129 printpair(os,*it,this_precedence);
133 for (it = it_last ; it!=it_start; --it) {
134 if ((*it).rest.is_equal(exMINUSONE()) &&
135 (*it).coeff.is_equal(exONE())) {
138 printpair(os, *it,this_precedence);
142 printpair(os,*it,this_precedence);
145 clog << "Nobody expects the Spanish Inquisition: "
146 << "deliminator unknown!" << endl;
148 if (this_precedence<=upper_precedence) os << ")";
152 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
153 unsigned upper_precedence) const
155 if (this_precedence<=upper_precedence) os << "(";
156 epvector::const_iterator it,it_last;
159 for (it=seq.begin(); it!=it_last; ++it) {
160 printpair(os,*it,this_precedence);
163 printpair(os,*it,this_precedence);
164 if (!overall_coeff.is_equal(default_overall_coeff())) {
165 os << delim << overall_coeff;
167 if (this_precedence<=upper_precedence) os << ")";
170 void expairseq::print(ostream & os, unsigned upper_precedence) const
172 debugmsg("expairseq print",LOGLEVEL_PRINT);
174 printseq(os,',',precedence,upper_precedence);
178 void add::printpair(ostream & os, expair const & p, unsigned upper_precedence) const
193 void add::print(ostream & os, unsigned upper_precedence) const
195 debugmsg("add print",LOGLEVEL_PRINT);
196 if (precedence<=upper_precedence) os << "(";
198 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
200 if (cit->coeff > 0) os << '+';
204 if (cit->coeff == -1) {
207 if (cit->coeff != 1) {
208 (cit->coeff).print(os,precedence);
214 if (!overall_coeff.is_zero()) {
215 if (overall_coeff > 0) os << '+';
218 if (precedence<=upper_precedence) os << ")";
221 void mul::printpair(ostream & os, expair const & p, unsigned upper_precedence) const
224 if (p.coeff.compare(exONE())==0) {
225 p.rest.print(os,upper_precedence);
227 // outer parens around ex needed for broken gcc-2.95 parser:
228 (ex(power(p.rest,p.coeff))).print(os,upper_precedence);
233 void mul::print(ostream & os, unsigned upper_precedence) const
235 debugmsg("mul print",LOGLEVEL_PRINT);
236 if (precedence<=upper_precedence) os << "(";
238 if (!overall_coeff.is_equal(exONE())) {
239 overall_coeff.print(os,precedence);
242 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
248 recombine_pair_to_ex(*cit).print(os,precedence);
250 if (precedence<=upper_precedence) os << ")";
253 void ncmul::print(ostream & os, unsigned upper_precedence) const
255 debugmsg("ncmul print",LOGLEVEL_PRINT);
256 printseq(os,'(','%',')',precedence,upper_precedence);
259 /*void function::print(ostream & os, unsigned upper_precedence) const
261 * debugmsg("function print",LOGLEVEL_PRINT);
263 * printseq(os,'(',',',')',exprseq::precedence,function::precedence);
266 void series::print(ostream &os, unsigned upper_precedence) const
268 debugmsg("symbol print", LOGLEVEL_PRINT);
269 convert_to_poly().print(os, upper_precedence);
272 void relational::print(ostream & os, unsigned upper_precedence) const
274 debugmsg("relational print",LOGLEVEL_PRINT);
275 if (precedence<=upper_precedence) os << "(";
276 lh.print(os,precedence);
293 case greater_or_equal:
297 os << "(INVALID RELATIONAL OPERATOR)";
299 rh.print(os,precedence);
300 if (precedence<=upper_precedence) os << ")";
303 void matrix::print(ostream & os, unsigned upper_precedence) const
305 debugmsg("matrix print",LOGLEVEL_PRINT);
307 for (int r=0; r<row-1; ++r) {
309 for (int c=0; c<col-1; ++c) {
310 os << m[r*col+c] << ",";
312 os << m[col*(r+1)-1] << "]], ";
315 for (int c=0; c<col-1; ++c) {
316 os << m[(row-1)*col+c] << ",";
318 os << m[row*col-1] << "]] ]]";