My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
/*
* Copyright 2008-2009 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Copyright (c) 2010, The Regents of the University of California,
* through Lawrence Berkeley National Laboratory (subject to receipt of
* any required approvals from U.S. Dept. of Energy) All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* * Neither the name of the University of California,
* Berkeley, nor the names of its contributors may be used to
* endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/

/*! \file complex.h
* \brief Complex numbers
*/

#pragma once

#include <cusp/detail/config.h>

#if (defined THRUST_DEVICE_BACKEND && THRUST_DEVICE_BACKEND == THRUST_DEVICE_BACKEND_CUDA) || (defined THRUST_DEVICE_SYSTEM && THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA)

#ifdef _WIN32
#define _USE_MATH_DEFINES 1 // make sure M_PI is defined
#endif

#include <math.h>
#include <complex>
#include <cuComplex.h>
#include <sstream>
#include <cusp/cmath.h>

namespace cusp
{

template <typename ValueType> struct complex;
template <> struct complex<float>;
template <> struct complex<double>;


/// Returns the magnitude of z.
template<typename ValueType> ValueType abs(const complex<ValueType>& z);
/// Returns the phase angle of z.
template<typename ValueType> ValueType arg(const complex<ValueType>& z);
/// Returns the magnitude of z squared.
template<typename ValueType> ValueType norm(const complex<ValueType>& z);

/// Returns the complex conjugate of z.
template<typename ValueType> complex<ValueType> conj(const complex<ValueType>& z);
/// Returns the complex with magnitude m and angle theta in radians.
template<typename ValueType> complex<ValueType> polar(const ValueType& m, const ValueType& theta = 0);

// Arithmetic operators:
// Multiplication
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator*(const complex<ValueType>& lhs, const complex<ValueType>& rhs);
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator*(const complex<ValueType>& lhs, const ValueType & rhs);
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator*(const ValueType& lhs, const complex<ValueType>& rhs);
// Division
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator/(const complex<ValueType>& lhs, const complex<ValueType>& rhs);
template <>
__host__ __device__
inline complex<float> operator/(const complex<float>& lhs, const complex<float>& rhs);
template <>
__host__ __device__
inline complex<double> operator/(const complex<double>& lhs, const complex<double>& rhs);

// Addition
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const complex<ValueType>& lhs, const complex<ValueType>& rhs);
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const complex<ValueType>& lhs, const ValueType & rhs);
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const ValueType& lhs, const complex<ValueType>& rhs);
// Subtraction
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const complex<ValueType>& lhs, const complex<ValueType>& rhs);
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const complex<ValueType>& lhs, const ValueType & rhs);
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const ValueType& lhs, const complex<ValueType>& rhs);

// Unary plus and minus
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const complex<ValueType>& rhs);
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const complex<ValueType>& rhs);

// Transcendentals:
// Returns the complex cosine of z.
template<typename ValueType> complex<ValueType> cos(const complex<ValueType>& z);
// Returns the complex hyperbolic cosine of z.
template<typename ValueType> complex<ValueType> cosh(const complex<ValueType>& z);
// Returns the complex base e exponential of z.
template<typename ValueType> complex<ValueType> exp(const complex<ValueType>& z);
// Returns the complex natural logarithm of z.
template<typename ValueType> complex<ValueType> log(const complex<ValueType>& z);
// Returns the complex base 10 logarithm of z.
template<typename ValueType> complex<ValueType> log10(const complex<ValueType>& z);
// Returns z to the n'th power.
template<typename ValueType> complex<ValueType> pow(const complex<ValueType>& z, const int& n);
// Returns z to the x'th power.
template<typename ValueType> complex<ValueType> pow(const complex<ValueType>&z, const ValueType&x);
// Returns z to the z2'th power.
template<typename ValueType> complex<ValueType> pow(const complex<ValueType>&z,
const complex<ValueType>&z2);
// Returns x to the z'th power.
template<typename ValueType> complex<ValueType> pow(const ValueType& x, const complex<ValueType>& z);
// Returns the complex sine of z.
template<typename ValueType> complex<ValueType> sin(const complex<ValueType>&z);
// Returns the complex hyperbolic sine of z.
template<typename ValueType> complex<ValueType> sinh(const complex<ValueType>&z);
// Returns the complex square root of z.
template<typename ValueType> complex<ValueType> sqrt(const complex<ValueType>&z);
// Returns the complex tangent of z.
template<typename ValueType> complex<ValueType> tan(const complex<ValueType>&z);
// Returns the complex hyperbolic tangent of z.
template<typename ValueType> complex<ValueType> tanh(const complex<ValueType>&z);


