3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2008 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
39 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(ncmul, exprseq,
40 print_func<print_context>(&ncmul::do_print).
41 print_func<print_tree>(&ncmul::do_print_tree).
42 print_func<print_csrc>(&ncmul::do_print_csrc).
43 print_func<print_python_repr>(&ncmul::do_print_csrc))
47 // default constructor
60 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
64 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
68 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
69 const ex & f4) : inherited(f1,f2,f3,f4)
73 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
74 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
78 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
79 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
83 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
87 ncmul::ncmul(std::auto_ptr<exvector> vp) : inherited(vp)
95 DEFAULT_ARCHIVING(ncmul)
98 // functions overriding virtual functions from base classes
103 void ncmul::do_print(const print_context & c, unsigned level) const
105 printseq(c, '(', '*', ')', precedence(), level);
108 void ncmul::do_print_csrc(const print_context & c, unsigned level) const
111 printseq(c, '(', ',', ')', precedence(), precedence());
114 bool ncmul::info(unsigned inf) const
116 return inherited::info(inf);
119 typedef std::vector<std::size_t> uintvector;
121 ex ncmul::expand(unsigned options) const
123 // First, expand the children
124 std::auto_ptr<exvector> vp = expandchildren(options);
125 const exvector &expanded_seq = vp.get() ? *vp : this->seq;
127 // Now, look for all the factors that are sums and remember their
128 // position and number of terms.
129 uintvector positions_of_adds(expanded_seq.size());
130 uintvector number_of_add_operands(expanded_seq.size());
132 size_t number_of_adds = 0;
133 size_t number_of_expanded_terms = 1;
135 size_t current_position = 0;
136 exvector::const_iterator last = expanded_seq.end();
137 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
138 if (is_exactly_a<add>(*cit)) {
139 positions_of_adds[number_of_adds] = current_position;
140 size_t num_ops = cit->nops();
141 number_of_add_operands[number_of_adds] = num_ops;
142 number_of_expanded_terms *= num_ops;
148 // If there are no sums, we are done
149 if (number_of_adds == 0) {
151 return (new ncmul(vp))->
152 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
157 // Now, form all possible products of the terms of the sums with the
158 // remaining factors, and add them together
160 distrseq.reserve(number_of_expanded_terms);
162 uintvector k(number_of_adds);
164 /* Rename indices in the static members of the product */
165 exvector expanded_seq_mod;
169 for (size_t i=0; i<expanded_seq.size(); i++) {
170 if (i == positions_of_adds[j]) {
171 expanded_seq_mod.push_back(_ex1);
174 expanded_seq_mod.push_back(rename_dummy_indices_uniquely(va, expanded_seq[i], true));
179 exvector term = expanded_seq_mod;
180 for (size_t i=0; i<number_of_adds; i++) {
181 term[positions_of_adds[i]] = rename_dummy_indices_uniquely(va, expanded_seq[positions_of_adds[i]].op(k[i]), true);
184 distrseq.push_back((new ncmul(term, true))->
185 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
188 int l = number_of_adds-1;
189 while ((l>=0) && ((++k[l]) >= number_of_add_operands[l])) {
197 return (new add(distrseq))->
198 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
201 int ncmul::degree(const ex & s) const
203 if (is_equal(ex_to<basic>(s)))
206 // Sum up degrees of factors
208 exvector::const_iterator i = seq.begin(), end = seq.end();
210 deg_sum += i->degree(s);
216 int ncmul::ldegree(const ex & s) const
218 if (is_equal(ex_to<basic>(s)))
221 // Sum up degrees of factors
223 exvector::const_iterator i = seq.begin(), end = seq.end();
225 deg_sum += i->degree(s);
231 ex ncmul::coeff(const ex & s, int n) const
233 if (is_equal(ex_to<basic>(s)))
234 return n==1 ? _ex1 : _ex0;
237 coeffseq.reserve(seq.size());
240 // product of individual coeffs
241 // if a non-zero power of s is found, the resulting product will be 0
242 exvector::const_iterator it=seq.begin();
243 while (it!=seq.end()) {
244 coeffseq.push_back((*it).coeff(s,n));
247 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
250 exvector::const_iterator i = seq.begin(), end = seq.end();
251 bool coeff_found = false;
253 ex c = i->coeff(s,n);
255 coeffseq.push_back(*i);
257 coeffseq.push_back(c);
263 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
268 size_t ncmul::count_factors(const ex & e) const
270 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
271 (is_exactly_a<ncmul>(e))) {
273 for (size_t i=0; i<e.nops(); i++)
274 factors += count_factors(e.op(i));
281 void ncmul::append_factors(exvector & v, const ex & e) const
283 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
284 (is_exactly_a<ncmul>(e))) {
285 for (size_t i=0; i<e.nops(); i++)
286 append_factors(v, e.op(i));
291 typedef std::vector<unsigned> unsignedvector;
292 typedef std::vector<exvector> exvectorvector;
294 /** Perform automatic term rewriting rules in this class. In the following
295 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
296 * stand for such expressions that contain a plain number.
297 * - ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) -> ncmul(...,x1,x2,...,x3,x4,...) (associativity)
300 * - ncmul(...,c1,...,c2,...) -> *(c1,c2,ncmul(...)) (pull out commutative elements)
301 * - ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2)) (collect elements of same type)
302 * - ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
304 * @param level cut-off in recursive evaluation */
305 ex ncmul::eval(int level) const
307 // The following additional rule would be nice, but produces a recursion,
308 // which must be trapped by introducing a flag that the sub-ncmuls()
309 // are already evaluated (maybe later...)
310 // ncmul(x1,x2,...,X,y1,y2,...) ->
311 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
312 // (X noncommutative_composite)
314 if ((level==1) && (flags & status_flags::evaluated)) {
318 exvector evaledseq=evalchildren(level);
320 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
321 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
323 exvector::const_iterator cit = evaledseq.begin(), citend = evaledseq.end();
324 while (cit != citend)
325 factors += count_factors(*cit++);
328 assocseq.reserve(factors);
329 cit = evaledseq.begin();
330 make_flat_inserter mf(evaledseq, true);
331 while (cit != citend)
332 { ex factor = mf.handle_factor(*(cit++), 1);
333 append_factors(assocseq, factor);
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 size_t count_commutative=0;
347 size_t count_noncommutative=0;
348 size_t 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 size_t num = assocseq.size();
376 for (size_t 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 size_t assoc_num = assocseq.size();
396 std::vector<return_type_t> rttinfos;
397 evv.reserve(assoc_num);
398 rttinfos.reserve(assoc_num);
400 cit = assocseq.begin(), citend = assocseq.end();
401 while (cit != citend) {
402 return_type_t ti = cit->return_type_tinfo();
403 size_t 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 size_t 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].eval_ncmul(evv[0]);
437 splitseq.reserve(evv_num);
438 for (i=0; i<evv_num; ++i)
439 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
441 return (new mul(splitseq))->setflag(status_flags::dynallocated);
444 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
445 status_flags::evaluated);
448 ex ncmul::evalm() const
450 // Evaluate children first
451 std::auto_ptr<exvector> s(new exvector);
452 s->reserve(seq.size());
453 exvector::const_iterator it = seq.begin(), itend = seq.end();
454 while (it != itend) {
455 s->push_back(it->evalm());
459 // If there are only matrices, simply multiply them
460 it = s->begin(); itend = s->end();
461 if (is_a<matrix>(*it)) {
462 matrix prod(ex_to<matrix>(*it));
464 while (it != itend) {
465 if (!is_a<matrix>(*it))
467 prod = prod.mul(ex_to<matrix>(*it));
474 return (new ncmul(s))->setflag(status_flags::dynallocated);
477 ex ncmul::thiscontainer(const exvector & v) const
479 return (new ncmul(v))->setflag(status_flags::dynallocated);
482 ex ncmul::thiscontainer(std::auto_ptr<exvector> vp) const
484 return (new ncmul(vp))->setflag(status_flags::dynallocated);
487 ex ncmul::conjugate() const
489 if (return_type() != return_types::noncommutative) {
490 return exprseq::conjugate();
493 if (!is_clifford_tinfo(return_type_tinfo())) {
494 return exprseq::conjugate();
499 for (const_iterator i=end(); i!=begin();) {
501 ev.push_back(i->conjugate());
503 return (new ncmul(ev, true))->setflag(status_flags::dynallocated).eval();
506 ex ncmul::real_part() const
508 return basic::real_part();
511 ex ncmul::imag_part() const
513 return basic::imag_part();
518 /** Implementation of ex::diff() for a non-commutative product. It applies
521 ex ncmul::derivative(const symbol & s) const
523 size_t num = seq.size();
527 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
528 exvector ncmulseq = seq;
529 for (size_t i=0; i<num; ++i) {
530 ex e = seq[i].diff(s);
532 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
535 return (new add(addseq))->setflag(status_flags::dynallocated);
538 int ncmul::compare_same_type(const basic & other) const
540 return inherited::compare_same_type(other);
543 unsigned ncmul::return_type() const
546 return return_types::commutative;
548 bool all_commutative = true;
549 exvector::const_iterator noncommutative_element; // point to first found nc element
551 exvector::const_iterator i = seq.begin(), end = seq.end();
553 unsigned rt = i->return_type();
554 if (rt == return_types::noncommutative_composite)
555 return rt; // one ncc -> mul also ncc
556 if ((rt == return_types::noncommutative) && (all_commutative)) {
557 // first nc element found, remember position
558 noncommutative_element = i;
559 all_commutative = false;
561 if ((rt == return_types::noncommutative) && (!all_commutative)) {
562 // another nc element found, compare type_infos
563 if(noncommutative_element->return_type_tinfo() != i->return_type_tinfo())
564 return return_types::noncommutative_composite;
568 // all factors checked
569 GINAC_ASSERT(!all_commutative); // not all factors should commutate, because this is a ncmul();
570 return all_commutative ? return_types::commutative : return_types::noncommutative;
573 return_type_t ncmul::return_type_tinfo() const
576 return make_return_type_t<ncmul>();
578 // return type_info of first noncommutative element
579 exvector::const_iterator i = seq.begin(), end = seq.end();
581 if (i->return_type() == return_types::noncommutative)
582 return i->return_type_tinfo();
586 // no noncommutative element found, should not happen
587 return make_return_type_t<ncmul>();
591 // new virtual functions which can be overridden by derived classes
597 // non-virtual functions in this class
600 std::auto_ptr<exvector> ncmul::expandchildren(unsigned options) const
602 const_iterator cit = this->seq.begin(), end = this->seq.end();
604 const ex & expanded_ex = cit->expand(options);
605 if (!are_ex_trivially_equal(*cit, expanded_ex)) {
607 // copy first part of seq which hasn't changed
608 std::auto_ptr<exvector> s(new exvector(this->seq.begin(), cit));
609 reserve(*s, this->seq.size());
611 // insert changed element
612 s->push_back(expanded_ex);
617 s->push_back(cit->expand(options));
627 return std::auto_ptr<exvector>(0); // nothing has changed
630 const exvector & ncmul::get_factors() const
639 ex reeval_ncmul(const exvector & v)
641 return (new ncmul(v))->setflag(status_flags::dynallocated);
644 ex hold_ncmul(const exvector & v)
648 else if (v.size() == 1)
651 return (new ncmul(v))->setflag(status_flags::dynallocated |
652 status_flags::evaluated);