Skip to content

Commit

Permalink
Add new functions and rework operators
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Apr 6, 2024
1 parent 3923b4e commit 9e2a499
Show file tree
Hide file tree
Showing 82 changed files with 1,149 additions and 702 deletions.
9 changes: 2 additions & 7 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Expression final : public IExpressionCRTP<Expression> {

void setVariable(const Variable &var, const Expression &val);

template <typename Function, bool isPolynomial = false>
template <typename Function>
static void registerExpressionConstructor(ExpressionConstructor constructor);

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

template <typename Function, bool isPolynomial>
template <typename Function>
void Expression::registerExpressionConstructor(ExpressionConstructor constructor) {
getExpressionMaker()[Function::getClassStatic()] = [maker = std::move(constructor)](ArgumentPtrVector &&args) {
static const size_t funcArgSize = Function{}.getArgumentClasses().size();
Expand All @@ -169,11 +169,6 @@ void Expression::registerExpressionConstructor(ExpressionConstructor constructor
if constexpr (Function::isVariadicStatic()) {
res = maker(std::move(args));
}
else if constexpr (isPolynomial) {
if (funcArgSize <= args.size()) {
res = maker(std::move(args));
}
}
else {
if (funcArgSize == args.size()) {
res = maker(std::move(args));
Expand Down
4 changes: 1 addition & 3 deletions include/fintamath/expressions/ExpressionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ std::pair<ArgumentPtr, ArgumentPtr> splitRational(const ArgumentPtr &arg);

ArgumentPtr negate(const ArgumentPtr &arg);

ArgumentPtr makePolynom(const IFunction &func, ArgumentPtrVector &&args);

ArgumentPtr makePolynom(const IFunction &func, const ArgumentPtrVector &args);
ArgumentPtr invert(const ArgumentPtr &arg);

ArgumentPtrVector getPolynomChildren(const IFunction &func, const ArgumentPtr &arg);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <compare>
#include <functional>
#include <memory>
#include <string>
Expand Down Expand Up @@ -41,8 +40,6 @@ class IPolynomExpression : public IExpression {

virtual std::string childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const;

virtual std::strong_ordering compare(const ArgumentPtr &lhs, const ArgumentPtr &rhs) const;

ArgumentPtr preSimplify() const override;

ArgumentPtr postSimplify() const override;
Expand All @@ -51,6 +48,8 @@ class IPolynomExpression : public IExpression {

virtual bool isComparableOrderInversed() const;

virtual std::strong_ordering compare(const ArgumentPtr &lhs, const ArgumentPtr &rhs) const;

private:
void simplifyRec(bool isPostSimplify);

Expand Down
2 changes: 0 additions & 2 deletions include/fintamath/functions/FunctionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class IFunction;

namespace detail {

extern bool isExpression(const IMathObject &arg);

extern std::unique_ptr<IMathObject> makeExpr(const IFunction &func, ArgumentPtrVector args);

extern std::unique_ptr<IMathObject> makeExpr(const IFunction &func, const ArgumentRefVector &args);
Expand Down
2 changes: 0 additions & 2 deletions include/fintamath/functions/IFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class IFunction : public IMathObject {

protected:
virtual std::unique_ptr<IMathObject> callAbstract(const ArgumentRefVector &argVect) const = 0;

virtual void validateArgsSize(const ArgumentRefVector &argVect) const;
};

template <typename Return, typename Derived, typename... Args>
Expand Down
25 changes: 16 additions & 9 deletions include/fintamath/functions/IFunctionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class IFunctionCRTP_ : public IFunction {

bool doArgsMatch(const ArgumentRefVector &argVect) const override {
if constexpr (Derived::isVariadicStatic()) {
return doAnyArgsMatch(argVect);
return doVariadicArgsMatch(argVect);
}
else {
if (argVect.size() != getArgumentClassesStatic().size()) {
Expand Down Expand Up @@ -65,8 +65,6 @@ class IFunctionCRTP_ : public IFunction {
virtual std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const = 0;

std::unique_ptr<IMathObject> callAbstract(const ArgumentRefVector &argVect) const override {
validateArgsSize(argVect);

if (doArgsMatch(argVect)) {
try {
if (auto res = call(argVect)) {
Expand All @@ -85,27 +83,36 @@ class IFunctionCRTP_ : public IFunction {

private:
template <size_t i, typename Head, typename... Tail>
bool doArgsMatch(const ArgumentRefVector &argVect) const {
if (!is<Head>(argVect[i]) || detail::isExpression(argVect[i])) {
static bool doArgsMatch(const ArgumentRefVector &argVect) {
if (!doArgMatch<Head>(argVect[i])) {
return false;
}

return doArgsMatch<i + 1, Tail...>(argVect);
}

template <size_t>
bool doArgsMatch(const ArgumentRefVector & /*unused*/) const {
static bool doArgsMatch(const ArgumentRefVector &) {
return true;
}

bool doAnyArgsMatch(const ArgumentRefVector &argVect) const {
using AnyArgsType = typename std::tuple_element_t<0, std::tuple<Args...>>;
static bool doVariadicArgsMatch(const ArgumentRefVector &argVect) {
if (argVect.empty()) {
return false;
}

using ExpectedArg = typename std::tuple_element_t<0, std::tuple<Args...>>;

return std::ranges::all_of(argVect, [](const auto &arg) {
return is<AnyArgsType>(arg);
return doArgMatch<ExpectedArg>(arg);
});
}

template <typename Expected>
static bool doArgMatch(const ArgumentRef &arg) {
return is<Expected>(arg);
}

private:
#if !defined(I_FUNCTION_CRTP) && !defined(NDEBUG)
};
Expand Down
12 changes: 4 additions & 8 deletions include/fintamath/functions/arithmetic/Add.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
#include "fintamath/functions/IFunction.hpp"

namespace fintamath {

class Add final : public IOperatorCRTP<IArithmetic, Add, IArithmetic, IArithmetic> {
class Add final : public IFunctionCRTP<IArithmetic, Add, IArithmetic> {
FINTAMATH_CLASS_BODY(Add)

public:
std::string toString() const override {
return "+";
return "add";
}

static constexpr bool isAssociativeStatic() {
static constexpr bool isVariadicStatic() {
return true;
}

static constexpr Priority getPriorityStatic() {
return Priority::Addition;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const override;
};
Expand Down
33 changes: 33 additions & 0 deletions include/fintamath/functions/arithmetic/AddOper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <memory>
#include <string>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IOperator.hpp"

namespace fintamath {

class AddOper final : public IOperatorCRTP<IArithmetic, AddOper, IArithmetic, IArithmetic> {
FINTAMATH_CLASS_BODY(AddOper)

public:
std::string toString() const override {
return "+";
}

static constexpr bool isAssociativeStatic() {
return true;
}

static constexpr Priority getPriorityStatic() {
return Priority::Addition;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const override;
};

}
12 changes: 4 additions & 8 deletions include/fintamath/functions/arithmetic/Mul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
#include "fintamath/functions/IFunction.hpp"

namespace fintamath {

class Mul final : public IOperatorCRTP<IArithmetic, Mul, IArithmetic, IArithmetic> {
class Mul final : public IFunctionCRTP<IArithmetic, Mul, IArithmetic> {
FINTAMATH_CLASS_BODY(Mul)

public:
std::string toString() const override {
return "*";
return "mul";
}

static constexpr bool isAssociativeStatic() {
static constexpr bool isVariadicStatic() {
return true;
}

static constexpr Priority getPriorityStatic() {
return Priority::Multiplication;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const override;
};
Expand Down
33 changes: 33 additions & 0 deletions include/fintamath/functions/arithmetic/MulOper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <memory>
#include <string>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IOperator.hpp"

namespace fintamath {

class MulOper final : public IOperatorCRTP<IArithmetic, MulOper, IArithmetic, IArithmetic> {
FINTAMATH_CLASS_BODY(MulOper)

public:
std::string toString() const override {
return "*";
}

static constexpr bool isAssociativeStatic() {
return true;
}

static constexpr Priority getPriorityStatic() {
return Priority::Multiplication;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const override;
};

}
11 changes: 4 additions & 7 deletions include/fintamath/functions/logic/And.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IFunction.hpp"
#include "fintamath/functions/IOperator.hpp"
#include "fintamath/literals/Boolean.hpp"

namespace fintamath {

class And final : public IOperatorCRTP<Boolean, And, Boolean, Boolean> {
class And final : public IFunctionCRTP<Boolean, And, Boolean> {
FINTAMATH_CLASS_BODY(And)

public:
std::string toString() const override {
return "&";
return "and";
}

static constexpr bool isAssociativeStatic() {
static constexpr bool isVariadicStatic() {
return true;
}

static constexpr Priority getPriorityStatic() {
return Priority::Conjunction;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const override;
};
Expand Down
33 changes: 33 additions & 0 deletions include/fintamath/functions/logic/AndOper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <memory>
#include <string>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IOperator.hpp"
#include "fintamath/literals/Boolean.hpp"

namespace fintamath {

class AndOper final : public IOperatorCRTP<Boolean, AndOper, Boolean, Boolean> {
FINTAMATH_CLASS_BODY(AndOper)

public:
std::string toString() const override {
return "&";
}

static constexpr bool isAssociativeStatic() {
return true;
}

static constexpr Priority getPriorityStatic() {
return Priority::Conjunction;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const override;
};

}
12 changes: 4 additions & 8 deletions include/fintamath/functions/logic/Or.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
#include "fintamath/functions/IFunction.hpp"
#include "fintamath/literals/Boolean.hpp"

namespace fintamath {

class Or final : public IOperatorCRTP<Boolean, Or, Boolean, Boolean> {
class Or final : public IFunctionCRTP<Boolean, Or, Boolean> {
FINTAMATH_CLASS_BODY(Or)

public:
std::string toString() const override {
return "|";
return "or";
}

static constexpr bool isAssociativeStatic() {
static constexpr bool isVariadicStatic() {
return true;
}

static constexpr Priority getPriorityStatic() {
return Priority::Disjunction;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argVect) const override;
};
Expand Down
Loading

0 comments on commit 9e2a499

Please sign in to comment.