~smspillaz/nux/nux.fix_1036521

« back to all changes in this revision

Viewing changes to NuxCore/Math/Complex.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "../NKernel.h"
 
2
#include "Complex.h"
 
3
 
 
4
NAMESPACE_BEGIN
 
5
 
 
6
Complex::Complex(t_float re, t_float im)
 
7
{
 
8
    real_ = re;
 
9
    imaginary_ = im;
 
10
}
 
11
 
 
12
Complex::Complex(const Complex& complex)
 
13
{
 
14
    real_ = complex.real_;
 
15
    imaginary_ = complex.imaginary_;
 
16
}
 
17
 
 
18
Complex::~Complex()
 
19
{
 
20
}
 
21
 
 
22
Complex& Complex::operator=(const Complex& complex)
 
23
{
 
24
    real_ = complex.real_;
 
25
    imaginary_ = complex.imaginary_;
 
26
 
 
27
    return *this;
 
28
}
 
29
 
 
30
/*const Complex Complex::operator + (const Complex& complex) const
 
31
{
 
32
    Complex result;
 
33
 
 
34
    result.real_ = real_ + complex.real_;
 
35
    result.imaginary_ = imaginary_ + complex.imaginary_;
 
36
 
 
37
    return result;
 
38
}
 
39
 
 
40
const Complex Complex::operator - (const Complex& complex) const
 
41
{
 
42
    Complex result;
 
43
 
 
44
    result.real_ = real_ - complex.real_;
 
45
    result.imaginary_ = imaginary_ - complex.imaginary_;
 
46
 
 
47
    return result;
 
48
}
 
49
 
 
50
const Complex Complex::operator*(const Complex& complex) const
 
51
{
 
52
        Complex result;
 
53
        t_float a, b, c, d;
 
54
 
 
55
        a = real_; b = imaginary_;
 
56
        c = complex.real_; d = complex.imaginary_;
 
57
 
 
58
        result.real_ = (a*c - b*d);
 
59
        result.imaginary_ = (a*d + b*c);
 
60
 
 
61
    return result;
 
62
}
 
63
 
 
64
const Complex Complex::operator / (const Complex& complex) const
 
65
{
 
66
    Complex result;
 
67
    t_float a, b, c, d;
 
68
    t_float inv_denominator;
 
69
 
 
70
    a = real_; b = imaginary_;
 
71
    c = complex.real_; d = complex.imaginary_;
 
72
    inv_denominator = (t_float) 1.0 / (c*c + d*d);
 
73
 
 
74
    result.real_ = (a*c + b*d) * inv_denominator;
 
75
    result.imaginary_ = (b*c - a*d) * inv_denominator;
 
76
 
 
77
    return result;
 
78
}
 
79
*/
 
80
/*const Complex Complex::operator * (const t_float& f) const
 
81
{
 
82
    Complex result;
 
83
 
 
84
    result.real_ = real_ * f;
 
85
    result.imaginary_ = imaginary_ * f;
 
86
 
 
87
    return result;
 
88
}*/
 
89
 
 
90
/*const Complex Complex::operator / (const t_float& f) const
 
91
{
 
92
    Complex result;
 
93
 
 
94
    //if(f == 0.0f)
 
95
    //    trow(Exception);
 
96
 
 
97
    result.real_ = real_ / f ;
 
98
    result.imaginary_ = imaginary_ / f;
 
99
 
 
100
    return result;
 
101
}*/
 
102
 
 
103
void Complex::operator+=(const Complex& complex)
 
104
{
 
105
    real_ += complex.real_;
 
106
    imaginary_ += complex.imaginary_;
 
107
}
 
108
 
 
109
void Complex::operator-=(const Complex& complex)
 
110
{
 
111
    real_ -= complex.real_;
 
112
    imaginary_ -= complex.imaginary_;
 
113
}
 
114
 
 
115
void Complex::operator*=(const Complex& complex)
 
116
{
 
117
    Complex result;
 
118
    t_float a, b, c, d;
 
119
 
 
120
    a = real_; b = imaginary_;
 
121
    c = complex.real_; d = complex.imaginary_;
 
122
 
 
123
    real_ = (a*c - b*d);
 
124
    imaginary_ = (a*d + b*c);
 
125
}
 
126
 
 
127
void Complex::operator /= (const Complex& complex)
 
128
{
 
129
    Complex result;
 
130
    t_float a, b, c, d;
 
131
    t_float inv_denominator;
 
132
 
 
133
    //if(complex.real_ == 0 && complex.imaginary_ == 0)
 
134
    //    trow(Exeption);
 
135
 
 
136
    a = real_; b = imaginary_;
 
137
    c = complex.real_; d = complex.imaginary_;
 
138
    inv_denominator = (t_float)1.0 / (c*c + d*d);
 
139
 
 
140
    real_ = (a*c + b*d) * inv_denominator;
 
141
    imaginary_ = (b*c - a*d) * inv_denominator;
 
142
}
 
143
 
 
144
/*void Complex::operator *= (const t_float& f)
 
145
{
 
146
    real_ *= f;
 
147
    imaginary_ *= f;
 
148
}*/
 
149
 
 
150
/*void Complex::operator/=(const t_float& f)
 
151
{
 
152
    //if(f == 0.0f)
 
153
    //    trow(Exception);
 
154
 
 
155
    real_ *= (t_float)1.0 / f;
 
156
    imaginary_ *= (t_float)1.0 / f;
 
157
}*/
 
158
 
 
159
void Complex::conjugue()
 
160
{
 
161
        imaginary_ = -imaginary_;
 
162
}
 
163
 
 
164
t_float Complex::absolute()
 
