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
29 // default constructor, destructor, copy constructor assignment operator and helpers
36 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
37 tinfo_key = TINFO_NCMUL;
42 debugmsg("ncmul destructor",LOGLEVEL_DESTRUCT);
46 ncmul::ncmul(ncmul const & other)
48 debugmsg("ncmul copy constructor",LOGLEVEL_CONSTRUCT);
52 ncmul const & ncmul::operator=(ncmul const & other)
54 debugmsg("ncmul operator=",LOGLEVEL_ASSIGNMENT);
64 void ncmul::copy(ncmul const & other)
69 void ncmul::destroy(bool call_parent)
71 if (call_parent) exprseq::destroy(call_parent);
80 ncmul::ncmul(ex const & lh, ex const & rh) :
83 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
84 tinfo_key = TINFO_NCMUL;
87 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3) :
90 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
91 tinfo_key = TINFO_NCMUL;
94 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
95 ex const & f4) : exprseq(f1,f2,f3,f4)
97 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
98 tinfo_key = TINFO_NCMUL;
101 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
102 ex const & f4, ex const & f5) : exprseq(f1,f2,f3,f4,f5)
104 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
105 tinfo_key = TINFO_NCMUL;
108 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
109 ex const & f4, ex const & f5, ex const & f6) :
110 exprseq(f1,f2,f3,f4,f5,f6)
112 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
113 tinfo_key = TINFO_NCMUL;
116 ncmul::ncmul(exvector const & v, bool discardable) : exprseq(v,discardable)
118 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
119 tinfo_key = TINFO_NCMUL;
122 ncmul::ncmul(exvector * vp) : exprseq(vp)
124 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
125 tinfo_key = TINFO_NCMUL;
129 // functions overriding virtual functions from bases classes
134 basic * ncmul::duplicate() const
136 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
137 return new ncmul(*this);
140 bool ncmul::info(unsigned inf) const
142 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
145 typedef vector<int> intvector;
147 ex ncmul::expand(unsigned options) const
149 exvector sub_expanded_seq;
150 intvector positions_of_adds;
151 intvector number_of_add_operands;
153 exvector expanded_seq=expandchildren(options);
155 positions_of_adds.resize(expanded_seq.size());
156 number_of_add_operands.resize(expanded_seq.size());
158 int number_of_adds=0;
159 int number_of_expanded_terms=1;
161 unsigned current_position=0;
162 exvector::const_iterator last=expanded_seq.end();
163 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
164 if (is_ex_exactly_of_type((*cit),add)) {
165 positions_of_adds[number_of_adds]=current_position;
166 add const & expanded_addref=ex_to_add(*cit);
167 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
168 number_of_expanded_terms *= expanded_addref.seq.size();
174 if (number_of_adds==0) {
175 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
176 status_flags::expanded);
180 distrseq.reserve(number_of_expanded_terms);
183 k.resize(number_of_adds);
186 for (l=0; l<number_of_adds; l++) {
193 for (l=0; l<number_of_adds; l++) {
194 ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
195 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
196 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
198 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
199 status_flags::expanded));
203 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
210 return (new add(distrseq))->setflag(status_flags::dynallocated |
211 status_flags::expanded);
214 int ncmul::degree(symbol const & s) const
217 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
218 deg_sum+=(*cit).degree(s);
223 int ncmul::ldegree(symbol const & s) const
226 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
227 deg_sum+=(*cit).ldegree(s);
232 ex ncmul::coeff(symbol const & s, int const n) const
235 coeffseq.reserve(seq.size());
238 // product of individual coeffs
239 // if a non-zero power of s is found, the resulting product will be 0
240 exvector::const_iterator it=seq.begin();
241 while (it!=seq.end()) {
242 coeffseq.push_back((*it).coeff(s,n));
245 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
248 exvector::const_iterator it=seq.begin();
250 while (it!=seq.end()) {
251 ex c=(*it).coeff(s,n);
253 coeffseq.push_back(c);
256 coeffseq.push_back(*it);
261 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
266 unsigned ncmul::count_factors(ex const & e) const
268 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
269 (is_ex_exactly_of_type(e,ncmul))) {
271 for (int i=0; i<e.nops(); i++) {
272 factors += count_factors(e.op(i));
279 void ncmul::append_factors(exvector & v, ex const & e) const
281 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
282 (is_ex_exactly_of_type(e,ncmul))) {
283 for (int i=0; i<e.nops(); i++) {
284 append_factors(v,e.op(i));
291 typedef vector<unsigned> unsignedvector;
292 typedef vector<exvector> exvectorvector;
294 ex ncmul::eval(int level) const
296 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
297 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
300 // ncmul(...,c1,...,c2,...) ->
301 // *(c1,c2,ncmul(...)) (pull out commutative elements)
302 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
303 // (collect elements of same type)
304 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
305 // the following rule would be nice, but produces a recursion,
306 // which must be trapped by introducing a flag that the sub-ncmuls()
307 // are already evaluated (maybe later...)
308 // ncmul(x1,x2,...,X,y1,y2,...) ->
309 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
310 // (X noncommutative_composite)
312 if ((level==1)&&(flags & status_flags::evaluated)) {
316 exvector evaledseq=evalchildren(level);
318 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
319 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
321 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
322 factors += count_factors(*cit);
326 assocseq.reserve(factors);
327 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
328 append_factors(assocseq,*cit);
332 if (assocseq.size()==1) return *(seq.begin());
335 if (assocseq.size()==0) return exONE();
337 // determine return types
338 unsignedvector rettypes;
339 rettypes.reserve(assocseq.size());
341 unsigned count_commutative=0;
342 unsigned count_noncommutative=0;
343 unsigned count_noncommutative_composite=0;
344 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
345 switch (rettypes[i]=(*cit).return_type()) {
346 case return_types::commutative:
349 case return_types::noncommutative:
350 count_noncommutative++;
352 case return_types::noncommutative_composite:
353 count_noncommutative_composite++;
356 throw(std::logic_error("ncmul::eval(): invalid return type"));
360 ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
362 // ncmul(...,c1,...,c2,...) ->
363 // *(c1,c2,ncmul(...)) (pull out commutative elements)
364 if (count_commutative!=0) {
365 exvector commutativeseq;
366 commutativeseq.reserve(count_commutative+1);
367 exvector noncommutativeseq;
368 noncommutativeseq.reserve(assocseq.size()-count_commutative);
369 for (i=0; i<assocseq.size(); ++i) {
370 if (rettypes[i]==return_types::commutative) {
371 commutativeseq.push_back(assocseq[i]);
373 noncommutativeseq.push_back(assocseq[i]);
376 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->
377 setflag(status_flags::dynallocated));
378 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
381 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
382 // (collect elements of same type)
384 if (count_noncommutative_composite==0) {
385 // there are neither commutative nor noncommutative_composite
386 // elements in assocseq
387 ASSERT(count_commutative==0);
390 unsignedvector rttinfos;
391 evv.reserve(assocseq.size());
392 rttinfos.reserve(assocseq.size());
394 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
395 unsigned ti=(*cit).return_type_tinfo();
396 // search type in vector of known types
397 for (i=0; i<rttinfos.size(); ++i) {
398 if (ti==rttinfos[i]) {
399 evv[i].push_back(*cit);
403 if (i>=rttinfos.size()) {
405 rttinfos.push_back(ti);
406 evv.push_back(exvector());
407 (*(evv.end()-1)).reserve(assocseq.size());
408 (*(evv.end()-1)).push_back(*cit);
413 ASSERT(evv.size()==rttinfos.size());
414 ASSERT(evv.size()>0);
416 for (i=0; i<evv.size(); ++i) {
419 ASSERT(s==assocseq.size());
420 #endif // def DOASSERT
422 // if all elements are of same type, simplify the string
424 return evv[0][0].simplify_ncmul(evv[0]);
428 splitseq.reserve(evv.size());
429 for (i=0; i<evv.size(); ++i) {
430 splitseq.push_back((new ncmul(evv[i]))->
431 setflag(status_flags::dynallocated));
434 return (new mul(splitseq))->setflag(status_flags::dynallocated);
437 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
438 status_flags::evaluated);
441 exvector ncmul::get_indices(void) const
443 // return union of indices of factors
445 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
446 exvector subiv=(*cit).get_indices();
447 iv.reserve(iv.size()+subiv.size());
448 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
455 ex ncmul::subs(lst const & ls, lst const & lr) const
457 return ncmul(subschildren(ls, lr));
460 ex ncmul::thisexprseq(exvector const & v) const
462 return (new ncmul(v))->setflag(status_flags::dynallocated);
465 ex ncmul::thisexprseq(exvector * vp) const
467 return (new ncmul(vp))->setflag(status_flags::dynallocated);
472 int ncmul::compare_same_type(basic const & other) const
474 return exprseq::compare_same_type(other);
477 unsigned ncmul::return_type(void) const
480 // ncmul without factors: should not happen, but commutes
481 return return_types::commutative;
484 bool all_commutative=1;
486 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
488 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
489 rt=(*cit).return_type();
490 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
491 if ((rt==return_types::noncommutative)&&(all_commutative)) {
492 // first nc element found, remember position
493 cit_noncommutative_element=cit;
496 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
497 // another nc element found, compare type_infos
498 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
499 // diffent types -> mul is ncc
500 return return_types::noncommutative_composite;
504 // all factors checked
505 ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
506 return all_commutative ? return_types::commutative : return_types::noncommutative;
509 unsigned ncmul::return_type_tinfo(void) const
512 // mul without factors: should not happen
515 // return type_info of first noncommutative element
516 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
517 if ((*cit).return_type()==return_types::noncommutative) {
518 return (*cit).return_type_tinfo();
521 // no noncommutative element found, should not happen
526 // new virtual functions which can be overridden by derived classes
532 // non-virtual functions in this class
535 exvector ncmul::expandchildren(unsigned options) const
538 s.reserve(seq.size());
540 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
541 s.push_back((*it).expand(options));
546 exvector const & ncmul::get_factors(void) const
552 // static member variables
557 unsigned ncmul::precedence=50;
564 const ncmul some_ncmul;
565 type_info const & typeid_ncmul=typeid(some_ncmul);
571 ex nonsimplified_ncmul(exvector const & v)
573 return (new ncmul(v))->setflag(status_flags::dynallocated);
576 ex simplified_ncmul(exvector const & v)
580 } else if (v.size()==1) {
583 return (new ncmul(v))->setflag(status_flags::dynallocated |
584 status_flags::evaluated);