Skip to content

Commit

Permalink
Simplify constants in hyperbolic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Aug 8, 2023
1 parent 193a0e3 commit cb15640
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 22 deletions.
4 changes: 4 additions & 0 deletions include/fintamath/functions/hyperbolic/Acosh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace fintamath {

class Real;

class Acosh : public IFunctionCRTP<INumber, Acosh, INumber> {
public:
Acosh() = default;
Expand All @@ -22,6 +24,8 @@ class Acosh : public IFunctionCRTP<INumber, Acosh, INumber> {

private:
static std::unique_ptr<IMathObject> multiAcoshSimpl(const INumber &rhs);

static std::unique_ptr<IMathObject> acoshSimpl(const Real &rhs);
};

}
4 changes: 4 additions & 0 deletions include/fintamath/functions/hyperbolic/Acoth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace fintamath {

class Real;

class Acoth : public IFunctionCRTP<INumber, Acoth, INumber> {
public:
Acoth() = default;
Expand All @@ -22,6 +24,8 @@ class Acoth : public IFunctionCRTP<INumber, Acoth, INumber> {

private:
static std::unique_ptr<IMathObject> multiAcothSimpl(const INumber &rhs);

static std::unique_ptr<IMathObject> acothSimpl(const Real &rhs);
};

}
4 changes: 4 additions & 0 deletions include/fintamath/functions/hyperbolic/Atanh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace fintamath {

class Real;

class Atanh : public IFunctionCRTP<INumber, Atanh, INumber> {
public:
Atanh() = default;
Expand All @@ -22,6 +24,8 @@ class Atanh : public IFunctionCRTP<INumber, Atanh, INumber> {

private:
static std::unique_ptr<IMathObject> multiAtanhSimpl(const INumber &rhs);

static std::unique_ptr<IMathObject> atanhSimpl(const Real &rhs);
};

}
26 changes: 25 additions & 1 deletion src/fintamath/functions/hyperbolic/Acosh.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#include "fintamath/functions/hyperbolic/Acosh.hpp"

#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/numbers/RealFunctions.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> Acosh::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

// TODO! uncomment
// if (rhs == Integer(-1)) {
// return I Pi;
// }

// TODO! uncomment
// if (rhs == Integer(0)) {
// return I Pi / 2;
// }

if (rhs == Integer(1)) {
return Integer(0).clone();
}

return multiAcoshSimpl(rhs);
}

Expand All @@ -23,7 +38,7 @@ std::unique_ptr<IMathObject> Acosh::multiAcoshSimpl(const INumber &rhs) {
});

outMultiAcosh.add<Real>([](const Real &inRhs) {
return acosh(inRhs).toMinimalObject();
return acoshSimpl(inRhs);
});

return outMultiAcosh;
Expand All @@ -32,4 +47,13 @@ std::unique_ptr<IMathObject> Acosh::multiAcoshSimpl(const INumber &rhs) {
return multiAcosh(rhs);
}

std::unique_ptr<IMathObject> Acosh::acoshSimpl(const Real &rhs) {
try {
return acosh(rhs).toMinimalObject();
}
catch (const UndefinedException &) {
return {};
}
}

}
27 changes: 26 additions & 1 deletion src/fintamath/functions/hyperbolic/Acoth.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#include "fintamath/functions/hyperbolic/Acoth.hpp"

#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"
#include "fintamath/numbers/RealFunctions.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> Acoth::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

if (rhs == Integer(-1)) {
return NegInf().clone();
}

// TODO! uncomment
// if (rhs == Integer(0)) {
// return I Pi / 2;
// }

if (rhs == Integer(1)) {
return Inf().clone();
}

return multiAcothSimpl(rhs);
}

Expand All @@ -23,7 +39,7 @@ std::unique_ptr<IMathObject> Acoth::multiAcothSimpl(const INumber &rhs) {
});

outMultiAcoth.add<Real>([](const Real &inRhs) {
return acoth(inRhs).toMinimalObject();
return acothSimpl(inRhs);
});

