Skip to content

Commit

Permalink
Merge pull request #26 from petiaccja/math-functions
Browse files Browse the repository at this point in the history
math functions additions
  • Loading branch information
petiaccja authored Nov 9, 2021
2 parents 17209ff + cce5cba commit 95ebe22
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ While the library is already quite capable, I'm planning to change the interface
- ✔️ Space
- ✔️ Linspace
- ✔️ Logspace
- ️ Common math functions
- ✔️ Complex (Abs, Arg, Real, Imag)
- ️ Common math functions
- ✔️ Complex (Abs, Arg, Real, Imag, Conj)
- ✔️ Trigonometric (Sin, Cos, Tan x inverse)
- ✔️ Hyperbolic (Sinh, Cosh, Tanh x inverse)
- ✔️ Exponential (Exp, Log, Log2, Log10)
- ✔️ Polynomial (Pow, Sqrt, Cbrt)
- ❌️ Special math (Erf, Gamma, etc.)
- ✔️ Erf and gamma
- ✔️ Vector math basics
- ✔️ Dot product
- ✔️ Norm (sqrt(SumSquare))
Expand Down
11 changes: 11 additions & 0 deletions include/dspbb/ComputeKernels/VectorizedMathFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ namespace math_functions {
using xsimd::conj;
using std::conj;

// Erf & gamma
using std::erf;
using std::erfc;
using std::tgamma;
using std::lgamma;

using xsimd::erf;
using xsimd::erfc;
using xsimd::tgamma;
using xsimd::lgamma;

// Misc
namespace impl {
template <class T, std::enable_if_t<(xsimd::simd_batch_traits<T>::size > 1), int> = 0>
Expand Down
10 changes: 10 additions & 0 deletions include/dspbb/Math/Functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ DSPBB_IMPL_FUNCTION(Abs, abs)
DSPBB_IMPL_FUNCTION(Arg, arg)
DSPBB_IMPL_FUNCTION(Real, real)
DSPBB_IMPL_FUNCTION(Imag, imag)
DSPBB_IMPL_FUNCTION(Conj, conj)

//------------------------------------------------------------------------------
// Exponential functions
Expand Down Expand Up @@ -93,4 +94,13 @@ DSPBB_IMPL_FUNCTION(Acosh, acosh)
DSPBB_IMPL_FUNCTION(Atanh, atanh)


//------------------------------------------------------------------------------
// Erf & gamma
//------------------------------------------------------------------------------

DSPBB_IMPL_FUNCTION(Erf, erf)
DSPBB_IMPL_FUNCTION(Erfc, erfc)
DSPBB_IMPL_FUNCTION(TGamma, tgamma)
DSPBB_IMPL_FUNCTION(LGamma, lgamma)

} // namespace dspbb
44 changes: 35 additions & 9 deletions test/Math/Test_Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ auto iden(T arg) {
}


#define TEST_CASE_FUNCTION_REAL(NAME, FUNC, STDFUNC) \
TEST_CASE(NAME " real", "[Functions]") { \
using namespace std; \
const TimeSignal<float> signal = { 1, 8, 2, 5, 3, 6, 3, 6, 4 }; \
const auto applied = FUNC(signal); \
for (size_t i = 0; i < signal.Size(); ++i) { \
REQUIRE(Approx(applied[i]) == STDFUNC(signal[i])); \
} \
#define TEST_CASE_FUNCTION_REAL(NAME, FUNC, STDFUNC) \
TEST_CASE(NAME " real", "[Functions]") { \
using namespace std; \
const TimeSignal<float> signal = { 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f }; \
const auto applied = FUNC(signal); \
for (size_t i = 0; i < signal.Size(); ++i) { \
REQUIRE(Approx(applied[i]) == STDFUNC(signal[i])); \
} \
}


Expand All @@ -37,19 +37,26 @@ auto iden(T arg) {
// Complex number functions.
TEST_CASE_FUNCTION_REAL("Abs", Abs, abs);
TEST_CASE_FUNCTION_CPLX("Abs", Abs, abs);
TEST_CASE_FUNCTION_REAL("Arg", Arg, arg);
TEST_CASE_FUNCTION_CPLX("Arg", Arg, arg);

TEST_CASE_FUNCTION_REAL("Real", Real, iden);
TEST_CASE_FUNCTION_CPLX("Real", Real, real);
TEST_CASE_FUNCTION_REAL("Imag", Imag, imag);
TEST_CASE_FUNCTION_CPLX("Imag", Imag, imag);

TEST_CASE_FUNCTION_CPLX("Conj", Conj, conj);

// Exponential functions
TEST_CASE_FUNCTION_REAL("Log", Log, log);
TEST_CASE_FUNCTION_REAL("Log2", Log2, log2);
TEST_CASE_FUNCTION_REAL("Log10", Log10, log10);
TEST_CASE_FUNCTION_REAL("Exp", Exp, exp);

TEST_CASE_FUNCTION_CPLX("Log", Log, log);
TEST_CASE_FUNCTION_CPLX("Log10", Log10, log10);
TEST_CASE_FUNCTION_CPLX("Exp", Exp, exp);


// Polynomial functions
TEST_CASE_FUNCTION_REAL("Sqrt", Sqrt, sqrt);
Expand All @@ -73,6 +80,13 @@ TEST_CASE("Pow complex", "[Functions]") {
}

// Trigonometric functions
TEST_CASE_FUNCTION_REAL("Sin", Sin, sin);
TEST_CASE_FUNCTION_REAL("Cos", Cos, cos);
TEST_CASE_FUNCTION_REAL("Tan", Tan, tan);
TEST_CASE_FUNCTION_REAL("Asin", Asin, asin);
TEST_CASE_FUNCTION_REAL("Acos", Acos, acos);
TEST_CASE_FUNCTION_REAL("Atan", Atan, atan);

TEST_CASE_FUNCTION_CPLX("Sin", Sin, sin);
TEST_CASE_FUNCTION_CPLX("Cos", Cos, cos);
TEST_CASE_FUNCTION_CPLX("Tan", Tan, tan);
Expand All @@ -81,9 +95,21 @@ TEST_CASE_FUNCTION_CPLX("Acos", Acos, acos);
TEST_CASE_FUNCTION_CPLX("Atan", Atan, atan);

// Hyperbolic functions
TEST_CASE_FUNCTION_REAL("Sinh", Sinh, sinh);
TEST_CASE_FUNCTION_REAL("Cosh", Cosh, cosh);
TEST_CASE_FUNCTION_REAL("Tanh", Tanh, tanh);
TEST_CASE_FUNCTION_REAL("Asinh", Asinh, asinh);
TEST_CASE_FUNCTION_REAL("Atanh", Atanh, atanh);

TEST_CASE_FUNCTION_CPLX("Sinh", Sinh, sinh);
TEST_CASE_FUNCTION_CPLX("Cosh", Cosh, cosh);
TEST_CASE_FUNCTION_CPLX("Tanh", Tanh, tanh);
TEST_CASE_FUNCTION_CPLX("Asinh", Asinh, asinh);
TEST_CASE_FUNCTION_CPLX("Acosh", Acosh, acosh);
TEST_CASE_FUNCTION_CPLX("Atanh", Atanh, atanh);
TEST_CASE_FUNCTION_CPLX("Atanh", Atanh, atanh);

// Hyperbolic functions
TEST_CASE_FUNCTION_REAL("Erf", Erf, erf);
TEST_CASE_FUNCTION_REAL("Erfc", Erfc, erfc);
TEST_CASE_FUNCTION_REAL("TGamma", TGamma, tgamma);
TEST_CASE_FUNCTION_REAL("LGamma", LGamma, lgamma);

0 comments on commit 95ebe22

Please sign in to comment.