[poincare] Add ReductionContext to unaryFunctionDifferential

Change-Id: I99ba90f8c38cab5d8fe2139f49e4f94c1d52d296
This commit is contained in:
Gabriel Ozouf
2020-11-26 17:07:51 +01:00
committed by EmilieNumworks
parent f54c5a8172
commit f329ca825f
19 changed files with 49 additions and 49 deletions

View File

@@ -35,7 +35,7 @@ private:
// Derivation
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
Expression unaryFunctionDifferential(ReductionContext reductionContext) override;
// Evaluation
Evaluation<float> approximate(SinglePrecision p, ApproximationContext approximationContext) const override {
@@ -56,7 +56,7 @@ public:
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext);
};
}

View File

@@ -52,7 +52,7 @@ public:
static Derivative Builder(Expression child0, Symbol child1, Expression child2) { return TreeHandle::FixedArityBuilder<Derivative, DerivativeNode>({child0, child1, child2}); }
static Expression UntypedBuilder(Expression children);
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("diff", 3, &UntypedBuilder);
static void DerivateUnaryFunction(Expression function, Expression symbol, Expression symbolValue);
static void DerivateUnaryFunction(Expression function, Expression symbol, Expression symbolValue, ExpressionNode::ReductionContext reductionContext);
Expression shallowReduce(ExpressionNode::ReductionContext context);
};

View File

@@ -400,7 +400,7 @@ protected:
* It returns whether the instance is differentiable, and differentiates it if
* able. */
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) { return node()->derivate(reductionContext, symbol, symbolValue); }
Expression unaryFunctionDifferential() { return node()->unaryFunctionDifferential(); }
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) { return node()->unaryFunctionDifferential(reductionContext); }
private:
static constexpr int k_maxSymbolReplacementsCount = 10;

View File

@@ -294,7 +294,7 @@ public:
* the reduction context. */
/*!*/ virtual Expression shallowBeautify(ReductionContext * reductionContext);
/*!*/ virtual bool derivate(ReductionContext, Expression symbol, Expression symbolValue);
virtual Expression unaryFunctionDifferential();
virtual Expression unaryFunctionDifferential(ReductionContext reductionContext);
/* Return a clone of the denominator part of the expression */
/*!*/ virtual Expression denominator(ExpressionNode::ReductionContext reductionContext) const;
/* LayoutShape is used to check if the multiplication sign can be omitted between two expressions. It depends on the "layout syle" of the on the right of the left expression */

View File

@@ -27,7 +27,7 @@ private:
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
// Derivation
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
Expression unaryFunctionDifferential(ReductionContext reductionContext) override;
//Evaluation
template<typename T> static Complex<T> computeOnComplex(const std::complex<T> c, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit);
Evaluation<float> approximate(SinglePrecision p, ApproximationContext approximationContext) const override {
@@ -44,7 +44,7 @@ public:
static HyperbolicCosine Builder(Expression child) { return TreeHandle::FixedArityBuilder<HyperbolicCosine, HyperbolicCosineNode>({child}); }
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext);
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("cosh", 1, &UntypedBuilderOneChild<HyperbolicCosine>);
};

View File

@@ -25,7 +25,7 @@ private:
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
// Derivation
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
Expression unaryFunctionDifferential(ReductionContext reductionContext) override;
//Evaluation
template<typename T> static Complex<T> computeOnComplex(const std::complex<T> c, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit);
Evaluation<float> approximate(SinglePrecision p, ApproximationContext approximationContext) const override {
@@ -42,7 +42,7 @@ public:
static HyperbolicSine Builder(Expression child) { return TreeHandle::FixedArityBuilder<HyperbolicSine, HyperbolicSineNode>({child}); }
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext);
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("sinh", 1, &UntypedBuilderOneChild<HyperbolicSine>);
};

View File

@@ -25,7 +25,7 @@ private:
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
// Derivation
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
Expression unaryFunctionDifferential(ReductionContext reductionContext) override;
//Evaluation
template<typename T> static Complex<T> computeOnComplex(const std::complex<T> c, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit);
Evaluation<float> approximate(SinglePrecision p, ApproximationContext approximationContext) const override {
@@ -42,7 +42,7 @@ public:
static HyperbolicTangent Builder(Expression child) { return TreeHandle::FixedArityBuilder<HyperbolicTangent, HyperbolicTangentNode>({child}); }
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext);
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("tanh", 1, &UntypedBuilderOneChild<HyperbolicTangent>);
};

View File

