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

Factorial of Real numbers #135

Merged
merged 1 commit into from
Aug 1, 2023
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
17 changes: 17 additions & 0 deletions include/fintamath/functions/other/Factorial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@

namespace fintamath {

class Integer;
class Rational;
class Real;

class Factorial : public IOperatorCRTP<INumber, Factorial, INumber> {
public:
Factorial() : IOperatorCRTP(IOperator::Priority::PostfixUnary) {
}

Factorial(size_t inOrder) : Factorial() {
setOrder(inOrder);
}

std::string toString() const override {
return std::string(order, '!');
}
Expand All @@ -28,6 +36,15 @@ class Factorial : public IOperatorCRTP<INumber, Factorial, INumber> {
protected:
std::unique_ptr<IMathObject> call(const ArgumentsRefVector &argsVect) const override;

private:
static std::unique_ptr<IMathObject> multiFactorialSimpl(const INumber &lhs, size_t order);

static std::unique_ptr<IMathObject> factorialSimpl(const Integer &rhs, size_t order);

static std::unique_ptr<IMathObject> factorialSimpl(const Rational &rhs, size_t order);

static std::unique_ptr<IMathObject> factorialSimpl(const Real &rhs, size_t order);

private:
size_t order = 1;
};
Expand Down
4 changes: 4 additions & 0 deletions include/fintamath/numbers/Integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Integer : public IIntegerCRTP<Integer> {

explicit Integer(std::string str);

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
explicit Integer(T val) : backend(val) {
}

Integer(int64_t val);

std::string toString() const override;
Expand Down
2 changes: 2 additions & 0 deletions include/fintamath/numbers/RealFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ Real getE();

Real getPi();

Real tgamma(const Real &rhs);

}
31 changes: 9 additions & 22 deletions src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ std::string functionToString(const IFunction &func, const ArgumentsPtrVector &ar
return result;
}

std::string binaryOperatorChildToString(const IOperator &oper, const ArgumentPtr &child) {
std::string operatorChildToString(const IOperator &oper, const ArgumentPtr &child) {
std::string childStr = child->toString();
std::shared_ptr<IOperator> childOper;

if (const auto childExpr = cast<IExpression>(child)) {
childOper = cast<IOperator>(childExpr->getOutputFunction());
}
else if (oper.getFunctionType() == IFunction::Type::Unary && childStr.front() == Neg().toString().front()) {
childOper = std::make_shared<Neg>();
}
else if (is<Rational>(child)) {
childOper = std::make_shared<Div>();
}
Expand All @@ -58,7 +61,7 @@ std::string binaryOperatorChildToString(const IOperator &oper, const ArgumentPtr
return putInBrackets(childStr);
}

if (lhsOperPriority > operPriority || (lhsOperPriority == operPriority && !oper.isAssociative())) {
if (lhsOperPriority >= operPriority || (lhsOperPriority == operPriority && !oper.isAssociative())) {
return putInBrackets(childStr);
}
}
Expand All @@ -76,34 +79,18 @@ std::string binaryOperatorToString(const IOperator &oper, const ArgumentPtr &lhs
operStr = ' ' + operStr + ' ';
}

std::string lhsStr = binaryOperatorChildToString(oper, lhs);
std::string rhsStr = binaryOperatorChildToString(oper, rhs);
std::string lhsStr = operatorChildToString(oper, lhs);
std::string rhsStr = operatorChildToString(oper, rhs);

return lhsStr + operStr + rhsStr;
}

std::string prefixUnaryOperatorToString(const IOperator &oper, const ArgumentPtr &rhs) {
std::string result = oper.toString();

if (const auto child = cast<IExpression>(rhs)) {
if (is<IOperator>(child->getOutputFunction())) {
return result + putInBrackets(rhs->toString());
}
}

return result + rhs->toString();
return oper.toString() + operatorChildToString(oper, rhs);
}

std::string postfixUnaryOperatorToString(const IOperator &oper, const ArgumentPtr &rhs) {
std::string result = rhs->toString();

if (const auto child = cast<IExpression>(rhs)) {
if (is<IOperator>(child->getOutputFunction())) {
return putInBrackets(result) + oper.toString();
}
}

return result + oper.toString();
return operatorChildToString(oper, rhs) + oper.toString();
}

bool hasVariables(const std::shared_ptr<const IExpression> &expr) {
Expand Down
67 changes: 62 additions & 5 deletions src/fintamath/functions/other/Factorial.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,75 @@
#include "fintamath/functions/other/Factorial.hpp"

#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/numbers/IntegerFunctions.hpp"
#include "fintamath/numbers/Rational.hpp"
#include "fintamath/numbers/Real.hpp"
#include "fintamath/numbers/RealFunctions.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> Factorial::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = argsVect.front().get();
const auto &rhs = cast<INumber>(argsVect.front().get());
return multiFactorialSimpl(rhs, order);
}

std::unique_ptr<IMathObject> Factorial::multiFactorialSimpl(const INumber &lhs, size_t order) {
static const auto multiFactorial = [] {
static MultiMethod<std::unique_ptr<IMathObject>(const INumber &, const Integer &)> outMultiAbs;

outMultiAbs.add<Integer, Integer>([](const Integer &inRhs, const Integer &inOrder) {
return factorialSimpl(inRhs, size_t(inOrder));
});

outMultiAbs.add<Rational, Integer>([](const Rational &inRhs, const Integer &inOrder) {
return factorialSimpl(inRhs, size_t(inOrder));
});

outMultiAbs.add<Real, Integer>([](const Real &inRhs, const Integer &inOrder) {
return factorialSimpl(inRhs, size_t(inOrder));
});

return outMultiAbs;
}();

if (!is<Integer>(rhs)) {
throw UndefinedUnaryOperatorException(toString(), rhs.toString(), UndefinedUnaryOperatorException::Type::Postfix);
return multiFactorial(lhs, Integer(order));
}

std::unique_ptr<IMathObject> Factorial::factorialSimpl(const Integer &rhs, size_t order) {
if (rhs < 0) {
if (order != 1) {
return makeExpr(Factorial(order), rhs);
}

return ComplexInf().clone();
}

return factorial(cast<Integer>(rhs), order).toMinimalObject();
return factorial(rhs, order).toMinimalObject();
}

std::unique_ptr<IMathObject> Factorial::factorialSimpl(const Rational &rhs, size_t order) {
if (rhs.denominator() == 1) {
return factorialSimpl(rhs.numerator(), order);
}

if (order != 1) {
return makeExpr(Factorial(order), rhs);
}

return factorialSimpl(Real(rhs), order);
}

std::unique_ptr<IMathObject> Factorial::factorialSimpl(const Real &rhs, size_t order) {
if (order != 1) {
return makeExpr(Factorial(order), rhs);
}

try {
return tgamma(rhs + 1).toMinimalObject();
}
catch (const UndefinedException &) {
return makeExpr(Factorial(order), rhs);
}
}

}
12 changes: 12 additions & 0 deletions src/fintamath/numbers/RealFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,16 @@ Real getPi() {
return {cpp_dec_float_100(get_constant_pi<cpp_dec_float_100::backend_type>())};
}

Real tgamma(const Real &rhs) {
try {
return boost::math::tgamma(rhs.getBackend());
}
catch (const std::domain_error &) {
throw UndefinedFunctionException("tgamma", {rhs.toString()});
}
catch (const std::overflow_error &) {
throw UndefinedFunctionException("tgamma", {rhs.toString()});
}
}

}
2 changes: 1 addition & 1 deletion tests/src/FintamathTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST(FintamathTests, fintamathTests) {
expr = Expression("(4x^4 + 1 + 3x^3 + 2x) / (x^2 + x + 2)");
EXPECT_EQ(expr.toString(), "4 x^2 - x - 7 + (11 x + 15)/(x^2 + x + 2)");

expr = log(2, 256) + ln(pow(E(), 2));
expr = log(2, 256) + ln(pow(e(), 2));
EXPECT_EQ(expr.toString(), "10");

expr = sqrt(Expression(8));
Expand Down
20 changes: 15 additions & 5 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("30!!!!!!").toString(), "933120");
EXPECT_EQ(Expression("30!!!!!!!").toString(), "198720");

EXPECT_EQ(Expression("(2/3)!").toString(), "(2/3)!");
EXPECT_EQ(Expression("E!").toString(), "E!");
EXPECT_EQ(Expression("(2/3)!!").toString(), "(2/3)!!");
EXPECT_EQ(Expression("(-1)!!").toString(), "(-1)!!");

EXPECT_EQ(Expression("sqrt144").toString(), "12");
EXPECT_EQ(Expression("sqrt0").toString(), "0");
EXPECT_EQ(Expression("sqrt(25)").toString(), "5");
Expand Down Expand Up @@ -738,6 +743,7 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("log(1, 10)").toString(), "ComplexInf");
EXPECT_EQ(Expression("log(10, 0)").toString(), "-Inf");
EXPECT_EQ(Expression("log(1/10, 0)").toString(), "Inf");
EXPECT_EQ(Expression("(-1)!").toString(), "ComplexInf");

EXPECT_EQ(Expression("0*Inf").toString(), "Undefined");
EXPECT_EQ(Expression("0*-Inf").toString(), "Undefined");
Expand Down Expand Up @@ -1064,10 +1070,6 @@ TEST(ExpressionTests, stringConstructorNegativeTest) {
EXPECT_THROW(Expression("max()"), InvalidInputException);
EXPECT_THROW(Expression("max(True, False)"), InvalidInputException);

EXPECT_THROW(Expression("(-1)!"), UndefinedException);
EXPECT_THROW(Expression("(2/3)!"), UndefinedException);
EXPECT_THROW(Expression("(-1)!!"), UndefinedException);
EXPECT_THROW(Expression("(2/3)!!"), UndefinedException);
EXPECT_THROW(Expression("sqrt(-1)"), UndefinedException);
EXPECT_THROW(Expression("sqrt(-1)"), UndefinedException);
EXPECT_THROW(Expression("(-1)^(2/3)"), UndefinedException);
Expand All @@ -1078,7 +1080,6 @@ TEST(ExpressionTests, stringConstructorNegativeTest) {
EXPECT_THROW(Expression("lg(-1)"), UndefinedException);
EXPECT_THROW(Expression("log(-1, 1)"), UndefinedException);
// TODO constants
// EXPECT_THROW(Expression("E!"), UndefinedException);
// EXPECT_THROW(Expression("tan(Pi/2)"), UndefinedException);
// EXPECT_THROW(Expression("cot(0)"), UndefinedException);
// EXPECT_THROW(Expression("asin(2)"), UndefinedException);
Expand Down Expand Up @@ -1186,6 +1187,15 @@ TEST(ExpressionTests, preciseTest) {
EXPECT_EQ(Expression("((x - z)^2 / 8) * (x / y)").precise().toString(),
"(0.125 x^3)/y + (-0.25 x^2 z)/y + (0.125 x z^2)/y");

EXPECT_EQ(Expression("(2/3)!").precise().toString(),
"0.90274529295093361129685868543634252367955151070452913226268164530918864360116169");
EXPECT_EQ(Expression("E!").precise().toString(),
"4.2608204763570033817001212246457024649334243739593219749116048935993443487275001");
EXPECT_EQ(Expression("(2/3)!!").precise().toString(),
"0.66666666666666666666666666666666666666666666666666666666666666666666666666666667!!");
EXPECT_EQ(Expression("(1/1000000000000000000000000000000000000000)!!").precise().toString(), "(1*10^-39)!!");
EXPECT_EQ(Expression("(-1)!!").precise().toString(), "(-1)!!");

EXPECT_EQ(Expression("ln(x)").precise().toString(), "ln(x)");
EXPECT_EQ(Expression("sqrt(x)").precise().toString(), "sqrt(x)");
EXPECT_EQ(Expression("root(x, 3)").precise().toString(), "root(x, 3)");
Expand Down
Loading