|
| 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