[poincare/derivative] Derivate special numbers

Derivate the numbers Undefined, Unreal, Infinity symbolically :
  Undefined -> Undefined
  Unreal    -> Unreal
  Infinity  -> Undefined
This commit is contained in:
Gabriel Ozouf
2020-12-03 14:41:00 +01:00
committed by EmilieNumworks
parent 76eb155ed5
commit 7ae03975cc
5 changed files with 21 additions and 3 deletions

View File

@@ -37,6 +37,11 @@ public:
// Layout // Layout
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override; Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const override; int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = 0) const override;
/* Derivation
* Unlike Numbers that derivate to 0, Infinity derivates to Undefined. */
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override;
private: private:
// Simplification // Simplification
LayoutShape leftLayoutShape() const override { assert(!m_negative); return LayoutShape::MoreLetters; } LayoutShape leftLayoutShape() const override { assert(!m_negative); return LayoutShape::MoreLetters; }
@@ -56,6 +61,7 @@ public:
static int NameSize() { static int NameSize() {
return 4; return 4;
} }
bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue);
private: private:
InfinityNode * node() const { return static_cast<InfinityNode *>(Number::node()); } InfinityNode * node() const { return static_cast<InfinityNode *>(Number::node()); }
}; };

View File

@@ -30,8 +30,8 @@ public:
} }
/* Derivation /* Derivation
* Overrides NumberNode's derivate to revert to a non-derivable state */ * Unlike Numbers that derivate to 0, Undefined derivates to Undefined. */
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override { return false; } bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override { return true; }
// Layout // Layout
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override; Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;

View File

@@ -28,7 +28,7 @@ public:
} }
/* Derivation /* Derivation
* Overrides NumberNode's derivate to revert to a non-derivable state */ * Unlike Numbers that derivate to 0, Unreal derivates to Unreal. */
bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override { return false; } bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override { return false; }
// Layout // Layout

View File

@@ -31,6 +31,10 @@ template<typename T> Evaluation<T> InfinityNode::templatedApproximate() const {
return Complex<T>::Builder(m_negative ? -INFINITY : INFINITY); return Complex<T>::Builder(m_negative ? -INFINITY : INFINITY);
} }
bool InfinityNode::derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
return Infinity(this).derivate(reductionContext, symbol, symbolValue);
}
Infinity Infinity::Builder(bool negative) { Infinity Infinity::Builder(bool negative) {
void * bufferNode = TreePool::sharedPool()->alloc(sizeof(InfinityNode)); void * bufferNode = TreePool::sharedPool()->alloc(sizeof(InfinityNode));
InfinityNode * node = new (bufferNode) InfinityNode(negative); InfinityNode * node = new (bufferNode) InfinityNode(negative);
@@ -45,6 +49,11 @@ Expression Infinity::setSign(ExpressionNode::Sign s) {
return result; return result;
} }
bool Infinity::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) {
replaceWithUndefinedInPlace();
return true;
}
template Evaluation<float> InfinityNode::templatedApproximate<float>() const; template Evaluation<float> InfinityNode::templatedApproximate<float>() const;
template Evaluation<double> InfinityNode::templatedApproximate() const; template Evaluation<double> InfinityNode::templatedApproximate() const;
} }

View File

@@ -8,6 +8,9 @@ void assert_reduces_to_formal_expression(const char * expression, const char * r
} }
QUIZ_CASE(poincare_derivative_formal) { QUIZ_CASE(poincare_derivative_formal) {
assert_reduces_to_formal_expression("diff(undef,x,x)", Undefined::Name());
assert_reduces_to_formal_expression("diff(unreal,x,x)", Unreal::Name());
assert_reduces_to_formal_expression("diff(inf,x,x)", Undefined::Name());
assert_reduces_to_formal_expression("diff(1,x,x)", "0"); assert_reduces_to_formal_expression("diff(1,x,x)", "0");
assert_reduces_to_formal_expression("diff(π,x,x)", "0"); assert_reduces_to_formal_expression("diff(π,x,x)", "0");
assert_reduces_to_formal_expression("diff(y,x,x)", "0"); assert_reduces_to_formal_expression("diff(y,x,x)", "0");