Skip to content

Commit 79fc880

Browse files
committed
std::complex -> af_cfloat/af_cdouble
1 parent 7f7e2fc commit 79fc880

25 files changed

Lines changed: 345 additions & 88 deletions

include/af/complex.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#pragma once
11+
#include "af/defines.h"
12+
13+
14+
#ifdef __cplusplus
15+
#include <ostream>
16+
#include <istream>
17+
18+
namespace af{
19+
#endif
20+
21+
extern "C" {
22+
typedef struct af_cfloat {
23+
float real;
24+
float imag;
25+
#ifdef __cplusplus
26+
af_cfloat(const float real = 0, const float imag = 0) :real(real), imag(imag) {};
27+
#endif
28+
} af_cfloat;
29+
30+
typedef struct af_cdouble {
31+
double real;
32+
double imag;
33+
#ifdef __cplusplus
34+
af_cdouble(const double real = 0, const double imag = 0) :real(real), imag(imag) {}
35+
#endif
36+
} af_cdouble;
37+
}
38+
39+
#ifdef __cplusplus
40+
typedef af::af_cfloat cfloat;
41+
typedef af::af_cdouble cdouble;
42+
43+
AFAPI float real(af_cfloat val);
44+
AFAPI double real(af_cdouble val);
45+
46+
AFAPI float imag(af_cfloat val);
47+
AFAPI double imag(af_cdouble val);
48+
49+
AFAPI af::cfloat operator+(const af::cfloat &lhs, const af::cfloat &rhs);
50+
AFAPI af::cfloat operator+(const af::cfloat &lhs, const double &rhs);
51+
AFAPI af::cdouble operator+(const af::cdouble &lhs, const af::cdouble &rhs);
52+
AFAPI af::cdouble operator+(const af::cdouble &lhs, const double &rhs);
53+
54+
AFAPI af::cfloat operator-(const af::cfloat &lhs, const af::cfloat &rhs);
55+
AFAPI af::cdouble operator-(const af::cdouble &lhs, const af::cdouble &rhs);
56+
57+
AFAPI cfloat operator*(const cfloat &lhs, const cfloat &rhs);
58+
AFAPI cdouble operator*(const cdouble &lhs, const cdouble &rhs);
59+
60+
AFAPI cfloat operator/(const cfloat &lhs, const cfloat &rhs);
61+
AFAPI af::cfloat operator/(const af::cfloat &lhs, const float &rhs);
62+
AFAPI cdouble operator/(const cdouble &lhs, const cdouble &rhs);
63+
AFAPI af::cdouble operator/(const af::cdouble &lhs, const double &rhs);
64+
65+
AFAPI bool operator==(const cfloat &lhs, const cfloat &rhs);
66+
AFAPI bool operator==(const cdouble &lhs, const cdouble &rhs);
67+
68+
AFAPI bool operator!=(const cfloat &lhs, const cfloat &rhs);
69+
AFAPI bool operator!=(const cdouble &lhs, const cdouble &rhs);
70+
71+
AFAPI std::istream& operator>> (std::istream &is, cfloat &in);
72+
AFAPI std::istream& operator>> (std::istream &is, cdouble &in);
73+
74+
AFAPI std::ostream& operator<< (std::ostream &os, const cfloat &in);
75+
AFAPI std::ostream& operator<< (std::ostream &os, const cdouble &in);
76+
77+
#endif
78+
79+
AFAPI float abs(const cfloat &val);
80+
AFAPI double abs(const cdouble &val);
81+
82+
AFAPI cfloat conj(const cfloat &val);
83+
AFAPI cdouble conj(const cdouble &val);
84+
85+
#ifdef __cplusplus
86+
}
87+
#endif

include/af/data.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ namespace af
1818
class array;
1919

2020
template<typename T>
21-
AFAPI array constant(T val, const dim4 &dims, const dtype ty=(af_dtype)dtype_traits<T>::ctype);
21+
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype)dtype_traits<T>::ctype);
2222

