/[GiNaC]/ginac/add.cpp
ViewVC logotype

Contents of /ginac/add.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Mon Dec 20 17:12:36 1999 UTC (13 years, 5 months ago) by kreckel
Branch: MAIN
Changes since 1.10: +52 -5 lines
- more logic on the trigonometric function stuff.
- changed several occurences of numeric const & to const numeric &, which
  is the same, but doxygen kept being confused because declarations
  differed from implementations.

1 cbauer 1.1 /** @file add.cpp
2     *
3 cbauer 1.4 * Implementation of GiNaC's sums of expressions. */
4    
5     /*
6 cbauer 1.2 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7     *
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.
12     *
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.
17     *
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
21     */
22 cbauer 1.1
23     #include <iostream>
24     #include <stdexcept>
25    
26 cbauer 1.3 #include "add.h"
27     #include "mul.h"
28 cbauer 1.5 #include "debugmsg.h"
29 kreckel 1.10 #include "utils.h"
30 cbauer 1.5
31 frink 1.7 #ifndef NO_GINAC_NAMESPACE
32 cbauer 1.5 namespace GiNaC {
33 frink 1.7 #endif // ndef NO_GINAC_NAMESPACE
34 cbauer 1.1
35     //////////
36     // default constructor, destructor, copy constructor assignment operator and helpers
37     //////////
38    
39     // public
40    
41     add::add()
42     {
43     debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
44 cbauer 1.3 tinfo_key = TINFO_add;
45 cbauer 1.1 }
46    
47     add::~add()
48     {
49     debugmsg("add destructor",LOGLEVEL_DESTRUCT);
50     destroy(0);
51     }
52    
53     add::add(add const & other)
54     {
55     debugmsg("add copy constructor",LOGLEVEL_CONSTRUCT);
56     copy(other);
57     }
58    
59     add const & add::operator=(add const & other)
60     {
61     debugmsg("add operator=",LOGLEVEL_ASSIGNMENT);
62     if (this != &other) {
63     destroy(1);
64     copy(other);
65     }
66     return *this;
67     }
68    
69     // protected
70    
71     void add::copy(add const & other)
72     {
73     expairseq::copy(other);
74     }
75    
76     void add::destroy(bool call_parent)
77     {
78     if (call_parent) expairseq::destroy(call_parent);
79     }
80    
81     //////////
82     // other constructors
83     //////////
84    
85     // public
86    
87     add::add(ex const & lh, ex const & rh)
88     {
89     debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
90 cbauer 1.3 tinfo_key = TINFO_add;
91 kreckel 1.10 overall_coeff=_ex0();
92 cbauer 1.1 construct_from_2_ex(lh,rh);
93 cbauer 1.6 GINAC_ASSERT(is_canonical());
94 cbauer 1.1 }
95    
96     add::add(exvector const & v)
97     {
98     debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
99 cbauer 1.3 tinfo_key = TINFO_add;
100 kreckel 1.10 overall_coeff=_ex0();
101 cbauer 1.1 construct_from_exvector(v);
102 cbauer 1.6 GINAC_ASSERT(is_canonical());
103 cbauer 1.1 }
104    
105     /*
106     add::add(epvector const & v, bool do_not_canonicalize)
107     {
108     debugmsg("add constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
109 cbauer 1.3 tinfo_key = TINFO_add;
110 cbauer 1.1 if (do_not_canonicalize) {
111     seq=v;
112     #ifdef EXPAIRSEQ_USE_HASHTAB
113     combine_same_terms(); // to build hashtab
114     #endif // def EXPAIRSEQ_USE_HASHTAB
115     } else {
116     construct_from_epvector(v);
117     }
118 cbauer 1.6 GINAC_ASSERT(is_canonical());
119 cbauer 1.1 }
120     */
121    
122     add::add(epvector const & v)
123     {
124     debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
125 cbauer 1.3 tinfo_key = TINFO_add;
126 kreckel 1.10 overall_coeff=_ex0();
127 cbauer 1.1 construct_from_epvector(v);
128 cbauer 1.6 GINAC_ASSERT(is_canonical());
129 cbauer 1.1 }
130    
131     add::add(epvector const & v, ex const & oc)
132     {
133     debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
134 cbauer 1.3 tinfo_key = TINFO_add;
135 cbauer 1.1 overall_coeff=oc;
136     construct_from_epvector(v);
137 cbauer 1.6 GINAC_ASSERT(is_canonical());
138 cbauer 1.1 }
139    
140     add::add(epvector * vp, ex const & oc)
141     {
142     debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
143 cbauer 1.3 tinfo_key = TINFO_add;
144 cbauer 1.6 GINAC_ASSERT(vp!=0);
145 cbauer 1.1 overall_coeff=oc;
146     construct_from_epvector(*vp);
147     delete vp;
148 cbauer 1.6 GINAC_ASSERT(is_canonical());
149 cbauer 1.1 }
150    
151     //////////
152     // functions overriding virtual functions from bases classes
153     //////////
154    
155     // public
156    
157     basic * add::duplicate() const
158     {
159     debugmsg("add duplicate",LOGLEVEL_DUPLICATE);
160     return new add(*this);
161     }
162    
163 kreckel 1.11 /*void add::print(ostream & os, unsigned upper_precedence) const
164 kreckel 1.9 {
165     debugmsg("add print",LOGLEVEL_PRINT);
166     if (precedence<=upper_precedence) os << "(";
167     numeric coeff;
168     bool first=true;
169     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
170     coeff = ex_to_numeric(cit->coeff);
171     if (!first) {
172     if (coeff.csgn()==-1) os << '-'; else os << '+';
173     } else {
174     if (coeff.csgn()==-1) os << '-';
175     first=false;
176     }
177 kreckel 1.10 if (!coeff.is_equal(_num1()) &&
178     !coeff.is_equal(_num_1())) {
179 kreckel 1.11 if (coeff.is_rational()) {
180     if (coeff.is_negative())
181     os << -coeff;
182     else
183     os << coeff;
184     } else {
185     if (coeff.csgn()==-1)
186     (-coeff).print(os, precedence);
187     else
188     coeff.print(os, precedence);
189     }
190 kreckel 1.9 os << '*';
191     }
192     os << cit->rest;
193     }
194     // print the overall numeric coefficient, if present:
195     if (!overall_coeff.is_zero()) {
196     if (overall_coeff.info(info_flags::positive)) os << '+';
197     os << overall_coeff;
198 kreckel 1.11 }
199     if (precedence<=upper_precedence) os << ")";
200     }*/
201    
202     void add::print(ostream & os, unsigned upper_precedence) const
203     {
204     debugmsg("add print",LOGLEVEL_PRINT);
205     if (precedence<=upper_precedence) os << "(";
206     numeric coeff;
207     bool first = true;
208     // first print the overall numeric coefficient, if present:
209     if (!overall_coeff.is_zero()) {
210     os << overall_coeff;
211     first = false;
212     }
213     // then proceed with the remaining factors:
214     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
215     coeff = ex_to_numeric(cit->coeff);
216     if (!first) {
217     if (coeff.csgn()==-1) os << '-'; else os << '+';
218     } else {
219     if (coeff.csgn()==-1) os << '-';
220     first = false;
221     }
222     if (!coeff.is_equal(_num1()) &&
223     !coeff.is_equal(_num_1())) {
224     if (coeff.is_rational()) {
225     if (coeff.is_negative())
226     os << -coeff;
227     else
228     os << coeff;
229     } else {
230     if (coeff.csgn()==-1)
231     (-coeff).print(os, precedence);
232     else
233     coeff.print(os, precedence);
234     }
235     os << '*';
236     }
237     os << cit->rest;
238 kreckel 1.9 }
239     if (precedence<=upper_precedence) os << ")";
240     }
241    
242     void add::printraw(ostream & os) const
243     {
244     debugmsg("add printraw",LOGLEVEL_PRINT);
245    
246     os << "+(";
247     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
248     os << "(";
249     (*it).rest.bp->printraw(os);
250     os << ",";
251     (*it).coeff.bp->printraw(os);
252     os << "),";
253     }
254     os << ",hash=" << hashvalue << ",flags=" << flags;
255     os << ")";
256     }
257    
258     void add::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
259     {
260     debugmsg("add print csrc", LOGLEVEL_PRINT);
261     if (precedence <= upper_precedence)
262     os << "(";
263    
264     // Print arguments, separated by "+"
265     epvector::const_iterator it = seq.begin();
266     epvector::const_iterator itend = seq.end();
267     while (it != itend) {
268    
269     // If the coefficient is -1, it is replaced by a single minus sign
270 kreckel 1.10 if (it->coeff.compare(_num1()) == 0) {
271 kreckel 1.9 it->rest.bp->printcsrc(os, type, precedence);
272 kreckel 1.10 } else if (it->coeff.compare(_num_1()) == 0) {
273 kreckel 1.9 os << "-";
274     it->rest.bp->printcsrc(os, type, precedence);
275 kreckel 1.10 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
276 kreckel 1.9 it->rest.bp->printcsrc(os, type, precedence);
277     os << "/";
278     ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
279 kreckel 1.10 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
280 kreckel 1.9 os << "-";
281     it->rest.bp->printcsrc(os, type, precedence);
282     os << "/";
283     ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
284     } else {
285     it->coeff.bp->printcsrc(os, type, precedence);
286     os << "*";
287     it->rest.bp->printcsrc(os, type, precedence);
288     }
289    
290     // Separator is "+", except if the following expression would have a leading minus sign
291     it++;
292 kreckel 1.10 if (it != itend && !(it->coeff.compare(_num0()) < 0 || (it->coeff.compare(_num1()) == 0 && is_ex_exactly_of_type(it->rest, numeric) && it->rest.compare(_num0()) < 0)))
293 kreckel 1.9 os << "+";
294     }
295    
296 kreckel 1.10 if (!overall_coeff.is_equal(_ex0())) {
297 kreckel 1.9 if (overall_coeff.info(info_flags::positive)) os << '+';
298     overall_coeff.bp->printcsrc(os,type,precedence);
299     }
300    
301     if (precedence <= upper_precedence)
302     os << ")";
303     }
304    
305 cbauer 1.1 bool add::info(unsigned inf) const
306     {
307     // TODO: optimize
308 kreckel 1.9 if (inf==info_flags::polynomial ||
309     inf==info_flags::integer_polynomial ||
310     inf==info_flags::cinteger_polynomial ||
311     inf==info_flags::rational_polynomial ||
312     inf==info_flags::crational_polynomial ||
313     inf==info_flags::rational_function) {
314 cbauer 1.1 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
315     if (!(recombine_pair_to_ex(*it).info(inf)))
316     return false;
317     }
318 kreckel 1.9 return overall_coeff.info(inf);
319 cbauer 1.1 } else {
320     return expairseq::info(inf);
321     }
322     }
323    
324     int add::degree(symbol const & s) const
325     {
326     int deg=INT_MIN;
327 kreckel 1.10 if (!overall_coeff.is_equal(_ex0())) {
328 cbauer 1.1 deg=0;
329     }
330     int cur_deg;
331     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
332     cur_deg=(*cit).rest.degree(s);
333     if (cur_deg>deg) deg=cur_deg;
334     }
335     return deg;
336     }
337    
338     int add::ldegree(symbol const & s) const
339     {
340     int deg=INT_MAX;
341 kreckel 1.10 if (!overall_coeff.is_equal(_ex0())) {
342 cbauer 1.1 deg=0;
343     }
344     int cur_deg;
345     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
346     cur_deg=(*cit).rest.ldegree(s);
347     if (cur_deg<deg) deg=cur_deg;
348     }
349     return deg;
350     }
351    
352     ex add::coeff(symbol const & s, int const n) const
353     {
354     epvector coeffseq;
355     coeffseq.reserve(seq.size());
356    
357     epvector::const_iterator it=seq.begin();
358     while (it!=seq.end()) {
359     coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
360     (*it).coeff));
361     ++it;
362     }
363     if (n==0) {
364     return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
365     }
366     return (new add(coeffseq))->setflag(status_flags::dynallocated);
367     }
368    
369     ex add::eval(int level) const
370     {
371     // simplifications: +(;c) -> c
372     // +(x;1) -> x
373    
374     debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
375    
376     epvector * evaled_seqp=evalchildren(level);
377     if (evaled_seqp!=0) {
378     // do more evaluation later
379     return (new add(evaled_seqp,overall_coeff))->
380     setflag(status_flags::dynallocated);
381     }
382    
383 cbauer 1.6 #ifdef DO_GINAC_ASSERT
384 cbauer 1.1 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
385 cbauer 1.6 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
386 cbauer 1.1 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
387     dbgprint();
388     }
389 cbauer 1.6 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
390 cbauer 1.1 }
391 cbauer 1.6 #endif // def DO_GINAC_ASSERT
392 cbauer 1.1
393     if (flags & status_flags::evaluated) {
394 cbauer 1.6 GINAC_ASSERT(seq.size()>0);
395 kreckel 1.10 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex0()));
396 cbauer 1.1 return *this;
397     }
398    
399     int seq_size=seq.size();
400     if (seq_size==0) {
401     // +(;c) -> c
402     return overall_coeff;
403 kreckel 1.10 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex0())) {
404 cbauer 1.1 // +(x;0) -> x
405     return recombine_pair_to_ex(*(seq.begin()));
406     }
407     return this->hold();
408     }
409    
410     exvector add::get_indices(void) const
411     {
412 cbauer 1.5 // FIXME: all terms in the sum should have the same indices (compatible
413     // tensors) however this is not checked, since there is no function yet
414     // which compares indices (idxvector can be unsorted)
415 cbauer 1.1 if (seq.size()==0) {
416     return exvector();
417     }
418     return (seq.begin())->rest.get_indices();
419     }
420    
421     ex add::simplify_ncmul(exvector const & v) const
422     {
423     if (seq.size()==0) {
424     return expairseq::simplify_ncmul(v);
425     }
426     return (*seq.begin()).rest.simplify_ncmul(v);
427     }
428    
429     // protected
430    
431     int add::compare_same_type(basic const & other) const
432     {
433     return expairseq::compare_same_type(other);
434     }
435    
436     bool add::is_equal_same_type(basic const & other) const
437     {
438     return expairseq::is_equal_same_type(other);
439     }
440    
441     unsigned add::return_type(void) const
442     {
443     if (seq.size()==0) {
444     return return_types::commutative;
445     }
446     return (*seq.begin()).rest.return_type();
447     }
448    
449     unsigned add::return_type_tinfo(void) const
450     {
451     if (seq.size()==0) {
452     return tinfo_key;
453     }
454     return (*seq.begin()).rest.return_type_tinfo();
455     }
456    
457     ex add::thisexpairseq(epvector const & v, ex const & oc) const
458     {
459     return (new add(v,oc))->setflag(status_flags::dynallocated);
460     }
461    
462     ex add::thisexpairseq(epvector * vp, ex const & oc) const
463     {
464     return (new add(vp,oc))->setflag(status_flags::dynallocated);
465     }
466    
467     expair add::split_ex_to_pair(ex const & e) const
468     {
469     if (is_ex_exactly_of_type(e,mul)) {
470     mul const & mulref=ex_to_mul(e);
471     ex numfactor=mulref.overall_coeff;
472     // mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
473     mul * mulcopyp=new mul(mulref);
474 kreckel 1.10 mulcopyp->overall_coeff=_ex1();
475 cbauer 1.1 mulcopyp->clearflag(status_flags::evaluated);
476     mulcopyp->clearflag(status_flags::hash_calculated);
477     return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
478     }
479 kreckel 1.10 return expair(e,_ex1());
480 cbauer 1.1 }
481    
482     expair add::combine_ex_with_coeff_to_pair(ex const & e,
483     ex const & c) const
484     {
485 cbauer 1.6 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
486 cbauer 1.1 if (is_ex_exactly_of_type(e,mul)) {
487     mul const & mulref=ex_to_mul(e);
488     ex numfactor=mulref.overall_coeff;
489     //mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
490     mul * mulcopyp=new mul(mulref);
491 kreckel 1.10 mulcopyp->overall_coeff=_ex1();
492 cbauer 1.1 mulcopyp->clearflag(status_flags::evaluated);
493     mulcopyp->clearflag(status_flags::hash_calculated);
494 kreckel 1.10 if (are_ex_trivially_equal(c,_ex1())) {
495 cbauer 1.1 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
496 kreckel 1.10 } else if (are_ex_trivially_equal(numfactor,_ex1())) {
497 cbauer 1.1 return expair(mulcopyp->setflag(status_flags::dynallocated),c);
498     }
499     return expair(mulcopyp->setflag(status_flags::dynallocated),
500     ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
501     } else if (is_ex_exactly_of_type(e,numeric)) {
502 kreckel 1.10 if (are_ex_trivially_equal(c,_ex1())) {
503     return expair(e,_ex1());
504 cbauer 1.1 }
505 kreckel 1.10 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)),_ex1());
506 cbauer 1.1 }
507     return expair(e,c);
508     }
509    
510     expair add::combine_pair_with_coeff_to_pair(expair const & p,
511     ex const & c) const
512     {
513 cbauer 1.6 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
514     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
515 cbauer 1.1
516     if (is_ex_exactly_of_type(p.rest,numeric)) {
517 kreckel 1.10 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
518     return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
519 cbauer 1.1 }
520    
521     return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
522     }
523    
524     ex add::recombine_pair_to_ex(expair const & p) const
525     {
526 kreckel 1.10 //if (p.coeff.compare(_ex1())==0) {
527     //if (are_ex_trivially_equal(p.coeff,_ex1())) {
528     if (ex_to_numeric(p.coeff).is_equal(_num1())) {
529 cbauer 1.1 return p.rest;
530     } else {
531     return p.rest*p.coeff;
532     }
533     }
534    
535     ex add::expand(unsigned options) const
536     {
537     epvector * vp=expandchildren(options);
538     if (vp==0) {
539     return *this;
540     }
541     return (new add(vp,overall_coeff))->setflag(status_flags::expanded |
542     status_flags::dynallocated );
543     }
544    
545     //////////
546     // new virtual functions which can be overridden by derived classes
547     //////////
548    
549     // none
550    
551     //////////
552     // non-virtual functions in this class
553     //////////
554    
555     // none
556    
557     //////////
558     // static member variables
559     //////////
560    
561     // protected
562    
563     unsigned add::precedence=40;
564    
565     //////////
566     // global constants
567     //////////
568    
569     const add some_add;
570     type_info const & typeid_add=typeid(some_add);
571    
572 frink 1.7 #ifndef NO_GINAC_NAMESPACE
573 cbauer 1.5 } // namespace GiNaC
574 frink 1.7 #endif // ndef NO_GINAC_NAMESPACE

Christian Bauer">Christian Bauer
ViewVC Help
Powered by ViewVC 1.1.15