Skip to content

Commit

Permalink
Implement Frac function
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 18, 2023
1 parent 8fe319b commit f59af8d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/fintamath/core/MathObjectTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ enum class MathObjectType : MathObjectTypeId {
Equiv,
Nequiv,
Index,
Frac,

None = std::numeric_limits<size_t>::max(),
};
Expand Down
22 changes: 22 additions & 0 deletions include/fintamath/functions/arithmetic/Frac.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/functions/IFunction.hpp"

namespace fintamath {

class Frac : public IFunctionCRTP<IArithmetic, Frac, IArithmetic, IArithmetic> {
public:
std::string toString() const override {
return "frac";
}

static MathObjectTypeId getTypeIdStatic() {
return MathObjectTypeId(MathObjectType::Frac);
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentsRefVector &argsVect) const override;
};

}
5 changes: 5 additions & 0 deletions src/fintamath/config/ExpressionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "fintamath/expressions/unary/TrigonometryExpression.hpp"
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Frac.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
Expand Down Expand Up @@ -151,6 +152,10 @@ struct ExpressionConfig {
return std::make_unique<DivExpression>(args.front(), args.back());
});

Expression::registerFunctionExpressionMaker<Frac>([](const ArgumentsPtrVector &args) {
return std::make_unique<DivExpression>(args.front(), args.back());
});

Expression::registerFunctionExpressionMaker<And, true>([](const ArgumentsPtrVector &args) {
return std::make_unique<AndExpression>(args);
});
Expand Down
2 changes: 2 additions & 0 deletions src/fintamath/config/ParserConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "fintamath/functions/arithmetic/Abs.hpp"
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Frac.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sign.hpp"
Expand Down Expand Up @@ -194,6 +195,7 @@ struct ParserConfig {
IFunction::registerType<Max>();
IFunction::registerType<Derivative>();
IFunction::registerType<Integral>();
IFunction::registerType<Frac>();

IOperator::registerType<Add>();
IOperator::registerType<Sub>();
Expand Down
11 changes: 11 additions & 0 deletions src/fintamath/functions/arithmetic/Frac.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "fintamath/functions/arithmetic/Frac.hpp"

#include "fintamath/functions/arithmetic/Div.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> Frac::call(const ArgumentsRefVector &argsVect) const {
return Div()(argsVect);
}

}
2 changes: 2 additions & 0 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("sign(10)").toString(), "1");
EXPECT_EQ(Expression("sign(0)").toString(), "0");
EXPECT_EQ(Expression("sign(-5)").toString(), "-1");
EXPECT_EQ(Expression("frac(2,4)").toString(), "1/2");
EXPECT_EQ(Expression("frac(x,y)").toString(), "x/y");

EXPECT_EQ(Expression("a*0").toString(), "0");
EXPECT_EQ(Expression("0*a").toString(), "0");
Expand Down
62 changes: 62 additions & 0 deletions tests/src/functions/arithmetic/FracTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "gtest/gtest.h"

#include "fintamath/functions/arithmetic/Add.hpp"

#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/functions/arithmetic/Frac.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/numbers/Rational.hpp"

using namespace fintamath;

const Frac f;

TEST(FracTests, toStringTest) {
EXPECT_EQ(f.toString(), "frac");
}

TEST(FracTests, getFunctionTypeTest) {
EXPECT_EQ(f.getFunctionType(), IFunction::Type::Binary);
}

TEST(FracTests, callTest) {
EXPECT_EQ(f(Integer(3), Integer(5))->toString(), "3/5");
EXPECT_EQ(f(Integer(3), Rational(5, 2))->toString(), "6/5");
EXPECT_EQ(f(Rational(5, 2), Integer(3))->toString(), "5/6");
EXPECT_EQ(f(Rational(5, 2), Rational(5, 3))->toString(), "3/2");

EXPECT_EQ(f(Integer(3), Variable("a"))->toString(), "3/a");

EXPECT_THROW(f(Integer(1)), InvalidInputFunctionException);
EXPECT_THROW(f(Rational(2, 3)), InvalidInputFunctionException);
EXPECT_THROW(f(), InvalidInputFunctionException);
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
}

TEST(FracTests, doArgsMatchTest) {
Integer a;

EXPECT_FALSE(f.doArgsMatch({}));
EXPECT_FALSE(f.doArgsMatch({a}));
EXPECT_TRUE(f.doArgsMatch({a, a}));
EXPECT_FALSE(f.doArgsMatch({a, a, a}));
}

TEST(FracTests, equalsTest) {
EXPECT_EQ(f, f);
EXPECT_EQ(f, Frac());
EXPECT_EQ(Frac(), f);
EXPECT_EQ(f, cast<IMathObject>(Frac()));
EXPECT_EQ(cast<IMathObject>(Frac()), f);
EXPECT_NE(f, Sub());
EXPECT_NE(Sub(), f);
EXPECT_NE(f, UnaryPlus());
EXPECT_NE(UnaryPlus(), f);
}

TEST(FracTests, getTypeIdTest) {
EXPECT_EQ(Frac::getTypeIdStatic(), MathObjectTypeId(MathObjectType::Frac));
EXPECT_EQ(Frac().getTypeId(), MathObjectTypeId(MathObjectType::Frac));
}
2 changes: 2 additions & 0 deletions tests/src/parser/ParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "fintamath/functions/arithmetic/Abs.hpp"
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Frac.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sign.hpp"
Expand Down Expand Up @@ -215,6 +216,7 @@ TEST(ParserTests, parseFunctionTest) {
EXPECT_TRUE(is<Acoth>(IFunction::parse("acoth")));
EXPECT_TRUE(is<Derivative>(IFunction::parse("derivative")));
EXPECT_TRUE(is<Integral>(IFunction::parse("integral")));
EXPECT_TRUE(is<Frac>(IFunction::parse("frac")));

EXPECT_TRUE(is<Add>(IFunction::parse("+", IFunction::Type::Binary)));
EXPECT_TRUE(is<UnaryPlus>(IFunction::parse("+", IFunction::Type::Unary)));
Expand Down

0 comments on commit f59af8d

Please sign in to comment.