3 * Implementation of GiNaC's non-commutative products of expressions.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 // default constructor, destructor, copy constructor assignment operator and helpers
39 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
40 tinfo_key = TINFO_ncmul;
45 debugmsg("ncmul destructor",LOGLEVEL_DESTRUCT);
49 ncmul::ncmul(ncmul const & other)
51 debugmsg("ncmul copy constructor",LOGLEVEL_CONSTRUCT);
55 ncmul const & ncmul::operator=(ncmul const & other)
57 debugmsg("ncmul operator=",LOGLEVEL_ASSIGNMENT);
67 void ncmul::copy(ncmul const & other)
72 void ncmul::destroy(bool call_parent)
74 if (call_parent) exprseq::destroy(call_parent);
83 ncmul::ncmul(ex const & lh, ex const & rh) :
86 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
87 tinfo_key = TINFO_ncmul;
90 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3) :
93 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_ncmul;
97 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
98 ex const & f4) : exprseq(f1,f2,f3,f4)
100 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
101 tinfo_key = TINFO_ncmul;
104 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
105 ex const & f4, ex const & f5) : exprseq(f1,f2,f3,f4,f5)
107 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
108 tinfo_key = TINFO_ncmul;
111 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
112 ex const & f4, ex const & f5, ex const & f6) :
113 exprseq(f1,f2,f3,f4,f5,f6)
115 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
116 tinfo_key = TINFO_ncmul;
119 ncmul::ncmul(exvector const & v, bool discardable) : exprseq(v,discardable)
121 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
122 tinfo_key = TINFO_ncmul;
125 ncmul::ncmul(exvector * vp) : exprseq(vp)
127 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
128 tinfo_key = TINFO_ncmul;
132 // functions overriding virtual functions from bases classes
137 basic * ncmul::duplicate() const
139 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
140 return new ncmul(*this);
143 bool ncmul::info(unsigned inf) const
145 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
148 typedef vector<int> intvector;
150 ex ncmul::expand(unsigned options) const
152 exvector sub_expanded_seq;
153 intvector positions_of_adds;
154 intvector number_of_add_operands;
156 exvector expanded_seq=expandchildren(options);
158 positions_of_adds.resize(expanded_seq.size());
159 number_of_add_operands.resize(expanded_seq.size());
161 int number_of_adds=0;
162 int number_of_expanded_terms=1;
164 unsigned current_position=0;
165 exvector::const_iterator last=expanded_seq.end();
166 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
167 if (is_ex_exactly_of_type((*cit),add)) {
168 positions_of_adds[number_of_adds]=current_position;
169 add const & expanded_addref=ex_to_add(*cit);
170 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
171 number_of_expanded_terms *= expanded_addref.seq.size();
177 if (number_of_adds==0) {
178 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
179 status_flags::expanded);
183 distrseq.reserve(number_of_expanded_terms);
186 k.resize(number_of_adds);
189 for (l=0; l<number_of_adds; l++) {
196 for (l=0; l<number_of_adds; l++) {
197 ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
198 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
199 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
201 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
202 status_flags::expanded));
206 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
213 return (new add(distrseq))->setflag(status_flags::dynallocated |
214 status_flags::expanded);
217 int ncmul::degree(symbol const & s) const
220 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
221 deg_sum+=(*cit).degree(s);
226 int ncmul::ldegree(symbol const & s) const
229 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
230 deg_sum+=(*cit).ldegree(s);
235 ex ncmul::coeff(symbol const & s, int const n) const
238 coeffseq.reserve(seq.size());
241 // product of individual coeffs
242 // if a non-zero power of s is found, the resulting product will be 0
243 exvector::const_iterator it=seq.begin();
244 while (it!=seq.end()) {
245 coeffseq.push_back((*it).coeff(s,n));
248 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
251 exvector::const_iterator it=seq.begin();
253 while (it!=seq.end()) {
254 ex c=(*it).coeff(s,n);
256 coeffseq.push_back(c);
259 coeffseq.push_back(*it);
264 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
269 unsigned ncmul::count_factors(ex const & e) const
271 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
272 (is_ex_exactly_of_type(e,ncmul))) {
274 for (int i=0; i<e.nops(); i++) {
275 factors += count_factors(e.op(i));
282 void ncmul::append_factors(exvector & v, ex const & e) const
284 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
285 (is_ex_exactly_of_type(e,ncmul))) {
286 for (int i=0; i<e.nops(); i++) {
287 append_factors(v,e.op(i));
294 typedef vector<unsigned> unsignedvector;
295 typedef vector<exvector> exvectorvector;
297 ex ncmul::eval(int level) const
299 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
300 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
303 // ncmul(...,c1,...,c2,...) ->
304 // *(c1,c2,ncmul(...)) (pull out commutative elements)
305 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
306 // (collect elements of same type)
307 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
308 // the following rule would be nice, but produces a recursion,
309 // which must be trapped by introducing a flag that the sub-ncmuls()
310 // are already evaluated (maybe later...)
311 // ncmul(x1,x2,...,X,y1,y2,...) ->
312 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
313 // (X noncommutative_composite)
315 if ((level==1)&&(flags & status_flags::evaluated)) {
319 exvector evaledseq=evalchildren(level);
321 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
322 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
324 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
325 factors += count_factors(*cit);
329 assocseq.reserve(factors);
330 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
331 append_factors(assocseq,*cit);
335 if (assocseq.size()==1) return *(seq.begin());
338 if (assocseq.size()==0) return exONE();
340 // determine return types
341 unsignedvector rettypes;
342 rettypes.reserve(assocseq.size());
344 unsigned count_commutative=0;
345 unsigned count_noncommutative=0;
346 unsigned count_noncommutative_composite=0;
347 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
348 switch (rettypes[i]=(*cit).return_type()) {
349 case return_types::commutative:
352 case return_types::noncommutative:
353 count_noncommutative++;
355 case return_types::noncommutative_composite:
356 count_noncommutative_composite++;
359 throw(std::logic_error("ncmul::eval(): invalid return type"));
363 ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
365 // ncmul(...,c1,...,c2,...) ->
366 // *(c1,c2,ncmul(...)) (pull out commutative elements)
367 if (count_commutative!=0) {
368 exvector commutativeseq;
369 commutativeseq.reserve(count_commutative+1);
370 exvector noncommutativeseq;
371 noncommutativeseq.reserve(assocseq.size()-count_commutative);
372 for (i=0; i<assocseq.size(); ++i) {
373 if (rettypes[i]==return_types::commutative) {
374 commutativeseq.push_back(assocseq[i]);
376 noncommutativeseq.push_back(assocseq[i]);
379 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->
380 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 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);
416 ASSERT(evv.size()==rttinfos.size());
417 ASSERT(evv.size()>0);
419 for (i=0; i<evv.size(); ++i) {
422 ASSERT(s==assocseq.size());
423 #endif // def DOASSERT
425 // if all elements are of same type, simplify the string
427 return evv[0][0].simplify_ncmul(evv[0]);
431 splitseq.reserve(evv.size());
432 for (i=0; i<evv.size(); ++i) {
433 splitseq.push_back((new ncmul(evv[i]))->
434 setflag(status_flags::dynallocated));
437 return (new mul(splitseq))->setflag(status_flags::dynallocated);
440 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
441 status_flags::evaluated);
444 exvector ncmul::get_indices(void) const
446 // return union of indices of factors
448 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
449 exvector subiv=(*cit).get_indices();
450 iv.reserve(iv.size()+subiv.size());
451 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
458 ex ncmul::subs(lst const & ls, lst const & lr) const
460 return ncmul(subschildren(ls, lr));
463 ex ncmul::thisexprseq(exvector const & v) const
465 return (new ncmul(v))->setflag(status_flags::dynallocated);
468 ex ncmul::thisexprseq(exvector * vp) const
470 return (new ncmul(vp))->setflag(status_flags::dynallocated);
475 int ncmul::compare_same_type(basic const & other) const
477 return exprseq::compare_same_type(other);
480 unsigned ncmul::return_type(void) const
483 // ncmul without factors: should not happen, but commutes
484 return return_types::commutative;
487 bool all_commutative=1;
489 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
491 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
492 rt=(*cit).return_type();
493 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
494 if ((rt==return_types::noncommutative)&&(all_commutative)) {
495 // first nc element found, remember position
496 cit_noncommutative_element=cit;
499 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
500 // another nc element found, compare type_infos
501 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
502 // diffent types -> mul is ncc
503 return return_types::noncommutative_composite;
507 // all factors checked
508 ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
509 return all_commutative ? return_types::commutative : return_types::noncommutative;
512 unsigned ncmul::return_type_tinfo(void) const
515 // mul without factors: should not happen
518 // return type_info of first noncommutative element
519 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
520 if ((*cit).return_type()==return_types::noncommutative) {
521 return (*cit).return_type_tinfo();
524 // no noncommutative element found, should not happen
529 // new virtual functions which can be overridden by derived classes
535 // non-virtual functions in this class
538 exvector ncmul::expandchildren(unsigned options) const
541 s.reserve(seq.size());
543 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
544 s.push_back((*it).expand(options));
549 exvector const & ncmul::get_factors(void) const
555 // static member variables
560 unsigned ncmul::precedence=50;
567 const ncmul some_ncmul;
568 type_info const & typeid_ncmul=typeid(some_ncmul);
574 ex nonsimplified_ncmul(exvector const & v)
576 return (new ncmul(v))->setflag(status_flags::dynallocated);
579 ex simplified_ncmul(exvector const & v)
583 } else if (v.size()==1) {
586 return (new ncmul(v))->setflag(status_flags::dynallocated |
587 status_flags::evaluated);