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
217 // Sum up degrees of factors
219 exvector::const_iterator i = seq.begin(), end = seq.end();
221 deg_sum += i->degree(s);
227 int ncmul::ldegree(const ex & s) const
229 // Sum up degrees of factors
231 exvector::const_iterator i = seq.begin(), end = seq.end();
233 deg_sum += i->degree(s);
239 ex ncmul::coeff(const ex & s, int n) const
242 coeffseq.reserve(seq.size());
245 // product of individual coeffs
246 // if a non-zero power of s is found, the resulting product will be 0
247 exvector::const_iterator it=seq.begin();
248 while (it!=seq.end()) {
249 coeffseq.push_back((*it).coeff(s,n));
252 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
255 exvector::const_iterator i = seq.begin(), end = seq.end();
256 bool coeff_found = false;
258 ex c = i->coeff(s,n);
260 coeffseq.push_back(*i);
262 coeffseq.push_back(c);
268 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
273 unsigned ncmul::count_factors(const ex & e) const
275 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
276 (is_ex_exactly_of_type(e,ncmul))) {
278 for (unsigned i=0; i<e.nops(); i++)
279 factors += count_factors(e.op(i));
286 void ncmul::append_factors(exvector & v, const ex & e) const
288 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
289 (is_ex_exactly_of_type(e,ncmul))) {
290 for (unsigned i=0; i<e.nops(); i++)
291 append_factors(v,e.op(i));
296 typedef std::vector<unsigned> unsignedvector;
297 typedef std::vector<exvector> exvectorvector;
299 ex ncmul::eval(int level) const
301 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
302 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
305 // ncmul(...,c1,...,c2,...)
306 // *(c1,c2,ncmul(...)) (pull out commutative elements)
307 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
308 // (collect elements of same type)
309 // ncmul(x1,x2,x3,...) -> x::simplify_ncmul(x1,x2,x3,...)
310 // the following rule would be nice, but produces a recursion,
311 // which must be trapped by introducing a flag that the sub-ncmuls()
312 // are already evaluated (maybe later...)
313 // ncmul(x1,x2,...,X,y1,y2,...) ->
314 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
315 // (X noncommutative_composite)
317 if ((level==1) && (flags & status_flags::evaluated)) {
321 exvector evaledseq=evalchildren(level);
323 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
324 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
325 unsigned factors = 0;
326 exvector::const_iterator cit = evaledseq.begin(), citend = evaledseq.end();
327 while (cit != citend)
328 factors += count_factors(*cit++);
331 assocseq.reserve(factors);
332 cit = evaledseq.begin();
333 while (cit != citend)
334 append_factors(assocseq, *cit++);
337 if (assocseq.size()==1) return *(seq.begin());
340 if (assocseq.empty()) return _ex1();
342 // determine return types
343 unsignedvector rettypes;
344 rettypes.reserve(assocseq.size());
346 unsigned count_commutative=0;
347 unsigned count_noncommutative=0;
348 unsigned count_noncommutative_composite=0;
349 cit = assocseq.begin(); citend = assocseq.end();
350 while (cit != citend) {
351 switch (rettypes[i] = cit->return_type()) {
352 case return_types::commutative:
355 case return_types::noncommutative:
356 count_noncommutative++;
358 case return_types::noncommutative_composite:
359 count_noncommutative_composite++;
362 throw(std::logic_error("ncmul::eval(): invalid return type"));
366 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
368 // ncmul(...,c1,...,c2,...) ->
369 // *(c1,c2,ncmul(...)) (pull out commutative elements)
370 if (count_commutative!=0) {
371 exvector commutativeseq;
372 commutativeseq.reserve(count_commutative+1);
373 exvector noncommutativeseq;
374 noncommutativeseq.reserve(assocseq.size()-count_commutative);
375 unsigned num = assocseq.size();
376 for (unsigned i=0; i<num; ++i) {
377 if (rettypes[i]==return_types::commutative)
378 commutativeseq.push_back(assocseq[i]);
380 noncommutativeseq.push_back(assocseq[i]);
382 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
383 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
386 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
387 // (collect elements of same type)
389 if (count_noncommutative_composite==0) {
390 // there are neither commutative nor noncommutative_composite
391 // elements in assocseq
392 GINAC_ASSERT(count_commutative==0);
394 unsigned assoc_num = assocseq.size();
396 unsignedvector rttinfos;
397 evv.reserve(assoc_num);
398 rttinfos.reserve(assoc_num);
400 cit = assocseq.begin(), citend = assocseq.end();
401 while (cit != citend) {
402 unsigned ti = cit->return_type_tinfo();
403 unsigned rtt_num = rttinfos.size();
404 // search type in vector of known types
405 for (i=0; i<rtt_num; ++i) {
406 if (ti == rttinfos[i]) {
407 evv[i].push_back(*cit);
413 rttinfos.push_back(ti);
414 evv.push_back(exvector());
415 (evv.end()-1)->reserve(assoc_num);
416 (evv.end()-1)->push_back(*cit);
421 unsigned evv_num = evv.size();
422 #ifdef DO_GINAC_ASSERT
423 GINAC_ASSERT(evv_num == rttinfos.size());
424 GINAC_ASSERT(evv_num > 0);
426 for (i=0; i<evv_num; ++i)
428 GINAC_ASSERT(s == assoc_num);
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]);
436 splitseq.reserve(evv_num);
437 for (i=0; i<evv_num; ++i)
438 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
440 return (new mul(splitseq))->setflag(status_flags::dynallocated);
443 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
444 status_flags::evaluated);
447 ex ncmul::evalm(void) const
449 // Evaluate children first
450 exvector *s = new exvector;
451 s->reserve(seq.size());
452 exvector::const_iterator it = seq.begin(), itend = seq.end();
453 while (it != itend) {
454 s->push_back(it->evalm());
458 // If there are only matrices, simply multiply them
459 it = s->begin(); itend = s->end();
460 if (is_ex_of_type(*it, matrix)) {
461 matrix prod(ex_to<matrix>(*it));
463 while (it != itend) {
464 if (!is_ex_of_type(*it, matrix))
466 prod = prod.mul(ex_to<matrix>(*it));
474 return (new ncmul(s))->setflag(status_flags::dynallocated);
477 ex ncmul::thisexprseq(const exvector & v) const
479 return (new ncmul(v))->setflag(status_flags::dynallocated);
482 ex ncmul::thisexprseq(exvector * vp) const
484 return (new ncmul(vp))->setflag(status_flags::dynallocated);
489 /** Implementation of ex::diff() for a non-commutative product. It applies
492 ex ncmul::derivative(const symbol & s) const
494 unsigned num = seq.size();
498 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
499 exvector ncmulseq = seq;
500 for (unsigned i=0; i<num; ++i) {
501 ex e = seq[i].diff(s);
503 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
506 return (new add(addseq))->setflag(status_flags::dynallocated);
509 int ncmul::compare_same_type(const basic & other) const
511 return inherited::compare_same_type(other);
514 unsigned ncmul::return_type(void) const
517 return return_types::commutative;
519 bool all_commutative = true;
520 exvector::const_iterator noncommutative_element; // point to first found nc element
522 exvector::const_iterator i = seq.begin(), end = seq.end();
524 unsigned rt = i->return_type();
525 if (rt == return_types::noncommutative_composite)
526 return rt; // one ncc -> mul also ncc
527 if ((rt == return_types::noncommutative) && (all_commutative)) {
528 // first nc element found, remember position
529 noncommutative_element = i;
530 all_commutative = false;
532 if ((rt == return_types::noncommutative) && (!all_commutative)) {
533 // another nc element found, compare type_infos
534 if (noncommutative_element->return_type_tinfo() != i->return_type_tinfo()) {
535 // diffent types -> mul is ncc
536 return return_types::noncommutative_composite;
541 // all factors checked
542 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
543 return all_commutative ? return_types::commutative : return_types::noncommutative;
546 unsigned ncmul::return_type_tinfo(void) const
551 // return type_info of first noncommutative element
552 exvector::const_iterator i = seq.begin(), end = seq.end();
554 if (i->return_type() == return_types::noncommutative)
555 return i->return_type_tinfo();
559 // no noncommutative element found, should not happen
564 // new virtual functions which can be overridden by derived classes
570 // non-virtual functions in this class
573 exvector ncmul::expandchildren(unsigned options) const
576 s.reserve(seq.size());
577 exvector::const_iterator it = seq.begin(), itend = seq.end();
578 while (it != itend) {
579 s.push_back(it->expand(options));
585 const exvector & ncmul::get_factors(void) const
594 ex nonsimplified_ncmul(const exvector & v)
596 return (new ncmul(v))->setflag(status_flags::dynallocated);
599 ex simplified_ncmul(const exvector & v)
603 else if (v.size() == 1)
606 return (new ncmul(v))->setflag(status_flags::dynallocated |
607 status_flags::evaluated);