[poincare] NormPDF

This commit is contained in:
Léa Saviot
2019-08-23 15:26:38 +02:00
parent 04ccc285c8
commit 976c3ce245
10 changed files with 118 additions and 0 deletions

View File

@@ -98,6 +98,7 @@ poincare_src += $(addprefix poincare/src/,\
naperian_logarithm.cpp \
norm_cdf.cpp \
norm_cdf2.cpp \
norm_pdf.cpp \
nth_root.cpp \
number.cpp \
opposite.cpp \

View File

@@ -66,6 +66,7 @@ class Expression : public TreeHandle {
friend class Multiplication;
friend class MultiplicationNode;
friend class NaperianLogarithm;
friend class NormPDF;
friend class NormCDF;
friend class NormCDF2;
friend class NthRoot;

View File

@@ -75,6 +75,7 @@ public:
NaperianLogarithm,
NormCDF,
NormCDF2,
NormPDF,
NthRoot,
Opposite,
Parenthesis,

View File

@@ -0,0 +1,52 @@
#ifndef POINCARE_NORM_PDF_H
#define POINCARE_NORM_PDF_H
#include <poincare/approximation_helper.h>
#include <poincare/expression.h>
namespace Poincare {
class NormPDFNode final : public ExpressionNode {
public:
// TreeNode
size_t size() const override { return sizeof(NormPDFNode); }
int numberOfChildren() const override;
#if POINCARE_TREE_LOG
virtual void logNodeName(std::ostream & stream) const override {
stream << "NormPDF";
}
#endif
// Properties
Type type() const override { return Type::NormPDF; }
Sign sign(Context * context) const override { return Sign::Positive; }
Expression setSign(Sign s, ReductionContext reductionContext) override;
private:
// Layout
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
// Simplication
Expression shallowReduce(ReductionContext reductionContext) override;
LayoutShape leftLayoutShape() const override { return LayoutShape::MoreLetters; };
LayoutShape rightLayoutShape() const override { return LayoutShape::BoundaryPunctuation; }
// Evaluation
Evaluation<float> approximate(SinglePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<float>(context, complexFormat, angleUnit); }
Evaluation<double> approximate(DoublePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<double>(context, complexFormat, angleUnit); }
template<typename T> Evaluation<T> templatedApproximate(Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const;
};
class NormPDF final : public Expression {
public:
NormPDF(const NormPDFNode * n) : Expression(n) {}
static NormPDF Builder(Expression child0, Expression child1, Expression child2) { return TreeHandle::FixedArityBuilder<NormPDF, NormPDFNode>(ArrayBuilder<TreeHandle>(child0, child1, child2).array(), 3); }
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("normpdf", 3, &UntypedBuilderThreeChildren<NormPDF>);
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
};
}
#endif

View File

@@ -54,6 +54,7 @@
#include <poincare/naperian_logarithm.h>
#include <poincare/norm_cdf.h>
#include <poincare/norm_cdf2.h>
#include <poincare/norm_pdf.h>
#include <poincare/nth_root.h>
#include <poincare/number.h>
#include <poincare/opposite.h>

57
poincare/src/norm_pdf.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include <poincare/norm_pdf.h>
#include <poincare/layout_helper.h>
#include <poincare/normal_distribution.h>
#include <poincare/serialization_helper.h>
#include <assert.h>
namespace Poincare {
constexpr Expression::FunctionHelper NormPDF::s_functionHelper;
int NormPDFNode::numberOfChildren() const { return NormPDF::s_functionHelper.numberOfChildren(); }
Expression NormPDFNode::setSign(Sign s, ReductionContext reductionContext) {
assert(s == Sign::Positive);
return NormPDF(this);
}
Layout NormPDFNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return LayoutHelper::Prefix(NormPDF(this), floatDisplayMode, numberOfSignificantDigits, NormPDF::s_functionHelper.name());
}
int NormPDFNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, NormPDF::s_functionHelper.name());
}
Expression NormPDFNode::shallowReduce(ReductionContext reductionContext) {
return NormPDF(this).shallowReduce(reductionContext);
}
template<typename T>
Evaluation<T> NormPDFNode::templatedApproximate(Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const {
Evaluation<T> xEvaluation = childAtIndex(0)->approximate(T(), context, complexFormat, angleUnit);
Evaluation<T> muEvaluation = childAtIndex(1)->approximate(T(), context, complexFormat, angleUnit);
Evaluation<T> sigmaEvaluation = childAtIndex(2)->approximate(T(), context, complexFormat, angleUnit);
T x = xEvaluation.toScalar();
T mu = muEvaluation.toScalar();
T sigma = sigmaEvaluation.toScalar();
if (std::isnan(x) || std::isnan(mu) || std::isnan(sigma)) {
return Complex<T>::Undefined();
}
return Complex<T>::Builder(NormalDistribution::EvaluateAtAbscissa(x, mu, sigma));
}
Expression NormPDF::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
{
Expression e = Expression::defaultShallowReduce();
if (e.isUndefined()) {
return e;
}
}
//TODO LEA
return *this;
}
}

