Skip to content

Commit

Permalink
Add new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 23, 2024
1 parent a18b364 commit 26d43b6
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 96 deletions.
75 changes: 47 additions & 28 deletions include/fintamath/core/MathObjectType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,35 @@ struct MathObjectType final {

IFunction = 11000,

Add,
Sub,
Mul,
Div,
Neg,
Factorial,
Eqv,
Neqv,
Less,
More,
LessEqv,
MoreEqv,
Not,
And,
Or,
Impl,
Equiv,
Nequiv,
Mod,
Abs,
Log,
Ln,
Lb,
Pow,
Lg,
Exp,
Sqr,
Sqrt,
Root,
Log,
Ln,
Lb,
Lg,
Sin,
Cos,
Tan,
Expand Down Expand Up @@ -125,31 +144,31 @@ struct MathObjectType final {

IOperator = 12000,

Add,
Sub,
Mul,
Div,
Neg,
UnaryPlus,
Factorial,
Percent,
AddOper,
SubOper,
MulOper,
DivOper,
NegOper,
PlusOper,
FactorialOper,
PercentOper,
PowOper,
Eqv,
Neqv,
Less,
More,
LessEqv,
MoreEqv,
Not,
And,
Or,
Impl,
Equiv,
Nequiv,
Index,
Deg,
Comma,
Mod,
EqvOper,
NeqvOper,
LessOper,
MoreOper,
LessEqvOper,
MoreEqvOper,
NotOper,
AndOper,
OrOper,
ImplOper,
EquivOper,
NequivOper,
IndexOper,
DegOper,
CommaOper,
ModOper,

None = std::numeric_limits<size_t>::max(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace fintamath {

class UnaryPlus final : public IOperatorCRTP<IArithmetic, UnaryPlus, IArithmetic> {
class PlusOper final : public IOperatorCRTP<IArithmetic, PlusOper, IArithmetic> {
public:
std::string toString() const override {
return "+";
Expand All @@ -22,7 +22,7 @@ class UnaryPlus final : public IOperatorCRTP<IArithmetic, UnaryPlus, IArithmetic
}

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

protected:
Expand Down
4 changes: 1 addition & 3 deletions include/fintamath/functions/other/Comma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class Comma final : public IOperatorCRTP<IMathObject, Comma, IMathObject, IMathO
}

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

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

FINTAMATH_FUNCTION_EXPRESSION(Comma, commaExpr);

}
4 changes: 1 addition & 3 deletions include/fintamath/functions/other/Deg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ class Deg final : public IOperatorCRTP<INumber, Deg, INumber> {
}

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

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

FINTAMATH_FUNCTION_EXPRESSION(Deg, degExpr);

}
4 changes: 1 addition & 3 deletions include/fintamath/functions/other/Index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ class Index final : public IOperatorCRTP<Variable, Index, Variable, Integer> {
}

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

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

FINTAMATH_FUNCTION_EXPRESSION(Index, indexExpr);

}
4 changes: 1 addition & 3 deletions include/fintamath/functions/other/Percent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ class Percent final : public IOperatorCRTP<INumber, Percent, INumber> {
}

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

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

FINTAMATH_FUNCTION_EXPRESSION(Percent, percentExpr);

}
4 changes: 2 additions & 2 deletions src/fintamath/config/ExpressionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sign.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/functions/arithmetic/PlusOper.hpp"
#include "fintamath/functions/calculus/Derivative.hpp"
#include "fintamath/functions/calculus/Integral.hpp"
#include "fintamath/functions/calculus/Max.hpp"
Expand Down Expand Up @@ -259,7 +259,7 @@ void registerFunctionExpressionMakers() {
return mulExpr(negOne, std::move(args.front()));
});

