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 Mar 15, 2024
1 parent 1879e2c commit c971777
Show file tree
Hide file tree
Showing 62 changed files with 912 additions and 482 deletions.
29 changes: 16 additions & 13 deletions include/fintamath/core/MathObjectType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,37 +119,40 @@ struct MathObjectType final {
Integral,
Frac,
FracMixed,
PowFunction,
Floor,
Ceil,
Add,
Mul,
Pow,
And,
Or,

IOperator = 12000,

Add,
AddOper,
UnaryPlus,
Sub,
Mul,
Div,
Neg,
UnaryPlus,
MulOper,
Div,
Mod,
PowOper,
Factorial,
Percent,
Pow,
Deg,
Index,
Comma,
Eqv,
Neqv,
Less,
More,
LessEqv,
MoreEqv,
Not,
And,
Or,
AndOper,
OrOper,
Impl,
Equiv,
Nequiv,
Index,
Deg,
Comma,
Mod,

None = std::numeric_limits<size_t>::max(),
};
Expand Down
9 changes: 2 additions & 7 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Expression final : public IExpressionCRTP<Expression> {
getTermParser().registerType(std::move(constructor));
}

template <typename Function, bool isPolynomial = false>
template <typename Function>
static void registerFunctionExpressionMaker(ExpressionMaker maker);

static constexpr MathObjectType getTypeStatic() {
Expand Down Expand Up @@ -195,7 +195,7 @@ Expression operator/(const Expression &lhs, const Variable &rhs);

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

template <typename Function, bool isPolynomial>
template <typename Function>
void Expression::registerFunctionExpressionMaker(ExpressionMaker maker) {
ExpressionParser::Constructor constructor = [maker = std::move(maker)](ArgumentPtrVector &&args) {
static const size_t argSize = Function{}.getArgumentTypes().size();
Expand All @@ -204,11 +204,6 @@ void Expression::registerFunctionExpressionMaker(ExpressionMaker maker) {
if constexpr (Function::isVariadicStatic()) {
res = maker(std::move(args));
}
else if constexpr (isPolynomial) {
if (argSize <= args.size()) {
res = maker(std::move(args));
}
}
else {
if (argSize == args.size()) {
res = maker(std::move(args));
Expand Down
2 changes: 2 additions & 0 deletions include/fintamath/expressions/ExpressionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ std::pair<ArgumentPtr, ArgumentPtr> splitRational(const ArgumentPtr &arg);

ArgumentPtr negate(const ArgumentPtr &arg);

ArgumentPtr invert(const ArgumentPtr &arg);

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

ArgumentPtr makePolynom(const IFunction &func, const ArgumentPtrVector &args);
Expand Down
6 changes: 6 additions & 0 deletions include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class IExpression : public IArithmetic {
}

protected:
virtual bool isTermOrderInversed() const;

virtual bool isComparableOrderInversed() const;

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

virtual ArgumentPtr simplify() const;

virtual ArgumentPtr preSimplify() const;
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 @@ -43,16 +42,10 @@ 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;

virtual bool isTermOrderInversed() const;

virtual bool isComparableOrderInversed() const;

private:
void simplifyRec(bool isPostSimplify);

Expand Down
19 changes: 12 additions & 7 deletions include/fintamath/functions/IFunctionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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() != argTypes.size()) {
Expand Down Expand Up @@ -75,27 +75,32 @@ 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) {
using ExpectedArg = typename std::tuple_element_t<0, std::tuple<Args...>>;

return stdr::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) && !detail::isExpression(arg);
}

private:
inline static const ArgumentTypeVector argTypes = {Args::getTypeStatic()...};

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,24 +8,20 @@
#include "fintamath/core/MathObjectType.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> {
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;
}

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::Add, "Add"};
}
Expand Down
36 changes: 36 additions & 0 deletions include/fintamath/functions/arithmetic/AddOper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <memory>
#include <string>

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

namespace fintamath {

class AddOper final : public IOperatorCRTP<IArithmetic, AddOper, IArithmetic, IArithmetic> {
public:
std::string toString() const override {
return "+";
}

static constexpr bool isAssociativeStatic() {
return true;
}

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

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::AddOper, "AddOper"};
}

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,24 +8,20 @@
#include "fintamath/core/MathObjectType.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> {
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;
}

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::Mul, "Mul"};
}
Expand Down
36 changes: 36 additions & 0 deletions include/fintamath/functions/arithmetic/MulOper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <memory>
#include <string>

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

namespace fintamath {

class MulOper final : public IOperatorCRTP<IArithmetic, MulOper, IArithmetic, IArithmetic> {
public:
std::string toString() const override {
return "*";
}

static constexpr bool isAssociativeStatic() {
return true;
}

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

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::MulOper, "MulOper"};
}

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,25 +7,22 @@
#include "fintamath/core/MathObjectType.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> {
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;
}

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::And, "And"};
}
Expand Down
36 changes: 36 additions & 0 deletions include/fintamath/functions/logic/AndOper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <memory>
#include <string>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectType.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> {
public:
std::string toString() const override {
return "&";
}

static constexpr bool isAssociativeStatic() {
return true;
}

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

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::AndOper, "AndOper"};
}

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

}
Loading

0 comments on commit c971777

Please sign in to comment.