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) : 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 * @param level cut-off in recursive evaluation */
290 ex ncmul::eval(int level) const
292 // The following additional rule would be nice, but produces a recursion,
293 // which must be trapped by introducing a flag that the sub-ncmuls()
294 // are already evaluated (maybe later...)
295 // ncmul(x1,x2,...,X,y1,y2,...) ->
296 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
297 // (X noncommutative_composite)
299 if ((level==1) && (flags & status_flags::evaluated)) {
303 exvector evaledseq=evalchildren(level);
305 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
306 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
308 for (auto & it : evaledseq)
309 factors += count_factors(it);
312 assocseq.reserve(factors);
313 make_flat_inserter mf(evaledseq, true);
314 for (auto & it : evaledseq) {
315 ex factor = mf.handle_factor(it, 1);
316 append_factors(assocseq, factor);
320 if (assocseq.size()==1) return *(seq.begin());
323 if (assocseq.empty()) return _ex1;
325 // determine return types
326 unsignedvector rettypes(assocseq.size());
328 size_t count_commutative=0;
329 size_t count_noncommutative=0;
330 size_t count_noncommutative_composite=0;
331 for (auto & it : assocseq) {
332 rettypes[i] = it.return_type();
333 switch (rettypes[i]) {
334 case return_types::commutative:
337 case return_types::noncommutative:
338 count_noncommutative++;
340 case return_types::noncommutative_composite:
341 count_noncommutative_composite++;
344 throw(std::logic_error("ncmul::eval(): invalid return type"));
348 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
350 // ncmul(...,c1,...,c2,...) ->
351 // *(c1,c2,ncmul(...)) (pull out commutative elements)
352 if (count_commutative!=0) {
353 exvector commutativeseq;
354 commutativeseq.reserve(count_commutative+1);
355 exvector noncommutativeseq;
356 noncommutativeseq.reserve(assocseq.size()-count_commutative);
357 size_t num = assocseq.size();
358 for (size_t i=0; i<num; ++i) {
359 if (rettypes[i]==return_types::commutative)
360 commutativeseq.push_back(assocseq[i]);
362 noncommutativeseq.push_back(assocseq[i]);
364 commutativeseq.push_back(dynallocate<ncmul>(std::move(noncommutativeseq)));
365 return dynallocate<mul>(std::move(commutativeseq));
368 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
369 // (collect elements of same type)
371 if (count_noncommutative_composite==0) {
372 // there are neither commutative nor noncommutative_composite
373 // elements in assocseq
374 GINAC_ASSERT(count_commutative==0);
376 size_t assoc_num = assocseq.size();
378 std::vector<return_type_t> rttinfos;
379 evv.reserve(assoc_num);
380 rttinfos.reserve(assoc_num);
382 for (auto & it : assocseq) {
383 return_type_t ti = it.return_type_tinfo();
384 size_t rtt_num = rttinfos.size();
385 // search type in vector of known types
386 for (i=0; i<rtt_num; ++i) {
387 if(ti == rttinfos[i]) {
388 evv[i].push_back(it);
394 rttinfos.push_back(ti);
395 evv.push_back(exvector());
396 (evv.end()-1)->reserve(assoc_num);
397 (evv.end()-1)->push_back(it);
401 size_t evv_num = evv.size();
402 #ifdef DO_GINAC_ASSERT
403 GINAC_ASSERT(evv_num == rttinfos.size());
404 GINAC_ASSERT(evv_num > 0);
406 for (i=0; i<evv_num; ++i)
408 GINAC_ASSERT(s == assoc_num);
409 #endif // def DO_GINAC_ASSERT
411 // if all elements are of same type, simplify the string
413 return evv[0][0].eval_ncmul(evv[0]);
417 splitseq.reserve(evv_num);
418 for (i=0; i<evv_num; ++i)
419 splitseq.push_back(dynallocate<ncmul>(evv[i]));
421 return dynallocate<mul>(splitseq);
424 return dynallocate<ncmul>(assocseq).setflag(status_flags::evaluated);
427 ex ncmul::evalm() const
429 // Evaluate children first
431 s.reserve(seq.size());
432 for (auto & it : seq)
433 s.push_back(it.evalm());
435 // If there are only matrices, simply multiply them
436 auto it = s.begin(), itend = s.end();
437 if (is_a<matrix>(*it)) {
438 matrix prod(ex_to<matrix>(*it));
440 while (it != itend) {
441 if (!is_a<matrix>(*it))
443 prod = prod.mul(ex_to<matrix>(*it));
450 return dynallocate<ncmul>(std::move(s));
453 ex ncmul::thiscontainer(const exvector & v) const
455 return dynallocate<ncmul>(v);
458 ex ncmul::thiscontainer(exvector && v) const
460 return dynallocate<ncmul>(std::move(v));
463 ex ncmul::conjugate() const
465 if (return_type() != return_types::noncommutative) {
466 return exprseq::conjugate();
469 if (!is_clifford_tinfo(return_type_tinfo())) {
470 return exprseq::conjugate();
475 for (auto i=end(); i!=begin();) {
477 ev.push_back(i->conjugate());
479 return dynallocate<ncmul>(std::move(ev)).eval();
482 ex ncmul::real_part() const
484 return basic::real_part();
487 ex ncmul::imag_part() const
489 return basic::imag_part();
494 /** Implementation of ex::diff() for a non-commutative product. It applies
497 ex ncmul::derivative(const symbol & s) const
499 size_t num = seq.size();
503 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
504 exvector ncmulseq = seq;
505 for (size_t i=0; i<num; ++i) {
506 ex e = seq[i].diff(s);
508 addseq.push_back(dynallocate<ncmul>(ncmulseq));
511 return dynallocate<add>(addseq);
514 int ncmul::compare_same_type(const basic & other) const
516 return inherited::compare_same_type(other);
519 unsigned ncmul::return_type() const
522 return return_types::commutative;
524 bool all_commutative = true;
525 exvector::const_iterator noncommutative_element; // point to first found nc element
527 exvector::const_iterator i = seq.begin(), end = seq.end();
529 unsigned rt = i->return_type();
530 if (rt == return_types::noncommutative_composite)
531 return rt; // one ncc -> mul also ncc
532 if ((rt == return_types::noncommutative) && (all_commutative)) {
533 // first nc element found, remember position
534 noncommutative_element = i;
535 all_commutative = false;
537 if ((rt == return_types::noncommutative) && (!all_commutative)) {
538 // another nc element found, compare type_infos
539 if(noncommutative_element->return_type_tinfo() != i->return_type_tinfo())
540 return return_types::noncommutative_composite;
544 // all factors checked
545 GINAC_ASSERT(!all_commutative); // not all factors should commutate, because this is a ncmul();
546 return all_commutative ? return_types::commutative : return_types::noncommutative;
549 return_type_t ncmul::return_type_tinfo() const
552 return make_return_type_t<ncmul>();
554 // return type_info of first noncommutative element
556 if (i.return_type() == return_types::noncommutative)
557 return i.return_type_tinfo();
559 // no noncommutative element found, should not happen
560 return make_return_type_t<ncmul>();
564 // new virtual functions which can be overridden by derived classes
570 // non-virtual functions in this class
573 exvector ncmul::expandchildren(unsigned options) const
575 auto cit = this->seq.begin(), end = this->seq.end();
577 const ex & expanded_ex = cit->expand(options);
578 if (!are_ex_trivially_equal(*cit, expanded_ex)) {
580 // copy first part of seq which hasn't changed
581 exvector s(this->seq.begin(), cit);
582 s.reserve(this->seq.size());
584 // insert changed element
585 s.push_back(expanded_ex);
590 s.push_back(cit->expand(options));
600 return exvector(); // nothing has changed
603 const exvector & ncmul::get_factors() const
612 ex reeval_ncmul(const exvector & v)
614 return dynallocate<ncmul>(v);
617 ex hold_ncmul(const exvector & v)
621 else if (v.size() == 1)
624 return dynallocate<ncmul>(v).setflag(status_flags::evaluated);
627 GINAC_BIND_UNARCHIVER(ncmul);