3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2001 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 void ncmul::copy(const ncmul & other)
57 inherited::copy(other);
60 void ncmul::destroy(bool call_parent)
62 if (call_parent) inherited::destroy(call_parent);
71 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
73 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
74 tinfo_key = TINFO_ncmul;
77 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
79 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
80 tinfo_key = TINFO_ncmul;
83 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
84 const ex & f4) : inherited(f1,f2,f3,f4)
86 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
87 tinfo_key = TINFO_ncmul;
90 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
91 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
93 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_ncmul;
97 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
98 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
100 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
101 tinfo_key = TINFO_ncmul;
104 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
106 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
107 tinfo_key = TINFO_ncmul;
110 ncmul::ncmul(exvector * vp) : inherited(vp)
112 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
113 tinfo_key = TINFO_ncmul;
120 /** Construct object from archive_node. */
121 ncmul::ncmul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
123 debugmsg("ncmul constructor from archive_node", LOGLEVEL_CONSTRUCT);
126 /** Unarchive the object. */
127 ex ncmul::unarchive(const archive_node &n, const lst &sym_lst)
129 return (new ncmul(n, sym_lst))->setflag(status_flags::dynallocated);
132 /** Archive the object. */
133 void ncmul::archive(archive_node &n) const
135 inherited::archive(n);
140 // functions overriding virtual functions from bases classes
145 void ncmul::print(std::ostream & os, unsigned upper_precedence) const
147 debugmsg("ncmul print",LOGLEVEL_PRINT);
148 printseq(os,'(','%',')',precedence,upper_precedence);
151 void ncmul::printraw(std::ostream & os) const
153 debugmsg("ncmul printraw",LOGLEVEL_PRINT);
155 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
156 (*it).bp->printraw(os);
159 os << ",hash=" << hashvalue << ",flags=" << flags;
163 void ncmul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
165 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
166 exvector::const_iterator it;
167 exvector::const_iterator itend = seq.end()-1;
169 for (it=seq.begin(); it!=itend; ++it) {
170 (*it).bp->printcsrc(os,precedence);
173 (*it).bp->printcsrc(os,precedence);
177 bool ncmul::info(unsigned inf) const
179 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
182 typedef std::vector<int> intvector;
184 ex ncmul::expand(unsigned options) const
186 exvector sub_expanded_seq;
187 intvector positions_of_adds;
188 intvector number_of_add_operands;
190 exvector expanded_seq=expandchildren(options);
192 positions_of_adds.resize(expanded_seq.size());
193 number_of_add_operands.resize(expanded_seq.size());
195 int number_of_adds=0;
196 int number_of_expanded_terms=1;
198 unsigned current_position=0;
199 exvector::const_iterator last=expanded_seq.end();
200 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
201 if (is_ex_exactly_of_type((*cit),add)) {
202 positions_of_adds[number_of_adds]=current_position;
203 const add & expanded_addref=ex_to_add(*cit);
204 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
205 number_of_expanded_terms *= expanded_addref.seq.size();
211 if (number_of_adds==0) {
212 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
213 status_flags::expanded);
217 distrseq.reserve(number_of_expanded_terms);
220 k.resize(number_of_adds);
223 for (l=0; l<number_of_adds; l++) {
230 for (l=0; l<number_of_adds; l++) {
231 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
232 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
233 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
235 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
236 status_flags::expanded));
240 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
247 return (new add(distrseq))->setflag(status_flags::dynallocated |
248 status_flags::expanded);
251 int ncmul::degree(const symbol & s) const
254 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
255 deg_sum+=(*cit).degree(s);
260 int ncmul::ldegree(const symbol & s) const
263 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
264 deg_sum+=(*cit).ldegree(s);
269 ex ncmul::coeff(const symbol & s, int n) const
272 coeffseq.reserve(seq.size());
275 // product of individual coeffs
276 // if a non-zero power of s is found, the resulting product will be 0
277 exvector::const_iterator it=seq.begin();
278 while (it!=seq.end()) {
279 coeffseq.push_back((*it).coeff(s,n));
282 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
285 exvector::const_iterator it=seq.begin();
287 while (it!=seq.end()) {
288 ex c=(*it).coeff(s,n);
290 coeffseq.push_back(c);
293 coeffseq.push_back(*it);
298 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
303 unsigned ncmul::count_factors(const ex & e) const
305 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
306 (is_ex_exactly_of_type(e,ncmul))) {
308 for (unsigned i=0; i<e.nops(); i++)
309 factors += count_factors(e.op(i));
316 void ncmul::append_factors(exvector & v, const ex & e) const
318 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
319 (is_ex_exactly_of_type(e,ncmul))) {
320 for (unsigned i=0; i<e.nops(); i++)
321 append_factors(v,e.op(i));
328 typedef std::vector<unsigned> unsignedvector;
329 typedef std::vector<exvector> exvectorvector;
331 ex ncmul::eval(int level) const
333 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
334 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
337 // ncmul(...,c1,...,c2,...)
338 // *(c1,c2,ncmul(...)) (pull out commutative elements)
339 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
340 // (collect elements of same type)
341 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
342 // the following rule would be nice, but produces a recursion,
343 // which must be trapped by introducing a flag that the sub-ncmuls()
344 // are already evaluated (maybe later...)
345 // ncmul(x1,x2,...,X,y1,y2,...) ->
346 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
347 // (X noncommutative_composite)
349 if ((level==1)&&(flags & status_flags::evaluated)) {
353 exvector evaledseq=evalchildren(level);
355 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
356 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
358 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
359 factors += count_factors(*cit);
363 assocseq.reserve(factors);
364 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
365 append_factors(assocseq,*cit);
369 if (assocseq.size()==1) return *(seq.begin());
372 if (assocseq.size()==0) return _ex1();
374 // determine return types
375 unsignedvector rettypes;
376 rettypes.reserve(assocseq.size());
378 unsigned count_commutative=0;
379 unsigned count_noncommutative=0;
380 unsigned count_noncommutative_composite=0;
381 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
382 switch (rettypes[i]=(*cit).return_type()) {
383 case return_types::commutative:
386 case return_types::noncommutative:
387 count_noncommutative++;
389 case return_types::noncommutative_composite:
390 count_noncommutative_composite++;
393 throw(std::logic_error("ncmul::eval(): invalid return type"));
397 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
399 // ncmul(...,c1,...,c2,...) ->
400 // *(c1,c2,ncmul(...)) (pull out commutative elements)
401 if (count_commutative!=0) {
402 exvector commutativeseq;
403 commutativeseq.reserve(count_commutative+1);
404 exvector noncommutativeseq;
405 noncommutativeseq.reserve(assocseq.size()-count_commutative);
406 for (i=0; i<assocseq.size(); ++i) {
407 if (rettypes[i]==return_types::commutative) {
408 commutativeseq.push_back(assocseq[i]);
410 noncommutativeseq.push_back(assocseq[i]);
413 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
414 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
417 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
418 // (collect elements of same type)
420 if (count_noncommutative_composite==0) {
421 // there are neither commutative nor noncommutative_composite
422 // elements in assocseq
423 GINAC_ASSERT(count_commutative==0);
426 unsignedvector rttinfos;
427 evv.reserve(assocseq.size());
428 rttinfos.reserve(assocseq.size());
430 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
431 unsigned ti=(*cit).return_type_tinfo();
432 // search type in vector of known types
433 for (i=0; i<rttinfos.size(); ++i) {
434 if (ti==rttinfos[i]) {
435 evv[i].push_back(*cit);
439 if (i>=rttinfos.size()) {
441 rttinfos.push_back(ti);
442 evv.push_back(exvector());
443 (*(evv.end()-1)).reserve(assocseq.size());
444 (*(evv.end()-1)).push_back(*cit);
448 #ifdef DO_GINAC_ASSERT
449 GINAC_ASSERT(evv.size()==rttinfos.size());
450 GINAC_ASSERT(evv.size()>0);
452 for (i=0; i<evv.size(); ++i) {
455 GINAC_ASSERT(s==assocseq.size());
456 #endif // def DO_GINAC_ASSERT
458 // if all elements are of same type, simplify the string
460 return evv[0][0].simplify_ncmul(evv[0]);
464 splitseq.reserve(evv.size());
465 for (i=0; i<evv.size(); ++i) {
466 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
469 return (new mul(splitseq))->setflag(status_flags::dynallocated);
472 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
473 status_flags::evaluated);
476 exvector ncmul::get_indices(void) const
478 // return union of indices of factors
480 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
481 exvector subiv=(*cit).get_indices();
482 iv.reserve(iv.size()+subiv.size());
483 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
490 ex ncmul::subs(const lst & ls, const lst & lr) const
492 return ncmul(subschildren(ls, lr));
495 ex ncmul::thisexprseq(const exvector & v) const
497 return (new ncmul(v))->setflag(status_flags::dynallocated);
500 ex ncmul::thisexprseq(exvector * vp) const
502 return (new ncmul(vp))->setflag(status_flags::dynallocated);
507 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
509 ex ncmul::derivative(const symbol & s) const
514 int ncmul::compare_same_type(const basic & other) const
516 return inherited::compare_same_type(other);
519 unsigned ncmul::return_type(void) const
522 // ncmul without factors: should not happen, but commutes
523 return return_types::commutative;
526 bool all_commutative=1;
528 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
530 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
531 rt=(*cit).return_type();
532 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
533 if ((rt==return_types::noncommutative)&&(all_commutative)) {
534 // first nc element found, remember position
535 cit_noncommutative_element=cit;
538 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
539 // another nc element found, compare type_infos
540 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
541 // diffent types -> mul is ncc
542 return return_types::noncommutative_composite;
546 // all factors checked
547 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
548 return all_commutative ? return_types::commutative : return_types::noncommutative;
551 unsigned ncmul::return_type_tinfo(void) const
554 // mul without factors: should not happen
557 // return type_info of first noncommutative element
558 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
559 if ((*cit).return_type()==return_types::noncommutative) {
560 return (*cit).return_type_tinfo();
563 // no noncommutative element found, should not happen
568 // new virtual functions which can be overridden by derived classes
574 // non-virtual functions in this class
577 exvector ncmul::expandchildren(unsigned options) const
580 s.reserve(seq.size());
582 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
583 s.push_back((*it).expand(options));
588 const exvector & ncmul::get_factors(void) const
594 // static member variables
599 unsigned ncmul::precedence=50;
605 ex nonsimplified_ncmul(const exvector & v)
607 return (new ncmul(v))->setflag(status_flags::dynallocated);
610 ex simplified_ncmul(const exvector & v)
614 } else if (v.size()==1) {
617 return (new ncmul(v))->setflag(status_flags::dynallocated |
618 status_flags::evaluated);
621 #ifndef NO_NAMESPACE_GINAC
623 #endif // ndef NO_NAMESPACE_GINAC