CUSP
Loading...
Searching...
No Matches
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
33namespace cusp
34{
35
36using thrust::complex;
37
38/* \cond */
39template <typename T>
40struct norm_type
41{
42 typedef T type;
43};
44
45template <typename T>
46struct norm_type< cusp::complex<T> >
47{
48 typedef T type;
49};
50/* \endcond */
51
52using thrust::conj;
54template<typename T>
55_CCCL_HOST_DEVICE
56inline T
57conj(const T& z)
58{
59 return z;
60}
61
62using thrust::abs;
64template <typename T>
65_CCCL_HOST_DEVICE
66inline typename cusp::norm_type<T>::type
67abs(const T& z)
68{
69 return z > 0 ? z : -z;
70}
71
72using thrust::norm;
74template <typename T>
75_CCCL_HOST_DEVICE
76inline typename cusp::norm_type<T>::type
77norm(const T& z)
78{
79 return cusp::abs(z);
80}
81
82using thrust::sqrt;
83
84// define complex type for the purpose of Doxygenating it
85// it is actually defined in Thrust
86#if 0
110template <typename T>
111struct complex
112{
113public:
114
117 typedef T value_type;
118
119 /* --- Constructors --- */
120
126 inline _CCCL_HOST_DEVICE
127 complex(const T & re = T(), const T& im = T());
128
136 template <typename X>
137 inline _CCCL_HOST_DEVICE
138 complex(const complex<X> & z);
139
147 template <typename X>
148 inline _CCCL_HOST
149 complex(const std::complex<X> & z);
150
151
152
153 /* --- Compound Assignment Operators --- */
154
160 _CCCL_HOST_DEVICE
161 inline complex<T>& operator+=(const complex<T> z);
162
168 _CCCL_HOST_DEVICE
169 inline complex<T>& operator-=(const complex<T> z);
170
176 _CCCL_HOST_DEVICE
177 inline complex<T>& operator*=(const complex<T> z);
178
184 _CCCL_HOST_DEVICE
185 inline complex<T>& operator/=(const complex<T> z);
186
187
188
189 /* --- Getter functions ---
190 * The volatile ones are there to help for example
191 * with certain reductions optimizations
192 */
193
196 _CCCL_HOST_DEVICE inline T real() const volatile {
197 return m_data[0];
198 }
199
202 _CCCL_HOST_DEVICE inline T imag() const volatile {
203 return m_data[1];
204 }
205
208 _CCCL_HOST_DEVICE inline T real() const {
209 return m_data[0];
210 }
211
214 _CCCL_HOST_DEVICE inline T imag() const {
215 return m_data[1];
216 }
217
218
219
220 /* --- Setter functions ---
221 * The volatile ones are there to help for example
222 * with certain reductions optimizations
223 */
224
229 _CCCL_HOST_DEVICE inline void real(T re)volatile {
230 m_data[0] = re;
231 }
232
237 _CCCL_HOST_DEVICE inline void imag(T im)volatile {
238 m_data[1] = im;
239 }
240
245 _CCCL_HOST_DEVICE inline void real(T re) {
246 m_data[0] = re;
247 }
248
253 _CCCL_HOST_DEVICE inline void imag(T im) {
254 m_data[1] = im;
255 }
256
257
258
259 /* --- Casting functions --- */
260
263 inline operator std::complex<T>() const {
264 return std::complex<T>(real(),imag());
265 }
266
267private:
268 T m_data[2];
269};
270
271
272/* --- General Functions --- */
273
278template<typename T> _CCCL_HOST_DEVICE inline T abs(const complex<T>& z);
279
284template<typename T> _CCCL_HOST_DEVICE inline T arg(const complex<T>& z);
285
290template<typename T> _CCCL_HOST_DEVICE inline T norm(const complex<T>& z);
291
296template<typename T> _CCCL_HOST_DEVICE inline complex<T> conj(const complex<T>& z);
297
303template<typename T> _CCCL_HOST_DEVICE inline complex<T> polar(const T& m, const T& theta = 0);
304
312template<typename T> _CCCL_HOST_DEVICE inline complex<T> proj(const T& z);
313
314
315
316/* --- Binary Arithmetic operators --- */
317
323template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs);
324
330template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator*(const complex<T>& lhs, const T & rhs);
331
337template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator*(const T& lhs, const complex<T>& rhs);
338
344template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs);
345
351template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator/(const complex<T>& lhs, const T & rhs);
352
358template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator/(const T& lhs, const complex<T> & rhs);
359
365template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs);
366
372template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator+(const complex<T>& lhs, const T & rhs);
373
379template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator+(const T& lhs, const complex<T>& rhs);
380
386template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs);
387
393template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator-(const complex<T>& lhs, const T & rhs);
394
400template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator-(const T& lhs, const complex<T>& rhs);
401
402
403
404/* --- Unary Arithmetic operators --- */
405
410template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator+(const complex<T>& rhs);
411
416template <typename T> _CCCL_HOST_DEVICE inline complex<T> operator-(const complex<T>& rhs);
417
418
419
420/* --- Exponential Functions --- */
421
426template <typename T> _CCCL_HOST_DEVICE complex<T> exp(const complex<T>& z);
427
432template <typename T> _CCCL_HOST_DEVICE complex<T> log(const complex<T>& z);
433
438template <typename T> _CCCL_HOST_DEVICE inline complex<T> log10(const complex<T>& z);
439
440
441
442/* --- Power Functions --- */
443
449template <typename T> _CCCL_HOST_DEVICE complex<T> pow(const complex<T>& x, const complex<T>& y);
450
456template <typename T> _CCCL_HOST_DEVICE complex<T> pow(const complex<T>& x, const T& y);
457
463template <typename T> _CCCL_HOST_DEVICE complex<T> pow(const T& x, const complex<T>& y);
464
465#if !defined _MSC_VER
472template <typename T, typename U> _CCCL_HOST_DEVICE complex<typename detail::promoted_numerical_type<T,U>::type > pow(const complex<T>& x, const complex<U>& y);
473
480template <typename T, typename U> _CCCL_HOST_DEVICE complex<typename detail::promoted_numerical_type<T,U>::type > pow(const complex<T>& x, const U& y);
481
488template <typename T, typename U> _CCCL_HOST_DEVICE complex<typename detail::promoted_numerical_type<T,U>::type > pow(const T& x,const complex<U>& y);
489
490#endif // !defined _MSC_VER
491
496template <typename T> _CCCL_HOST_DEVICE complex<T> sqrt(const complex<T>&z);
497
498
499
500/* --- Trigonometric Functions --- */
501
506template <typename T> _CCCL_HOST_DEVICE complex<T> cos(const complex<T>&z);
507
512template <typename T> _CCCL_HOST_DEVICE complex<T> sin(const complex<T>&z);
513
518template <typename T> _CCCL_HOST_DEVICE complex<T> tan(const complex<T>&z);
519
520
521
522/* --- Hyperbolic Functions --- */
523
528template <typename T> _CCCL_HOST_DEVICE complex<T> cosh(const complex<T>& z);
529
534template <typename T> _CCCL_HOST_DEVICE complex<T> sinh(const complex<T>&z);
535
540template <typename T> _CCCL_HOST_DEVICE complex<T> tanh(const complex<T>&z);
541
542
543
544/* --- Inverse Trigonometric Functions --- */
545
553template <typename T> _CCCL_HOST_DEVICE complex<T> acos(const complex<T>& z);
554
562template <typename T> _CCCL_HOST_DEVICE complex<T> asin(const complex<T>& z);
563
571template <typename T> _CCCL_HOST_DEVICE complex<T> atan(const complex<T>& z);
572
573
574
575/* --- Inverse Hyperbolic Functions --- */
576
584template <typename T> _CCCL_HOST_DEVICE complex<T> acosh(const complex<T>& z);
585
593template <typename T> _CCCL_HOST_DEVICE complex<T> asinh(const complex<T>& z);
594
602template <typename T> _CCCL_HOST_DEVICE complex<T> atanh(const complex<T>& z);
603
604
605
606/* --- Stream Operators --- */
607
613template<typename ValueType,class charT, class traits>
614std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const complex<ValueType>& z);
615
627template<typename ValueType, typename charT, class traits>
628std::basic_istream<charT, traits>&
629operator>>(std::basic_istream<charT, traits>& is, complex<ValueType>& z);
630
631
632
633/* --- Equality Operators --- */
634
640template <typename T> _CCCL_HOST_DEVICE inline bool operator==(const complex<T>& lhs, const complex<T>& rhs);
641
647template <typename T> _CCCL_HOST_DEVICE inline bool operator==(const T & lhs, const complex<T>& rhs);
648
654template <typename T> _CCCL_HOST_DEVICE inline bool operator==(const complex<T> & lhs, const T& rhs);
655
661template <typename T> _CCCL_HOST_DEVICE inline bool operator!=(const complex<T>& lhs, const complex<T>& rhs);
662
668template <typename T> _CCCL_HOST_DEVICE inline bool operator!=(const T & lhs, const complex<T>& rhs);
669
675template <typename T> _CCCL_HOST_DEVICE inline bool operator!=(const complex<T> & lhs, const T& rhs);
676
677#include <thrust/detail/complex/complex.inl>
678
682#endif
683
684} // end namespace cusp
685
686
687
_CCCL_HOST_DEVICE cusp::norm_type< T >::type abs(const T &z)
Compute the absolute value of z.
Definition complex.h:67
_CCCL_HOST_DEVICE cusp::norm_type< T >::type norm(const T &z)
Compute the norm of z.
Definition complex.h:77
_CCCL_HOST_DEVICE T conj(const T &z)
Compute the complex conjugate of z. For real types, returns z unchanged.
Definition complex.h:57