3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
40 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(ncmul, exprseq,
41 print_func<print_context>(&ncmul::do_print).
42 print_func<print_tree>(&ncmul::do_print_tree).
43 print_func<print_csrc>(&ncmul::do_print_csrc).
44 print_func<print_python_repr>(&ncmul::do_print_csrc))
48 // default constructor
53 tinfo_key = &ncmul::tinfo_static;
62 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
64 tinfo_key = &ncmul::tinfo_static;
67 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
69 tinfo_key = &ncmul::tinfo_static;
72 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
73 const ex & f4) : inherited(f1,f2,f3,f4)
75 tinfo_key = &ncmul::tinfo_static;
78 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
79 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
81 tinfo_key = &ncmul::tinfo_static;
84 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
85 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
87 tinfo_key = &ncmul::tinfo_static;
90 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
92 tinfo_key = &ncmul::tinfo_static;
95 ncmul::ncmul(std::auto_ptr<exvector> vp) : inherited(vp)
97 tinfo_key = &ncmul::tinfo_static;
104 DEFAULT_ARCHIVING(ncmul)
107 // functions overriding virtual functions from base classes
112 void ncmul::do_print(const print_context & c, unsigned level) const
114 printseq(c, '(', '*', ')', precedence(), level);
117 void ncmul::do_print_csrc(const print_context & c, unsigned level) const
120 printseq(c, '(', ',', ')', precedence(), precedence());
123 bool ncmul::info(unsigned inf) const
125 return inherited::info(inf);
128 typedef std::vector<int> intvector;
130 ex ncmul::expand(unsigned options) const
132 // First, expand the children
133 std::auto_ptr<exvector> vp = expandchildren(options);
134 const exvector &expanded_seq = vp.get() ? *vp : this->seq;
136 // Now, look for all the factors that are sums and remember their
137 // position and number of terms.
138 intvector positions_of_adds(expanded_seq.size());
139 intvector number_of_add_operands(expanded_seq.size());
141 size_t number_of_adds = 0;
142 size_t number_of_expanded_terms = 1;
144 size_t current_position = 0;
145 exvector::const_iterator last = expanded_seq.end();
146 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
147 if (is_exactly_a<add>(*cit)) {
148 positions_of_adds[number_of_adds] = current_position;
149 size_t num_ops = cit->nops();
150 number_of_add_operands[number_of_adds] = num_ops;
151 number_of_expanded_terms *= num_ops;
157 // If there are no sums, we are done
158 if (number_of_adds == 0) {
160 return (new ncmul(vp))->
161 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
166 // Now, form all possible products of the terms of the sums with the
167 // remaining factors, and add them together
169 distrseq.reserve(number_of_expanded_terms);
171 intvector k(number_of_adds);
173 /* Rename indices in the static members of the product */
174 exvector expanded_seq_mod;
178 for (size_t i=0; i<expanded_seq.size(); i++) {
179 if (i == positions_of_adds[j]) {
180 expanded_seq_mod.push_back(_ex1);
183 expanded_seq_mod.push_back(rename_dummy_indices_uniquely(va, expanded_seq[i], true));
188 exvector term = expanded_seq_mod;
189 for (size_t i=0; i<number_of_adds; i++) {
190 term[positions_of_adds[i]] = rename_dummy_indices_uniquely(va, expanded_seq[positions_of_adds[i]].op(k[i]), true);
193 distrseq.push_back((new ncmul(term, true))->
194 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
197 int l = number_of_adds-1;
198 while ((l>=0) && ((++k[l]) >= number_of_add_operands[l])) {
206 return (new add(distrseq))->
207 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
210 int ncmul::degree(const ex & s) const
212 if (is_equal(ex_to<basic>(s)))
215 // Sum up degrees of factors
217 exvector::const_iterator i = seq.begin(), end = seq.end();
219 deg_sum += i->degree(s);
225 int ncmul::ldegree(const ex & s) const
227 if (is_equal(ex_to<basic>(s)))
230 // Sum up degrees of factors
232 exvector::const_iterator i = seq.begin(), end = seq.end();
234 deg_sum += i->degree(s);
240 ex ncmul::coeff(const ex & s, int n) const
242 if (is_equal(ex_to<basic>(s)))
243 return n==1 ? _ex1 : _ex0;
246 coeffseq.reserve(seq.size());
249 // product of individual coeffs
250 // if a non-zero power of s is found, the resulting product will be 0
251 exvector::const_iterator it=seq.begin();
252 while (it!=seq.end()) {
253 coeffseq.push_back((*it).coeff(s,n));
256 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
259 exvector::const_iterator i = seq.begin(), end = seq.end();
260 bool coeff_found = false;
262 ex c = i->coeff(s,n);
264 coeffseq.push_back(*i);
266 coeffseq.push_back(c);
272 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
277 size_t ncmul::count_factors(const ex & e) const
279 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
280 (is_exactly_a<ncmul>(e))) {
282 for (size_t i=0; i<e.nops(); i++)
283 factors += count_factors(e.op(i));
290 void ncmul::append_factors(exvector & v, const ex & e) const
292 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
293 (is_exactly_a<ncmul>(e))) {
294 for (size_t i=0; i<e.nops(); i++)
295 append_factors(v, e.op(i));
300 typedef std::vector<unsigned> unsignedvector;
301 typedef std::vector<exvector> exvectorvector;
303 /** Perform automatic term rewriting rules in this class. In the following
304 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
305 * stand for such expressions that contain a plain number.
306 * - ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) -> ncmul(...,x1,x2,...,x3,x4,...) (associativity)
309 * - ncmul(...,c1,...,c2,...) -> *(c1,c2,ncmul(...)) (pull out commutative elements)
310 * - ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2)) (collect elements of same type)
311 * - ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
313 * @param level cut-off in recursive evaluation */
314 ex ncmul::eval(int level) const
316 // The following additional rule would be nice, but produces a recursion,
317 // which must be trapped by introducing a flag that the sub-ncmuls()
318 // are already evaluated (maybe later...)
319 // ncmul(x1,x2,...,X,y1,y2,...) ->
320 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
321 // (X noncommutative_composite)
323 if ((level==1) && (flags & status_flags::evaluated)) {
327 exvector evaledseq=evalchildren(level);
329 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
330 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
332 exvector::const_iterator cit = evaledseq.begin(), citend = evaledseq.end();
333 while (cit != citend)
334 factors += count_factors(*cit++);
337 assocseq.reserve(factors);
338 cit = evaledseq.begin();
339 while (cit != citend)
340 append_factors(assocseq, *cit++);
343 if (assocseq.size()==1) return *(seq.begin());
346 if (assocseq.empty()) return _ex1;
348 // determine return types
349 unsignedvector rettypes;
350 rettypes.reserve(assocseq.size());
352 size_t count_commutative=0;
353 size_t count_noncommutative=0;
354 size_t count_noncommutative_composite=0;
355 cit = assocseq.begin(); citend = assocseq.end();
356 while (cit != citend) {
357 switch (rettypes[i] = cit->return_type()) {
358 case return_types::commutative:
361 case return_types::noncommutative:
362 count_noncommutative++;
364 case return_types::noncommutative_composite:
365 count_noncommutative_composite++;
368 throw(std::logic_error("ncmul::eval(): invalid return type"));
372 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
374 // ncmul(...,c1,...,c2,...) ->
375 // *(c1,c2,ncmul(...)) (pull out commutative elements)
376 if (count_commutative!=0) {
377 exvector commutativeseq;
378 commutativeseq.reserve(count_commutative+1);
379 exvector noncommutativeseq;
380 noncommutativeseq.reserve(assocseq.size()-count_commutative);
381 size_t num = assocseq.size();
382 for (size_t i=0; i<num; ++i) {
383 if (rettypes[i]==return_types::commutative)
384 commutativeseq.push_back(assocseq[i]);
386 noncommutativeseq.push_back(assocseq[i]);
388 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
389 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
392 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
393 // (collect elements of same type)
395 if (count_noncommutative_composite==0) {
396 // there are neither commutative nor noncommutative_composite
397 // elements in assocseq
398 GINAC_ASSERT(count_commutative==0);
400 size_t assoc_num = assocseq.size();
402 std::vector<const basic*> rttinfos;
403 evv.reserve(assoc_num);
404 rttinfos.reserve(assoc_num);
406 cit = assocseq.begin(), citend = assocseq.end();
407 while (cit != citend) {
408 const basic* ti = cit->return_type_tinfo();
409 size_t rtt_num = rttinfos.size();
410 // search type in vector of known types
411 for (i=0; i<rtt_num; ++i) {
412 tinfo_t tinf = ti->tinfo();
413 if (tinf == rttinfos[i]->tinfo()) {
414 if (tinf == &clifford::tinfo_static) {
415 if (((clifford*)ti)->get_representation_label() == ((clifford*)rttinfos[i])->get_representation_label()) {
416 evv[i].push_back(*cit);
419 } else if (tinf == &color::tinfo_static) {
420 if (((color*)ti)->get_representation_label() == ((color*)rttinfos[i])->get_representation_label()) {
421 evv[i].push_back(*cit);
425 evv[i].push_back(*cit);
432 rttinfos.push_back(ti);
433 evv.push_back(exvector());
434 (evv.end()-1)->reserve(assoc_num);
435 (evv.end()-1)->push_back(*cit);
440 size_t evv_num = evv.size();
441 #ifdef DO_GINAC_ASSERT
442 GINAC_ASSERT(evv_num == rttinfos.size());
443 GINAC_ASSERT(evv_num > 0);
445 for (i=0; i<evv_num; ++i)
447 GINAC_ASSERT(s == assoc_num);
448 #endif // def DO_GINAC_ASSERT
450 // if all elements are of same type, simplify the string
452 return evv[0][0].eval_ncmul(evv[0]);
456 splitseq.reserve(evv_num);
457 for (i=0; i<evv_num; ++i)
458 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
460 return (new mul(splitseq))->setflag(status_flags::dynallocated);
463 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
464 status_flags::evaluated);
467 ex ncmul::evalm() const
469 // Evaluate children first
470 std::auto_ptr<exvector> s(new exvector);
471 s->reserve(seq.size());
472 exvector::const_iterator it = seq.begin(), itend = seq.end();
473 while (it != itend) {
474 s->push_back(it->evalm());
478 // If there are only matrices, simply multiply them
479 it = s->begin(); itend = s->end();
480 if (is_a<matrix>(*it)) {
481 matrix prod(ex_to<matrix>(*it));
483 while (it != itend) {
484 if (!is_a<matrix>(*it))
486 prod = prod.mul(ex_to<matrix>(*it));
493 return (new ncmul(s))->setflag(status_flags::dynallocated);
496 ex ncmul::thiscontainer(const exvector & v) const
498 return (new ncmul(v))->setflag(status_flags::dynallocated);
501 ex ncmul::thiscontainer(std::auto_ptr<exvector> vp) const
503 return (new ncmul(vp))->setflag(status_flags::dynallocated);
506 ex ncmul::conjugate() const
508 if (return_type() != return_types::noncommutative) {
509 return exprseq::conjugate();
512 if (return_type_tinfo()->tinfo() != &clifford::tinfo_static) {
513 return exprseq::conjugate();
518 for (const_iterator i=end(); i!=begin();) {
520 ev.push_back(i->conjugate());
522 return (new ncmul(ev, true))->setflag(status_flags::dynallocated).eval();
527 /** Implementation of ex::diff() for a non-commutative product. It applies
530 ex ncmul::derivative(const symbol & s) const
532 size_t num = seq.size();
536 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
537 exvector ncmulseq = seq;
538 for (size_t i=0; i<num; ++i) {
539 ex e = seq[i].diff(s);
541 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
544 return (new add(addseq))->setflag(status_flags::dynallocated);
547 int ncmul::compare_same_type(const basic & other) const
549 return inherited::compare_same_type(other);
552 unsigned ncmul::return_type() const
555 return return_types::commutative;
557 bool all_commutative = true;
558 exvector::const_iterator noncommutative_element; // point to first found nc element
560 exvector::const_iterator i = seq.begin(), end = seq.end();
562 unsigned rt = i->return_type();
563 if (rt == return_types::noncommutative_composite)
564 return rt; // one ncc -> mul also ncc
565 if ((rt == return_types::noncommutative) && (all_commutative)) {
566 // first nc element found, remember position
567 noncommutative_element = i;
568 all_commutative = false;
570 if ((rt == return_types::noncommutative) && (!all_commutative)) {
571 // another nc element found, compare type_infos
572 if (noncommutative_element->return_type_tinfo()->tinfo() == &clifford::tinfo_static) {
573 if (i->return_type_tinfo()->tinfo() != &clifford::tinfo_static ||
574 ((clifford*)(noncommutative_element->return_type_tinfo()))->get_representation_label() !=
575 ((clifford*)(i->return_type_tinfo()))->get_representation_label()) {
576 // diffent types -> mul is ncc
577 return return_types::noncommutative_composite;
579 } else if (noncommutative_element->return_type_tinfo()->tinfo() == &color::tinfo_static) {
580 if (i->return_type_tinfo()->tinfo() != &color::tinfo_static ||
581 ((color*)(noncommutative_element->return_type_tinfo()))->get_representation_label() !=
582 ((color*)(i->return_type_tinfo()))->get_representation_label()) {
583 // diffent types -> mul is ncc
584 return return_types::noncommutative_composite;
586 } else if (noncommutative_element->return_type_tinfo()->tinfo() != i->return_type_tinfo()->tinfo()) {
587 return return_types::noncommutative_composite;
592 // all factors checked
593 GINAC_ASSERT(!all_commutative); // not all factors should commutate, because this is a ncmul();
594 return all_commutative ? return_types::commutative : return_types::noncommutative;
597 const basic* ncmul::return_type_tinfo() const
602 // return type_info of first noncommutative element
603 exvector::const_iterator i = seq.begin(), end = seq.end();
605 if (i->return_type() == return_types::noncommutative)
606 return i->return_type_tinfo();
610 // no noncommutative element found, should not happen
615 // new virtual functions which can be overridden by derived classes
621 // non-virtual functions in this class
624 std::auto_ptr<exvector> ncmul::expandchildren(unsigned options) const
626 const_iterator cit = this->seq.begin(), end = this->seq.end();
628 const ex & expanded_ex = cit->expand(options);
629 if (!are_ex_trivially_equal(*cit, expanded_ex)) {
631 // copy first part of seq which hasn't changed
632 std::auto_ptr<exvector> s(new exvector(this->seq.begin(), cit));
633 reserve(*s, this->seq.size());
635 // insert changed element
636 s->push_back(expanded_ex);
641 s->push_back(cit->expand(options));
651 return std::auto_ptr<exvector>(0); // nothing has changed
654 const exvector & ncmul::get_factors() const
663 ex reeval_ncmul(const exvector & v)
665 return (new ncmul(v))->setflag(status_flags::dynallocated);
668 ex hold_ncmul(const exvector & v)
672 else if (v.size() == 1)
675 return (new ncmul(v))->setflag(status_flags::dynallocated |
676 status_flags::evaluated);