Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IExpression non IArithmetic #228

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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
Loading