[poincare] Use default methods for GCD and LCM instead of n_ary ones

Change-Id: Id353e4367bcb6700bae2f9c121af93b9379a8051
This commit is contained in:
Hugo Saint-Vignes
2020-08-10 16:09:59 +02:00
committed by Émilie Feral
parent ec0e766c94
commit 74a2211a6a
10 changed files with 90 additions and 69 deletions

View File

@@ -106,6 +106,7 @@ poincare_src += $(addprefix poincare/src/,\
matrix_reduced_row_echelon_form.cpp \
multiplication.cpp \
n_ary_expression.cpp \
n_ary_infix_expression.cpp \
naperian_logarithm.cpp \
norm_cdf.cpp \
norm_cdf2.cpp \

View File

@@ -2,15 +2,15 @@
#define POINCARE_ADDITION_H
#include <poincare/approximation_helper.h>
#include <poincare/n_ary_expression.h>
#include <poincare/n_ary_infix_expression.h>
#include <poincare/rational.h>
namespace Poincare {
class AdditionNode final : public NAryExpressionNode {
class AdditionNode final : public NAryInfixExpressionNode {
friend class Addition;
public:
using NAryExpressionNode::NAryExpressionNode;
using NAryInfixExpressionNode::NAryInfixExpressionNode;
// Tree
size_t size() const override { return sizeof(AdditionNode); }

View File

@@ -120,6 +120,7 @@ class Expression : public TreeHandle {
friend class MatrixNode;
friend class NaperianLogarithmNode;
friend class NAryExpressionNode;
friend class NAryInfixExpressionNode;
friend class StoreNode;
friend class SymbolNode;
friend class UnitNode;

View File

@@ -20,6 +20,7 @@ class ExpressionNode : public TreeNode {
friend class AdditionNode;
friend class DivisionNode;
friend class NAryExpressionNode;
friend class NAryInfixExpressionNode;
friend class PowerNode;
friend class SymbolNode;
public:

View File

@@ -2,14 +2,14 @@
#define POINCARE_MULTIPLICATION_H
#include <poincare/approximation_helper.h>
#include <poincare/n_ary_expression.h>
#include <poincare/n_ary_infix_expression.h>
namespace Poincare {
class MultiplicationNode final : public NAryExpressionNode {
class MultiplicationNode final : public NAryInfixExpressionNode {
friend class Addition;
public:
using NAryExpressionNode::NAryExpressionNode;
using NAryInfixExpressionNode::NAryInfixExpressionNode;
// Tree
size_t size() const override { return sizeof(MultiplicationNode); }

View File

@@ -3,8 +3,6 @@
#include <poincare/expression.h>
// NAryExpressions are additions and multiplications
namespace Poincare {
class NAryExpressionNode : public ExpressionNode { // TODO: VariableArityExpressionNode?
@@ -20,9 +18,6 @@ public:
}
void eraseNumberOfChildren() override { m_numberOfChildren = 0; }
// Properties
bool childAtIndexNeedsUserParentheses(const Expression & child, int childIndex) const override;
// Comparison
typedef int (*ExpressionOrder)(const ExpressionNode * e1, const ExpressionNode * e2, bool canBeInterrupted);
@@ -37,9 +32,6 @@ protected:
/* With a pool of size < 120k and TreeNode of size 20, a node can't have more
* than 6144 children which fit in uint16_t. */
uint16_t m_numberOfChildren;
private:
int simplificationOrderSameType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const override;
int simplificationOrderGreaterType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const override;
};
class NAryExpression : public Expression {

View File

@@ -0,0 +1,23 @@
#ifndef POINCARE_N_ARY_INFIX_EXPRESSION_H
#define POINCARE_N_ARY_INFIX_EXPRESSION_H
#include <poincare/n_ary_expression.h>
// NAryInfixExpressionNode are additions and multiplications
namespace Poincare {
class NAryInfixExpressionNode : public NAryExpressionNode {
public:
using NAryExpressionNode::NAryExpressionNode;
// Properties
bool childAtIndexNeedsUserParentheses(const Expression & child, int childIndex) const override;
protected:
// Order
int simplificationOrderSameType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const override;
int simplificationOrderGreaterType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const override;
};
}
#endif

View File

@@ -57,7 +57,7 @@ int MultiplicationNode::getPolynomialCoefficients(Context * context, const char
}
bool MultiplicationNode::childAtIndexNeedsUserParentheses(const Expression & child, int childIndex) const {
if (NAryExpressionNode::childAtIndexNeedsUserParentheses(child, childIndex)) {
if (NAryInfixExpressionNode::childAtIndexNeedsUserParentheses(child, childIndex)) {
return true;
}
Type types[] = {Type::Subtraction, Type::Addition};

View File

@@ -1,5 +1,4 @@
#include <poincare/n_ary_expression.h>
#include <poincare/number.h>
#include <poincare/rational.h>
extern "C" {
#include <assert.h>
@@ -9,21 +8,6 @@ extern "C" {
namespace Poincare {
bool NAryExpressionNode::childAtIndexNeedsUserParentheses(const Expression & child, int childIndex) const {
/* Expressions like "-2" require parentheses in Addition/Multiplication except
* when they are the first operand. */
if (childIndex != 0
&& ((child.isNumber() && static_cast<const Number &>(child).sign() == Sign::Negative)
|| child.type() == Type::Opposite))
{
return true;
}
if (child.type() == Type::Conjugate) {
return childAtIndexNeedsUserParentheses(child.childAtIndex(0), childIndex);
}
return false;
}
void NAryExpressionNode::sortChildrenInPlace(ExpressionOrder order, Context * context, bool canSwapMatrices, bool canBeInterrupted) {
Expression reference(this);
const int childrenCount = reference.numberOfChildren();
@@ -60,44 +44,6 @@ Expression NAryExpressionNode::squashUnaryHierarchyInPlace() {
return std::move(reference);
}
// Private
int NAryExpressionNode::simplificationOrderSameType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const {
int m = numberOfChildren();
int n = e->numberOfChildren();
for (int i = 1; i <= m; i++) {
// The NULL node is the least node type.
if (n < i) {
return 1;
}
int order = SimplificationOrder(childAtIndex(m-i), e->childAtIndex(n-i), ascending, canBeInterrupted, ignoreParentheses);
if (order != 0) {
return order;
}
}
// The NULL node is the least node type.
if (n > m) {
return ascending ? -1 : 1;
}
return 0;
}
int NAryExpressionNode::simplificationOrderGreaterType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const {
int m = numberOfChildren();
if (m == 0) {
return -1;
}
/* Compare e to last term of hierarchy. */
int order = SimplificationOrder(childAtIndex(m-1), e, ascending, canBeInterrupted, ignoreParentheses);
if (order != 0) {
return order;
}
if (m > 1) {
return ascending ? 1 : -1;
}
return 0;
}
void NAryExpression::mergeSameTypeChildrenInPlace() {
// Multiplication is associative: a*(b*c)->a*b*c
// The same goes for Addition

View File

@@ -0,0 +1,57 @@
#include <poincare/n_ary_infix_expression.h>
#include <poincare/number.h>
namespace Poincare {
bool NAryInfixExpressionNode::childAtIndexNeedsUserParentheses(const Expression & child, int childIndex) const {
/* Expressions like "-2" require parentheses in Addition/Multiplication except
* when they are the first operand. */
if (childIndex != 0
&& ((child.isNumber() && static_cast<const Number &>(child).sign() == Sign::Negative)
|| child.type() == Type::Opposite))
{
return true;
}
if (child.type() == Type::Conjugate) {
return childAtIndexNeedsUserParentheses(child.childAtIndex(0), childIndex);
}
return false;
}
int NAryInfixExpressionNode::simplificationOrderSameType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const {
int m = numberOfChildren();
int n = e->numberOfChildren();
for (int i = 1; i <= m; i++) {
// The NULL node is the least node type.
if (n < i) {
return 1;
}
int order = SimplificationOrder(childAtIndex(m-i), e->childAtIndex(n-i), ascending, canBeInterrupted, ignoreParentheses);
if (order != 0) {
return order;
}
}
// The NULL node is the least node type.
if (n > m) {
return ascending ? -1 : 1;
}
return 0;
}
int NAryInfixExpressionNode::simplificationOrderGreaterType(const ExpressionNode * e, bool ascending, bool canBeInterrupted, bool ignoreParentheses) const {
int m = numberOfChildren();
if (m == 0) {
return -1;
}
/* Compare e to last term of hierarchy. */
int order = SimplificationOrder(childAtIndex(m-1), e, ascending, canBeInterrupted, ignoreParentheses);
if (order != 0) {
return order;
}
if (m > 1) {
return ascending ? 1 : -1;
}
return 0;
}
}