-
- if (flags & status_flags::evaluated) {
- GINAC_ASSERT(seq.size()>0);
- GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex1()));
- return *this;
- }
-
- int seq_size=seq.size();
- if (overall_coeff.is_equal(_ex0())) {
- // *(...,x;0) -> 0
- return _ex0();
- } else if (seq_size==0) {
- // *(;c) -> c
- return overall_coeff;
- } else if ((seq_size==1)&&overall_coeff.is_equal(_ex1())) {
- // *(x;1) -> x
- return recombine_pair_to_ex(*(seq.begin()));
- } else if ((seq_size==1) &&
- is_ex_exactly_of_type((*seq.begin()).rest,add) &&
- ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
- // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
- const add & addref=ex_to_add((*seq.begin()).rest);
- epvector distrseq;
- distrseq.reserve(addref.seq.size());
- for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
- distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
- overall_coeff));
- }
- return (new add(distrseq,
- ex_to_numeric(addref.overall_coeff).
- mul_dyn(ex_to_numeric(overall_coeff))))
- ->setflag(status_flags::dynallocated |
- status_flags::evaluated );
- }
- return this->hold();
-}
-
-exvector mul::get_indices(void) const
-{
- // return union of indices of factors
- exvector iv;
- for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
- exvector subiv=(*cit).rest.get_indices();
- iv.reserve(iv.size()+subiv.size());
- for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
- iv.push_back(*cit2);
- }
- }
- return iv;
-}
-
-ex mul::simplify_ncmul(const exvector & v) const
-{
- throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
+
+ if (flags & status_flags::evaluated) {
+ GINAC_ASSERT(seq.size()>0);
+ GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1));
+ return *this;
+ }
+
+ int seq_size = seq.size();
+ if (overall_coeff.is_zero()) {
+ // *(...,x;0) -> 0
+ return _ex0;
+ } else if (seq_size==0) {
+ // *(;c) -> c
+ return overall_coeff;
+ } else if (seq_size==1 && overall_coeff.is_equal(_ex1)) {
+ // *(x;1) -> x
+ return recombine_pair_to_ex(*(seq.begin()));
+ } else if ((seq_size==1) &&
+ is_exactly_a<add>((*seq.begin()).rest) &&
+ ex_to<numeric>((*seq.begin()).coeff).is_equal(_num1)) {
+ // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
+ const add & addref = ex_to<add>((*seq.begin()).rest);
+ epvector *distrseq = new epvector();
+ distrseq->reserve(addref.seq.size());
+ epvector::const_iterator i = addref.seq.begin(), end = addref.seq.end();
+ while (i != end) {
+ distrseq->push_back(addref.combine_pair_with_coeff_to_pair(*i, overall_coeff));
+ ++i;
+ }
+ return (new add(distrseq,
+ ex_to<numeric>(addref.overall_coeff).
+ mul_dyn(ex_to<numeric>(overall_coeff))))
+ ->setflag(status_flags::dynallocated | status_flags::evaluated);
+ }
+ return this->hold();
+}
+
+ex mul::evalf(int level) const
+{
+ if (level==1)
+ return mul(seq,overall_coeff);
+
+ if (level==-max_recursion_level)
+ throw(std::runtime_error("max recursion level reached"));
+
+ epvector *s = new epvector();
+ s->reserve(seq.size());
+
+ --level;
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ s->push_back(combine_ex_with_coeff_to_pair(i->rest.evalf(level),
+ i->coeff));
+ ++i;
+ }
+ return mul(s, overall_coeff.evalf(level));
+}
+
+ex mul::evalm() const
+{
+ // numeric*matrix
+ if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1)
+ && is_a<matrix>(seq[0].rest))
+ return ex_to<matrix>(seq[0].rest).mul(ex_to<numeric>(overall_coeff));
+
+ // Evaluate children first, look whether there are any matrices at all
+ // (there can be either no matrices or one matrix; if there were more
+ // than one matrix, it would be a non-commutative product)
+ epvector *s = new epvector;
+ s->reserve(seq.size());
+
+ bool have_matrix = false;
+ epvector::iterator the_matrix;
+
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ const ex &m = recombine_pair_to_ex(*i).evalm();
+ s->push_back(split_ex_to_pair(m));
+ if (is_a<matrix>(m)) {
+ have_matrix = true;
+ the_matrix = s->end() - 1;
+ }
+ ++i;
+ }
+
+ if (have_matrix) {
+
+ // The product contained a matrix. We will multiply all other factors
+ // into that matrix.
+ matrix m = ex_to<matrix>(the_matrix->rest);
+ s->erase(the_matrix);
+ ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
+ return m.mul_scalar(scalar);
+
+ } else
+ return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
+}
+
+ex mul::eval_ncmul(const exvector & v) const
+{
+ if (seq.empty())
+ return inherited::eval_ncmul(v);
+
+ // Find first noncommutative element and call its eval_ncmul()
+ epvector::const_iterator i = seq.begin(), end = seq.end();
+ while (i != end) {
+ if (i->rest.return_type() == return_types::noncommutative)
+ return i->rest.eval_ncmul(v);
+ ++i;
+ }
+ return inherited::eval_ncmul(v);
+}
+
+bool tryfactsubs(const ex & origfactor, const ex & patternfactor, int & nummatches, lst & repls)
+{
+ ex origbase;
+ int origexponent;
+ int origexpsign;
+
+ if (is_exactly_a<power>(origfactor) && origfactor.op(1).info(info_flags::integer)) {
+ origbase = origfactor.op(0);
+ int expon = ex_to<numeric>(origfactor.op(1)).to_int();
+ origexponent = expon > 0 ? expon : -expon;
+ origexpsign = expon > 0 ? 1 : -1;
+ } else {
+ origbase = origfactor;
+ origexponent = 1;
+ origexpsign = 1;
+ }
+
+ ex patternbase;
+ int patternexponent;
+ int patternexpsign;
+
+ if (is_exactly_a<power>(patternfactor) && patternfactor.op(1).info(info_flags::integer)) {
+ patternbase = patternfactor.op(0);
+ int expon = ex_to<numeric>(patternfactor.op(1)).to_int();
+ patternexponent = expon > 0 ? expon : -expon;
+ patternexpsign = expon > 0 ? 1 : -1;
+ } else {
+ patternbase = patternfactor;
+ patternexponent = 1;
+ patternexpsign = 1;
+ }
+
+ lst saverepls = repls;
+ if (origexponent < patternexponent || origexpsign != patternexpsign || !origbase.match(patternbase,saverepls))
+ return false;
+ repls = saverepls;
+
+ int newnummatches = origexponent / patternexponent;
+ if (newnummatches < nummatches)
+ nummatches = newnummatches;
+ return true;
+}
+
+ex mul::algebraic_subs_mul(const exmap & m, unsigned options) const
+{
+ std::vector<bool> subsed(seq.size(), false);
+ exvector subsresult(seq.size());
+
+ for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
+
+ if (is_exactly_a<mul>(it->first)) {
+
+ int nummatches = std::numeric_limits<int>::max();
+ std::vector<bool> currsubsed(seq.size(), false);
+ bool succeed = true;
+ lst repls;
+
+ for (size_t j=0; j<it->first.nops(); j++) {
+ bool found=false;
+ for (size_t k=0; k<nops(); k++) {
+ if (currsubsed[k] || subsed[k])
+ continue;
+ if (tryfactsubs(op(k), it->first.op(j), nummatches, repls)) {
+ currsubsed[k] = true;
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ succeed = false;
+ break;
+ }
+ }
+ if (!succeed)
+ continue;
+
+ bool foundfirstsubsedfactor = false;
+ for (size_t j=0; j<subsed.size(); j++) {
+ if (currsubsed[j]) {
+ if (foundfirstsubsedfactor)
+ subsresult[j] = op(j);
+ else {
+ foundfirstsubsedfactor = true;
+ subsresult[j] = op(j) * power(it->second.subs(ex(repls), subs_options::subs_no_pattern) / it->first.subs(ex(repls), subs_options::subs_no_pattern), nummatches);
+ }
+ subsed[j] = true;
+ }
+ }
+
+ } else {
+
+ int nummatches = std::numeric_limits<int>::max();
+ lst repls;
+
+ for (size_t j=0; j<this->nops(); j++) {
+ if (!subsed[j] && tryfactsubs(op(j), it->first, nummatches, repls)) {
+ subsed[j] = true;
+ subsresult[j] = op(j) * power(it->second.subs(ex(repls), subs_options::subs_no_pattern) / it->first.subs(ex(repls), subs_options::subs_no_pattern), nummatches);
+ }
+ }
+ }
+ }
+
+ bool subsfound = false;
+ for (size_t i=0; i<subsed.size(); i++) {
+ if (subsed[i]) {
+ subsfound = true;
+ break;
+ }
+ }
+ if (!subsfound)
+ return subs_one_level(m, options | subs_options::subs_algebraic);
+
+ exvector ev; ev.reserve(nops());
+ for (size_t i=0; i<nops(); i++) {
+ if (subsed[i])
+ ev.push_back(subsresult[i]);
+ else
+ ev.push_back(op(i));
+ }
+
+ return (new mul(ev))->setflag(status_flags::dynallocated);