#include #include extern "C" { #include } #include 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 Evaluation * PermuteCoefficient::templatedEvaluate(Context& context, AngleUnit angleUnit) const { Evaluation * nInput = m_args[0]->evaluate(context, angleUnit); Evaluation * kInput = m_args[1]->evaluate(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(Complex::Float(NAN)); } if (k > n) { return new Complex(Complex::Float(0)); } T result = 1; for (int i = (int)n-(int)k+1; i <= (int)n; i++) { result *= i; } return new Complex(Complex::Float(std::round(result))); } }