Numworks Epsilon  1.4.1
Graphing Calculator Operating System
opposite.cpp
Go to the documentation of this file.
1 #include <poincare/opposite.h>
2 #include <poincare/complex.h>
5 #include <poincare/rational.h>
6 extern "C" {
7 #include <assert.h>
8 #include <stdlib.h>
9 }
10 #include <cmath>
13 #include "layout/string_layout.h"
14 
15 namespace Poincare {
16 
18  return Type::Opposite;
19 }
20 
22  Opposite * o = new Opposite(m_operands, true);
23  return o;
24 }
25 
26 int Opposite::polynomialDegree(char symbolName) const {
27  return operand(0)->polynomialDegree(symbolName);
28 }
29 
31  if (operand(0)->sign() == Sign::Positive) {
32  return Sign::Negative;
33  }
34  if (operand(0)->sign() == Sign::Negative) {
35  return Sign::Positive;
36  }
37  return Sign::Unknown;
38 }
39 
40 /* Layout */
41 
42 bool Opposite::needParenthesisWithParent(const Expression * e) const {
44  return e->isOfType(types, 7);
45 }
46 
47 template<typename T>
49  return Complex<T>::Cartesian(-c.a(), -c.b());
50 }
51 
52 Expression * Opposite::shallowReduce(Context& context, AngleUnit angleUnit) {
53  Expression * e = Expression::shallowReduce(context, angleUnit);
54  if (e != this) {
55  return e;
56  }
57  const Expression * op = operand(0);
58 #if MATRIX_EXACT_REDUCING
59  if (op->type() == Type::Matrix) {
60  return SimplificationEngine::map(this, context, angleUnit);
61  }
62 #endif
63  detachOperand(op);
64  Multiplication * m = new Multiplication(new Rational(-1), op, false);
65  replaceWith(m, true);
66  return m->shallowReduce(context, angleUnit);
67 }
68 
69 ExpressionLayout * Opposite::privateCreateLayout(PrintFloat::Mode floatDisplayMode, ComplexFormat complexFormat) const {
70  assert(floatDisplayMode != PrintFloat::Mode::Default);
71  assert(complexFormat != ComplexFormat::Default);
72  ExpressionLayout * children_layouts[2];
73  char string[2] = {'-', '\0'};
74  children_layouts[0] = new StringLayout(string, 1);
75  children_layouts[1] = operand(0)->type() == Type::Opposite ? new ParenthesisLayout(operand(0)->createLayout(floatDisplayMode, complexFormat)) : operand(0)->createLayout(floatDisplayMode, complexFormat);
76  return new HorizontalLayout(children_layouts, 2);
77 }
78 
79 int Opposite::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits) const {
80  if (bufferSize == 0) {
81  return -1;
82  }
83  buffer[bufferSize-1] = 0;
84  int numberOfChar = 0;
85  if (bufferSize == 1) { return 0; }
86  buffer[numberOfChar++] = '-';
87  numberOfChar += operand(0)->writeTextInBuffer(buffer+numberOfChar, bufferSize-numberOfChar, numberOfSignificantDigits);
88  buffer[numberOfChar] = 0;
89  return numberOfChar;
90 }
91 
92 }
93 
94 template Poincare::Complex<float> Poincare::Opposite::compute<float>(Poincare::Complex<float>, AngleUnit angleUnit);
95 template Poincare::Complex<double> Poincare::Opposite::compute<double>(Poincare::Complex<double>, AngleUnit angleUnit);
Expression * clone() const override
Definition: opposite.cpp:21
int polynomialDegree(char symbolName) const override
Definition: opposite.cpp:26
static Complex< T > Cartesian(T a, T b)
Definition: complex.cpp:28
#define assert(e)
Definition: assert.h:9
Expression * replaceWith(Expression *newOperand, bool deleteAfterReplace=true)
Definition: expression.cpp:85
static Complex< T > compute(const Complex< T > c, AngleUnit angleUnit)
Definition: opposite.cpp:48
c(generic_all_nodes)
friend class Rational
Definition: expression.h:18
friend class Opposite
Definition: expression.h:62
const Expression * m_operands[T]
friend class Multiplication
Definition: expression.h:20
ExpressionLayout * createLayout(PrintFloat::Mode floatDisplayMode=PrintFloat::Mode::Default, ComplexFormat complexFormat=ComplexFormat::Default) const
Definition: expression.cpp:244
Sign sign() const override
Definition: opposite.cpp:30
Type type() const override
Definition: opposite.cpp:17
void detachOperand(const Expression *e)
Definition: expression.cpp:115
const Expression * operand(int i) const
Definition: expression.cpp:78
virtual int polynomialDegree(char symbolName) const
Definition: expression.cpp:202
virtual Type type() const =0
virtual int writeTextInBuffer(char *buffer, int bufferSize, int numberOfSignificantDigits=PrintFloat::k_numberOfStoredSignificantDigits) const =0