Blame view

Giac_maj/epsilon-giac/poincare/src/function.cpp 3.62 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
  extern "C" {
  #include <assert.h>
  #include <stdlib.h>
  }
  #include <cmath>
  #include <poincare/function.h>
  #include <poincare/complex.h>
  #include "layout/horizontal_layout.h"
  #include "layout/parenthesis_layout.h"
  #include "layout/string_layout.h"
  
  namespace Poincare {
  
  Function::Function(const char * name, int requiredNumberOfArguments) :
    m_args(nullptr),
    m_numberOfArguments(0),
    m_requiredNumberOfArguments(requiredNumberOfArguments),
    m_name(name)
  {
  }
  
  void Function::setArgument(Expression ** args, int numberOfArguments, bool clone) {
    build(args, numberOfArguments, clone);
  }
  
  void Function::setArgument(ListData * listData, bool clone) {
    build(listData->operands(), listData->numberOfOperands(), clone);
  }
  
  Function::~Function() {
    clean();
  }
  
  bool Function::hasValidNumberOfArguments() const {
    if (m_numberOfArguments != m_requiredNumberOfArguments) {
      return false;
    }
    for (int i = 0; i < m_requiredNumberOfArguments; i++) {
      if (!m_args[i]->hasValidNumberOfArguments()) {
        return false;
      }
    }
    return true;
  }
  
  const Expression * Function::operand(int i) const {
    assert(i >= 0 && i < m_numberOfArguments);
    return m_args[i];
  }
  
  int Function::numberOfOperands() const {
    return m_numberOfArguments;
  }
  
  Expression * Function::clone() const {
    return this->cloneWithDifferentOperands(m_args, m_numberOfArguments, true);
  }
  
  template<typename T>
  Evaluation<T> * Function::templatedEvaluate(Context& context, AngleUnit angleUnit) const {
    if (m_numberOfArguments != 1) {
      return new Complex<T>(Complex<T>::Float(NAN));
    }
    Evaluation<T> * input = m_args[0]->evaluate<T>(context, angleUnit);
    Complex<T> * operands = new Complex<T>[input->numberOfRows()*input->numberOfColumns()];
    for (int i = 0; i < input->numberOfOperands(); i++) {
      operands[i] = computeComplex(*input->complexOperand(i), angleUnit);
    }
    Evaluation<T> * result = nullptr;
    if (input->numberOfOperands() == 1) {
      result = new Complex<T>(operands[0]);
    } else {
      result = new ComplexMatrix<T>(operands, input->numberOfRows(), input->numberOfColumns());
    }
    delete input;
    delete[] operands;
    return result;
  }
  
  ExpressionLayout * Function::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
    assert(floatDisplayMode != FloatDisplayMode::Default);
    assert(complexFormat != ComplexFormat::Default);
    ExpressionLayout ** grandChildrenLayouts = new ExpressionLayout *[2*m_numberOfArguments-1];
    int layoutIndex = 0;
    grandChildrenLayouts[layoutIndex++] = m_args[0]->createLayout(floatDisplayMode, complexFormat);
    for (int i = 1; i < m_numberOfArguments; i++) {
      grandChildrenLayouts[layoutIndex++] = new StringLayout(",", 1);
      grandChildrenLayouts[layoutIndex++] = m_args[i]->createLayout(floatDisplayMode, complexFormat);
    }
    ExpressionLayout * argumentLayouts = new HorizontalLayout(grandChildrenLayouts, 2*m_numberOfArguments-1);
    delete [] grandChildrenLayouts;
    ExpressionLayout * childrenLayouts[2];
    childrenLayouts[0] = new StringLayout(m_name, strlen(m_name));
    childrenLayouts[1] = new ParenthesisLayout(argumentLayouts);
    return new HorizontalLayout(childrenLayouts, 2);
  }
  
  void Function::build(Expression ** args, int numberOfArguments, bool clone) {
    clean();
    m_numberOfArguments = numberOfArguments;
    m_args = new Expression * [numberOfArguments];
    for (int i = 0; i < numberOfArguments; i++) {
      assert(args[i] != nullptr);
      if (clone) {
        m_args[i] = args[i]->clone();
      } else {
        m_args[i] = args[i];
      }
    }
  }
  
  void Function::clean() {
      if (m_args != nullptr) {
      for (int i = 0; i < m_numberOfArguments; i++) {
        delete m_args[i];
      }
      delete[] m_args;
    }
  }
  
  }