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
39 GINAC_IMPLEMENT_REGISTERED_CLASS(ncmul, exprseq)
42 // default constructor, destructor, copy constructor assignment operator and helpers
47 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
48 tinfo_key = TINFO_ncmul;
52 DEFAULT_DESTROY(ncmul)
60 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
62 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
63 tinfo_key = TINFO_ncmul;
66 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
68 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
69 tinfo_key = TINFO_ncmul;
72 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
73 const ex & f4) : inherited(f1,f2,f3,f4)
75 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
76 tinfo_key = TINFO_ncmul;
79 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
80 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
82 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
83 tinfo_key = TINFO_ncmul;
86 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
87 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
89 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
90 tinfo_key = TINFO_ncmul;
93 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
95 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
96 tinfo_key = TINFO_ncmul;
99 ncmul::ncmul(exvector * vp) : inherited(vp)
101 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
102 tinfo_key = TINFO_ncmul;
109 DEFAULT_ARCHIVING(ncmul)
112 // functions overriding virtual functions from bases classes
117 void ncmul::print(const print_context & c, unsigned level) const
119 debugmsg("ncmul print", LOGLEVEL_PRINT);
121 if (is_of_type(c, print_tree)) {
123 inherited::print(c, level);
125 } else if (is_of_type(c, print_csrc)) {
128 exvector::const_iterator it = seq.begin(), itend = seq.end()-1;
129 while (it != itend) {
130 it->print(c, precedence());
134 it->print(c, precedence());
138 printseq(c, '(', '*', ')', precedence(), level);
141 bool ncmul::info(unsigned inf) const
143 throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
146 typedef std::vector<int> intvector;
148 ex ncmul::expand(unsigned options) const
150 exvector sub_expanded_seq;
151 intvector positions_of_adds;
152 intvector number_of_add_operands;
154 exvector expanded_seq=expandchildren(options);
156 positions_of_adds.resize(expanded_seq.size());
157 number_of_add_operands.resize(expanded_seq.size());
159 int number_of_adds=0;
160 int number_of_expanded_terms=1;
162 unsigned current_position=0;
163 exvector::const_iterator last=expanded_seq.end();
164 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
165 if (is_ex_exactly_of_type((*cit),add)) {
166 positions_of_adds[number_of_adds]=current_position;
167 const add & expanded_addref=ex_to<add>(*cit);
168 number_of_add_operands[number_of_adds]=expanded_addref.seq.size();
169 number_of_expanded_terms *= expanded_addref.seq.size();
175 if (number_of_adds==0) {
176 return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
177 status_flags::expanded);
181 distrseq.reserve(number_of_expanded_terms);
184 k.resize(number_of_adds);
187 for (l=0; l<number_of_adds; l++) {
194 for (l=0; l<number_of_adds; l++) {
195 GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
196 const add & addref=ex_to<add>(expanded_seq[positions_of_adds[l]]);
197 term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
199 distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
200 status_flags::expanded));
204 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
211 return (new add(distrseq))->setflag(status_flags::dynallocated |
212 status_flags::expanded);
215 int ncmul::degree(const ex & s) const
218 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
219 deg_sum+=(*cit).degree(s);
224 int ncmul::ldegree(const ex & s) const
227 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
228 deg_sum+=(*cit).ldegree(s);
233 ex ncmul::coeff(const ex & s, int n) const
236 coeffseq.reserve(seq.size());
239 // product of individual coeffs
240 // if a non-zero power of s is found, the resulting product will be 0
241 exvector::const_iterator it=seq.begin();
242 while (it!=seq.end()) {
243 coeffseq.push_back((*it).coeff(s,n));
246 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
249 exvector::const_iterator it=seq.begin();
251 while (it!=seq.end()) {
252 ex c=(*it).coeff(s,n);
254 coeffseq.push_back(c);
257 coeffseq.push_back(*it);
262 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
267 unsigned ncmul::count_factors(const ex & e) const
269 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
270 (is_ex_exactly_of_type(e,ncmul))) {
272 for (unsigned i=0; i<e.nops(); i++)
273 factors += count_factors(e.op(i));
280 void ncmul::append_factors(exvector & v, const ex & e) const
282 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
283 (is_ex_exactly_of_type(e,ncmul))) {
284 for (unsigned i=0; i<e.nops(); i++)
285 append_factors(v,e.op(i));
292 typedef std::vector<unsigned> unsignedvector;
293 typedef std::vector<exvector> exvectorvector;
295 ex ncmul::eval(int level) const
297 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
298 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
301 // ncmul(...,c1,...,c2,...)
302 // *(c1,c2,ncmul(...)) (pull out commutative elements)
303 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
304 // (collect elements of same type)
305 // ncmul(x1,x2,x3,...) -> x::simplify_ncmul(x1,x2,x3,...)
306 // the following rule would be nice, but produces a recursion,
307 // which must be trapped by introducing a flag that the sub-ncmuls()
308 // are already evaluated (maybe later...)
309 // ncmul(x1,x2,...,X,y1,y2,...) ->
310 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
311 // (X noncommutative_composite)
313 if ((level==1) && (flags & status_flags::evaluated)) {
317 exvector evaledseq=evalchildren(level);
319 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
320 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
322 for (exvector::const_iterator cit=evaledseq.begin(); cit!=evaledseq.end(); ++cit)
323 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);
331 if (assocseq.size()==1) return *(seq.begin());
334 if (assocseq.size()==0) return _ex1();
336 // determine return types
337 unsignedvector rettypes;
338 rettypes.reserve(assocseq.size());
340 unsigned count_commutative=0;
341 unsigned count_noncommutative=0;
342 unsigned count_noncommutative_composite=0;
343 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
344 switch (rettypes[i]=(*cit).return_type()) {
345 case return_types::commutative:
348 case return_types::noncommutative:
349 count_noncommutative++;
351 case return_types::noncommutative_composite:
352 count_noncommutative_composite++;
355 throw(std::logic_error("ncmul::eval(): invalid return type"));
359 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
361 // ncmul(...,c1,...,c2,...) ->
362 // *(c1,c2,ncmul(...)) (pull out commutative elements)
363 if (count_commutative!=0) {
364 exvector commutativeseq;
365 commutativeseq.reserve(count_commutative+1);
366 exvector noncommutativeseq;
367 noncommutativeseq.reserve(assocseq.size()-count_commutative);
368 for (i=0; i<assocseq.size(); ++i) {
369 if (rettypes[i]==return_types::commutative)
370 commutativeseq.push_back(assocseq[i]);
372 noncommutativeseq.push_back(assocseq[i]);
374 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
375 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
378 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
379 // (collect elements of same type)
381 if (count_noncommutative_composite==0) {
382 // there are neither commutative nor noncommutative_composite
383 // elements in assocseq
384 GINAC_ASSERT(count_commutative==0);
387 unsignedvector rttinfos;
388 evv.reserve(assocseq.size());
389 rttinfos.reserve(assocseq.size());
391 for (exvector::const_iterator cit=assocseq.begin(); cit!=assocseq.end(); ++cit) {
392 unsigned ti=(*cit).return_type_tinfo();
393 // search type in vector of known types
394 for (i=0; i<rttinfos.size(); ++i) {
395 if (ti==rttinfos[i]) {
396 evv[i].push_back(*cit);
400 if (i>=rttinfos.size()) {
402 rttinfos.push_back(ti);
403 evv.push_back(exvector());
404 (*(evv.end()-1)).reserve(assocseq.size());
405 (*(evv.end()-1)).push_back(*cit);
409 #ifdef DO_GINAC_ASSERT
410 GINAC_ASSERT(evv.size()==rttinfos.size());
411 GINAC_ASSERT(evv.size()>0);
413 for (i=0; i<evv.size(); ++i) {
416 GINAC_ASSERT(s==assocseq.size());
417 #endif // def DO_GINAC_ASSERT
419 // if all elements are of same type, simplify the string
421 return evv[0][0].simplify_ncmul(evv[0]);
424 splitseq.reserve(evv.size());
425 for (i=0; i<evv.size(); ++i) {
426 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
429 return (new mul(splitseq))->setflag(status_flags::dynallocated);
432 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
433 status_flags::evaluated);
436 ex ncmul::evalm(void) const
438 // Evaluate children first
439 exvector *s = new exvector;
440 s->reserve(seq.size());
441 exvector::const_iterator it = seq.begin(), itend = seq.end();
442 while (it != itend) {
443 s->push_back(it->evalm());
447 // If there are only matrices, simply multiply them
448 it = s->begin(); itend = s->end();
449 if (is_ex_of_type(*it, matrix)) {
450 matrix prod(ex_to<matrix>(*it));
452 while (it != itend) {
453 if (!is_ex_of_type(*it, matrix))
455 prod = prod.mul(ex_to<matrix>(*it));
463 return (new ncmul(s))->setflag(status_flags::dynallocated);
466 ex ncmul::thisexprseq(const exvector & v) const
468 return (new ncmul(v))->setflag(status_flags::dynallocated);
471 ex ncmul::thisexprseq(exvector * vp) const
473 return (new ncmul(vp))->setflag(status_flags::dynallocated);
478 /** Implementation of ex::diff() for a non-commutative product. It applies
481 ex ncmul::derivative(const symbol & s) const
484 addseq.reserve(seq.size());
486 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
487 for (unsigned i=0; i!=seq.size(); ++i) {
488 exvector ncmulseq = seq;
489 ncmulseq[i] = seq[i].diff(s);
490 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
492 return (new add(addseq))->setflag(status_flags::dynallocated);
495 int ncmul::compare_same_type(const basic & other) const
497 return inherited::compare_same_type(other);
500 unsigned ncmul::return_type(void) const
503 // ncmul without factors: should not happen, but commutes
504 return return_types::commutative;
507 bool all_commutative=1;
509 exvector::const_iterator cit_noncommutative_element; // point to first found nc element
511 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
512 rt=(*cit).return_type();
513 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
514 if ((rt==return_types::noncommutative)&&(all_commutative)) {
515 // first nc element found, remember position
516 cit_noncommutative_element=cit;
519 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
520 // another nc element found, compare type_infos
521 if ((*cit_noncommutative_element).return_type_tinfo()!=(*cit).return_type_tinfo()) {
522 // diffent types -> mul is ncc
523 return return_types::noncommutative_composite;
527 // all factors checked
528 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
529 return all_commutative ? return_types::commutative : return_types::noncommutative;
532 unsigned ncmul::return_type_tinfo(void) const
535 // mul without factors: should not happen
538 // return type_info of first noncommutative element
539 for (exvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
540 if ((*cit).return_type()==return_types::noncommutative) {
541 return (*cit).return_type_tinfo();
544 // no noncommutative element found, should not happen
549 // new virtual functions which can be overridden by derived classes
555 // non-virtual functions in this class
558 exvector ncmul::expandchildren(unsigned options) const
561 s.reserve(seq.size());
562 exvector::const_iterator it = seq.begin(), itend = seq.end();
563 while (it != itend) {
564 s.push_back(it->expand(options));
570 const exvector & ncmul::get_factors(void) const
579 ex nonsimplified_ncmul(const exvector & v)
581 return (new ncmul(v))->setflag(status_flags::dynallocated);
584 ex simplified_ncmul(const exvector & v)
588 } else if (v.size()==1) {
591 return (new ncmul(v))->setflag(status_flags::dynallocated |
592 status_flags::evaluated);