-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
198 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,6 @@ Real getE(); | |
|
||
Real getPi(); | ||
|
||
Real tgamma(const Real &rhs); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ComplexInf().clone(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters