3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999 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
33 // default constructor, destructor, copy constructor assignment operator and helpers
40 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
41 tinfo_key = TINFO_ncmul;
46 debugmsg("ncmul destructor",LOGLEVEL_DESTRUCT);
50 ncmul::ncmul(ncmul const & other)
52 debugmsg("ncmul copy constructor",LOGLEVEL_CONSTRUCT);
56 ncmul const & ncmul::operator=(ncmul const & other)
58 debugmsg("ncmul operator=",LOGLEVEL_ASSIGNMENT);
68 void ncmul::copy(ncmul const & other)
73 void ncmul::destroy(bool call_parent)
75 if (call_parent) exprseq::destroy(call_parent);
84 ncmul::ncmul(ex const & lh, ex const & rh) :
87 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
88 tinfo_key = TINFO_ncmul;
91 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3) :
94 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
95 tinfo_key = TINFO_ncmul;
98 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
99 ex const & f4) : exprseq(f1,f2,f3,f4)
101 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
102 tinfo_key = TINFO_ncmul;
105 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
106 ex const & f4, ex const & f5) : exprseq(f1,f2,f3,f4,f5)
108 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
109 tinfo_key = TINFO_ncmul;
112 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
113 ex const & f4, ex const & f5, ex const & f6) :
114 exprseq(f1,f2,f3,f4,f5,f6)
116 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
117 tinfo_key = TINFO_ncmul;
120 ncmul::ncmul(exvector const & v, bool discardable) : exprseq(v,discardable)
122 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
123 tinfo_key = TINFO_ncmul;
126 ncmul::ncmul(exvector * vp) : exprseq(vp)
128 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
129 tinfo_key = TINFO_ncmul;
133 // functions overriding virtual functions from bases classes
138 basic * ncmul::duplicate() const
140 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
141 return new ncmul(*this);
144 bool ncmul::info(unsigned inf) const
146 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
149 typedef vector<int> intvector;
151 ex ncmul::expand(unsigned options) const
153 exvector sub_expanded_seq;
154 intvector positions_of_adds;
155 intvector number_of_add_operands;
157 exvector expanded_seq=expandchildren(options);
159 positions_of_adds.resize(expanded_seq.size());
160 number_of_add_operands.resize(expanded_seq.size());
162 int number_of_adds=0;
163 int number_of_expanded_terms=1;
165 unsigned current_position=0;
166 exvector::const_iterator last=expanded_seq.end();
167 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
168 if (is_ex_exactly_of_type((*cit),add)) {
169 positions_of_adds[number_of_adds]=current_position;
170 add const & expanded_addref=ex_to_add(*cit);
171 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
172 number_of_expanded_terms *= expanded_addref.seq.size();
178 if (number_of_adds==0) {
179 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
180 status_flags::expanded);
184 distrseq.reserve(number_of_expanded_terms);
187 k.resize(number_of_adds);
190 for (l=0; l<number_of_adds; l++) {
197 for (l=0; l<number_of_adds; l++) {
198 ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
199 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
200 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
202 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
203 status_flags::expanded));
207 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
214 return (new add(distrseq))->setflag(status_flags::dynallocated |
215 status_flags::expanded);
218 int ncmul::degree(symbol const & s) const
221 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
222 deg_sum+=(*cit).degree(s);
227 int ncmul::ldegree(symbol const & s) const
230 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
231 deg_sum+=(*cit).ldegree(s);
236 ex ncmul::coeff(symbol const & s, int const n) const
239 coeffseq.reserve(seq.size());
242 // product of individual coeffs
243 // if a non-zero power of s is found, the resulting product will be 0
244 exvector::const_iterator it=seq.begin();
245 while (it!=seq.end()) {
246 coeffseq.push_back((*it).coeff(s,n));
249 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
252 exvector::const_iterator it=seq.begin();
254 while (it!=seq.end()) {
255 ex c=(*it).coeff(s,n);
257 coeffseq.push_back(c);
260 coeffseq.push_back(*it);
265 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
270 unsigned ncmul::count_factors(ex const & e) const
272 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
273 (is_ex_exactly_of_type(e,ncmul))) {
275 for (int i=0; i<e.nops(); i++) {
276 factors += count_factors(e.op(i));
283 void ncmul::append_factors(exvector & v, ex const & e) const
285 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
286 (is_ex_exactly_of_type(e,ncmul))) {
287 for (int i=0; i<e.nops(); i++) {
288 append_factors(v,e.op(i));
295 typedef vector<unsigned> unsignedvector;
296 typedef vector<exvector> exvectorvector;
298 ex ncmul::eval(int level) const
300 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
301 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
304 // ncmul(...,c1,...,c2,...) ->
305 // *(c1,c2,ncmul(...)) (pull out commutative elements)
306 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
307 // (collect elements of same type)
308 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
309 // the following rule would be nice, but produces a recursion,
310 // which must be trapped by introducing a flag that the sub-ncmuls()
311 // are already evaluated (maybe later...)
312 // ncmul(x1,x2,...,X,y1,y2,...) ->
313 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
314 // (X noncommutative_composite)
316 if ((level==1)&&(flags & status_flags::evaluated)) {
320 exvector evaledseq=evalchildren(level);
322 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
323 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
325 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
326 factors += count_factors(*cit);
330 assocseq.reserve(factors);
331 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
332 append_factors(assocseq,*cit);
336 if (assocseq.size()==1) return *(seq.begin());
339 if (assocseq.size()==0) return exONE();
341 // determine return types
342 unsignedvector rettypes;
343 rettypes.reserve(assocseq.size());
345 unsigned count_commutative=0;
346 unsigned count_noncommutative=0;
347 unsigned count_noncommutative_composite=0;
348 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
349 switch (rettypes[i]=(*cit).return_type()) {
350 case return_types::commutative:
353 case return_types::noncommutative:
354 count_noncommutative++;
356 case return_types::noncommutative_composite:
357 count_noncommutative_composite++;
360 throw(std::logic_error("ncmul::eval(): invalid return type"));
364 ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
366 // ncmul(...,c1,...,c2,...) ->
367 // *(c1,c2,ncmul(...)) (pull out commutative elements)
368 if (count_commutative!=0) {
369 exvector commutativeseq;
370 commutativeseq.reserve(count_commutative+1);
371 exvector noncommutativeseq;
372 noncommutativeseq.reserve(assocseq.size()-count_commutative);
373 for (i=0; i<assocseq.size(); ++i) {
374 if (rettypes[i]==return_types::commutative) {
375 commutativeseq.push_back(assocseq[i]);
377 noncommutativeseq.push_back(assocseq[i]);
380 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->
381 setflag(status_flags::dynallocated));
382 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
385 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
386 // (collect elements of same type)
388 if (count_noncommutative_composite==0) {
389 // there are neither commutative nor noncommutative_composite
390 // elements in assocseq
391 ASSERT(count_commutative==0);
394 unsignedvector rttinfos;
395 evv.reserve(assocseq.size());
396 rttinfos.reserve(assocseq.size());
398 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
399 unsigned ti=(*cit).return_type_tinfo();
400 // search type in vector of known types
401 for (i=0; i<rttinfos.size(); ++i) {
402 if (ti==rttinfos[i]) {
403 evv[i].push_back(*cit);
407 if (i>=rttinfos.size()) {
409 rttinfos.push_back(ti);
410 evv.push_back(exvector());
411 (*(evv.end()-1)).reserve(assocseq.size());
412 (*(evv.end()-1)).push_back(*cit);
417 ASSERT(evv.size()==rttinfos.size());
418 ASSERT(evv.size()>0);
420 for (i=0; i<evv.size(); ++i) {
423 ASSERT(s==assocseq.size());
424 #endif // def DOASSERT
426 // if all elements are of same type, simplify the string
428 return evv[0][0].simplify_ncmul(evv[0]);
432 splitseq.reserve(evv.size());
433 for (i=0; i<evv.size(); ++i) {
434 splitseq.push_back((new ncmul(evv[i]))->
435 setflag(status_flags::dynallocated));
438 return (new mul(splitseq))->setflag(status_flags::dynallocated);
441 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
442 status_flags::evaluated);
445 exvector ncmul::get_indices(void) const
447 // return union of indices of factors
449 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
450 exvector subiv=(*cit).get_indices();
451 iv.reserve(iv.size()+subiv.size());
452 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
459 ex ncmul::subs(lst const & ls, lst const & lr) const
461 return ncmul(subschildren(ls, lr));
464 ex ncmul::thisexprseq(exvector const & v) const
466 return (new ncmul(v))->setflag(status_flags::dynallocated);
469 ex ncmul::thisexprseq(exvector * vp) const
471 return (new ncmul(vp))->setflag(status_flags::dynallocated);
476 int ncmul::compare_same_type(basic const & other) const
478 return exprseq::compare_same_type(other);
481 unsigned ncmul::return_type(void) const
484 // ncmul without factors: should not happen, but commutes
485 return return_types::commutative;
488 bool all_commutative=1;
490 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
492 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
493 rt=(*cit).return_type();
494 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
495 if ((rt==return_types::noncommutative)&&(all_commutative)) {
496 // first nc element found, remember position
497 cit_noncommutative_element=cit;
500 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
501 // another nc element found, compare type_infos
502 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
503 // diffent types -> mul is ncc
504 return return_types::noncommutative_composite;
508 // all factors checked
509 ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
510 return all_commutative ? return_types::commutative : return_types::noncommutative;
513 unsigned ncmul::return_type_tinfo(void) const
516 // mul without factors: should not happen
519 // return type_info of first noncommutative element
520 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
521 if ((*cit).return_type()==return_types::noncommutative) {
522 return (*cit).return_type_tinfo();
525 // no noncommutative element found, should not happen
530 // new virtual functions which can be overridden by derived classes
536 // non-virtual functions in this class
539 exvector ncmul::expandchildren(unsigned options) const
542 s.reserve(seq.size());
544 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
545 s.push_back((*it).expand(options));
550 exvector const & ncmul::get_factors(void) const
556 // static member variables
561 unsigned ncmul::precedence=50;
568 const ncmul some_ncmul;
569 type_info const & typeid_ncmul=typeid(some_ncmul);
575 ex nonsimplified_ncmul(exvector const & v)
577 return (new ncmul(v))->setflag(status_flags::dynallocated);
580 ex simplified_ncmul(exvector const & v)
584 } else if (v.size()==1) {
587 return (new ncmul(v))->setflag(status_flags::dynallocated |
588 status_flags::evaluated);