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 void ncmul::print(ostream & os, unsigned upper_precedence) const
151 debugmsg("ncmul print",LOGLEVEL_PRINT);
152 printseq(os,'(','%',')',precedence,upper_precedence);
155 void ncmul::printraw(ostream & os) const
157 debugmsg("ncmul printraw",LOGLEVEL_PRINT);
160 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
161 (*it).bp->printraw(os);
164 os << ",hash=" << hashvalue << ",flags=" << flags;
168 void ncmul::printcsrc(ostream & os, unsigned upper_precedence) const
170 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
171 exvector::const_iterator it;
172 exvector::const_iterator itend = seq.end()-1;
174 for (it=seq.begin(); it!=itend; ++it) {
175 (*it).bp->printcsrc(os,precedence);
178 (*it).bp->printcsrc(os,precedence);
182 bool ncmul::info(unsigned inf) const
184 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
187 typedef vector<int> intvector;
189 ex ncmul::expand(unsigned options) const
191 exvector sub_expanded_seq;
192 intvector positions_of_adds;
193 intvector number_of_add_operands;
195 exvector expanded_seq=expandchildren(options);
197 positions_of_adds.resize(expanded_seq.size());
198 number_of_add_operands.resize(expanded_seq.size());
200 int number_of_adds=0;
201 int number_of_expanded_terms=1;
203 unsigned current_position=0;
204 exvector::const_iterator last=expanded_seq.end();
205 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
206 if (is_ex_exactly_of_type((*cit),add)) {
207 positions_of_adds[number_of_adds]=current_position;
208 add const & expanded_addref=ex_to_add(*cit);
209 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
210 number_of_expanded_terms *= expanded_addref.seq.size();
216 if (number_of_adds==0) {
217 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
218 status_flags::expanded);
222 distrseq.reserve(number_of_expanded_terms);
225 k.resize(number_of_adds);
228 for (l=0; l<number_of_adds; l++) {
235 for (l=0; l<number_of_adds; l++) {
236 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
237 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
238 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
240 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
241 status_flags::expanded));
245 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
252 return (new add(distrseq))->setflag(status_flags::dynallocated |
253 status_flags::expanded);
256 int ncmul::degree(symbol const & s) const
259 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
260 deg_sum+=(*cit).degree(s);
265 int ncmul::ldegree(symbol const & s) const
268 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
269 deg_sum+=(*cit).ldegree(s);
274 ex ncmul::coeff(symbol const & s, int const n) const
277 coeffseq.reserve(seq.size());
280 // product of individual coeffs
281 // if a non-zero power of s is found, the resulting product will be 0
282 exvector::const_iterator it=seq.begin();
283 while (it!=seq.end()) {
284 coeffseq.push_back((*it).coeff(s,n));
287 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
290 exvector::const_iterator it=seq.begin();
292 while (it!=seq.end()) {
293 ex c=(*it).coeff(s,n);
295 coeffseq.push_back(c);
298 coeffseq.push_back(*it);
303 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
308 unsigned ncmul::count_factors(ex const & e) const
310 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
311 (is_ex_exactly_of_type(e,ncmul))) {
313 for (int i=0; i<e.nops(); i++) {
314 factors += count_factors(e.op(i));
321 void ncmul::append_factors(exvector & v, ex const & e) const
323 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
324 (is_ex_exactly_of_type(e,ncmul))) {
325 for (int i=0; i<e.nops(); i++) {
326 append_factors(v,e.op(i));
333 typedef vector<unsigned> unsignedvector;
334 typedef vector<exvector> exvectorvector;
336 ex ncmul::eval(int level) const
338 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
339 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
342 // ncmul(...,c1,...,c2,...) ->
343 // *(c1,c2,ncmul(...)) (pull out commutative elements)
344 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
345 // (collect elements of same type)
346 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
347 // the following rule would be nice, but produces a recursion,
348 // which must be trapped by introducing a flag that the sub-ncmuls()
349 // are already evaluated (maybe later...)
350 // ncmul(x1,x2,...,X,y1,y2,...) ->
351 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
352 // (X noncommutative_composite)
354 if ((level==1)&&(flags & status_flags::evaluated)) {
358 exvector evaledseq=evalchildren(level);
360 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
361 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
363 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
364 factors += count_factors(*cit);
368 assocseq.reserve(factors);
369 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit) {
370 append_factors(assocseq,*cit);
374 if (assocseq.size()==1) return *(seq.begin());
377 if (assocseq.size()==0) return exONE();
379 // determine return types
380 unsignedvector rettypes;
381 rettypes.reserve(assocseq.size());
383 unsigned count_commutative=0;
384 unsigned count_noncommutative=0;
385 unsigned count_noncommutative_composite=0;
386 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
387 switch (rettypes[i]=(*cit).return_type()) {
388 case return_types::commutative:
391 case return_types::noncommutative:
392 count_noncommutative++;
394 case return_types::noncommutative_composite:
395 count_noncommutative_composite++;
398 throw(std::logic_error("ncmul::eval(): invalid return type"));
402 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
404 // ncmul(...,c1,...,c2,...) ->
405 // *(c1,c2,ncmul(...)) (pull out commutative elements)
406 if (count_commutative!=0) {
407 exvector commutativeseq;
408 commutativeseq.reserve(count_commutative+1);
409 exvector noncommutativeseq;
410 noncommutativeseq.reserve(assocseq.size()-count_commutative);
411 for (i=0; i<assocseq.size(); ++i) {
412 if (rettypes[i]==return_types::commutative) {
413 commutativeseq.push_back(assocseq[i]);
415 noncommutativeseq.push_back(assocseq[i]);
418 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->
419 setflag(status_flags::dynallocated));
420 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
423 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
424 // (collect elements of same type)
426 if (count_noncommutative_composite==0) {
427 // there are neither commutative nor noncommutative_composite
428 // elements in assocseq
429 GINAC_ASSERT(count_commutative==0);
432 unsignedvector rttinfos;
433 evv.reserve(assocseq.size());
434 rttinfos.reserve(assocseq.size());
436 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
437 unsigned ti=(*cit).return_type_tinfo();
438 // search type in vector of known types
439 for (i=0; i<rttinfos.size(); ++i) {
440 if (ti==rttinfos[i]) {
441 evv[i].push_back(*cit);
445 if (i>=rttinfos.size()) {
447 rttinfos.push_back(ti);
448 evv.push_back(exvector());
449 (*(evv.end()-1)).reserve(assocseq.size());
450 (*(evv.end()-1)).push_back(*cit);
454 #ifdef DO_GINAC_ASSERT
455 GINAC_ASSERT(evv.size()==rttinfos.size());
456 GINAC_ASSERT(evv.size()>0);
458 for (i=0; i<evv.size(); ++i) {
461 GINAC_ASSERT(s==assocseq.size());
462 #endif // def DO_GINAC_ASSERT
464 // if all elements are of same type, simplify the string
466 return evv[0][0].simplify_ncmul(evv[0]);
470 splitseq.reserve(evv.size());
471 for (i=0; i<evv.size(); ++i) {
472 splitseq.push_back((new ncmul(evv[i]))->
473 setflag(status_flags::dynallocated));
476 return (new mul(splitseq))->setflag(status_flags::dynallocated);
479 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
480 status_flags::evaluated);
483 exvector ncmul::get_indices(void) const
485 // return union of indices of factors
487 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
488 exvector subiv=(*cit).get_indices();
489 iv.reserve(iv.size()+subiv.size());
490 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
497 ex ncmul::subs(lst const & ls, lst const & lr) const
499 return ncmul(subschildren(ls, lr));
502 ex ncmul::thisexprseq(exvector const & v) const
504 return (new ncmul(v))->setflag(status_flags::dynallocated);
507 ex ncmul::thisexprseq(exvector * vp) const
509 return (new ncmul(vp))->setflag(status_flags::dynallocated);
514 int ncmul::compare_same_type(basic const & other) const
516 return exprseq::compare_same_type(other);
519 unsigned ncmul::return_type(void) const
522 // ncmul without factors: should not happen, but commutes
523 return return_types::commutative;
526 bool all_commutative=1;
528 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
530 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
531 rt=(*cit).return_type();
532 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
533 if ((rt==return_types::noncommutative)&&(all_commutative)) {
534 // first nc element found, remember position
535 cit_noncommutative_element=cit;
538 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
539 // another nc element found, compare type_infos
540 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
541 // diffent types -> mul is ncc
542 return return_types::noncommutative_composite;
546 // all factors checked
547 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
548 return all_commutative ? return_types::commutative : return_types::noncommutative;
551 unsigned ncmul::return_type_tinfo(void) const
554 // mul without factors: should not happen
557 // return type_info of first noncommutative element
558 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
559 if ((*cit).return_type()==return_types::noncommutative) {
560 return (*cit).return_type_tinfo();
563 // no noncommutative element found, should not happen
568 // new virtual functions which can be overridden by derived classes
574 // non-virtual functions in this class
577 exvector ncmul::expandchildren(unsigned options) const
580 s.reserve(seq.size());
582 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
583 s.push_back((*it).expand(options));
588 exvector const & ncmul::get_factors(void) const
594 // static member variables
599 unsigned ncmul::precedence=50;
606 const ncmul some_ncmul;
607 type_info const & typeid_ncmul=typeid(some_ncmul);
613 ex nonsimplified_ncmul(exvector const & v)
615 return (new ncmul(v))->setflag(status_flags::dynallocated);
618 ex simplified_ncmul(exvector const & v)
622 } else if (v.size()==1) {
625 return (new ncmul(v))->setflag(status_flags::dynallocated |
626 status_flags::evaluated);
629 #ifndef NO_GINAC_NAMESPACE
631 #endif // ndef NO_GINAC_NAMESPACE