Blame view

emulateur/epsilon-nofrendo/poincare/src/subtraction.cpp 2.06 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
  #include <poincare/subtraction.h>
  #include <poincare/addition.h>
  #include <poincare/matrix.h>
  #include <poincare/multiplication.h>
  #include <poincare/opposite.h>
  #include <poincare/rational.h>
  extern "C" {
  #include <assert.h>
  #include <stdlib.h>
  }
  
  namespace Poincare {
  
  Expression::Type Subtraction::type() const {
    return Expression::Type::Subtraction;
  }
  
  Expression * Subtraction::clone() const {
    return new Subtraction(m_operands, true);
  }
  
  int Subtraction::polynomialDegree(char symbolName) const {
    int degree = 0;
    for (int i = 0; i < numberOfOperands(); i++) {
      int d = operand(i)->polynomialDegree(symbolName);
      if (d < 0) {
        return -1;
      }
      degree = d > degree ? d : degree;
    }
    return degree;
  }
  
  /* Layout */
  
  bool Subtraction::needParenthesisWithParent(const Expression * e) const {
    Type types[] = {Type::Subtraction, Type::Opposite, Type::Multiplication, Type::Division, Type::Power, Type::Factorial};
    return e->isOfType(types, 6);
  }
  
  template<typename T>
  std::complex<T> Subtraction::compute(const std::complex<T> c, const std::complex<T> d) {
    return c - d;
  }
  
  template<typename T> MatrixComplex<T> Subtraction::computeOnComplexAndMatrix(const std::complex<T> c, const MatrixComplex<T> m) {
    MatrixComplex<T> opposite = computeOnMatrixAndComplex(m, c);
    std::complex<T> * operands = new std::complex<T> [opposite.numberOfComplexOperands()];
    for (int i = 0; i < opposite.numberOfComplexOperands(); i++) {
      operands[i] = -opposite.complexOperand(i);
    }
    MatrixComplex<T> result = MatrixComplex<T>(operands, opposite.numberOfRows(), opposite.numberOfColumns());
    delete[] operands;
    return result;
  }
  
  Expression * Subtraction::shallowReduce(Context& context, AngleUnit angleUnit) {
    Expression * e = Expression::shallowReduce(context, angleUnit);
    if (e != this) {
      return e;
    }
    Multiplication * m = new Multiplication(new Rational(-1), operand(1), false);
    Addition * a = new Addition(operand(0), m, false);
    detachOperands();
    m->shallowReduce(context, angleUnit);
    replaceWith(a, true);
    return a->shallowReduce(context, angleUnit);
  }
  
  }