return outMultiAcoth;
Expand All @@ -32,4 +48,13 @@ std::unique_ptr<IMathObject> Acoth::multiAcothSimpl(const INumber &rhs) {
return multiAcoth(rhs);
}

std::unique_ptr<IMathObject> Acoth::acothSimpl(const Real &rhs) {
try {
return acoth(rhs).toMinimalObject();
}
catch (const UndefinedException &) {
return {};
}
}

}
4 changes: 4 additions & 0 deletions src/fintamath/functions/hyperbolic/Asinh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace fintamath {
std::unique_ptr<IMathObject> Asinh::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

if (rhs == Integer(0)) {
return rhs.clone();
}

return multiAsinhSimpl(rhs);
}

Expand Down
26 changes: 25 additions & 1 deletion src/fintamath/functions/hyperbolic/Atanh.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#include "fintamath/functions/hyperbolic/Atanh.hpp"

#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"
#include "fintamath/numbers/RealFunctions.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> Atanh::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

if (rhs == Integer(-1)) {
return NegInf().clone();
}

if (rhs == Integer(0)) {
return rhs.clone();
}

if (rhs == Integer(1)) {
return Inf().clone();
}

return multiAtanhSimpl(rhs);
}

Expand All @@ -23,7 +38,7 @@ std::unique_ptr<IMathObject> Atanh::multiAtanhSimpl(const INumber &rhs) {
});

outMultiAtanh.add<Real>([](const Real &inRhs) {
return atanh(inRhs).toMinimalObject();
return atanhSimpl(inRhs);
});

return outMultiAtanh;
Expand All @@ -32,4 +47,13 @@ std::unique_ptr<IMathObject> Atanh::multiAtanhSimpl(const INumber &rhs) {
return multiAtanh(rhs);
}

std::unique_ptr<IMathObject> Atanh::atanhSimpl(const Real &rhs) {
try {
return atanh(rhs).toMinimalObject();
}
catch (const UndefinedException &) {
return {};
}
}

}
4 changes: 4 additions & 0 deletions src/fintamath/functions/hyperbolic/Cosh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace fintamath {
std::unique_ptr<IMathObject> Cosh::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

if (rhs == Integer(0)) {
return Integer(1).clone();
}

return multiCoshSimpl(rhs);
}

Expand Down
5 changes: 5 additions & 0 deletions src/fintamath/functions/hyperbolic/Coth.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "fintamath/functions/hyperbolic/Coth.hpp"

#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/numbers/RealFunctions.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> Coth::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

if (rhs == Integer(0)) {
return ComplexInf().clone();
}

return multiCothSimpl(rhs);
}

Expand Down
4 changes: 4 additions & 0 deletions src/fintamath/functions/hyperbolic/Sinh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace fintamath {
std::unique_ptr<IMathObject> Sinh::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

if (rhs == Integer(0)) {
return rhs.clone();
}

return multiSinhSimpl(rhs);
}

