[poincare] Conjugate

This commit is contained in:
Léa Saviot
2018-08-27 12:11:06 +02:00
parent d6d6e578df
commit 768539d1e8
6 changed files with 70 additions and 38 deletions

View File

@@ -46,6 +46,7 @@ objs += $(addprefix poincare/src/,\
complex.o\
complex_argument.o\
confidence_interval.o\
conjugate.o\
cosine.o\
decimal.o\
derivative.o\

View File

@@ -114,6 +114,7 @@
#include <poincare/binomial_coefficient.h>
#include <poincare/complex_argument.h>
#include <poincare/confidence_interval.h>
#include <poincare/conjugate.h>
#include <poincare/cosine.h>
#include <poincare/ceiling.h>
#include <poincare/sine.h>

View File

@@ -1,26 +1,36 @@
#ifndef POINCARE_CONJUGATE_H
#define POINCARE_CONJUGATE_H
#include <poincare/static_hierarchy.h>
#include <poincare/approximation_helper.h>
#include <poincare/layout_helper.h>
#include <poincare/expression.h>
namespace Poincare {
class Conjugate : public StaticHierarchy<1> {
using StaticHierarchy<1>::StaticHierarchy;
class ConjugateNode : public ExpressionNode {
public:
Type type() const override;
private:
/* Layout */
LayoutRef createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "conj");
// Allocation Failure
static ConjugateNode * FailedAllocationStaticNode();
ConjugateNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
// TreeNode
size_t size() const override { return sizeof(ConjugateNode); }
int numberOfChildren() const override { return 1; }
#if POINCARE_TREE_LOG
virtual void logNodeName(std::ostream & stream) const override {
stream << "Conjugate";
}
/* Simplification */
#endif
// Properties
Type type() const override { return Type::Conjugate; }
private:
// Layout
LayoutReference createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
// Simplification
Expression shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const override;
/* Evaluation */
template<typename T> static std::complex<T> computeOnComplex(const std::complex<T> c, Preferences::AngleUnit angleUnit);
// Evaluation
template<typename T> static Complex<T> computeOnComplex(const std::complex<T> c, Preferences::AngleUnit angleUnit);
Evaluation<float> approximate(SinglePrecision p, Context& context, Preferences::AngleUnit angleUnit) const override {
return ApproximationHelper::Map<float>(this, context, angleUnit,computeOnComplex<float>);
}
@@ -29,6 +39,18 @@ private:
}
};
class Conjugate : public Expression {
public:
Conjugate() : Expression(TreePool::sharedPool()->createTreeNode<ConjugateNode>()) {}
Conjugate(const ConjugateNode * n) : Expression(n) {}
Conjugate(Expression operand) : Conjugate() {
replaceChildAtIndexInPlace(0, operand);
}
Expression shallowReduce(Context & context, Preferences::AngleUnit angleUnit) const;
};
}
#endif

View File

@@ -1,44 +1,50 @@
#include <poincare/conjugate.h>
#include <poincare/simplification_helper.h>
#include <poincare/conjugate_layout_node.h>
#include <poincare/serialization_helper.h>
#include <poincare/simplification_helper.h>
#include <assert.h>
#include <cmath>
namespace Poincare {
ExpressionNode::Type Conjugate::type() const {
return Type::Conjugate;
ConjugateNode * ConjugateNode::FailedAllocationStaticNode() {
static AllocationFailureExpressionNode<ConjugateNode> failure;
TreePool::sharedPool()->registerStaticNodeIfRequired(&failure);
return &failure;
}
Expression * Conjugate::clone() const {
Conjugate * a = new Conjugate(m_operands, true);
return a;
}
LayoutRef Conjugate::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
LayoutRef ConjugateNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return ConjugateLayoutRef(childAtIndex(0)->createLayout(floatDisplayMode, numberOfSignificantDigits));
}
int ConjugateNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "conj");
}
Expression ConjugateNode::shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const {
return Conjugate(this).shallowReduce(context, angleUnit);
}
template<typename T>
Complex<T> ConjugateNode::computeOnComplex(const std::complex<T> c, Preferences::AngleUnit angleUnit) {
return Complex<T>(std::conj(c));
}
Expression Conjugate::shallowReduce(Context& context, Preferences::AngleUnit angleUnit) const {
Expression e = Expression::defaultShallowReduce(context, angleUnit);
if (e.isUndefinedOrAllocationFailure()) {
return e;
}
Expression * op = childAtIndex(0);
Expression c = childAtIndex(0);
#if MATRIX_EXACT_REDUCING
if (op->type() == Type::Matrix) {
return SimplificationHelper::Map(this, context, angleUnit);
if (c.type() == ExpressionNode::Type::Matrix) {
return SimplificationHelper::Map(*this, context, angleUnit);
}
#endif
if (op->type() == Type::Rational) {
return replaceWith(op, true);
if (c.type() == ExpressionNode::Type::Rational) {
return c;
}
return this;
}
template<typename T>
std::complex<T> Conjugate::computeOnComplex(const std::complex<T> c, Preferences::AngleUnit angleUnit) {
return std::conj(c);
return *this;
}
}

View File

@@ -126,8 +126,8 @@ confidence { poincare_expression_yylval.expression = ConfidenceInterval(); retur
/*diff { poincare_expression_yylval.expression = new Derivative(); return FUNCTION; }
dim { poincare_expression_yylval.expression = new MatrixDimension(); return FUNCTION; }
det { poincare_expression_yylval.expression = new Determinant(); return FUNCTION; }
conj { poincare_expression_yylval.expression = new Conjugate(); return FUNCTION; }
*/
conj { poincare_expression_yylval.expression = Conjugate(); return FUNCTION; }
cos { poincare_expression_yylval.expression = Cosine(); return FUNCTION; }
cosh { poincare_expression_yylval.expression = HyperbolicCosine(); return FUNCTION; }
/*factor { poincare_expression_yylval.expression = new Factor(); return FUNCTION; }

View File

@@ -28,8 +28,8 @@ QUIZ_CASE(poincare_parse_function) {
#endif
#endif
assert_parsed_expression_type("confidence(0.1, 100)", ExpressionNode::Type::ConfidenceInterval);
#if 0
assert_parsed_expression_type("conj(2)", ExpressionNode::Type::Conjugate);
#if 0
assert_parsed_expression_type("factor(23/42)", ExpressionNode::Type::Factor);
assert_parsed_expression_type("floor(2.3)", ExpressionNode::Type::Floor);
assert_parsed_expression_type("frac(2.3)", ExpressionNode::Type::FracPart);
@@ -152,15 +152,17 @@ QUIZ_CASE(poincare_function_evaluate) {
assert_parsed_expression_evaluates_to<float>("confidence(0.1, 100)", "[[0,0.2]]");
assert_parsed_expression_evaluates_to<double>("confidence(0.1, 100)", "[[0,0.2]]");
#if 0
#if 0
#if MATRICES_ARE_DEFINED
assert_parsed_expression_evaluates_to<float>("dim([[1,2,3][4,5,-6]])", "[[2,3]]");
assert_parsed_expression_evaluates_to<double>("dim([[1,2,3][4,5,-6]])", "[[2,3]]");
#endif
#endif
assert_parsed_expression_evaluates_to<float>("conj(3+2*I)", "3-2*I");
assert_parsed_expression_evaluates_to<double>("conj(3+2*I)", "3-2*I");
#if 0
#if MATRICES_ARE_DEFINED
assert_parsed_expression_evaluates_to<float>("inverse([[1,2,3][4,5,-6][7,8,9]])", "[[-1.2917,-0.083333,0.375][1.0833,0.16667,-0.25][0.041667,-0.083333,0.041667]]", Degree, Cartesian, 5); // inverse is not precise enough to display 7 significative digits
@@ -242,9 +244,9 @@ QUIZ_CASE(poincare_function_simplify) {
assert_parsed_expression_simplify_to("binomial(20,3)", "1140");
assert_parsed_expression_simplify_to("binomial(20,10)", "184756");
assert_parsed_expression_simplify_to("ceil(-1.3)", "-1");
#if 0
assert_parsed_expression_simplify_to("conj(1/2)", "1/2");
assert_parsed_expression_simplify_to("quo(19,3)", "6");
#if 0
assert_parsed_expression_simplify_to("quo(19,3)", "6");
assert_parsed_expression_simplify_to("quo(19,0)", "undef");
assert_parsed_expression_simplify_to("quo(-19,3)", "-7");
assert_parsed_expression_simplify_to("rem(19,3)", "1");