3 * Implementation of GiNaC's non-commutative products of expressions. */
6 * GiNaC Copyright (C) 1999-2001 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 GINAC_IMPLEMENT_REGISTERED_CLASS(ncmul, exprseq)
42 // default constructor, destructor, copy constructor assignment operator and helpers
47 debugmsg("ncmul default constructor",LOGLEVEL_CONSTRUCT);
48 tinfo_key = TINFO_ncmul;
52 DEFAULT_DESTROY(ncmul)
60 ncmul::ncmul(const ex & lh, const ex & rh) : inherited(lh,rh)
62 debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
63 tinfo_key = TINFO_ncmul;
66 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3) : inherited(f1,f2,f3)
68 debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
69 tinfo_key = TINFO_ncmul;
72 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
73 const ex & f4) : inherited(f1,f2,f3,f4)
75 debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
76 tinfo_key = TINFO_ncmul;
79 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
80 const ex & f4, const ex & f5) : inherited(f1,f2,f3,f4,f5)
82 debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
83 tinfo_key = TINFO_ncmul;
86 ncmul::ncmul(const ex & f1, const ex & f2, const ex & f3,
87 const ex & f4, const ex & f5, const ex & f6) : inherited(f1,f2,f3,f4,f5,f6)
89 debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
90 tinfo_key = TINFO_ncmul;
93 ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
95 debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
96 tinfo_key = TINFO_ncmul;
99 ncmul::ncmul(exvector * vp) : inherited(vp)
101 debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
102 tinfo_key = TINFO_ncmul;
109 DEFAULT_ARCHIVING(ncmul)
112 // functions overriding virtual functions from base classes
117 void ncmul::print(const print_context & c, unsigned level) const
119 debugmsg("ncmul print", LOGLEVEL_PRINT);
121 if (is_of_type(c, print_tree)) {
123 inherited::print(c, level);
125 } else if (is_of_type(c, print_csrc)) {
128 exvector::const_iterator it = seq.begin(), itend = seq.end()-1;
129 while (it != itend) {
130 it->print(c, precedence());
134 it->print(c, precedence());
138 printseq(c, '(', '*', ')', precedence(), level);
141 bool ncmul::info(unsigned inf) const
143 return inherited::info(inf);
146 typedef std::vector<int> intvector;
148 ex ncmul::expand(unsigned options) const
150 // First, expand the children
151 exvector expanded_seq = expandchildren(options);
153 // Now, look for all the factors that are sums and remember their
154 // position and number of terms.
155 intvector positions_of_adds(expanded_seq.size());
156 intvector number_of_add_operands(expanded_seq.size());
158 int number_of_adds = 0;
159 int number_of_expanded_terms = 1;
161 unsigned current_position = 0;
162 exvector::const_iterator last = expanded_seq.end();
163 for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
164 if (is_exactly_a<add>(*cit)) {
165 positions_of_adds[number_of_adds] = current_position;
166 unsigned num_ops = cit->nops();
167 number_of_add_operands[number_of_adds] = num_ops;
168 number_of_expanded_terms *= num_ops;
174 // If there are no sums, we are done
175 if (number_of_adds == 0)
176 return (new ncmul(expanded_seq, true))->
177 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
179 // Now, form all possible products of the terms of the sums with the
180 // remaining factors, and add them together
182 distrseq.reserve(number_of_expanded_terms);
184 intvector k(number_of_adds);
187 exvector term = expanded_seq;
188 for (int i=0; i<number_of_adds; i++)
189 term[positions_of_adds[i]] = expanded_seq[positions_of_adds[i]].op(k[i]);
190 distrseq.push_back((new ncmul(term, true))->
191 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
194 int l = number_of_adds-1;
195 while ((l>=0) && ((++k[l]) >= number_of_add_operands[l])) {
203 return (new add(distrseq))->
204 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
207 int ncmul::degree(const ex & s) const
209 // Sum up degrees of factors
211 exvector::const_iterator i = seq.begin(), end = seq.end();
213 deg_sum += i->degree(s);
219 int ncmul::ldegree(const ex & s) const
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
234 coeffseq.reserve(seq.size());
237 // product of individual coeffs
238 // if a non-zero power of s is found, the resulting product will be 0
239 exvector::const_iterator it=seq.begin();
240 while (it!=seq.end()) {
241 coeffseq.push_back((*it).coeff(s,n));
244 return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
247 exvector::const_iterator i = seq.begin(), end = seq.end();
248 bool coeff_found = false;
250 ex c = i->coeff(s,n);
252 coeffseq.push_back(*i);
254 coeffseq.push_back(c);
260 if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
265 unsigned ncmul::count_factors(const ex & e) const
267 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
268 (is_ex_exactly_of_type(e,ncmul))) {
270 for (unsigned i=0; i<e.nops(); i++)
271 factors += count_factors(e.op(i));
278 void ncmul::append_factors(exvector & v, const ex & e) const
280 if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
281 (is_ex_exactly_of_type(e,ncmul))) {
282 for (unsigned i=0; i<e.nops(); i++)
283 append_factors(v,e.op(i));
288 typedef std::vector<unsigned> unsignedvector;
289 typedef std::vector<exvector> exvectorvector;
291 ex ncmul::eval(int level) const
293 // simplifications: ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
294 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
297 // ncmul(...,c1,...,c2,...)
298 // *(c1,c2,ncmul(...)) (pull out commutative elements)
299 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
300 // (collect elements of same type)
301 // ncmul(x1,x2,x3,...) -> x::simplify_ncmul(x1,x2,x3,...)
302 // the following rule would be nice, but produces a recursion,
303 // which must be trapped by introducing a flag that the sub-ncmuls()
304 // are already evaluated (maybe later...)
305 // ncmul(x1,x2,...,X,y1,y2,...) ->
306 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
307 // (X noncommutative_composite)
309 if ((level==1) && (flags & status_flags::evaluated)) {
313 exvector evaledseq=evalchildren(level);
315 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
316 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
317 unsigned factors = 0;
318 exvector::const_iterator cit = evaledseq.begin(), citend = evaledseq.end();
319 while (cit != citend)
320 factors += count_factors(*cit++);
323 assocseq.reserve(factors);
324 cit = evaledseq.begin();
325 while (cit != citend)
326 append_factors(assocseq, *cit++);
329 if (assocseq.size()==1) return *(seq.begin());
332 if (assocseq.empty()) return _ex1();
334 // determine return types
335 unsignedvector rettypes;
336 rettypes.reserve(assocseq.size());
338 unsigned count_commutative=0;
339 unsigned count_noncommutative=0;
340 unsigned count_noncommutative_composite=0;
341 cit = assocseq.begin(); citend = assocseq.end();
342 while (cit != citend) {
343 switch (rettypes[i] = cit->return_type()) {
344 case return_types::commutative:
347 case return_types::noncommutative:
348 count_noncommutative++;
350 case return_types::noncommutative_composite:
351 count_noncommutative_composite++;
354 throw(std::logic_error("ncmul::eval(): invalid return type"));
358 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
360 // ncmul(...,c1,...,c2,...) ->
361 // *(c1,c2,ncmul(...)) (pull out commutative elements)
362 if (count_commutative!=0) {
363 exvector commutativeseq;
364 commutativeseq.reserve(count_commutative+1);
365 exvector noncommutativeseq;
366 noncommutativeseq.reserve(assocseq.size()-count_commutative);
367 unsigned num = assocseq.size();
368 for (unsigned i=0; i<num; ++i) {
369 if (rettypes[i]==return_types::commutative)
370 commutativeseq.push_back(assocseq[i]);
372 noncommutativeseq.push_back(assocseq[i]);
374 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
375 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
378 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
379 // (collect elements of same type)
381 if (count_noncommutative_composite==0) {
382 // there are neither commutative nor noncommutative_composite
383 // elements in assocseq
384 GINAC_ASSERT(count_commutative==0);
386 unsigned assoc_num = assocseq.size();
388 unsignedvector rttinfos;
389 evv.reserve(assoc_num);
390 rttinfos.reserve(assoc_num);
392 cit = assocseq.begin(), citend = assocseq.end();
393 while (cit != citend) {
394 unsigned ti = cit->return_type_tinfo();
395 unsigned rtt_num = rttinfos.size();
396 // search type in vector of known types
397 for (i=0; i<rtt_num; ++i) {
398 if (ti == rttinfos[i]) {
399 evv[i].push_back(*cit);
405 rttinfos.push_back(ti);
406 evv.push_back(exvector());
407 (evv.end()-1)->reserve(assoc_num);
408 (evv.end()-1)->push_back(*cit);
413 unsigned evv_num = evv.size();
414 #ifdef DO_GINAC_ASSERT
415 GINAC_ASSERT(evv_num == rttinfos.size());
416 GINAC_ASSERT(evv_num > 0);
418 for (i=0; i<evv_num; ++i)
420 GINAC_ASSERT(s == assoc_num);
421 #endif // def DO_GINAC_ASSERT
423 // if all elements are of same type, simplify the string
425 return evv[0][0].simplify_ncmul(evv[0]);
428 splitseq.reserve(evv_num);
429 for (i=0; i<evv_num; ++i)
430 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
432 return (new mul(splitseq))->setflag(status_flags::dynallocated);
435 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
436 status_flags::evaluated);
439 ex ncmul::evalm(void) const
441 // Evaluate children first
442 exvector *s = new exvector;
443 s->reserve(seq.size());
444 exvector::const_iterator it = seq.begin(), itend = seq.end();
445 while (it != itend) {
446 s->push_back(it->evalm());
450 // If there are only matrices, simply multiply them
451 it = s->begin(); itend = s->end();
452 if (is_ex_of_type(*it, matrix)) {
453 matrix prod(ex_to<matrix>(*it));
455 while (it != itend) {
456 if (!is_ex_of_type(*it, matrix))
458 prod = prod.mul(ex_to<matrix>(*it));
466 return (new ncmul(s))->setflag(status_flags::dynallocated);
469 ex ncmul::thisexprseq(const exvector & v) const
471 return (new ncmul(v))->setflag(status_flags::dynallocated);
474 ex ncmul::thisexprseq(exvector * vp) const
476 return (new ncmul(vp))->setflag(status_flags::dynallocated);
481 /** Implementation of ex::diff() for a non-commutative product. It applies
484 ex ncmul::derivative(const symbol & s) const
486 unsigned num = seq.size();
490 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
491 exvector ncmulseq = seq;
492 for (unsigned i=0; i<num; ++i) {
493 ex e = seq[i].diff(s);
495 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
498 return (new add(addseq))->setflag(status_flags::dynallocated);
501 int ncmul::compare_same_type(const basic & other) const
503 return inherited::compare_same_type(other);
506 unsigned ncmul::return_type(void) const
509 return return_types::commutative;
511 bool all_commutative = true;
512 exvector::const_iterator noncommutative_element; // point to first found nc element
514 exvector::const_iterator i = seq.begin(), end = seq.end();
516 unsigned rt = i->return_type();
517 if (rt == return_types::noncommutative_composite)
518 return rt; // one ncc -> mul also ncc
519 if ((rt == return_types::noncommutative) && (all_commutative)) {
520 // first nc element found, remember position
521 noncommutative_element = i;
522 all_commutative = false;
524 if ((rt == return_types::noncommutative) && (!all_commutative)) {
525 // another nc element found, compare type_infos
526 if (noncommutative_element->return_type_tinfo() != i->return_type_tinfo()) {
527 // diffent types -> mul is ncc
528 return return_types::noncommutative_composite;
533 // all factors checked
534 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
535 return all_commutative ? return_types::commutative : return_types::noncommutative;
538 unsigned ncmul::return_type_tinfo(void) const
543 // return type_info of first noncommutative element
544 exvector::const_iterator i = seq.begin(), end = seq.end();
546 if (i->return_type() == return_types::noncommutative)
547 return i->return_type_tinfo();
551 // no noncommutative element found, should not happen
556 // new virtual functions which can be overridden by derived classes
562 // non-virtual functions in this class
565 exvector ncmul::expandchildren(unsigned options) const
568 s.reserve(seq.size());
569 exvector::const_iterator it = seq.begin(), itend = seq.end();
570 while (it != itend) {
571 s.push_back(it->expand(options));
577 const exvector & ncmul::get_factors(void) const
586 ex nonsimplified_ncmul(const exvector & v)
588 return (new ncmul(v))->setflag(status_flags::dynallocated);
591 ex simplified_ncmul(const exvector & v)
595 else if (v.size() == 1)
598 return (new ncmul(v))->setflag(status_flags::dynallocated |
599 status_flags::evaluated);