Expand Down
4 changes: 4 additions & 0 deletions src/fintamath/functions/hyperbolic/Tanh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace fintamath {
std::unique_ptr<IMathObject> Tanh::call(const ArgumentsRefVector &argsVect) const {
const auto &rhs = cast<INumber>(argsVect.front().get());

if (rhs == Integer(0)) {
return rhs.clone();
}

return multiTanhSimpl(rhs);
}

Expand Down
11 changes: 8 additions & 3 deletions tests/src/functions/hyperbolic/AcoshTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/numbers/Rational.hpp"
#include "fintamath/numbers/Real.hpp"

using namespace fintamath;

Expand All @@ -21,17 +22,21 @@ TEST(AcoshTests, getFunctionTypeTest) {
}

TEST(AcoshTests, callTest) {
EXPECT_EQ(f(Integer(1))->toString(), "0.0");
EXPECT_EQ(f(Integer(-1))->toString(), "acosh(-1)");
EXPECT_EQ(f(Integer(0))->toString(), "acosh(0)");
EXPECT_EQ(f(Integer(1))->toString(), "0");
EXPECT_EQ(f(Integer(2))->toString(),
"1.316957896924816708625046347307968444026981971467516479768472256920460185416444");

EXPECT_EQ(f(Rational(11, 10))->toString(),
"0.44356825438511518913291106635249808664900116609997546389342095580766881611303472");
EXPECT_EQ(f(Rational(23, 13))->toString(),
"1.1720930632462197632831227513306924192738346732428024686924646851189700836483782");

EXPECT_EQ(f(Variable("a"))->toString(), "acosh(a)");
EXPECT_EQ(f(Real("1.5"))->toString(),
"0.96242365011920689499551782684873684627036866877132103932203633768032773521644355");

EXPECT_THROW(f(Integer(0)), UndefinedFunctionException);
EXPECT_EQ(f(Variable("a"))->toString(), "acosh(a)");

EXPECT_THROW(f(), InvalidInputFunctionException);
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
Expand Down
12 changes: 8 additions & 4 deletions tests/src/functions/hyperbolic/AcothTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/numbers/Rational.hpp"
#include "fintamath/numbers/Real.hpp"

using namespace fintamath;

Expand All @@ -21,18 +22,21 @@ TEST(AcothTests, getFunctionTypeTest) {
}

TEST(AcothTests, callTest) {
EXPECT_EQ(f(Integer(-1))->toString(), "-Inf");
EXPECT_EQ(f(Integer(0))->toString(), "acoth(0)");
EXPECT_EQ(f(Integer(1))->toString(), "Inf");
EXPECT_EQ(f(Integer(2))->toString(),
"0.54930614433405484569762261846126285232374527891137472586734716681874714660930448");
EXPECT_EQ(f(Integer(-2))->toString(),
"-0.54930614433405484569762261846126285232374527891137472586734716681874714660930448");

EXPECT_EQ(f(Rational(27, 10))->toString(),
"0.38885228429400418205928052657916031415593728729094568817383747165534858559471489");
EXPECT_EQ(f(Rational(23, 13))->toString(),
"0.64046692273103215880348163103852016892243994786861821783871039264710161034962744");

EXPECT_EQ(f(Variable("a"))->toString(), "acoth(a)");
EXPECT_EQ(f(Real("1.5"))->toString(),
"0.80471895621705018730037966661309381976280067713425886095632394573708949385382888");

EXPECT_THROW(f(Integer(0)), UndefinedFunctionException);
EXPECT_EQ(f(Variable("a"))->toString(), "acoth(a)");

EXPECT_THROW(f(), InvalidInputFunctionException);
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
Expand Down
7 changes: 6 additions & 1 deletion tests/src/functions/hyperbolic/AsinhTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/numbers/Rational.hpp"
#include "fintamath/numbers/Real.hpp"

using namespace fintamath;

Expand All @@ -21,14 +22,18 @@ TEST(AsinhTests, getFunctionTypeTest) {
}

TEST(AsinhTests, callTest) {
EXPECT_EQ(f(Integer(0))->toString(), "0.0");
EXPECT_EQ(f(Integer(0))->toString(), "0");
EXPECT_EQ(f(Integer(1))->toString(),
"0.88137358701954302523260932497979230902816032826163541075329560865337718422202609");

EXPECT_EQ(f(Rational(1, 10))->toString(),
"0.099834078899207563327303124704769443267712911708825010742382695651591768393613465");
EXPECT_EQ(f(Rational(-1, 5))->toString(),
"-0.19869011034924140647463691595020696822130879422445377302126322228548564789597237");

EXPECT_EQ(f(Real("0.5"))->toString(),
"0.48121182505960344749775891342436842313518433438566051966101816884016386760822177");

EXPECT_EQ(f(Variable("a"))->toString(), "asinh(a)");

EXPECT_THROW(f(), InvalidInputFunctionException);
Expand Down
Loading

0 comments on commit cb15640

Please sign in to comment.