1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_TYPES_COMPLEX_HPP
12 #define BOOST_COMPUTE_TYPES_COMPLEX_HPP
16 #include <boost/compute/functional.hpp>
17 #include <boost/compute/types/fundamental.hpp>
18 #include <boost/compute/type_traits/make_vector_type.hpp>
19 #include <boost/compute/type_traits/type_name.hpp>
20 #include <boost/compute/detail/meta_kernel.hpp>
27 meta_kernel& operator<<(meta_kernel &kernel, const std::complex<T> &x)
29 typedef typename std::complex<T> value_type;
31 kernel << "(" << type_name<value_type>() << ")"
32 << "(" << x.real() << ", " << x.imag() << ")";
37 // get<N>() result type specialization for std::complex<>
38 template<size_t N, class T>
39 struct get_result_type<N, std::complex<T> >
44 // get<N>() specialization for std::complex<>
45 template<size_t N, class Arg, class T>
46 inline meta_kernel& operator<<(meta_kernel &kernel,
47 const invoked_get<N, Arg, std::complex<T> > &expr)
49 BOOST_STATIC_ASSERT(N < 2);
51 return kernel << expr.m_arg << (N == 0 ? ".x" : ".y");
54 } // end detail namespace
56 // returns the real component of a complex<T>
60 typedef T result_type;
63 detail::invoked_get<0, Arg, std::complex<T> >
64 operator()(const Arg &x) const
66 return detail::invoked_get<0, Arg, std::complex<T> >(x);
70 // returns the imaginary component of a complex<T>
74 typedef T result_type;
77 detail::invoked_get<1, Arg, std::complex<T> >
78 operator()(const Arg &x) const
80 return detail::invoked_get<1, Arg, std::complex<T> >(x);
86 template<class Arg1, class Arg2, class T>
87 struct invoked_complex_multiplies
89 typedef typename std::complex<T> result_type;
91 invoked_complex_multiplies(const Arg1 &x, const Arg2 &y)
101 template<class Arg1, class Arg2, class T>
102 inline meta_kernel& operator<<(meta_kernel &kernel,
103 const invoked_complex_multiplies<Arg1, Arg2, T> &expr)
105 typedef typename std::complex<T> value_type;
107 kernel << "(" << type_name<value_type>() << ")"
108 << "(" << expr.m_x << ".x*" << expr.m_y << ".x-"
109 << expr.m_x << ".y*" << expr.m_y << ".y,"
110 << expr.m_x << ".y*" << expr.m_y << ".x+"
111 << expr.m_x << ".x*" << expr.m_y << ".y" << ")";
116 template<class Arg, class T>
117 struct invoked_complex_conj
119 typedef typename std::complex<T> result_type;
121 invoked_complex_conj(const Arg &arg)
129 template<class Arg, class T>
130 inline meta_kernel& operator<<(meta_kernel &kernel,
131 const invoked_complex_conj<Arg, T> &expr)
133 typedef typename std::complex<T> value_type;
135 kernel << "(" << type_name<value_type>() << ")"
136 << "(" << expr.m_arg << ".x" << ", -" << expr.m_arg << ".y" << ")";
141 } // end detail namespace
143 // specialization for multiplies<T>
145 class multiplies<std::complex<T> > :
146 public function<std::complex<T> (std::complex<T>, std::complex<T>)>
151 std::complex<T> (std::complex<T>, std::complex<T>)
152 >("complex_multiplies")
156 template<class Arg1, class Arg2>
157 detail::invoked_complex_multiplies<Arg1, Arg2, T>
158 operator()(const Arg1 &x, const Arg2 &y) const
160 return detail::invoked_complex_multiplies<Arg1, Arg2, T>(x, y);
164 // returns the complex conjugate of a complex<T>
168 typedef typename std::complex<T> result_type;
171 detail::invoked_complex_conj<Arg, T>
172 operator()(const Arg &x) const
174 return detail::invoked_complex_conj<Arg, T>(x);
180 // type_name() specialization for std::complex
182 struct type_name_trait<std::complex<T> >
184 static const char* value()
186 typedef typename make_vector_type<T, 2>::type vector_type;
188 return type_name<vector_type>();
192 } // end detail namespace
193 } // end compute namespace
194 } // end boost namespace
196 #endif // BOOST_COMPUTE_TYPES_COMPLEX_HPP