binomial_coefficient.cpp
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <poincare/binomial_coefficient.h>
#include <poincare/evaluation.h>
#include <poincare/complex.h>
#include "layout/parenthesis_layout.h"
#include "layout/grid_layout.h"
extern "C" {
#include <stdlib.h>
#include <assert.h>
}
#include <cmath>
namespace Poincare {
BinomialCoefficient::BinomialCoefficient() :
Function("binomial", 2)
{
}
Expression::Type BinomialCoefficient::type() const {
return Type::BinomialCoefficient;
}
Expression * BinomialCoefficient::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(newOperands != nullptr);
BinomialCoefficient * bc = new BinomialCoefficient();
bc->setArgument(newOperands, numberOfOperands, cloneOperands);
return bc;
}
template<typename T>
Evaluation<T> * BinomialCoefficient::templatedEvaluate(Context& context, AngleUnit angleUnit) const {
Evaluation<T> * nInput = m_args[0]->evaluate<T>(context, angleUnit);
Evaluation<T> * kInput = m_args[1]->evaluate<T>(context, angleUnit);
T n = nInput->toScalar();
T k = kInput->toScalar();
delete nInput;
delete kInput;
if (std::isnan(n) || std::isnan(k) || n != (int)n || k != (int)k || k > n || k < 0 || n < 0) {
return new Complex<T>(Complex<T>::Float(NAN));
}
T result = 1;
for (int i = 0; i < (int)k; i++) {
result *= (n-(T)i)/(k-(T)i);
}
return new Complex<T>(Complex<T>::Float(std::round(result)));
}
ExpressionLayout * BinomialCoefficient::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
assert(floatDisplayMode != FloatDisplayMode::Default);
assert(complexFormat != ComplexFormat::Default);
ExpressionLayout * childrenLayouts[2];
childrenLayouts[0] = m_args[0]->createLayout(floatDisplayMode, complexFormat);
childrenLayouts[1] = m_args[1]->createLayout(floatDisplayMode, complexFormat);
return new ParenthesisLayout(new GridLayout(childrenLayouts, 2, 1));
}
}