3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2018 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) : inherited(v)
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 dynallocate<ncmul>(std::move(v)).setflag(options == 0 ? status_flags::expanded : 0);
154 // Now, form all possible products of the terms of the sums with the
155 // remaining factors, and add them together
157 distrseq.reserve(number_of_expanded_terms);
159 uintvector k(number_of_adds);
161 /* Rename indices in the static members of the product */
162 exvector expanded_seq_mod;
166 for (size_t i=0; i<expanded_seq.size(); i++) {
167 if (i == positions_of_adds[j]) {
168 expanded_seq_mod.push_back(_ex1);
171 expanded_seq_mod.push_back(rename_dummy_indices_uniquely(va, expanded_seq[i], true));
176 exvector term = expanded_seq_mod;
177 for (size_t i=0; i<number_of_adds; i++) {
178 term[positions_of_adds[i]] = rename_dummy_indices_uniquely(va, expanded_seq[positions_of_adds[i]].op(k[i]), true);
181 distrseq.push_back(dynallocate<ncmul>(std::move(term)).setflag(options == 0 ? status_flags::expanded : 0));
184 int l = number_of_adds-1;
185 while ((l>=0) && ((++k[l]) >= number_of_add_operands[l])) {
193 return dynallocate<add>(distrseq).setflag(options == 0 ? status_flags::expanded : 0);
196 int ncmul::degree(const ex & s) const
198 if (is_equal(ex_to<basic>(s)))
201 // Sum up degrees of factors
204 deg_sum += i.degree(s);
208 int ncmul::ldegree(const ex & s) const
210 if (is_equal(ex_to<basic>(s)))
213 // Sum up degrees of factors
216 deg_sum += i.degree(s);
220 ex ncmul::coeff(const ex & s, int n) const
222 if (is_equal(ex_to<basic>(s)))
223 return n==1 ? _ex1 : _ex0;
226 coeffseq.reserve(seq.size());
229 // product of individual coeffs
230 // if a non-zero power of s is found, the resulting product will be 0
231 for (auto & it : seq)
232 coeffseq.push_back(it.coeff(s,n));
233 return dynallocate<ncmul>(std::move(coeffseq));
236 bool coeff_found = false;
237 for (auto & i : seq) {
240 coeffseq.push_back(i);
242 coeffseq.push_back(c);
248 return dynallocate<ncmul>(std::move(coeffseq));
253 size_t ncmul::count_factors(const ex & e) const
255 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
256 (is_exactly_a<ncmul>(e))) {
258 for (size_t i=0; i<e.nops(); i++)
259 factors += count_factors(e.op(i));
266 void ncmul::append_factors(exvector & v, const ex & e) const
268 if ((is_exactly_a<mul>(e)&&(e.return_type()!=return_types::commutative))||
269 (is_exactly_a<ncmul>(e))) {
270 for (size_t i=0; i<e.nops(); i++)
271 append_factors(v, e.op(i));
276 typedef std::vector<unsigned> unsignedvector;
277 typedef std::vector<exvector> exvectorvector;
279 /** Perform automatic term rewriting rules in this class. In the following
280 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
281 * stand for such expressions that contain a plain number.
282 * - ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) -> ncmul(...,x1,x2,...,x3,x4,...) (associativity)
285 * - ncmul(...,c1,...,c2,...) -> *(c1,c2,ncmul(...)) (pull out commutative elements)
286 * - ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2)) (collect elements of same type)
287 * - ncmul(x1,x2,x3,...) -> x::eval_ncmul(x1,x2,x3,...)
289 ex ncmul::eval() const
291 // The following additional rule would be nice, but produces a recursion,
292 // which must be trapped by introducing a flag that the sub-ncmuls()
293 // are already evaluated (maybe later...)
294 // ncmul(x1,x2,...,X,y1,y2,...) ->
295 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
296 // (X noncommutative_composite)
298 if (flags & status_flags::evaluated) {
302 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
303 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
305 for (auto & it : seq)
306 factors += count_factors(it);
309 assocseq.reserve(factors);
310 make_flat_inserter mf(seq, true);
311 for (auto & it : seq) {
312 ex factor = mf.handle_factor(it, 1);
313 append_factors(assocseq, factor);
317 if (assocseq.size()==1) return *(seq.begin());
320 if (assocseq.empty()) return _ex1;
322 // determine return types
323 unsignedvector rettypes(assocseq.size());
325 size_t count_commutative=0;
326 size_t count_noncommutative=0;
327 size_t count_noncommutative_composite=0;
328 for (auto & it : assocseq) {
329 rettypes[i] = it.return_type();
330 switch (rettypes[i]) {
331 case return_types::commutative:
334 case return_types::noncommutative:
335 count_noncommutative++;
337 case return_types::noncommutative_composite:
338 count_noncommutative_composite++;
341 throw(std::logic_error("ncmul::eval(): invalid return type"));
345 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
347 // ncmul(...,c1,...,c2,...) ->
348 // *(c1,c2,ncmul(...)) (pull out commutative elements)
349 if (count_commutative!=0) {
350 exvector commutativeseq;
351 commutativeseq.reserve(count_commutative+1);
352 exvector noncommutativeseq;
353 noncommutativeseq.reserve(assocseq.size()-count_commutative);
354 size_t num = assocseq.size();
355 for (size_t i=0; i<num; ++i) {
356 if (rettypes[i]==return_types::commutative)
357 commutativeseq.push_back(assocseq[i]);
359 noncommutativeseq.push_back(assocseq[i]);
361 commutativeseq.push_back(dynallocate<ncmul>(std::move(noncommutativeseq)));
362 return dynallocate<mul>(std::move(commutativeseq));
365 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
366 // (collect elements of same type)
368 if (count_noncommutative_composite==0) {
369 // there are neither commutative nor noncommutative_composite
370 // elements in assocseq
371 GINAC_ASSERT(count_commutative==0);
373 size_t assoc_num = assocseq.size();
375 std::vector<return_type_t> rttinfos;
376 evv.reserve(assoc_num);
377 rttinfos.reserve(assoc_num);
379 for (auto & it : assocseq) {
380 return_type_t ti = it.return_type_tinfo();
381 size_t rtt_num = rttinfos.size();
382 // search type in vector of known types
383 for (i=0; i<rtt_num; ++i) {
384 if(ti == rttinfos[i]) {
385 evv[i].push_back(it);
391 rttinfos.push_back(ti);
392 evv.push_back(exvector());
393 (evv.end()-1)->reserve(assoc_num);
394 (evv.end()-1)->push_back(it);
398 size_t evv_num = evv.size();
399 #ifdef DO_GINAC_ASSERT
400 GINAC_ASSERT(evv_num == rttinfos.size());
401 GINAC_ASSERT(evv_num > 0);
403 for (i=0; i<evv_num; ++i)
405 GINAC_ASSERT(s == assoc_num);
406 #endif // def DO_GINAC_ASSERT
408 // if all elements are of same type, simplify the string
410 return evv[0][0].eval_ncmul(evv[0]);
414 splitseq.reserve(evv_num);
415 for (i=0; i<evv_num; ++i)
416 splitseq.push_back(dynallocate<ncmul>(evv[i]));
418 return dynallocate<mul>(splitseq);
421 return dynallocate<ncmul>(assocseq).setflag(status_flags::evaluated);
424 ex ncmul::evalm() const
426 // Evaluate children first
428 s.reserve(seq.size());
429 for (auto & it : seq)
430 s.push_back(it.evalm());
432 // If there are only matrices, simply multiply them
433 auto it = s.begin(), itend = s.end();
434 if (is_a<matrix>(*it)) {
435 matrix prod(ex_to<matrix>(*it));
437 while (it != itend) {
438 if (!is_a<matrix>(*it))
440 prod = prod.mul(ex_to<matrix>(*it));
447 return dynallocate<ncmul>(std::move(s));
450 ex ncmul::thiscontainer(const exvector & v) const
452 return dynallocate<ncmul>(v);
455 ex ncmul::thiscontainer(exvector && v) const
457 return dynallocate<ncmul>(std::move(v));
460 ex ncmul::conjugate() const
462 if (return_type() != return_types::noncommutative) {
463 return exprseq::conjugate();
466 if (!is_clifford_tinfo(return_type_tinfo())) {
467 return exprseq::conjugate();
472 for (auto i=end(); i!=begin();) {
474 ev.push_back(i->conjugate());
476 return dynallocate<ncmul>(std::move(ev));
479 ex ncmul::real_part() const
481 return basic::real_part();
484 ex ncmul::imag_part() const
486 return basic::imag_part();
491 /** Implementation of ex::diff() for a non-commutative product. It applies
494 ex ncmul::derivative(const symbol & s) const
496 size_t num = seq.size();
500 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
501 exvector ncmulseq = seq;
502 for (size_t i=0; i<num; ++i) {
503 ex e = seq[i].diff(s);
505 addseq.push_back(dynallocate<ncmul>(ncmulseq));
508 return dynallocate<add>(addseq);
511 int ncmul::compare_same_type(const basic & other) const
513 return inherited::compare_same_type(other);
516 unsigned ncmul::return_type() const
519 return return_types::commutative;
521 bool all_commutative = true;
522 exvector::const_iterator noncommutative_element; // point to first found nc element
524 auto i = seq.begin(), end = seq.end();
526 unsigned rt = i->return_type();
527 if (rt == return_types::noncommutative_composite)
528 return rt; // one ncc -> mul also ncc
529 if ((rt == return_types::noncommutative) && (all_commutative)) {
530 // first nc element found, remember position
531 noncommutative_element = i;
532 all_commutative = false;
534 if ((rt == return_types::noncommutative) && (!all_commutative)) {
535 // another nc element found, compare type_infos
536 if(noncommutative_element->return_type_tinfo() != i->return_type_tinfo())
537 return return_types::noncommutative_composite;
541 // all factors checked
542 GINAC_ASSERT(!all_commutative); // not all factors should commutate, because this is a ncmul();
543 return all_commutative ? return_types::commutative : return_types::noncommutative;
546 return_type_t ncmul::return_type_tinfo() const
549 return make_return_type_t<ncmul>();
551 // return type_info of first noncommutative element
553 if (i.return_type() == return_types::noncommutative)
554 return i.return_type_tinfo();
556 // no noncommutative element found, should not happen
557 return make_return_type_t<ncmul>();
561 // new virtual functions which can be overridden by derived classes
567 // non-virtual functions in this class
570 exvector ncmul::expandchildren(unsigned options) const
572 auto cit = this->seq.begin(), end = this->seq.end();
574 const ex & expanded_ex = cit->expand(options);
575 if (!are_ex_trivially_equal(*cit, expanded_ex)) {
577 // copy first part of seq which hasn't changed
578 exvector s(this->seq.begin(), cit);
579 s.reserve(this->seq.size());
581 // insert changed element
582 s.push_back(expanded_ex);
587 s.push_back(cit->expand(options));
597 return exvector(); // nothing has changed
600 const exvector & ncmul::get_factors() const
609 ex reeval_ncmul(const exvector & v)
611 return dynallocate<ncmul>(v);
614 ex hold_ncmul(const exvector & v)
618 else if (v.size() == 1)
621 return dynallocate<ncmul>(v).setflag(status_flags::evaluated);
624 GINAC_BIND_UNARCHIVER(ncmul);