HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Complex.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Complex.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  * Defines a complex number and some simple complex operations.
11  *
12  */
13 
14 #ifndef __UT_COMPLEX__
15 #define __UT_COMPLEX__
16 
17 #include "UT_API.h"
18 #include "UT_Vector2.h"
19 
20 #include <SYS/SYS_Math.h>
21 #include <SYS/SYS_Types.h>
22 #include <SYS/SYS_TypeDecorate.h>
23 
24 
25 template <typename T> class UT_ComplexT;
28 // UT_Complex typedef for backward compatibility with old non-template version.
30 
31 // Declare the following types as POD so that UT_ValArray can be optimized
34 
35 
36 template <typename T>
38 {
39 public:
40  UT_ComplexT();
41  UT_ComplexT(T r,T i);
42  UT_ComplexT(const UT_ComplexT &);
44  : myReal(vec.x())
45  , myImag(vec.y()) {}
46 
47  void set(T r,T i);
48 
49  UT_ComplexT & operator=(const UT_ComplexT &);
50  int operator==(const UT_ComplexT &) const;
51 
52  UT_ComplexT operator+(T) const;
53  UT_ComplexT operator+(const UT_ComplexT &) const;
54  UT_ComplexT operator-() const;
55  UT_ComplexT operator-(T) const;
56  UT_ComplexT operator-(const UT_ComplexT &) const;
57 
58  UT_ComplexT operator*(T) const;
59  UT_ComplexT operator*(const UT_ComplexT &) const;
60  UT_ComplexT operator/(T) const;
61  UT_ComplexT operator/(const UT_ComplexT &) const;
62 
63  operator UT_Vector2T<T>() const
64  { return UT_Vector2T<T>(myReal, myImag); }
65 
66  UT_ComplexT pow(T exp) const;
67 
68  T real() const { return myReal; }
69  T& real() { return myReal; }
70  T imaginary() const { return myImag; }
71  T& imaginary() { return myImag; }
72 
73  T magnitude() const;
74  T magnitude2() const;
75 
76  // Phase is returned in radians
77  T phase() const;
78 
79 
80 private:
81  T myReal;
82  T myImag;
83 };
84 
85 // Implementation
86 template <typename T>
87 inline
89 {
90  myReal = 0.0F;
91  myImag = 0.0F;
92 }
93 
94 template <typename T>
95 inline
97 {
98  myReal = r;
99  myImag = i;
100 }
101 
102 template <typename T>
103 inline
105 {
106  myReal = source.real();
107  myImag = source.imaginary();
108 }
109 
110 template <typename T>
111 inline void
113 {
114  myReal = r;
115  myImag = i;
116 }
117 
118 template <typename T>
119 inline UT_ComplexT<T> &
121 {
122  myReal = source.real();
123  myImag = source.imaginary();
124 
125  return *this;
126 }
127 
128 template <typename T>
129 inline int
131 {
132  if(SYSisEqual(source.real(), myReal) &&
133  SYSisEqual(source.imaginary(), myImag))
134  return 1;
135  return 0;
136 }
137 
138 template <typename T>
139 inline UT_ComplexT<T>
141 {
142  return UT_ComplexT(myReal + r, myImag);
143 }
144 
145 template <typename T>
146 inline UT_ComplexT<T>
148 {
149  return UT_ComplexT(myReal - r, myImag);
150 }
151 
152 template <typename T>
153 inline UT_ComplexT<T>
155 {
156  return UT_ComplexT(myReal + add.real(), myImag + add.imaginary());
157 }
158 
159 template <typename T>
160 inline UT_ComplexT<T>
162 {
163  return UT_ComplexT(-myReal, -myImag);
164 }
165 
166 template <typename T>
167 inline UT_ComplexT<T>
169 {
170  return UT_ComplexT(myReal - sub.real(), myImag - sub.imaginary());
171 }
172 
173 template <typename T>
174 inline UT_ComplexT<T>
176 {
177  return UT_ComplexT(myReal * a, myImag * a);
178 }
179 
180 template <typename T>
181 inline UT_ComplexT<T>
183 {
184  T r,i;
185 
186  r = mult.real();
187  i = mult.imaginary();
188 
189  return UT_ComplexT(myReal * r - myImag * i,
190  myReal * i + myImag * r);
191 }
192 
193 template <typename T>
194 inline UT_ComplexT<T>
196 {
197  UT_ComplexT raised;
198  T mag,phs;
199 
200  mag = magnitude();
201  phs=phase();
202 
203  mag = SYSpow(mag, exp);
204 
205  phs *= exp;
206 
207 #define UT_COMPLEX_SINCOS
208 #ifdef UT_COMPLEX_SINCOS
209  T s,c;
210  SYSsincos(phs, &s, &c);
211  return UT_ComplexT( mag * c, mag * s);
212 #else
213  return UT_ComplexT( mag * SYScos(phs), mag * SYSsin(phs));
214 #endif
215 }
216 
217 template <typename T>
218 inline UT_ComplexT<T>
220 {
221  return UT_ComplexT(myReal/d,myImag/d);
222 }
223 
224 template <typename T>
225 inline UT_ComplexT<T>
227 {
228  T r,i,d;
229 
230  r = div.real();
231  i = div.imaginary();
232 
233  d = r*r + i*i;
234 
235  return UT_ComplexT((myReal * r + myImag * i)/d,
236  (myImag * r - myReal * i)/d);
237 }
238 
239 template <typename T>
240 inline T
242 {
243  return SYSsqrt(myReal*myReal + myImag *myImag);
244 }
245 
246 template <typename T>
247 inline T
249 {
250  return myReal*myReal + myImag *myImag;
251 }
252 
253 template <typename T>
254 inline T
256 {
257  return SYSatan(-myImag, myReal);
258 }
259 
260 #endif
261 
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:561
GA_API const UT_StringHolder div
T real() const
Definition: UT_Complex.h:68
UT_ComplexT operator*(T) const
Definition: UT_Complex.h:175
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Add corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:577
UT_ComplexT< fpreal64 > UT_Complex
Definition: UT_Complex.h:29
T & imaginary()
Definition: UT_Complex.h:71
UT_ComplexT operator+(T) const
Definition: UT_Complex.h:140
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
T magnitude2() const
Definition: UT_Complex.h:248
UT_ComplexT< fpreal32 > UT_ComplexF
Definition: UT_Complex.h:25
GLint y
Definition: glcorearb.h:103
2D Vector class.
Definition: UT_Vector2.h:159
UT_ComplexT pow(T exp) const
Definition: UT_Complex.h:195
VectorToScalarConverter< GridType >::Type::Ptr magnitude(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the magnitudes of the vectors of the given vector-valued grid.
ImageBuf OIIO_API pow(const ImageBuf &A, cspan< float > B, ROI roi={}, int nthreads=0)
T magnitude() const
Definition: UT_Complex.h:241
ImageBuf OIIO_API sub(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:587
UT_ComplexT< fpreal64 > UT_ComplexD
Definition: UT_Complex.h:27
int operator==(const UT_ComplexT &) const
Definition: UT_Complex.h:130
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
T & real()
Definition: UT_Complex.h:69
#define SYS_DECLARE_IS_POD(T)
Declare a type as POD.
UT_ComplexT operator/(T) const
Definition: UT_Complex.h:219
UT_ComplexT & operator=(const UT_ComplexT &)
Definition: UT_Complex.h:120
GLint GLenum GLint x
Definition: glcorearb.h:409
T imaginary() const
Definition: UT_Complex.h:70
UT_ComplexT(const UT_Vector2T< T > &vec)
Definition: UT_Complex.h:43
T phase() const
Definition: UT_Complex.h:255
LeafData & operator=(const LeafData &)=delete
void set(T r, T i)
Definition: UT_Complex.h:112
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLboolean r
Definition: glcorearb.h:1222
bool SYSisEqual(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b, S tol=SYS_FTOLERANCE)
Componentwise equality.
Definition: UT_Vector2.h:674
UT_ComplexT operator-() const
Definition: UT_Complex.h:161
IMATH_HOSTDEVICE constexpr Quat< T > operator/(const Quat< T > &q1, const Quat< T > &q2) IMATH_NOEXCEPT
Quaterion division.
Definition: ImathQuat.h:871
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542