1 #ifndef INCLUDE_AL_COMPLEX_HPP
2 #define INCLUDE_AL_COMPLEX_HPP
46 #include "al/math/al_Constants.hpp"
55 typedef Polar<float> Polarf;
56 typedef Polar<double> Polard;
57 typedef Complex<float> Complexf;
58 typedef Complex<double> Complexd;
75 Polar(
const T& phs = T(0)) :
m(T(1)),
p(phs) {}
79 Polar(
const T& mag,
const T& phs) :
m(mag),
p(phs) {}
107 Complex(
const Complex& v) :
r(v.
r),
i(v.
i) {}
108 Complex(
const Polar<T>& v) { *
this = v; }
109 Complex(
const T&
r = T(0),
const T&
i = T(0)) :
r(
r),
i(
i) {}
110 Complex(
const T& m,
const T& p,
int fromPolar) { (*this) = Polar<T>(m, p); }
136 T& operator[](
int i) {
return elems[
i]; }
137 const T& operator[](
int i)
const {
return elems[
i]; }
140 T& real() {
return r; }
141 const T& real()
const {
return r; }
142 T& imag() {
return i; }
143 const T& imag()
const {
return i; }
146 return (
r == v.
r) && (
i == v.
i);
149 return (
r == v) && (
i == T(0));
152 return (
r != v.
r) || (
i != v.
i);
166 C& operator=(
const C& v) {
176 C& operator-=(
const C& v) {
185 C& operator+=(
const C& v) {
194 C& operator*=(
const C& v) {
195 return set(
r * v.r -
i * v.i,
i * v.r +
r * v.i);
202 C& operator/=(
const C& v) {
return (*
this) *= v.recip(); }
209 C operator-()
const {
return C(-
r, -
i); }
210 C operator-(
const C& v)
const {
return C(*
this) -= v; }
211 C operator-(T v)
const {
return C(*
this) -= v; }
212 C operator+(
const C& v)
const {
return C(*
this) += v; }
213 C operator+(T v)
const {
return C(*
this) += v; }
214 C operator*(
const C& v)
const {
return C(*
this) *= v; }
215 C operator*(T v)
const {
return C(*
this) *= v; }
216 C operator/(
const C& v)
const {
return C(*
this) /= v; }
217 C operator/(T v)
const {
return C(*
this) /= v; }
219 T
arg()
const {
return atan2(
i,
r); }
221 T
r =
arg() / (2 * M_PI);
222 return r > 0 ?
r :
r + 1;
226 return r * v.
r +
i * v.
i;
235 return *
this *= (m /
norm());
238 return ((*this).log() * v).
exp();
240 C pow(T v)
const {
return ((*this).log() * v).
exp(); }
251 static const T c = T(1) /
::sqrt(T(2));
254 T b =
::sqrt(n -
r) * (
i < T(0) ? -c : c);
280 TEM T abs(
const Complex<T>& c) {
return c.mag(); }
281 TEM Complex<T> exp(
const Complex<T>& c) {
return c.exp(); }
282 TEM Complex<T> log(
const Complex<T>& c) {
return c.log(); }
283 TEM Complex<T> pow(
const Complex<T>& b,
const Complex<T>& e) {
286 TEM Complex<T> pow(
const Complex<T>& b,
const T& e) {
return b.pow(e); }
293 inline Complex<T> operator+(T r,
const Complex<T>& c) {
298 inline Complex<T> operator-(T r,
const Complex<T>& c) {
303 inline Complex<T> operator*(T r,
const Complex<T>& c) {
308 inline Complex<T> operator/(T r,
const Complex<T>& c) {
309 return c.conj() * (r / c.normSqr());
312 template <
class VecN,
class T>
313 VecN rotate(
const VecN& v,
const VecN& p,
const Complex<T>& a) {
314 return v * a.r + p * a.i;
320 template <
class VecN,
class T>
322 VecN t = al::rotate(v1, v2, a);
323 v2 = al::rotate(v2, VecN(-v1), a);
330 template <
class Vec3,
class T>
333 T mul = T(2) / (magSqr + T(1));
334 return Vec3(c.
r * mul, c.
i * mul, (magSqr - T(1)) * mul * T(0.5));
T phase() const
Returns argument (angle)
T arg() const
Returns argument in [-pi, pi].
C sin() const
Returns sin(z)
C sqrt() const
Returns square root.
C & set(T vr, T vi)
Set real and imaginary components.
C & fromPolar(T m, T p)
Set magnitude and phase.
C pow(const C &v) const
Returns z^v.
T abs() const
Returns norm (radius), |z|.
T norm() const
Returns norm (radius), |z|.
C exp() const
Returns e^z.
bool operator<(const C &c) const
Returns true if norm is less than argument's norm.
C sinh() const
Returns sinh(z)
bool operator==(const T &v) const
Returns true if real and equals value.
C cosh() const
Returns cosh(z)
bool operator>(const C &v) const
Returns true if norm is greater than argument's norm.
T dot(const C &v) const
Returns vector dot product.
T normSqr() const
Returns square of norm, |z|^2.
C recip() const
Return multiplicative inverse, 1/z.
C cos() const
Returns cos(z)
T mag() const
Returns norm (radius), |z|.
C & norm(T v)
Set norm leaving argument the same.
bool operator!=(const C &v) const
Returns true if any components are not equal.
C & arg(T v)
Set argument leaving norm the same.
C sqr() const
Returns square.
bool operator==(const C &v) const
Returns true if all components are equal.
C conj() const
Returns conjugate, z*.
C & normalize(T m=T(1))
Sets magnitude to 1, |z|=1.
C & fromPolar(T phase)
Set phase and normalize.
C sgn(T m=T(1)) const
Returns signum, z/|z|, the closest point on unit circle.
T argUnit() const
Return argument in unit interval [0, 1)
C pow(T v) const
Returns z^v.
C log() const
Returns log(z)
T magSqr() const
Returns square of norm, |z|^2.
Polar(const T &mag, const T &phs)
Polar(const Complex< T > &v)
Vec3 sterProj(const al::Complex< T > &c)
void rotatePlane(VecN &v1, VecN &v2, const Complex< T > &a)