From 7ae03975ccdc2492d8727f7ef33ad97ca4360a5d Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Thu, 3 Dec 2020 14:41:00 +0100 Subject: [PATCH] [poincare/derivative] Derivate special numbers Derivate the numbers Undefined, Unreal, Infinity symbolically : Undefined -> Undefined Unreal -> Unreal Infinity -> Undefined --- poincare/include/poincare/infinity.h | 6 ++++++ poincare/include/poincare/undefined.h | 4 ++-- poincare/include/poincare/unreal.h | 2 +- poincare/src/infinity.cpp | 9 +++++++++ poincare/test/derivative.cpp | 3 +++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/poincare/include/poincare/infinity.h b/poincare/include/poincare/infinity.h index b4d6aa02c..03740eead 100644 --- a/poincare/include/poincare/infinity.h +++ b/poincare/include/poincare/infinity.h @@ -37,6 +37,11 @@ public: // Layout 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; + + /* Derivation + * Unlike Numbers that derivate to 0, Infinity derivates to Undefined. */ + bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override; + private: // Simplification LayoutShape leftLayoutShape() const override { assert(!m_negative); return LayoutShape::MoreLetters; } @@ -56,6 +61,7 @@ public: static int NameSize() { return 4; } + bool derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue); private: InfinityNode * node() const { return static_cast(Number::node()); } }; diff --git a/poincare/include/poincare/undefined.h b/poincare/include/poincare/undefined.h index 9a09fe95f..7dfb094f3 100644 --- a/poincare/include/poincare/undefined.h +++ b/poincare/include/poincare/undefined.h @@ -30,8 +30,8 @@ public: } /* Derivation - * Overrides NumberNode's derivate to revert to a non-derivable state */ - bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override { return false; } + * Unlike Numbers that derivate to 0, Undefined derivates to Undefined. */ + bool derivate(ReductionContext reductionContext, Expression symbol, Expression symbolValue) override { return true; } // Layout Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override; diff --git a/poincare/include/poincare/unreal.h b/poincare/include/poincare/unreal.h index 7e4290e3e..d74d253b1 100644 --- a/poincare/include/poincare/unreal.h +++ b/poincare/include/poincare/unreal.h @@ -28,7 +28,7 @@ public: } /* 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; } // Layout diff --git a/poincare/src/infinity.cpp b/poincare/src/infinity.cpp index 8a537a68e..312e74334 100644 --- a/poincare/src/infinity.cpp +++ b/poincare/src/infinity.cpp @@ -31,6 +31,10 @@ template Evaluation InfinityNode::templatedApproximate() const { return Complex::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) { void * bufferNode = TreePool::sharedPool()->alloc(sizeof(InfinityNode)); InfinityNode * node = new (bufferNode) InfinityNode(negative); @@ -45,6 +49,11 @@ Expression Infinity::setSign(ExpressionNode::Sign s) { return result; } +bool Infinity::derivate(ExpressionNode::ReductionContext reductionContext, Expression symbol, Expression symbolValue) { + replaceWithUndefinedInPlace(); + return true; +} + template Evaluation InfinityNode::templatedApproximate() const; template Evaluation InfinityNode::templatedApproximate() const; } diff --git a/poincare/test/derivative.cpp b/poincare/test/derivative.cpp index 89e9a1aee..5cde15597 100644 --- a/poincare/test/derivative.cpp +++ b/poincare/test/derivative.cpp @@ -8,6 +8,9 @@ void assert_reduces_to_formal_expression(const char * expression, const char * r } 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(π,x,x)", "0"); assert_reduces_to_formal_expression("diff(y,x,x)", "0");