View File

@@ -60,6 +60,7 @@ T NormalDistribution::StandardNormalCumulativeDistributiveInverseForProbability(
}
template float NormalDistribution::EvaluateAtAbscissa<float>(float, float, float);
template double NormalDistribution::EvaluateAtAbscissa<double>(double, double, double);
template float NormalDistribution::CumulativeDistributiveFunctionAtAbscissa<float>(float, float, float);
template double NormalDistribution::CumulativeDistributiveFunctionAtAbscissa<double>(double, double, double);
template float NormalDistribution::CumulativeDistributiveInverseForProbability<float>(float, float, float);

View File

@@ -123,6 +123,7 @@ private:
&Logarithm::s_functionHelper,
&NormCDF::s_functionHelper,
&NormCDF2::s_functionHelper,
&NormPDF::s_functionHelper,
&PermuteCoefficient::s_functionHelper,
&SimplePredictionInterval::s_functionHelper,
&PredictionInterval::s_functionHelper,

View File

@@ -319,6 +319,7 @@ template Multiplication TreeHandle::NAryBuilder<Multiplication, MultiplicationNo
template NaperianLogarithm TreeHandle::FixedArityBuilder<NaperianLogarithm, NaperianLogarithmNode>(TreeHandle*, size_t);
template NormCDF TreeHandle::FixedArityBuilder<NormCDF, NormCDFNode>(TreeHandle*, size_t);
template NormCDF2 TreeHandle::FixedArityBuilder<NormCDF2, NormCDF2Node>(TreeHandle*, size_t);
template NormPDF TreeHandle::FixedArityBuilder<NormPDF, NormPDFNode>(TreeHandle*, size_t);
template NthRoot TreeHandle::FixedArityBuilder<NthRoot, NthRootNode>(TreeHandle*, size_t);
template Opposite TreeHandle::FixedArityBuilder<Opposite, OppositeNode>(TreeHandle*, size_t);
template Parenthesis TreeHandle::FixedArityBuilder<Parenthesis, ParenthesisNode>(TreeHandle*, size_t);

View File

@@ -285,6 +285,8 @@ QUIZ_CASE(poincare_approximation_function) {
assert_expression_approximates_to<float>("normcdf2(0.5, 3.6, 1.3, 3.4)", "0.3436388");
assert_expression_approximates_to<double>("normcdf2(0.5, 3.6, 1.3, 3.4)", "3.4363881299147ᴇ-1");
assert_expression_approximates_to<float>("normpdf(1.2, 3.4, 5.6)", "0.06594901");
assert_expression_approximates_to<float>("permute(10, 4)", "5040");
assert_expression_approximates_to<double>("permute(10, 4)", "5040");