// Inverse Trigonometric:
// Returns the complex arc cosine of z.
template<typename ValueType> complex<ValueType> acos(const complex<ValueType>& z);
// Returns the complex arc sine of z.
template<typename ValueType> complex<ValueType> asin(const complex<ValueType>& z);
// Returns the complex arc tangent of z.
template<typename ValueType> complex<ValueType> atan(const complex<ValueType>& z);
// Returns the complex hyperbolic arc cosine of z.
template<typename ValueType> complex<ValueType> acosh(const complex<ValueType>& z);
// Returns the complex hyperbolic arc sine of z.
template<typename ValueType> complex<ValueType> asinh(const complex<ValueType>& z);
// Returns the complex hyperbolic arc tangent of z.
template<typename ValueType> complex<ValueType> atanh(const complex<ValueType>& z);



// Stream operators:
template<typename ValueType,class charT, class traits>
std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const complex<ValueType>& z);
template<typename ValueType, typename charT, class traits>
std::basic_istream<charT, traits>&
operator>>(std::basic_istream<charT, traits>& is, complex<ValueType>& z);


// Stream operators
template<typename ValueType,class charT, class traits>
std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const complex<ValueType>& z)
{
os << '(' << z.real() << ',' << z.imag() << ')';
return os;
};

template<typename ValueType, typename charT, class traits>
std::basic_istream<charT, traits>&
operator>>(std::basic_istream<charT, traits>& is, complex<ValueType>& z)
{
ValueType re, im;

charT ch;
is >> ch;

if(ch == '(')
{
is >> re >> ch;
if (ch == ',')
{
is >> im >> ch;
if (ch == ')')
{
z = complex<ValueType>(re, im);
}
else
{
is.setstate(std::ios_base::failbit);
}
}
else if (ch == ')')
{
z = re;
}
else
{
is.setstate(std::ios_base::failbit);
}
}
else
{
is.putback(ch);
is >> re;
z = re;
}
return is;
}

template <typename T>
struct norm_type {
typedef T type;
};
template <typename T>
struct norm_type< complex<T> > {
typedef T type;
};

template <typename ValueType>
struct complex
{
public:
typedef ValueType value_type;

// Constructors
__host__ __device__
inline complex<ValueType>(const ValueType & re = ValueType(), const ValueType& im = ValueType())
{
real(re);
imag(im);
}

template <class X>
__host__ __device__
inline complex<ValueType>(const complex<X> & z)
{
real(z.real());
imag(z.imag());
}

template <class X>
__host__ __device__
inline complex<ValueType>(const std::complex<X> & z)
{
real(z.real());
imag(z.imag());
}

template <typename T>
__host__ __device__
inline complex<ValueType>& operator=(const complex<T> z)
{
real(z.real());
imag(z.imag());
return *this;
}

__host__ __device__
inline complex<ValueType>& operator+=(const complex<ValueType> z)
{
real(real()+z.real());
imag(imag()+z.imag());
return *this;
}

__host__ __device__
inline complex<ValueType>& operator-=(const complex<ValueType> z)
{
real(real()-z.real());
imag(imag()-z.imag());
return *this;
}

__host__ __device__
inline complex<ValueType>& operator*=(const complex<ValueType> z)
{
*this = *this * z;
return *this;
}

__host__ __device__
inline complex<ValueType>& operator/=(const complex<ValueType> z)
{
*this = *this / z;
return *this;
}

__host__ __device__ inline ValueType real() const volatile;
__host__ __device__ inline ValueType imag() const volatile;
__host__ __device__ inline ValueType real() const;
__host__ __device__ inline ValueType imag() const;
__host__ __device__ inline void real(ValueType) volatile;
__host__ __device__ inline void imag(ValueType) volatile;
__host__ __device__ inline void real(ValueType);
__host__ __device__ inline void imag(ValueType);
};

// TODO make cuFloatComplex and cuDoubleComplex protected
// TODO see if returning references is a perf hazard

