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 #ifndef NO_GINAC_NAMESPACE
35 #endif // ndef NO_GINAC_NAMESPACE
38 // default constructor, destructor, copy constructor assignment operator and helpers
45 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
46 tinfo_key = TINFO_ncmul;
51 debugmsg("ncmul destructor",LOGLEVEL_DESTRUCT);
55 ncmul::ncmul(ncmul const & other)
57 debugmsg("ncmul copy constructor",LOGLEVEL_CONSTRUCT);
61 ncmul const & ncmul::operator=(ncmul const & other)
63 debugmsg("ncmul operator=",LOGLEVEL_ASSIGNMENT);
73 void ncmul::copy(ncmul const & other)
78 void ncmul::destroy(bool call_parent)
80 if (call_parent) exprseq::destroy(call_parent);
89 ncmul::ncmul(ex const & lh, ex const & rh) :
92 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
93 tinfo_key = TINFO_ncmul;
96 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3) :
99 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
100 tinfo_key = TINFO_ncmul;
103 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
104 ex const & f4) : exprseq(f1,f2,f3,f4)
106 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
107 tinfo_key = TINFO_ncmul;
110 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
111 ex const & f4, ex const & f5) : exprseq(f1,f2,f3,f4,f5)
113 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
114 tinfo_key = TINFO_ncmul;
117 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
118 ex const & f4, ex const & f5, ex const & f6) :
119 exprseq(f1,f2,f3,f4,f5,f6)
121 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
122 tinfo_key = TINFO_ncmul;
125 ncmul::ncmul(exvector const & v, bool discardable) : exprseq(v,discardable)
127 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
128 tinfo_key = TINFO_ncmul;
131 ncmul::ncmul(exvector * vp) : exprseq(vp)
133 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
134 tinfo_key = TINFO_ncmul;
138 // functions overriding virtual functions from bases classes
143 basic * ncmul::duplicate() const
145 debugmsg("ncmul duplicate",LOGLEVEL_ASSIGNMENT);
146 return new ncmul(*this);
149 bool ncmul::info(unsigned inf) const
151 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
154 typedef vector<int> intvector;
156 ex ncmul::expand(unsigned options) const
158 exvector sub_expanded_seq;
159 intvector positions_of_adds;
160 intvector number_of_add_operands;
162 exvector expanded_seq=expandchildren(options);
164 positions_of_adds.resize(expanded_seq.size());
165 number_of_add_operands.resize(expanded_seq.size());
167 int number_of_adds=0;
168 int number_of_expanded_terms=1;
170 unsigned current_position=0;
171 exvector::const_iterator last=expanded_seq.end();
172 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
173 if (is_ex_exactly_of_type((*cit),add)) {
174 positions_of_adds[number_of_adds]=current_position;
175 add const & expanded_addref=ex_to_add(*cit);
176 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
177 number_of_expanded_terms *= expanded_addref.seq.size();
183 if (number_of_adds==0) {
184 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
185 status_flags::expanded);
189 distrseq.reserve(number_of_expanded_terms);
192 k.resize(number_of_adds);
195 for (l=0; l<number_of_adds; l++) {
202 for (l=0; l<number_of_adds; l++) {
203 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
204 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
205 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
207 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
208 status_flags::expanded));
212 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
219 return (new add(distrseq))->setflag(status_flags::dynallocated |
220 status_flags::expanded);
223 int ncmul::degree(symbol const & s) const
226 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
227 deg_sum+=(*cit).degree(s);
232 int ncmul::ldegree(symbol const & s) const
235 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
236 deg_sum+=(*cit).ldegree(s);
241 ex ncmul::coeff(symbol const & s, int const n) const
244 coeffseq.reserve(seq.size());
247 // product of individual coeffs
248 // if a non-zero power of s is found, the resulting product will be 0
249 exvector::const_iterator it=seq.begin();
250 while (it!=seq.end()) {
251 coeffseq.push_back((*it).coeff(s,n));
254 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
257 exvector::const_iterator it=seq.begin();
259 while (it!=seq.end()) {
260 ex c=(*it).coeff(s,n);
262 coeffseq.push_back(c);
265 coeffseq.push_back(*it);
270 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
275 unsigned ncmul::count_factors(ex const & e) const
277 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
278 (is_ex_exactly_of_type(e,ncmul))) {
280 for (int i=0; i<e.nops(); i++) {
281 factors += count_factors(e.op(i));
288 void ncmul::append_factors(exvector & v, ex const & e) const
290 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
291 (is_ex_exactly_of_type(e,ncmul))) {
292 for (int i=0; i<e.nops(); i++) {
293 append_factors(v,e.op(i));
300 typedef vector<unsigned> unsignedvector;
301 typedef vector<exvector> exvectorvector;
303 ex ncmul::eval(int level) const
305 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
306 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
309 // ncmul(...,c1,...,c2,...) ->
310 // *(c1,c2,ncmul(...)) (pull out commutative elements)
311 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
312 // (collect elements of same type)
313 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
314 // the following rule would be nice, but produces a recursion,
315 // which must be trapped by introducing a flag that the sub-ncmuls()
316 // are already evaluated (maybe later...)
317 // ncmul(x1,x2,...,X,y1,y2,...) ->
318 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
319 // (X noncommutative_composite)
321 if ((level==1)&&(flags & status_flags::evaluated)) {
325 exvector evaledseq=evalchildren(level);
327 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
328 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
330 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
331 factors += count_factors(*cit);
335 assocseq.reserve(factors);
336 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
337 append_factors(assocseq,*cit);
341 if (assocseq.size()==1) return *(seq.begin());
344 if (assocseq.size()==0) return exONE();
346 // determine return types
347 unsignedvector rettypes;
348 rettypes.reserve(assocseq.size());
350 unsigned count_commutative=0;
351 unsigned count_noncommutative=0;
352 unsigned count_noncommutative_composite=0;
353 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
354 switch (rettypes[i]=(*cit).return_type()) {
355 case return_types::commutative:
358 case return_types::noncommutative:
359 count_noncommutative++;
361 case return_types::noncommutative_composite:
362 count_noncommutative_composite++;
365 throw(std::logic_error("ncmul::eval(): invalid return type"));
369 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
371 // ncmul(...,c1,...,c2,...) ->
372 // *(c1,c2,ncmul(...)) (pull out commutative elements)
373 if (count_commutative!=0) {
374 exvector commutativeseq;
375 commutativeseq.reserve(count_commutative+1);
376 exvector noncommutativeseq;
377 noncommutativeseq.reserve(assocseq.size()-count_commutative);
378 for (i=0; i<assocseq.size(); ++i) {
379 if (rettypes[i]==return_types::commutative) {
380 commutativeseq.push_back(assocseq[i]);
382 noncommutativeseq.push_back(assocseq[i]);
385 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->
386 setflag(status_flags::dynallocated));
387 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
390 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
391 // (collect elements of same type)
393 if (count_noncommutative_composite==0) {
394 // there are neither commutative nor noncommutative_composite
395 // elements in assocseq
396 GINAC_ASSERT(count_commutative==0);
399 unsignedvector rttinfos;
400 evv.reserve(assocseq.size());
401 rttinfos.reserve(assocseq.size());
403 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
404 unsigned ti=(*cit).return_type_tinfo();
405 // search type in vector of known types
406 for (i=0; i<rttinfos.size(); ++i) {
407 if (ti==rttinfos[i]) {
408 evv[i].push_back(*cit);
412 if (i>=rttinfos.size()) {
414 rttinfos.push_back(ti);
415 evv.push_back(exvector());
416 (*(evv.end()-1)).reserve(assocseq.size());
417 (*(evv.end()-1)).push_back(*cit);
421 #ifdef DO_GINAC_ASSERT
422 GINAC_ASSERT(evv.size()==rttinfos.size());
423 GINAC_ASSERT(evv.size()>0);
425 for (i=0; i<evv.size(); ++i) {
428 GINAC_ASSERT(s==assocseq.size());
429 #endif // def DO_GINAC_ASSERT
431 // if all elements are of same type, simplify the string
433 return evv[0][0].simplify_ncmul(evv[0]);
437 splitseq.reserve(evv.size());
438 for (i=0; i<evv.size(); ++i) {
439 splitseq.push_back((new ncmul(evv[i]))->
440 setflag(status_flags::dynallocated));
443 return (new mul(splitseq))->setflag(status_flags::dynallocated);
446 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
447 status_flags::evaluated);
450 exvector ncmul::get_indices(void) const
452 // return union of indices of factors
454 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
455 exvector subiv=(*cit).get_indices();
456 iv.reserve(iv.size()+subiv.size());
457 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
464 ex ncmul::subs(lst const & ls, lst const & lr) const
466 return ncmul(subschildren(ls, lr));
469 ex ncmul::thisexprseq(exvector const & v) const
471 return (new ncmul(v))->setflag(status_flags::dynallocated);
474 ex ncmul::thisexprseq(exvector * vp) const
476 return (new ncmul(vp))->setflag(status_flags::dynallocated);
481 int ncmul::compare_same_type(basic const & other) const
483 return exprseq::compare_same_type(other);
486 unsigned ncmul::return_type(void) const
489 // ncmul without factors: should not happen, but commutes
490 return return_types::commutative;
493 bool all_commutative=1;
495 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
497 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
498 rt=(*cit).return_type();
499 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
500 if ((rt==return_types::noncommutative)&&(all_commutative)) {
501 // first nc element found, remember position
502 cit_noncommutative_element=cit;
505 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
506 // another nc element found, compare type_infos
507 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
508 // diffent types -> mul is ncc
509 return return_types::noncommutative_composite;
513 // all factors checked
514 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
515 return all_commutative ? return_types::commutative : return_types::noncommutative;
518 unsigned ncmul::return_type_tinfo(void) const
521 // mul without factors: should not happen
524 // return type_info of first noncommutative element
525 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
526 if ((*cit).return_type()==return_types::noncommutative) {
527 return (*cit).return_type_tinfo();
530 // no noncommutative element found, should not happen
535 // new virtual functions which can be overridden by derived classes
541 // non-virtual functions in this class
544 exvector ncmul::expandchildren(unsigned options) const
547 s.reserve(seq.size());
549 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
550 s.push_back((*it).expand(options));
555 exvector const & ncmul::get_factors(void) const
561 // static member variables
566 unsigned ncmul::precedence=50;
573 const ncmul some_ncmul;
574 type_info const & typeid_ncmul=typeid(some_ncmul);
580 ex nonsimplified_ncmul(exvector const & v)
582 return (new ncmul(v))->setflag(status_flags::dynallocated);
585 ex simplified_ncmul(exvector const & v)
589 } else if (v.size()==1) {
592 return (new ncmul(v))->setflag(status_flags::dynallocated |
593 status_flags::evaluated);
596 #ifndef NO_GINAC_NAMESPACE
598 #endif // ndef NO_GINAC_NAMESPACE