division.cpp
2.71 KB
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
extern "C" {
#include <assert.h>
#include <string.h>
#include <float.h>
}
#include <poincare/division.h>
#include <poincare/power.h>
#include <poincare/rational.h>
#include <poincare/tangent.h>
#include <poincare/multiplication.h>
#include <poincare/opposite.h>
#include "layout/fraction_layout.h"
#include <cmath>
namespace Poincare {
Expression::Type Division::type() const {
return Type::Division;
}
Expression * Division::clone() const {
return new Division(m_operands, true);
}
int Division::polynomialDegree(char symbolName) const {
if (operand(1)->polynomialDegree(symbolName) != 0) {
return -1;
}
return operand(0)->polynomialDegree(symbolName);
}
bool Division::needParenthesisWithParent(const Expression * e) const {
Type types[] = {Type::Division, Type::Power, Type::Factorial};
return e->isOfType(types, 3);
}
Expression * Division::shallowReduce(Context& context, AngleUnit angleUnit) {
Expression * e = Expression::shallowReduce(context, angleUnit);
if (e != this) {
return e;
}
Power * p = new Power(operand(1), new Rational(-1), false);
Multiplication * m = new Multiplication(operand(0), p, false);
detachOperands();
p->shallowReduce(context, angleUnit);
replaceWith(m, true);
return m->shallowReduce(context, angleUnit);
}
template<typename T>
std::complex<T> Division::compute(const std::complex<T> c, const std::complex<T> d) {
return c/d;
}
ExpressionLayout * Division::createLayout(PrintFloat::Mode floatDisplayMode, int numberOfSignificantDigits) const {
const Expression * numerator = operand(0)->type() == Type::Parenthesis ? operand(0)->operand(0) : operand(0);
const Expression * denominator = operand(1)->type() == Type::Parenthesis ? operand(1)->operand(0) : operand(1);
return new FractionLayout(numerator->createLayout(floatDisplayMode, numberOfSignificantDigits), denominator->createLayout(floatDisplayMode, numberOfSignificantDigits), false);
}
template<typename T> MatrixComplex<T> Division::computeOnComplexAndMatrix(const std::complex<T> c, const MatrixComplex<T> n) {
MatrixComplex<T> * inverse = n.createInverse();
if (inverse == nullptr) {
return MatrixComplex<T>::Undefined();
}
MatrixComplex<T> result = Multiplication::computeOnComplexAndMatrix<T>(c, *inverse);
delete inverse;
return result;
}
template<typename T> MatrixComplex<T> Division::computeOnMatrices(const MatrixComplex<T> m, const MatrixComplex<T> n) {
if (m.numberOfColumns() != n.numberOfColumns()) {
return MatrixComplex<T>::Undefined();
}
MatrixComplex<T> * inverse = n.createInverse();
if (inverse == nullptr) {
return MatrixComplex<T>::Undefined();
}
MatrixComplex<T> result = Multiplication::computeOnMatrices<T>(m, *inverse);
delete inverse;
return result;
}
}