Files
Upsilon/poincare/src/nth_root.cpp
2018-09-03 11:39:41 +02:00

62 lines
2.2 KiB
C++

#include <poincare/nth_root.h>
#include <poincare/division.h>
#include <poincare/power.h>
#include <poincare/undefined.h>
#include <poincare/nth_root_layout_node.h>
#include <assert.h>
#include <cmath>
namespace Poincare {
NthRootNode * NthRootNode::FailedAllocationStaticNode() {
static AllocationFailureExpressionNode<NthRootNode> failure;
TreePool::sharedPool()->registerStaticNodeIfRequired(&failure);
return &failure;
}
LayoutReference NthRootNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return NthRootLayoutRef(
childAtIndex(0)->createLayout(floatDisplayMode, numberOfSignificantDigits),
childAtIndex(1)->createLayout(floatDisplayMode, numberOfSignificantDigits));
}
Expression NthRootNode::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) {
return NthRoot(this).shallowReduce(context, angleUnit);
}
template<typename T>
Evaluation<T> NthRootNode::templatedApproximate(Context& context, Preferences::AngleUnit angleUnit) const {
Evaluation<T> base = childAtIndex(0)->approximate(T(), context, angleUnit);
Evaluation<T> index = childAtIndex(1)->approximate(T(), context, angleUnit);
Complex<T> result = Complex<T>::Undefined();
if (base.type() == EvaluationNode<T>::Type::Complex
&& index.type() == EvaluationNode<T>::Type::Complex)
{
Complex<T> basec = static_cast<Complex<T> &>(base);
Complex<T> indexc = static_cast<Complex<T> &>(index);
result = PowerNode::compute(basec.stdComplex(), std::complex<T>(1)/(indexc.stdComplex()));
}
return result;
}
Expression NthRoot::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) {
{
Expression e = Expression::defaultShallowReduce(context, angleUnit);
if (e.isUndefinedOrAllocationFailure()) {
return e;
}
}
#if MATRIX_EXACT_REDUCING
if (childAtIndex(0).type() == ExpressionNode::Type::Matrix || childAtIndex(1).type() == ExpressionNode:Type::Matrix) {
return Undefined();
}
#endif
Expression invIndex = Power(childAtIndex(1), Rational(-1));
Power p = Power(childAtIndex(0), invIndex);
invIndex.shallowReduce(context, angleUnit);
replaceWithInPlace(p);
return p.shallowReduce(context, angleUnit);
}
}