3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2000 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
34 #ifndef NO_GINAC_NAMESPACE
36 #endif // ndef NO_GINAC_NAMESPACE
39 // default constructor, destructor, copy constructor assignment operator and helpers
46 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
47 tinfo_key = TINFO_ncmul;
52 debugmsg("ncmul destructor",LOGLEVEL_DESTRUCT);
56 ncmul::ncmul(ncmul const & other)
58 debugmsg("ncmul copy constructor",LOGLEVEL_CONSTRUCT);
62 ncmul const & ncmul::operator=(ncmul const & other)
64 debugmsg("ncmul operator=",LOGLEVEL_ASSIGNMENT);
74 void ncmul::copy(ncmul const & other)
79 void ncmul::destroy(bool call_parent)
81 if (call_parent) exprseq::destroy(call_parent);
90 ncmul::ncmul(ex const & lh, ex const & rh) :
93 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_ncmul;
97 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3) :
100 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
101 tinfo_key = TINFO_ncmul;
104 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
105 ex const & f4) : exprseq(f1,f2,f3,f4)
107 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
108 tinfo_key = TINFO_ncmul;
111 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
112 ex const & f4, ex const & f5) : exprseq(f1,f2,f3,f4,f5)
114 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
115 tinfo_key = TINFO_ncmul;
118 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
119 ex const & f4, ex const & f5, ex const & f6) :
120 exprseq(f1,f2,f3,f4,f5,f6)
122 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
123 tinfo_key = TINFO_ncmul;
126 ncmul::ncmul(exvector const & v, bool discardable) : exprseq(v,discardable)
128 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
129 tinfo_key = TINFO_ncmul;
132 ncmul::ncmul(exvector * vp) : exprseq(vp)
134 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
135 tinfo_key = TINFO_ncmul;
139 // functions overriding virtual functions from bases classes
144 basic * ncmul::duplicate() const
146 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
147 return new ncmul(*this);
150 void ncmul::print(ostream & os, unsigned upper_precedence) const
152 debugmsg("ncmul print",LOGLEVEL_PRINT);
153 printseq(os,'(','%',')',precedence,upper_precedence);
156 void ncmul::printraw(ostream & os) const
158 debugmsg("ncmul printraw",LOGLEVEL_PRINT);
161 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
162 (*it).bp->printraw(os);
165 os << ",hash=" << hashvalue << ",flags=" << flags;
169 void ncmul::printcsrc(ostream & os, unsigned upper_precedence) const
171 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
172 exvector::const_iterator it;
173 exvector::const_iterator itend = seq.end()-1;
175 for (it=seq.begin(); it!=itend; ++it) {
176 (*it).bp->printcsrc(os,precedence);
179 (*it).bp->printcsrc(os,precedence);
183 bool ncmul::info(unsigned inf) const
185 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
188 typedef vector<int> intvector;
190 ex ncmul::expand(unsigned options) const
192 exvector sub_expanded_seq;
193 intvector positions_of_adds;
194 intvector number_of_add_operands;
196 exvector expanded_seq=expandchildren(options);
198 positions_of_adds.resize(expanded_seq.size());
199 number_of_add_operands.resize(expanded_seq.size());
201 int number_of_adds=0;
202 int number_of_expanded_terms=1;
204 unsigned current_position=0;
205 exvector::const_iterator last=expanded_seq.end();
206 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
207 if (is_ex_exactly_of_type((*cit),add)) {
208 positions_of_adds[number_of_adds]=current_position;
209 add const & expanded_addref=ex_to_add(*cit);
210 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
211 number_of_expanded_terms *= expanded_addref.seq.size();
217 if (number_of_adds==0) {
218 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
219 status_flags::expanded);
223 distrseq.reserve(number_of_expanded_terms);
226 k.resize(number_of_adds);
229 for (l=0; l<number_of_adds; l++) {
236 for (l=0; l<number_of_adds; l++) {
237 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
238 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
239 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
241 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
242 status_flags::expanded));
246 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
253 return (new add(distrseq))->setflag(status_flags::dynallocated |
254 status_flags::expanded);
257 int ncmul::degree(symbol const & s) const
260 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
261 deg_sum+=(*cit).degree(s);
266 int ncmul::ldegree(symbol const & s) const
269 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
270 deg_sum+=(*cit).ldegree(s);
275 ex ncmul::coeff(symbol const & s, int const n) const
278 coeffseq.reserve(seq.size());
281 // product of individual coeffs
282 // if a non-zero power of s is found, the resulting product will be 0
283 exvector::const_iterator it=seq.begin();
284 while (it!=seq.end()) {
285 coeffseq.push_back((*it).coeff(s,n));
288 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
291 exvector::const_iterator it=seq.begin();
293 while (it!=seq.end()) {
294 ex c=(*it).coeff(s,n);
296 coeffseq.push_back(c);
299 coeffseq.push_back(*it);
304 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
309 unsigned ncmul::count_factors(ex const & e) const
311 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
312 (is_ex_exactly_of_type(e,ncmul))) {
314 for (unsigned i=0; i<e.nops(); i++)
315 factors += count_factors(e.op(i));
322 void ncmul::append_factors(exvector & v, ex const & e) const
324 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
325 (is_ex_exactly_of_type(e,ncmul))) {
326 for (unsigned i=0; i<e.nops(); i++)
327 append_factors(v,e.op(i));
334 typedef vector<unsigned> unsignedvector;
335 typedef vector<exvector> exvectorvector;
337 ex ncmul::eval(int level) const
339 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
340 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
343 // ncmul(...,c1,...,c2,...) ->
344 // *(c1,c2,ncmul(...)) (pull out commutative elements)
345 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
346 // (collect elements of same type)
347 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
348 // the following rule would be nice, but produces a recursion,
349 // which must be trapped by introducing a flag that the sub-ncmuls()
350 // are already evaluated (maybe later...)
351 // ncmul(x1,x2,...,X,y1,y2,...) ->
352 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
353 // (X noncommutative_composite)
355 if ((level==1)&&(flags & status_flags::evaluated)) {
359 exvector evaledseq=evalchildren(level);
361 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
362 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
364 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
365 factors += count_factors(*cit);
369 assocseq.reserve(factors);
370 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
371 append_factors(assocseq,*cit);
375 if (assocseq.size()==1) return *(seq.begin());
378 if (assocseq.size()==0) return _ex1();
380 // determine return types
381 unsignedvector rettypes;
382 rettypes.reserve(assocseq.size());
384 unsigned count_commutative=0;
385 unsigned count_noncommutative=0;
386 unsigned count_noncommutative_composite=0;
387 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
388 switch (rettypes[i]=(*cit).return_type()) {
389 case return_types::commutative:
392 case return_types::noncommutative:
393 count_noncommutative++;
395 case return_types::noncommutative_composite:
396 count_noncommutative_composite++;
399 throw(std::logic_error("ncmul::eval(): invalid return type"));
403 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
405 // ncmul(...,c1,...,c2,...) ->
406 // *(c1,c2,ncmul(...)) (pull out commutative elements)
407 if (count_commutative!=0) {
408 exvector commutativeseq;
409 commutativeseq.reserve(count_commutative+1);
410 exvector noncommutativeseq;
411 noncommutativeseq.reserve(assocseq.size()-count_commutative);
412 for (i=0; i<assocseq.size(); ++i) {
413 if (rettypes[i]==return_types::commutative) {
414 commutativeseq.push_back(assocseq[i]);
416 noncommutativeseq.push_back(assocseq[i]);
419 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->
420 setflag(status_flags::dynallocated));
421 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
424 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
425 // (collect elements of same type)
427 if (count_noncommutative_composite==0) {
428 // there are neither commutative nor noncommutative_composite
429 // elements in assocseq
430 GINAC_ASSERT(count_commutative==0);
433 unsignedvector rttinfos;
434 evv.reserve(assocseq.size());
435 rttinfos.reserve(assocseq.size());
437 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
438 unsigned ti=(*cit).return_type_tinfo();
439 // search type in vector of known types
440 for (i=0; i<rttinfos.size(); ++i) {
441 if (ti==rttinfos[i]) {
442 evv[i].push_back(*cit);
446 if (i>=rttinfos.size()) {
448 rttinfos.push_back(ti);
449 evv.push_back(exvector());
450 (*(evv.end()-1)).reserve(assocseq.size());
451 (*(evv.end()-1)).push_back(*cit);
455 #ifdef DO_GINAC_ASSERT
456 GINAC_ASSERT(evv.size()==rttinfos.size());
457 GINAC_ASSERT(evv.size()>0);
459 for (i=0; i<evv.size(); ++i) {
462 GINAC_ASSERT(s==assocseq.size());
463 #endif // def DO_GINAC_ASSERT
465 // if all elements are of same type, simplify the string
467 return evv[0][0].simplify_ncmul(evv[0]);
471 splitseq.reserve(evv.size());
472 for (i=0; i<evv.size(); ++i) {
473 splitseq.push_back((new ncmul(evv[i]))->
474 setflag(status_flags::dynallocated));
477 return (new mul(splitseq))->setflag(status_flags::dynallocated);
480 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
481 status_flags::evaluated);
484 exvector ncmul::get_indices(void) const
486 // return union of indices of factors
488 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
489 exvector subiv=(*cit).get_indices();
490 iv.reserve(iv.size()+subiv.size());
491 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
498 ex ncmul::subs(lst const & ls, lst const & lr) const
500 return ncmul(subschildren(ls, lr));
503 ex ncmul::thisexprseq(exvector const & v) const
505 return (new ncmul(v))->setflag(status_flags::dynallocated);
508 ex ncmul::thisexprseq(exvector * vp) const
510 return (new ncmul(vp))->setflag(status_flags::dynallocated);
515 int ncmul::compare_same_type(basic const & other) const
517 return exprseq::compare_same_type(other);
520 unsigned ncmul::return_type(void) const
523 // ncmul without factors: should not happen, but commutes
524 return return_types::commutative;
527 bool all_commutative=1;
529 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
531 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
532 rt=(*cit).return_type();
533 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
534 if ((rt==return_types::noncommutative)&&(all_commutative)) {
535 // first nc element found, remember position
536 cit_noncommutative_element=cit;
539 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
540 // another nc element found, compare type_infos
541 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
542 // diffent types -> mul is ncc
543 return return_types::noncommutative_composite;
547 // all factors checked
548 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
549 return all_commutative ? return_types::commutative : return_types::noncommutative;
552 unsigned ncmul::return_type_tinfo(void) const
555 // mul without factors: should not happen
558 // return type_info of first noncommutative element
559 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
560 if ((*cit).return_type()==return_types::noncommutative) {
561 return (*cit).return_type_tinfo();
564 // no noncommutative element found, should not happen
569 // new virtual functions which can be overridden by derived classes
575 // non-virtual functions in this class
578 exvector ncmul::expandchildren(unsigned options) const
581 s.reserve(seq.size());
583 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
584 s.push_back((*it).expand(options));
589 exvector const & ncmul::get_factors(void) const
595 // static member variables
600 unsigned ncmul::precedence=50;
607 const ncmul some_ncmul;
608 type_info const & typeid_ncmul=typeid(some_ncmul);
614 ex nonsimplified_ncmul(exvector const & v)
616 return (new ncmul(v))->setflag(status_flags::dynallocated);
619 ex simplified_ncmul(exvector const & v)
623 } else if (v.size()==1) {
626 return (new ncmul(v))->setflag(status_flags::dynallocated |
627 status_flags::evaluated);
630 #ifndef NO_GINAC_NAMESPACE
632 #endif // ndef NO_GINAC_NAMESPACE