2323
template<typename T>
24-
AFAPI array constant(T val, const dim_t d0, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
24+
array constant(T val, const dim_t d0, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
2525

2626
template<typename T>
27-
AFAPI array constant(T val, const dim_t d0, const dim_t d1, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
27+
array constant(T val, const dim_t d0, const dim_t d1, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
2828

2929
template<typename T>
30-
AFAPI array constant(T val, const dim_t d0, const dim_t d1, const dim_t d2, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
30+
array constant(T val, const dim_t d0, const dim_t d1, const dim_t d2, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
3131

3232
template<typename T>
33-
AFAPI array constant(T val, const dim_t d0, const dim_t d1, const dim_t d2, const dim_t d3, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
33+
array constant(T val, const dim_t d0, const dim_t d1, const dim_t d2, const dim_t d3, const af_dtype ty=(af_dtype)dtype_traits<T>::ctype);
3434

3535
/**
3636
\param[in] dims is the dimensions of the array to be generated

include/af/defines.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,8 @@
4949
typedef long long dim_t;
5050
#endif
5151

52-
#ifdef __cplusplus
53-
#include <complex>
54-
#include <cstddef>
55-
56-
typedef std::complex<float> af_cfloat;
57-
typedef std::complex<double> af_cdouble;
58-
59-
#else
6052
#include <stdlib.h>
6153

62-
typedef struct {
63-
float x;
64-
float y;
65-
} af_cfloat;
66-
67-
typedef struct {
68-
double x;
69-
double y;
70-
} af_cdouble;
71-
72-
#endif
73-
7454
typedef long long intl;
7555
typedef unsigned long long uintl;
7656

@@ -305,8 +285,6 @@ typedef enum {
305285
#ifdef __cplusplus
306286
namespace af
307287
{
308-
typedef af_cfloat cfloat;
309-
typedef af_cdouble cdouble;
310288
typedef af_dtype dtype;
311289
typedef af_source source;
312290
typedef af_interp_type interpType;

include/af/traits.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <complex>
1515
#include <af/defines.h>
16+
#include <af/complex.h>
1617

1718
namespace af {
1819

@@ -38,6 +39,16 @@ struct dtype_traits<af::cfloat> {
3839
static const char* getName() { return "std::complex<float>"; }
3940
};
4041

42+
template<>
43+
struct dtype_traits<std::complex<float> > {
44+
enum {
45+
af_type = c32 ,
46+
ctype = c32
47+
};
48+
typedef float base_type;
49+
static const char* getName() { return "std::complex<float>"; }
50+
};
51+
4152
template<>
4253
struct dtype_traits<double> {
4354
enum {
@@ -58,6 +69,16 @@ struct dtype_traits<af::cdouble> {
5869
static const char* getName() { return "std::complex<double>"; }
5970
};
6071

72+
template<>
73+
struct dtype_traits<std::complex<double> > {
74+
enum {
75+
af_type = c64 ,
76+
ctype = c64
77+
};
78+
typedef double base_type;
79+
static const char* getName() { return "std::complex<double>"; }
80+
};
81+
6182
template<>
6283
struct dtype_traits<char> {
6384
enum {

include/arrayfire.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@
256256
#include "af/array.h"
257257
#include "af/blas.h"
258258
#include "af/constants.h"
259+
#include "af/complex.h"
259260
#include "af/data.h"
260261
#include "af/device.h"
261262
#include "af/exception.h"

src/api/cpp/complex.cpp

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <af/complex.h>
11+
#include <istream>
12+
#include <cmath>
13+
#include <complex>
14+
15+
namespace af
16+
{
17+
18+
float real(af_cfloat val) { return val.real; }
19+
double real(af_cdouble val) { return val.real; }
20+
21+
float imag(af_cfloat val) { return val.imag; }
22+
double imag(af_cdouble val) { return val.imag; }
23+
24+
25+
cfloat operator+(const cfloat &lhs, const cfloat &rhs)
26+
{
27+
cfloat out(lhs.real + rhs.real, lhs.imag + rhs.imag);
28+
return out;
29+
}
30+
31+
cdouble operator+(const cdouble &lhs, const cdouble &rhs)
32+
{
33+
cdouble out(lhs.real + rhs.real, lhs.imag + rhs.imag);
34+
return out;
35+
}
36+
37+
cfloat operator-(const cfloat &lhs, const cfloat &rhs)
38+
{
39+
cfloat out(lhs.real - rhs.real, lhs.imag - rhs.imag);
40+
return out;
41+
}
42+
43+
cdouble operator-(const cdouble &lhs, const cdouble &rhs)
44+
{
45+
cdouble out(lhs.real - rhs.real, lhs.imag - rhs.imag);
46+
return out;
47+
}
48+
49+
using std::complex;
50+
cfloat operator*(const cfloat &lhs, const cfloat &rhs)
51+
{
52+
complex<float> clhs(lhs.real, lhs.imag);
53+
complex<float> crhs(rhs.real, rhs.imag);
54+
complex<float> out = clhs * crhs;
55+
return cfloat(out.real(), out.imag());
56+
}
57+
58+
cdouble operator*(const cdouble &lhs, const cdouble &rhs)
59+
{
60+
complex<double> clhs(lhs.real, lhs.imag);
61+
complex<double> crhs(rhs.real, rhs.imag);
62+
complex<double> out = clhs * crhs;
63+
return cdouble(out.real(), out.imag());
64+
}
65+
66+
cfloat operator/(const cfloat &lhs, const cfloat &rhs)
67+
{
68+
complex<float> clhs(lhs.real, lhs.imag);
69+
complex<float> crhs(rhs.real, rhs.imag);
70+
complex<float> out = clhs / crhs;
71+
return cfloat(out.real(), out.imag());
72+
}
73+
74+
cdouble operator/(const cdouble &lhs, const cdouble &rhs)
75+
{
76+
complex<double> clhs(lhs.real, lhs.imag);
77+
complex<double> crhs(rhs.real, rhs.imag);
78+
complex<double> out = clhs / crhs;
79+
return cdouble(out.real(), out.imag());
80+
}
81+
82+
cfloat operator/(const cfloat &lhs, const float &rhs)
83+
{
84+
complex<float> clhs(lhs.real, lhs.imag);
85+
complex<float> out = clhs / rhs;
86+
return cfloat(out.real(), out.imag());
87+
88+
}
89+
90+
cdouble operator/(const cdouble &lhs, const double &rhs)
91+
{
92+
complex<double> clhs(lhs.real, lhs.imag);
93+
complex<double> out = clhs / rhs;
94+
return cdouble(out.real(), out.imag());
95+
}
96+
97+
bool operator!=(const cfloat &lhs, const cfloat &rhs)
98+
{
99+
return !(lhs == rhs);
100+
}
101+
102+
bool operator!=(const cdouble &lhs, const cdouble &rhs)
103+
{
104+
return !(lhs == rhs);
105+
}
106+
107+
bool operator==(const cfloat &lhs, const cfloat &rhs)
108+
{
109+
return lhs.real == rhs.real && lhs.imag == rhs.imag;
110+
}
111+
112+
bool operator==(const cdouble &lhs, const cdouble &rhs)
113+
{
114+
return lhs.real == rhs.real && lhs.imag == rhs.imag;
115+
}
116+
117+
float abs(const cfloat &val)
118+
{
119+
std::complex<float> out(val.real, val.imag);
120+
return abs(out);
121+
}
122+
123+
double abs(const cdouble &val)
124+
{
125+
std::complex<double> out(val.real, val.imag);
126+
return abs(out);
127+
}
128+
129+
cfloat conj(const cfloat &val)
130+
{
131+
return cfloat(val.real, -val.imag);
132+
}
133+
134+
cdouble conj(const cdouble &val)
135+
{
136+
return cdouble(val.real, -val.imag);
137+
}
138+
139+
std::ostream& operator<< (std::ostream &os, const cfloat &in)
140+
{
141+
os << "(" << in.real << ", " << in.imag << ")";
142+
return os;
143+
}
144+
145+
std::ostream& operator<< (std::ostream &os, const cdouble &in)
146+
{
147+
os << "(" << in.real << " " << in.imag << ")";
148+
return os;
149+
}
150+
151+
std::istream& operator>> (std::istream &is, cfloat &in)
152+
{
153+
char trash;
154+
is >> trash;
155+
is >> in.real;
156+
is >> trash;
157+
is >> in.imag;
158+
is >> trash;
159+
return is;
160+
}
161+
162+
std::istream& operator>> (std::istream &is, cdouble &in)
163+
{
164+
char trash;
165+
is >> trash;
166+
is >> in.real;
167+
is >> trash;
168+
is >> in.imag;
169+
is >> trash;
170+
return is;
171+
}
172+
173+
}

0 commit comments

Comments
 (0)