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) :
96 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
97 tinfo_key = TINFO_ncmul;
100 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) :
103 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
104 tinfo_key = TINFO_ncmul;
107 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
108 const ex & f4) : inherited(f1,f2,f3,f4)
110 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
111 tinfo_key = TINFO_ncmul;
114 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
115 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
117 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
118 tinfo_key = TINFO_ncmul;
121 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
122 const ex & f4, const ex & f5, const ex & f6) :
123 inherited(f1,f2,f3,f4,f5,f6)
125 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
126 tinfo_key = TINFO_ncmul;
129 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
131 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
132 tinfo_key = TINFO_ncmul;
135 ncmul::ncmul(exvector * vp) : inherited(vp)
137 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
138 tinfo_key = TINFO_ncmul;
145 /** Construct object from archive_node. */
146 ncmul::ncmul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
148 debugmsg("ncmul constructor from archive_node", LOGLEVEL_CONSTRUCT);
151 /** Unarchive the object. */
152 ex ncmul::unarchive(const archive_node &n, const lst &sym_lst)
154 return (new ncmul(n, sym_lst))->setflag(status_flags::dynallocated);
157 /** Archive the object. */
158 void ncmul::archive(archive_node &n) const
160 inherited::archive(n);
165 // functions overriding virtual functions from bases classes
170 basic * ncmul::duplicate() const
172 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
173 return new ncmul(*this);
176 void ncmul::print(std::ostream & os, unsigned upper_precedence) const
178 debugmsg("ncmul print",LOGLEVEL_PRINT);
179 printseq(os,'(','%',')',precedence,upper_precedence);
182 void ncmul::printraw(std::ostream & os) const
184 debugmsg("ncmul printraw",LOGLEVEL_PRINT);
187 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
188 (*it).bp->printraw(os);
191 os << ",hash=" << hashvalue << ",flags=" << flags;
195 void ncmul::printcsrc(std::ostream & os, unsigned upper_precedence) const
197 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
198 exvector::const_iterator it;
199 exvector::const_iterator itend = seq.end()-1;
201 for (it=seq.begin(); it!=itend; ++it) {
202 (*it).bp->printcsrc(os,precedence);
205 (*it).bp->printcsrc(os,precedence);
209 bool ncmul::info(unsigned inf) const
211 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
214 typedef std::vector<int> intvector;
216 ex ncmul::expand(unsigned options) const
218 exvector sub_expanded_seq;
219 intvector positions_of_adds;
220 intvector number_of_add_operands;
222 exvector expanded_seq=expandchildren(options);
224 positions_of_adds.resize(expanded_seq.size());
225 number_of_add_operands.resize(expanded_seq.size());
227 int number_of_adds=0;
228 int number_of_expanded_terms=1;
230 unsigned current_position=0;
231 exvector::const_iterator last=expanded_seq.end();
232 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
233 if (is_ex_exactly_of_type((*cit),add)) {
234 positions_of_adds[number_of_adds]=current_position;
235 const add & expanded_addref=ex_to_add(*cit);
236 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
237 number_of_expanded_terms *= expanded_addref.seq.size();
243 if (number_of_adds==0) {
244 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
245 status_flags::expanded);
249 distrseq.reserve(number_of_expanded_terms);
252 k.resize(number_of_adds);
255 for (l=0; l<number_of_adds; l++) {
262 for (l=0; l<number_of_adds; l++) {
263 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
264 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
265 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
267 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
268 status_flags::expanded));
272 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
279 return (new add(distrseq))->setflag(status_flags::dynallocated |
280 status_flags::expanded);
283 int ncmul::degree(const symbol & s) const
286 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
287 deg_sum+=(*cit).degree(s);
292 int ncmul::ldegree(const symbol & s) const
295 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
296 deg_sum+=(*cit).ldegree(s);
301 ex ncmul::coeff(const symbol & s, int n) const
304 coeffseq.reserve(seq.size());
307 // product of individual coeffs
308 // if a non-zero power of s is found, the resulting product will be 0
309 exvector::const_iterator it=seq.begin();
310 while (it!=seq.end()) {
311 coeffseq.push_back((*it).coeff(s,n));
314 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
317 exvector::const_iterator it=seq.begin();
319 while (it!=seq.end()) {
320 ex c=(*it).coeff(s,n);
322 coeffseq.push_back(c);
325 coeffseq.push_back(*it);
330 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
335 unsigned ncmul::count_factors(const ex & e) const
337 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
338 (is_ex_exactly_of_type(e,ncmul))) {
340 for (unsigned i=0; i<e.nops(); i++)
341 factors += count_factors(e.op(i));
348 void ncmul::append_factors(exvector & v, const ex & e) const
350 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
351 (is_ex_exactly_of_type(e,ncmul))) {
352 for (unsigned i=0; i<e.nops(); i++)
353 append_factors(v,e.op(i));
360 typedef std::vector<unsigned> unsignedvector;
361 typedef std::vector<exvector> exvectorvector;
363 ex ncmul::eval(int level) const
365 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
366 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
369 // ncmul(...,c1,...,c2,...) ->
370 // *(c1,c2,ncmul(...)) (pull out commutative elements)
371 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
372 // (collect elements of same type)
373 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
374 // the following rule would be nice, but produces a recursion,
375 // which must be trapped by introducing a flag that the sub-ncmuls()
376 // are already evaluated (maybe later...)
377 // ncmul(x1,x2,...,X,y1,y2,...) ->
378 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
379 // (X noncommutative_composite)
381 if ((level==1)&&(flags & status_flags::evaluated)) {
385 exvector evaledseq=evalchildren(level);
387 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
388 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
390 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
391 factors += count_factors(*cit);
395 assocseq.reserve(factors);
396 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
397 append_factors(assocseq,*cit);
401 if (assocseq.size()==1) return *(seq.begin());
404 if (assocseq.size()==0) return _ex1();
406 // determine return types
407 unsignedvector rettypes;
408 rettypes.reserve(assocseq.size());
410 unsigned count_commutative=0;
411 unsigned count_noncommutative=0;
412 unsigned count_noncommutative_composite=0;
413 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
414 switch (rettypes[i]=(*cit).return_type()) {
415 case return_types::commutative:
418 case return_types::noncommutative:
419 count_noncommutative++;
421 case return_types::noncommutative_composite:
422 count_noncommutative_composite++;
425 throw(std::logic_error("ncmul::eval(): invalid return type"));
429 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
431 // ncmul(...,c1,...,c2,...) ->
432 // *(c1,c2,ncmul(...)) (pull out commutative elements)
433 if (count_commutative!=0) {
434 exvector commutativeseq;
435 commutativeseq.reserve(count_commutative+1);
436 exvector noncommutativeseq;
437 noncommutativeseq.reserve(assocseq.size()-count_commutative);
438 for (i=0; i<assocseq.size(); ++i) {
439 if (rettypes[i]==return_types::commutative) {
440 commutativeseq.push_back(assocseq[i]);
442 noncommutativeseq.push_back(assocseq[i]);
445 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->
446 setflag(status_flags::dynallocated));
447 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
450 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
451 // (collect elements of same type)
453 if (count_noncommutative_composite==0) {
454 // there are neither commutative nor noncommutative_composite
455 // elements in assocseq
456 GINAC_ASSERT(count_commutative==0);
459 unsignedvector rttinfos;
460 evv.reserve(assocseq.size());
461 rttinfos.reserve(assocseq.size());
463 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
464 unsigned ti=(*cit).return_type_tinfo();
465 // search type in vector of known types
466 for (i=0; i<rttinfos.size(); ++i) {
467 if (ti==rttinfos[i]) {
468 evv[i].push_back(*cit);
472 if (i>=rttinfos.size()) {
474 rttinfos.push_back(ti);
475 evv.push_back(exvector());
476 (*(evv.end()-1)).reserve(assocseq.size());
477 (*(evv.end()-1)).push_back(*cit);
481 #ifdef DO_GINAC_ASSERT
482 GINAC_ASSERT(evv.size()==rttinfos.size());
483 GINAC_ASSERT(evv.size()>0);
485 for (i=0; i<evv.size(); ++i) {
488 GINAC_ASSERT(s==assocseq.size());
489 #endif // def DO_GINAC_ASSERT
491 // if all elements are of same type, simplify the string
493 return evv[0][0].simplify_ncmul(evv[0]);
497 splitseq.reserve(evv.size());
498 for (i=0; i<evv.size(); ++i) {
499 splitseq.push_back((new ncmul(evv[i]))->
500 setflag(status_flags::dynallocated));
503 return (new mul(splitseq))->setflag(status_flags::dynallocated);
506 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
507 status_flags::evaluated);
510 exvector ncmul::get_indices(void) const
512 // return union of indices of factors
514 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
515 exvector subiv=(*cit).get_indices();
516 iv.reserve(iv.size()+subiv.size());
517 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
524 ex ncmul::subs(const lst & ls, const lst & lr) const
526 return ncmul(subschildren(ls, lr));
529 ex ncmul::thisexprseq(const exvector & v) const
531 return (new ncmul(v))->setflag(status_flags::dynallocated);
534 ex ncmul::thisexprseq(exvector * vp) const
536 return (new ncmul(vp))->setflag(status_flags::dynallocated);
541 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
543 ex ncmul::derivative(const symbol & s) const
548 int ncmul::compare_same_type(const basic & other) const
550 return inherited::compare_same_type(other);
553 unsigned ncmul::return_type(void) const
556 // ncmul without factors: should not happen, but commutes
557 return return_types::commutative;
560 bool all_commutative=1;
562 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
564 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
565 rt=(*cit).return_type();
566 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
567 if ((rt==return_types::noncommutative)&&(all_commutative)) {
568 // first nc element found, remember position
569 cit_noncommutative_element=cit;
572 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
573 // another nc element found, compare type_infos
574 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
575 // diffent types -> mul is ncc
576 return return_types::noncommutative_composite;
580 // all factors checked
581 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
582 return all_commutative ? return_types::commutative : return_types::noncommutative;
585 unsigned ncmul::return_type_tinfo(void) const
588 // mul without factors: should not happen
591 // return type_info of first noncommutative element
592 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
593 if ((*cit).return_type()==return_types::noncommutative) {
594 return (*cit).return_type_tinfo();
597 // no noncommutative element found, should not happen
602 // new virtual functions which can be overridden by derived classes
608 // non-virtual functions in this class
611 exvector ncmul::expandchildren(unsigned options) const
614 s.reserve(seq.size());
616 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
617 s.push_back((*it).expand(options));
622 const exvector & ncmul::get_factors(void) const
628 // static member variables
633 unsigned ncmul::precedence=50;
640 const ncmul some_ncmul;
641 const type_info & typeid_ncmul=typeid(some_ncmul);
647 ex nonsimplified_ncmul(const exvector & v)
649 return (new ncmul(v))->setflag(status_flags::dynallocated);
652 ex simplified_ncmul(const exvector & v)
656 } else if (v.size()==1) {
659 return (new ncmul(v))->setflag(status_flags::dynallocated |
660 status_flags::evaluated);
663 #ifndef NO_NAMESPACE_GINAC
665 #endif // ndef NO_NAMESPACE_GINAC