#include #include #include #include #include #include "layout/nth_root_layout.h" extern "C" { #include } #include namespace Poincare { Expression::Type NthRoot::type() const { return Type::NthRoot; } Expression * NthRoot::clone() const { NthRoot * a = new NthRoot(m_operands, true); return a; } Expression * NthRoot::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } #if MATRIX_EXACT_REDUCING if (operand(0)->type() == Type::Matrix || operand(1)->type() == Type::Matrix) { return replaceWith(new Undefined(), true); } #endif Power * invIndex = new Power(operand(1), new Rational(-1), false); Power * p = new Power(operand(0), invIndex, false); detachOperands(); invIndex->shallowReduce(context, angleUnit); replaceWith(p, true); return p->shallowReduce(context, angleUnit); } ExpressionLayout * NthRoot::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const { assert(floatDisplayMode != FloatDisplayMode::Default); assert(complexFormat != ComplexFormat::Default); return new NthRootLayout(operand(0)->createLayout(floatDisplayMode, complexFormat), operand(1)->createLayout(floatDisplayMode, complexFormat)); } template Complex NthRoot::compute(const Complex c, const Complex d) { if (c.a() >= 0 && c.b() == 0 && d.b() == 0) { return Complex::Float(std::pow(c.a(), 1/d.a())); } Complex invIndex = Division::compute(Complex::Float(1), d); return Power::compute(c, invIndex); } template Expression * NthRoot::templatedApproximate(Context& context, AngleUnit angleUnit) const { Expression * base = operand(0)->approximate(context, angleUnit); Expression * index = operand(1)->approximate(context, angleUnit); Complex result = Complex::Float(NAN); if (base->type() == Type::Complex && index->type() == Type::Complex) { Complex * basec = static_cast *>(base); Complex * indexc = static_cast *>(index); result = compute(*basec, *indexc); } delete base; delete index; return new Complex(result); } }