diff --git a/sprout/complex.hpp b/sprout/complex.hpp index 26df2016..9b8bd196 100644 --- a/sprout/complex.hpp +++ b/sprout/complex.hpp @@ -3,14 +3,9 @@ #include #include +#include namespace sprout { - namespace detail { - template - SPROUT_CONSTEXPR T pi_div_two() { - return 1.5707963267948966192313216916397514L; - } - } // namespace detail template class complex; template @@ -94,7 +89,7 @@ namespace sprout { complex& operator*=(complex const& rhs) { return *this = complex( re_ * rhs.real() - im_ * rhs.imag(), - re_ * rhs.imag() + im_ * rhs.imag() + re_ * rhs.imag() + im_ * rhs.real() ); } template @@ -153,7 +148,7 @@ namespace sprout { SPROUT_CONSTEXPR sprout::complex operator*(sprout::complex const& lhs, sprout::complex const& rhs) { return sprout::complex( lhs.real() * rhs.real() - lhs.imag() * rhs.imag(), - lhs.real() * rhs.imag() + lhs.imag() * rhs.imag() + lhs.real() * rhs.imag() + lhs.imag() * rhs.real() ); } template @@ -364,7 +359,7 @@ namespace sprout { namespace detail { template SPROUT_CONSTEXPR sprout::complex acos_impl(sprout::complex const& t) { - return sprout::complex(sprout::detail::pi_div_two() - t.real(), -t.imag()); + return sprout::complex(sprout::math::pi_div_two() - t.real(), -t.imag()); } } // namespace detail template diff --git a/sprout/fft/bitrev_table.hpp b/sprout/fft/bitrev_table.hpp deleted file mode 100644 index 1929212b..00000000 --- a/sprout/fft/bitrev_table.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SPROUT_FFT_BITREV_TABLE_HPP -#define SPROUT_FFT_BITREV_TABLE_HPP - -#include -#include -#include - -#endif // #ifndef SPROUT_FFT_BITREV_TABLE_HPP diff --git a/sprout/functional/dft.hpp b/sprout/functional/dft.hpp new file mode 100644 index 00000000..0e7ba62f --- /dev/null +++ b/sprout/functional/dft.hpp @@ -0,0 +1,11 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_HPP +#define SPROUT_FUNCTIONAL_DFT_HPP + +#include +#include +#include +#include +#include + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_HPP + diff --git a/sprout/functional/dft/detail/dft_element_gen.hpp b/sprout/functional/dft/detail/dft_element_gen.hpp new file mode 100644 index 00000000..498ad5a3 --- /dev/null +++ b/sprout/functional/dft/detail/dft_element_gen.hpp @@ -0,0 +1,40 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_DETAIL_DFT_ELEMENT_GEN_HPP +#define SPROUT_FUNCTIONAL_DFT_DETAIL_DFT_ELEMENT_GEN_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + SPROUT_CONSTEXPR inline typename std::iterator_traits::value_type dft_element_gen( + InputIterator first, + InputIterator last, + typename std::iterator_traits::value_type::value_type arg, + typename std::iterator_traits::difference_type k = 0, + typename std::iterator_traits::value_type value = typename std::iterator_traits::value_type(), + typename std::iterator_traits::value_type::value_type theta = typename std::iterator_traits::value_type::value_type() + ) + { + typedef typename std::iterator_traits::value_type value_type; + using std::cos; + using std::sin; + return first != last + ? value + sprout::detail::dft_element_gen( + sprout::next(first), + last, + arg, + k + 1, + *first * value_type(cos(theta), sin(theta)), + arg * (k + 1) + ) + : value + ; + } + } // namespace detail +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_DETAIL_DFT_ELEMENT_GEN_HPP diff --git a/sprout/functional/dft/dft.hpp b/sprout/functional/dft/dft.hpp new file mode 100644 index 00000000..e567013e --- /dev/null +++ b/sprout/functional/dft/dft.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_DFT_HPP +#define SPROUT_FUNCTIONAL_DFT_DFT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_DFT_HPP diff --git a/sprout/functional/dft/dft_element.hpp b/sprout/functional/dft/dft_element.hpp new file mode 100644 index 00000000..890f08f9 --- /dev/null +++ b/sprout/functional/dft/dft_element.hpp @@ -0,0 +1,48 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_DFT_ELEMENT_HPP +#define SPROUT_FUNCTIONAL_DFT_FIXED_DFT_ELEMENT_HPP + +#include +#include +#include +#include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace detail { + template + SPROUT_CONSTEXPR inline typename std::iterator_traits::value_type dft_element_impl( + InputIterator first, + InputIterator last, + typename std::iterator_traits::difference_type i, + Size size + ) + { + typedef typename std::iterator_traits::value_type value_type; + typedef typename value_type::value_type elem_type; + return sprout::detail::dft_element_gen( + first, + last, + -(2 * sprout::math::pi() * i / size) + ); + } + } // namespace detail + // + // dft_element + // + template + SPROUT_CONSTEXPR inline typename std::iterator_traits::value_type dft_element( + InputIterator first, + InputIterator last, + typename std::iterator_traits::difference_type i + ) + { + return sprout::detail::dft_element_impl( + first, + last, + i, + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last) + ); + } +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_FIXED_DFT_ELEMENT_HPP diff --git a/sprout/functional/dft/fit/dft.hpp b/sprout/functional/dft/fit/dft.hpp new file mode 100644 index 00000000..1955e803 --- /dev/null +++ b/sprout/functional/dft/fit/dft.hpp @@ -0,0 +1,46 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_FIT_DFT_HPP +#define SPROUT_FUNCTIONAL_DFT_FIT_DFT_HPP + +#include +#include +#include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace fit { + namespace detail { + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type dft_impl( + InputIterator first, + InputIterator last, + Result const& result, + typename sprout::fixed_container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_fixed(sprout::fixed::dft(first, last, result)), + offset, + offset + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last), sprout::size(result)) + ); + } + } // namespace detail + // + // dft + // + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type dft( + InputIterator first, + InputIterator last, + Result const& result + ) + { + return sprout::fit::detail::dft_impl(first, last, result, sprout::fixed_begin_offset(result)); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_FIT_DFT_HPP diff --git a/sprout/functional/dft/fit/idft.hpp b/sprout/functional/dft/fit/idft.hpp new file mode 100644 index 00000000..3cfbe81d --- /dev/null +++ b/sprout/functional/dft/fit/idft.hpp @@ -0,0 +1,46 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_FIT_IDFT_HPP +#define SPROUT_FUNCTIONAL_DFT_FIT_IDFT_HPP + +#include +#include +#include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace fit { + namespace detail { + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type idft_impl( + InputIterator first, + InputIterator last, + Result const& result, + typename sprout::fixed_container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_fixed(sprout::fixed::idft(first, last, result)), + offset, + offset + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last), sprout::size(result)) + ); + } + } // namespace detail + // + // idft + // + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type idft( + InputIterator first, + InputIterator last, + Result const& result + ) + { + return sprout::fit::detail::idft_impl(first, last, result, sprout::fixed_begin_offset(result)); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_FIT_IDFT_HPP diff --git a/sprout/functional/dft/fixed/dft.hpp b/sprout/functional/dft/fixed/dft.hpp new file mode 100644 index 00000000..0d1f4525 --- /dev/null +++ b/sprout/functional/dft/fixed/dft.hpp @@ -0,0 +1,72 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_DFT_HPP +#define SPROUT_FUNCTIONAL_DFT_FIXED_DFT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace fixed { + namespace detail { + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type dft_impl( + InputIterator first, + InputIterator last, + Result const& result, + sprout::index_tuple, + typename sprout::fixed_container_traits::difference_type offset, + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::size_type input_size + ) + { + return sprout::remake_clone( + result, + sprout::size(result), + (Indexes >= offset && Indexes < offset + size && Indexes < offset + input_size + ? sprout::detail::dft_element_impl(first, last, Indexes - offset, input_size) + : *sprout::next(sprout::fixed_begin(result), Indexes) + )... + ); + } + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type dft( + InputIterator first, + InputIterator last, + Result const& result + ) + { + return sprout::fixed::detail::dft_impl( + first, + last, + result, + typename sprout::index_range<0, sprout::fixed_container_traits::fixed_size>::type(), + sprout::fixed_begin_offset(result), + sprout::size(result), + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last) + ); + } + } // namespace detail + // + // dft + // + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type dft( + InputIterator first, + InputIterator last, + Result const& result + ) + { + return sprout::fixed::detail::dft(first, last, result); + } + } // namespace fixed + + using sprout::fixed::dft; +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_FIXED_DFT_HPP diff --git a/sprout/functional/dft/fixed/idft.hpp b/sprout/functional/dft/fixed/idft.hpp new file mode 100644 index 00000000..db90b584 --- /dev/null +++ b/sprout/functional/dft/fixed/idft.hpp @@ -0,0 +1,72 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_HPP +#define SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace fixed { + namespace detail { + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type idft_impl( + InputIterator first, + InputIterator last, + Result const& result, + sprout::index_tuple, + typename sprout::fixed_container_traits::difference_type offset, + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::size_type input_size + ) + { + return sprout::remake_clone( + result, + sprout::size(result), + (Indexes >= offset && Indexes < offset + size && Indexes < offset + input_size + ? sprout::detail::idft_element_impl(first, last, Indexes - offset, input_size) + : *sprout::next(sprout::fixed_begin(result), Indexes) + )... + ); + } + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type idft( + InputIterator first, + InputIterator last, + Result const& result + ) + { + return sprout::fixed::detail::idft_impl( + first, + last, + result, + typename sprout::index_range<0, sprout::fixed_container_traits::fixed_size>::type(), + sprout::fixed_begin_offset(result), + sprout::size(result), + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last) + ); + } + } // namespace detail + // + // idft + // + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type idft( + InputIterator first, + InputIterator last, + Result const& result + ) + { + return sprout::fixed::detail::idft(first, last, result); + } + } // namespace fixed + + using sprout::fixed::idft; +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_HPP diff --git a/sprout/functional/dft/idft.hpp b/sprout/functional/dft/idft.hpp new file mode 100644 index 00000000..6fb05b7a --- /dev/null +++ b/sprout/functional/dft/idft.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_IDFT_HPP +#define SPROUT_FUNCTIONAL_DFT_IDFT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_IDFT_HPP diff --git a/sprout/functional/dft/idft_element.hpp b/sprout/functional/dft/idft_element.hpp new file mode 100644 index 00000000..b4be4e01 --- /dev/null +++ b/sprout/functional/dft/idft_element.hpp @@ -0,0 +1,50 @@ +#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_ELEMENT_HPP +#define SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_ELEMENT_HPP + +#include +#include +#include +#include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace detail { + template + SPROUT_CONSTEXPR inline typename std::iterator_traits::value_type idft_element_impl( + InputIterator first, + InputIterator last, + typename std::iterator_traits::difference_type i, + Size size + ) + { + typedef typename std::iterator_traits::value_type value_type; + typedef typename value_type::value_type elem_type; + return sprout::detail::dft_element_gen( + first, + last, + 2 * sprout::math::pi() * i / size + ) + / static_cast(size) + ; + } + } // namespace detail + // + // idft_element + // + template + SPROUT_CONSTEXPR inline typename std::iterator_traits::value_type idft_element( + InputIterator first, + InputIterator last, + typename std::iterator_traits::difference_type i + ) + { + return sprout::detail::idft_element_impl( + first, + last, + i, + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last) + ); + } +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_ELEMENT_HPP diff --git a/sprout/functional/fft.hpp b/sprout/functional/fft.hpp new file mode 100644 index 00000000..a6bd8c96 --- /dev/null +++ b/sprout/functional/fft.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_FUNCTIONAL_FFT_HPP +#define SPROUT_FUNCTIONAL_FFT_HPP + +#include +#include + +#endif // #ifndef SPROUT_FUNCTIONAL_FFT_HPP + diff --git a/sprout/functional/fft/bitrev_table.hpp b/sprout/functional/fft/bitrev_table.hpp new file mode 100644 index 00000000..75a30652 --- /dev/null +++ b/sprout/functional/fft/bitrev_table.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_FUNCTIONAL_FFT_BITREV_TABLE_HPP +#define SPROUT_FUNCTIONAL_FFT_BITREV_TABLE_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_FUNCTIONAL_FFT_BITREV_TABLE_HPP diff --git a/sprout/fft/fit/bitrev_table.hpp b/sprout/functional/fft/fit/bitrev_table.hpp similarity index 79% rename from sprout/fft/fit/bitrev_table.hpp rename to sprout/functional/fft/fit/bitrev_table.hpp index b35ccd24..30f6eeaf 100644 --- a/sprout/fft/fit/bitrev_table.hpp +++ b/sprout/functional/fft/fit/bitrev_table.hpp @@ -1,11 +1,11 @@ -#ifndef SPROUT_FFT_FIT_BITREV_TABLE_HPP -#define SPROUT_FFT_FIT_BITREV_TABLE_HPP +#ifndef SPROUT_FUNCTIONAL_FFT_FIT_BITREV_TABLE_HPP +#define SPROUT_FUNCTIONAL_FFT_FIT_BITREV_TABLE_HPP #include #include #include #include -#include +#include #include #include @@ -38,4 +38,4 @@ namespace sprout { } // namespace fit } // namespace sprout -#endif // #ifndef SPROUT_FFT_FIT_BITREV_TABLE_HPP +#endif // #ifndef SPROUT_FUNCTIONAL_FFT_FIT_BITREV_TABLE_HPP diff --git a/sprout/fft/fixed/bitrev_table.hpp b/sprout/functional/fft/fixed/bitrev_table.hpp similarity index 88% rename from sprout/fft/fixed/bitrev_table.hpp rename to sprout/functional/fft/fixed/bitrev_table.hpp index 311e48eb..80e6ae21 100644 --- a/sprout/fft/fixed/bitrev_table.hpp +++ b/sprout/functional/fft/fixed/bitrev_table.hpp @@ -1,5 +1,5 @@ -#ifndef SPROUT_FFT_FIXED_BITREV_TABLE_HPP -#define SPROUT_FFT_FIXED_BITREV_TABLE_HPP +#ifndef SPROUT_FUNCTIONAL_FFT_FIXED_BITREV_TABLE_HPP +#define SPROUT_FUNCTIONAL_FFT_FIXED_BITREV_TABLE_HPP #include #include @@ -61,4 +61,4 @@ namespace sprout { using sprout::fixed::bitrev_table; } // namespace sprout -#endif // #ifndef SPROUT_FFT_FIXED_BITREV_TABLE_HPP +#endif // #ifndef SPROUT_FUNCTIONAL_FFT_FIXED_BITREV_TABLE_HPP diff --git a/sprout/math/constants.hpp b/sprout/math/constants.hpp new file mode 100644 index 00000000..87f2a385 --- /dev/null +++ b/sprout/math/constants.hpp @@ -0,0 +1,25 @@ +#ifndef SPROUT_MATH_CONSTANTS_HPP +#define SPROUT_MATH_CONSTANTS_HPP + +#include + +namespace sprout { + namespace math { + // + // pi + // + template + SPROUT_CONSTEXPR inline T pi() { + return 3.141592653589793238462643383279502884197169399375105820974944L; + } + // + // pi_div_two + // + template + SPROUT_CONSTEXPR inline T pi_div_two() { + return 1.570796326794896619231321691639751442098584699687552910487472L; + } + } // namespace math +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_CONSTANTS_HPP