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
|
#include <poincare/great_common_divisor.h>
#include <poincare/undefined.h>
#include <poincare/rational.h>
#include <poincare/arithmetic.h>
extern "C" {
#include <assert.h>
}
#include <cmath>
namespace Poincare {
Expression::Type GreatCommonDivisor::type() const {
return Type::GreatCommonDivisor;
}
Expression * GreatCommonDivisor::clone() const {
GreatCommonDivisor * a = new GreatCommonDivisor(m_operands, true);
return a;
}
Expression * GreatCommonDivisor::shallowReduce(Context& context, AngleUnit angleUnit) {
Expression * e = Expression::shallowReduce(context, angleUnit);
if (e != this) {
return e;
}
Expression * op0 = editableOperand(0);
Expression * op1 = editableOperand(1);
#if MATRIX_EXACT_REDUCING
if (op0->type() == Type::Matrix || op1->type() == Type::Matrix) {
return replaceWith(new Undefined(), true);
}
#endif
if (op0->type() == Type::Rational) {
Rational * r0 = static_cast<Rational *>(op0);
if (!r0->denominator().isOne()) {
return replaceWith(new Undefined(), true);
}
}
if (op1->type() == Type::Rational) {
Rational * r1 = static_cast<Rational *>(op1);
if (!r1->denominator().isOne()) {
return replaceWith(new Undefined(), true);
}
}
if (op0->type() != Type::Rational || op1->type() != Type::Rational) {
return this;
}
Rational * r0 = static_cast<Rational *>(op0);
Rational * r1 = static_cast<Rational *>(op1);
Integer a = r0->numerator();
Integer b = r1->numerator();
Integer gcd = Arithmetic::GCD(&a, &b);
return replaceWith(new Rational(gcd), true);
}
template<typename T>
Complex<T> * GreatCommonDivisor::templatedApproximate(Context& context, AngleUnit angleUnit) const {
Evaluation<T> * f1Input = operand(0)->privateApproximate(T(), context, angleUnit);
Evaluation<T> * f2Input = operand(1)->privateApproximate(T(), context, angleUnit);
T f1 = f1Input->toScalar();
T f2 = f2Input->toScalar();
delete f1Input;
delete f2Input;
if (std::isnan(f1) || std::isnan(f2) || f1 != (int)f1 || f2 != (int)f2) {
return new Complex<T>(Complex<T>::Undefined());
}
int a = (int)f2;
int b = (int)f1;
if (f1 > f2) {
b = a;
a = (int)f1;
}
int r = 0;
while((int)b!=0){
r = a - ((int)(a/b))*b;
a = b;
b = r;
}
return new Complex<T>(std::round((T)a));
}
}
|