permute_coefficient.cpp
1.32 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
#include <poincare/permute_coefficient.h>
#include <poincare/complex.h>
extern "C" {
#include <assert.h>
}
#include <cmath>
namespace Poincare {
PermuteCoefficient::PermuteCoefficient() :
Function("permute", 2)
{
}
Expression::Type PermuteCoefficient::type() const {
return Type::PermuteCoefficient;
}
Expression * PermuteCoefficient::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(newOperands != nullptr);
PermuteCoefficient * pc = new PermuteCoefficient();
pc->setArgument(newOperands, numberOfOperands, cloneOperands);
return pc;
}
template<typename T>
Evaluation<T> * PermuteCoefficient::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 || n < 0.0f || k < 0.0f) {
return new Complex<T>(Complex<T>::Float(NAN));
}
if (k > n) {
return new Complex<T>(Complex<T>::Float(0));
}
T result = 1;
for (int i = (int)n-(int)k+1; i <= (int)n; i++) {
result *= i;
}
return new Complex<T>(Complex<T>::Float(std::round(result)));
}
}