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 basic * ncmul::duplicate() const
147 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
148 return new ncmul(*this);
151 void ncmul::print(std::ostream & os, unsigned upper_precedence) const
153 debugmsg("ncmul print",LOGLEVEL_PRINT);
154 printseq(os,'(','%',')',precedence,upper_precedence);
157 void ncmul::printraw(std::ostream & os) const
159 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(std::ostream & os, unsigned type, 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 std::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 const add & 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 const add & 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(const symbol & s) const
260 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
261 deg_sum+=(*cit).degree(s);
266 int ncmul::ldegree(const symbol & s) const
269 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
270 deg_sum+=(*cit).ldegree(s);
275 ex ncmul::coeff(const symbol & s, int 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(const ex & 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, const ex & 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 std::vector<unsigned> unsignedvector;
335 typedef std::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))->setflag(status_flags::dynallocated));
420 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
423 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
424 // (collect elements of same type)
426 if (count_noncommutative_composite==0) {
427 // there are neither commutative nor noncommutative_composite
428 // elements in assocseq
429 GINAC_ASSERT(count_commutative==0);
432 unsignedvector rttinfos;
433 evv.reserve(assocseq.size());
434 rttinfos.reserve(assocseq.size());
436 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
437 unsigned ti=(*cit).return_type_tinfo();
438 // search type in vector of known types
439 for (i=0; i<rttinfos.size(); ++i) {
440 if (ti==rttinfos[i]) {
441 evv[i].push_back(*cit);
445 if (i>=rttinfos.size()) {
447 rttinfos.push_back(ti);
448 evv.push_back(exvector());
449 (*(evv.end()-1)).reserve(assocseq.size());
450 (*(evv.end()-1)).push_back(*cit);
454 #ifdef DO_GINAC_ASSERT
455 GINAC_ASSERT(evv.size()==rttinfos.size());
456 GINAC_ASSERT(evv.size()>0);
458 for (i=0; i<evv.size(); ++i) {
461 GINAC_ASSERT(s==assocseq.size());
462 #endif // def DO_GINAC_ASSERT
464 // if all elements are of same type, simplify the string
466 return evv[0][0].simplify_ncmul(evv[0]);
470 splitseq.reserve(evv.size());
471 for (i=0; i<evv.size(); ++i) {
472 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
475 return (new mul(splitseq))->setflag(status_flags::dynallocated);
478 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
479 status_flags::evaluated);
482 exvector ncmul::get_indices(void) const
484 // return union of indices of factors
486 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
487 exvector subiv=(*cit).get_indices();
488 iv.reserve(iv.size()+subiv.size());
489 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
496 ex ncmul::subs(const lst & ls, const lst & lr) const
498 return ncmul(subschildren(ls, lr));
501 ex ncmul::thisexprseq(const exvector & v) const
503 return (new ncmul(v))->setflag(status_flags::dynallocated);
506 ex ncmul::thisexprseq(exvector * vp) const
508 return (new ncmul(vp))->setflag(status_flags::dynallocated);
513 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
515 ex ncmul::derivative(const symbol & s) const
520 int ncmul::compare_same_type(const basic & other) const
522 return inherited::compare_same_type(other);
525 unsigned ncmul::return_type(void) const
528 // ncmul without factors: should not happen, but commutes
529 return return_types::commutative;
532 bool all_commutative=1;
534 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
536 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
537 rt=(*cit).return_type();
538 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
539 if ((rt==return_types::noncommutative)&&(all_commutative)) {
540 // first nc element found, remember position
541 cit_noncommutative_element=cit;
544 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
545 // another nc element found, compare type_infos
546 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
547 // diffent types -> mul is ncc
548 return return_types::noncommutative_composite;
552 // all factors checked
553 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
554 return all_commutative ? return_types::commutative : return_types::noncommutative;
557 unsigned ncmul::return_type_tinfo(void) const
560 // mul without factors: should not happen
563 // return type_info of first noncommutative element
564 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
565 if ((*cit).return_type()==return_types::noncommutative) {
566 return (*cit).return_type_tinfo();
569 // no noncommutative element found, should not happen
574 // new virtual functions which can be overridden by derived classes
580 // non-virtual functions in this class
583 exvector ncmul::expandchildren(unsigned options) const
586 s.reserve(seq.size());
588 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
589 s.push_back((*it).expand(options));
594 const exvector & ncmul::get_factors(void) const
600 // static member variables
605 unsigned ncmul::precedence=50;
611 ex nonsimplified_ncmul(const exvector & v)
613 return (new ncmul(v))->setflag(status_flags::dynallocated);
616 ex simplified_ncmul(const exvector & v)
620 } else if (v.size()==1) {
623 return (new ncmul(v))->setflag(status_flags::dynallocated |
624 status_flags::evaluated);
627 #ifndef NO_NAMESPACE_GINAC
629 #endif // ndef NO_NAMESPACE_GINAC