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);
183 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
184 (*it).bp->printraw(os);
187 os << ",hash=" << hashvalue << ",flags=" << flags;
191 void ncmul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
193 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
194 exvector::const_iterator it;
195 exvector::const_iterator itend = seq.end()-1;
197 for (it=seq.begin(); it!=itend; ++it) {
198 (*it).bp->printcsrc(os,precedence);
201 (*it).bp->printcsrc(os,precedence);
205 bool ncmul::info(unsigned inf) const
207 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
210 typedef std::vector<int> intvector;
212 ex ncmul::expand(unsigned options) const
214 exvector sub_expanded_seq;
215 intvector positions_of_adds;
216 intvector number_of_add_operands;
218 exvector expanded_seq=expandchildren(options);
220 positions_of_adds.resize(expanded_seq.size());
221 number_of_add_operands.resize(expanded_seq.size());
223 int number_of_adds=0;
224 int number_of_expanded_terms=1;
226 unsigned current_position=0;
227 exvector::const_iterator last=expanded_seq.end();
228 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
229 if (is_ex_exactly_of_type((*cit),add)) {
230 positions_of_adds[number_of_adds]=current_position;
231 const add & expanded_addref=ex_to_add(*cit);
232 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
233 number_of_expanded_terms *= expanded_addref.seq.size();
239 if (number_of_adds==0) {
240 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
241 status_flags::expanded);
245 distrseq.reserve(number_of_expanded_terms);
248 k.resize(number_of_adds);
251 for (l=0; l<number_of_adds; l++) {
258 for (l=0; l<number_of_adds; l++) {
259 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
260 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
261 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
263 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
264 status_flags::expanded));
268 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
275 return (new add(distrseq))->setflag(status_flags::dynallocated |
276 status_flags::expanded);
279 int ncmul::degree(const symbol & s) const
282 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
283 deg_sum+=(*cit).degree(s);
288 int ncmul::ldegree(const symbol & s) const
291 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
292 deg_sum+=(*cit).ldegree(s);
297 ex ncmul::coeff(const symbol & s, int n) const
300 coeffseq.reserve(seq.size());
303 // product of individual coeffs
304 // if a non-zero power of s is found, the resulting product will be 0
305 exvector::const_iterator it=seq.begin();
306 while (it!=seq.end()) {
307 coeffseq.push_back((*it).coeff(s,n));
310 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
313 exvector::const_iterator it=seq.begin();
315 while (it!=seq.end()) {
316 ex c=(*it).coeff(s,n);
318 coeffseq.push_back(c);
321 coeffseq.push_back(*it);
326 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
331 unsigned ncmul::count_factors(const ex & e) const
333 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
334 (is_ex_exactly_of_type(e,ncmul))) {
336 for (unsigned i=0; i<e.nops(); i++)
337 factors += count_factors(e.op(i));
344 void ncmul::append_factors(exvector & v, const ex & e) const
346 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
347 (is_ex_exactly_of_type(e,ncmul))) {
348 for (unsigned i=0; i<e.nops(); i++)
349 append_factors(v,e.op(i));
356 typedef std::vector<unsigned> unsignedvector;
357 typedef std::vector<exvector> exvectorvector;
359 ex ncmul::eval(int level) const
361 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
362 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
365 // ncmul(...,c1,...,c2,...)
366 // *(c1,c2,ncmul(...)) (pull out commutative elements)
367 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
368 // (collect elements of same type)
369 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
370 // the following rule would be nice, but produces a recursion,
371 // which must be trapped by introducing a flag that the sub-ncmuls()
372 // are already evaluated (maybe later...)
373 // ncmul(x1,x2,...,X,y1,y2,...) ->
374 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
375 // (X noncommutative_composite)
377 if ((level==1)&&(flags & status_flags::evaluated)) {
381 exvector evaledseq=evalchildren(level);
383 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
384 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
386 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
387 factors += count_factors(*cit);
391 assocseq.reserve(factors);
392 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
393 append_factors(assocseq,*cit);
397 if (assocseq.size()==1) return *(seq.begin());
400 if (assocseq.size()==0) return _ex1();
402 // determine return types
403 unsignedvector rettypes;
404 rettypes.reserve(assocseq.size());
406 unsigned count_commutative=0;
407 unsigned count_noncommutative=0;
408 unsigned count_noncommutative_composite=0;
409 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
410 switch (rettypes[i]=(*cit).return_type()) {
411 case return_types::commutative:
414 case return_types::noncommutative:
415 count_noncommutative++;
417 case return_types::noncommutative_composite:
418 count_noncommutative_composite++;
421 throw(std::logic_error("ncmul::eval(): invalid return type"));
425 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
427 // ncmul(...,c1,...,c2,...) ->
428 // *(c1,c2,ncmul(...)) (pull out commutative elements)
429 if (count_commutative!=0) {
430 exvector commutativeseq;
431 commutativeseq.reserve(count_commutative+1);
432 exvector noncommutativeseq;
433 noncommutativeseq.reserve(assocseq.size()-count_commutative);
434 for (i=0; i<assocseq.size(); ++i) {
435 if (rettypes[i]==return_types::commutative) {
436 commutativeseq.push_back(assocseq[i]);
438 noncommutativeseq.push_back(assocseq[i]);
441 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
442 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
445 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
446 // (collect elements of same type)
448 if (count_noncommutative_composite==0) {
449 // there are neither commutative nor noncommutative_composite
450 // elements in assocseq
451 GINAC_ASSERT(count_commutative==0);
454 unsignedvector rttinfos;
455 evv.reserve(assocseq.size());
456 rttinfos.reserve(assocseq.size());
458 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
459 unsigned ti=(*cit).return_type_tinfo();
460 // search type in vector of known types
461 for (i=0; i<rttinfos.size(); ++i) {
462 if (ti==rttinfos[i]) {
463 evv[i].push_back(*cit);
467 if (i>=rttinfos.size()) {
469 rttinfos.push_back(ti);
470 evv.push_back(exvector());
471 (*(evv.end()-1)).reserve(assocseq.size());
472 (*(evv.end()-1)).push_back(*cit);
476 #ifdef DO_GINAC_ASSERT
477 GINAC_ASSERT(evv.size()==rttinfos.size());
478 GINAC_ASSERT(evv.size()>0);
480 for (i=0; i<evv.size(); ++i) {
483 GINAC_ASSERT(s==assocseq.size());
484 #endif // def DO_GINAC_ASSERT
486 // if all elements are of same type, simplify the string
488 return evv[0][0].simplify_ncmul(evv[0]);
492 splitseq.reserve(evv.size());
493 for (i=0; i<evv.size(); ++i) {
494 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
497 return (new mul(splitseq))->setflag(status_flags::dynallocated);
500 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
501 status_flags::evaluated);
504 exvector ncmul::get_indices(void) const
506 // return union of indices of factors
508 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
509 exvector subiv=(*cit).get_indices();
510 iv.reserve(iv.size()+subiv.size());
511 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
518 ex ncmul::subs(const lst & ls, const lst & lr) const
520 return ncmul(subschildren(ls, lr));
523 ex ncmul::thisexprseq(const exvector & v) const
525 return (new ncmul(v))->setflag(status_flags::dynallocated);
528 ex ncmul::thisexprseq(exvector * vp) const
530 return (new ncmul(vp))->setflag(status_flags::dynallocated);
535 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
537 ex ncmul::derivative(const symbol & s) const
542 int ncmul::compare_same_type(const basic & other) const
544 return inherited::compare_same_type(other);
547 unsigned ncmul::return_type(void) const
550 // ncmul without factors: should not happen, but commutes
551 return return_types::commutative;
554 bool all_commutative=1;
556 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
558 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
559 rt=(*cit).return_type();
560 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
561 if ((rt==return_types::noncommutative)&&(all_commutative)) {
562 // first nc element found, remember position
563 cit_noncommutative_element=cit;
566 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
567 // another nc element found, compare type_infos
568 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
569 // diffent types -> mul is ncc
570 return return_types::noncommutative_composite;
574 // all factors checked
575 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
576 return all_commutative ? return_types::commutative : return_types::noncommutative;
579 unsigned ncmul::return_type_tinfo(void) const
582 // mul without factors: should not happen
585 // return type_info of first noncommutative element
586 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
587 if ((*cit).return_type()==return_types::noncommutative) {
588 return (*cit).return_type_tinfo();
591 // no noncommutative element found, should not happen
596 // new virtual functions which can be overridden by derived classes
602 // non-virtual functions in this class
605 exvector ncmul::expandchildren(unsigned options) const
608 s.reserve(seq.size());
610 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
611 s.push_back((*it).expand(options));
616 const exvector & ncmul::get_factors(void) const
622 // static member variables
627 unsigned ncmul::precedence=50;
634 const ncmul some_ncmul;
635 const std::type_info & typeid_ncmul = typeid(some_ncmul);
641 ex nonsimplified_ncmul(const exvector & v)
643 return (new ncmul(v))->setflag(status_flags::dynallocated);
646 ex simplified_ncmul(const exvector & v)
650 } else if (v.size()==1) {
653 return (new ncmul(v))->setflag(status_flags::dynallocated |
654 status_flags::evaluated);
657 #ifndef NO_NAMESPACE_GINAC
659 #endif // ndef NO_NAMESPACE_GINAC