template<>
struct complex <float> : public cuFloatComplex
{
public:
typedef float value_type;
__host__ __device__
inline complex<float>(){};
__host__ __device__
inline complex<float>(const float & re, const float& im = float())
{
real(re);
imag(im);
}

// For some reason having the following constructor
// explicitly makes things faster with at least g++
__host__ __device__
complex<float>(const complex<float> & z){
// : cuFloatComplex(z){
real(z.real());
imag(z.imag());
}

__host__ __device__
complex<float>(cuFloatComplex z){
// : cuFloatComplex(z){
real(z.x);
imag(z.y);
}

template <class X>
inline complex<float>(const std::complex<X> & z)
{
real(z.real());
imag(z.imag());
}

// Member operators
template <typename T>
__host__ __device__
inline volatile complex<float>& operator=(const complex<T> z) volatile
{
real(z.real());
imag(z.imag());
return *this;
}

template <typename T>
__host__ __device__
inline complex<float>& operator=(const complex<T> z)
{
real(z.real());
imag(z.imag());
return *this;
}

__host__ __device__
inline complex<float>& operator+=(const complex<float> z)
{
real(real()+z.real());
imag(imag()+z.imag());
return *this;
}

__host__ __device__
inline complex<float>& operator-=(const complex<float> z)
{
real(real()-z.real());
imag(imag()-z.imag());
return *this;
}

__host__ __device__
inline complex<float>& operator*=(const complex<float> z)
{
*this = *this * z;
return *this;
}

__host__ __device__
inline complex<float>& operator/=(const complex<float> z)
{
*this = *this / z;
return *this;
}

// Let the compiler synthesize the copy and assignment operators.
__host__ __device__ inline complex<float>(const volatile complex<float> & z)
{
real(z.real());
imag(z.imag());
}

__host__ __device__ inline float real() const volatile{ return x; }
__host__ __device__ inline float imag() const volatile{ return y; }
__host__ __device__ inline float real() const{ return x; }
__host__ __device__ inline float imag() const{ return y; }
__host__ __device__ inline void real(float re)volatile{ x = re; }
__host__ __device__ inline void imag(float im)volatile{ y = im; }
__host__ __device__ inline void real(float re){ x = re; }
__host__ __device__ inline void imag(float im){ y = im; }

// cast operators
inline operator std::complex<float>() const { return std::complex<float>(real(),imag()); }
// inline operator float() const { return real(); }
};

template<>
struct complex <double> : public cuDoubleComplex
{
public:
typedef double value_type;
__host__ __device__
inline complex<double>(){};
__host__ __device__
inline complex<double>(const double & re, const double& im = double())
{
real(re);
imag(im);
}

// For some reason having the following constructor
// explicitly makes things faster with at least g++
__host__ __device__
inline complex<double>(const complex<double> & z){
// : cuDoubleComplex(z) {
real(z.real());
imag(z.imag());
}

__host__ __device__
inline complex<double>(cuDoubleComplex z){
// : cuDoubleComplex(z) {
real(z.x);
imag(z.y);
}

template <class X>
inline complex<double>(const std::complex<X> & z)
{
real(z.real());
imag(z.imag());
}

// Member operators
template <typename T>
__host__ __device__
inline volatile complex<double>& operator=(const complex<T> z) volatile
{
real(z.real());
imag(z.imag());
return *this;
}

template <typename T>
__host__ __device__
inline complex<double>& operator=(const complex<T> z)
{
real(z.real());
imag(z.imag());
return *this;
}

__host__ __device__
inline complex<double>& operator+=(const complex<double> z)
{
real(real()+z.real());
imag(imag()+z.imag());
return *this;
}

__host__ __device__
inline complex<double>& operator-=(const complex<double> z)
{
real(real()-z.real());
imag(imag()-z.imag());
return *this;
}

__host__ __device__
inline complex<double>& operator*=(const complex<double> z)
{
*this = *this * z;
return *this;
}

__host__ __device__
inline complex<double>& operator/=(const complex<double> z)
{
*this = *this / z;
return *this;
}

__host__ __device__ inline complex<double>(const volatile complex<double> & z)
{
real(z.real());
imag(z.imag());
}

// Let the compiler synthesize the copy and assignment operators.
__host__ __device__ inline double real() const volatile { return x; }
__host__ __device__ inline double imag() const volatile { return y; }
__host__ __device__ inline double real() const { return x; }
__host__ __device__ inline double imag() const { return y; }
__host__ __device__ inline void real(double re)volatile{ x = re; }
__host__ __device__ inline void imag(double im)volatile{ y = im; }
__host__ __device__ inline void real(double re){ x = re; }
__host__ __device__ inline void imag(double im){ y = im; }

// cast operators
inline operator std::complex<double>() const { return std::complex<double>(real(),imag()); }
// inline operator double() { return real(); }
};



