Blame view

build3/poincare/src/nth_root.cpp 2.27 KB
6663b6c9   adorian   projet complet av...
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
61
62
63
64
65
66
67
68
69
70
71
  #include <poincare/nth_root.h>
  #include <poincare/complex.h>
  #include <poincare/division.h>
  #include <poincare/power.h>
  #include <poincare/undefined.h>
  #include "layout/nth_root_layout.h"
  
  extern "C" {
  #include <assert.h>
  }
  #include <cmath>
  
  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<typename T>
  Complex<T> NthRoot::compute(const Complex<T> c, const Complex<T> d) {
    if (c.a() >= 0 && c.b() == 0 && d.b() == 0) {
      return Complex<T>::Float(std::pow(c.a(), 1/d.a()));
    }
    Complex<T> invIndex = Division::compute(Complex<T>::Float(1), d);
    return Power::compute(c, invIndex);
  }
  
  template<typename T>
  Expression * NthRoot::templatedApproximate(Context& context, AngleUnit angleUnit) const {
    Expression * base = operand(0)->approximate<T>(context, angleUnit);
    Expression * index = operand(1)->approximate<T>(context, angleUnit);
    Complex<T> result = Complex<T>::Float(NAN);
    if (base->type() == Type::Complex && index->type() == Type::Complex) {
      Complex<T> * basec = static_cast<Complex<T> *>(base);
      Complex<T> * indexc = static_cast<Complex<T> *>(index);
      result = compute(*basec, *indexc);
    }
    delete base;
    delete index;
    return new Complex<T>(result);
  }
  
  }