mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-22 15:20:39 +01:00
[poicare] Parse binomial() function
Change-Id: Ib75b980794dc05ee85002c67dd5a54746a2cfb2c
This commit is contained in:
@@ -6,6 +6,7 @@ objs += $(addprefix poincare/src/,\
|
||||
absolute_value.o\
|
||||
addition.o\
|
||||
binary_operation.o\
|
||||
binomial_coefficient.o\
|
||||
complex.o\
|
||||
complex_argument.o\
|
||||
conjugate.o\
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <poincare/absolute_value.h>
|
||||
#include <poincare/addition.h>
|
||||
#include <poincare/binomial_coefficient.h>
|
||||
#include <poincare/complex.h>
|
||||
#include <poincare/complex_argument.h>
|
||||
#include <poincare/conjugate.h>
|
||||
|
||||
22
poincare/include/poincare/binomial_coefficient.h
Normal file
22
poincare/include/poincare/binomial_coefficient.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef POINCARE_BINOMIAL_COEFFICIENT_H
|
||||
#define POINCARE_BINOMIAL_COEFFICIENT_H
|
||||
|
||||
#include <poincare/function.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
class BinomialCoefficient : public Function {
|
||||
public:
|
||||
BinomialCoefficient();
|
||||
Type type() const override;
|
||||
Expression * cloneWithDifferentOperands(Expression ** newOperands,
|
||||
int numberOfOperands, bool cloneOperands = true) const override;
|
||||
private:
|
||||
float privateApproximate(Context & context, AngleUnit angleUnit) const override;
|
||||
ExpressionLayout * privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,6 +13,7 @@ public:
|
||||
enum class Type : uint8_t {
|
||||
AbsoluteValue,
|
||||
Addition,
|
||||
BinomialCoefficient,
|
||||
Complex,
|
||||
ComplexArgument,
|
||||
Conjugate,
|
||||
|
||||
56
poincare/src/binomial_coefficient.cpp
Normal file
56
poincare/src/binomial_coefficient.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <poincare/binomial_coefficient.h>
|
||||
#include <poincare/complex.h>
|
||||
#include "layout/parenthesis_layout.h"
|
||||
#include "layout/grid_layout.h"
|
||||
|
||||
extern "C" {
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
}
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
BinomialCoefficient::BinomialCoefficient() :
|
||||
Function("binomial")
|
||||
{
|
||||
}
|
||||
|
||||
Expression::Type BinomialCoefficient::type() const {
|
||||
return Type::BinomialCoefficient;
|
||||
}
|
||||
|
||||
Expression * BinomialCoefficient::cloneWithDifferentOperands(Expression** newOperands,
|
||||
int numberOfOperands, bool cloneOperands) const {
|
||||
assert(numberOfOperands == 1);
|
||||
assert(newOperands != nullptr);
|
||||
BinomialCoefficient * bc = new BinomialCoefficient();
|
||||
bc->setArgument(newOperands, numberOfOperands, cloneOperands);
|
||||
return bc;
|
||||
}
|
||||
|
||||
float BinomialCoefficient::privateApproximate(Context& context, AngleUnit angleUnit) const {
|
||||
assert(angleUnit != AngleUnit::Default);
|
||||
float n = m_args[0]->approximate(context, angleUnit);
|
||||
float k = m_args[1]->approximate(context, angleUnit);
|
||||
if (isnan(n) || isnan(k) || n != (int)n || k != (int)k) {
|
||||
return NAN;
|
||||
}
|
||||
float result = 1.0f;
|
||||
for (int i = 0; i < (int)k; i++) {
|
||||
result *= (n-(float)i)/(k-(float)i);
|
||||
}
|
||||
return roundf(result);
|
||||
}
|
||||
|
||||
ExpressionLayout * BinomialCoefficient::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
|
||||
assert(floatDisplayMode != FloatDisplayMode::Default);
|
||||
assert(complexFormat != ComplexFormat::Default);
|
||||
ExpressionLayout ** childrenLayouts = (ExpressionLayout **)malloc(2*sizeof(ExpressionLayout *));
|
||||
childrenLayouts[0] = m_args[0]->createLayout(floatDisplayMode, complexFormat);
|
||||
childrenLayouts[1] = m_args[1]->createLayout(floatDisplayMode, complexFormat);
|
||||
return new ParenthesisLayout(new GridLayout(childrenLayouts, 2, 1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ w\(n\+1\) { poincare_expression_yylval.character = Symbol::SpecialSymbols::wn1;
|
||||
abs { poincare_expression_yylval.expression = new AbsoluteValue(); return FUNCTION; }
|
||||
ans { poincare_expression_yylval.character = Symbol::SpecialSymbols::Ans; return SYMBOL; }
|
||||
arg { poincare_expression_yylval.expression = new ComplexArgument(); return FUNCTION; }
|
||||
binomial { poincare_expression_yylval.expression = new BinomialCoefficient(); return FUNCTION; }
|
||||
diff { poincare_expression_yylval.expression = new Derivative(); return FUNCTION; }
|
||||
det { poincare_expression_yylval.expression = new Determinant(); return FUNCTION; }
|
||||
conj { poincare_expression_yylval.expression = new Conjugate(); return FUNCTION; }
|
||||
|
||||
Reference in New Issue
Block a user