// Binary arithmetic operations
// At the moment I'm implementing the basic functions, and the
// corresponding cuComplex calls are commented.

template<typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const complex<ValueType>& lhs,
const complex<ValueType>& rhs){
return complex<ValueType>(lhs.real()+rhs.real(),lhs.imag()+rhs.imag());
// return cuCaddf(lhs,rhs);
}

template<typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const volatile complex<ValueType>& lhs,
const volatile complex<ValueType>& rhs){
return complex<ValueType>(lhs.real()+rhs.real(),lhs.imag()+rhs.imag());
// return cuCaddf(lhs,rhs);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const complex<ValueType>& lhs, const ValueType & rhs){
return complex<ValueType>(lhs.real()+rhs,lhs.imag());
// return cuCaddf(lhs,complex<ValueType>(rhs));
}
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const ValueType& lhs, const complex<ValueType>& rhs){
return complex<ValueType>(rhs.real()+lhs,rhs.imag());
// return cuCaddf(complex<float>(lhs),rhs);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const complex<ValueType>& lhs, const complex<ValueType>& rhs){
return complex<ValueType>(lhs.real()-rhs.real(),lhs.imag()-rhs.imag());
// return cuCsubf(lhs,rhs);
}
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const complex<ValueType>& lhs, const ValueType & rhs){
return complex<ValueType>(lhs.real()-rhs,lhs.imag());
// return cuCsubf(lhs,complex<float>(rhs));
}
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const ValueType& lhs, const complex<ValueType>& rhs){
return complex<ValueType>(lhs-rhs.real(),-rhs.imag());
// return cuCsubf(complex<float>(lhs),rhs);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator*(const complex<ValueType>& lhs,
const complex<ValueType>& rhs){
return complex<ValueType>(lhs.real()*rhs.real()-lhs.imag()*rhs.imag(),
lhs.real()*rhs.imag()+lhs.imag()*rhs.real());
// return cuCmulf(lhs,rhs);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator*(const complex<ValueType>& lhs, const ValueType & rhs){
return complex<ValueType>(lhs.real()*rhs,lhs.imag()*rhs);
// return cuCmulf(lhs,complex<float>(rhs));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator*(const ValueType& lhs, const complex<ValueType>& rhs){
return complex<ValueType>(rhs.real()*lhs,rhs.imag()*lhs);
// return cuCmulf(complex<float>(lhs),rhs);
}


template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator/(const complex<ValueType>& lhs, const complex<ValueType>& rhs){
const ValueType cross_norm = lhs.real() * rhs.real() + lhs.imag() * rhs.imag();
const ValueType rhs_norm = norm(rhs);
return complex<ValueType>(cross_norm/rhs_norm,
(lhs.imag() * rhs.real() - lhs.real() * rhs.imag()) / rhs_norm);
}

template <>
__host__ __device__
inline complex<float> operator/(const complex<float>& lhs, const complex<float>& rhs){
return cuCdivf(lhs,rhs);
}

