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
37 GINAC_IMPLEMENT_REGISTERED_CLASS(ncmul, exprseq)
40 // default constructor, destructor, copy constructor assignment operator and helpers
45 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
46 tinfo_key = TINFO_ncmul;
50 DEFAULT_DESTROY(ncmul)
58 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
60 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
61 tinfo_key = TINFO_ncmul;
64 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
66 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
67 tinfo_key = TINFO_ncmul;
70 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
71 const ex & f4) : inherited(f1,f2,f3,f4)
73 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
74 tinfo_key = TINFO_ncmul;
77 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
78 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
80 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
81 tinfo_key = TINFO_ncmul;
84 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
85 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
87 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
88 tinfo_key = TINFO_ncmul;
91 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
93 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_ncmul;
97 ncmul::ncmul(exvector * vp) : inherited(vp)
99 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
100 tinfo_key = TINFO_ncmul;
107 DEFAULT_ARCHIVING(ncmul)
110 // functions overriding virtual functions from bases classes
115 void ncmul::print(std::ostream & os, unsigned upper_precedence) const
117 debugmsg("ncmul print",LOGLEVEL_PRINT);
118 printseq(os,'(','*',')',precedence,upper_precedence);
121 void ncmul::printraw(std::ostream & os) const
123 debugmsg("ncmul printraw",LOGLEVEL_PRINT);
125 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
126 (*it).bp->printraw(os);
129 os << ",hash=" << hashvalue << ",flags=" << flags;
133 void ncmul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
135 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
136 exvector::const_iterator it;
137 exvector::const_iterator itend = seq.end()-1;
139 for (it=seq.begin(); it!=itend; ++it) {
140 (*it).bp->printcsrc(os,precedence);
143 (*it).bp->printcsrc(os,precedence);
147 bool ncmul::info(unsigned inf) const
149 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
152 typedef std::vector<int> intvector;
154 ex ncmul::expand(unsigned options) const
156 exvector sub_expanded_seq;
157 intvector positions_of_adds;
158 intvector number_of_add_operands;
160 exvector expanded_seq=expandchildren(options);
162 positions_of_adds.resize(expanded_seq.size());
163 number_of_add_operands.resize(expanded_seq.size());
165 int number_of_adds=0;
166 int number_of_expanded_terms=1;
168 unsigned current_position=0;
169 exvector::const_iterator last=expanded_seq.end();
170 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
171 if (is_ex_exactly_of_type((*cit),add)) {
172 positions_of_adds[number_of_adds]=current_position;
173 const add & expanded_addref=ex_to_add(*cit);
174 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
175 number_of_expanded_terms *= expanded_addref.seq.size();
181 if (number_of_adds==0) {
182 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
183 status_flags::expanded);
187 distrseq.reserve(number_of_expanded_terms);
190 k.resize(number_of_adds);
193 for (l=0; l<number_of_adds; l++) {
200 for (l=0; l<number_of_adds; l++) {
201 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
202 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
203 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
205 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
206 status_flags::expanded));
210 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
217 return (new add(distrseq))->setflag(status_flags::dynallocated |
218 status_flags::expanded);
221 int ncmul::degree(const ex & s) const
224 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
225 deg_sum+=(*cit).degree(s);
230 int ncmul::ldegree(const ex & s) const
233 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
234 deg_sum+=(*cit).ldegree(s);
239 ex ncmul::coeff(const ex & s, int n) const
242 coeffseq.reserve(seq.size());
245 // product of individual coeffs
246 // if a non-zero power of s is found, the resulting product will be 0
247 exvector::const_iterator it=seq.begin();
248 while (it!=seq.end()) {
249 coeffseq.push_back((*it).coeff(s,n));
252 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
255 exvector::const_iterator it=seq.begin();
257 while (it!=seq.end()) {
258 ex c=(*it).coeff(s,n);
260 coeffseq.push_back(c);
263 coeffseq.push_back(*it);
268 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
273 unsigned ncmul::count_factors(const ex & e) const
275 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
276 (is_ex_exactly_of_type(e,ncmul))) {
278 for (unsigned i=0; i<e.nops(); i++)
279 factors += count_factors(e.op(i));
286 void ncmul::append_factors(exvector & v, const ex & e) const
288 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
289 (is_ex_exactly_of_type(e,ncmul))) {
290 for (unsigned i=0; i<e.nops(); i++)
291 append_factors(v,e.op(i));
298 typedef std::vector<unsigned> unsignedvector;
299 typedef std::vector<exvector> exvectorvector;
301 ex ncmul::eval(int level) const
303 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
304 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
307 // ncmul(...,c1,...,c2,...)
308 // *(c1,c2,ncmul(...)) (pull out commutative elements)
309 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
310 // (collect elements of same type)
311 // ncmul(x1,x2,x3,...) -> x::simplify_ncmul(x1,x2,x3,...)
312 // the following rule would be nice, but produces a recursion,
313 // which must be trapped by introducing a flag that the sub-ncmuls()
314 // are already evaluated (maybe later...)
315 // ncmul(x1,x2,...,X,y1,y2,...) ->
316 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
317 // (X noncommutative_composite)
319 if ((level==1) && (flags & status_flags::evaluated)) {
323 exvector evaledseq=evalchildren(level);
325 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
326 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
328 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit)
329 factors += count_factors(*cit);
332 assocseq.reserve(factors);
333 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit)
334 append_factors(assocseq,*cit);
337 if (assocseq.size()==1) return *(seq.begin());
340 if (assocseq.size()==0) return _ex1();
342 // determine return types
343 unsignedvector rettypes;
344 rettypes.reserve(assocseq.size());
346 unsigned count_commutative=0;
347 unsigned count_noncommutative=0;
348 unsigned count_noncommutative_composite=0;
349 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
350 switch (rettypes[i]=(*cit).return_type()) {
351 case return_types::commutative:
354 case return_types::noncommutative:
355 count_noncommutative++;
357 case return_types::noncommutative_composite:
358 count_noncommutative_composite++;
361 throw(std::logic_error("ncmul::eval(): invalid return type"));
365 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
367 // ncmul(...,c1,...,c2,...) ->
368 // *(c1,c2,ncmul(...)) (pull out commutative elements)
369 if (count_commutative!=0) {
370 exvector commutativeseq;
371 commutativeseq.reserve(count_commutative+1);
372 exvector noncommutativeseq;
373 noncommutativeseq.reserve(assocseq.size()-count_commutative);
374 for (i=0; i<assocseq.size(); ++i) {
375 if (rettypes[i]==return_types::commutative)
376 commutativeseq.push_back(assocseq[i]);
378 noncommutativeseq.push_back(assocseq[i]);
380 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
381 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
384 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
385 // (collect elements of same type)
387 if (count_noncommutative_composite==0) {
388 // there are neither commutative nor noncommutative_composite
389 // elements in assocseq
390 GINAC_ASSERT(count_commutative==0);
393 unsignedvector rttinfos;
394 evv.reserve(assocseq.size());
395 rttinfos.reserve(assocseq.size());
397 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
398 unsigned ti=(*cit).return_type_tinfo();
399 // search type in vector of known types
400 for (i=0; i<rttinfos.size(); ++i) {
401 if (ti==rttinfos[i]) {
402 evv[i].push_back(*cit);
406 if (i>=rttinfos.size()) {
408 rttinfos.push_back(ti);
409 evv.push_back(exvector());
410 (*(evv.end()-1)).reserve(assocseq.size());
411 (*(evv.end()-1)).push_back(*cit);
415 #ifdef DO_GINAC_ASSERT
416 GINAC_ASSERT(evv.size()==rttinfos.size());
417 GINAC_ASSERT(evv.size()>0);
419 for (i=0; i<evv.size(); ++i) {
422 GINAC_ASSERT(s==assocseq.size());
423 #endif // def DO_GINAC_ASSERT
425 // if all elements are of same type, simplify the string
427 return evv[0][0].simplify_ncmul(evv[0]);
430 splitseq.reserve(evv.size());
431 for (i=0; i<evv.size(); ++i) {
432 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
435 return (new mul(splitseq))->setflag(status_flags::dynallocated);
438 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
439 status_flags::evaluated);
442 ex ncmul::subs(const lst & ls, const lst & lr) const
444 return ncmul(subschildren(ls, lr));
447 ex ncmul::thisexprseq(const exvector & v) const
449 return (new ncmul(v))->setflag(status_flags::dynallocated);
452 ex ncmul::thisexprseq(exvector * vp) const
454 return (new ncmul(vp))->setflag(status_flags::dynallocated);
459 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
461 ex ncmul::derivative(const symbol & s) const
466 int ncmul::compare_same_type(const basic & other) const
468 return inherited::compare_same_type(other);
471 unsigned ncmul::return_type(void) const
474 // ncmul without factors: should not happen, but commutes
475 return return_types::commutative;
478 bool all_commutative=1;
480 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
482 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
483 rt=(*cit).return_type();
484 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
485 if ((rt==return_types::noncommutative)&&(all_commutative)) {
486 // first nc element found, remember position
487 cit_noncommutative_element=cit;
490 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
491 // another nc element found, compare type_infos
492 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
493 // diffent types -> mul is ncc
494 return return_types::noncommutative_composite;
498 // all factors checked
499 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
500 return all_commutative ? return_types::commutative : return_types::noncommutative;
503 unsigned ncmul::return_type_tinfo(void) const
506 // mul without factors: should not happen
509 // return type_info of first noncommutative element
510 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
511 if ((*cit).return_type()==return_types::noncommutative) {
512 return (*cit).return_type_tinfo();
515 // no noncommutative element found, should not happen
520 // new virtual functions which can be overridden by derived classes
526 // non-virtual functions in this class
529 exvector ncmul::expandchildren(unsigned options) const
532 s.reserve(seq.size());
534 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
535 s.push_back((*it).expand(options));
540 const exvector & ncmul::get_factors(void) const
546 // static member variables
551 unsigned ncmul::precedence = 50;
557 ex nonsimplified_ncmul(const exvector & v)
559 return (new ncmul(v))->setflag(status_flags::dynallocated);
562 ex simplified_ncmul(const exvector & v)
566 } else if (v.size()==1) {
569 return (new ncmul(v))->setflag(status_flags::dynallocated |
570 status_flags::evaluated);