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"
46 void ex::print(ostream & os, unsigned upper_precedence) const
48 debugmsg("ex print",LOGLEVEL_PRINT);
50 bp->print(os,upper_precedence);
53 void ex::dbgprint(void) const
55 debugmsg("ex dbgprint",LOGLEVEL_PRINT);
60 void basic::print(ostream & os, unsigned upper_precedence) const
62 debugmsg("basic print",LOGLEVEL_PRINT);
63 os << "[basic object]";
66 void basic::dbgprint(void) const
72 void symbol::print(ostream & os, unsigned upper_precedence) const
74 debugmsg("symbol print",LOGLEVEL_PRINT);
78 void constant::print(ostream & os, unsigned upper_precedence) const
80 debugmsg("constant print",LOGLEVEL_PRINT);
84 void power::print(ostream & os, unsigned upper_precedence) const
86 debugmsg("power print",LOGLEVEL_PRINT);
88 if (precedence<=upper_precedence) os << "(";
89 basis.print(os,precedence);
91 exponent.print(os,precedence);
92 if (precedence<=upper_precedence) os << ")";
94 os << "pow(" << basis << "," << exponent << ")";
98 void fail::print(ostream & os, unsigned upper_precedence) const
100 debugmsg("fail print",LOGLEVEL_PRINT);
104 void expairseq::printpair(ostream & os, expair const & p, unsigned upper_precedence) const
107 p.rest.bp->print(os,precedence);
109 p.coeff.bp->print(os,precedence);
114 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
115 unsigned upper_precedence) const
117 if (this_precedence<=upper_precedence) os << "(";
118 epvector::const_iterator itt,it,it_last,it_start;
121 it_start=seq.begin();
125 for (it=seq.begin(); it!=it_last; ++it) {
127 expair const & k = *itt;
128 printpair(os,*it, this_precedence);
129 if (((is_ex_of_type(k.rest, numeric)) && (k.coeff*k.rest > 0) ) || ((!is_ex_of_type(k.rest, numeric)) && (k.coeff >0))){
133 printpair(os,*it,this_precedence);
137 for (it = it_last ; it!=it_start; --it) {
138 if ((*it).rest.is_equal(exMINUSONE()) &&
139 (*it).coeff.is_equal(exONE())) {
142 printpair(os, *it,this_precedence);
146 printpair(os,*it,this_precedence);
149 clog << "Nobody expects the Spanish Inquisition: "
150 << "deliminator unknown!" << endl;
152 if (this_precedence<=upper_precedence) os << ")";
156 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
157 unsigned upper_precedence) const
159 if (this_precedence<=upper_precedence) os << "(";
160 epvector::const_iterator it,it_last;
163 for (it=seq.begin(); it!=it_last; ++it) {
164 printpair(os,*it,this_precedence);
167 printpair(os,*it,this_precedence);
168 if (!overall_coeff.is_equal(default_overall_coeff())) {
169 os << delim << overall_coeff;
171 if (this_precedence<=upper_precedence) os << ")";
174 void expairseq::print(ostream & os, unsigned upper_precedence) const
176 debugmsg("expairseq print",LOGLEVEL_PRINT);
178 printseq(os,',',precedence,upper_precedence);
182 void add::print(ostream & os, unsigned upper_precedence) const
184 debugmsg("add print",LOGLEVEL_PRINT);
185 if (precedence<=upper_precedence) os << "(";
188 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
189 coeff = ex_to_numeric(cit->coeff);
191 if (coeff < 0) os << '-'; else os << '+';
193 if (coeff < 0) os << '-';
196 if (coeff.compare(numONE()) && coeff.compare(numMINUSONE())) {
197 if (!coeff.is_real() && !coeff.real().is_zero()) os << '(';
201 os << numeric(-1)*coeff;
202 if (!coeff.is_real() && !coeff.real().is_zero()) os << ')';
207 // print the overall numeric coefficient, if present:
208 if (!overall_coeff.is_zero()) {
209 if (overall_coeff > 0) os << '+';
212 if (precedence<=upper_precedence) os << ")";
215 void mul::print(ostream & os, unsigned upper_precedence) const
217 debugmsg("mul print",LOGLEVEL_PRINT);
218 if (precedence<=upper_precedence) os << "(";
220 if (!overall_coeff.is_equal(exONE())) {
221 overall_coeff.print(os,precedence);
224 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
230 recombine_pair_to_ex(*cit).print(os,precedence);
232 if (precedence<=upper_precedence) os << ")";
235 void ncmul::print(ostream & os, unsigned upper_precedence) const
237 debugmsg("ncmul print",LOGLEVEL_PRINT);
238 printseq(os,'(','%',')',precedence,upper_precedence);
241 /*void function::print(ostream & os, unsigned upper_precedence) const
243 * debugmsg("function print",LOGLEVEL_PRINT);
245 * printseq(os,'(',',',')',exprseq::precedence,function::precedence);
248 void series::print(ostream &os, unsigned upper_precedence) const
250 debugmsg("symbol print", LOGLEVEL_PRINT);
251 convert_to_poly().print(os, upper_precedence);
254 void relational::print(ostream & os, unsigned upper_precedence) const
256 debugmsg("relational print",LOGLEVEL_PRINT);
257 if (precedence<=upper_precedence) os << "(";
258 lh.print(os,precedence);
275 case greater_or_equal:
279 os << "(INVALID RELATIONAL OPERATOR)";
281 rh.print(os,precedence);
282 if (precedence<=upper_precedence) os << ")";
285 void matrix::print(ostream & os, unsigned upper_precedence) const
287 debugmsg("matrix print",LOGLEVEL_PRINT);
289 for (int r=0; r<row-1; ++r) {
291 for (int c=0; c<col-1; ++c) {
292 os << m[r*col+c] << ",";
294 os << m[col*(r+1)-1] << "]], ";
297 for (int c=0; c<col-1; ++c) {
298 os << m[(row-1)*col+c] << ",";
300 os << m[row*col-1] << "]] ]]";