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_a<print_tree>(c)) {
123 inherited::print(c, level);
125 } else if (is_a<print_csrc>(c)) {
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 /** Perform automatic term rewriting rules in this class. In the following
292 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
293 * stand for such expressions that contain a plain number.
294 * - ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) -> ncmul(...,x1,x2,...,x3,x4,...) (associativity)
297 * - ncmul(...,c1,...,c2,...) -> *(c1,c2,ncmul(...)) (pull out commutative elements)
298 * - ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2)) (collect elements of same type)
299 * - ncmul(x1,x2,x3,...) -> x::simplify_ncmul(x1,x2,x3,...)
301 * @param level cut-off in recursive evaluation */
302 ex ncmul::eval(int level) const
304 // The following additional rule would be nice, but produces a recursion,
305 // which must be trapped by introducing a flag that the sub-ncmuls()
306 // are already evaluated (maybe later...)
307 // ncmul(x1,x2,...,X,y1,y2,...) ->
308 // ncmul(ncmul(x1,x2,...),X,ncmul(y1,y2,...)
309 // (X noncommutative_composite)
311 if ((level==1) && (flags & status_flags::evaluated)) {
315 exvector evaledseq=evalchildren(level);
317 // ncmul(...,*(x1,x2),...,ncmul(x3,x4),...) ->
318 // ncmul(...,x1,x2,...,x3,x4,...) (associativity)
319 unsigned factors = 0;
320 exvector::const_iterator cit = evaledseq.begin(), citend = evaledseq.end();
321 while (cit != citend)
322 factors += count_factors(*cit++);
325 assocseq.reserve(factors);
326 cit = evaledseq.begin();
327 while (cit != citend)
328 append_factors(assocseq, *cit++);
331 if (assocseq.size()==1) return *(seq.begin());
334 if (assocseq.empty()) return _ex1;
336 // determine return types
337 unsignedvector rettypes;
338 rettypes.reserve(assocseq.size());
340 unsigned count_commutative=0;
341 unsigned count_noncommutative=0;
342 unsigned count_noncommutative_composite=0;
343 cit = assocseq.begin(); citend = assocseq.end();
344 while (cit != citend) {
345 switch (rettypes[i] = cit->return_type()) {
346 case return_types::commutative:
349 case return_types::noncommutative:
350 count_noncommutative++;
352 case return_types::noncommutative_composite:
353 count_noncommutative_composite++;
356 throw(std::logic_error("ncmul::eval(): invalid return type"));
360 GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
362 // ncmul(...,c1,...,c2,...) ->
363 // *(c1,c2,ncmul(...)) (pull out commutative elements)
364 if (count_commutative!=0) {
365 exvector commutativeseq;
366 commutativeseq.reserve(count_commutative+1);
367 exvector noncommutativeseq;
368 noncommutativeseq.reserve(assocseq.size()-count_commutative);
369 unsigned num = assocseq.size();
370 for (unsigned i=0; i<num; ++i) {
371 if (rettypes[i]==return_types::commutative)
372 commutativeseq.push_back(assocseq[i]);
374 noncommutativeseq.push_back(assocseq[i]);
376 commutativeseq.push_back((new ncmul(noncommutativeseq,1))->setflag(status_flags::dynallocated));
377 return (new mul(commutativeseq))->setflag(status_flags::dynallocated);
380 // ncmul(x1,y1,x2,y2) -> *(ncmul(x1,x2),ncmul(y1,y2))
381 // (collect elements of same type)
383 if (count_noncommutative_composite==0) {
384 // there are neither commutative nor noncommutative_composite
385 // elements in assocseq
386 GINAC_ASSERT(count_commutative==0);
388 unsigned assoc_num = assocseq.size();
390 unsignedvector rttinfos;
391 evv.reserve(assoc_num);
392 rttinfos.reserve(assoc_num);
394 cit = assocseq.begin(), citend = assocseq.end();
395 while (cit != citend) {
396 unsigned ti = cit->return_type_tinfo();
397 unsigned rtt_num = rttinfos.size();
398 // search type in vector of known types
399 for (i=0; i<rtt_num; ++i) {
400 if (ti == rttinfos[i]) {
401 evv[i].push_back(*cit);
407 rttinfos.push_back(ti);
408 evv.push_back(exvector());
409 (evv.end()-1)->reserve(assoc_num);
410 (evv.end()-1)->push_back(*cit);
415 unsigned evv_num = evv.size();
416 #ifdef DO_GINAC_ASSERT
417 GINAC_ASSERT(evv_num == rttinfos.size());
418 GINAC_ASSERT(evv_num > 0);
420 for (i=0; i<evv_num; ++i)
422 GINAC_ASSERT(s == assoc_num);
423 #endif // def DO_GINAC_ASSERT
425 // if all elements are of same type, simplify the string
427 return evv[0][0].simplify_ncmul(evv[0]);
430 splitseq.reserve(evv_num);
431 for (i=0; i<evv_num; ++i)
432 splitseq.push_back((new ncmul(evv[i]))->setflag(status_flags::dynallocated));
434 return (new mul(splitseq))->setflag(status_flags::dynallocated);
437 return (new ncmul(assocseq))->setflag(status_flags::dynallocated |
438 status_flags::evaluated);
441 ex ncmul::evalm(void) const
443 // Evaluate children first
444 exvector *s = new exvector;
445 s->reserve(seq.size());
446 exvector::const_iterator it = seq.begin(), itend = seq.end();
447 while (it != itend) {
448 s->push_back(it->evalm());
452 // If there are only matrices, simply multiply them
453 it = s->begin(); itend = s->end();
454 if (is_ex_of_type(*it, matrix)) {
455 matrix prod(ex_to<matrix>(*it));
457 while (it != itend) {
458 if (!is_ex_of_type(*it, matrix))
460 prod = prod.mul(ex_to<matrix>(*it));
468 return (new ncmul(s))->setflag(status_flags::dynallocated);
471 ex ncmul::thisexprseq(const exvector & v) const
473 return (new ncmul(v))->setflag(status_flags::dynallocated);
476 ex ncmul::thisexprseq(exvector * vp) const
478 return (new ncmul(vp))->setflag(status_flags::dynallocated);
483 /** Implementation of ex::diff() for a non-commutative product. It applies
486 ex ncmul::derivative(const symbol & s) const
488 unsigned num = seq.size();
492 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
493 exvector ncmulseq = seq;
494 for (unsigned i=0; i<num; ++i) {
495 ex e = seq[i].diff(s);
497 addseq.push_back((new ncmul(ncmulseq))->setflag(status_flags::dynallocated));
500 return (new add(addseq))->setflag(status_flags::dynallocated);
503 int ncmul::compare_same_type(const basic & other) const
505 return inherited::compare_same_type(other);
508 unsigned ncmul::return_type(void) const
511 return return_types::commutative;
513 bool all_commutative = true;
514 exvector::const_iterator noncommutative_element; // point to first found nc element
516 exvector::const_iterator i = seq.begin(), end = seq.end();
518 unsigned rt = i->return_type();
519 if (rt == return_types::noncommutative_composite)
520 return rt; // one ncc -> mul also ncc
521 if ((rt == return_types::noncommutative) && (all_commutative)) {
522 // first nc element found, remember position
523 noncommutative_element = i;
524 all_commutative = false;
526 if ((rt == return_types::noncommutative) && (!all_commutative)) {
527 // another nc element found, compare type_infos
528 if (noncommutative_element->return_type_tinfo() != i->return_type_tinfo()) {
529 // diffent types -> mul is ncc
530 return return_types::noncommutative_composite;
535 // all factors checked
536 GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
537 return all_commutative ? return_types::commutative : return_types::noncommutative;
540 unsigned ncmul::return_type_tinfo(void) const
545 // return type_info of first noncommutative element
546 exvector::const_iterator i = seq.begin(), end = seq.end();
548 if (i->return_type() == return_types::noncommutative)
549 return i->return_type_tinfo();
553 // no noncommutative element found, should not happen
558 // new virtual functions which can be overridden by derived classes
564 // non-virtual functions in this class
567 exvector ncmul::expandchildren(unsigned options) const
570 s.reserve(seq.size());
571 exvector::const_iterator it = seq.begin(), itend = seq.end();
572 while (it != itend) {
573 s.push_back(it->expand(options));
579 const exvector & ncmul::get_factors(void) const
588 ex nonsimplified_ncmul(const exvector & v)
590 return (new ncmul(v))->setflag(status_flags::dynallocated);
593 ex simplified_ncmul(const exvector & v)
597 else if (v.size() == 1)
600 return (new ncmul(v))->setflag(status_flags::dynallocated |
601 status_flags::evaluated);