@@ -34,7 +34,7 @@ public:
LayoutShape rightLayoutShape() const override { return LayoutShape::BoundaryPunctuation; }
// Derivation
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
Expression unaryFunctionDifferential(ReductionContext reductionContext) override;
// Evaluation
template<typename U> static Complex<U> computeOnComplex(const std::complex<U> c, Preferences::ComplexFormat, Preferences::AngleUnit angleUnit) {
/* log has a branch cut on ]-inf, 0]: it is then multivalued on this cut. We
@@ -57,7 +57,7 @@ public:
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
Expression shallowBeautify();
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext);
private:
void deepReduceChildren(ExpressionNode::ReductionContext reductionContext);

View File

@@ -36,7 +36,7 @@ private:
// Derivation
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
Expression unaryFunctionDifferential(ReductionContext reductionContext) override;
// Evaluation
Evaluation<float> approximate(SinglePrecision p, ApproximationContext approximationContext) const override {
@@ -57,7 +57,7 @@ public:
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext);
};
}

View File

@@ -33,7 +33,7 @@ private:
// Derivation
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
Expression unaryFunctionDifferential() override;
Expression unaryFunctionDifferential(ReductionContext reductionContext) override;
// Evaluation
template<typename T> static Complex<T> computeOnComplex(const std::complex<T> c, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit = Preferences::AngleUnit::Radian);
@@ -55,7 +55,7 @@ public:
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
Expression unaryFunctionDifferential();
Expression unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext);
};
}

View File

@@ -38,8 +38,8 @@ bool CosineNode::derivate(ReductionContext reductionContext, Expression symbol,
return Cosine(this).derivate(reductionContext, symbol, symbolValue);
}
Expression CosineNode::unaryFunctionDifferential() {
return Cosine(this).unaryFunctionDifferential();
Expression CosineNode::unaryFunctionDifferential(ReductionContext reductionContext) {
return Cosine(this).unaryFunctionDifferential(reductionContext);
}
Expression Cosine::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
@@ -55,11 +55,11 @@ Expression Cosine::shallowReduce(ExpressionNode::ReductionContext reductionConte
bool Cosine::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue, reductionContext);
return true;
}
Expression Cosine::unaryFunctionDifferential() {
Expression Cosine::unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) {
return Multiplication::Builder(Rational::Builder(-1), Sine::Builder(childAtIndex(0).clone()));
}

View File

@@ -187,8 +187,8 @@ Expression Derivative::shallowReduce(ExpressionNode::ReductionContext reductionC
return derivand;
}
void Derivative::DerivateUnaryFunction(Expression function, Expression symbol, Expression symbolValue) {
Expression df = function.unaryFunctionDifferential();
void Derivative::DerivateUnaryFunction(Expression function, Expression symbol, Expression symbolValue, ExpressionNode::ReductionContext reductionContext) {
Expression df = function.unaryFunctionDifferential(reductionContext);
Expression dg = Derivative::Builder(function.childAtIndex(0), symbol.clone().convert<Symbol>(), symbolValue.clone());
function.replaceWithInPlace(Multiplication::Builder(df, dg));

View File

@@ -119,7 +119,7 @@ bool ExpressionNode::derivate(ReductionContext reductionContext, Expression symb
return Expression(this).defaultDidDerivate();
}
Expression ExpressionNode::unaryFunctionDifferential() {
Expression ExpressionNode::unaryFunctionDifferential(ReductionContext reductionContext) {
return Expression(this).defaultUnaryFunctionDifferential();
}

View File

@@ -25,17 +25,17 @@ bool HyperbolicCosineNode::derivate(ReductionContext reductionContext, Expressio
return HyperbolicCosine(this).derivate(reductionContext, symbol, symbolValue);
}
Expression HyperbolicCosineNode::unaryFunctionDifferential() {
return HyperbolicCosine(this).unaryFunctionDifferential();
Expression HyperbolicCosineNode::unaryFunctionDifferential(ReductionContext reductionContext) {
return HyperbolicCosine(this).unaryFunctionDifferential(reductionContext);
}
bool HyperbolicCosine::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue, reductionContext);
return true;
}
Expression HyperbolicCosine::unaryFunctionDifferential() {
Expression HyperbolicCosine::unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) {
return HyperbolicSine::Builder(childAtIndex(0).clone());
}

View File

@@ -25,16 +25,16 @@ bool HyperbolicSineNode::derivate(ReductionContext reductionContext, Expression
return HyperbolicSine(this).derivate(reductionContext, symbol, symbolValue);
}
Expression HyperbolicSineNode::unaryFunctionDifferential() {
return HyperbolicSine(this).unaryFunctionDifferential();
Expression HyperbolicSineNode::unaryFunctionDifferential(ReductionContext reductionContext) {
return HyperbolicSine(this).unaryFunctionDifferential(reductionContext);
}
bool HyperbolicSine::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue, reductionContext);
return true;
}
Expression HyperbolicSine::unaryFunctionDifferential() {
Expression HyperbolicSine::unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) {
return HyperbolicCosine::Builder(childAtIndex(0).clone());
}

View File

@@ -26,16 +26,16 @@ bool HyperbolicTangentNode::derivate(ReductionContext reductionContext, Expressi
return HyperbolicTangent(this).derivate(reductionContext, symbol, symbolValue);
}
Expression HyperbolicTangentNode::unaryFunctionDifferential() {
return HyperbolicTangent(this).unaryFunctionDifferential();
Expression HyperbolicTangentNode::unaryFunctionDifferential(ReductionContext reductionContext) {
return HyperbolicTangent(this).unaryFunctionDifferential(reductionContext);
}
bool HyperbolicTangent::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue, reductionContext);
return true;
}
Expression HyperbolicTangent::unaryFunctionDifferential() {
Expression HyperbolicTangent::unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) {
return Power::Builder(HyperbolicCosine::Builder(childAtIndex(0).clone()), Rational::Builder(-2));
}

View File

@@ -76,8 +76,8 @@ bool LogarithmNode<2>::derivate(ReductionContext reductionContext, Expression sy
}
template <>
Expression LogarithmNode<2>::unaryFunctionDifferential() {
return Logarithm(this).unaryFunctionDifferential();
Expression LogarithmNode<2>::unaryFunctionDifferential(ReductionContext reductionContext) {
return Logarithm(this).unaryFunctionDifferential(reductionContext);
}
/* Those two methods will not be called, as CommonLogarithm disappears in
@@ -89,7 +89,7 @@ bool LogarithmNode<1>::derivate(ReductionContext reductionContext, Expression sy
}
template <>
Expression LogarithmNode<1>::unaryFunctionDifferential() {
Expression LogarithmNode<1>::unaryFunctionDifferential(ReductionContext reductionContext) {
assert(false);
return Expression();
}
@@ -339,11 +339,11 @@ bool Logarithm::derivate(ExpressionNode::ReductionContext reductionContext, Expr
if (childAtIndex(1).polynomialDegree(reductionContext.context(), symbol.convert<Symbol>().name()) != 0) {
return false;
}
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue, reductionContext);
return true;
}
Expression Logarithm::unaryFunctionDifferential() {
Expression Logarithm::unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) {
/* log(x, b)` = (ln(x)/ln(b))`
* = 1 / (x * ln(b))
*

View File

@@ -36,8 +36,8 @@ bool SineNode::derivate(ReductionContext reductionContext, Expression symbol, Ex
return Sine(this).derivate(reductionContext, symbol, symbolValue);
}
Expression SineNode::unaryFunctionDifferential() {
return Sine(this).unaryFunctionDifferential();
Expression SineNode::unaryFunctionDifferential(ReductionContext reductionContext) {
return Sine(this).unaryFunctionDifferential(reductionContext);
}
Expression Sine::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
@@ -52,11 +52,11 @@ Expression Sine::shallowReduce(ExpressionNode::ReductionContext reductionContext
}
bool Sine::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue, reductionContext);
return true;
}
Expression Sine::unaryFunctionDifferential() {
Expression Sine::unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) {
return Cosine::Builder(childAtIndex(0).clone());
}

View File

@@ -40,8 +40,8 @@ bool TangentNode::derivate(ReductionContext reductionContext, Expression symbol,
return Tangent(this).derivate(reductionContext, symbol, symbolValue);
}
Expression TangentNode::unaryFunctionDifferential() {
return Tangent(this).unaryFunctionDifferential();
Expression TangentNode::unaryFunctionDifferential(ReductionContext reductionContext) {
return Tangent(this).unaryFunctionDifferential(reductionContext);
}
Expression Tangent::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
@@ -67,11 +67,11 @@ Expression Tangent::shallowReduce(ExpressionNode::ReductionContext reductionCont
}
bool Tangent::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue);
Derivative::DerivateUnaryFunction(*this, symbol, symbolValue, reductionContext);
return true;
}
Expression Tangent::unaryFunctionDifferential() {
Expression Tangent::unaryFunctionDifferential(ExpressionNode::ReductionContext reductionContext) {
return Power::Builder(Cosine::Builder(childAtIndex(0).clone()), Rational::Builder(-2));
}