3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2015 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(exvector && v) : inherited(std::move(v))
97 // functions overriding virtual functions from base classes
102 void ncmul::do_print(const print_context & c, unsigned level) const
104 printseq(c, '(', '*', ')', precedence(), level);
107 void ncmul::do_print_csrc(const print_context & c, unsigned level) const
110 printseq(c, '(', ',', ')', precedence(), precedence());
113 bool ncmul::info(unsigned inf) const
115 return inherited::info(inf);
118 typedef std::vector<std::size_t> uintvector;
120 ex ncmul::expand(unsigned options) const
122 // First, expand the children
123 exvector v = expandchildren(options);
124 const exvector &expanded_seq = v.empty() ? this->seq : v;
126 // Now, look for all the factors that are sums and remember their
127 // position and number of terms.
128 uintvector positions_of_adds(expanded_seq.size());
129 uintvector number_of_add_operands(expanded_seq.size());
131 size_t number_of_adds = 0;
132 size_t number_of_expanded_terms = 1;
134 size_t current_position = 0;
135 for (auto & it : expanded_seq) {
136 if (is_exactly_a<add>(it)) {
137 positions_of_adds[number_of_adds] = current_position;
138 size_t num_ops = it.nops();
139 number_of_add_operands[number_of_adds] = num_ops;
140 number_of_expanded_terms *= num_ops;
146 // If there are no sums, we are done
147 if (number_of_adds == 0) {
149 return (new ncmul(std::move(v)))->
150 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
155 // Now, form all possible products of the terms of the sums with the
156 // remaining factors, and add them together
158 distrseq.reserve(number_of_expanded_terms);
160 uintvector k(number_of_adds);
162 /* Rename indices in the static members of the product */
163 exvector expanded_seq_mod;
167 for (size_t i=0; i<expanded_seq.size(); i++) {
168 if (i == positions_of_adds[j]) {
169 expanded_seq_mod.push_back(_ex1);
172 expanded_seq_mod.push_back(rename_dummy_indices_uniquely(va, expanded_seq[i], true));
177 exvector term = expanded_seq_mod;
178 for (size_t i=0; i<number_of_adds; i++) {
179 term[positions_of_adds[i]] = rename_dummy_indices_uniquely(va, expanded_seq[positions_of_adds[i]].op(k[i]), true);
182 distrseq.push_back((new ncmul(term, true))->
183 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
186 int l = number_of_adds-1;
187 while ((l>=0) && ((++k[l]) >= number_of_add_operands[l])) {
195 return (new add(distrseq))->
196 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
199 int ncmul::degree(const ex & s) const
201 if (is_equal(ex_to<basic>(s)))
204 // Sum up degrees of factors
207 deg_sum += i.degree(s);
211 int ncmul::ldegree(const ex & s) const
213 if (is_equal(ex_to<basic>(s)))
216 // Sum up degrees of factors
219 deg_sum += i.degree(s);
223 ex ncmul::coeff(const ex & s, int n) const
225 if (is_equal(ex_to<basic>(s)))
226 return n==1 ? _ex1 : _ex0;
229 coeffseq.reserve(seq.size());
232 // product of individual coeffs
233 // if a non-zero power of s is found, the resulting product will be 0
234 for (auto & it : seq)
235 coeffseq.push_back(it.coeff(s,n));
236 return (new ncmul(std::move(coeffseq),1))->setflag(status_flags::dynallocated);
239 bool coeff_found = false;
240 for (auto & i : seq) {
243 coeffseq.push_back(i);
245 coeffseq.push_back(c);
251 return (new ncmul(std::move(coeffseq), 1))->setflag(status_flags::dynallocated);
256 size_t ncmul::count_factors(const ex & e) const
258 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
259 (is_exactly_a<ncmul>(e))) {
261 for (size_t i=0; i<e.nops(); i++)
262 factors += count_factors(e.op(i));
269 void ncmul::append_factors(exvector & v, const ex & e) const
271 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
272 (is_exactly_a<ncmul>(e))) {
273 for (size_t i=0; i<e.nops(); i++)
274 append_factors(v, e.op(i));
279 typedef std::vector<unsigned> unsignedvector;
280 typedef std::vector<exvector> exvectorvector;
282 /** Perform automatic term rewriting rules in this class. In the following
283 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
284 * stand for such expressions that contain a plain number.
285 * - ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) -> ncmul(...,x1,x2,...,x3,x4,...) (associativity)
288 * - ncmul(...,c1,...,c2,...) -> *(c1,c2,ncmul(...)) (pull out commutative elements)
289 * - ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2)) (collect elements of same type)
290 * - ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
292 * @param level cut-off in recursive evaluation */
293 ex ncmul::eval(int level) const
295 // The following additional rule would be nice, but produces a recursion,
296 // which must be trapped by introducing a flag that the sub-ncmuls()
297 // are already evaluated (maybe later...)
298 // ncmul(x1,x2,...,X,y1,y2,...) ->
299 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
300 // (X noncommutative_composite)
302 if ((level==1) && (flags & status_flags::evaluated)) {
306 exvector evaledseq=evalchildren(level);
308 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
309 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
311 for (auto & it : evaledseq)
312 factors += count_factors(it);
315 assocseq.reserve(factors);
316 make_flat_inserter mf(evaledseq, true);
317 for (auto & it : evaledseq) {
318 ex factor = mf.handle_factor(it, 1);
319 append_factors(assocseq, factor);
323 if (assocseq.size()==1) return *(seq.begin());
326 if (assocseq.empty()) return _ex1;
328 // determine return types
329 unsignedvector rettypes(assocseq.size());
331 size_t count_commutative=0;
332 size_t count_noncommutative=0;
333 size_t count_noncommutative_composite=0;
334 for (auto & it : assocseq) {
335 rettypes[i] = it.return_type();
336 switch (rettypes[i]) {
337 case return_types::commutative:
340 case return_types::noncommutative:
341 count_noncommutative++;
343 case return_types::noncommutative_composite:
344 count_noncommutative_composite++;
347 throw(std::logic_error("ncmul::eval(): invalid return type"));
351 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
353 // ncmul(...,c1,...,c2,...) ->
354 // *(c1,c2,ncmul(...)) (pull out commutative elements)
355 if (count_commutative!=0) {
356 exvector commutativeseq;
357 commutativeseq.reserve(count_commutative+1);
358 exvector noncommutativeseq;
359 noncommutativeseq.reserve(assocseq.size()-count_commutative);
360 size_t num = assocseq.size();
361 for (size_t i=0; i<num; ++i) {
362 if (rettypes[i]==return_types::commutative)
363 commutativeseq.push_back(assocseq[i]);
365 noncommutativeseq.push_back(assocseq[i]);
367 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
368 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
371 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
372 // (collect elements of same type)
374 if (count_noncommutative_composite==0) {
375 // there are neither commutative nor noncommutative_composite
376 // elements in assocseq
377 GINAC_ASSERT(count_commutative==0);
379 size_t assoc_num = assocseq.size();
381 std::vector<return_type_t> rttinfos;
382 evv.reserve(assoc_num);
383 rttinfos.reserve(assoc_num);
385 for (auto & it : assocseq) {
386 return_type_t ti = it.return_type_tinfo();
387 size_t rtt_num = rttinfos.size();
388 // search type in vector of known types
389 for (i=0; i<rtt_num; ++i) {
390 if(ti == rttinfos[i]) {
391 evv[i].push_back(it);
397 rttinfos.push_back(ti);
398 evv.push_back(exvector());
399 (evv.end()-1)->reserve(assoc_num);
400 (evv.end()-1)->push_back(it);
404 size_t evv_num = evv.size();
405 #ifdef DO_GINAC_ASSERT
406 GINAC_ASSERT(evv_num == rttinfos.size());
407 GINAC_ASSERT(evv_num > 0);
409 for (i=0; i<evv_num; ++i)
411 GINAC_ASSERT(s == assoc_num);
412 #endif // def DO_GINAC_ASSERT
414 // if all elements are of same type, simplify the string
416 return evv[0][0].eval_ncmul(evv[0]);
420 splitseq.reserve(evv_num);
421 for (i=0; i<evv_num; ++i)
422 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
424 return (new mul(splitseq))->setflag(status_flags::dynallocated);
427 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
428 status_flags::evaluated);
431 ex ncmul::evalm() const
433 // Evaluate children first
435 s.reserve(seq.size());
436 for (auto & it : seq)
437 s.push_back(it.evalm());
439 // If there are only matrices, simply multiply them
440 auto it = s.begin(), itend = s.end();
441 if (is_a<matrix>(*it)) {
442 matrix prod(ex_to<matrix>(*it));
444 while (it != itend) {
445 if (!is_a<matrix>(*it))
447 prod = prod.mul(ex_to<matrix>(*it));
454 return (new ncmul(std::move(s)))->setflag(status_flags::dynallocated);
457 ex ncmul::thiscontainer(const exvector & v) const
459 return (new ncmul(v))->setflag(status_flags::dynallocated);
462 ex ncmul::thiscontainer(exvector && v) const
464 return (new ncmul(std::move(v)))->setflag(status_flags::dynallocated);
467 ex ncmul::conjugate() const
469 if (return_type() != return_types::noncommutative) {
470 return exprseq::conjugate();
473 if (!is_clifford_tinfo(return_type_tinfo())) {
474 return exprseq::conjugate();
479 for (auto i=end(); i!=begin();) {
481 ev.push_back(i->conjugate());
483 return (new ncmul(std::move(ev), true))->setflag(status_flags::dynallocated).eval();
486 ex ncmul::real_part() const
488 return basic::real_part();
491 ex ncmul::imag_part() const
493 return basic::imag_part();
498 /** Implementation of ex::diff() for a non-commutative product. It applies
501 ex ncmul::derivative(const symbol & s) const
503 size_t num = seq.size();
507 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
508 exvector ncmulseq = seq;
509 for (size_t i=0; i<num; ++i) {
510 ex e = seq[i].diff(s);
512 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
515 return (new add(addseq))->setflag(status_flags::dynallocated);
518 int ncmul::compare_same_type(const basic & other) const
520 return inherited::compare_same_type(other);
523 unsigned ncmul::return_type() const
526 return return_types::commutative;
528 bool all_commutative = true;
529 exvector::const_iterator noncommutative_element; // point to first found nc element
531 exvector::const_iterator i = seq.begin(), end = seq.end();
533 unsigned rt = i->return_type();
534 if (rt == return_types::noncommutative_composite)
535 return rt; // one ncc -> mul also ncc
536 if ((rt == return_types::noncommutative) && (all_commutative)) {
537 // first nc element found, remember position
538 noncommutative_element = i;
539 all_commutative = false;
541 if ((rt == return_types::noncommutative) && (!all_commutative)) {
542 // another nc element found, compare type_infos
543 if(noncommutative_element->return_type_tinfo() != i->return_type_tinfo())
544 return return_types::noncommutative_composite;
548 // all factors checked
549 GINAC_ASSERT(!all_commutative); // not all factors should commutate, because this is a ncmul();
550 return all_commutative ? return_types::commutative : return_types::noncommutative;
553 return_type_t ncmul::return_type_tinfo() const
556 return make_return_type_t<ncmul>();
558 // return type_info of first noncommutative element
560 if (i.return_type() == return_types::noncommutative)
561 return i.return_type_tinfo();
563 // no noncommutative element found, should not happen
564 return make_return_type_t<ncmul>();
568 // new virtual functions which can be overridden by derived classes
574 // non-virtual functions in this class
577 exvector ncmul::expandchildren(unsigned options) const
579 auto cit = this->seq.begin(), end = this->seq.end();
581 const ex & expanded_ex = cit->expand(options);
582 if (!are_ex_trivially_equal(*cit, expanded_ex)) {
584 // copy first part of seq which hasn't changed
585 exvector s(this->seq.begin(), cit);
586 s.reserve(this->seq.size());
588 // insert changed element
589 s.push_back(expanded_ex);
594 s.push_back(cit->expand(options));
604 return exvector(); // nothing has changed
607 const exvector & ncmul::get_factors() const
616 ex reeval_ncmul(const exvector & v)
618 return (new ncmul(v))->setflag(status_flags::dynallocated);
621 ex hold_ncmul(const exvector & v)
625 else if (v.size() == 1)
628 return (new ncmul(v))->setflag(status_flags::dynallocated |
629 status_flags::evaluated);
632 GINAC_BIND_UNARCHIVER(ncmul);