3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2001 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
37 GINAC_IMPLEMENT_REGISTERED_CLASS(ncmul, exprseq)
40 // default constructor, destructor, copy constructor assignment operator and helpers
47 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
48 tinfo_key = TINFO_ncmul;
53 void ncmul::copy(const ncmul & other)
55 inherited::copy(other);
58 void ncmul::destroy(bool call_parent)
60 if (call_parent) inherited::destroy(call_parent);
69 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
71 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
72 tinfo_key = TINFO_ncmul;
75 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
77 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
78 tinfo_key = TINFO_ncmul;
81 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
82 const ex & f4) : inherited(f1,f2,f3,f4)
84 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
85 tinfo_key = TINFO_ncmul;
88 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
89 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
91 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
92 tinfo_key = TINFO_ncmul;
95 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
96 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
98 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
99 tinfo_key = TINFO_ncmul;
102 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
104 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
105 tinfo_key = TINFO_ncmul;
108 ncmul::ncmul(exvector * vp) : inherited(vp)
110 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
111 tinfo_key = TINFO_ncmul;
118 /** Construct object from archive_node. */
119 ncmul::ncmul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
121 debugmsg("ncmul constructor from archive_node", LOGLEVEL_CONSTRUCT);
124 /** Unarchive the object. */
125 ex ncmul::unarchive(const archive_node &n, const lst &sym_lst)
127 return (new ncmul(n, sym_lst))->setflag(status_flags::dynallocated);
130 /** Archive the object. */
131 void ncmul::archive(archive_node &n) const
133 inherited::archive(n);
138 // functions overriding virtual functions from bases classes
143 void ncmul::print(std::ostream & os, unsigned upper_precedence) const
145 debugmsg("ncmul print",LOGLEVEL_PRINT);
146 printseq(os,'(','*',')',precedence,upper_precedence);
149 void ncmul::printraw(std::ostream & os) const
151 debugmsg("ncmul printraw",LOGLEVEL_PRINT);
153 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
154 (*it).bp->printraw(os);
157 os << ",hash=" << hashvalue << ",flags=" << flags;
161 void ncmul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
163 debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
164 exvector::const_iterator it;
165 exvector::const_iterator itend = seq.end()-1;
167 for (it=seq.begin(); it!=itend; ++it) {
168 (*it).bp->printcsrc(os,precedence);
171 (*it).bp->printcsrc(os,precedence);
175 bool ncmul::info(unsigned inf) const
177 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
180 typedef std::vector<int> intvector;
182 ex ncmul::expand(unsigned options) const
184 exvector sub_expanded_seq;
185 intvector positions_of_adds;
186 intvector number_of_add_operands;
188 exvector expanded_seq=expandchildren(options);
190 positions_of_adds.resize(expanded_seq.size());
191 number_of_add_operands.resize(expanded_seq.size());
193 int number_of_adds=0;
194 int number_of_expanded_terms=1;
196 unsigned current_position=0;
197 exvector::const_iterator last=expanded_seq.end();
198 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
199 if (is_ex_exactly_of_type((*cit),add)) {
200 positions_of_adds[number_of_adds]=current_position;
201 const add & expanded_addref=ex_to_add(*cit);
202 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
203 number_of_expanded_terms *= expanded_addref.seq.size();
209 if (number_of_adds==0) {
210 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
211 status_flags::expanded);
215 distrseq.reserve(number_of_expanded_terms);
218 k.resize(number_of_adds);
221 for (l=0; l<number_of_adds; l++) {
228 for (l=0; l<number_of_adds; l++) {
229 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
230 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
231 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
233 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
234 status_flags::expanded));
238 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
245 return (new add(distrseq))->setflag(status_flags::dynallocated |
246 status_flags::expanded);
249 int ncmul::degree(const symbol & s) const
252 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
253 deg_sum+=(*cit).degree(s);
258 int ncmul::ldegree(const symbol & s) const
261 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
262 deg_sum+=(*cit).ldegree(s);
267 ex ncmul::coeff(const symbol & s, int n) const
270 coeffseq.reserve(seq.size());
273 // product of individual coeffs
274 // if a non-zero power of s is found, the resulting product will be 0
275 exvector::const_iterator it=seq.begin();
276 while (it!=seq.end()) {
277 coeffseq.push_back((*it).coeff(s,n));
280 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
283 exvector::const_iterator it=seq.begin();
285 while (it!=seq.end()) {
286 ex c=(*it).coeff(s,n);
288 coeffseq.push_back(c);
291 coeffseq.push_back(*it);
296 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
301 unsigned ncmul::count_factors(const ex & e) const
303 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
304 (is_ex_exactly_of_type(e,ncmul))) {
306 for (unsigned i=0; i<e.nops(); i++)
307 factors += count_factors(e.op(i));
314 void ncmul::append_factors(exvector & v, const ex & e) const
316 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
317 (is_ex_exactly_of_type(e,ncmul))) {
318 for (unsigned i=0; i<e.nops(); i++)
319 append_factors(v,e.op(i));
326 typedef std::vector<unsigned> unsignedvector;
327 typedef std::vector<exvector> exvectorvector;
329 ex ncmul::eval(int level) const
331 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
332 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
335 // ncmul(...,c1,...,c2,...)
336 // *(c1,c2,ncmul(...)) (pull out commutative elements)
337 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
338 // (collect elements of same type)
339 // ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
340 // the following rule would be nice, but produces a recursion,
341 // which must be trapped by introducing a flag that the sub-ncmuls()
342 // are already evaluated (maybe later...)
343 // ncmul(x1,x2,...,X,y1,y2,...) ->
344 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
345 // (X noncommutative_composite)
347 if ((level==1) && (flags & status_flags::evaluated)) {
351 exvector evaledseq=evalchildren(level);
353 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
354 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
356 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit)
357 factors += count_factors(*cit);
360 assocseq.reserve(factors);
361 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit)
362 append_factors(assocseq,*cit);
365 if (assocseq.size()==1) return *(seq.begin());
368 if (assocseq.size()==0) return _ex1();
370 // determine return types
371 unsignedvector rettypes;
372 rettypes.reserve(assocseq.size());
374 unsigned count_commutative=0;
375 unsigned count_noncommutative=0;
376 unsigned count_noncommutative_composite=0;
377 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
378 switch (rettypes[i]=(*cit).return_type()) {
379 case return_types::commutative:
382 case return_types::noncommutative:
383 count_noncommutative++;
385 case return_types::noncommutative_composite:
386 count_noncommutative_composite++;
389 throw(std::logic_error("ncmul::eval(): invalid return type"));
393 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
395 // ncmul(...,c1,...,c2,...) ->
396 // *(c1,c2,ncmul(...)) (pull out commutative elements)
397 if (count_commutative!=0) {
398 exvector commutativeseq;
399 commutativeseq.reserve(count_commutative+1);
400 exvector noncommutativeseq;
401 noncommutativeseq.reserve(assocseq.size()-count_commutative);
402 for (i=0; i<assocseq.size(); ++i) {
403 if (rettypes[i]==return_types::commutative)
404 commutativeseq.push_back(assocseq[i]);
406 noncommutativeseq.push_back(assocseq[i]);
408 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
409 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
412 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
413 // (collect elements of same type)
415 if (count_noncommutative_composite==0) {
416 // there are neither commutative nor noncommutative_composite
417 // elements in assocseq
418 GINAC_ASSERT(count_commutative==0);
421 unsignedvector rttinfos;
422 evv.reserve(assocseq.size());
423 rttinfos.reserve(assocseq.size());
425 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
426 unsigned ti=(*cit).return_type_tinfo();
427 // search type in vector of known types
428 for (i=0; i<rttinfos.size(); ++i) {
429 if (ti==rttinfos[i]) {
430 evv[i].push_back(*cit);
434 if (i>=rttinfos.size()) {
436 rttinfos.push_back(ti);
437 evv.push_back(exvector());
438 (*(evv.end()-1)).reserve(assocseq.size());
439 (*(evv.end()-1)).push_back(*cit);
443 #ifdef DO_GINAC_ASSERT
444 GINAC_ASSERT(evv.size()==rttinfos.size());
445 GINAC_ASSERT(evv.size()>0);
447 for (i=0; i<evv.size(); ++i) {
450 GINAC_ASSERT(s==assocseq.size());
451 #endif // def DO_GINAC_ASSERT
453 // if all elements are of same type, simplify the string
455 return evv[0][0].simplify_ncmul(evv[0]);
458 splitseq.reserve(evv.size());
459 for (i=0; i<evv.size(); ++i) {
460 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
463 return (new mul(splitseq))->setflag(status_flags::dynallocated);
466 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
467 status_flags::evaluated);
470 ex ncmul::subs(const lst & ls, const lst & lr) const
472 return ncmul(subschildren(ls, lr));
475 ex ncmul::thisexprseq(const exvector & v) const
477 return (new ncmul(v))->setflag(status_flags::dynallocated);
480 ex ncmul::thisexprseq(exvector * vp) const
482 return (new ncmul(vp))->setflag(status_flags::dynallocated);
487 /** Implementation of ex::diff() for a non-commutative product. It always returns 0.
489 ex ncmul::derivative(const symbol & s) const
494 int ncmul::compare_same_type(const basic & other) const
496 return inherited::compare_same_type(other);
499 unsigned ncmul::return_type(void) const
502 // ncmul without factors: should not happen, but commutes
503 return return_types::commutative;
506 bool all_commutative=1;
508 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
510 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
511 rt=(*cit).return_type();
512 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
513 if ((rt==return_types::noncommutative)&&(all_commutative)) {
514 // first nc element found, remember position
515 cit_noncommutative_element=cit;
518 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
519 // another nc element found, compare type_infos
520 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
521 // diffent types -> mul is ncc
522 return return_types::noncommutative_composite;
526 // all factors checked
527 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
528 return all_commutative ? return_types::commutative : return_types::noncommutative;
531 unsigned ncmul::return_type_tinfo(void) const
534 // mul without factors: should not happen
537 // return type_info of first noncommutative element
538 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
539 if ((*cit).return_type()==return_types::noncommutative) {
540 return (*cit).return_type_tinfo();
543 // no noncommutative element found, should not happen
548 // new virtual functions which can be overridden by derived classes
554 // non-virtual functions in this class
557 exvector ncmul::expandchildren(unsigned options) const
560 s.reserve(seq.size());
562 for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
563 s.push_back((*it).expand(options));
568 const exvector & ncmul::get_factors(void) const
574 // static member variables
579 unsigned ncmul::precedence = 50;
585 ex nonsimplified_ncmul(const exvector & v)
587 return (new ncmul(v))->setflag(status_flags::dynallocated);
590 ex simplified_ncmul(const exvector & v)
594 } else if (v.size()==1) {
597 return (new ncmul(v))->setflag(status_flags::dynallocated |
598 status_flags::evaluated);