DEFAULT_ARCHIVING(mul)
//////////
-// functions overriding virtual functions from bases classes
+// functions overriding virtual functions from base classes
//////////
// public
c.s << "(";
if (!overall_coeff.is_equal(_ex1())) {
- overall_coeff.bp->print(c, precedence());
+ overall_coeff.print(c, precedence());
c.s << "*";
}
first = false;
}
recombine_pair_to_ex(*it).print(c, precedence());
- it++;
+ ++it;
}
if (precedence() <= level) {
return _ex0();
}
+/** Perform automatic term rewriting rules in this class. In the following
+ * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
+ * stand for such expressions that contain a plain number.
+ * - *(...,x;0) -> 0
+ * - *(+(x1,x2,...);c) -> *(+(*(x1,c),*(x2,c),...))
+ * - *(x;1) -> x
+ * - *(;c) -> c
+ *
+ * @param level cut-off in recursive evaluation */
ex mul::eval(int level) const
{
- // simplifications *(...,x;0) -> 0
- // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
- // *(x;1) -> x
- // *(;c) -> c
-
debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
epvector *evaled_seqp = evalchildren(level);
#ifdef DO_GINAC_ASSERT
epvector::const_iterator i = seq.begin(), end = seq.end();
while (i != end) {
- GINAC_ASSERT((!is_ex_exactly_of_type(i->rest, mul)) ||
+ GINAC_ASSERT((!is_exactly_a<mul>(i->rest)) ||
(!(ex_to<numeric>(i->coeff).is_integer())));
- GINAC_ASSERT(!(cit->is_canonical_numeric()));
+ GINAC_ASSERT(!(i->is_canonical_numeric()));
if (is_ex_exactly_of_type(recombine_pair_to_ex(*i), numeric))
print(print_tree(std::cerr));
- GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*i), numeric));
+ GINAC_ASSERT(!is_exactly_a<numeric>(recombine_pair_to_ex(*i)));
/* for paranoia */
expair p = split_ex_to_pair(recombine_pair_to_ex(*i));
GINAC_ASSERT(p.rest.is_equal(i->rest));
void mul::combine_overall_coeff(const ex & c)
{
- GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
- GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
+ GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
+ GINAC_ASSERT(is_exactly_a<numeric>(c));
overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c));
}
void mul::combine_overall_coeff(const ex & c1, const ex & c2)
{
- GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
- GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
- GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
+ GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
+ GINAC_ASSERT(is_exactly_a<numeric>(c1));
+ GINAC_ASSERT(is_exactly_a<numeric>(c2));
overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c1).power(ex_to<numeric>(c2)));
}
bool mul::can_make_flat(const expair & p) const
{
- GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
+ GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
// this assertion will probably fail somewhere
// it would require a more careful make_flat, obeying the power laws
// probably should return true only if p.coeff is integer
ex mul::expand(unsigned options) const
{
- if (flags & status_flags::expanded)
- return *this;
-
- exvector sub_expanded_seq;
-
+ // First, expand the children
epvector * expanded_seqp = expandchildren(options);
-
- const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
-
+ const epvector & expanded_seq = (expanded_seqp == NULL) ? seq : *expanded_seqp;
+
+ // Now, look for all the factors that are sums and multiply each one out
+ // with the next one that is found while collecting the factors which are
+ // not sums
int number_of_adds = 0;
+ ex last_expanded = _ex1();
epvector non_adds;
non_adds.reserve(expanded_seq.size());
- epvector::const_iterator cit = expanded_seq.begin();
- epvector::const_iterator last = expanded_seq.end();
- ex last_expanded = _ex1();
- while (cit!=last) {
- if (is_ex_exactly_of_type((*cit).rest,add) &&
- ((*cit).coeff.is_equal(_ex1()))) {
+ epvector::const_iterator cit = expanded_seq.begin(), last = expanded_seq.end();
+ while (cit != last) {
+ if (is_ex_exactly_of_type(cit->rest, add) &&
+ (cit->coeff.is_equal(_ex1()))) {
++number_of_adds;
- if (is_ex_exactly_of_type(last_expanded,add)) {
- // expand adds
+ if (is_ex_exactly_of_type(last_expanded, add)) {
const add & add1 = ex_to<add>(last_expanded);
- const add & add2 = ex_to<add>((*cit).rest);
+ const add & add2 = ex_to<add>(cit->rest);
int n1 = add1.nops();
int n2 = add2.nops();
exvector distrseq;
distrseq.reserve(n1*n2);
for (int i1=0; i1<n1; ++i1) {
for (int i2=0; i2<n2; ++i2) {
- distrseq.push_back(add1.op(i1)*add2.op(i2));
+ distrseq.push_back(add1.op(i1) * add2.op(i2));
}
}
- last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
+ last_expanded = (new add(distrseq))->
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
} else {
non_adds.push_back(split_ex_to_pair(last_expanded));
- last_expanded = (*cit).rest;
+ last_expanded = cit->rest;
}
} else {
non_adds.push_back(*cit);
}
if (expanded_seqp)
delete expanded_seqp;
-
- if (is_ex_exactly_of_type(last_expanded,add)) {
- add const & finaladd = ex_to<add>(last_expanded);
+
+ // Now the only remaining thing to do is to multiply the factors which
+ // were not sums into the "last_expanded" sum
+ if (is_ex_exactly_of_type(last_expanded, add)) {
+ const add & finaladd = ex_to<add>(last_expanded);
exvector distrseq;
int n = finaladd.nops();
distrseq.reserve(n);
for (int i=0; i<n; ++i) {
epvector factors = non_adds;
factors.push_back(split_ex_to_pair(finaladd.op(i)));
- distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
+ distrseq.push_back((new mul(factors, overall_coeff))->
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
}
return ((new add(distrseq))->
- setflag(status_flags::dynallocated | status_flags::expanded));
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
}
non_adds.push_back(split_ex_to_pair(last_expanded));
return (new mul(non_adds, overall_coeff))->
- setflag(status_flags::dynallocated | status_flags::expanded);
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
}