Skip to content

Commit

Permalink
Make IExpression non IArithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Mar 28, 2024
1 parent 906ef37 commit 64dc952
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 268 deletions.
34 changes: 0 additions & 34 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ class Expression final : public IExpressionCRTP<Expression> {
static void registerExpressionConstructor(ExpressionConstructor constructor);

protected:
Expression &add(const Expression &rhs) override;

Expression &substract(const Expression &rhs) override;

Expression &multiply(const Expression &rhs) override;

Expression &divide(const Expression &rhs) override;

Expression &negate() override;

ArgumentPtr simplify() const override;

private:
Expand Down Expand Up @@ -169,30 +159,6 @@ class Expression final : public IExpressionCRTP<Expression> {
mutable bool isSimplified = false;
};

Expression operator+(const Variable &lhs, const Variable &rhs);

Expression operator+(const Expression &lhs, const Variable &rhs);

Expression operator+(const Variable &lhs, const Expression &rhs);

Expression operator-(const Variable &lhs, const Variable &rhs);

Expression operator-(const Expression &lhs, const Variable &rhs);

Expression operator-(const Variable &lhs, const Expression &rhs);

Expression operator*(const Variable &lhs, const Variable &rhs);

Expression operator*(const Expression &lhs, const Variable &rhs);

Expression operator*(const Variable &lhs, const Expression &rhs);

Expression operator/(const Variable &lhs, const Variable &rhs);

Expression operator/(const Expression &lhs, const Variable &rhs);

Expression operator/(const Variable &lhs, const Expression &rhs);

template <typename Function, bool isPolynomial>
void Expression::registerExpressionConstructor(ExpressionConstructor constructor) {
getExpressionMaker()[Function::getClassStatic()] = [maker = std::move(constructor)](ArgumentPtrVector &&args) {
Expand Down
18 changes: 8 additions & 10 deletions include/fintamath/expressions/ExpressionFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@

namespace fintamath {

Expression add(const auto &...args) {
return (Expression(args) + ...);
}
Expression operator+(const Expression &lhs, const Expression &rhs);

Expression mul(const auto &...args) {
return (Expression(args) * ...);
}
Expression operator+(const Expression &rhs);

Expression sub(const Expression &lhs, const Expression &rhs);
Expression operator-(const Expression &lhs, const Expression &rhs);

Expression div(const Expression &lhs, const Expression &rhs);
Expression operator-(const Expression &rhs);

Expression mod(const Expression &lhs, const Expression &rhs);
Expression operator*(const Expression &lhs, const Expression &rhs);

Expression neg(const Expression &rhs);
Expression operator/(const Expression &lhs, const Expression &rhs);

Expression mod(const Expression &lhs, const Expression &rhs);

Expression eqv(const Expression &lhs, const Expression &rhs);

Expand Down
3 changes: 1 addition & 2 deletions include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <utility>
#include <vector>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/Parser.hpp"
Expand All @@ -18,7 +17,7 @@

namespace fintamath {

class IExpression : public IArithmetic {
class IExpression : public IMathObject {
FINTAMATH_PARENT_CLASS_BODY(IExpression)

public:
Expand Down
6 changes: 3 additions & 3 deletions include/fintamath/expressions/IExpressionBaseCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class IExpressionBaseCRTP_ : public IExpression {

#endif // I_EXPRESSION_BASE_CRTP

#define I_ARITHMETIC_CRTP I_EXPRESSION_BASE_CRTP
#include "fintamath/core/IArithmeticCRTP.hpp"
#undef I_ARITHMETIC_CRTP
#define I_MATH_OBJECT_CRTP I_EXPRESSION_BASE_CRTP
#include "fintamath/core/IMathObjectCRTP.hpp"
#undef I_MATH_OBJECT_CRTP

private:
#if !defined(I_EXPRESSION_BASE_CRTP) && !defined(NDEBUG)
Expand Down
21 changes: 0 additions & 21 deletions include/fintamath/expressions/IExpressionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,6 @@ class IExpressionCRTP_ : public IExpressionBaseCRTP<Derived> {
return true;
}

protected:
Derived &add(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("+", this->toString(), rhs.toString());
}

Derived &substract(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("-", this->toString(), rhs.toString());
}

Derived &multiply(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("*", this->toString(), rhs.toString());
}

Derived &divide(const Derived &rhs) override {
throw InvalidInputBinaryOperatorException("/", this->toString(), rhs.toString());
}

Derived &negate() override {
throw InvalidInputUnaryOperatorException("-", this->toString(), InvalidInputUnaryOperatorException::Type::Prefix);
}

private:
#if !defined(I_EXPRESSION_CRTP) && !defined(NDEBUG)
};
Expand Down
1 change: 1 addition & 0 deletions include/fintamath/functions/IFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstddef>
#include <memory>
#include <string>
#include <tuple>
#include <unordered_map>

#include "fintamath/core/IMathObject.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/literals/Boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Boolean final : public ILiteralCRTP<Boolean> {

std::string toString() const override;

operator bool() const;
explicit operator bool() const;

private:
std::string name;
Expand Down
6 changes: 3 additions & 3 deletions src/fintamath/config/TypeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@
namespace fintamath::detail {

TypeConfig::TypeConfig() {
IMathObject::registerType<ILiteral>();
IMathObject::registerType<IFunction>();
IMathObject::registerType<IArithmetic>();
IMathObject::registerType<IFunction>();
IMathObject::registerType<ILiteral>();
IMathObject::registerType<IExpression>();

IArithmetic::registerType<IComparable>();
IArithmetic::registerType<IExpression>();

IComparable::registerType<INumber>();

Expand Down
78 changes: 0 additions & 78 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,6 @@ const std::shared_ptr<IFunction> &Expression::getFunction() const {
return func;
}

Expression &Expression::add(const Expression &rhs) {
child = makeExpr(Add{}, *child, *rhs.child);
isSimplified = false;
return *this;
}

Expression &Expression::substract(const Expression &rhs) {
child = makeExpr(Sub{}, *child, *rhs.child);
isSimplified = false;
return *this;
}

Expression &Expression::multiply(const Expression &rhs) {
child = makeExpr(Mul{}, *child, *rhs.child);
isSimplified = false;
return *this;
}

Expression &Expression::divide(const Expression &rhs) {
child = makeExpr(Div{}, *child, *rhs.child);
isSimplified = false;
return *this;
}

Expression &Expression::negate() {
child = makeExpr(Neg{}, *child);
isSimplified = false;
return *this;
}

const ArgumentPtrVector &Expression::getChildren() const {
simplifyMutable();
childrenCached.front() = child;
Expand Down Expand Up @@ -530,54 +500,6 @@ bool Expression::doesArgMatch(const MathObjectClass &expectedType, const Argumen
return true;
}

Expression operator+(const Variable &lhs, const Variable &rhs) {
return Expression(addExpr(lhs, rhs));
}

Expression operator+(const Expression &lhs, const Variable &rhs) {
return Expression(addExpr(lhs, rhs));
}

Expression operator+(const Variable &lhs, const Expression &rhs) {
return Expression(addExpr(lhs, rhs));
}

Expression operator-(const Variable &lhs, const Variable &rhs) {
return Expression(subExpr(lhs, rhs));
}

Expression operator-(const Expression &lhs, const Variable &rhs) {
return Expression(subExpr(lhs, rhs));
}

Expression operator-(const Variable &lhs, const Expression &rhs) {
return Expression(subExpr(lhs, rhs));
}

Expression operator*(const Variable &lhs, const Variable &rhs) {
return Expression(mulExpr(lhs, rhs));
}

Expression operator*(const Expression &lhs, const Variable &rhs) {
return Expression(mulExpr(lhs, rhs));
}

Expression operator*(const Variable &lhs, const Expression &rhs) {
return Expression(mulExpr(lhs, rhs));
}

Expression operator/(const Variable &lhs, const Variable &rhs) {
return Expression(divExpr(lhs, rhs));
}

Expression operator/(const Expression &lhs, const Variable &rhs) {
return Expression(divExpr(lhs, rhs));
}

Expression operator/(const Variable &lhs, const Expression &rhs) {
return Expression(divExpr(lhs, rhs));
}

namespace detail {

std::unique_ptr<IMathObject> makeExpr(const IFunction &func, ArgumentPtrVector args) {
Expand Down
33 changes: 25 additions & 8 deletions src/fintamath/expressions/ExpressionFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include "fintamath/expressions/Expression.hpp"
#include "fintamath/functions/arithmetic/Abs.hpp"
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/calculus/Derivative.hpp"
#include "fintamath/functions/comparison/Eqv.hpp"
#include "fintamath/functions/comparison/Less.hpp"
Expand Down Expand Up @@ -47,20 +52,32 @@

namespace fintamath {

Expression sub(const Expression &lhs, const Expression &rhs) {
return lhs - rhs;
Expression operator+(const Expression &lhs, const Expression &rhs) {
return Expression(addExpr(lhs, rhs));
}

Expression div(const Expression &lhs, const Expression &rhs) {
return lhs / rhs;
Expression operator+(const Expression &rhs) {
return rhs;
}

Expression mod(const Expression &lhs, const Expression &rhs) {
return Expression(modExpr(lhs, rhs));
Expression operator-(const Expression &lhs, const Expression &rhs) {
return Expression(subExpr(lhs, rhs));
}

Expression operator-(const Expression &rhs) {
return Expression(negExpr(rhs));
}

Expression operator*(const Expression &lhs, const Expression &rhs) {
return Expression(mulExpr(lhs, rhs));
}

Expression operator/(const Expression &lhs, const Expression &rhs) {
return Expression(divExpr(lhs, rhs));
}

Expression neg(const Expression &rhs) {
return -rhs;
Expression mod(const Expression &lhs, const Expression &rhs) {
return Expression(modExpr(lhs, rhs));
}

Expression eqv(const Expression &lhs, const Expression &rhs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ ArgumentPtrVector solveQuadraticEquation(const ArgumentPtrVector &coeffAtPow) {
const Expression b(coeffAtPow[1]);
const Expression c(coeffAtPow[0]);

const ArgumentPtr discriminantArg = sub(pow(b, 2), mul(4, a, c)).toMinimalObject();
const ArgumentPtr discriminantArg = (pow(b, 2) - (4 * a * c)).toMinimalObject();
const Expression discriminant(discriminantArg);

const Expression firstRoot = div(add(neg(b), sqrt(discriminant)), mul(2, a));
const Expression secondRoot = div(sub(neg(b), sqrt(discriminant)), mul(2, a));
const Expression firstRoot = (-b + sqrt(discriminant)) / 2 * a;
const Expression secondRoot = (-b - sqrt(discriminant)) / (2 * a);

return {firstRoot.getChildren().front(), secondRoot.getChildren().front()};
}
Expand Down
4 changes: 2 additions & 2 deletions tests/src/FintamathTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ TEST(FintamathTests, fintamathTest) {

//-------------------------------------------------------------------------------------//

expr = add(e(), pi(), Variable("a"), Variable("b"));
expr = e() + pi() + Variable("a") + Variable("b");
EXPECT_EQ(expr.toString(), "a + b + E + Pi");

expr = mul(e(), pi(), Variable("a"), Variable("b"));
expr = e() * pi() * Variable("a") * Variable("b");
EXPECT_EQ(expr.toString(), "E Pi a b");

expr = pow(Variable("a"), Variable("b")) * Variable("c");
Expand Down
Loading

0 comments on commit 64dc952

Please sign in to comment.