Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
complex.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2014 NVIDIA Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
21 #pragma once
22 
23 #include <cusp/detail/config.h>
24 
25 #include <thrust/functional.h>
26 
27 #if THRUST_VERSION >= 100800
28 #include <thrust/complex.h>
29 #else
30 #include <cusp/detail/thrust/complex.h>
31 #endif
32 
33 namespace cusp
34 {
35 
36 using thrust::complex;
37 
38 /* \cond */
39 template <typename T>
40 struct norm_type
41 {
42  typedef T type;
43 };
44 
45 template <typename T>
46 struct norm_type< cusp::complex<T> >
47 {
48  typedef T type;
49 };
50 /* \endcond */
51 
52 using thrust::conj;
53 template<typename T>
54 __host__ __device__
55 inline T
56 conj(const T& z)
57 {
58  return z;
59 }
60 
61 using thrust::abs;
62 template <typename T>
63 __host__ __device__
64 inline typename cusp::norm_type<T>::type
65 abs(const T& z)
66 {
67  return z > 0 ? z : -z;
68 }
69 
70 using thrust::norm;
71 template <typename T>
72 __host__ __device__
73 inline typename cusp::norm_type<T>::type
74 norm(const T& z)
75 {
76  return cusp::abs(z);
77 }
78 
79 using thrust::sqrt;
80 
81 // define complex type for the purpose of Doxygenating it
82 // it is actually defined in Thrust
83 #if 0
84 
106 template <typename T>
107 struct complex
108 {
109 public:
110 
113  typedef T value_type;
114 
115  /* --- Constructors --- */
116 
122  inline __host__ __device__
123  complex(const T & re = T(), const T& im = T());
124 
132  template <typename X>
133  inline __host__ __device__
134  complex(const complex<X> & z);
135 
143  template <typename X>
144  inline __host__
145  complex(const std::complex<X> & z);
146 
147 
148 
149  /* --- Compound Assignment Operators --- */
150 
156  __host__ __device__
157  inline complex<T>& operator+=(const complex<T> z);
158 
164  __host__ __device__
165  inline complex<T>& operator-=(const complex<T> z);
166 
172  __host__ __device__
173  inline complex<T>& operator*=(const complex<T> z);
174 
180  __host__ __device__
181  inline complex<T>& operator/=(const complex<T> z);
182 
183 
184 
185  /* --- Getter functions ---
186  * The volatile ones are there to help for example
187  * with certain reductions optimizations
188  */
189 
192  __host__ __device__ inline T real() const volatile {
193  return m_data[0];
194  }
195 
198  __host__ __device__ inline T imag() const volatile {
199  return m_data[1];
200  }
201 
204  __host__ __device__ inline T real() const {
205  return m_data[0];
206  }
207 
210  __host__ __device__ inline T imag() const {
211  return m_data[1];
212  }
213 
214 
215 
216  /* --- Setter functions ---
217  * The volatile ones are there to help for example
218  * with certain reductions optimizations
219  */
220 
225  __host__ __device__ inline void real(T re)volatile {
226  m_data[0] = re;
227  }
228 
233  __host__ __device__ inline void imag(T im)volatile {
234  m_data[1] = im;
235  }
236 
241  __host__ __device__ inline void real(T re) {
242  m_data[0] = re;
243  }
244 
249  __host__ __device__ inline void imag(T im) {
250  m_data[1] = im;
251  }
252 
253 
254 
255  /* --- Casting functions --- */
256 
259  inline operator std::complex<T>() const {
260  return std::complex<T>(real(),imag());
261  }
262 
263 private:
264  T m_data[2];
265 };
266 
267 
268 /* --- General Functions --- */
269 
274 template<typename T> __host__ __device__ inline T abs(const complex<T>& z);
275 
280 template<typename T> __host__ __device__ inline T arg(const complex<T>& z);
281 
286 template<typename T> __host__ __device__ inline T norm(const complex<T>& z);
287 
292 template<typename T> __host__ __device__ inline complex<T> conj(const complex<T>& z);
293 
299 template<typename T> __host__ __device__ inline complex<T> polar(const T& m, const T& theta = 0);
300 
308 template<typename T> __host__ __device__ inline complex<T> proj(const T& z);
309 
310 
311 
312 /* --- Binary Arithmetic operators --- */
313 
319 template <typename T> __host__ __device__ inline complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs);
320 
326 template <typename T> __host__ __device__ inline complex<T> operator*(const complex<T>& lhs, const T & rhs);
327 
333 template <typename T> __host__ __device__ inline complex<T> operator*(const T& lhs, const complex<T>& rhs);
334 
340 template <typename T> __host__ __device__ inline complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs);
341 
347 template <typename T> __host__ __device__ inline complex<T> operator/(const complex<T>& lhs, const T & rhs);
348 
354 template <typename T> __host__ __device__ inline complex<T> operator/(const T& lhs, const complex<T> & rhs);
355 
361 template <typename T> __host__ __device__ inline complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs);
362 
368 template <typename T> __host__ __device__ inline complex<T> operator+(const complex<T>& lhs, const T & rhs);
369 
375 template <typename T> __host__ __device__ inline complex<T> operator+(const T& lhs, const complex<T>& rhs);
376 
382 template <typename T> __host__ __device__ inline complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs);
383 
389 template <typename T> __host__ __device__ inline complex<T> operator-(const complex<T>& lhs, const T & rhs);
390 
396 template <typename T> __host__ __device__ inline complex<T> operator-(const T& lhs, const complex<T>& rhs);
397 
398 
399 
400 /* --- Unary Arithmetic operators --- */
401 
406 template <typename T> __host__ __device__ inline complex<T> operator+(const complex<T>& rhs);
407 
412 template <typename T> __host__ __device__ inline complex<T> operator-(const complex<T>& rhs);
413 
414 
415 
416 /* --- Exponential Functions --- */
417 
422 template <typename T> __host__ __device__ complex<T> exp(const complex<T>& z);
423 
428 template <typename T> __host__ __device__ complex<T> log(const complex<T>& z);
429 
434 template <typename T> __host__ __device__ inline complex<T> log10(const complex<T>& z);
435 
436 
437 
438 /* --- Power Functions --- */
439 
445 template <typename T> __host__ __device__ complex<T> pow(const complex<T>& x, const complex<T>& y);
446 
452 template <typename T> __host__ __device__ complex<T> pow(const complex<T>& x, const T& y);
453 
459 template <typename T> __host__ __device__ complex<T> pow(const T& x, const complex<T>& y);
460 
461 #if !defined _MSC_VER
462 
468 template <typename T, typename U> __host__ __device__ complex<typename detail::promoted_numerical_type<T,U>::type > pow(const complex<T>& x, const complex<U>& y);
469 
476 template <typename T, typename U> __host__ __device__ complex<typename detail::promoted_numerical_type<T,U>::type > pow(const complex<T>& x, const U& y);
477 
484 template <typename T, typename U> __host__ __device__ complex<typename detail::promoted_numerical_type<T,U>::type > pow(const T& x,const complex<U>& y);
485 
486 #endif // !defined _MSC_VER
487 
492 template <typename T> __host__ __device__ complex<T> sqrt(const complex<T>&z);
493 
494 
495 
496 /* --- Trigonometric Functions --- */
497 
502 template <typename T> __host__ __device__ complex<T> cos(const complex<T>&z);
503 
508 template <typename T> __host__ __device__ complex<T> sin(const complex<T>&z);
509 
514 template <typename T> __host__ __device__ complex<T> tan(const complex<T>&z);
515 
516 
517 
518 /* --- Hyperbolic Functions --- */
519 
524 template <typename T> __host__ __device__ complex<T> cosh(const complex<T>& z);
525 
530 template <typename T> __host__ __device__ complex<T> sinh(const complex<T>&z);
531 
536 template <typename T> __host__ __device__ complex<T> tanh(const complex<T>&z);
537 
538 
539 
540 /* --- Inverse Trigonometric Functions --- */
541 
549 template <typename T> __host__ __device__ complex<T> acos(const complex<T>& z);
550 
558 template <typename T> __host__ __device__ complex<T> asin(const complex<T>& z);
559 
567 template <typename T> __host__ __device__ complex<T> atan(const complex<T>& z);
568 
569 
570 
571 /* --- Inverse Hyperbolic Functions --- */
572 
580 template <typename T> __host__ __device__ complex<T> acosh(const complex<T>& z);
581 
589 template <typename T> __host__ __device__ complex<T> asinh(const complex<T>& z);
590 
598 template <typename T> __host__ __device__ complex<T> atanh(const complex<T>& z);
599 
600 
601 
602 /* --- Stream Operators --- */
603 
609 template<typename ValueType,class charT, class traits>
610 std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const complex<ValueType>& z);
611 
623 template<typename ValueType, typename charT, class traits>
624 std::basic_istream<charT, traits>&
625 operator>>(std::basic_istream<charT, traits>& is, complex<ValueType>& z);
626 
627 
628 
629 /* --- Equality Operators --- */
630 
636 template <typename T> __host__ __device__ inline bool operator==(const complex<T>& lhs, const complex<T>& rhs);
637 
643 template <typename T> __host__ __device__ inline bool operator==(const T & lhs, const complex<T>& rhs);
644 
650 template <typename T> __host__ __device__ inline bool operator==(const complex<T> & lhs, const T& rhs);
651 
657 template <typename T> __host__ __device__ inline bool operator!=(const complex<T>& lhs, const complex<T>& rhs);
658 
664 template <typename T> __host__ __device__ inline bool operator!=(const T & lhs, const complex<T>& rhs);
665 
671 template <typename T> __host__ __device__ inline bool operator!=(const complex<T> & lhs, const T& rhs);
672 
673 #include <thrust/detail/complex/complex.inl>
674 
675 #endif
676 
680 } // end namespace cusp
681 
682 
683