From 24d2a229f308b682bc38203e5d8c3c05999497d5 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 3 Dec 2012 21:48:50 +0900 Subject: [PATCH] add compost dft, spectrum, etc --- sprout/compost.hpp | 2 + sprout/compost/analyses.hpp | 10 + .../compost/analyses/amplitude_spectrum.hpp | 20 ++ sprout/compost/analyses/dft.hpp | 20 ++ sprout/compost/analyses/idft.hpp | 20 ++ sprout/compost/analyses/phase_spectrum.hpp | 20 ++ sprout/compost/formats.hpp | 3 + sprout/compost/formats/as_complex.hpp | 56 +++++ sprout/compost/formats/as_imag.hpp | 57 +++++ sprout/compost/formats/as_real.hpp | 57 +++++ sprout/compost/utility.hpp | 7 + sprout/compost/utility/equal_temperament.hpp | 28 +++ sprout/functional/mem_fn.hpp | 16 +- sprout/iterator/clamp_iterator.hpp | 6 +- sprout/iterator/dft.hpp | 8 + sprout/iterator/dft_iterator.hpp | 213 ++++++++++++++++++ sprout/iterator/filter_iterator.hpp | 12 +- sprout/iterator/idft_iterator.hpp | 213 ++++++++++++++++++ sprout/iterator/predefined.hpp | 1 + sprout/math/cos.hpp | 2 +- sprout/numeric/dft.hpp | 2 + .../numeric/dft/amplitude_spectrum_value.hpp | 21 ++ sprout/numeric/dft/detail/dft_element_gen.hpp | 72 +++++- sprout/numeric/dft/dft_element.hpp | 6 +- .../numeric/dft/fixed/amplitude_spectrum.hpp | 10 +- sprout/numeric/dft/fixed/phase_spectrum.hpp | 9 +- sprout/numeric/dft/idft_element.hpp | 6 +- sprout/numeric/dft/phase_spectrum_value.hpp | 22 ++ sprout/numeric/non_modifying.hpp | 2 + sprout/numeric/unstable_accumulate.hpp | 81 +++++++ sprout/numeric/unstable_inner_product.hpp | 89 ++++++++ sprout/range/adaptor/amplitude_spectrum.hpp | 103 +++++++++ sprout/range/adaptor/clamped.hpp | 10 +- sprout/range/adaptor/dft.hpp | 83 +++++++ sprout/range/adaptor/filtered.hpp | 8 +- sprout/range/adaptor/idft.hpp | 83 +++++++ sprout/range/adaptor/phase_spectrum.hpp | 103 +++++++++ sprout/range/adaptor/replaced.hpp | 4 +- sprout/range/adaptor/replaced_if.hpp | 10 +- sprout/range/adaptor/wave.hpp | 4 + sprout/range/numeric/dft.hpp | 2 + sprout/range/numeric/dft/dft_element.hpp | 22 ++ sprout/range/numeric/dft/fit/spectrum.hpp | 3 + sprout/range/numeric/dft/fixed/spectrum.hpp | 3 + sprout/range/numeric/dft/idft_element.hpp | 22 ++ 45 files changed, 1490 insertions(+), 61 deletions(-) create mode 100644 sprout/compost/analyses.hpp create mode 100644 sprout/compost/analyses/amplitude_spectrum.hpp create mode 100644 sprout/compost/analyses/dft.hpp create mode 100644 sprout/compost/analyses/idft.hpp create mode 100644 sprout/compost/analyses/phase_spectrum.hpp create mode 100644 sprout/compost/formats/as_complex.hpp create mode 100644 sprout/compost/formats/as_imag.hpp create mode 100644 sprout/compost/formats/as_real.hpp create mode 100644 sprout/compost/utility.hpp create mode 100644 sprout/compost/utility/equal_temperament.hpp create mode 100644 sprout/iterator/dft.hpp create mode 100644 sprout/iterator/dft_iterator.hpp create mode 100644 sprout/iterator/idft_iterator.hpp create mode 100644 sprout/numeric/dft/amplitude_spectrum_value.hpp create mode 100644 sprout/numeric/dft/phase_spectrum_value.hpp create mode 100644 sprout/numeric/unstable_accumulate.hpp create mode 100644 sprout/numeric/unstable_inner_product.hpp create mode 100644 sprout/range/adaptor/amplitude_spectrum.hpp create mode 100644 sprout/range/adaptor/dft.hpp create mode 100644 sprout/range/adaptor/idft.hpp create mode 100644 sprout/range/adaptor/phase_spectrum.hpp create mode 100644 sprout/range/numeric/dft/dft_element.hpp create mode 100644 sprout/range/numeric/dft/idft_element.hpp diff --git a/sprout/compost.hpp b/sprout/compost.hpp index 6d4e63be..e58edc3b 100644 --- a/sprout/compost.hpp +++ b/sprout/compost.hpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #endif // #ifndef SPROUT_COMPOST_HPP diff --git a/sprout/compost/analyses.hpp b/sprout/compost/analyses.hpp new file mode 100644 index 00000000..b108f238 --- /dev/null +++ b/sprout/compost/analyses.hpp @@ -0,0 +1,10 @@ +#ifndef SPROUT_COMPOST_ANALYSES_HPP +#define SPROUT_COMPOST_ANALYSES_HPP + +#include +#include +#include +#include +#include + +#endif // #ifndef SPROUT_COMPOST_ANALYSES_HPP diff --git a/sprout/compost/analyses/amplitude_spectrum.hpp b/sprout/compost/analyses/amplitude_spectrum.hpp new file mode 100644 index 00000000..98839e17 --- /dev/null +++ b/sprout/compost/analyses/amplitude_spectrum.hpp @@ -0,0 +1,20 @@ +#ifndef SPROUT_COMPOST_ANALYSES_AMPLITUDE_SPECTRUM_HPP +#define SPROUT_COMPOST_ANALYSES_AMPLITUDE_SPECTRUM_HPP + +#include +#include + +namespace sprout { + namespace compost { + namespace analyses { + // + // amplitude_spectrum + // + using sprout::adaptors::amplitude_spectrum; + } // namespace analyses + + using sprout::compost::analyses::amplitude_spectrum; + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_ANALYSES_AMPLITUDE_SPECTRUM_HPP diff --git a/sprout/compost/analyses/dft.hpp b/sprout/compost/analyses/dft.hpp new file mode 100644 index 00000000..0d0c88cc --- /dev/null +++ b/sprout/compost/analyses/dft.hpp @@ -0,0 +1,20 @@ +#ifndef SPROUT_COMPOST_ANALYSES_DFT_HPP +#define SPROUT_COMPOST_ANALYSES_DFT_HPP + +#include +#include + +namespace sprout { + namespace compost { + namespace analyses { + // + // dft + // + using sprout::adaptors::dft; + } // namespace analyses + + using sprout::compost::analyses::dft; + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_ANALYSES_DFT_HPP diff --git a/sprout/compost/analyses/idft.hpp b/sprout/compost/analyses/idft.hpp new file mode 100644 index 00000000..e7d7d5c3 --- /dev/null +++ b/sprout/compost/analyses/idft.hpp @@ -0,0 +1,20 @@ +#ifndef SPROUT_COMPOST_ANALYSES_IDFT_HPP +#define SPROUT_COMPOST_ANALYSES_IDFT_HPP + +#include +#include + +namespace sprout { + namespace compost { + namespace analyses { + // + // idft + // + using sprout::adaptors::idft; + } // namespace analyses + + using sprout::compost::analyses::idft; + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_ANALYSES_IDFT_HPP diff --git a/sprout/compost/analyses/phase_spectrum.hpp b/sprout/compost/analyses/phase_spectrum.hpp new file mode 100644 index 00000000..de36a48e --- /dev/null +++ b/sprout/compost/analyses/phase_spectrum.hpp @@ -0,0 +1,20 @@ +#ifndef SPROUT_COMPOST_ANALYSES_PHASE_SPECTRUM_HPP +#define SPROUT_COMPOST_ANALYSES_PHASE_SPECTRUM_HPP + +#include +#include + +namespace sprout { + namespace compost { + namespace analyses { + // + // phase_spectrum + // + using sprout::adaptors::phase_spectrum; + } // namespace analyses + + using sprout::compost::analyses::phase_spectrum; + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_ANALYSES_PHASE_SPECTRUM_HPP diff --git a/sprout/compost/formats.hpp b/sprout/compost/formats.hpp index 8ef6934f..8f003782 100644 --- a/sprout/compost/formats.hpp +++ b/sprout/compost/formats.hpp @@ -9,5 +9,8 @@ #include #include #include +#include +#include +#include #endif // #ifndef SPROUT_COMPOST_FORMATS_HPP diff --git a/sprout/compost/formats/as_complex.hpp b/sprout/compost/formats/as_complex.hpp new file mode 100644 index 00000000..1714cd0c --- /dev/null +++ b/sprout/compost/formats/as_complex.hpp @@ -0,0 +1,56 @@ +#ifndef SPROUT_COMPOST_FORMATS_AS_COMPLEX_HPP +#define SPROUT_COMPOST_FORMATS_AS_COMPLEX_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace compost { + // + // to_complex_value + // + struct to_complex_value { + public: + template + SPROUT_CONSTEXPR sprout::complex operator()(FloatType const& x) const { + return sprout::complex(x); + } + }; + + namespace formats { + // + // as_complex_forwarder + // + class as_complex_forwarder {}; + + // + // as_complex + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::compost::formats::as_complex_forwarder as_complex{}; + } // anonymous-namespace + + // + // operator| + // + template + inline SPROUT_CONSTEXPR auto + operator|(Range&& lhs, sprout::compost::formats::as_complex_forwarder const& rhs) + -> decltype( + sprout::forward(lhs) + | sprout::adaptors::transformed(sprout::compost::to_complex_value()) + ) + { + return sprout::forward(lhs) + | sprout::adaptors::transformed(sprout::compost::to_complex_value()) + ; + } + } // namespace formats + + using sprout::compost::formats::as_complex; + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_FORMATS_AS_COMPLEX_HPP diff --git a/sprout/compost/formats/as_imag.hpp b/sprout/compost/formats/as_imag.hpp new file mode 100644 index 00000000..592cefa3 --- /dev/null +++ b/sprout/compost/formats/as_imag.hpp @@ -0,0 +1,57 @@ +#ifndef SPROUT_COMPOST_FORMATS_AS_IMAG_HPP +#define SPROUT_COMPOST_FORMATS_AS_IMAG_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace compost { + // + // to_imag_value + // + struct to_imag_value { + public: + template + SPROUT_CONSTEXPR decltype(imag(std::declval())) operator()(Complex const& x) const { + return imag(x); + } + }; + + namespace formats { + // + // as_imag_forwarder + // + class as_imag_forwarder {}; + + // + // as_imag + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::compost::formats::as_imag_forwarder as_imag{}; + } // anonymous-namespace + + // + // operator| + // + template + inline SPROUT_CONSTEXPR auto + operator|(Range&& lhs, sprout::compost::formats::as_imag_forwarder const& rhs) + -> decltype( + sprout::forward(lhs) + | sprout::adaptors::transformed(sprout::compost::to_imag_value()) + ) + { + return sprout::forward(lhs) + | sprout::adaptors::transformed(sprout::compost::to_imag_value()) + ; + } + } // namespace formats + + using sprout::compost::formats::as_imag; + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_FORMATS_AS_IMAG_HPP diff --git a/sprout/compost/formats/as_real.hpp b/sprout/compost/formats/as_real.hpp new file mode 100644 index 00000000..04184553 --- /dev/null +++ b/sprout/compost/formats/as_real.hpp @@ -0,0 +1,57 @@ +#ifndef SPROUT_COMPOST_FORMATS_AS_REAL_HPP +#define SPROUT_COMPOST_FORMATS_AS_REAL_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace compost { + // + // to_real_value + // + struct to_real_value { + public: + template + SPROUT_CONSTEXPR decltype(real(std::declval())) operator()(Complex const& x) const { + return real(x); + } + }; + + namespace formats { + // + // as_real_forwarder + // + class as_real_forwarder {}; + + // + // as_real + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::compost::formats::as_real_forwarder as_real{}; + } // anonymous-namespace + + // + // operator| + // + template + inline SPROUT_CONSTEXPR auto + operator|(Range&& lhs, sprout::compost::formats::as_real_forwarder const& rhs) + -> decltype( + sprout::forward(lhs) + | sprout::adaptors::transformed(sprout::compost::to_real_value()) + ) + { + return sprout::forward(lhs) + | sprout::adaptors::transformed(sprout::compost::to_real_value()) + ; + } + } // namespace formats + + using sprout::compost::formats::as_real; + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_FORMATS_AS_REAL_HPP diff --git a/sprout/compost/utility.hpp b/sprout/compost/utility.hpp new file mode 100644 index 00000000..d9b1318f --- /dev/null +++ b/sprout/compost/utility.hpp @@ -0,0 +1,7 @@ +#ifndef SPROUT_COMPOST_UTILITY_HPP +#define SPROUT_COMPOST_UTILITY_HPP + +#include +#include + +#endif // #ifndef SPROUT_COMPOST_UTILITY_HPP diff --git a/sprout/compost/utility/equal_temperament.hpp b/sprout/compost/utility/equal_temperament.hpp new file mode 100644 index 00000000..ea684643 --- /dev/null +++ b/sprout/compost/utility/equal_temperament.hpp @@ -0,0 +1,28 @@ +#ifndef SPROUT_COMPOST_UTILITY_EQUAL_TEMPERAMENT_HPP +#define SPROUT_COMPOST_UTILITY_EQUAL_TEMPERAMENT_HPP + +#include +#include +#include + +namespace sprout { + namespace compost { + // + // equal_temperament_value + // + template + SPROUT_CONSTEXPR typename sprout::float_promote::type + equal_temperament_value(ArithmeticType1 i, ArithmeticType2 div) { + typedef typename sprout::float_promote::type type; + using sprout::pow; + return pow(type(2), type(i) / type(div)); + } + template + SPROUT_CONSTEXPR typename sprout::float_promote::type + equal_temperament_value(ArithmeticType i) { + return sprout::compost::equal_temperament_value(i, 12); + } + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_UTILITY_EQUAL_TEMPERAMENT_HPP diff --git a/sprout/functional/mem_fn.hpp b/sprout/functional/mem_fn.hpp index 4253a211..35a787c6 100644 --- a/sprout/functional/mem_fn.hpp +++ b/sprout/functional/mem_fn.hpp @@ -48,7 +48,7 @@ namespace sprout { private: typedef Res (Class::*functor)(Args...); private: - functor pmf_; + Res (Class::*pmf_)(Args...); private: template Res call(T& object, Class const volatile*, Args... args) const { @@ -59,7 +59,7 @@ namespace sprout { return ((*ptr).*pmf_)(sprout::forward(args)...); } public: - explicit SPROUT_CONSTEXPR mem_fn_adaptor(functor pmf) + explicit SPROUT_CONSTEXPR mem_fn_adaptor(Res (Class::*pmf)(Args...)) : pmf_(pmf) {} Res operator()(Class& object, Args... args) const { @@ -82,7 +82,7 @@ namespace sprout { private: typedef Res (Class::*functor)(Args...) const; private: - functor pmf_; + Res (Class::*pmf_)(Args...) const; private: template SPROUT_CONSTEXPR Res call(T const& object, Class const volatile*, Args... args) const { @@ -93,7 +93,7 @@ namespace sprout { return ((*ptr).*pmf_)(sprout::forward(args)...); } public: - explicit SPROUT_CONSTEXPR mem_fn_adaptor(functor pmf) + explicit SPROUT_CONSTEXPR mem_fn_adaptor(Res (Class::*pmf)(Args...) const) : pmf_(pmf) {} SPROUT_CONSTEXPR Res operator()(Class const& object, Args... args) const { @@ -116,7 +116,7 @@ namespace sprout { private: typedef Res (Class::*functor)(Args...) volatile; private: - functor pmf_; + Res (Class::*pmf_)(Args...) volatile; private: template Res call(T& object, Class const volatile*, Args... args) const { @@ -127,7 +127,7 @@ namespace sprout { return ((*ptr).*pmf_)(sprout::forward(args)...); } public: - explicit SPROUT_CONSTEXPR mem_fn_adaptor(functor pmf) + explicit SPROUT_CONSTEXPR mem_fn_adaptor(Res (Class::*pmf)(Args...) volatile) : pmf_(pmf) {} Res operator()(Class volatile& object, Args... args) const { @@ -150,7 +150,7 @@ namespace sprout { private: typedef Res (Class::*functor)(Args...) const volatile; private: - functor pmf_; + Res (Class::*pmf_)(Args...) const volatile; private: template SPROUT_CONSTEXPR Res call(T const& object, Class const volatile*, Args... args) const { @@ -161,7 +161,7 @@ namespace sprout { return ((*ptr).*pmf_)(sprout::forward(args)...); } public: - explicit SPROUT_CONSTEXPR mem_fn_adaptor(functor pmf) + explicit SPROUT_CONSTEXPR mem_fn_adaptor(Res (Class::*pmf)(Args...) const volatile) : pmf_(pmf) {} SPROUT_CONSTEXPR Res operator()(Class const volatile& object, Args... args) const { diff --git a/sprout/iterator/clamp_iterator.hpp b/sprout/iterator/clamp_iterator.hpp index ee2f31dc..058e7c76 100644 --- a/sprout/iterator/clamp_iterator.hpp +++ b/sprout/iterator/clamp_iterator.hpp @@ -34,14 +34,14 @@ namespace sprout { typedef typename std::iterator_traits::reference reference; protected: iterator_type current; - compare_type comp; + Compare comp; value_type low; value_type up; private: public: clamp_iterator() = default; clamp_iterator(clamp_iterator const&) = default; - SPROUT_CONSTEXPR clamp_iterator(iterator_type it, value_type const& low, value_type const& up, compare_type comp = compare_type()) + SPROUT_CONSTEXPR clamp_iterator(iterator_type it, value_type const& low, value_type const& up, Compare comp = Compare()) : current(it), comp(comp), low(low), up(up) {} template @@ -57,7 +57,7 @@ namespace sprout { SPROUT_CONSTEXPR iterator_type const& base() const { return current; } - SPROUT_CONSTEXPR compare_type const& compare() const { + SPROUT_CONSTEXPR Compare const& compare() const { return comp; } SPROUT_CONSTEXPR value_type const& lower() const { diff --git a/sprout/iterator/dft.hpp b/sprout/iterator/dft.hpp new file mode 100644 index 00000000..2ca894a5 --- /dev/null +++ b/sprout/iterator/dft.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ITERATOR_DFT_HPP +#define SPROUT_ITERATOR_DFT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ITERATOR_DFT_HPP diff --git a/sprout/iterator/dft_iterator.hpp b/sprout/iterator/dft_iterator.hpp new file mode 100644 index 00000000..3d400eaa --- /dev/null +++ b/sprout/iterator/dft_iterator.hpp @@ -0,0 +1,213 @@ +#ifndef SPROUT_ITERATOR_DFT_ITERATOR_HPP +#define SPROUT_ITERATOR_DFT_ITERATOR_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + // + // dft_iterator + // + template + class dft_iterator + : public std::iterator< + std::random_access_iterator_tag, + typename std::iterator_traits::value_type, + typename std::iterator_traits::difference_type, + void, + typename std::iterator_traits::value_type + > + { + private: + typedef std::iterator< + std::random_access_iterator_tag, + typename std::iterator_traits::value_type, + typename std::iterator_traits::difference_type, + void, + typename std::iterator_traits::value_type + > base_type; + public: + typedef Iterator iterator_type; + typedef typename base_type::iterator_category iterator_category; + typedef typename base_type::value_type value_type; + typedef typename base_type::difference_type difference_type; + typedef typename base_type::pointer pointer; + typedef typename base_type::reference reference; + private: + iterator_type first_; + iterator_type last_; + difference_type index_; + difference_type size_; + private: + SPROUT_CONSTEXPR dft_iterator(iterator_type first, iterator_type last, difference_type index, difference_type size) + : first_(first), last_(last), index_(index) + , size_(size) + {} + public: + SPROUT_CONSTEXPR dft_iterator() + : first_(), last_(), index_() + , size_() + {} + dft_iterator(dft_iterator const&) = default; + SPROUT_CONSTEXPR dft_iterator(iterator_type first, iterator_type last, difference_type index = 0) + : first_(first), last_(last), index_(index) + , size_(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last)) + {} + SPROUT_CONSTEXPR dft_iterator next() const { + return dft_iterator(first_, last_, index_, index_ + 1, size_); + } + SPROUT_CONSTEXPR dft_iterator prev() const { + return dft_iterator(first_, last_, index_, index_ - 1, size_); + } + void swap(dft_iterator& other) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(first_, other.first_)) && SPROUT_NOEXCEPT_EXPR(sprout::swap(last_, other.last_))) + { + sprout::swap(first_, other.first_); + sprout::swap(last_, other.last_); + sprout::swap(index_, other.index_); + sprout::swap(size_, other.size_); + } + friend SPROUT_CONSTEXPR bool operator==(dft_iterator const& lhs, dft_iterator const& rhs) { + return lhs.index_ == rhs.index_; + } + friend SPROUT_CONSTEXPR bool operator!=(dft_iterator const& lhs, dft_iterator const& rhs) { + return !(lhs == rhs); + } + friend SPROUT_CONSTEXPR bool operator<(dft_iterator const& lhs, dft_iterator const& rhs) { + return lhs.index_ < rhs.index_; + } + friend SPROUT_CONSTEXPR bool operator>(dft_iterator const& lhs, dft_iterator const& rhs) { + return rhs < lhs; + } + friend SPROUT_CONSTEXPR bool operator<=(dft_iterator const& lhs, dft_iterator const& rhs) { + return !(rhs < lhs); + } + friend SPROUT_CONSTEXPR bool operator>=(dft_iterator const& lhs, dft_iterator const& rhs) { + return !(lhs < rhs); + } + SPROUT_CONSTEXPR reference operator*() const { + return sprout::detail::dft_element_impl(first_, last_, index_, size_); + } + dft_iterator& operator++() { + dft_iterator temp(next()); + temp.swap(*this); + return *this; + } + dft_iterator operator++(int) { + dft_iterator result(*this); + ++*this; + return result; + } + dft_iterator& operator--() { + dft_iterator temp(prev()); + temp.swap(*this); + return *this; + } + dft_iterator operator--(int) { + dft_iterator result(*this); + --*this; + return result; + } + SPROUT_CONSTEXPR dft_iterator operator+(difference_type n) const { + return dft_iterator(first_, last_, index_, index_ + n, size_); + } + SPROUT_CONSTEXPR dft_iterator operator-(difference_type n) const { + return dft_iterator(first_, last_, index_, index_ - n, size_); + } + dft_iterator& operator+=(difference_type n) { + dft_iterator temp(first_, last_, index_, index_ + n, size_); + temp.swap(*this); + return *this; + } + dft_iterator& operator-=(difference_type n) { + dft_iterator temp(first_, last_, index_, index_ - n, size_); + temp.swap(*this); + return *this; + } + SPROUT_CONSTEXPR reference operator[](difference_type n) const { + return sprout::detail::dft_element_impl(first_, last_, index_ + n, size_); + } + friend SPROUT_CONSTEXPR difference_type operator-(dft_iterator const& lhs, dft_iterator const& rhs) { + return lhs.index_ - rhs.index_; + } + friend SPROUT_CONSTEXPR dft_iterator operator+(difference_type n, dft_iterator const& it) { + return it + n; + } + }; + + // + // make_dft_iterator + // + template + inline SPROUT_CONSTEXPR sprout::dft_iterator + make_dft_iterator(Iterator first, Iterator last, typename std::iterator_traits::difference_type i = 0) { + return sprout::dft_iterator(first, last, i); + } + + // + // swap + // + template + inline void + swap(sprout::dft_iterator& lhs, sprout::dft_iterator& rhs) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) + { + lhs.swap(rhs); + } + + // + // iterator_distance + // + template + inline SPROUT_CONSTEXPR typename std::iterator_traits >::difference_type + iterator_distance(sprout::dft_iterator first, sprout::dft_iterator last) { + return last - first; + } + + // + // iterator_next + // + template + inline SPROUT_CONSTEXPR sprout::dft_iterator + iterator_next(sprout::dft_iterator const& it) { + return it.next(); + } + template + inline SPROUT_CONSTEXPR sprout::dft_iterator + iterator_next( + sprout::dft_iterator const& it, + typename sprout::dft_iterator::difference_type n + ) + { + return it + n; + } + + // + // iterator_prev + // + template + inline SPROUT_CONSTEXPR sprout::dft_iterator + iterator_prev(sprout::dft_iterator const& it) { + return it.prev(); + } + template + inline SPROUT_CONSTEXPR sprout::dft_iterator + iterator_prev( + sprout::dft_iterator const& it, + typename sprout::dft_iterator::difference_type n + ) + { + return it - n; + } +} // namespace sprout + +#endif // #ifndef SPROUT_ITERATOR_DFT_ITERATOR_HPP diff --git a/sprout/iterator/filter_iterator.hpp b/sprout/iterator/filter_iterator.hpp index 71ce73ed..969c2bf1 100644 --- a/sprout/iterator/filter_iterator.hpp +++ b/sprout/iterator/filter_iterator.hpp @@ -43,10 +43,10 @@ namespace sprout { private: struct private_constructor_tag {}; private: - static SPROUT_CONSTEXPR iterator_type find_next(iterator_type first, iterator_type last, predicate_type pred) { + static SPROUT_CONSTEXPR iterator_type find_next(iterator_type first, iterator_type last, Predicate pred) { return sprout::find_if(first, last, pred); } - static SPROUT_CONSTEXPR iterator_type find_prev(iterator_type first, predicate_type pred) { + static SPROUT_CONSTEXPR iterator_type find_prev(iterator_type first, Predicate pred) { return pred(*first) ? first : find_prev(sprout::prev(first), pred) ; @@ -54,7 +54,7 @@ namespace sprout { protected: iterator_type current; iterator_type last; - predicate_type pred; + Predicate pred; private: void satisfy_predicate() { while (current != last && !pred(*current)) { @@ -66,7 +66,7 @@ namespace sprout { --current; } } - SPROUT_CONSTEXPR filter_iterator(predicate_type pred, iterator_type it, iterator_type last, private_constructor_tag) + SPROUT_CONSTEXPR filter_iterator(Predicate pred, iterator_type it, iterator_type last, private_constructor_tag) : current(it) , last(last) , pred(pred) @@ -74,7 +74,7 @@ namespace sprout { public: filter_iterator() = default; filter_iterator(filter_iterator const&) = default; - SPROUT_CONSTEXPR filter_iterator(predicate_type pred, iterator_type it, iterator_type last = iterator_type()) + SPROUT_CONSTEXPR filter_iterator(Predicate pred, iterator_type it, iterator_type last = iterator_type()) : current(find_next(it, last, pred)) , last(last) , pred(pred) @@ -97,7 +97,7 @@ namespace sprout { SPROUT_CONSTEXPR iterator_type end() const { return last; } - SPROUT_CONSTEXPR predicate_type predicate() const { + SPROUT_CONSTEXPR Predicate predicate() const { return pred; } SPROUT_CONSTEXPR reference operator*() const { diff --git a/sprout/iterator/idft_iterator.hpp b/sprout/iterator/idft_iterator.hpp new file mode 100644 index 00000000..2f248b76 --- /dev/null +++ b/sprout/iterator/idft_iterator.hpp @@ -0,0 +1,213 @@ +#ifndef SPROUT_ITERATOR_IDFT_ITERATOR_HPP +#define SPROUT_ITERATOR_IDFT_ITERATOR_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + // + // idft_iterator + // + template + class idft_iterator + : public std::iterator< + std::random_access_iterator_tag, + typename std::iterator_traits::value_type, + typename std::iterator_traits::difference_type, + void, + typename std::iterator_traits::value_type + > + { + private: + typedef std::iterator< + std::random_access_iterator_tag, + typename std::iterator_traits::value_type, + typename std::iterator_traits::difference_type, + void, + typename std::iterator_traits::value_type + > base_type; + public: + typedef Iterator iterator_type; + typedef typename base_type::iterator_category iterator_category; + typedef typename base_type::value_type value_type; + typedef typename base_type::difference_type difference_type; + typedef typename base_type::pointer pointer; + typedef typename base_type::reference reference; + private: + iterator_type first_; + iterator_type last_; + difference_type index_; + difference_type size_; + private: + SPROUT_CONSTEXPR idft_iterator(iterator_type first, iterator_type last, difference_type index, difference_type size) + : first_(first), last_(last), index_(index) + , size_(size) + {} + public: + SPROUT_CONSTEXPR idft_iterator() + : first_(), last_(), index_() + , size_() + {} + idft_iterator(idft_iterator const&) = default; + SPROUT_CONSTEXPR idft_iterator(iterator_type first, iterator_type last, difference_type index = 0) + : first_(first), last_(last), index_(index) + , size_(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last)) + {} + SPROUT_CONSTEXPR idft_iterator next() const { + return idft_iterator(first_, last_, index_, index_ + 1, size_); + } + SPROUT_CONSTEXPR idft_iterator prev() const { + return idft_iterator(first_, last_, index_, index_ - 1, size_); + } + void swap(idft_iterator& other) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(first_, other.first_)) && SPROUT_NOEXCEPT_EXPR(sprout::swap(last_, other.last_))) + { + sprout::swap(first_, other.first_); + sprout::swap(last_, other.last_); + sprout::swap(index_, other.index_); + sprout::swap(size_, other.size_); + } + friend SPROUT_CONSTEXPR bool operator==(idft_iterator const& lhs, idft_iterator const& rhs) { + return lhs.index_ == rhs.index_; + } + friend SPROUT_CONSTEXPR bool operator!=(idft_iterator const& lhs, idft_iterator const& rhs) { + return !(lhs == rhs); + } + friend SPROUT_CONSTEXPR bool operator<(idft_iterator const& lhs, idft_iterator const& rhs) { + return lhs.index_ < rhs.index_; + } + friend SPROUT_CONSTEXPR bool operator>(idft_iterator const& lhs, idft_iterator const& rhs) { + return rhs < lhs; + } + friend SPROUT_CONSTEXPR bool operator<=(idft_iterator const& lhs, idft_iterator const& rhs) { + return !(rhs < lhs); + } + friend SPROUT_CONSTEXPR bool operator>=(idft_iterator const& lhs, idft_iterator const& rhs) { + return !(lhs < rhs); + } + SPROUT_CONSTEXPR reference operator*() const { + return sprout::detail::idft_element_impl(first_, last_, index_, size_); + } + idft_iterator& operator++() { + idft_iterator temp(next()); + temp.swap(*this); + return *this; + } + idft_iterator operator++(int) { + idft_iterator result(*this); + ++*this; + return result; + } + idft_iterator& operator--() { + idft_iterator temp(prev()); + temp.swap(*this); + return *this; + } + idft_iterator operator--(int) { + idft_iterator result(*this); + --*this; + return result; + } + SPROUT_CONSTEXPR idft_iterator operator+(difference_type n) const { + return idft_iterator(first_, last_, index_, index_ + n, size_); + } + SPROUT_CONSTEXPR idft_iterator operator-(difference_type n) const { + return idft_iterator(first_, last_, index_, index_ - n, size_); + } + idft_iterator& operator+=(difference_type n) { + idft_iterator temp(first_, last_, index_, index_ + n, size_); + temp.swap(*this); + return *this; + } + idft_iterator& operator-=(difference_type n) { + idft_iterator temp(first_, last_, index_, index_ - n, size_); + temp.swap(*this); + return *this; + } + SPROUT_CONSTEXPR reference operator[](difference_type n) const { + return sprout::detail::idft_element_impl(first_, last_, index_ + n, size_); + } + friend SPROUT_CONSTEXPR difference_type operator-(idft_iterator const& lhs, idft_iterator const& rhs) { + return lhs.index_ - rhs.index_; + } + friend SPROUT_CONSTEXPR idft_iterator operator+(difference_type n, idft_iterator const& it) { + return it + n; + } + }; + + // + // make_idft_iterator + // + template + inline SPROUT_CONSTEXPR sprout::idft_iterator + make_idft_iterator(Iterator first, Iterator last, typename std::iterator_traits::difference_type i = 0) { + return sprout::idft_iterator(first, last, i); + } + + // + // swap + // + template + inline void + swap(sprout::idft_iterator& lhs, sprout::idft_iterator& rhs) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) + { + lhs.swap(rhs); + } + + // + // iterator_distance + // + template + inline SPROUT_CONSTEXPR typename std::iterator_traits >::difference_type + iterator_distance(sprout::idft_iterator first, sprout::idft_iterator last) { + return last - first; + } + + // + // iterator_next + // + template + inline SPROUT_CONSTEXPR sprout::idft_iterator + iterator_next(sprout::idft_iterator const& it) { + return it.next(); + } + template + inline SPROUT_CONSTEXPR sprout::idft_iterator + iterator_next( + sprout::idft_iterator const& it, + typename sprout::idft_iterator::difference_type n + ) + { + return it + n; + } + + // + // iterator_prev + // + template + inline SPROUT_CONSTEXPR sprout::idft_iterator + iterator_prev(sprout::idft_iterator const& it) { + return it.prev(); + } + template + inline SPROUT_CONSTEXPR sprout::idft_iterator + iterator_prev( + sprout::idft_iterator const& it, + typename sprout::idft_iterator::difference_type n + ) + { + return it - n; + } +} // namespace sprout + +#endif // #ifndef SPROUT_ITERATOR_IDFT_ITERATOR_HPP diff --git a/sprout/iterator/predefined.hpp b/sprout/iterator/predefined.hpp index c51c965d..990f18c4 100644 --- a/sprout/iterator/predefined.hpp +++ b/sprout/iterator/predefined.hpp @@ -5,5 +5,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_ITERATOR_PREDEFINED_HPP diff --git a/sprout/math/cos.hpp b/sprout/math/cos.hpp index 68a2bd63..9d3c82bd 100644 --- a/sprout/math/cos.hpp +++ b/sprout/math/cos.hpp @@ -7,8 +7,8 @@ #include #include #include -#include #include +#include #include namespace sprout { diff --git a/sprout/numeric/dft.hpp b/sprout/numeric/dft.hpp index 7c9c642b..056fcfa0 100644 --- a/sprout/numeric/dft.hpp +++ b/sprout/numeric/dft.hpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #endif // #ifndef SPROUT_NUMERIC_DFT_HPP diff --git a/sprout/numeric/dft/amplitude_spectrum_value.hpp b/sprout/numeric/dft/amplitude_spectrum_value.hpp new file mode 100644 index 00000000..67cda033 --- /dev/null +++ b/sprout/numeric/dft/amplitude_spectrum_value.hpp @@ -0,0 +1,21 @@ +#ifndef SPROUT_NUMERIC_DFT_AMPLITUDE_SPECTRUM_VALUE_HPP +#define SPROUT_NUMERIC_DFT_AMPLITUDE_SPECTRUM_VALUE_HPP + +#include +#include +#include + +namespace sprout { + // + // amplitude_spectrum_value + // + template + inline SPROUT_CONSTEXPR typename T::value_type + amplitude_spectrum_value(T const& x) { + using sprout::real; + using sprout::imag; + return sprout::sqrt(real(x) * real(x) + imag(x) * imag(x)); + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_AMPLITUDE_SPECTRUM_VALUE_HPP diff --git a/sprout/numeric/dft/detail/dft_element_gen.hpp b/sprout/numeric/dft/detail/dft_element_gen.hpp index b20b5465..80e01517 100644 --- a/sprout/numeric/dft/detail/dft_element_gen.hpp +++ b/sprout/numeric/dft/detail/dft_element_gen.hpp @@ -7,12 +7,48 @@ #include #include #include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT namespace sprout { namespace detail { + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::value_type + dft_element_gen_impl_ra( + RandomAccessIterator first, RandomAccessIterator last, + typename std::iterator_traits::value_type::value_type arg, + typename std::iterator_traits::difference_type pivot, + typename std::iterator_traits::difference_type k = 0 + ) + { + typedef typename std::iterator_traits::value_type value_type; + using sprout::cos; + using sprout::sin; + return pivot == 0 ? *first * value_type(cos(arg * k), sin(arg * k)) + : sprout::detail::dft_element_gen_impl_ra( + first, sprout::next(first, pivot), arg, pivot / 2, k + ) + + sprout::detail::dft_element_gen_impl_ra( + sprout::next(first, pivot), last, arg, (NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) - pivot) / 2, k + pivot + ) + ; + } + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::value_type + dft_element_gen_impl( + RandomAccessIterator first, RandomAccessIterator last, + typename std::iterator_traits::value_type::value_type arg, + std::random_access_iterator_tag* + ) + { + typedef typename std::iterator_traits::value_type value_type; + return first == last ? value_type() + : sprout::detail::dft_element_gen_impl_ra(first, last, arg, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2) + ; + } + template inline SPROUT_CONSTEXPR typename std::iterator_traits::value_type - dft_element_gen( + dft_element_gen_impl_1( InputIterator first, InputIterator last, typename std::iterator_traits::value_type::value_type arg, typename std::iterator_traits::difference_type k = 0, @@ -25,18 +61,42 @@ namespace sprout { typedef typename std::iterator_traits::value_type value_type; using sprout::cos; using sprout::sin; - return first != last - ? value + sprout::detail::dft_element_gen( - sprout::next(first), - last, + return first == last ? value + : value + sprout::detail::dft_element_gen_impl_1( + sprout::next(first), last, arg, k + 1, *first * value_type(cos(theta), sin(theta)), arg * (k + 1) ) - : value ; } + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::value_type + dft_element_gen_impl( + InputIterator first, InputIterator last, + typename std::iterator_traits::value_type::value_type arg, + void* + ) + { + return sprout::detail::dft_element_gen_impl_1(first, last, arg); + } + + template + inline SPROUT_CONSTEXPR 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::iterator_category* category; + return sprout::detail::dft_element_gen_impl(first, last, arg, category()); + } } // namespace detail } // namespace sprout diff --git a/sprout/numeric/dft/dft_element.hpp b/sprout/numeric/dft/dft_element.hpp index 794a8936..072cd14c 100644 --- a/sprout/numeric/dft/dft_element.hpp +++ b/sprout/numeric/dft/dft_element.hpp @@ -1,5 +1,5 @@ -#ifndef SPROUT_NUMERIC_DFT_FIXED_DFT_ELEMENT_HPP -#define SPROUT_NUMERIC_DFT_FIXED_DFT_ELEMENT_HPP +#ifndef SPROUT_NUMERIC_DFT_DFT_ELEMENT_HPP +#define SPROUT_NUMERIC_DFT_DFT_ELEMENT_HPP #include #include @@ -38,4 +38,4 @@ namespace sprout { } } // namespace sprout -#endif // #ifndef SPROUT_NUMERIC_DFT_FIXED_DFT_ELEMENT_HPP +#endif // #ifndef SPROUT_NUMERIC_DFT_DFT_ELEMENT_HPP diff --git a/sprout/numeric/dft/fixed/amplitude_spectrum.hpp b/sprout/numeric/dft/fixed/amplitude_spectrum.hpp index 61a62e38..b0c5cfe8 100644 --- a/sprout/numeric/dft/fixed/amplitude_spectrum.hpp +++ b/sprout/numeric/dft/fixed/amplitude_spectrum.hpp @@ -7,8 +7,7 @@ #include #include #include -#include -#include +#include #include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT namespace sprout { @@ -24,16 +23,11 @@ namespace sprout { typename sprout::container_traits::size_type input_size ) { - using sprout::real; - using sprout::imag; return sprout::remake( result, size, (Indexes >= offset && Indexes < offset + size && Indexes < offset + input_size - ? sprout::sqrt( - real(*sprout::next(first, Indexes)) * real(*sprout::next(first, Indexes)) - + imag(*sprout::next(first, Indexes)) * imag(*sprout::next(first, Indexes)) - ) + ? sprout::amplitude_spectrum_value(*sprout::next(first, Indexes)) : *sprout::next(sprout::internal_begin(result), Indexes) )... ); diff --git a/sprout/numeric/dft/fixed/phase_spectrum.hpp b/sprout/numeric/dft/fixed/phase_spectrum.hpp index 73a1f7bc..59b156e2 100644 --- a/sprout/numeric/dft/fixed/phase_spectrum.hpp +++ b/sprout/numeric/dft/fixed/phase_spectrum.hpp @@ -7,9 +7,7 @@ #include #include #include -#include -#include -#include +#include #include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT namespace sprout { @@ -31,10 +29,7 @@ namespace sprout { result, size, (Indexes >= offset && Indexes < offset + size && Indexes < offset + input_size - ? sprout::atan2( - sprout::llround(imag(*sprout::next(first, Indexes))), - sprout::llround(real(*sprout::next(first, Indexes))) - ) + ? sprout::phase_spectrum_value(*sprout::next(first, Indexes)) : *sprout::next(sprout::internal_begin(result), Indexes) )... ); diff --git a/sprout/numeric/dft/idft_element.hpp b/sprout/numeric/dft/idft_element.hpp index 9e55ef28..4b7fa7e5 100644 --- a/sprout/numeric/dft/idft_element.hpp +++ b/sprout/numeric/dft/idft_element.hpp @@ -1,5 +1,5 @@ -#ifndef SPROUT_NUMERIC_DFT_FIXED_IDFT_ELEMENT_HPP -#define SPROUT_NUMERIC_DFT_FIXED_IDFT_ELEMENT_HPP +#ifndef SPROUT_NUMERIC_DFT_IDFT_ELEMENT_HPP +#define SPROUT_NUMERIC_DFT_IDFT_ELEMENT_HPP #include #include @@ -40,4 +40,4 @@ namespace sprout { } } // namespace sprout -#endif // #ifndef SPROUT_NUMERIC_DFT_FIXED_IDFT_ELEMENT_HPP +#endif // #ifndef SPROUT_NUMERIC_DFT_IDFT_ELEMENT_HPP diff --git a/sprout/numeric/dft/phase_spectrum_value.hpp b/sprout/numeric/dft/phase_spectrum_value.hpp new file mode 100644 index 00000000..c2774bf8 --- /dev/null +++ b/sprout/numeric/dft/phase_spectrum_value.hpp @@ -0,0 +1,22 @@ +#ifndef SPROUT_NUMERIC_DFT_PHASE_SPECTRUM_VALUE_HPP +#define SPROUT_NUMERIC_DFT_PHASE_SPECTRUM_VALUE_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // phase_spectrum_value + // + template + inline SPROUT_CONSTEXPR typename T::value_type + phase_spectrum_value(T const& x) { + using sprout::real; + using sprout::imag; + return sprout::atan2(sprout::llround(imag(x)), sprout::llround(real(x))); + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_PHASE_SPECTRUM_VALUE_HPP diff --git a/sprout/numeric/non_modifying.hpp b/sprout/numeric/non_modifying.hpp index d748b72a..ea6b4888 100644 --- a/sprout/numeric/non_modifying.hpp +++ b/sprout/numeric/non_modifying.hpp @@ -4,5 +4,7 @@ #include #include #include +#include +#include #endif // #ifndef SPROUT_NUMERIC_NON_MODIFYIING_HPP diff --git a/sprout/numeric/unstable_accumulate.hpp b/sprout/numeric/unstable_accumulate.hpp new file mode 100644 index 00000000..637f52ab --- /dev/null +++ b/sprout/numeric/unstable_accumulate.hpp @@ -0,0 +1,81 @@ +#ifndef SPROUT_NUMERIC_UNSTABLE_ACCUMLATE_HPP +#define SPROUT_NUMERIC_UNSTABLE_ACCUMLATE_HPP + +#include +#include +#include +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + // + // unstable_accumulate + // + namespace detail { + template + inline SPROUT_CONSTEXPR T + unstable_accumulate_ra( + RandomAccessIterator first, RandomAccessIterator last, BinaryOperation binary_op, + typename std::iterator_traits::difference_type pivot + ) + { + return pivot == 0 ? *first + : binary_op( + sprout::detail::unstable_accumulate_ra( + first, sprout::next(first, pivot), binary_op, + pivot / 2 + ), + sprout::detail::unstable_accumulate_ra( + sprout::next(first, pivot), last, binary_op, + (NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) - pivot) / 2 + ) + ) + ; + } + template + inline SPROUT_CONSTEXPR T + unstable_accumulate( + RandomAccessIterator first, RandomAccessIterator last, T init, BinaryOperation binary_op, + std::random_access_iterator_tag* + ) + { + return first == last ? init + : binary_op( + init, + sprout::detail::unstable_accumulate_ra( + first, last, binary_op, + NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2 + ) + ) + ; + } + + template + inline SPROUT_CONSTEXPR T + unstable_accumulate( + InputIterator first, InputIterator last, T init, BinaryOperation binary_op, + void* + ) + { + return sprout::accumulate(first, last, init, binary_op); + } + } // namespace detail + + template + inline SPROUT_CONSTEXPR T + unstable_accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op) { + typedef typename std::iterator_traits::iterator_category* category; + return sprout::detail::unstable_accumulate(first, last, init, binary_op, category()); + } + + template + inline SPROUT_CONSTEXPR T + unstable_accumulate(InputIterator first, InputIterator last, T init) { + return sprout::unstable_accumulate( + first, last, init, + NS_SSCRISK_CEL_OR_SPROUT::plus::value_type>() + ); + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_UNSTABLE_ACCUMLATE_HPP diff --git a/sprout/numeric/unstable_inner_product.hpp b/sprout/numeric/unstable_inner_product.hpp new file mode 100644 index 00000000..5ace0b0a --- /dev/null +++ b/sprout/numeric/unstable_inner_product.hpp @@ -0,0 +1,89 @@ +#ifndef SPROUT_NUMERIC_UNSTABLE_INNNER_PRODUCT_HPP +#define SPROUT_NUMERIC_UNSTABLE_INNNER_PRODUCT_HPP + +#include +#include +#include +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + // + // unstable_inner_product + // + namespace detail { + template + inline SPROUT_CONSTEXPR T + unstable_inner_product_ra( + RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, + BinaryOperation1 binary_op1, BinaryOperation2 binary_op2, + typename std::iterator_traits::difference_type pivot + ) + { + return pivot == 0 ? binary_op2(*first1, *first2) + : binary_op1( + sprout::detail::unstable_inner_product_ra( + first1, sprout::next(first1, pivot), first2, binary_op1, binary_op2, + pivot / 2 + ), + sprout::detail::unstable_inner_product_ra( + sprout::next(first1, pivot), last1, sprout::next(first2, pivot), binary_op1, binary_op2, + (NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) - pivot) / 2 + ) + ) + ; + } + template + inline SPROUT_CONSTEXPR T + unstable_inner_product( + RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, T init, + BinaryOperation1 binary_op1, BinaryOperation2 binary_op2, + std::random_access_iterator_tag* + ) + { + return first1 == last1 ? init + : binary_op1( + init, + sprout::detail::unstable_inner_product_ra( + first1, last1, first2, binary_op1, binary_op2, + NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) / 2 + ) + ) + ; + } + + template + inline SPROUT_CONSTEXPR T + unstable_inner_product( + InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, + BinaryOperation1 binary_op1, BinaryOperation2 binary_op2, + void* + ) + { + return sprout::inner_product(first1, last1, first2, init, binary_op1, binary_op2); + } + } // namespace detail + + template + inline SPROUT_CONSTEXPR T + unstable_inner_product( + InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, + BinaryOperation1 binary_op1, BinaryOperation2 binary_op2 + ) + { + typedef typename std::iterator_traits::iterator_category* category; + return sprout::detail::unstable_inner_product(first1, last1, first2, init, binary_op1, binary_op2, category()); + } + + template + inline SPROUT_CONSTEXPR T + unstable_inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init) { + return sprout::unstable_inner_product( + first1, last1, first2, init, + sprout::plus::value_type>(), + NS_SSCRISK_CEL_OR_SPROUT::multiplies::value_type>() + ); + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_UNSTABLE_INNNER_PRODUCT_HPP diff --git a/sprout/range/adaptor/amplitude_spectrum.hpp b/sprout/range/adaptor/amplitude_spectrum.hpp new file mode 100644 index 00000000..5f6a860b --- /dev/null +++ b/sprout/range/adaptor/amplitude_spectrum.hpp @@ -0,0 +1,103 @@ +#ifndef SPROUT_RANGE_ADAPTOR_AMPLITUDE_SPECTRUM_HPP +#define SPROUT_RANGE_ADAPTOR_AMPLITUDE_SPECTRUM_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace adaptors { + namespace detail { + template + class amplitude_spectrum_value { + public: + typedef typename T::value_type result_type; + typedef T argument_type; + public: + SPROUT_CONSTEXPR result_type operator()(T const& value) const { + return sprout::amplitude_spectrum_value(value); + } + }; + } // namespace detail + // + // amplitude_spectrum_range + // + template + class amplitude_spectrum_range + : public sprout::adaptors::detail::adapted_range_default< + Range, + sprout::transform_iterator< + sprout::adaptors::detail::amplitude_spectrum_value::value_type>, + typename sprout::container_traits::iterator + > + > + { + public: + typedef sprout::adaptors::detail::adapted_range_default< + Range, + sprout::transform_iterator< + sprout::adaptors::detail::amplitude_spectrum_value::value_type>, + typename sprout::container_traits::iterator + > + > base_type; + typedef typename base_type::range_type range_type; + typedef typename base_type::iterator iterator; + typedef typename base_type::value_type value_type; + public: + amplitude_spectrum_range() = default; + amplitude_spectrum_range(amplitude_spectrum_range const&) = default; + explicit SPROUT_CONSTEXPR amplitude_spectrum_range(range_type& range) + : base_type( + iterator(sprout::begin(range), typename iterator::functor_type()), + iterator(sprout::end(range), typename iterator::functor_type()) + ) + {} + }; + + // + // amplitude_spectrum_forwarder + // + class amplitude_spectrum_forwarder {}; + + // + // amplitude_spectrum + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::adaptors::amplitude_spectrum_forwarder amplitude_spectrum{}; + } // anonymous-namespace + + // + // operator| + // + template + inline SPROUT_CONSTEXPR sprout::adaptors::amplitude_spectrum_range< + typename std::remove_reference::type>::type + > + operator|(Range&& lhs, amplitude_spectrum_forwarder) { + return sprout::adaptors::amplitude_spectrum_range< + typename std::remove_reference::type>::type + >( + sprout::lvalue_forward(lhs) + ); + } + } // namespace adaptors + + // + // container_construct_traits + // + template + struct container_construct_traits > + : public sprout::container_construct_traits::base_type> + {}; +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_ADAPTOR_AMPLITUDE_SPECTRUM_HPP diff --git a/sprout/range/adaptor/clamped.hpp b/sprout/range/adaptor/clamped.hpp index 0f6e2027..d663d1b5 100644 --- a/sprout/range/adaptor/clamped.hpp +++ b/sprout/range/adaptor/clamped.hpp @@ -45,14 +45,14 @@ namespace sprout { range_type& range, value_type const& low, value_type const& up, - compare_type comp = compare_type() + Compare comp = Compare() ) : base_type( iterator(sprout::begin(range), low, up, comp), iterator(sprout::end(range), low, up, comp) ) {} - SPROUT_CONSTEXPR compare_type compare() const { + SPROUT_CONSTEXPR Compare compare() const { return base_type::begin().compare(); } SPROUT_CONSTEXPR value_type lower() const { @@ -72,14 +72,14 @@ namespace sprout { typedef Value value_type; typedef Compare compare_type; private: - compare_type comp_; + Compare comp_; value_type low_; value_type up_; public: - SPROUT_CONSTEXPR clamp_holder(value_type const& low, value_type const& up, compare_type comp = compare_type()) + SPROUT_CONSTEXPR clamp_holder(value_type const& low, value_type const& up, Compare comp = Compare()) : comp_(comp), low_(low), up_(up) {} - SPROUT_CONSTEXPR compare_type const& compare() const { + SPROUT_CONSTEXPR Compare const& compare() const { return comp_; } SPROUT_CONSTEXPR value_type const& lower() const { diff --git a/sprout/range/adaptor/dft.hpp b/sprout/range/adaptor/dft.hpp new file mode 100644 index 00000000..54609757 --- /dev/null +++ b/sprout/range/adaptor/dft.hpp @@ -0,0 +1,83 @@ +#ifndef SPROUT_RANGE_ADAPTOR_DFT_HPP +#define SPROUT_RANGE_ADAPTOR_DFT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace adaptors { + // + // dft_range + // + template + class dft_range + : public sprout::adaptors::detail::adapted_range_default< + Range, + sprout::dft_iterator::iterator> + > + { + public: + typedef sprout::adaptors::detail::adapted_range_default< + Range, + sprout::dft_iterator::iterator> + > base_type; + typedef typename base_type::range_type range_type; + typedef typename base_type::iterator iterator; + public: + dft_range() = default; + dft_range(dft_range const&) = default; + explicit SPROUT_CONSTEXPR dft_range(range_type& range) + : base_type( + iterator(sprout::begin(range), sprout::end(range)), + iterator(sprout::begin(range), sprout::end(range), sprout::size(range)) + ) + {} + }; + + // + // dft_forwarder + // + class dft_forwarder {}; + + // + // dft + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::adaptors::dft_forwarder dft{}; + } // anonymous-namespace + + // + // operator| + // + template + inline SPROUT_CONSTEXPR sprout::adaptors::dft_range< + typename std::remove_reference::type>::type + > + operator|(Range&& lhs, sprout::adaptors::dft_forwarder) { + return sprout::adaptors::dft_range< + typename std::remove_reference::type>::type + >( + sprout::lvalue_forward(lhs) + ); + } + } // namespace adaptors + + // + // container_construct_traits + // + template + struct container_construct_traits > + : public sprout::container_construct_traits::base_type> + {}; +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_ADAPTOR_DFT_HPP diff --git a/sprout/range/adaptor/filtered.hpp b/sprout/range/adaptor/filtered.hpp index 79053821..9b65eebf 100644 --- a/sprout/range/adaptor/filtered.hpp +++ b/sprout/range/adaptor/filtered.hpp @@ -42,7 +42,7 @@ namespace sprout { public: filtered_range() = default; filtered_range(filtered_range const&) = default; - SPROUT_CONSTEXPR filtered_range(predicate_type pred, range_type& range) + SPROUT_CONSTEXPR filtered_range(Predicate pred, range_type& range) : base_type( iterator(pred, sprout::begin(range), sprout::end(range)), iterator(pred, sprout::end(range), sprout::end(range)) @@ -58,14 +58,14 @@ namespace sprout { public: typedef Predicate predicate_type; private: - predicate_type pred_; + Predicate pred_; public: filter_holder() = default; filter_holder(filter_holder const&) = default; - SPROUT_CONSTEXPR filter_holder(predicate_type pred) + SPROUT_CONSTEXPR filter_holder(Predicate pred) : pred_(pred) {} - SPROUT_CONSTEXPR predicate_type predicate() const { + SPROUT_CONSTEXPR Predicate predicate() const { return pred_; } }; diff --git a/sprout/range/adaptor/idft.hpp b/sprout/range/adaptor/idft.hpp new file mode 100644 index 00000000..d0a7fac2 --- /dev/null +++ b/sprout/range/adaptor/idft.hpp @@ -0,0 +1,83 @@ +#ifndef SPROUT_RANGE_ADAPTOR_IDFT_HPP +#define SPROUT_RANGE_ADAPTOR_IDFT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace adaptors { + // + // idft_range + // + template + class idft_range + : public sprout::adaptors::detail::adapted_range_default< + Range, + sprout::idft_iterator::iterator> + > + { + public: + typedef sprout::adaptors::detail::adapted_range_default< + Range, + sprout::idft_iterator::iterator> + > base_type; + typedef typename base_type::range_type range_type; + typedef typename base_type::iterator iterator; + public: + idft_range() = default; + idft_range(idft_range const&) = default; + explicit SPROUT_CONSTEXPR idft_range(range_type& range) + : base_type( + iterator(sprout::begin(range), sprout::end(range)), + iterator(sprout::begin(range), sprout::end(range), sprout::size(range)) + ) + {} + }; + + // + // idft_forwarder + // + class idft_forwarder {}; + + // + // idft + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::adaptors::idft_forwarder idft{}; + } // anonymous-namespace + + // + // operator| + // + template + inline SPROUT_CONSTEXPR sprout::adaptors::idft_range< + typename std::remove_reference::type>::type + > + operator|(Range&& lhs, sprout::adaptors::idft_forwarder) { + return sprout::adaptors::idft_range< + typename std::remove_reference::type>::type + >( + sprout::lvalue_forward(lhs) + ); + } + } // namespace adaptors + + // + // container_construct_traits + // + template + struct container_construct_traits > + : public sprout::container_construct_traits::base_type> + {}; +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_ADAPTOR_IDFT_HPP diff --git a/sprout/range/adaptor/phase_spectrum.hpp b/sprout/range/adaptor/phase_spectrum.hpp new file mode 100644 index 00000000..ef6c0c09 --- /dev/null +++ b/sprout/range/adaptor/phase_spectrum.hpp @@ -0,0 +1,103 @@ +#ifndef SPROUT_RANGE_ADAPTOR_PHASE_SPECTRUM_HPP +#define SPROUT_RANGE_ADAPTOR_PHASE_SPECTRUM_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace adaptors { + namespace detail { + template + class phase_spectrum_value { + public: + typedef typename T::value_type result_type; + typedef T argument_type; + public: + SPROUT_CONSTEXPR result_type operator()(T const& value) const { + return sprout::phase_spectrum_value(value); + } + }; + } // namespace detail + // + // phase_spectrum_range + // + template + class phase_spectrum_range + : public sprout::adaptors::detail::adapted_range_default< + Range, + sprout::transform_iterator< + sprout::adaptors::detail::phase_spectrum_value::value_type>, + typename sprout::container_traits::iterator + > + > + { + public: + typedef sprout::adaptors::detail::adapted_range_default< + Range, + sprout::transform_iterator< + sprout::adaptors::detail::phase_spectrum_value::value_type>, + typename sprout::container_traits::iterator + > + > base_type; + typedef typename base_type::range_type range_type; + typedef typename base_type::iterator iterator; + typedef typename base_type::value_type value_type; + public: + phase_spectrum_range() = default; + phase_spectrum_range(phase_spectrum_range const&) = default; + explicit SPROUT_CONSTEXPR phase_spectrum_range(range_type& range) + : base_type( + iterator(sprout::begin(range), typename iterator::functor_type()), + iterator(sprout::end(range), typename iterator::functor_type()) + ) + {} + }; + + // + // phase_spectrum_forwarder + // + class phase_spectrum_forwarder {}; + + // + // phase_spectrum + // + namespace { + SPROUT_STATIC_CONSTEXPR sprout::adaptors::phase_spectrum_forwarder phase_spectrum{}; + } // anonymous-namespace + + // + // operator| + // + template + inline SPROUT_CONSTEXPR sprout::adaptors::phase_spectrum_range< + typename std::remove_reference::type>::type + > + operator|(Range&& lhs, phase_spectrum_forwarder) { + return sprout::adaptors::phase_spectrum_range< + typename std::remove_reference::type>::type + >( + sprout::lvalue_forward(lhs) + ); + } + } // namespace adaptors + + // + // container_construct_traits + // + template + struct container_construct_traits > + : public sprout::container_construct_traits::base_type> + {}; +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_ADAPTOR_PHASE_SPECTRUM_HPP diff --git a/sprout/range/adaptor/replaced.hpp b/sprout/range/adaptor/replaced.hpp index 49ac513d..ed5101af 100644 --- a/sprout/range/adaptor/replaced.hpp +++ b/sprout/range/adaptor/replaced.hpp @@ -20,7 +20,7 @@ namespace sprout { class replace_value { public: typedef T const& result_type; - typedef T const& first_argument_type; + typedef T const& argument_type; private: T old_; T new_; @@ -61,7 +61,7 @@ namespace sprout { public: replaced_range() = default; replaced_range(replaced_range const&) = default; - explicit SPROUT_CONSTEXPR replaced_range(range_type& range, value_type const& old_value, value_type const& new_value) + SPROUT_CONSTEXPR replaced_range(range_type& range, value_type const& old_value, value_type const& new_value) : base_type( iterator(sprout::begin(range), typename iterator::functor_type(old_value, new_value)), iterator(sprout::end(range), typename iterator::functor_type(old_value, new_value)) diff --git a/sprout/range/adaptor/replaced_if.hpp b/sprout/range/adaptor/replaced_if.hpp index 1eca4714..f017bf94 100644 --- a/sprout/range/adaptor/replaced_if.hpp +++ b/sprout/range/adaptor/replaced_if.hpp @@ -21,7 +21,7 @@ namespace sprout { public: typedef Predicate predicate_type; typedef T const& result_type; - typedef T const& first_argument_type; + typedef T const& argument_type; private: Predicate pred_; T new_; @@ -63,7 +63,7 @@ namespace sprout { public: replaced_if_range() = default; replaced_if_range(replaced_if_range const&) = default; - explicit SPROUT_CONSTEXPR replaced_if_range(range_type& range, predicate_type pred, value_type const& new_value) + SPROUT_CONSTEXPR replaced_if_range(range_type& range, Predicate pred, value_type const& new_value) : base_type( iterator(sprout::begin(range), typename iterator::functor_type(pred, new_value)), iterator(sprout::end(range), typename iterator::functor_type(pred, new_value)) @@ -80,14 +80,14 @@ namespace sprout { typedef Predicate predicate_type; typedef T value_type; private: - predicate_type pred_; + Predicate pred_; value_type new_; public: - SPROUT_CONSTEXPR replace_if_holder(predicate_type pred, value_type const& new_value) + SPROUT_CONSTEXPR replace_if_holder(Predicate pred, value_type const& new_value) : pred_(pred) , new_(new_value) {} - SPROUT_CONSTEXPR predicate_type const& predicate() const { + SPROUT_CONSTEXPR Predicate const& predicate() const { return pred_; } SPROUT_CONSTEXPR value_type const& new_value() const { diff --git a/sprout/range/adaptor/wave.hpp b/sprout/range/adaptor/wave.hpp index c66b9989..98d8a2b5 100644 --- a/sprout/range/adaptor/wave.hpp +++ b/sprout/range/adaptor/wave.hpp @@ -6,5 +6,9 @@ #include #include #include +#include +#include +#include +#include #endif // #ifndef SPROUT_RANGE_ADAPTOR_WAVE_HPP diff --git a/sprout/range/numeric/dft.hpp b/sprout/range/numeric/dft.hpp index 09605168..0c05ae47 100644 --- a/sprout/range/numeric/dft.hpp +++ b/sprout/range/numeric/dft.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #endif // #ifndef SPROUT_RANGE_NUMERIC_DFT_HPP diff --git a/sprout/range/numeric/dft/dft_element.hpp b/sprout/range/numeric/dft/dft_element.hpp new file mode 100644 index 00000000..ca347e36 --- /dev/null +++ b/sprout/range/numeric/dft/dft_element.hpp @@ -0,0 +1,22 @@ +#ifndef SPROUT_RANGE_NUMERIC_DFT_DFT_ELEMENT_HPP +#define SPROUT_RANGE_NUMERIC_DFT_DFT_ELEMENT_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace range { + // + // dft_element + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::value_type + dft_element(Input const& input, typename sprout::container_traits::difference_type i) { + return sprout::dft_element(sprout::begin(input), sprout::end(input), i); + } + } // namespace range +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_NUMERIC_DFT_DFT_ELEMENT_HPP diff --git a/sprout/range/numeric/dft/fit/spectrum.hpp b/sprout/range/numeric/dft/fit/spectrum.hpp index 9cc91d51..1bb12833 100644 --- a/sprout/range/numeric/dft/fit/spectrum.hpp +++ b/sprout/range/numeric/dft/fit/spectrum.hpp @@ -22,4 +22,7 @@ namespace sprout { } // namespace range } // namespace sprout +#include +#include + #endif // #ifndef SPROUT_RANGE_NUMERIC_DFT_FIT_SPECTRUM_HPP diff --git a/sprout/range/numeric/dft/fixed/spectrum.hpp b/sprout/range/numeric/dft/fixed/spectrum.hpp index fb6d0bc2..f3200c27 100644 --- a/sprout/range/numeric/dft/fixed/spectrum.hpp +++ b/sprout/range/numeric/dft/fixed/spectrum.hpp @@ -24,4 +24,7 @@ namespace sprout { } // namespace range } // namespace sprout +#include +#include + #endif // #ifndef SPROUT_RANGE_NUMERIC_DFT_FIXED_SPECTRUM_HPP diff --git a/sprout/range/numeric/dft/idft_element.hpp b/sprout/range/numeric/dft/idft_element.hpp new file mode 100644 index 00000000..fb6c2f18 --- /dev/null +++ b/sprout/range/numeric/dft/idft_element.hpp @@ -0,0 +1,22 @@ +#ifndef SPROUT_RANGE_NUMERIC_DFT_IDFT_ELEMENT_HPP +#define SPROUT_RANGE_NUMERIC_DFT_IDFT_ELEMENT_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace range { + // + // idft_element + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::value_type + idft_element(Input const& input, typename sprout::container_traits::difference_type i) { + return sprout::idft_element(sprout::begin(input), sprout::end(input), i); + } + } // namespace range +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_NUMERIC_DFT_IDFT_ELEMENT_HPP