Blame view

Modif/epsilon-master/poincare/src/logarithm.cpp 8.87 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  #include <poincare/logarithm.h>
  #include "layout/horizontal_layout.h"
  #include "layout/vertical_offset_layout.h"
  #include <poincare/addition.h>
  #include <poincare/approximation_engine.h>
  #include <poincare/arithmetic.h>
  #include <poincare/division.h>
  #include <poincare/multiplication.h>
  #include <poincare/naperian_logarithm.h>
  #include <poincare/power.h>
  #include <poincare/rational.h>
  #include <poincare/simplification_engine.h>
  #include <poincare/symbol.h>
  #include <poincare/undefined.h>
  #include <cmath>
  #include <ion.h>
  extern "C" {
  #include <assert.h>
  #include <stdlib.h>
  }
  
  namespace Poincare {
  
  Expression::Type Logarithm::type() const {
    return Type::Logarithm;
  }
  
  Expression * Logarithm::clone() const {
    return new Logarithm(operands(), numberOfOperands(), true);
  }
  
  template<typename T>
  std::complex<T> Logarithm::computeOnComplex(const std::complex<T> c, AngleUnit angleUnit) {
    /* log has a branch cut on ]-inf, 0]: it is then multivalued on this cut. We
     * followed the convention chosen by the lib c++ of llvm on ]-inf+0i, 0+0i]
     * (warning: log takes the other side of the cut values on ]-inf-0i, 0-0i]). */
    return std::log10(c);
  }
  
  Expression * Logarithm::simpleShallowReduce(Context & context, AngleUnit angleUnit) {
    Expression * op = editableOperand(0);
    // log(x,x)->1
    if (numberOfOperands() == 2 && op->isIdenticalTo(operand(1))) {
      return replaceWith(new Rational(1), true);
    }
    if (op->type() == Type::Rational) {
      const Rational * r = static_cast<const Rational *>(operand(0));
      // log(0) = undef
      if (r->isZero()) {
        return replaceWith(new Undefined(), true);
      }
      // log(1) = 0;
      if (r->isOne()) {
        return replaceWith(new Rational(0), true);
      }
      // log(10) ->1
      if (numberOfOperands() == 1 && r->isTen()) {
        return replaceWith(new Rational(1), true);
      }
    }
    return this;
  }
  
  Expression * Logarithm::shallowReduce(Context& context, AngleUnit angleUnit) {
    Expression * e = Expression::shallowReduce(context, angleUnit);
    if (e != this) {
      return e;
    }
    Expression * op = editableOperand(0);
  #if MATRIX_EXACT_REDUCING
    if (numberOfOperands() == 1 && op->type() == Type::Matrix) {
      return SimplificationEngine::map(this, context, angleUnit);
    }
    if (numberOfOperands() == 2 && (op->type() == Type::Matrix || operand(1)->type() == Type::Matrix)) {
      return replaceWith(new Undefined(), true);
    }
  #endif
    if (op->sign() == Sign::Negative || (numberOfOperands() == 2 && operand(1)->sign() == Sign::Negative)) {
      return this;
    }
    Expression * f = simpleShallowReduce(context, angleUnit);
    if (f != this) {
      return f;
    }
  
    /* We do not apply some rules if the parent node is a power of b. In this
     * case there is a simplication of form e^ln(3^(1/2))->3^(1/2) */
    bool letLogAtRoot = parentIsAPowerOfSameBase();
    // log(x^y, b)->y*log(x, b) if x>0
    if (!letLogAtRoot && op->type() == Type::Power && op->operand(0)->sign() == Sign::Positive) {
      Power * p = static_cast<Power *>(op);
      Expression * x = p->editableOperand(0);
      Expression * y = p->editableOperand(1);
      p->detachOperands();
      replaceOperand(p, x, true);
      Expression * newLog = shallowReduce(context, angleUnit);
      newLog = newLog->replaceWith(new Multiplication(y, newLog->clone(), false), true);
      return newLog->shallowReduce(context, angleUnit);
    }
    // log(x*y, b)->log(x,b)+log(y, b) if x,y>0
    if (!letLogAtRoot && op->type() == Type::Multiplication) {
      Addition * a = new Addition();
      for (int i = 0; i<op->numberOfOperands()-1; i++) {
        Expression * factor = op->editableOperand(i);
        if (factor->sign() == Sign::Positive) {
          Expression * newLog = clone();
          static_cast<Multiplication *>(op)->removeOperand(factor, false);
          newLog->replaceOperand(newLog->editableOperand(0), factor, true);
          a->addOperand(newLog);
          newLog->shallowReduce(context, angleUnit);
        }
      }
      if (a->numberOfOperands() > 0) {
        op->shallowReduce(context, angleUnit);
        Expression * reducedLastLog = shallowReduce(context, angleUnit);
        reducedLastLog->replaceWith(a, false);
        a->addOperand(reducedLastLog);
        return a->shallowReduce(context, angleUnit);
      } else {
        delete a;
      }
    }
    // log(r) = a0log(p0)+a1log(p1)+... with r = p0^a0*p1^a1*... (Prime decomposition)
    if (!letLogAtRoot && op->type() == Type::Rational) {
      const Rational * r = static_cast<const Rational *>(operand(0));
      Expression * n = splitInteger(r->numerator(), false, context, angleUnit);
      Expression * d = splitInteger(r->denominator(), true, context, angleUnit);
      Addition * a = new Addition(n, d, false);
      replaceWith(a, true);
      return a->shallowReduce(context, angleUnit);
    }
    return this;
  }
  
  bool Logarithm::parentIsAPowerOfSameBase() const {
    // We look for expressions of types e^ln(x) or e^(ln(x)) where ln is this
    const Expression * parentExpression = parent();
    bool thisIsPowerExponent = parentExpression->type() == Type::Power ? parentExpression->operand(1) == this : false;
    if (parentExpression->type() == Type::Parenthesis) {
      const Expression * parentParentExpression = parentExpression->parent();
      if (parentExpression == nullptr) {
        return false;
      }
      thisIsPowerExponent = parentParentExpression->type() == Type::Power ? parentParentExpression->operand(1) == parentExpression : false;
      parentExpression = parentParentExpression;
    }
    if (thisIsPowerExponent) {
      const Expression * powerOperand0 = parentExpression->operand(0);
      if (numberOfOperands() == 1) {
        if (powerOperand0->type() == Type::Rational && static_cast<const Rational *>(powerOperand0)->isTen()) {
          return true;
        }
      }
      if (numberOfOperands() == 2) {
        if (powerOperand0->isIdenticalTo(operand(1))){
          return true;
        }
      }
    }
    return false;
  }
  
  Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & context, AngleUnit angleUnit) {
    assert(!i.isZero());
    assert(!i.isNegative());
    if (i.isOne()) {
      return new Rational(0);
    }
    assert(!i.isOne());
    Integer factors[Arithmetic::k_maxNumberOfPrimeFactors];
    Integer coefficients[Arithmetic::k_maxNumberOfPrimeFactors];
    Arithmetic::PrimeFactorization(&i, factors, coefficients, Arithmetic::k_maxNumberOfPrimeFactors);
    if (coefficients[0].isMinusOne()) {
      /* We could not break i in prime factor (either it might take too many
       * factors or too much time). */
      Expression * e = clone();
      e->replaceOperand(e->operand(0), new Rational(i), true);
      if (!isDenominator) {
        return e;
      }
      Multiplication * m = new Multiplication(new Rational(-1), e, false);
      return m;
    }
    Addition * a = new Addition();
    int index = 0;
    while (!coefficients[index].isZero() && index < Arithmetic::k_maxNumberOfPrimeFactors) {
      if (isDenominator) {
        coefficients[index].setNegative(true);
      }
      Expression * e = clone();
      e->replaceOperand(e->operand(0), new Rational(factors[index]), true);
      Multiplication * m = new Multiplication(new Rational(coefficients[index]), e, false);
      static_cast<Logarithm *>(e)->simpleShallowReduce(context, angleUnit);
      a->addOperand(m);
      m->shallowReduce(context, angleUnit);
      index++;
    }
    return a;
  }
  
  Expression * Logarithm::shallowBeautify(Context & context, AngleUnit angleUnit) {
    Symbol e = Symbol(Ion::Charset::Exponential);
    const Expression * op = operand(0);
    Rational one(1);
    if (numberOfOperands() == 2 && (operand(1)->isIdenticalTo(&e) || operand(1)->isIdenticalTo(&one))) {
      detachOperand(op);
      Expression * nl = operand(1)->isIdenticalTo(&e) ? static_cast<Expression *>(new NaperianLogarithm(op, false)) : static_cast<Expression *> (new Logarithm(op, false));
      return replaceWith(nl, true);
    }
    return this;
  }
  
  template<typename T>
  Evaluation<T> * Logarithm::templatedApproximate(Context& context, AngleUnit angleUnit) const {
    if (numberOfOperands() == 1) {
      return ApproximationEngine::map(this, context, angleUnit, computeOnComplex<T>);
    }
    Evaluation<T> * x = operand(0)->privateApproximate(T(), context, angleUnit);
    Evaluation<T> * n = operand(1)->privateApproximate(T(), context, angleUnit);
    std::complex<T> result = std::complex<T>(NAN, NAN);
    if (x->type() == Evaluation<T>::Type::Complex && n->type() == Evaluation<T>::Type::Complex) {
      Complex<T> * xc = static_cast<Complex<T> *>(x);
      Complex<T> * nc = static_cast<Complex<T> *>(n);
      result = Division::compute<T>(computeOnComplex(*xc, angleUnit), computeOnComplex(*nc, angleUnit));
    }
    delete x;
    delete n;
    return new Complex<T>(result);
  }
  
  ExpressionLayout * Logarithm::createLayout(PrintFloat::Mode floatDisplayMode, int numberOfSignificantDigits) const {
    if (numberOfOperands() == 1) {
      return LayoutEngine::createPrefixLayout(this, floatDisplayMode, numberOfSignificantDigits, "log");
    }
    return LayoutEngine::createLogLayout(operand(0)->createLayout(floatDisplayMode, numberOfSignificantDigits), operand(1)->createLayout(floatDisplayMode, numberOfSignificantDigits));
  }
  
  }