template <>
__host__ __device__
inline complex<double> operator/(const complex<double>& lhs, const complex<double>& rhs){
return cuCdiv(lhs,rhs);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator/(const complex<ValueType>& lhs, const ValueType & rhs){
return complex<ValueType>(lhs.real()/rhs,lhs.imag()/rhs);
// return cuCdivf(lhs,complex<float>(rhs));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator/(const ValueType& lhs, const complex<ValueType>& rhs){
const ValueType cross_norm = lhs * rhs.real();
const ValueType rhs_norm = norm(rhs);
return complex<ValueType>(cross_norm/rhs_norm,(-lhs.real() * rhs.imag()) / rhs_norm);
}

template <>
__host__ __device__
inline complex<float> operator/(const float& lhs, const complex<float>& rhs){
return cuCdivf(complex<float>(lhs),rhs);
}
template <>
__host__ __device__
inline complex<double> operator/(const double& lhs, const complex<double>& rhs){
return cuCdiv(complex<double>(lhs),rhs);
}


// Unary arithmetic operations
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator+(const complex<ValueType>& rhs){
return rhs;
}
template <typename ValueType>
__host__ __device__
inline complex<ValueType> operator-(const complex<ValueType>& rhs){
return rhs*-ValueType(1);
}

// Equality operators
template <typename ValueType>
__host__ __device__
inline bool operator==(const complex<ValueType>& lhs, const complex<ValueType>& rhs){
if(lhs.real() == rhs.real() && lhs.imag() == rhs.imag()){
return true;
}
return false;
}
template <typename ValueType>
__host__ __device__
inline bool operator==(const ValueType & lhs, const complex<ValueType>& rhs){
if(lhs == rhs.real() && rhs.imag() == 0){
return true;
}
return false;
}
template <typename ValueType>
__host__ __device__
inline bool operator==(const complex<ValueType> & lhs, const ValueType& rhs){
if(lhs.real() == rhs && lhs.imag() == 0){
return true;
}
return false;
}

template <typename ValueType>
__host__ __device__
inline bool operator!=(const complex<ValueType>& lhs, const complex<ValueType>& rhs){
return !(lhs == rhs);
}

template <typename ValueType>
__host__ __device__
inline bool operator!=(const ValueType & lhs, const complex<ValueType>& rhs){
return !(lhs == rhs);
}

template <typename ValueType>
__host__ __device__
inline bool operator!=(const complex<ValueType> & lhs, const ValueType& rhs){
return !(lhs == rhs);
}


template <typename ValueType>
__host__ __device__
inline complex<ValueType> conj(const complex<ValueType>& z){
return complex<ValueType>(z.real(),-z.imag());
}

template <typename ValueType>
__host__ __device__
inline ValueType abs(const complex<ValueType>& z){
return ::hypot(z.real(),z.imag());
}
template <>
__host__ __device__
inline float abs(const complex<float>& z){
return ::hypotf(z.real(),z.imag());
}
template<>
__host__ __device__
inline double abs(const complex<double>& z){
return ::hypot(z.real(),z.imag());
}

template <typename ValueType>
__host__ __device__
inline ValueType arg(const complex<ValueType>& z){
return atan2(z.imag(),z.real());
}
template<>
__host__ __device__
inline float arg(const complex<float>& z){
return atan2f(z.imag(),z.real());
}
template<>
__host__ __device__
inline double arg(const complex<double>& z){
return atan2(z.imag(),z.real());
}

template <typename ValueType>
__host__ __device__
inline ValueType norm(const complex<ValueType>& z){
return z.real()*z.real() + z.imag()*z.imag();
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> polar(const ValueType & m, const ValueType & theta){
return complex<ValueType>(m * ::cos(theta),m * ::sin(theta));
}

template <>
__host__ __device__
inline complex<float> polar(const float & magnitude, const float & angle){
return complex<float>(magnitude * ::cosf(angle),magnitude * ::sinf(angle));
}

template <>
__host__ __device__
inline complex<double> polar(const double & magnitude, const double & angle){
return complex<double>(magnitude * ::cos(angle),magnitude * ::sin(angle));
}

// Transcendental functions implementation
template <typename ValueType>
__host__ __device__
inline complex<ValueType> cos(const complex<ValueType>& z){
const ValueType re = z.real();
const ValueType im = z.imag();
return complex<ValueType>(::cos(re) * ::cosh(im), -::sin(re) * ::sinh(im));
}

template <>
__host__ __device__
inline complex<float> cos(const complex<float>& z){
const float re = z.real();
const float im = z.imag();
return complex<float>(cosf(re) * coshf(im), -sinf(re) * sinhf(im));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> cosh(const complex<ValueType>& z){
const ValueType re = z.real();
const ValueType im = z.imag();
return complex<ValueType>(::cosh(re) * ::cos(im), ::sinh(re) * ::sin(im));
}

template <>
__host__ __device__
inline complex<float> cosh(const complex<float>& z){
const float re = z.real();
const float im = z.imag();
return complex<float>(::coshf(re) * ::cosf(im), ::sinhf(re) * ::sinf(im));
}


template <typename ValueType>
__host__ __device__
inline complex<ValueType> exp(const complex<ValueType>& z){
return polar(::exp(z.real()),z.imag());
}

template <>
__host__ __device__
inline complex<float> exp(const complex<float>& z){
return polar(::expf(z.real()),z.imag());
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> log(const complex<ValueType>& z){
return complex<ValueType>(::log(abs(z)),arg(z));
}

template <>
__host__ __device__
inline complex<float> log(const complex<float>& z){
return complex<float>(::logf(abs(z)),arg(z));
}


template <typename ValueType>
__host__ __device__
inline complex<ValueType> log10(const complex<ValueType>& z){
// Using the explicit literal prevents compile time warnings in
// devices that don't support doubles
return log(z)/ValueType(2.30258509299404568402);
// return log(z)/ValueType(::log(10.0));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> pow(const complex<ValueType>& z, const ValueType & exponent){
return exp(log(z)*exponent);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> pow(const complex<ValueType>& z, const complex<ValueType> & exponent){
return exp(log(z)*exponent);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> pow(const ValueType & x, const complex<ValueType> & exponent){
return exp(::log(x)*exponent);
}

template <>
__host__ __device__
inline complex<float> pow(const float & x, const complex<float> & exponent){
return exp(::logf(x)*exponent);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> pow(const complex<ValueType>& z,const int & exponent){
return exp(log(z)*ValueType(exponent));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> sin(const complex<ValueType>& z){
const ValueType re = z.real();
const ValueType im = z.imag();
return complex<ValueType>(::sin(re) * ::cosh(im), ::cos(re) * ::sinh(im));
}

template <>
__host__ __device__
inline complex<float> sin(const complex<float>& z){
const float re = z.real();
const float im = z.imag();
return complex<float>(::sinf(re) * ::coshf(im), ::cosf(re) * ::sinhf(im));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> sinh(const complex<ValueType>& z){
const ValueType re = z.real();
const ValueType im = z.imag();
return complex<ValueType>(::sinh(re) * ::cos(im), ::cosh(re) * ::sin(im));
}

template <>
__host__ __device__
inline complex<float> sinh(const complex<float>& z){
const float re = z.real();
const float im = z.imag();
return complex<float>(::sinhf(re) * ::cosf(im), ::coshf(re) * ::sinf(im));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> sqrt(const complex<ValueType>& z){
return polar(::sqrt(abs(z)),arg(z)/ValueType(2));
}

template <typename ValueType>
__host__ __device__
inline complex<float> sqrt(const complex<float>& z){
return polar(::sqrtf(abs(z)),arg(z)/float(2));
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> tan(const complex<ValueType>& z){
return sin(z)/cos(z);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> tanh(const complex<ValueType>& z){
// This implementation seems better than the simple sin/cos
return (exp(ValueType(2)*z)-ValueType(1))/(exp(ValueType(2)*z)+ValueType(1));
// return sinh(z)/cosh(z);
}

// Inverse trigonometric functions implementation

template <typename ValueType>
__host__ __device__
inline complex<ValueType> acos(const complex<ValueType>& z){
const complex<ValueType> ret = asin(z);
return complex<ValueType>(ValueType(M_PI/2.0) - ret.real(),-ret.imag());
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> asin(const complex<ValueType>& z){
const complex<ValueType> i(0,1);
return -i*asinh(i*z);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> atan(const complex<ValueType>& z){
const complex<ValueType> i(0,1);
return -i*atanh(i*z);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> acosh(const complex<ValueType>& z){
cusp::complex<ValueType> ret((z.real() - z.imag()) * (z.real() + z.imag()) - ValueType(1.0),
ValueType(2.0) * z.real() * z.imag());
ret = sqrt(ret);
if (z.real() < ValueType(0.0)){
ret = -ret;
}
ret += z;
ret = log(ret);
if (ret.real() < ValueType(0.0)){
ret = -ret;
}
return ret;

/*
cusp::complex<ValueType> ret = log(sqrt(z*z-ValueType(1))+z);
if(ret.real() < 0){
ret.real(-ret.real());
}
return ret;
*/
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> asinh(const complex<ValueType>& z){
return log(sqrt(z*z+ValueType(1))+z);
}

template <typename ValueType>
__host__ __device__
inline complex<ValueType> atanh(const complex<ValueType>& z){
ValueType imag2 = z.imag() * z.imag();
ValueType n = ValueType(1.0) + z.real();
n = imag2 + n * n;

ValueType d = ValueType(1.0) - z.real();
d = imag2 + d * d;
complex<ValueType> ret(ValueType(0.25) * (::log(n) - ::log(d)),0);

d = ValueType(1.0) - z.real() * z.real() - imag2;

ret.imag(ValueType(0.5) * ::atan2(ValueType(2.0) * z.imag(), d));
return ret;
//return (log(ValueType(1)+z)-log(ValueType(1)-z))/ValueType(2);
}

template <typename ValueType>
__host__ __device__
inline complex<float> atanh(const complex<float>& z){
float imag2 = z.imag() * z.imag();
float n = float(1.0) + z.real();
n = imag2 + n * n;

float d = float(1.0) - z.real();
d = imag2 + d * d;
complex<float> ret(float(0.25) * (::logf(n) - ::logf(d)),0);

d = float(1.0) - z.real() * z.real() - imag2;

ret.imag(float(0.5) * ::atan2f(float(2.0) * z.imag(), d));
return ret;
//return (log(ValueType(1)+z)-log(ValueType(1)-z))/ValueType(2);

}

} // end namespace cusp

#else
#include <complex>

namespace cusp
{
using std::complex;
using std::conj;
using std::abs;
using std::arg;
using std::norm;
using std::polar;
using std::cos;
using std::cosh;
using std::exp;
using std::log;
using std::log10;
using std::pow;
using std::sin;
using std::sinh;
using std::sqrt;
using std::tan;
using std::tanh;

using std::acos;
using std::asin;
using std::atan;
#if __cplusplus >= 201103L
using std::acosh;
using std::asinh;
using std::atanh;
#else
template <typename ValueType>
inline complex<ValueType> acosh(const complex<ValueType>& z){
cusp::complex<ValueType> ret((z.real() - z.imag()) * (z.real() + z.imag()) - ValueType(1.0),
ValueType(2.0) * z.real() * z.imag());
ret = sqrt(ret);
if (z.real() < ValueType(0.0)){
ret = -ret;
}
ret += z;
ret = log(ret);
if (ret.real() < ValueType(0.0)){
ret = -ret;
}
return ret;
}
template <typename ValueType>
inline complex<ValueType> asinh(const complex<ValueType>& z){
return log(sqrt(z*z+ValueType(1))+z);
}

template <typename ValueType>
inline complex<ValueType> atanh(const complex<ValueType>& z){
ValueType imag2 = z.imag() * z.imag();
ValueType n = ValueType(1.0) + z.real();
n = imag2 + n * n;

ValueType d = ValueType(1.0) - z.real();
d = imag2 + d * d;
complex<ValueType> ret(ValueType(0.25) * (::log(n) - ::log(d)),0);

d = ValueType(1.0) - z.real() * z.real() - imag2;

ret.imag(ValueType(0.5) * ::atan2(ValueType(2.0) * z.imag(), d));
return ret;
}
#endif


template <typename T>
struct norm_type {
typedef T type;
};

template <typename T>
struct norm_type< complex<T> > {
typedef T type;
};
}
#endif

Change log

d3618b307dd7 by Filipe Maia <filipe.c.maia> on Jun 22, 2012   Diff
Solve "Access violation reading location
0x0000000000000000" errors on Windows when
using complex numbers.

Contributed by Paulo Alcino.
Go to: 
Project members, sign in to write a code review

Older revisions

1b598d9568e4 by Filipe Maia <filipe.c.maia> on Apr 3, 2012   Diff
Add complex support outside of CUDA.
Resolves  issue #87 
3880d39709ef by Filipe Maia <filipe.c.maia> on Jul 26, 2011   Diff
Remove complex to real implicit cast.
Several changes to try to prevent
problems caused by this change.
f17177dbda57 by Filipe Maia <filipe.c.maia> on Jul 26, 2011   Diff
Reintroduce the complex to real
implicit conversion. Too much code was
broken because of it.
It will be removed once the issues it
raises are addressed.
All revisions of this file

File info

Size: 34986 bytes, 1132 lines
Powered by Google Project Hosting