-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementations of sinh, cosh, and tan
- Loading branch information
1 parent
1063b55
commit 17bfd03
Showing
12 changed files
with
519 additions
and
19 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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
|
||
#include "pow_approx.hpp" | ||
|
||
namespace math_approx | ||
{ | ||
// ref: https://en.wikipedia.org/wiki/Hyperbolic_functions#Definitions | ||
// sinh = (e^(2x) - 1) / (2e^x), cosh = (e^(2x) + 1) / (2e^x) | ||
// let B = e^x, then sinh = (B^2 - 1) / (2B), cosh = (B^2 + 1) / (2B) | ||
// simplifying, we get: sinh = 0.5 (B - 1/B), cosh = 0.5 (B + 1/B) | ||
|
||
/** Approximation of sinh(x), using exp(x) internally */ | ||
template <int order, typename T> | ||
T sinh (T x) | ||
{ | ||
using S = scalar_of_t<T>; | ||
auto B = exp<order> (x); | ||
auto Br = (S) 0.5 / B; | ||
B *= (S) 0.5; | ||
return B - Br; | ||
} | ||
|
||
/** Approximation of cosh(x), using exp(x) internally */ | ||
template <int order, typename T> | ||
T cosh (T x) | ||
{ | ||
using S = scalar_of_t<T>; | ||
auto B = exp<order> (x); | ||
auto Br = (S) 0.5 / B; | ||
B *= (S) 0.5; | ||
return B + Br; | ||
} | ||
|
||
/** | ||
* Simultaneous pproximation of sinh(x) and cosh(x), | ||
* using exp(x) internally. | ||
* | ||
* For more information see the comments above. | ||
*/ | ||
template <int order, typename T> | ||
auto sinh_cosh (T x) | ||
{ | ||
using S = scalar_of_t<T>; | ||
auto B = exp<order> (x); | ||
auto Br = (S) 0.5 / B; | ||
B *= (S) 0.5; | ||
|
||
auto sinh = B - Br; | ||
auto cosh = B + Br; | ||
|
||
return std::make_pair (sinh, cosh); | ||
} | ||
} // namespace math_approx |
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
Oops, something went wrong.