//---------------------------------------------------------------------------// // Copyright (c) 2013 Kyle Lutz // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // // See http://boostorg.github.com/compute for more information. //---------------------------------------------------------------------------// #ifndef BOOST_COMPUTE_TYPES_COMPLEX_HPP #define BOOST_COMPUTE_TYPES_COMPLEX_HPP #include #include #include #include #include #include namespace boost { namespace compute { namespace detail { template meta_kernel& operator<<(meta_kernel &kernel, const std::complex &x) { typedef typename std::complex value_type; kernel << "(" << type_name() << ")" << "(" << x.real() << ", " << x.imag() << ")"; return kernel; } // get() result type specialization for std::complex<> template struct get_result_type > { typedef T type; }; // get() specialization for std::complex<> template inline meta_kernel& operator<<(meta_kernel &kernel, const invoked_get > &expr) { BOOST_STATIC_ASSERT(N < 2); return kernel << expr.m_arg << (N == 0 ? ".x" : ".y"); } } // end detail namespace // returns the real component of a complex template struct real { typedef T result_type; template detail::invoked_get<0, Arg, std::complex > operator()(const Arg &x) const { return detail::invoked_get<0, Arg, std::complex >(x); } }; // returns the imaginary component of a complex template struct imag { typedef T result_type; template detail::invoked_get<1, Arg, std::complex > operator()(const Arg &x) const { return detail::invoked_get<1, Arg, std::complex >(x); } }; namespace detail { template struct invoked_complex_multiplies { typedef typename std::complex result_type; invoked_complex_multiplies(const Arg1 &x, const Arg2 &y) : m_x(x), m_y(y) { } Arg1 m_x; Arg2 m_y; }; template inline meta_kernel& operator<<(meta_kernel &kernel, const invoked_complex_multiplies &expr) { typedef typename std::complex value_type; kernel << "(" << type_name() << ")" << "(" << expr.m_x << ".x*" << expr.m_y << ".x-" << expr.m_x << ".y*" << expr.m_y << ".y," << expr.m_x << ".y*" << expr.m_y << ".x+" << expr.m_x << ".x*" << expr.m_y << ".y" << ")"; return kernel; } template struct invoked_complex_conj { typedef typename std::complex result_type; invoked_complex_conj(const Arg &arg) : m_arg(arg) { } Arg m_arg; }; template inline meta_kernel& operator<<(meta_kernel &kernel, const invoked_complex_conj &expr) { typedef typename std::complex value_type; kernel << "(" << type_name() << ")" << "(" << expr.m_arg << ".x" << ", -" << expr.m_arg << ".y" << ")"; return kernel; } } // end detail namespace // specialization for multiplies template class multiplies > : public function (std::complex, std::complex)> { public: multiplies() : function< std::complex (std::complex, std::complex) >("complex_multiplies") { } template detail::invoked_complex_multiplies operator()(const Arg1 &x, const Arg2 &y) const { return detail::invoked_complex_multiplies(x, y); } }; // returns the complex conjugate of a complex template struct conj { typedef typename std::complex result_type; template detail::invoked_complex_conj operator()(const Arg &x) const { return detail::invoked_complex_conj(x); } }; namespace detail { // type_name() specialization for std::complex template struct type_name_trait > { static const char* value() { typedef typename make_vector_type::type vector_type; return type_name(); } }; } // end detail namespace } // end compute namespace } // end boost namespace #endif // BOOST_COMPUTE_TYPES_COMPLEX_HPP