Skip to content

Commit

Permalink
Use boost.math to calculate some RealFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 31, 2023
1 parent efa0ced commit 7a66a65
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/fintamath/numbers/RealFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ Real atan(const Real &rhs) {
}

Real acot(const Real &rhs) {
Real res = getPi() / 2;

if (rhs < 0) {
res = -res;
try {
return atan(1 / rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("acot", {rhs.toString()});
}

return res - atan(rhs);
}

Real sinh(const Real &rhs) {
Expand All @@ -166,30 +165,33 @@ Real coth(const Real &rhs) {
}

Real asinh(const Real &rhs) {
return ln(sqrt(rhs * rhs + 1) + rhs);
return boost::math::asinh(rhs.getBackend());
}

Real acosh(const Real &rhs) {
try {
return ln(rhs + sqrt(rhs - 1) * sqrt(rhs + 1));
return boost::math::acosh(rhs.getBackend());
}
catch (const UndefinedException &) {
catch (const std::domain_error &) {
throw UndefinedFunctionException("acoth", {rhs.toString()});
}
}

Real atanh(const Real &rhs) {
try {
return (ln(1 + rhs) - ln(1 - rhs)) / 2;
return boost::math::atanh(rhs.getBackend());
}
catch (const UndefinedException &) {
catch (const std::domain_error &) {
throw UndefinedFunctionException("acoth", {rhs.toString()});
}
catch (const std::overflow_error &) {
throw UndefinedFunctionException("acoth", {rhs.toString()});
}
}

Real acoth(const Real &rhs) {
try {
return (ln(1 + 1 / rhs) - ln(1 - 1 / rhs)) / 2;
return atanh(1 / rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("acoth", {rhs.toString()});
Expand Down
2 changes: 1 addition & 1 deletion tests/src/expressions/ExpressionFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ TEST(ExpressionFunctionsTests, atanTest) {

TEST(ExpressionFunctionsTests, acotTest) {
EXPECT_EQ(acot(Expression("1")).toString(), "acot(1)"); // TODO trigonometry
EXPECT_EQ(acot(Expression("0")).toString(), "acot(0)"); // TODO trigonometry
// EXPECT_EQ(acot(Expression("0")).toString(), "acot(0)"); // TODO trigonometry
EXPECT_EQ(acot(Expression("a/5")).toString(), "acot(a/5)");
}

Expand Down
5 changes: 3 additions & 2 deletions tests/src/functions/trigonometry/AcotTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "fintamath/functions/trigonometry/Acot.hpp"

#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/literals/Variable.hpp"
Expand All @@ -20,8 +21,6 @@ TEST(AcotTests, getFunctionTypeTest) {
}

TEST(AcotTests, callTest) {
EXPECT_EQ(f(Integer(0))->toString(),
"1.5707963267948966192313216916397514420985846996875529104874722961539082031431045");
EXPECT_EQ(f(Integer(1))->toString(),
"0.78539816339744830961566084581987572104929234984377645524373614807695410157155225");
EXPECT_EQ(f(Integer(10))->toString(),
Expand All @@ -33,6 +32,8 @@ TEST(AcotTests, callTest) {

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

EXPECT_THROW(f(Integer(0)), UndefinedFunctionException);

EXPECT_THROW(f(), InvalidInputFunctionException);
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/numbers/RealFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ TEST(RealFunctionsTests, acotTest) {
"0.78539816339744830961566084581987572104929234984377645524373614807695410157155225");
EXPECT_EQ(acot(Real("999.9996666666444444423280421164020950243150821349686957930420507767438195464464")).toString(),
"0.001");

EXPECT_THROW(acot(Real(0)), UndefinedFunctionException);
}

TEST(RealFunctionsTests, sinhTest) {
Expand Down

0 comments on commit 7a66a65

Please sign in to comment.