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
35 #ifndef NO_NAMESPACE_GINAC
37 #endif // ndef NO_NAMESPACE_GINAC
39 GINAC_IMPLEMENT_REGISTERED_CLASS(ncmul, exprseq)
42 // default constructor, destructor, copy constructor assignment operator and helpers
49 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
50 tinfo_key = TINFO_ncmul;
55 debugmsg("ncmul destructor",LOGLEVEL_DESTRUCT);
59 ncmul::ncmul(const ncmul & other)
61 debugmsg("ncmul copy constructor",LOGLEVEL_CONSTRUCT);
65 const ncmul & ncmul::operator=(const ncmul & other)
67 debugmsg("ncmul operator=",LOGLEVEL_ASSIGNMENT);
77 void ncmul::copy(const ncmul & other)
79 inherited::copy(other);
82 void ncmul::destroy(bool call_parent)
84 if (call_parent) inherited::destroy(call_parent);
93 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
95 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
96 tinfo_key = TINFO_ncmul;
99 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
101 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
102 tinfo_key = TINFO_ncmul;
105 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
106 const ex & f4) : inherited(f1,f2,f3,f4)
108 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
109 tinfo_key = TINFO_ncmul;
112 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
113 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
115 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
116 tinfo_key = TINFO_ncmul;
119 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
120 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
122 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
123 tinfo_key = TINFO_ncmul;
126 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
128 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
129 tinfo_key = TINFO_ncmul;
132 ncmul::ncmul(exvector * vp) : inherited(vp)
134 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
135 tinfo_key = TINFO_ncmul;
142 /** Construct object from archive_node. */
143 ncmul::ncmul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
145 debugmsg("ncmul constructor from archive_node", LOGLEVEL_CONSTRUCT);
148 /** Unarchive the object. */
149 ex ncmul::unarchive(const archive_node &n, const lst &sym_lst)
151 return (new ncmul(n, sym_lst))->setflag(status_flags::dynallocated);
154 /** Archive the object. */
155 void ncmul::archive(archive_node &n) const
157 inherited::archive(n);
162 // functions overriding virtual functions from bases classes
167 basic * ncmul::duplicate() const
169 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
170 return new ncmul(*this);
173 void ncmul::print(std::ostream & os, unsigned upper_precedence) const
175 debugmsg("ncmul print",LOGLEVEL_PRINT);
176 printseq(os,'(','%',')',precedence,upper_precedence);
179 void ncmul::printraw(std::ostream & os) const
181 debugmsg("ncmul printraw",LOGLEVEL_PRINT);
184 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
185 (*it).bp->printraw(os);
188 os << ",hash=" << hashvalue << ",flags=" << flags;
192 void ncmul::printcsrc(std::ostream & os, unsigned upper_precedence) const
194 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
195 exvector::const_iterator it;
196 exvector::const_iterator itend = seq.end()-1;
198 for (it=seq.begin(); it!=itend; ++it) {
199 (*it).bp->printcsrc(os,precedence);
202 (*it).bp->printcsrc(os,precedence);
206 bool ncmul::info(unsigned inf) const
208 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
211 typedef std::vector<int> intvector;
213 ex ncmul::expand(unsigned options) const
215 exvector sub_expanded_seq;
216 intvector positions_of_adds;
217 intvector number_of_add_operands;
219 exvector expanded_seq=expandchildren(options);
221 positions_of_adds.resize(expanded_seq.size());
222 number_of_add_operands.resize(expanded_seq.size());
224 int number_of_adds=0;
225 int number_of_expanded_terms=1;
227 unsigned current_position=0;
228 exvector::const_iterator last=expanded_seq.end();
229 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
230 if (is_ex_exactly_of_type((*cit),add)) {
231 positions_of_adds[number_of_adds]=current_position;
232 const add & expanded_addref=ex_to_add(*cit);
233 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
234 number_of_expanded_terms *= expanded_addref.seq.size();
240 if (number_of_adds==0) {
241 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
242 status_flags::expanded);
246 distrseq.reserve(number_of_expanded_terms);
249 k.resize(number_of_adds);
252 for (l=0; l<number_of_adds; l++) {
259 for (l=0; l<number_of_adds; l++) {
260 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
261 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
262 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
264 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
265 status_flags::expanded));
269 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
276 return (new add(distrseq))->setflag(status_flags::dynallocated |
277 status_flags::expanded);
280 int ncmul::degree(const symbol & s) const
283 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
284 deg_sum+=(*cit).degree(s);
289 int ncmul::ldegree(const symbol & s) const
292 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
293 deg_sum+=(*cit).ldegree(s);
298 ex ncmul::coeff(const symbol & s, int n) const
301 coeffseq.reserve(seq.size());
304 // product of individual coeffs
305 // if a non-zero power of s is found, the resulting product will be 0
306 exvector::const_iterator it=seq.begin();
307 while (it!=seq.end()) {
308 coeffseq.push_back((*it).coeff(s,n));
311 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
314 exvector::const_iterator it=seq.begin();
316 while (it!=seq.end()) {
317 ex c=(*it).coeff(s,n);
319 coeffseq.push_back(c);
322 coeffseq.push_back(*it);
327 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
332 unsigned ncmul::count_factors(const ex & e) const
334 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
335 (is_ex_exactly_of_type(e,ncmul))) {
337 for (unsigned i=0; i<e.nops(); i++)
338 factors += count_factors(e.op(i));
345 void ncmul::append_factors(exvector & v, const ex & e) const
347 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
348 (is_ex_exactly_of_type(e,ncmul))) {
349 for (unsigned i=0; i<e.nops(); i++)
350 append_factors(v,e.op(i));
357 typedef std::vector<unsigned> unsignedvector;
358 typedef std::vector<exvector> exvectorvector;
360 ex ncmul::eval(int level) const
362 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
363 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
366 // ncmul(...,c1,...,c2,...)
367 // *(c1,c2,ncmul(...)) (pull out commutative elements)
368 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
369 // (collect elements of same type)
370 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
371 // the following rule would be nice, but produces a recursion,
372 // which must be trapped by introducing a flag that the sub-ncmuls()
373 // are already evaluated (maybe later...)
374 // ncmul(x1,x2,...,X,y1,y2,...) ->
375 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
376 // (X noncommutative_composite)
378 if ((level==1)&&(flags & status_flags::evaluated)) {
382 exvector evaledseq=evalchildren(level);
384 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
385 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
387 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
388 factors += count_factors(*cit);
392 assocseq.reserve(factors);
393 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
394 append_factors(assocseq,*cit);
398 if (assocseq.size()==1) return *(seq.begin());
401 if (assocseq.size()==0) return _ex1();
403 // determine return types
404 unsignedvector rettypes;
405 rettypes.reserve(assocseq.size());
407 unsigned count_commutative=0;
408 unsigned count_noncommutative=0;
409 unsigned count_noncommutative_composite=0;
410 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
411 switch (rettypes[i]=(*cit).return_type()) {
412 case return_types::commutative:
415 case return_types::noncommutative:
416 count_noncommutative++;
418 case return_types::noncommutative_composite:
419 count_noncommutative_composite++;
422 throw(std::logic_error("ncmul::eval(): invalid return type"));
426 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
428 // ncmul(...,c1,...,c2,...) ->
429 // *(c1,c2,ncmul(...)) (pull out commutative elements)
430 if (count_commutative!=0) {
431 exvector commutativeseq;
432 commutativeseq.reserve(count_commutative+1);
433 exvector noncommutativeseq;
434 noncommutativeseq.reserve(assocseq.size()-count_commutative);
435 for (i=0; i<assocseq.size(); ++i) {
436 if (rettypes[i]==return_types::commutative) {
437 commutativeseq.push_back(assocseq[i]);
439 noncommutativeseq.push_back(assocseq[i]);
442 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
443 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
446 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
447 // (collect elements of same type)
449 if (count_noncommutative_composite==0) {
450 // there are neither commutative nor noncommutative_composite
451 // elements in assocseq
452 GINAC_ASSERT(count_commutative==0);
455 unsignedvector rttinfos;
456 evv.reserve(assocseq.size());
457 rttinfos.reserve(assocseq.size());
459 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
460 unsigned ti=(*cit).return_type_tinfo();
461 // search type in vector of known types
462 for (i=0; i<rttinfos.size(); ++i) {
463 if (ti==rttinfos[i]) {
464 evv[i].push_back(*cit);
468 if (i>=rttinfos.size()) {
470 rttinfos.push_back(ti);
471 evv.push_back(exvector());
472 (*(evv.end()-1)).reserve(assocseq.size());
473 (*(evv.end()-1)).push_back(*cit);
477 #ifdef DO_GINAC_ASSERT
478 GINAC_ASSERT(evv.size()==rttinfos.size());
479 GINAC_ASSERT(evv.size()>0);
481 for (i=0; i<evv.size(); ++i) {
484 GINAC_ASSERT(s==assocseq.size());
485 #endif // def DO_GINAC_ASSERT
487 // if all elements are of same type, simplify the string
489 return evv[0][0].simplify_ncmul(evv[0]);
493 splitseq.reserve(evv.size());
494 for (i=0; i<evv.size(); ++i) {
495 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
498 return (new mul(splitseq))->setflag(status_flags::dynallocated);
501 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
502 status_flags::evaluated);
505 exvector ncmul::get_indices(void) const
507 // return union of indices of factors
509 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
510 exvector subiv=(*cit).get_indices();
511 iv.reserve(iv.size()+subiv.size());
512 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
519 ex ncmul::subs(const lst & ls, const lst & lr) const
521 return ncmul(subschildren(ls, lr));
524 ex ncmul::thisexprseq(const exvector & v) const
526 return (new ncmul(v))->setflag(status_flags::dynallocated);
529 ex ncmul::thisexprseq(exvector * vp) const
531 return (new ncmul(vp))->setflag(status_flags::dynallocated);
536 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
538 ex ncmul::derivative(const symbol & s) const
543 int ncmul::compare_same_type(const basic & other) const
545 return inherited::compare_same_type(other);
548 unsigned ncmul::return_type(void) const
551 // ncmul without factors: should not happen, but commutes
552 return return_types::commutative;
555 bool all_commutative=1;
557 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
559 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
560 rt=(*cit).return_type();
561 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
562 if ((rt==return_types::noncommutative)&&(all_commutative)) {
563 // first nc element found, remember position
564 cit_noncommutative_element=cit;
567 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
568 // another nc element found, compare type_infos
569 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
570 // diffent types -> mul is ncc
571 return return_types::noncommutative_composite;
575 // all factors checked
576 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
577 return all_commutative ? return_types::commutative : return_types::noncommutative;
580 unsigned ncmul::return_type_tinfo(void) const
583 // mul without factors: should not happen
586 // return type_info of first noncommutative element
587 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
588 if ((*cit).return_type()==return_types::noncommutative) {
589 return (*cit).return_type_tinfo();
592 // no noncommutative element found, should not happen
597 // new virtual functions which can be overridden by derived classes
603 // non-virtual functions in this class
606 exvector ncmul::expandchildren(unsigned options) const
609 s.reserve(seq.size());
611 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
612 s.push_back((*it).expand(options));
617 const exvector & ncmul::get_factors(void) const
623 // static member variables
628 unsigned ncmul::precedence=50;
635 const ncmul some_ncmul;
636 const type_info & typeid_ncmul=typeid(some_ncmul);
642 ex nonsimplified_ncmul(const exvector & v)
644 return (new ncmul(v))->setflag(status_flags::dynallocated);
647 ex simplified_ncmul(const exvector & v)
651 } else if (v.size()==1) {
654 return (new ncmul(v))->setflag(status_flags::dynallocated |
655 status_flags::evaluated);
658 #ifndef NO_NAMESPACE_GINAC
660 #endif // ndef NO_NAMESPACE_GINAC