Blame view

build3/poincare/src/factorial.cpp 3.37 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  #include <poincare/factorial.h>
  #include "layout/string_layout.h"
  #include "layout/horizontal_layout.h"
  #include <poincare/rational.h>
  #include <poincare/undefined.h>
  #include <poincare/symbol.h>
  #include <poincare/simplification_engine.h>
  #include <ion.h>
  extern "C" {
  #include <assert.h>
  }
  #include <cmath>
  
  namespace Poincare {
  
  Factorial::Factorial(const Expression * argument, bool clone) :
    StaticHierarchy<1>(&argument, clone)
  {
  }
  
  Expression::Type Factorial::type() const {
    return Type::Factorial;
  }
  
  Expression * Factorial::clone() const {
    Factorial * a = new Factorial(m_operands[0], true);
    return a;
  }
  
  /* Layout */
  
  bool Factorial::needParenthesisWithParent(const Expression * e) const {
    return e->type() == Type::Factorial;
  }
  
  /* Simplification */
  
  Expression * Factorial::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) {
      return SimplificationEngine::map(this, context, angleUnit);
    }
  #endif
    if (operand(0)->type() == Type::Rational) {
      Rational * r = static_cast<Rational *>(editableOperand(0));
      if (!r->denominator().isOne() || r->sign() == Sign::Negative) {
        return replaceWith(new Undefined(), true);
      }
      if (Integer(k_maxOperandValue).isLowerThan(r->numerator())) {
        return this;
      }
      Rational * fact = new Rational(Integer::Factorial(r->numerator()));
      return replaceWith(fact, true);
    }
    if (operand(0)->type() == Type::Symbol) {
      Symbol * s = static_cast<Symbol *>(editableOperand(0));
      if (s->name() == Ion::Charset::SmallPi || s->name() == Ion::Charset::Exponential) {
        return replaceWith(new Undefined(), true);
      }
    }
    return this;
  }
  
  template<typename T>
  Complex<T> Factorial::computeOnComplex(const Complex<T> c, AngleUnit angleUnit) {
    T n = c.a();
    if (c.b() != 0 || std::isnan(n) || n != (int)n || n < 0) {
      return Complex<T>::Float(NAN);
    }
    T result = 1;
    for (int i = 1; i <= (int)n; i++) {
      result *= (T)i;
      if (std::isinf(result)) {
        return Complex<T>::Float(result);
      }
    }
    return Complex<T>::Float(std::round(result));
  }
  
  ExpressionLayout * Factorial::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
    assert(floatDisplayMode != FloatDisplayMode::Default);
    assert(complexFormat != ComplexFormat::Default);
    ExpressionLayout * childrenLayouts[2];
    childrenLayouts[0] = operand(0)->createLayout(floatDisplayMode, complexFormat);
    childrenLayouts[1] = new StringLayout("!", 1);
    return new HorizontalLayout(childrenLayouts, 2);
  }
  
  int Factorial::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits) const {
    if (bufferSize == 0) {
      return -1;
    }
    buffer[bufferSize-1] = 0;
    int numberOfChar = operand(0)->writeTextInBuffer(buffer, bufferSize, numberOfSignificantDigits);
    if (numberOfChar >= bufferSize-1) {
      return numberOfChar;
    }
    buffer[numberOfChar++] = '!';
    buffer[numberOfChar] = 0;
    return numberOfChar;
  }
  
  #if 0
  int Factorial::simplificationOrderGreaterType(const Expression * e) const {
    if (SimplificationOrder(operand(0),e) == 0) {
      return 1;
    }
    return SimplificationOrder(operand(0), e);
  }
  
  int Factorial::simplificationOrderSameType(const Expression * e) const {
    return SimplificationOrder(operand(0), e->operand(0));
  }
  #endif
  
  }