Numworks Epsilon  1.4.1
Graphing Calculator Operating System
subtraction.cpp
Go to the documentation of this file.
1 extern "C" {
2 #include <assert.h>
3 #include <stdlib.h>
4 }
5 
6 #include <poincare/subtraction.h>
8 #include <poincare/rational.h>
9 #include <poincare/addition.h>
10 #include <poincare/opposite.h>
11 #include <poincare/matrix.h>
13 #include "layout/string_layout.h"
15 
16 namespace Poincare {
17 
20 }
21 
23  return new Subtraction(m_operands, true);
24 }
25 
26 int Subtraction::polynomialDegree(char symbolName) const {
27  int degree = 0;
28  for (int i = 0; i < numberOfOperands(); i++) {
29  int d = operand(i)->polynomialDegree(symbolName);
30  if (d < 0) {
31  return -1;
32  }
33  degree = d > degree ? d : degree;
34  }
35  return degree;
36 }
37 
38 /* Layout */
39 
40 bool Subtraction::needParenthesisWithParent(const Expression * e) const {
42  return e->isOfType(types, 6);
43 }
44 
45 template<typename T>
47  return Complex<T>::Cartesian(c.a()-d.a(), c.b() - d.b());
48 }
49 
50 template<typename T> Matrix * Subtraction::computeOnComplexAndMatrix(const Complex<T> * c, const Matrix * m) {
51  Matrix * opposite = computeOnMatrixAndComplex(m, c);
52  if (opposite == nullptr) {
53  return nullptr;
54  }
55  Expression ** operands = new Expression * [opposite->numberOfRows() * opposite->numberOfColumns()];
56  for (int i = 0; i < opposite->numberOfOperands(); i++) {
57  const Complex<T> * entry = static_cast<const Complex<T> *>(opposite->operand(i));
58  operands[i] = new Complex<T>(Complex<T>::Cartesian(-entry->a(), -entry->b()));
59  }
60  Matrix * result = new Matrix(operands, m->numberOfRows(), m->numberOfColumns(), false);
61  delete[] operands;
62  delete opposite;
63  return result;
64 }
65 
66 Expression * Subtraction::shallowReduce(Context& context, AngleUnit angleUnit) {
67  Expression * e = Expression::shallowReduce(context, angleUnit);
68  if (e != this) {
69  return e;
70  }
71  Multiplication * m = new Multiplication(new Rational(-1), operand(1), false);
72  Addition * a = new Addition(operand(0), m, false);
74  m->shallowReduce(context, angleUnit);
75  replaceWith(a, true);
76  return a->shallowReduce(context, angleUnit);
77 }
78 
79 }
int numberOfColumns() const
Definition: matrix.cpp:40
friend class Addition
Definition: expression.h:22
static Complex< T > Cartesian(T a, T b)
Definition: complex.cpp:28
Expression * replaceWith(Expression *newOperand, bool deleteAfterReplace=true)
Definition: expression.cpp:85
Expression * clone() const override
Definition: subtraction.cpp:22
T a() const
Definition: complex.cpp:99
const Expression *const * operands() const override
Type type() const override
Definition: subtraction.cpp:18
c(generic_all_nodes)
friend class Rational
Definition: expression.h:18
int polynomialDegree(char symbolName) const override
Definition: subtraction.cpp:26
friend class Subtraction
Definition: expression.h:70
const Expression * m_operands[T]
friend class Multiplication
Definition: expression.h:20
static Complex< T > compute(const Complex< T > c, const Complex< T > d)
Definition: subtraction.cpp:46
int numberOfRows() const
Definition: matrix.cpp:36
int numberOfOperands() const override
friend class Matrix
Definition: expression.h:73
int numberOfOperands() const override
const Expression * operand(int i) const
Definition: expression.cpp:78
virtual int polynomialDegree(char symbolName) const
Definition: expression.cpp:202