33 degree = d > degree ? d : degree;
40 bool Addition::needParenthesisWithParent(
const Expression * e)
const {
42 return e->isOfType(types, 6);
47 Expression * Addition::shallowReduce(Context& context, AngleUnit angleUnit) {
48 Expression * e = Expression::shallowReduce(context, angleUnit);
56 while (i < initialNumberOfOperands) {
68 #if MATRIX_EXACT_REDUCING 78 Matrix * resultMatrix =
static_cast<Matrix *
>(lastOperand);
79 n = resultMatrix->numberOfRows();
80 m = resultMatrix->numberOfColumns();
87 int on = currentMatrix->numberOfRows();
88 int om = currentMatrix->numberOfColumns();
89 if (on != n || om != m) {
93 for (
int j = 0; j < n*m; j++) {
95 Expression * resultMatrixEntryJ = resultMatrix->editableOperand(j);
96 resultMatrix->replaceOperand(resultMatrixEntryJ, a,
false);
97 a->addOperand(currentMatrix->editableOperand(j));
98 a->addOperand(resultMatrixEntryJ);
99 a->shallowReduce(context, angleUnit);
101 currentMatrix->detachOperands();
106 for (
int i = 0; i < n*m; i++) {
108 Expression * entryI = resultMatrix->editableOperand(i);
109 resultMatrix->replaceOperand(entryI, a,
false);
110 a->addOperand(entryI);
111 a->shallowReduce(context, angleUnit);
113 return replaceWith(resultMatrix,
true)->shallowReduce(context, angleUnit);
131 if (TermsHaveIdenticalNonRationalFactors(o1, o2)) {
132 factorizeOperands(o1, o2, context, angleUnit);
158 result = factorizeOnCommonDenominator(context, angleUnit);
164 Expression * Addition::factorizeOnCommonDenominator(Context & context, AngleUnit angleUnit) {
173 commonDenominator->addMissingFactors(denominator, context, angleUnit);
177 if (commonDenominator->numberOfOperands() == 0) {
178 delete commonDenominator;
188 numerator->addOperand(m);
189 m->privateShallowReduce(context, angleUnit,
true,
false);
196 numerator->shallowReduce(context, angleUnit);
199 commonDenominator->deepReduce(context, angleUnit);
200 inverseDenominator->shallowReduce(context, angleUnit);
207 void Addition::factorizeOperands(Expression * e1, Expression * e2, Context & context, AngleUnit angleUnit) {
211 assert(e1->parent() ==
this && e2->parent() ==
this);
225 e1->replaceWith(m,
false);
229 m->replaceOperand(m->operand(0), r,
true);
235 m->shallowReduce(context, angleUnit);
238 const Rational Addition::RationalFactor(Expression * e) {
240 return *(
static_cast<const Rational *
>(e->operand(0)));
245 static inline int NumberOfNonRationalFactors(
const Expression * e) {
249 int result = e->numberOfOperands();
256 static inline const Expression * FirstNonRationalFactor(
const Expression * e) {
263 return e->operand(0);
266 bool Addition::TermsHaveIdenticalNonRationalFactors(
const Expression * e1,
const Expression * e2) {
270 int numberOfNonRationalFactorsInE1 = NumberOfNonRationalFactors(e1);
271 int numberOfNonRationalFactorsInE2 = NumberOfNonRationalFactors(e2);
273 if (numberOfNonRationalFactorsInE1 != numberOfNonRationalFactorsInE2) {
277 int numberOfNonRationalFactors = numberOfNonRationalFactorsInE1;
278 if (numberOfNonRationalFactors == 1) {
279 return FirstNonRationalFactor(e1)->
isIdenticalTo(FirstNonRationalFactor(e2));
281 assert(numberOfNonRationalFactors > 1);
282 return Multiplication::HaveSameNonRationalFactors(e1, e2);
286 Expression * Addition::shallowBeautify(Context & context, AngleUnit angleUnit) {
304 if (static_cast<const Rational *>(m->operand(0))->isMinusOne()) {
305 m->removeOperand(m->operand(0),
true);
307 m->editableOperand(0)->setSign(
Sign::Positive, context, angleUnit);
309 Expression * subtractant = m->squashUnaryHierarchy();
int polynomialDegree(char symbolName) const override
void removeOperand(const Expression *e, bool deleteAfterRemoval=true)
Expression * parent() const
static Complex< T > Cartesian(T a, T b)
Expression * replaceWith(Expression *newOperand, bool deleteAfterReplace=true)
static Rational Addition(const Rational &i, const Rational &j)
Expression * squashUnaryHierarchy()
Type type() const override
Expression * clone() const override
static Complex< T > compute(const Complex< T > c, const Complex< T > d)
Expression * editableOperand(int i)
static int SimplificationOrder(const Expression *e1, const Expression *e2, bool canBeInterrupted=false)
virtual int numberOfOperands() const =0
bool isIdenticalTo(const Expression *e) const
const Expression *const * operands() const override
void mergeOperands(DynamicHierarchy *d)
friend class Multiplication
virtual Sign sign() const
void sortOperands(ExpressionOrder order, bool canBeInterrupted)
int numberOfOperands() const override
const Expression * operand(int i) const
virtual int polynomialDegree(char symbolName) const
virtual Type type() const =0
void replaceOperand(const Expression *oldOperand, Expression *newOperand, bool deleteOldOperand=true)