Expression::registerFunctionExpressionMaker<UnaryPlus>([](ArgumentPtrVector args) {
Expression::registerFunctionExpressionMaker<PlusOper>([](ArgumentPtrVector args) {
return std::move(args.front())->clone();
});

Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/config/ParserConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sign.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/functions/arithmetic/PlusOper.hpp"
#include "fintamath/functions/calculus/Derivative.hpp"
#include "fintamath/functions/calculus/Integral.hpp"
#include "fintamath/functions/calculus/Max.hpp"
Expand Down Expand Up @@ -174,7 +174,7 @@ ParserConfig::ParserConfig() {
IOperator::registerType<Mul>();
IOperator::registerType<Div>();
IOperator::registerType<Neg>();
IOperator::registerType<UnaryPlus>();
IOperator::registerType<PlusOper>();
IOperator::registerType<Factorial>();
IOperator::registerType<Percent>();
IOperator::registerType<PowOper>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/functions/arithmetic/PlusOper.hpp"

#include <memory>

Expand All @@ -9,7 +9,7 @@

namespace fintamath {

std::unique_ptr<IMathObject> UnaryPlus::call(const ArgumentRefVector &argVect) const {
std::unique_ptr<IMathObject> PlusOper::call(const ArgumentRefVector &argVect) const {
const auto &rhs = cast<IArithmetic>(argVect.front().get());

return +rhs;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/IArithmeticTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ TEST(IArithmeticTests, divTest) {
EXPECT_EQ((-2 / Integer(2)).toString(), "-1");
}

TEST(IArithmeticTests, unaryPlusTest) {
TEST(IArithmeticTests, plusTest) {
const std::unique_ptr<IArithmetic> m1 = std::make_unique<Integer>(1);
EXPECT_EQ((+*m1)->toString(), "1");

Expand Down
6 changes: 3 additions & 3 deletions tests/src/core/ParseTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sign.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/functions/arithmetic/PlusOper.hpp"
#include "fintamath/functions/calculus/Derivative.hpp"
#include "fintamath/functions/calculus/Integral.hpp"
#include "fintamath/functions/comparison/Eqv.hpp"
Expand Down Expand Up @@ -254,9 +254,9 @@ TEST(ParseTests, parseFunctionTest) {

TEST(ParseTests, parseOperatorTest) {
EXPECT_TRUE(is<Add>(IOperator::parse("+", IOperator::Priority::Addition)));
EXPECT_TRUE(is<UnaryPlus>(IOperator::parse("+", IOperator::Priority::PrefixUnary)));
EXPECT_TRUE(is<PlusOper>(IOperator::parse("+", IOperator::Priority::PrefixUnary)));
EXPECT_TRUE(is<Add>(IOperator::parse("+", 2)));
EXPECT_TRUE(is<UnaryPlus>(IOperator::parse("+", 1)));
EXPECT_TRUE(is<PlusOper>(IOperator::parse("+", 1)));
EXPECT_TRUE(is<Sub>(IOperator::parse("-", IOperator::Priority::Addition)));
EXPECT_TRUE(is<Neg>(IOperator::parse("-", IOperator::Priority::PrefixUnary)));
EXPECT_TRUE(is<Sub>(IOperator::parse("-", 2)));
Expand Down
6 changes: 3 additions & 3 deletions tests/src/functions/IFunctionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/functions/arithmetic/PlusOper.hpp"
#include "fintamath/functions/trigonometry/Sin.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/numbers/Integer.hpp"
Expand Down Expand Up @@ -56,8 +56,8 @@ TEST(IFunctionTests, equalsTest) {
EXPECT_EQ(Add(), Add());
EXPECT_NE(Add(), Sub());
EXPECT_NE(Sub(), Add());
EXPECT_NE(Add(), UnaryPlus());
EXPECT_NE(UnaryPlus(), Add());
EXPECT_NE(Add(), PlusOper());
EXPECT_NE(PlusOper(), Add());
EXPECT_NE(Add(), Sin());
EXPECT_NE(Sin(), Add());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/functions/arithmetic/PlusOper.hpp"

#include "fintamath/literals/Variable.hpp"
#include "fintamath/numbers/Integer.hpp"
#include "fintamath/numbers/Rational.hpp"

using namespace fintamath;

using F = UnaryPlus;
using F = PlusOper;
const F f;

TEST(UnaryPlusTests, toStringTest) {
TEST(PlusOperTests, toStringTest) {
EXPECT_EQ(f.toString(), "+");
}

TEST(UnaryPlusTests, getArgumentTypesTest) {
TEST(PlusOperTests, getArgumentTypesTest) {
EXPECT_THAT(f.getArgumentTypes(), testing::ElementsAre(IArithmetic::getTypeStatic()));
}

TEST(UnaryPlusTests, getReturnTypeTest) {
TEST(PlusOperTests, getReturnTypeTest) {
EXPECT_EQ(f.getReturnType(), IArithmetic::getTypeStatic());
}

TEST(UnaryPlusTests, isVariadicTest) {
TEST(PlusOperTests, isVariadicTest) {
EXPECT_FALSE(F::isVariadicStatic());
EXPECT_FALSE(f.isVariadic());
}

TEST(UnaryPlusTests, isEvaluatableTest) {
TEST(PlusOperTests, isEvaluatableTest) {
EXPECT_TRUE(F::isEvaluatableStatic());
EXPECT_TRUE(f.isEvaluatable());
}

TEST(UnaryPlusTests, getPriorityTest) {
TEST(PlusOperTests, getPriorityTest) {
EXPECT_EQ(F::getPriorityStatic(), IOperator::Priority::PrefixUnary);
EXPECT_EQ(f.getPriority(), IOperator::Priority::PrefixUnary);
}

TEST(UnaryPlusTests, isAssociativeTest) {
TEST(PlusOperTests, isAssociativeTest) {
EXPECT_FALSE(F::isAssociativeStatic());
EXPECT_FALSE(f.isAssociative());
}

TEST(UnaryPlusTests, callTest) {
TEST(PlusOperTests, callTest) {
EXPECT_EQ(f(Integer(3))->toString(), "3");
EXPECT_EQ(f(Rational(5, 2))->toString(), "5/2");
EXPECT_EQ(f(Rational(-5, 2))->toString(), "-5/2");
Expand All @@ -56,7 +56,7 @@ TEST(UnaryPlusTests, callTest) {
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
}

TEST(UnaryPlusTests, getTypeTest) {
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::UnaryPlus, "UnaryPlus"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::UnaryPlus, "UnaryPlus"));
TEST(PlusOperTests, getTypeTest) {
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::PlusOper, "PlusOper"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::PlusOper, "PlusOper"));
}
8 changes: 2 additions & 6 deletions tests/src/functions/other/CommaTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ TEST(CommaTests, callTest) {
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
}

TEST(CommaTests, exprTest) {
EXPECT_EQ(commaExpr(Variable("a"), Integer(1))->toString(), "a , 1");
}

TEST(CommaTests, getTypeTest) {
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::Comma, "Comma"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::Comma, "Comma"));
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::CommaOper, "CommaOper"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::CommaOper, "CommaOper"));
}
8 changes: 2 additions & 6 deletions tests/src/functions/other/DegTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ TEST(DegTests, callTest) {
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
}

TEST(DegTests, degTest) {
EXPECT_EQ(degExpr(Integer(10))->toString(), "10 Pi/180");
}

TEST(DegTests, getTypeTest) {
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::Deg, "Deg"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::Deg, "Deg"));
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::DegOper, "DegOper"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::DegOper, "DegOper"));
}
8 changes: 2 additions & 6 deletions tests/src/functions/other/IndexTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ TEST(IndexTests, callTest) {
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
}

TEST(IndexTests, exprTest) {
EXPECT_EQ(indexExpr(Variable("a"), Integer(1))->toString(), "a_1");
}

TEST(IndexTests, getTypeTest) {
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::Index, "Index"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::Index, "Index"));
EXPECT_EQ(F::getTypeStatic(), MathObjectType(MathObjectType::IndexOper, "IndexOper"));
EXPECT_EQ(f.getType(), MathObjectType(MathObjectType::IndexOper, "IndexOper"));
}
Loading

0 comments on commit 26d43b6

Please sign in to comment.