3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2004 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_OPT(ncmul, exprseq,
38 print_func<print_context>(&ncmul::do_print).
39 print_func<print_tree>(&ncmul::do_print_tree).
40 print_func<print_csrc>(&ncmul::do_print_csrc).
41 print_func<print_python_repr>(&ncmul::do_print_csrc))
45 // default constructor
50 tinfo_key = TINFO_ncmul;
59 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
61 tinfo_key = TINFO_ncmul;
64 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
66 tinfo_key = TINFO_ncmul;
69 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
70 const ex & f4) : inherited(f1,f2,f3,f4)
72 tinfo_key = TINFO_ncmul;
75 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
76 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
78 tinfo_key = TINFO_ncmul;
81 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
82 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
84 tinfo_key = TINFO_ncmul;
87 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
89 tinfo_key = TINFO_ncmul;
92 ncmul::ncmul(std::auto_ptr<exvector> vp) : inherited(vp)
94 tinfo_key = TINFO_ncmul;
101 DEFAULT_ARCHIVING(ncmul)
104 // functions overriding virtual functions from base classes
109 void ncmul::do_print(const print_context & c, unsigned level) const
111 printseq(c, '(', '*', ')', precedence(), level);
114 void ncmul::do_print_csrc(const print_context & c, unsigned level) const
117 printseq(c, '(', ',', ')', precedence(), precedence());
120 bool ncmul::info(unsigned inf) const
122 return inherited::info(inf);
125 typedef std::vector<int> intvector;
127 ex ncmul::expand(unsigned options) const
129 // First, expand the children
130 exvector expanded_seq = expandchildren(options);
132 // Now, look for all the factors that are sums and remember their
133 // position and number of terms.
134 intvector positions_of_adds(expanded_seq.size());
135 intvector number_of_add_operands(expanded_seq.size());
137 size_t number_of_adds = 0;
138 size_t number_of_expanded_terms = 1;
140 size_t current_position = 0;
141 exvector::const_iterator last = expanded_seq.end();
142 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
143 if (is_exactly_a<add>(*cit)) {
144 positions_of_adds[number_of_adds] = current_position;
145 size_t num_ops = cit->nops();
146 number_of_add_operands[number_of_adds] = num_ops;
147 number_of_expanded_terms *= num_ops;
153 // If there are no sums, we are done
154 if (number_of_adds == 0)
155 return (new ncmul(expanded_seq, true))->
156 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
158 // Now, form all possible products of the terms of the sums with the
159 // remaining factors, and add them together
161 distrseq.reserve(number_of_expanded_terms);
163 intvector k(number_of_adds);
166 exvector term = expanded_seq;
167 for (size_t i=0; i<number_of_adds; i++)
168 term[positions_of_adds[i]] = expanded_seq[positions_of_adds[i]].op(k[i]);
169 distrseq.push_back((new ncmul(term, true))->
170 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
173 int l = number_of_adds-1;
174 while ((l>=0) && ((++k[l]) >= number_of_add_operands[l])) {
182 return (new add(distrseq))->
183 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
186 int ncmul::degree(const ex & s) const
188 // Sum up degrees of factors
190 exvector::const_iterator i = seq.begin(), end = seq.end();
192 deg_sum += i->degree(s);
198 int ncmul::ldegree(const ex & s) const
200 // Sum up degrees of factors
202 exvector::const_iterator i = seq.begin(), end = seq.end();
204 deg_sum += i->degree(s);
210 ex ncmul::coeff(const ex & s, int n) const
213 coeffseq.reserve(seq.size());
216 // product of individual coeffs
217 // if a non-zero power of s is found, the resulting product will be 0
218 exvector::const_iterator it=seq.begin();
219 while (it!=seq.end()) {
220 coeffseq.push_back((*it).coeff(s,n));
223 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
226 exvector::const_iterator i = seq.begin(), end = seq.end();
227 bool coeff_found = false;
229 ex c = i->coeff(s,n);
231 coeffseq.push_back(*i);
233 coeffseq.push_back(c);
239 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
244 size_t ncmul::count_factors(const ex & e) const
246 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
247 (is_exactly_a<ncmul>(e))) {
249 for (size_t i=0; i<e.nops(); i++)
250 factors += count_factors(e.op(i));
257 void ncmul::append_factors(exvector & v, const ex & e) const
259 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
260 (is_exactly_a<ncmul>(e))) {
261 for (size_t i=0; i<e.nops(); i++)
262 append_factors(v, e.op(i));
267 typedef std::vector<unsigned> unsignedvector;
268 typedef std::vector<exvector> exvectorvector;
270 /** Perform automatic term rewriting rules in this class. In the following
271 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
272 * stand for such expressions that contain a plain number.
273 * - ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) -> ncmul(...,x1,x2,...,x3,x4,...) (associativity)
276 * - ncmul(...,c1,...,c2,...) -> *(c1,c2,ncmul(...)) (pull out commutative elements)
277 * - ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2)) (collect elements of same type)
278 * - ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
280 * @param level cut-off in recursive evaluation */
281 ex ncmul::eval(int level) const
283 // The following additional rule would be nice, but produces a recursion,
284 // which must be trapped by introducing a flag that the sub-ncmuls()
285 // are already evaluated (maybe later...)
286 // ncmul(x1,x2,...,X,y1,y2,...) ->
287 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
288 // (X noncommutative_composite)
290 if ((level==1) && (flags & status_flags::evaluated)) {
294 exvector evaledseq=evalchildren(level);
296 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
297 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
299 exvector::const_iterator cit = evaledseq.begin(), citend = evaledseq.end();
300 while (cit != citend)
301 factors += count_factors(*cit++);
304 assocseq.reserve(factors);
305 cit = evaledseq.begin();
306 while (cit != citend)
307 append_factors(assocseq, *cit++);
310 if (assocseq.size()==1) return *(seq.begin());
313 if (assocseq.empty()) return _ex1;
315 // determine return types
316 unsignedvector rettypes;
317 rettypes.reserve(assocseq.size());
319 size_t count_commutative=0;
320 size_t count_noncommutative=0;
321 size_t count_noncommutative_composite=0;
322 cit = assocseq.begin(); citend = assocseq.end();
323 while (cit != citend) {
324 switch (rettypes[i] = cit->return_type()) {
325 case return_types::commutative:
328 case return_types::noncommutative:
329 count_noncommutative++;
331 case return_types::noncommutative_composite:
332 count_noncommutative_composite++;
335 throw(std::logic_error("ncmul::eval(): invalid return type"));
339 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
341 // ncmul(...,c1,...,c2,...) ->
342 // *(c1,c2,ncmul(...)) (pull out commutative elements)
343 if (count_commutative!=0) {
344 exvector commutativeseq;
345 commutativeseq.reserve(count_commutative+1);
346 exvector noncommutativeseq;
347 noncommutativeseq.reserve(assocseq.size()-count_commutative);
348 size_t num = assocseq.size();
349 for (size_t i=0; i<num; ++i) {
350 if (rettypes[i]==return_types::commutative)
351 commutativeseq.push_back(assocseq[i]);
353 noncommutativeseq.push_back(assocseq[i]);
355 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
356 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
359 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
360 // (collect elements of same type)
362 if (count_noncommutative_composite==0) {
363 // there are neither commutative nor noncommutative_composite
364 // elements in assocseq
365 GINAC_ASSERT(count_commutative==0);
367 size_t assoc_num = assocseq.size();
369 unsignedvector rttinfos;
370 evv.reserve(assoc_num);
371 rttinfos.reserve(assoc_num);
373 cit = assocseq.begin(), citend = assocseq.end();
374 while (cit != citend) {
375 unsigned ti = cit->return_type_tinfo();
376 size_t rtt_num = rttinfos.size();
377 // search type in vector of known types
378 for (i=0; i<rtt_num; ++i) {
379 if (ti == rttinfos[i]) {
380 evv[i].push_back(*cit);
386 rttinfos.push_back(ti);
387 evv.push_back(exvector());
388 (evv.end()-1)->reserve(assoc_num);
389 (evv.end()-1)->push_back(*cit);
394 size_t evv_num = evv.size();
395 #ifdef DO_GINAC_ASSERT
396 GINAC_ASSERT(evv_num == rttinfos.size());
397 GINAC_ASSERT(evv_num > 0);
399 for (i=0; i<evv_num; ++i)
401 GINAC_ASSERT(s == assoc_num);
402 #endif // def DO_GINAC_ASSERT
404 // if all elements are of same type, simplify the string
406 return evv[0][0].eval_ncmul(evv[0]);
409 splitseq.reserve(evv_num);
410 for (i=0; i<evv_num; ++i)
411 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
413 return (new mul(splitseq))->setflag(status_flags::dynallocated);
416 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
417 status_flags::evaluated);
420 ex ncmul::evalm() const
422 // Evaluate children first
423 std::auto_ptr<exvector> s(new exvector);
424 s->reserve(seq.size());
425 exvector::const_iterator it = seq.begin(), itend = seq.end();
426 while (it != itend) {
427 s->push_back(it->evalm());
431 // If there are only matrices, simply multiply them
432 it = s->begin(); itend = s->end();
433 if (is_a<matrix>(*it)) {
434 matrix prod(ex_to<matrix>(*it));
436 while (it != itend) {
437 if (!is_a<matrix>(*it))
439 prod = prod.mul(ex_to<matrix>(*it));
446 return (new ncmul(s))->setflag(status_flags::dynallocated);
449 ex ncmul::thiscontainer(const exvector & v) const
451 return (new ncmul(v))->setflag(status_flags::dynallocated);
454 ex ncmul::thiscontainer(std::auto_ptr<exvector> vp) const
456 return (new ncmul(vp))->setflag(status_flags::dynallocated);
459 ex ncmul::conjugate() const
461 if (return_type() != return_types::noncommutative) {
462 return exprseq::conjugate();
465 if (return_type_tinfo() & 0xffffff00U != TINFO_clifford) {
466 return exprseq::conjugate();
471 for (const_iterator i=end(); i!=begin();) {
473 ev.push_back(i->conjugate());
475 return (new ncmul(ev, true))->setflag(status_flags::dynallocated).eval();
480 /** Implementation of ex::diff() for a non-commutative product. It applies
483 ex ncmul::derivative(const symbol & s) const
485 size_t num = seq.size();
489 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
490 exvector ncmulseq = seq;
491 for (size_t i=0; i<num; ++i) {
492 ex e = seq[i].diff(s);
494 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
497 return (new add(addseq))->setflag(status_flags::dynallocated);
500 int ncmul::compare_same_type(const basic & other) const
502 return inherited::compare_same_type(other);
505 unsigned ncmul::return_type() const
508 return return_types::commutative;
510 bool all_commutative = true;
511 exvector::const_iterator noncommutative_element; // point to first found nc element
513 exvector::const_iterator i = seq.begin(), end = seq.end();
515 unsigned rt = i->return_type();
516 if (rt == return_types::noncommutative_composite)
517 return rt; // one ncc -> mul also ncc
518 if ((rt == return_types::noncommutative) && (all_commutative)) {
519 // first nc element found, remember position
520 noncommutative_element = i;
521 all_commutative = false;
523 if ((rt == return_types::noncommutative) && (!all_commutative)) {
524 // another nc element found, compare type_infos
525 if (noncommutative_element->return_type_tinfo() != i->return_type_tinfo()) {
526 // diffent types -> mul is ncc
527 return return_types::noncommutative_composite;
532 // all factors checked
533 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
534 return all_commutative ? return_types::commutative : return_types::noncommutative;
537 unsigned ncmul::return_type_tinfo() const
542 // return type_info of first noncommutative element
543 exvector::const_iterator i = seq.begin(), end = seq.end();
545 if (i->return_type() == return_types::noncommutative)
546 return i->return_type_tinfo();
550 // no noncommutative element found, should not happen
555 // new virtual functions which can be overridden by derived classes
561 // non-virtual functions in this class
564 exvector ncmul::expandchildren(unsigned options) const
567 s.reserve(seq.size());
568 exvector::const_iterator it = seq.begin(), itend = seq.end();
569 while (it != itend) {
570 s.push_back(it->expand(options));
576 const exvector & ncmul::get_factors() const
585 ex reeval_ncmul(const exvector & v)
587 return (new ncmul(v))->setflag(status_flags::dynallocated);
590 ex hold_ncmul(const exvector & v)
594 else if (v.size() == 1)
597 return (new ncmul(v))->setflag(status_flags::dynallocated |
598 status_flags::evaluated);