sprout/functional/dft.hpp 追加

sprout/complex.hpp 修正
This commit is contained in:
bolero-MURAKAMI 2012-02-25 23:59:46 +09:00
parent aa96f9ce73
commit 10c73ea3e3
17 changed files with 453 additions and 24 deletions

View file

@ -3,14 +3,9 @@
#include <cmath>
#include <sprout/config.hpp>
#include <sprout/math/constants.hpp>
namespace sprout {
namespace detail {
template<typename T>
SPROUT_CONSTEXPR T pi_div_two() {
return 1.5707963267948966192313216916397514L;
}
} // namespace detail
template<typename T>
class complex;
template<typename T>
@ -94,7 +89,7 @@ namespace sprout {
complex<T>& operator*=(complex<X> const& rhs) {
return *this = complex<T>(
re_ * rhs.real() - im_ * rhs.imag(),
re_ * rhs.imag() + im_ * rhs.imag()
re_ * rhs.imag() + im_ * rhs.real()
);
}
template<typename X>
@ -153,7 +148,7 @@ namespace sprout {
SPROUT_CONSTEXPR sprout::complex<T> operator*(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
return sprout::complex<T>(
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<typename T>
@ -364,7 +359,7 @@ namespace sprout {
namespace detail {
template<typename T>
SPROUT_CONSTEXPR sprout::complex<T> acos_impl(sprout::complex<T> const& t) {
return sprout::complex<T>(sprout::detail::pi_div_two<T>() - t.real(), -t.imag());
return sprout::complex<T>(sprout::math::pi_div_two<T>() - t.real(), -t.imag());
}
} // namespace detail
template<typename T>

View file

@ -1,8 +0,0 @@
#ifndef SPROUT_FFT_BITREV_TABLE_HPP
#define SPROUT_FFT_BITREV_TABLE_HPP
#include <sprout/config.hpp>
#include <sprout/fft/fixed/bitrev_table.hpp>
#include <sprout/fft/fit/bitrev_table.hpp>
#endif // #ifndef SPROUT_FFT_BITREV_TABLE_HPP

11
sprout/functional/dft.hpp Normal file
View file

@ -0,0 +1,11 @@
#ifndef SPROUT_FUNCTIONAL_DFT_HPP
#define SPROUT_FUNCTIONAL_DFT_HPP
#include <sprout/config.hpp>
#include <sprout/functional/dft/dft.hpp>
#include <sprout/functional/dft/idft.hpp>
#include <sprout/functional/dft/dft_element.hpp>
#include <sprout/functional/dft/idft_element.hpp>
#endif // #ifndef SPROUT_FUNCTIONAL_DFT_HPP

View file

@ -0,0 +1,40 @@
#ifndef SPROUT_FUNCTIONAL_DFT_DETAIL_DFT_ELEMENT_GEN_HPP
#define SPROUT_FUNCTIONAL_DFT_DETAIL_DFT_ELEMENT_GEN_HPP
#include <cmath>
#include <iterator>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
namespace sprout {
namespace detail {
template<typename InputIterator>
SPROUT_CONSTEXPR inline typename std::iterator_traits<InputIterator>::value_type dft_element_gen(
InputIterator first,
InputIterator last,
typename std::iterator_traits<InputIterator>::value_type::value_type arg,
typename std::iterator_traits<InputIterator>::difference_type k = 0,
typename std::iterator_traits<InputIterator>::value_type value = typename std::iterator_traits<InputIterator>::value_type(),
typename std::iterator_traits<InputIterator>::value_type::value_type theta = typename std::iterator_traits<InputIterator>::value_type::value_type()
)
{
typedef typename std::iterator_traits<InputIterator>::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

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_FUNCTIONAL_DFT_DFT_HPP
#define SPROUT_FUNCTIONAL_DFT_DFT_HPP
#include <sprout/config.hpp>
#include <sprout/functional/dft/fixed/dft.hpp>
#include <sprout/functional/dft/fit/dft.hpp>
#endif // #ifndef SPROUT_FUNCTIONAL_DFT_DFT_HPP

View file

@ -0,0 +1,48 @@
#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_DFT_ELEMENT_HPP
#define SPROUT_FUNCTIONAL_DFT_FIXED_DFT_ELEMENT_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/functional/dft/detail/dft_element_gen.hpp>
#include <sprout/math/constants.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
namespace detail {
template<typename InputIterator, typename Size>
SPROUT_CONSTEXPR inline typename std::iterator_traits<InputIterator>::value_type dft_element_impl(
InputIterator first,
InputIterator last,
typename std::iterator_traits<InputIterator>::difference_type i,
Size size
)
{
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
typedef typename value_type::value_type elem_type;
return sprout::detail::dft_element_gen(
first,
last,
-(2 * sprout::math::pi<elem_type>() * i / size)
);
}
} // namespace detail
//
// dft_element
//
template<typename InputIterator>
SPROUT_CONSTEXPR inline typename std::iterator_traits<InputIterator>::value_type dft_element(
InputIterator first,
InputIterator last,
typename std::iterator_traits<InputIterator>::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

View file

@ -0,0 +1,46 @@
#ifndef SPROUT_FUNCTIONAL_DFT_FIT_DFT_HPP
#define SPROUT_FUNCTIONAL_DFT_FIT_DFT_HPP
#include <sprout/config.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/functional/dft/fixed/dft.hpp>
#include <sprout/algorithm/fit/result_of.hpp>
#include <sprout/sub_array.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
namespace fit {
namespace detail {
template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Result>::type dft_impl(
InputIterator first,
InputIterator last,
Result const& result,
typename sprout::fixed_container_traits<Result>::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<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Result>::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

View file

@ -0,0 +1,46 @@
#ifndef SPROUT_FUNCTIONAL_DFT_FIT_IDFT_HPP
#define SPROUT_FUNCTIONAL_DFT_FIT_IDFT_HPP
#include <sprout/config.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/functional/dft/fixed/idft.hpp>
#include <sprout/algorithm/fit/result_of.hpp>
#include <sprout/sub_array.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
namespace fit {
namespace detail {
template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Result>::type idft_impl(
InputIterator first,
InputIterator last,
Result const& result,
typename sprout::fixed_container_traits<Result>::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<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Result>::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

View file

@ -0,0 +1,72 @@
#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_DFT_HPP
#define SPROUT_FUNCTIONAL_DFT_FIXED_DFT_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/algorithm/fixed/result_of.hpp>
#include <sprout/functional/dft/dft_element.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
namespace fixed {
namespace detail {
template<typename InputIterator, typename Result, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type dft_impl(
InputIterator first,
InputIterator last,
Result const& result,
sprout::index_tuple<Indexes...>,
typename sprout::fixed_container_traits<Result>::difference_type offset,
typename sprout::fixed_container_traits<Result>::size_type size,
typename sprout::fixed_container_traits<Result>::size_type input_size
)
{
return sprout::remake_clone<Result>(
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<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::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<Result>::fixed_size>::type(),
sprout::fixed_begin_offset(result),
sprout::size(result),
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last)
);
}
} // namespace detail
//
// dft
//
template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::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

View file

@ -0,0 +1,72 @@
#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_HPP
#define SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/algorithm/fixed/result_of.hpp>
#include <sprout/functional/dft/idft_element.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
namespace fixed {
namespace detail {
template<typename InputIterator, typename Result, std::ptrdiff_t... Indexes>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::type idft_impl(
InputIterator first,
InputIterator last,
Result const& result,
sprout::index_tuple<Indexes...>,
typename sprout::fixed_container_traits<Result>::difference_type offset,
typename sprout::fixed_container_traits<Result>::size_type size,
typename sprout::fixed_container_traits<Result>::size_type input_size
)
{
return sprout::remake_clone<Result>(
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<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::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<Result>::fixed_size>::type(),
sprout::fixed_begin_offset(result),
sprout::size(result),
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::distance(first, last)
);
}
} // namespace detail
//
// idft
//
template<typename InputIterator, typename Result>
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Result>::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

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_FUNCTIONAL_DFT_IDFT_HPP
#define SPROUT_FUNCTIONAL_DFT_IDFT_HPP
#include <sprout/config.hpp>
#include <sprout/functional/dft/fixed/idft.hpp>
#include <sprout/functional/dft/fit/idft.hpp>
#endif // #ifndef SPROUT_FUNCTIONAL_DFT_IDFT_HPP

View file

@ -0,0 +1,50 @@
#ifndef SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_ELEMENT_HPP
#define SPROUT_FUNCTIONAL_DFT_FIXED_IDFT_ELEMENT_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/functional/dft/detail/dft_element_gen.hpp>
#include <sprout/math/constants.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT_DETAIL
namespace sprout {
namespace detail {
template<typename InputIterator, typename Size>
SPROUT_CONSTEXPR inline typename std::iterator_traits<InputIterator>::value_type idft_element_impl(
InputIterator first,
InputIterator last,
typename std::iterator_traits<InputIterator>::difference_type i,
Size size
)
{
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
typedef typename value_type::value_type elem_type;
return sprout::detail::dft_element_gen(
first,
last,
2 * sprout::math::pi<elem_type>() * i / size
)
/ static_cast<elem_type>(size)
;
}
} // namespace detail
//
// idft_element
//
template<typename InputIterator>
SPROUT_CONSTEXPR inline typename std::iterator_traits<InputIterator>::value_type idft_element(
InputIterator first,
InputIterator last,
typename std::iterator_traits<InputIterator>::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

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_FUNCTIONAL_FFT_HPP
#define SPROUT_FUNCTIONAL_FFT_HPP
#include <sprout/config.hpp>
#include <sprout/functional/fft/bitrev_table.hpp>
#endif // #ifndef SPROUT_FUNCTIONAL_FFT_HPP

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_FUNCTIONAL_FFT_BITREV_TABLE_HPP
#define SPROUT_FUNCTIONAL_FFT_BITREV_TABLE_HPP
#include <sprout/config.hpp>
#include <sprout/functional/fft/fixed/bitrev_table.hpp>
#include <sprout/functional/fft/fit/bitrev_table.hpp>
#endif // #ifndef SPROUT_FUNCTIONAL_FFT_BITREV_TABLE_HPP

View file

@ -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 <type_traits>
#include <sprout/config.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/fft/fixed/bitrev_table.hpp>
#include <sprout/functional/fft/fixed/bitrev_table.hpp>
#include <sprout/algorithm/fit/result_of.hpp>
#include <sprout/sub_array.hpp>
@ -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

View file

@ -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 <cstddef>
#include <type_traits>
@ -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

25
sprout/math/constants.hpp Normal file
View file

@ -0,0 +1,25 @@
#ifndef SPROUT_MATH_CONSTANTS_HPP
#define SPROUT_MATH_CONSTANTS_HPP
#include <sprout/config.hpp>
namespace sprout {
namespace math {
//
// pi
//
template<typename T>
SPROUT_CONSTEXPR inline T pi() {
return 3.141592653589793238462643383279502884197169399375105820974944L;
}
//
// pi_div_two
//
template<typename T>
SPROUT_CONSTEXPR inline T pi_div_two() {
return 1.570796326794896619231321691639751442098584699687552910487472L;
}
} // namespace math
} // namespace sprout
#endif // #ifndef SPROUT_MATH_CONSTANTS_HPP