165
{
 
166
        t_float x, y, result, temp;
 
167
 
 
168
    x = (t_float) std::fabs(real_);
 
169
        y = (t_float) std::fabs(imaginary_);
 
170
 
 
171
        if(x == 0.0)
 
172
                result = y;
 
173
        else 
 
174
        {
 
175
                if(y == 0.0)
 
176
                        result = x;
 
177
                else 
 
178
                {
 
179
                        if(x > y)
 
180
                        {
 
181
                                temp = y/x;
 
182
                result = x * (t_float) std::sqrt(1.0+temp*temp);
 
183
                        }
 
184
                        else
 
185
                        {
 
186
                                temp = x/y;
 
187
                                result = y *(t_float) std::sqrt(1.0+temp*temp);
 
188
                        }
 
189
                }
 
190
        }
 
191
        return result;
 
192
}
 
193
 
 
194
t_bool Complex::IsNull()
 
195
{
 
196
        if((real_ == 0) && (imaginary_ == 0))
 
197
        {
 
198
                return true;
 
199
        }
 
200
        return false;
 
201
}
 
202
 
 
203
t_float Complex::real() const
 
204
{
 
205
        return real_;
 
206
}
 
207
 
 
208
t_float Complex::imaginary() const
 
209
{
 
210
        return imaginary_;
 
211
}
 
212
 
 
213
void Complex::real(t_float r)
 
214
{
 
215
        real_ = r;
 
216
}
 
217
 
 
218
void Complex::imaginary(t_float i)
 
219
{
 
220
        imaginary_ = i;
 
221
}
 
222
 
 
223
const Complex operator + (const Complex& lhs, const Complex& rhs)
 
224
{
 
225
    return Complex(lhs.real() + rhs.real(), lhs.imaginary() + rhs.imaginary());
 
226
}
 
227
 
 
228
const Complex operator - (const Complex& lhs, const Complex& rhs)
 
229
{
 
230
    return Complex(lhs.real() - rhs.real(), lhs.imaginary() - rhs.imaginary());
 
231
}
 
232
 
 
233
const Complex operator*(const Complex& lhs, const Complex& rhs)
 
234
{
 
235
        Complex result;
 
236
        t_float a, b, c, d;
 
237
 
 
238
        a = lhs.real(); b = lhs.imaginary();
 
239
        c = rhs.real(); d = rhs.imaginary();
 
240
 
 
241
        result.real(a*c - b*d);
 
242
        result.imaginary(a*d + b*c);
 
243
 
 
244
        return result;
 
245
}
 
246
 
 
247
const Complex operator/(const Complex& lhs, const Complex& rhs)
 
248
{
 
249
        Complex result;
 
250
        t_float a, b, c, d;
 
251
        t_float inv_denominator;
 
252
 
 
253
        a = lhs.real(); b = lhs.imaginary();
 
254
        c = rhs.real(); d = rhs.imaginary();
 
255
        inv_denominator = (t_float) 1.0 / (c*c + d*d);
 
256
 
 
257
        result.real((a*c + b*d) * inv_denominator);
 
258
        result.imaginary((b*c - a*d) * inv_denominator);
 
259
        return result;
 
260
}
 
261
 
 
262
/*fcomplex Cdiv(fcomplex a, fcomplex b)
 
263
   {
 
264
   fcomplex c;
 
265
   t_float den, r;
 
266
 
 
267
   if (fabs(b.r) >= fabs(b.i))
 
268
      {
 
269
      r = b.i/b.r;
 
270
      den = b.r + r*b.i;
 
271
      c.r = (a.r+r*a.i) / den;
 
272
      c.i = (a.i-r*a.r) / den;
 
273
      }
 
274
   else
 
275
      {
 
276
      r = b.r/b.i;
 
277
      den = b.i + r*b.r;
 
278
      c.r = (a.r*r+a.i) / den;
 
279
      c.i = (a.i*r-a.r) / den;
 
280
      }
 
281
 
 
282
   return c;
 
283
   }
 
284
*/
 
285
 
 
286
 
 
287
/*
 
288
fcomplex Csqrt(fcomplex z)
 
289
   {
 
290
   fcomplex c;
 
291
   t_float w;
 
292
 
 
293
   if ((z.r == 0.0) && (z.i == 0.0))
 
294
      {
 
295
      c.r = 0.0;
 
296
      c.i = 0.0;
 
297
      }
 
298
   else
 
299
      {
 
300
      w = sqrt( (sqrt( z.r*z.r + z.i*z.i ) + fabs(z.r)) * 0.5);
 
301
      if (z.r >= 0.0)
 
302
         {
 
303
         c.r = w;
 
304
         c.i = z.i / (2.0*w);
 
305
         }
 
306
      else
 
307
         {
 
308
         c.i = (z.i >= 0) ? w : -w;
 
309
         c.r = z.i / (2.0*c.i);
 
310
         }
 
311
      }
 
312
 
 
313
   return c;
 
314
   }
 
315
 
 
316
 
 
317
fcomplex RCmul(t_float x, fcomplex a)
 
318
   {
 
319
   fcomplex c;
 
320
 
 
321
   c.r = x*a.r;
 
322
   c.i = x*a.i;
 
323
 
 
324
   return c;
 
325
   }
 
326
 
 
327
 
 
328
fcomplex Cinv( fcomplex z)
 
329
   {
 
330
   fcomplex c;
 
331
   t_float s = 1.0 / (z.r*z.r + z.i*z.i);
 
332
 
 
333
   c.r = z.r * s;
 
334
   c.i = -z.i * s;
 
335
 
 
336
   return c;
 
337
   }
 
338
   */
 
339
NAMESPACE_END