Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Misc/ILFComplex.cs @ 11194

Last change on this file since 11194 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 98.7 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40#pragma warning disable 162
41using System;
42using System.Collections.Generic;
43using System.Text;
44using System.Runtime.InteropServices;
45
46
47
48namespace ILNumerics {
49    /// <summary>
50    /// Floating point complex value data type of float (single) precision
51    /// </summary>
52    /// <remarks>This class extends the system value types for real numbers to complex float
53    /// values. Besides the publicly available members 'real' and 'imag' it provides all the
54    /// basis functionality the floating point System.double brings (abs, log, sqrt, tan etc.) for
55    /// float precision complex,
56    /// as well as it overrides the basic unary and binary operators for all common system value
57    /// types including rarely used types (e.g. UInt16). This includes the basic numerical operations
58    /// like '+','-','/','*' and the relational operators: '==','>','>=' etc. Also there are some
59    /// explicit and some implicit casting operators from / to fcomplex values into system
60    /// value types. </remarks>
61    [Serializable]   
62    [StructLayout(LayoutKind.Sequential)]
63    public struct fcomplex : IEquatable<fcomplex> {
64        /// <summary>
65        /// Real part of this complex number
66        /// </summary>
67        public float real;
68        /// <summary>
69        /// Imaginary part of this complex number
70        /// </summary>
71        public float imag;
72        /// <summary>
73        /// Imaginary unit
74        /// </summary>
75        public static readonly fcomplex i = new fcomplex(0.0f,1.0f);
76
77        /// <summary>
78        /// Construct new float complex number
79        /// </summary>
80        /// <param name="real">Real part</param>
81        /// <param name="imag">Imaginary part</param>
82        public fcomplex(float real, float imag) {
83            this.real = real;
84            this.imag = imag;
85        }
86       
87        /// <summary>
88        /// Complex conjugate
89        /// </summary>
90        public fcomplex conj {
91            get{
92                return new fcomplex(real,imag * (-1.0f));
93            }
94        }
95
96        /// <summary>
97        /// Positive infinity for real and imag part of complex value
98        /// </summary>
99        public static fcomplex INF {
100            get {
101                return new fcomplex(
102                    float.PositiveInfinity,
103                    float.PositiveInfinity
104                );
105            }
106        }
107
108        /// <summary>
109        /// New fcomplex, real and imaginary parts are zero
110        /// </summary>
111       public static fcomplex Zero {
112            get {
113                return new fcomplex(0f,0f);
114            }
115        }
116
117        /// <summary>
118        /// fcomplex quantity, marked as being "not a number"
119        /// </summary>
120        public static fcomplex NaN {
121            get {
122                return new fcomplex(float.NaN,float.NaN);
123            }
124        }
125
126        /// <summary>
127        /// Are obj's real and imaginary part identical to the real and imaginary parts of this fcomplex
128        /// </summary>
129        /// <param name="obj">fcomplex object to determine the equality for</param>
130        /// <returns>true if obj is of fcomplex type and its real and imag part has the same
131        /// values as the real and imaginary part of this array.</returns>
132        public override bool Equals(object obj) {
133            if (obj is fcomplex && ((fcomplex)obj) == this)
134                return true;
135            return false;
136        }
137
138        /// <summary>
139        /// Check if a fcomplex number equals this fcomplex number
140        /// </summary>
141        /// <param name="other">other complex number</param>
142        /// <returns>true if both, real and imaginary parts of both complex number are (binary) equal, false otherwise</returns>
143        public bool Equals(fcomplex other) {
144            return real.Equals(other.real) && imag.Equals(other.imag);
145        }
146
147        /// <summary>
148        /// Give HashCode of this fcomplex number
149        /// </summary>
150        /// <returns>HashCode of this fcomplex number</returns>
151        public override int GetHashCode() {
152            return 31 * real.GetHashCode() + imag.GetHashCode();
153        }
154
155
156       
157
158#region HYCALPER AUTO GENERATED CODE
159
160       
161        /// <summary>
162        /// Add two complex numbers
163        /// </summary>
164        /// <param name="A">First summand</param>
165        /// <param name="B">Second summand</param>
166        /// <returns>result</returns>
167        public static  complex operator +( fcomplex A,  complex B) {
168            complex ret;
169             ret.real =  (double) (A.real + B.real );
170             ret.imag =  (double) (A.imag + B.imag );
171            return ret;
172        }
173        /// <summary>
174        /// Subtract two complex values
175        /// </summary>
176        /// <param name="A">Minuend</param>
177        /// <param name="B">Subtrahend</param>
178        /// <returns>result</returns>
179        public static  complex operator -( fcomplex A,  complex B) {
180            complex ret;
181            ret.real =  (double) (A.real  - B.real );
182            ret.imag =  (double) (A.imag - B.imag );
183            return ret;
184        }
185        /// <summary>
186        /// Multiply two complex values
187        /// </summary>
188        /// <param name="A">First factor</param>
189        /// <param name="B">Second factor</param>
190        /// <returns>result</returns>
191        public static  complex operator *( fcomplex A,  complex B) {
192            complex ret;
193            ret.real =  (double) ((A.real * B.real ) - (A.imag * B.imag ));
194            ret.imag =  (double) ((A.real * B.imag ) + (A.imag * B.real ));
195            return ret;
196        }
197        /// <summary>
198        /// Divide two numbers
199        /// </summary>
200        /// <param name="A">Divident</param>
201        /// <param name="B">Divisor</param>
202        /// <returns>Result</returns>
203        /// <remarks><para>Unless the operator must handle special inputs (Inf or 0 values),
204        /// the algorithm described in [1] is used for division. This is considered to be
205        /// more robust against floating point overflow than the naive approach of simple
206        /// cartesian division.</para>
207        /// <para>References: [1]: Smith, R.L., Algorithm 116: Complex division. Commun.ACM 5,8 (1962),435 <br />
208        /// [2]: Stewart, G.W., A note on complex division, ACM trans.on math software, Vol.11, N.3 (1985)</para></remarks>
209        public static  complex operator /( fcomplex A,  complex B) {
210            if (B.imag == 0) return A / B.real;
211            return A * (1 / B);
212            if (IsNaN(A) ||  complex .IsNaN(B)) return NaN;
213            //if ( complex .IsInfinity(B)) return NaN;           
214            //if (A.real == 0 && A.imag == 0) return ( complex )0;
215            complex ret;
216            if (B.real == 0) {
217                ret.imag =  (double) -(A.real / B.imag);
218                ret.real =  (double) (A.imag / B.imag);
219                return ret;
220            }
221            // this would be the naive approach. But it come with to little robustness against overflow
222            //double norm2 = B.real * B.real + B.imag * B.imag;
223            //if (norm2 == 0) return INF;    // this may be removed, since division by 0 results in inf anyway ?
224            //ret.real =  (double) (((A.real  * B.real ) + (A.imag  * B.imag )) / norm2);
225            //ret.imag =  (double) (((A.imag  * B.real ) - (A.real  * B.imag )) / norm2);
226           
227            // this algorithm is taken from [1]. The one described in [2] was not taken. Tests
228            // did not show any advantage when using double precision floating point arithmetic.
229            double tmp1, tmp2;
230            if (Math.Abs(B.real) >= Math.Abs(B.imag)) {
231                tmp1 =  (double) (B.imag * (1/B.real));
232                tmp2 =  (double) (B.real + B.imag*tmp1);
233                ret.real =  (double) (A.real + A.imag*tmp1)/tmp2;
234                ret.imag =  (double) (A.imag - A.real*tmp1)/tmp2;
235            } else {
236                tmp1 =  (double) (B.real * (1/B.imag));
237                tmp2 =  (double) (B.imag + B.real*tmp1);
238                ret.real =  (double) (A.imag + A.real*tmp1)/tmp2;
239                ret.imag = -  (double) (A.real - A.imag*tmp1)/tmp2;
240            }
241            return ret;                                           
242        }
243        /// <summary>
244        /// Equality comparison for complex numbers
245        /// </summary>
246        /// <param name="A">Left side</param>
247        /// <param name="B">Right side</param>
248        /// <returns>true, if real and imaginary part are identical</returns>
249        public static bool operator ==( fcomplex A,  complex B) {
250            return (A.imag  == B.imag ) && (A.real  == B.real );
251        }
252        /// <summary>
253        /// Unequality comparison for complex numbers
254        /// </summary>
255        /// <param name="A">Left side</param>
256        /// <param name="B">Right side</param>
257        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
258        public static bool operator !=( fcomplex A,  complex B) {
259            return (A.imag  != B.imag ) || (A.real  != B.real );
260        }
261        /// <summary>
262        /// Greater than comparison for complex numbers
263        /// </summary>
264        /// <param name="A">Left side</param>
265        /// <param name="B">Right side</param>
266        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
267        /// <remarks>Only the real parts are compared!</remarks>
268        public static bool operator > ( fcomplex A,  complex B) {
269            return (A.real > B.real );
270        }
271        /// <summary>
272        /// Lower than comparison for complex numbers
273        /// </summary>
274        /// <param name="A">Left side</param>
275        /// <param name="B">Right side</param>
276        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
277        /// <remarks>Only the real parts are compared!</remarks>
278        public static bool operator < ( fcomplex A,  complex B) {
279            return (A.real < B.real );
280        }
281        /// <summary>
282        /// Greater than or equal to comparison for complex numbers
283        /// </summary>
284        /// <param name="A">Left side</param>
285        /// <param name="B">Right side</param>
286        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
287        /// <remarks>Only the real parts are compared!</remarks>
288        public static bool operator >=( fcomplex A,  complex B) {
289            return (A.real >= B.real );
290        }
291        /// <summary>
292        /// Lower than or equal to comparison for complex numbers
293        /// </summary>
294        /// <param name="A">Left side</param>
295        /// <param name="B">Right side</param>
296        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
297        /// <remarks>Only the real parts are compared!</remarks>
298        public static bool operator <=( fcomplex A,  complex B) {
299            return (A.real <= B.real );
300        }
301       
302        /// <summary>
303        /// Add two complex numbers
304        /// </summary>
305        /// <param name="A">First summand</param>
306        /// <param name="B">Second summand</param>
307        /// <returns>result</returns>
308        public static  fcomplex operator +( fcomplex A,  fcomplex B) {
309            fcomplex ret;
310             ret.real =  (float) (A.real + B.real );
311             ret.imag =  (float) (A.imag + B.imag );
312            return ret;
313        }
314        /// <summary>
315        /// Subtract two complex values
316        /// </summary>
317        /// <param name="A">Minuend</param>
318        /// <param name="B">Subtrahend</param>
319        /// <returns>result</returns>
320        public static  fcomplex operator -( fcomplex A,  fcomplex B) {
321            fcomplex ret;
322            ret.real =  (float) (A.real  - B.real );
323            ret.imag =  (float) (A.imag - B.imag );
324            return ret;
325        }
326        /// <summary>
327        /// Multiply two complex values
328        /// </summary>
329        /// <param name="A">First factor</param>
330        /// <param name="B">Second factor</param>
331        /// <returns>result</returns>
332        public static  fcomplex operator *( fcomplex A,  fcomplex B) {
333            fcomplex ret;
334            ret.real =  (float) ((A.real * B.real ) - (A.imag * B.imag ));
335            ret.imag =  (float) ((A.real * B.imag ) + (A.imag * B.real ));
336            return ret;
337        }
338        /// <summary>
339        /// Divide two numbers
340        /// </summary>
341        /// <param name="A">Divident</param>
342        /// <param name="B">Divisor</param>
343        /// <returns>Result</returns>
344        /// <remarks><para>Unless the operator must handle special inputs (Inf or 0 values),
345        /// the algorithm described in [1] is used for division. This is considered to be
346        /// more robust against floating point overflow than the naive approach of simple
347        /// cartesian division.</para>
348        /// <para>References: [1]: Smith, R.L., Algorithm 116: Complex division. Commun.ACM 5,8 (1962),435 <br />
349        /// [2]: Stewart, G.W., A note on complex division, ACM trans.on math software, Vol.11, N.3 (1985)</para></remarks>
350        public static  fcomplex operator /( fcomplex A,  fcomplex B) {
351            if (B.imag == 0) return A / B.real;
352            return A * (1 / B);
353            if (IsNaN(A) ||  fcomplex .IsNaN(B)) return NaN;
354            //if ( fcomplex .IsInfinity(B)) return NaN;           
355            //if (A.real == 0 && A.imag == 0) return ( fcomplex )0;
356            fcomplex ret;
357            if (B.real == 0) {
358                ret.imag =  (float) -(A.real / B.imag);
359                ret.real =  (float) (A.imag / B.imag);
360                return ret;
361            }
362            // this would be the naive approach. But it come with to little robustness against overflow
363            //double norm2 = B.real * B.real + B.imag * B.imag;
364            //if (norm2 == 0) return INF;    // this may be removed, since division by 0 results in inf anyway ?
365            //ret.real =  (float) (((A.real  * B.real ) + (A.imag  * B.imag )) / norm2);
366            //ret.imag =  (float) (((A.imag  * B.real ) - (A.real  * B.imag )) / norm2);
367           
368            // this algorithm is taken from [1]. The one described in [2] was not taken. Tests
369            // did not show any advantage when using double precision floating point arithmetic.
370            float tmp1, tmp2;
371            if (Math.Abs(B.real) >= Math.Abs(B.imag)) {
372                tmp1 =  (float) (B.imag * (1/B.real));
373                tmp2 =  (float) (B.real + B.imag*tmp1);
374                ret.real =  (float) (A.real + A.imag*tmp1)/tmp2;
375                ret.imag =  (float) (A.imag - A.real*tmp1)/tmp2;
376            } else {
377                tmp1 =  (float) (B.real * (1/B.imag));
378                tmp2 =  (float) (B.imag + B.real*tmp1);
379                ret.real =  (float) (A.imag + A.real*tmp1)/tmp2;
380                ret.imag = -  (float) (A.real - A.imag*tmp1)/tmp2;
381            }
382            return ret;                                           
383        }
384        /// <summary>
385        /// Equality comparison for complex numbers
386        /// </summary>
387        /// <param name="A">Left side</param>
388        /// <param name="B">Right side</param>
389        /// <returns>true, if real and imaginary part are identical</returns>
390        public static bool operator ==( fcomplex A,  fcomplex B) {
391            return (A.imag  == B.imag ) && (A.real  == B.real );
392        }
393        /// <summary>
394        /// Unequality comparison for complex numbers
395        /// </summary>
396        /// <param name="A">Left side</param>
397        /// <param name="B">Right side</param>
398        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
399        public static bool operator !=( fcomplex A,  fcomplex B) {
400            return (A.imag  != B.imag ) || (A.real  != B.real );
401        }
402        /// <summary>
403        /// Greater than comparison for complex numbers
404        /// </summary>
405        /// <param name="A">Left side</param>
406        /// <param name="B">Right side</param>
407        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
408        /// <remarks>Only the real parts are compared!</remarks>
409        public static bool operator > ( fcomplex A,  fcomplex B) {
410            return (A.real > B.real );
411        }
412        /// <summary>
413        /// Lower than comparison for complex numbers
414        /// </summary>
415        /// <param name="A">Left side</param>
416        /// <param name="B">Right side</param>
417        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
418        /// <remarks>Only the real parts are compared!</remarks>
419        public static bool operator < ( fcomplex A,  fcomplex B) {
420            return (A.real < B.real );
421        }
422        /// <summary>
423        /// Greater than or equal to comparison for complex numbers
424        /// </summary>
425        /// <param name="A">Left side</param>
426        /// <param name="B">Right side</param>
427        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
428        /// <remarks>Only the real parts are compared!</remarks>
429        public static bool operator >=( fcomplex A,  fcomplex B) {
430            return (A.real >= B.real );
431        }
432        /// <summary>
433        /// Lower than or equal to comparison for complex numbers
434        /// </summary>
435        /// <param name="A">Left side</param>
436        /// <param name="B">Right side</param>
437        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
438        /// <remarks>Only the real parts are compared!</remarks>
439        public static bool operator <=( fcomplex A,  fcomplex B) {
440            return (A.real <= B.real );
441        }
442
443#endregion HYCALPER AUTO GENERATED CODE
444
445
446       
447
448#region HYCALPER AUTO GENERATED CODE
449
450       
451        /// <summary>
452        /// Add two complex numbers
453        /// </summary>
454        /// <param name="A">First summand</param>
455        /// <param name="B">Second summand</param>
456        /// <returns>Result</returns>
457        public static  fcomplex operator +( fcomplex A,  Int64 B) {
458            fcomplex ret;
459            ret.real =  (float) (A.real + B);
460            ret.imag =  (float) A.imag;
461            return ret;
462        }
463        /// <summary>
464        /// Subtract two values
465        /// </summary>
466        /// <param name="A">Minuend</param>
467        /// <param name="B">Subtrahend</param>
468        /// <returns>result</returns>
469        public static  fcomplex operator -( fcomplex A,  Int64 B) {
470            fcomplex ret;
471            ret.real =  (float) (A.real - B);
472            ret.imag =  (float) A.imag;
473            return ret;
474        }
475        /// <summary>
476        /// Multiply two values
477        /// </summary>
478        /// <param name="A">First factor</param>
479        /// <param name="B">Second factor</param>
480        /// <returns>result</returns>
481        public static  fcomplex operator *( fcomplex A,  Int64 B) {
482            fcomplex ret;
483            ret.real =  (float) (A.real * B);
484            ret.imag =  (float) (A.imag * B);
485            return ret;
486        }
487        /// <summary>
488        /// Divide two numbers
489        /// </summary>
490        /// <param name="A">Divident</param>
491        /// <param name="B">Divisor</param>
492        /// <returns>result</returns>
493        public static  fcomplex operator /( fcomplex A,  Int64 B) {
494            if (IsNaN(A)) return NaN;
495           
496            if (A.real == 0 && A.imag == 0) {
497                if (B == 0) return NaN;
498                return ( fcomplex )0;
499            } else {
500                if (false)
501                {
502                    if (IsInfinity(A)) {
503                        return NaN;
504                    } else {
505                        return ( fcomplex )0;
506                    }
507                }
508            }
509            fcomplex ret;
510            if (B == 0) return INF ;
511            ret.real =  (float) (A.real / B);
512            ret.imag =  (float) (A.imag / B);
513            return ret;
514        }
515        /// <summary>
516        /// Equality comparison for complex numbers
517        /// </summary>
518        /// <param name="A">Left side</param>
519        /// <param name="B">Right side</param>
520        /// <returns>result</returns>
521        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
522        public static bool operator ==( fcomplex A,  Int64 B) {
523            return (A.real == B && A.imag == 0.0);
524        }
525        /// <summary>
526        /// Unequality comparison for complex numbers
527        /// </summary>
528        /// <param name="A">Left side</param>
529        /// <param name="B">Right side</param>
530        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
531        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
532        public static bool operator !=( fcomplex A,  Int64 B) {
533            return (A.imag != 0.0) || (A.real != B);
534        }
535        /// <summary>
536        /// Freater than comparison for complex numbers
537        /// </summary>
538        /// <param name="A">Left side</param>
539        /// <param name="B">Right side</param>
540        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
541        /// <remarks>Only the real parts are compared!</remarks>
542        public static bool operator > ( fcomplex A,  Int64 B) {
543            return (A.real > B);
544        }
545        /// <summary>
546        /// Lower than comparison for complex numbers
547        /// </summary>
548        /// <param name="A">Left side</param>
549        /// <param name="B">Right side</param>
550        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
551        /// <remarks>Only the real parts are compared!</remarks>
552        public static bool operator <(  fcomplex A,  Int64 B) {
553            return (A.real < B);
554        }
555        /// <summary>
556        /// Greater than or equal to comparison for complex numbers
557        /// </summary>
558        /// <param name="A">Left side</param>
559        /// <param name="B">Right side</param>
560        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
561        /// <remarks>Only the real parts are compared!</remarks>
562        public static bool operator >=( fcomplex A,  Int64 B) {
563            return (A.real >= B);
564        }
565        /// <summary>
566        /// Lower than or equal to comparison for complex numbers
567        /// </summary>
568        /// <param name="A">Left side</param>
569        /// <param name="B">Right side</param>
570        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
571        /// <remarks>Only the real parts are compared!</remarks>
572        public static bool operator <=( fcomplex A,  Int64 B) {
573            return (A.real <= B);
574        }
575       
576        /// <summary>
577        /// Add two complex numbers
578        /// </summary>
579        /// <param name="A">First summand</param>
580        /// <param name="B">Second summand</param>
581        /// <returns>Result</returns>
582        public static  fcomplex operator +( fcomplex A,  Int32 B) {
583            fcomplex ret;
584            ret.real =  (float) (A.real + B);
585            ret.imag =  (float) A.imag;
586            return ret;
587        }
588        /// <summary>
589        /// Subtract two values
590        /// </summary>
591        /// <param name="A">Minuend</param>
592        /// <param name="B">Subtrahend</param>
593        /// <returns>result</returns>
594        public static  fcomplex operator -( fcomplex A,  Int32 B) {
595            fcomplex ret;
596            ret.real =  (float) (A.real - B);
597            ret.imag =  (float) A.imag;
598            return ret;
599        }
600        /// <summary>
601        /// Multiply two values
602        /// </summary>
603        /// <param name="A">First factor</param>
604        /// <param name="B">Second factor</param>
605        /// <returns>result</returns>
606        public static  fcomplex operator *( fcomplex A,  Int32 B) {
607            fcomplex ret;
608            ret.real =  (float) (A.real * B);
609            ret.imag =  (float) (A.imag * B);
610            return ret;
611        }
612        /// <summary>
613        /// Divide two numbers
614        /// </summary>
615        /// <param name="A">Divident</param>
616        /// <param name="B">Divisor</param>
617        /// <returns>result</returns>
618        public static  fcomplex operator /( fcomplex A,  Int32 B) {
619            if (IsNaN(A)) return NaN;
620           
621            if (A.real == 0 && A.imag == 0) {
622                if (B == 0) return NaN;
623                return ( fcomplex )0;
624            } else {
625                if (false)
626                {
627                    if (IsInfinity(A)) {
628                        return NaN;
629                    } else {
630                        return ( fcomplex )0;
631                    }
632                }
633            }
634            fcomplex ret;
635            if (B == 0) return INF ;
636            ret.real =  (float) (A.real / B);
637            ret.imag =  (float) (A.imag / B);
638            return ret;
639        }
640        /// <summary>
641        /// Equality comparison for complex numbers
642        /// </summary>
643        /// <param name="A">Left side</param>
644        /// <param name="B">Right side</param>
645        /// <returns>result</returns>
646        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
647        public static bool operator ==( fcomplex A,  Int32 B) {
648            return (A.real == B && A.imag == 0.0);
649        }
650        /// <summary>
651        /// Unequality comparison for complex numbers
652        /// </summary>
653        /// <param name="A">Left side</param>
654        /// <param name="B">Right side</param>
655        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
656        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
657        public static bool operator !=( fcomplex A,  Int32 B) {
658            return (A.imag != 0.0) || (A.real != B);
659        }
660        /// <summary>
661        /// Freater than comparison for complex numbers
662        /// </summary>
663        /// <param name="A">Left side</param>
664        /// <param name="B">Right side</param>
665        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
666        /// <remarks>Only the real parts are compared!</remarks>
667        public static bool operator > ( fcomplex A,  Int32 B) {
668            return (A.real > B);
669        }
670        /// <summary>
671        /// Lower than comparison for complex numbers
672        /// </summary>
673        /// <param name="A">Left side</param>
674        /// <param name="B">Right side</param>
675        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
676        /// <remarks>Only the real parts are compared!</remarks>
677        public static bool operator <(  fcomplex A,  Int32 B) {
678            return (A.real < B);
679        }
680        /// <summary>
681        /// Greater than or equal to comparison for complex numbers
682        /// </summary>
683        /// <param name="A">Left side</param>
684        /// <param name="B">Right side</param>
685        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
686        /// <remarks>Only the real parts are compared!</remarks>
687        public static bool operator >=( fcomplex A,  Int32 B) {
688            return (A.real >= B);
689        }
690        /// <summary>
691        /// Lower than or equal to comparison for complex numbers
692        /// </summary>
693        /// <param name="A">Left side</param>
694        /// <param name="B">Right side</param>
695        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
696        /// <remarks>Only the real parts are compared!</remarks>
697        public static bool operator <=( fcomplex A,  Int32 B) {
698            return (A.real <= B);
699        }
700       
701        /// <summary>
702        /// Add two complex numbers
703        /// </summary>
704        /// <param name="A">First summand</param>
705        /// <param name="B">Second summand</param>
706        /// <returns>Result</returns>
707        public static  fcomplex operator +( fcomplex A,  float B) {
708            fcomplex ret;
709            ret.real =  (float) (A.real + B);
710            ret.imag =  (float) A.imag;
711            return ret;
712        }
713        /// <summary>
714        /// Subtract two values
715        /// </summary>
716        /// <param name="A">Minuend</param>
717        /// <param name="B">Subtrahend</param>
718        /// <returns>result</returns>
719        public static  fcomplex operator -( fcomplex A,  float B) {
720            fcomplex ret;
721            ret.real =  (float) (A.real - B);
722            ret.imag =  (float) A.imag;
723            return ret;
724        }
725        /// <summary>
726        /// Multiply two values
727        /// </summary>
728        /// <param name="A">First factor</param>
729        /// <param name="B">Second factor</param>
730        /// <returns>result</returns>
731        public static  fcomplex operator *( fcomplex A,  float B) {
732            fcomplex ret;
733            ret.real =  (float) (A.real * B);
734            ret.imag =  (float) (A.imag * B);
735            return ret;
736        }
737        /// <summary>
738        /// Divide two numbers
739        /// </summary>
740        /// <param name="A">Divident</param>
741        /// <param name="B">Divisor</param>
742        /// <returns>result</returns>
743        public static  fcomplex operator /( fcomplex A,  float B) {
744            if (IsNaN(A)) return NaN;
745            if (float.IsNaN(B)) return NaN;
746            if (A.real == 0 && A.imag == 0) {
747                if (B == 0) return NaN;
748                return ( fcomplex )0;
749            } else {
750                if (float.IsInfinity(B))
751                {
752                    if (IsInfinity(A)) {
753                        return NaN;
754                    } else {
755                        return ( fcomplex )0;
756                    }
757                }
758            }
759            fcomplex ret;
760            if (B == 0) return INF ;
761            ret.real =  (float) (A.real / B);
762            ret.imag =  (float) (A.imag / B);
763            return ret;
764        }
765        /// <summary>
766        /// Equality comparison for complex numbers
767        /// </summary>
768        /// <param name="A">Left side</param>
769        /// <param name="B">Right side</param>
770        /// <returns>result</returns>
771        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
772        public static bool operator ==( fcomplex A,  float B) {
773            return (A.real == B && A.imag == 0.0);
774        }
775        /// <summary>
776        /// Unequality comparison for complex numbers
777        /// </summary>
778        /// <param name="A">Left side</param>
779        /// <param name="B">Right side</param>
780        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
781        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
782        public static bool operator !=( fcomplex A,  float B) {
783            return (A.imag != 0.0) || (A.real != B);
784        }
785        /// <summary>
786        /// Freater than comparison for complex numbers
787        /// </summary>
788        /// <param name="A">Left side</param>
789        /// <param name="B">Right side</param>
790        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
791        /// <remarks>Only the real parts are compared!</remarks>
792        public static bool operator > ( fcomplex A,  float B) {
793            return (A.real > B);
794        }
795        /// <summary>
796        /// Lower than comparison for complex numbers
797        /// </summary>
798        /// <param name="A">Left side</param>
799        /// <param name="B">Right side</param>
800        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
801        /// <remarks>Only the real parts are compared!</remarks>
802        public static bool operator <(  fcomplex A,  float B) {
803            return (A.real < B);
804        }
805        /// <summary>
806        /// Greater than or equal to comparison for complex numbers
807        /// </summary>
808        /// <param name="A">Left side</param>
809        /// <param name="B">Right side</param>
810        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
811        /// <remarks>Only the real parts are compared!</remarks>
812        public static bool operator >=( fcomplex A,  float B) {
813            return (A.real >= B);
814        }
815        /// <summary>
816        /// Lower than or equal to comparison for complex numbers
817        /// </summary>
818        /// <param name="A">Left side</param>
819        /// <param name="B">Right side</param>
820        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
821        /// <remarks>Only the real parts are compared!</remarks>
822        public static bool operator <=( fcomplex A,  float B) {
823            return (A.real <= B);
824        }
825       
826        /// <summary>
827        /// Add two complex numbers
828        /// </summary>
829        /// <param name="A">First summand</param>
830        /// <param name="B">Second summand</param>
831        /// <returns>Result</returns>
832        public static  fcomplex operator +( fcomplex A,  byte B) {
833            fcomplex ret;
834            ret.real =  (float) (A.real + B);
835            ret.imag =  (float) A.imag;
836            return ret;
837        }
838        /// <summary>
839        /// Subtract two values
840        /// </summary>
841        /// <param name="A">Minuend</param>
842        /// <param name="B">Subtrahend</param>
843        /// <returns>result</returns>
844        public static  fcomplex operator -( fcomplex A,  byte B) {
845            fcomplex ret;
846            ret.real =  (float) (A.real - B);
847            ret.imag =  (float) A.imag;
848            return ret;
849        }
850        /// <summary>
851        /// Multiply two values
852        /// </summary>
853        /// <param name="A">First factor</param>
854        /// <param name="B">Second factor</param>
855        /// <returns>result</returns>
856        public static  fcomplex operator *( fcomplex A,  byte B) {
857            fcomplex ret;
858            ret.real =  (float) (A.real * B);
859            ret.imag =  (float) (A.imag * B);
860            return ret;
861        }
862        /// <summary>
863        /// Divide two numbers
864        /// </summary>
865        /// <param name="A">Divident</param>
866        /// <param name="B">Divisor</param>
867        /// <returns>result</returns>
868        public static  fcomplex operator /( fcomplex A,  byte B) {
869            if (IsNaN(A)) return NaN;
870           
871            if (A.real == 0 && A.imag == 0) {
872                if (B == 0) return NaN;
873                return ( fcomplex )0;
874            } else {
875                if (false)
876                {
877                    if (IsInfinity(A)) {
878                        return NaN;
879                    } else {
880                        return ( fcomplex )0;
881                    }
882                }
883            }
884            fcomplex ret;
885            if (B == 0) return INF ;
886            ret.real =  (float) (A.real / B);
887            ret.imag =  (float) (A.imag / B);
888            return ret;
889        }
890        /// <summary>
891        /// Equality comparison for complex numbers
892        /// </summary>
893        /// <param name="A">Left side</param>
894        /// <param name="B">Right side</param>
895        /// <returns>result</returns>
896        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
897        public static bool operator ==( fcomplex A,  byte B) {
898            return (A.real == B && A.imag == 0.0);
899        }
900        /// <summary>
901        /// Unequality comparison for complex numbers
902        /// </summary>
903        /// <param name="A">Left side</param>
904        /// <param name="B">Right side</param>
905        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
906        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
907        public static bool operator !=( fcomplex A,  byte B) {
908            return (A.imag != 0.0) || (A.real != B);
909        }
910        /// <summary>
911        /// Freater than comparison for complex numbers
912        /// </summary>
913        /// <param name="A">Left side</param>
914        /// <param name="B">Right side</param>
915        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
916        /// <remarks>Only the real parts are compared!</remarks>
917        public static bool operator > ( fcomplex A,  byte B) {
918            return (A.real > B);
919        }
920        /// <summary>
921        /// Lower than comparison for complex numbers
922        /// </summary>
923        /// <param name="A">Left side</param>
924        /// <param name="B">Right side</param>
925        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
926        /// <remarks>Only the real parts are compared!</remarks>
927        public static bool operator <(  fcomplex A,  byte B) {
928            return (A.real < B);
929        }
930        /// <summary>
931        /// Greater than or equal to comparison for complex numbers
932        /// </summary>
933        /// <param name="A">Left side</param>
934        /// <param name="B">Right side</param>
935        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
936        /// <remarks>Only the real parts are compared!</remarks>
937        public static bool operator >=( fcomplex A,  byte B) {
938            return (A.real >= B);
939        }
940        /// <summary>
941        /// Lower than or equal to comparison for complex numbers
942        /// </summary>
943        /// <param name="A">Left side</param>
944        /// <param name="B">Right side</param>
945        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
946        /// <remarks>Only the real parts are compared!</remarks>
947        public static bool operator <=( fcomplex A,  byte B) {
948            return (A.real <= B);
949        }
950       
951        /// <summary>
952        /// Add two complex numbers
953        /// </summary>
954        /// <param name="A">First summand</param>
955        /// <param name="B">Second summand</param>
956        /// <returns>Result</returns>
957        public static  fcomplex operator +( fcomplex A,  double B) {
958            fcomplex ret;
959            ret.real =  (float) (A.real + B);
960            ret.imag =  (float) A.imag;
961            return ret;
962        }
963        /// <summary>
964        /// Subtract two values
965        /// </summary>
966        /// <param name="A">Minuend</param>
967        /// <param name="B">Subtrahend</param>
968        /// <returns>result</returns>
969        public static  fcomplex operator -( fcomplex A,  double B) {
970            fcomplex ret;
971            ret.real =  (float) (A.real - B);
972            ret.imag =  (float) A.imag;
973            return ret;
974        }
975        /// <summary>
976        /// Multiply two values
977        /// </summary>
978        /// <param name="A">First factor</param>
979        /// <param name="B">Second factor</param>
980        /// <returns>result</returns>
981        public static  fcomplex operator *( fcomplex A,  double B) {
982            fcomplex ret;
983            ret.real =  (float) (A.real * B);
984            ret.imag =  (float) (A.imag * B);
985            return ret;
986        }
987        /// <summary>
988        /// Divide two numbers
989        /// </summary>
990        /// <param name="A">Divident</param>
991        /// <param name="B">Divisor</param>
992        /// <returns>result</returns>
993        public static  fcomplex operator /( fcomplex A,  double B) {
994            if (IsNaN(A)) return NaN;
995            if (double.IsNaN(B)) return NaN;
996            if (A.real == 0 && A.imag == 0) {
997                if (B == 0) return NaN;
998                return ( fcomplex )0;
999            } else {
1000                if (double.IsInfinity(B))
1001                {
1002                    if (IsInfinity(A)) {
1003                        return NaN;
1004                    } else {
1005                        return ( fcomplex )0;
1006                    }
1007                }
1008            }
1009            fcomplex ret;
1010            if (B == 0) return INF ;
1011            ret.real =  (float) (A.real / B);
1012            ret.imag =  (float) (A.imag / B);
1013            return ret;
1014        }
1015        /// <summary>
1016        /// Equality comparison for complex numbers
1017        /// </summary>
1018        /// <param name="A">Left side</param>
1019        /// <param name="B">Right side</param>
1020        /// <returns>result</returns>
1021        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1022        public static bool operator ==( fcomplex A,  double B) {
1023            return (A.real == B && A.imag == 0.0);
1024        }
1025        /// <summary>
1026        /// Unequality comparison for complex numbers
1027        /// </summary>
1028        /// <param name="A">Left side</param>
1029        /// <param name="B">Right side</param>
1030        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
1031        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1032        public static bool operator !=( fcomplex A,  double B) {
1033            return (A.imag != 0.0) || (A.real != B);
1034        }
1035        /// <summary>
1036        /// Freater than comparison for complex numbers
1037        /// </summary>
1038        /// <param name="A">Left side</param>
1039        /// <param name="B">Right side</param>
1040        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1041        /// <remarks>Only the real parts are compared!</remarks>
1042        public static bool operator > ( fcomplex A,  double B) {
1043            return (A.real > B);
1044        }
1045        /// <summary>
1046        /// Lower than comparison for complex numbers
1047        /// </summary>
1048        /// <param name="A">Left side</param>
1049        /// <param name="B">Right side</param>
1050        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1051        /// <remarks>Only the real parts are compared!</remarks>
1052        public static bool operator <(  fcomplex A,  double B) {
1053            return (A.real < B);
1054        }
1055        /// <summary>
1056        /// Greater than or equal to comparison for complex numbers
1057        /// </summary>
1058        /// <param name="A">Left side</param>
1059        /// <param name="B">Right side</param>
1060        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1061        /// <remarks>Only the real parts are compared!</remarks>
1062        public static bool operator >=( fcomplex A,  double B) {
1063            return (A.real >= B);
1064        }
1065        /// <summary>
1066        /// Lower than or equal to comparison for complex numbers
1067        /// </summary>
1068        /// <param name="A">Left side</param>
1069        /// <param name="B">Right side</param>
1070        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1071        /// <remarks>Only the real parts are compared!</remarks>
1072        public static bool operator <=( fcomplex A,  double B) {
1073            return (A.real <= B);
1074        }
1075
1076#endregion HYCALPER AUTO GENERATED CODE
1077
1078
1079       
1080
1081#region HYCALPER AUTO GENERATED CODE
1082
1083       
1084        /// <summary>
1085        /// Add two complex values
1086        /// </summary>
1087        /// <param name="A">First summand</param>
1088        /// <param name="B">Second summand</param>
1089        /// <returns>Result</returns>
1090        public static  fcomplex operator +( Int64 A,  fcomplex B) {
1091            fcomplex ret;
1092            ret.real =  (float) (A + B.real);
1093            ret.imag =  (float) B.imag;
1094            return ret;
1095        }
1096        /// <summary>
1097        /// Subtract two values
1098        /// </summary>
1099        /// <param name="A">Minuend</param>
1100        /// <param name="B">Subtrahend</param>
1101        /// <returns>Result</returns>
1102        public static  fcomplex operator -( Int64 A,  fcomplex B) {
1103            fcomplex ret;
1104            ret.real =  (float) (A - B.real);
1105            ret.imag = - (float) B.imag;
1106            return ret;
1107        }
1108        /// <summary>
1109        /// Multiply two values
1110        /// </summary>
1111        /// <param name="A">First factor</param>
1112        /// <param name="B">Second factor</param>
1113        /// <returns>Result</returns>
1114        public static  fcomplex operator *( Int64 A,  fcomplex B) {
1115            fcomplex ret;
1116            ret.real =  (float) (A * B.real);
1117            ret.imag =  (float) (A * B.imag);
1118            return ret;
1119        }
1120        /// <summary>
1121        /// Divide two values
1122        /// </summary>
1123        /// <param name="A">Divident</param>
1124        /// <param name="B">Divisor</param>
1125        /// <returns>Result</returns>
1126        public static  fcomplex operator /( Int64 A,  fcomplex B) {
1127            fcomplex ret;
1128            if (A == 0) {
1129                if (IsInfinity(B)) return NaN;
1130            } else {
1131                if (IsInfinity(B)) return ( fcomplex )0;
1132            }
1133            if (B.real == 0 && B.imag == 0) {
1134                return INF;
1135            }
1136            // this algorithm is taken from [1]. The one described in [2] was not taken. Tests
1137            // did not show any advantage when using double precision floating point arithmetic.
1138            double tmp;
1139            if (Math.Abs(B.real) >= Math.Abs(B.imag)) {
1140                tmp =  (float) (B.imag * (1/B.real));
1141                ret.imag =  (float) (B.real + B.imag*tmp);
1142                ret.real =  (float) A/ret.imag;
1143                ret.imag = -  (float) (A*tmp)/ret.imag;
1144            } else {
1145                tmp =  (float) (B.real * (1/B.imag));
1146                ret.imag =  (float) (B.imag + B.real*tmp);
1147                ret.real =  (float) (A*tmp)/ret.imag;
1148                ret.imag = -  (float) A/ret.imag;
1149            }
1150            return ret;
1151        }
1152        /// <summary>
1153        /// Equality comparison for complex numbers
1154        /// </summary>
1155        /// <param name="A">Left side</param>
1156        /// <param name="B">Right side</param>
1157        /// <returns>Result</returns>
1158        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1159        public static bool operator ==( Int64 A,  fcomplex B) {
1160            return (B.real == A && B.imag == 0.0);
1161        }
1162        /// <summary>
1163        /// Unequality comparison for complex numbers
1164        /// </summary>
1165        /// <param name="A">Left side</param>
1166        /// <param name="B">Right side</param>
1167        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
1168        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1169        public static bool operator !=( Int64 A,  fcomplex B) {
1170            return (B.imag != 0.0) || (B.real != A);
1171        }
1172        /// <summary>
1173        /// Greater than comparison for complex numbers
1174        /// </summary>
1175        /// <param name="A">Left side</param>
1176        /// <param name="B">Right side</param>
1177        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1178        /// <remarks>Only the real parts are compared!</remarks>
1179        public static bool operator > ( Int64 A,  fcomplex B) {
1180            return (A > B.real);
1181        }
1182        /// <summary>
1183        /// Lower than comparison for complex numbers
1184        /// </summary>
1185        /// <param name="A">Left side</param>
1186        /// <param name="B">Right side</param>
1187        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1188        /// <remarks>Only the real parts are compared!</remarks>
1189        public static bool operator < ( Int64 A,  fcomplex B) {
1190            return (A < B.real);
1191        }
1192        /// <summary>
1193        /// Greater than or equal to comparison for complex numbers
1194        /// </summary>
1195        /// <param name="A">Left side</param>
1196        /// <param name="B">Right side</param>
1197        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1198        /// <remarks>Only the real parts are compared!</remarks>
1199        public static bool operator >=( Int64 A,  fcomplex B) {
1200            return (A >= B.real);
1201        }
1202        /// <summary>
1203        /// Lower than or equal to comparison for complex numbers
1204        /// </summary>
1205        /// <param name="A">Left side</param>
1206        /// <param name="B">Right side</param>
1207        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1208        /// <remarks>Only the real parts are compared!</remarks>
1209        public static bool operator <=( Int64 A,  fcomplex B) {
1210            return (A <= B.real);
1211        }
1212       
1213        /// <summary>
1214        /// Add two complex values
1215        /// </summary>
1216        /// <param name="A">First summand</param>
1217        /// <param name="B">Second summand</param>
1218        /// <returns>Result</returns>
1219        public static  fcomplex operator +( Int32 A,  fcomplex B) {
1220            fcomplex ret;
1221            ret.real =  (float) (A + B.real);
1222            ret.imag =  (float) B.imag;
1223            return ret;
1224        }
1225        /// <summary>
1226        /// Subtract two values
1227        /// </summary>
1228        /// <param name="A">Minuend</param>
1229        /// <param name="B">Subtrahend</param>
1230        /// <returns>Result</returns>
1231        public static  fcomplex operator -( Int32 A,  fcomplex B) {
1232            fcomplex ret;
1233            ret.real =  (float) (A - B.real);
1234            ret.imag = - (float) B.imag;
1235            return ret;
1236        }
1237        /// <summary>
1238        /// Multiply two values
1239        /// </summary>
1240        /// <param name="A">First factor</param>
1241        /// <param name="B">Second factor</param>
1242        /// <returns>Result</returns>
1243        public static  fcomplex operator *( Int32 A,  fcomplex B) {
1244            fcomplex ret;
1245            ret.real =  (float) (A * B.real);
1246            ret.imag =  (float) (A * B.imag);
1247            return ret;
1248        }
1249        /// <summary>
1250        /// Divide two values
1251        /// </summary>
1252        /// <param name="A">Divident</param>
1253        /// <param name="B">Divisor</param>
1254        /// <returns>Result</returns>
1255        public static  fcomplex operator /( Int32 A,  fcomplex B) {
1256            fcomplex ret;
1257            if (A == 0) {
1258                if (IsInfinity(B)) return NaN;
1259            } else {
1260                if (IsInfinity(B)) return ( fcomplex )0;
1261            }
1262            if (B.real == 0 && B.imag == 0) {
1263                return INF;
1264            }
1265            // this algorithm is taken from [1]. The one described in [2] was not taken. Tests
1266            // did not show any advantage when using double precision floating point arithmetic.
1267            double tmp;
1268            if (Math.Abs(B.real) >= Math.Abs(B.imag)) {
1269                tmp =  (float) (B.imag * (1/B.real));
1270                ret.imag =  (float) (B.real + B.imag*tmp);
1271                ret.real =  (float) A/ret.imag;
1272                ret.imag = -  (float) (A*tmp)/ret.imag;
1273            } else {
1274                tmp =  (float) (B.real * (1/B.imag));
1275                ret.imag =  (float) (B.imag + B.real*tmp);
1276                ret.real =  (float) (A*tmp)/ret.imag;
1277                ret.imag = -  (float) A/ret.imag;
1278            }
1279            return ret;
1280        }
1281        /// <summary>
1282        /// Equality comparison for complex numbers
1283        /// </summary>
1284        /// <param name="A">Left side</param>
1285        /// <param name="B">Right side</param>
1286        /// <returns>Result</returns>
1287        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1288        public static bool operator ==( Int32 A,  fcomplex B) {
1289            return (B.real == A && B.imag == 0.0);
1290        }
1291        /// <summary>
1292        /// Unequality comparison for complex numbers
1293        /// </summary>
1294        /// <param name="A">Left side</param>
1295        /// <param name="B">Right side</param>
1296        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
1297        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1298        public static bool operator !=( Int32 A,  fcomplex B) {
1299            return (B.imag != 0.0) || (B.real != A);
1300        }
1301        /// <summary>
1302        /// Greater than comparison for complex numbers
1303        /// </summary>
1304        /// <param name="A">Left side</param>
1305        /// <param name="B">Right side</param>
1306        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1307        /// <remarks>Only the real parts are compared!</remarks>
1308        public static bool operator > ( Int32 A,  fcomplex B) {
1309            return (A > B.real);
1310        }
1311        /// <summary>
1312        /// Lower than comparison for complex numbers
1313        /// </summary>
1314        /// <param name="A">Left side</param>
1315        /// <param name="B">Right side</param>
1316        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1317        /// <remarks>Only the real parts are compared!</remarks>
1318        public static bool operator < ( Int32 A,  fcomplex B) {
1319            return (A < B.real);
1320        }
1321        /// <summary>
1322        /// Greater than or equal to comparison for complex numbers
1323        /// </summary>
1324        /// <param name="A">Left side</param>
1325        /// <param name="B">Right side</param>
1326        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1327        /// <remarks>Only the real parts are compared!</remarks>
1328        public static bool operator >=( Int32 A,  fcomplex B) {
1329            return (A >= B.real);
1330        }
1331        /// <summary>
1332        /// Lower than or equal to comparison for complex numbers
1333        /// </summary>
1334        /// <param name="A">Left side</param>
1335        /// <param name="B">Right side</param>
1336        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1337        /// <remarks>Only the real parts are compared!</remarks>
1338        public static bool operator <=( Int32 A,  fcomplex B) {
1339            return (A <= B.real);
1340        }
1341       
1342        /// <summary>
1343        /// Add two complex values
1344        /// </summary>
1345        /// <param name="A">First summand</param>
1346        /// <param name="B">Second summand</param>
1347        /// <returns>Result</returns>
1348        public static  fcomplex operator +( float A,  fcomplex B) {
1349            fcomplex ret;
1350            ret.real =  (float) (A + B.real);
1351            ret.imag =  (float) B.imag;
1352            return ret;
1353        }
1354        /// <summary>
1355        /// Subtract two values
1356        /// </summary>
1357        /// <param name="A">Minuend</param>
1358        /// <param name="B">Subtrahend</param>
1359        /// <returns>Result</returns>
1360        public static  fcomplex operator -( float A,  fcomplex B) {
1361            fcomplex ret;
1362            ret.real =  (float) (A - B.real);
1363            ret.imag = - (float) B.imag;
1364            return ret;
1365        }
1366        /// <summary>
1367        /// Multiply two values
1368        /// </summary>
1369        /// <param name="A">First factor</param>
1370        /// <param name="B">Second factor</param>
1371        /// <returns>Result</returns>
1372        public static  fcomplex operator *( float A,  fcomplex B) {
1373            fcomplex ret;
1374            ret.real =  (float) (A * B.real);
1375            ret.imag =  (float) (A * B.imag);
1376            return ret;
1377        }
1378        /// <summary>
1379        /// Divide two values
1380        /// </summary>
1381        /// <param name="A">Divident</param>
1382        /// <param name="B">Divisor</param>
1383        /// <returns>Result</returns>
1384        public static  fcomplex operator /( float A,  fcomplex B) {
1385            fcomplex ret;
1386            if (A == 0) {
1387                if (IsInfinity(B)) return NaN;
1388            } else {
1389                if (IsInfinity(B)) return ( fcomplex )0;
1390            }
1391            if (B.real == 0 && B.imag == 0) {
1392                return INF;
1393            }
1394            // this algorithm is taken from [1]. The one described in [2] was not taken. Tests
1395            // did not show any advantage when using double precision floating point arithmetic.
1396            double tmp;
1397            if (Math.Abs(B.real) >= Math.Abs(B.imag)) {
1398                tmp =  (float) (B.imag * (1/B.real));
1399                ret.imag =  (float) (B.real + B.imag*tmp);
1400                ret.real =  (float) A/ret.imag;
1401                ret.imag = -  (float) (A*tmp)/ret.imag;
1402            } else {
1403                tmp =  (float) (B.real * (1/B.imag));
1404                ret.imag =  (float) (B.imag + B.real*tmp);
1405                ret.real =  (float) (A*tmp)/ret.imag;
1406                ret.imag = -  (float) A/ret.imag;
1407            }
1408            return ret;
1409        }
1410        /// <summary>
1411        /// Equality comparison for complex numbers
1412        /// </summary>
1413        /// <param name="A">Left side</param>
1414        /// <param name="B">Right side</param>
1415        /// <returns>Result</returns>
1416        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1417        public static bool operator ==( float A,  fcomplex B) {
1418            return (B.real == A && B.imag == 0.0);
1419        }
1420        /// <summary>
1421        /// Unequality comparison for complex numbers
1422        /// </summary>
1423        /// <param name="A">Left side</param>
1424        /// <param name="B">Right side</param>
1425        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
1426        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1427        public static bool operator !=( float A,  fcomplex B) {
1428            return (B.imag != 0.0) || (B.real != A);
1429        }
1430        /// <summary>
1431        /// Greater than comparison for complex numbers
1432        /// </summary>
1433        /// <param name="A">Left side</param>
1434        /// <param name="B">Right side</param>
1435        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1436        /// <remarks>Only the real parts are compared!</remarks>
1437        public static bool operator > ( float A,  fcomplex B) {
1438            return (A > B.real);
1439        }
1440        /// <summary>
1441        /// Lower than comparison for complex numbers
1442        /// </summary>
1443        /// <param name="A">Left side</param>
1444        /// <param name="B">Right side</param>
1445        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1446        /// <remarks>Only the real parts are compared!</remarks>
1447        public static bool operator < ( float A,  fcomplex B) {
1448            return (A < B.real);
1449        }
1450        /// <summary>
1451        /// Greater than or equal to comparison for complex numbers
1452        /// </summary>
1453        /// <param name="A">Left side</param>
1454        /// <param name="B">Right side</param>
1455        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1456        /// <remarks>Only the real parts are compared!</remarks>
1457        public static bool operator >=( float A,  fcomplex B) {
1458            return (A >= B.real);
1459        }
1460        /// <summary>
1461        /// Lower than or equal to comparison for complex numbers
1462        /// </summary>
1463        /// <param name="A">Left side</param>
1464        /// <param name="B">Right side</param>
1465        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1466        /// <remarks>Only the real parts are compared!</remarks>
1467        public static bool operator <=( float A,  fcomplex B) {
1468            return (A <= B.real);
1469        }
1470       
1471        /// <summary>
1472        /// Add two complex values
1473        /// </summary>
1474        /// <param name="A">First summand</param>
1475        /// <param name="B">Second summand</param>
1476        /// <returns>Result</returns>
1477        public static  fcomplex operator +( byte A,  fcomplex B) {
1478            fcomplex ret;
1479            ret.real =  (float) (A + B.real);
1480            ret.imag =  (float) B.imag;
1481            return ret;
1482        }
1483        /// <summary>
1484        /// Subtract two values
1485        /// </summary>
1486        /// <param name="A">Minuend</param>
1487        /// <param name="B">Subtrahend</param>
1488        /// <returns>Result</returns>
1489        public static  fcomplex operator -( byte A,  fcomplex B) {
1490            fcomplex ret;
1491            ret.real =  (float) (A - B.real);
1492            ret.imag = - (float) B.imag;
1493            return ret;
1494        }
1495        /// <summary>
1496        /// Multiply two values
1497        /// </summary>
1498        /// <param name="A">First factor</param>
1499        /// <param name="B">Second factor</param>
1500        /// <returns>Result</returns>
1501        public static  fcomplex operator *( byte A,  fcomplex B) {
1502            fcomplex ret;
1503            ret.real =  (float) (A * B.real);
1504            ret.imag =  (float) (A * B.imag);
1505            return ret;
1506        }
1507        /// <summary>
1508        /// Divide two values
1509        /// </summary>
1510        /// <param name="A">Divident</param>
1511        /// <param name="B">Divisor</param>
1512        /// <returns>Result</returns>
1513        public static  fcomplex operator /( byte A,  fcomplex B) {
1514            fcomplex ret;
1515            if (A == 0) {
1516                if (IsInfinity(B)) return NaN;
1517            } else {
1518                if (IsInfinity(B)) return ( fcomplex )0;
1519            }
1520            if (B.real == 0 && B.imag == 0) {
1521                return INF;
1522            }
1523            // this algorithm is taken from [1]. The one described in [2] was not taken. Tests
1524            // did not show any advantage when using double precision floating point arithmetic.
1525            double tmp;
1526            if (Math.Abs(B.real) >= Math.Abs(B.imag)) {
1527                tmp =  (float) (B.imag * (1/B.real));
1528                ret.imag =  (float) (B.real + B.imag*tmp);
1529                ret.real =  (float) A/ret.imag;
1530                ret.imag = -  (float) (A*tmp)/ret.imag;
1531            } else {
1532                tmp =  (float) (B.real * (1/B.imag));
1533                ret.imag =  (float) (B.imag + B.real*tmp);
1534                ret.real =  (float) (A*tmp)/ret.imag;
1535                ret.imag = -  (float) A/ret.imag;
1536            }
1537            return ret;
1538        }
1539        /// <summary>
1540        /// Equality comparison for complex numbers
1541        /// </summary>
1542        /// <param name="A">Left side</param>
1543        /// <param name="B">Right side</param>
1544        /// <returns>Result</returns>
1545        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1546        public static bool operator ==( byte A,  fcomplex B) {
1547            return (B.real == A && B.imag == 0.0);
1548        }
1549        /// <summary>
1550        /// Unequality comparison for complex numbers
1551        /// </summary>
1552        /// <param name="A">Left side</param>
1553        /// <param name="B">Right side</param>
1554        /// <returns>true if real and imaginary parts of A and B are not equal, false otherwise</returns>
1555        /// <remarks>Real inputs are converted to a complex number and the result is compared to the complex input.</remarks>
1556        public static bool operator !=( byte A,  fcomplex B) {
1557            return (B.imag != 0.0) || (B.real != A);
1558        }
1559        /// <summary>
1560        /// Greater than comparison for complex numbers
1561        /// </summary>
1562        /// <param name="A">Left side</param>
1563        /// <param name="B">Right side</param>
1564        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1565        /// <remarks>Only the real parts are compared!</remarks>
1566        public static bool operator > ( byte A,  fcomplex B) {
1567            return (A > B.real);
1568        }
1569        /// <summary>
1570        /// Lower than comparison for complex numbers
1571        /// </summary>
1572        /// <param name="A">Left side</param>
1573        /// <param name="B">Right side</param>
1574        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1575        /// <remarks>Only the real parts are compared!</remarks>
1576        public static bool operator < ( byte A,  fcomplex B) {
1577            return (A < B.real);
1578        }
1579        /// <summary>
1580        /// Greater than or equal to comparison for complex numbers
1581        /// </summary>
1582        /// <param name="A">Left side</param>
1583        /// <param name="B">Right side</param>
1584        /// <returns>true if real part of A is greater than real part of B, false otherwise</returns>
1585        /// <remarks>Only the real parts are compared!</remarks>
1586        public static bool operator >=( byte A,  fcomplex B) {
1587            return (A >= B.real);
1588        }
1589        /// <summary>
1590        /// Lower than or equal to comparison for complex numbers
1591        /// </summary>
1592        /// <param name="A">Left side</param>
1593        /// <param name="B">Right side</param>
1594        /// <returns>true if real part of A is lower then real part of B, false otherwise</returns>
1595        /// <remarks>Only the real parts are compared!</remarks>
1596        public static bool operator <=( byte A,  fcomplex B) {
1597            return (A <= B.real);
1598        }
1599
1600#endregion HYCALPER AUTO GENERATED CODE
1601
1602        #region unary minus
1603        /// <summary>
1604        /// Unary minus operator
1605        /// </summary>
1606        /// <param name="in1">fcomplex input</param>
1607        /// <returns>fcomplex number similar to in1, having real and imag part negated</returns>
1608        public static fcomplex operator -( fcomplex in1) {
1609            fcomplex ret = new fcomplex();
1610            ret.imag = -in1.imag;
1611            ret.real = -in1.real;
1612            return ret;
1613        }
1614        #endregion
1615
1616        /// <summary>
1617        /// Magnitude value of float complex number
1618        /// </summary>
1619        /// <param name="input">fcomplex number</param>
1620        /// <returns>Magnitude of input</returns>
1621        public static float Abs(fcomplex input) {
1622            return (float) Math.Sqrt ( input.real * input.real + input.imag * input.imag );
1623        }
1624        /// <summary>
1625        /// Angle of complex number
1626        /// </summary>
1627        /// <param name="input">fcomplex number to compute angle of</param>
1628        /// <returns>Angle of input</returns>
1629        public static double Angle(fcomplex input) {
1630            return (float) Math.Atan2 ( input.imag, input.real );
1631        }
1632        /// <summary>
1633        /// Arcus cosinus for float complex number
1634        /// </summary>
1635        /// <param name="input">fcomplex input</param>
1636        /// <returns>Arcus cosinus of input</returns>
1637        /// <remarks>The arcus cosinus of a complex number is computed by
1638        /// <para>Log(Sqrt(input^2 - 1) + input) * i </para></remarks>
1639        public static fcomplex Acos(fcomplex input) {
1640            fcomplex ret = new fcomplex ( 0, -1 );
1641            return fcomplex.Log ( fcomplex.Sqrt ( input * input - 1 )
1642                + input ) * ret;
1643        }
1644        /// <summary>
1645        /// Arcus cosinus of real number
1646        /// </summary>
1647        /// <param name="input">float input</param>
1648        /// <returns>Arcus cosinus of input</returns>
1649        /// <remarks>For input > 1.0, <see cref="ILNumerics.fcomplex.Acos(fcomplex)"/> will be used. </remarks>
1650        public static fcomplex Acos(float input) {
1651            if (Math.Abs(input) <= 1.0)
1652                return new fcomplex((float)Math.Acos(input), 0.0f);
1653            else {
1654                return Acos((fcomplex)input);
1655            }
1656        }
1657        /// <summary>
1658        /// Arcus sinus of real number
1659        /// </summary>
1660        /// <param name="input">float input</param>
1661        /// <returns>Arcus sinus of input</returns>
1662        /// <remarks>For input > 1.0, <see cref="ILNumerics.fcomplex.Asin(fcomplex)"/> will be used. </remarks>
1663        public static fcomplex Asin(float input) {
1664            if (Math.Abs(input) <= 1.0)
1665                return new fcomplex((float)Math.Asin(input), 0.0f);
1666            else {
1667                return Asin((fcomplex)input);
1668            }
1669        }
1670        /// <summary>
1671        /// Arcus sinus for complex number
1672        /// </summary>
1673        /// <param name="input">fcomplex input</param>
1674        /// <returns>Arcus sinus of input</returns>
1675        public static fcomplex Asin(fcomplex input) {
1676            fcomplex ret = Acos ( input );
1677            ret.real = (float) (Math.PI / 2 - ret.real);
1678            return ret;
1679        }
1680        /// <summary>
1681        /// Power of base e for float complex number
1682        /// </summary>
1683        /// <param name="input">fcomplex input</param>
1684        /// <returns>Result of Exp(input)</returns>
1685        public static fcomplex Exp(fcomplex input) {
1686            return fcomplex.FromPol ( (float) Math.Exp ( input.real ), input.imag );
1687        }
1688        /// <summary>
1689        /// fcomplex power real exponent
1690        /// </summary>
1691        /// <param name="input">Basis </param>
1692        /// <param name="exponent">Exponent</param>
1693        /// <returns>New fcomplex number with result</returns>
1694        public static fcomplex Pow(fcomplex input, double exponent) {
1695            fcomplex ret = input.Log ();
1696            ret.imag *= (float) exponent;
1697            ret.real *= (float) exponent;
1698            return ret.Exp ();
1699        }
1700        /// <summary>
1701        /// Complex power - real basis, real exponent
1702        /// </summary>
1703        /// <param name="basis">Basis</param>
1704        /// <param name="exponent">Exponent</param>
1705        /// <returns>fcomplex number.</returns>
1706        /// <remarks>The result will be a fcomplex number. For negative basis
1707        /// the basis will be converted to a fcomplex number and the power
1708        /// will be computed in the fcomplex plane.</remarks>
1709        public static fcomplex Pow(double basis, double exponent) {
1710            if (basis < 0) {
1711                return Pow((fcomplex)basis, exponent);
1712            } else {
1713                return (fcomplex)Math.Pow(basis, exponent);
1714            }
1715        }
1716        /// <summary>
1717        /// Power: complex base, complex exponent
1718        /// </summary>
1719        /// <param name="basis">Basis</param>
1720        /// <param name="exponent">Exponent</param>
1721        /// <returns>result of basis^exponent</returns>
1722        public static fcomplex Pow(fcomplex basis, fcomplex exponent) {
1723            fcomplex ret = ( basis.Log () * exponent );
1724            return ret.Exp ();
1725        }
1726        /// <summary>
1727        /// Square root of real input
1728        /// </summary>
1729        /// <param name="input">float input</param>
1730        /// <returns>Square root of input</returns>
1731        public static fcomplex Sqrt(float input) {
1732            if (input > 0)
1733                return new fcomplex((float)Math.Sqrt(input), 0.0f);
1734            else
1735                return Sqrt((fcomplex)input);
1736        }
1737        /// <summary>
1738        /// Square root of complex number
1739        /// </summary>
1740        /// <param name="input">fcomplex input</param>
1741        /// <returns>Square root of input</returns>
1742        public static fcomplex Sqrt(fcomplex input) {
1743            // Reference : numerical recipes in C: Appendix C
1744            fcomplex ret = new fcomplex ();
1745            double x, y, w, r;
1746            if (input.real == 0.0 && input.imag == 0.0)
1747                return ret;
1748            else {
1749                x = (float) Math.Abs ( input.real );
1750                y = (float) Math.Abs ( input.imag );
1751                if (x >= y) {
1752                    r = y / x;
1753                    w = Math.Sqrt ( x ) * Math.Sqrt ( 0.5 * ( 1.0 + Math.Sqrt ( 1.0 + r * r ) ) );
1754                } else {
1755                    r = x / y;
1756                    w = Math.Sqrt ( y ) * Math.Sqrt ( 0.5 * ( r + Math.Sqrt ( 1.0 + r * r ) ) );
1757                }
1758                if (input.real >= 0.0) {
1759                    ret.real = (float) w;
1760                    ret.imag = (float) (input.imag / ( 2.0 * w ));
1761                } else {
1762                    ret.imag = (float) (( input.imag >= 0 ) ? w : -w);
1763                    ret.real = (float) (input.imag / ( 2.0 * ret.imag ));
1764                }
1765                return ret;
1766            }
1767        }
1768        /// <summary>
1769        /// Tangens of float complex number
1770        /// </summary>
1771        /// <param name="input">fcomplex input</param>
1772        /// <returns>Tangens of input</returns>
1773        public static fcomplex Tan(fcomplex input) {
1774            fcomplex ci = Cos(input);
1775            if (ci.real == (float)0.0 && ci.imag == (float)0.0)
1776                return INF;
1777            return (Sin(input) / ci);
1778        }
1779        /// <summary>
1780        /// Tangens hyperbolicus of float complex input
1781        /// </summary>
1782        /// <param name="input">fcomplex input</param>
1783        /// <returns>Tangens hyperbolicus</returns>
1784        public static fcomplex Tanh(fcomplex input) {
1785            fcomplex si = Sin(input);
1786            if (si.real == (float)0.0 && si.imag == (float)0.0)
1787                return INF;
1788            return (Cos(input) / si);
1789        }
1790        /// <summary>
1791        /// Logarithm of complex input
1792        /// </summary>
1793        /// <param name="input">fcomplex input</param>
1794        /// <returns>Logarithm of input</returns>
1795        public static fcomplex Log(fcomplex input) {
1796            fcomplex ret = new fcomplex ();
1797            ret.real = (float) Math.Log ( Math.Sqrt ( input.real * input.real + input.imag * input.imag ) );
1798            ret.imag = (float) Math.Atan2 ( input.imag, input.real );
1799            return ret;
1800        }
1801        /// <summary>
1802        /// Logarithm to base 10
1803        /// </summary>
1804        /// <param name="input">fcomplex input</param>
1805        /// <returns>Logarithm of input</returns>
1806        public static fcomplex Log10(fcomplex input) {
1807            return Log(input) / 2.30258509299405f;
1808        }
1809        /// <summary>
1810        /// Logarithm of base 2
1811        /// </summary>
1812        /// <param name="input">fcomplex input</param>
1813        /// <returns>Logarithm of input</returns>
1814        public static fcomplex Log2(fcomplex input) {
1815            return Log(input) / 0.693147180559945f;
1816        }
1817        /// <summary>
1818        /// Logarithm of real input
1819        /// </summary>
1820        /// <param name="input">float input - may be negative</param>
1821        /// <returns>Complex logarithm</returns>
1822        public static fcomplex Log(float input) {
1823            return Log (new fcomplex(input,0.0f));
1824        }
1825        /// <summary>
1826        /// Logarithm of base 10 of real input
1827        /// </summary>
1828        /// <param name="input">float input - may be negative</param>
1829        /// <returns>Complex logarithm of base 10</returns>
1830        public static fcomplex Log10(float input) {
1831            return Log(new fcomplex(input,0.0f)) / 2.30258509299405f;
1832        }
1833        /// <summary>
1834        /// Logarithm of base 2
1835        /// </summary>
1836        /// <param name="input">float input - may be negative</param>
1837        /// <returns>Complex logarithm of base 2</returns>
1838        public static fcomplex Log2(float input) {
1839            return Log(new fcomplex(input,0.0f)) / 0.693147180559945f;
1840        }
1841        /// <summary>
1842        /// Convert from polar to cartesian form
1843        /// </summary>
1844        /// <param name="magnitude">Magnitude</param>
1845        /// <param name="angle">Angle</param>
1846        /// <returns>fcomplex number with magnitude <c>magnitude</c>
1847        /// and phase <c>angle</c></returns>
1848        public static fcomplex FromPol(float magnitude, float angle) {
1849            return new fcomplex (
1850                (magnitude * (float)Math.Cos ( angle )),
1851                (magnitude * (float)Math.Sin ( angle ))
1852            );
1853        }
1854        /// <summary>
1855        /// Convert this float complex number to string
1856        /// </summary>
1857        /// <returns>String representation of this float complex number</returns>
1858        public override String ToString() {
1859            if (imag>=0)
1860                return String.Format("{0} + {1}i",real,imag);
1861            else
1862                return String.Format("{0} {1}i",real,imag);
1863        }
1864        private static string m_precSpecI = "";
1865        private static string m_precSpecR = "";
1866        private static int m_lastDigits = 0;
1867        /// <summary>
1868        /// Print formated output of this number, determine number of digits
1869        /// </summary>
1870        /// <param name="digits">Number of digits</param>
1871        /// <returns>Formatted output</returns>
1872        public string ToString(int digits) {
1873            if (digits < 1) return "";
1874            if (digits != m_lastDigits) {
1875                m_lastDigits = digits;
1876                m_precSpecR = String.Format("{{0:f{0}}}",digits);
1877                m_precSpecI = String.Format("{{1:f{0}}}i",digits);
1878            }
1879            if (imag >= 0) {
1880                return String.Format(m_precSpecR+"+"+m_precSpecI,real,imag);
1881            } else {
1882                return String.Format(m_precSpecR+m_precSpecI,real,imag);
1883            }
1884        }
1885        /// <summary>
1886        /// Magnitude of this float complex number
1887        /// </summary>
1888        /// <returns>Magnitude</returns>
1889        public float Abs() {
1890            return (float)Math.Sqrt(real * real + imag * imag);
1891        }
1892        /// <summary>
1893        /// Phase angle of this float complex number
1894        /// </summary>
1895        /// <returns>Phase angle </returns>
1896        public double Angle() {
1897            return (float)Math.Atan2(imag, real);
1898        }
1899        /// <summary>
1900        /// Arcus cosinus of this float complex number
1901        /// </summary>
1902        /// <returns>Arcus cosinus</returns>
1903        public fcomplex Acos() {
1904            fcomplex ret = new fcomplex(0, -1);
1905            return fcomplex.Log(fcomplex.Sqrt(this * this - 1)
1906                + this) * ret;
1907        }
1908        /// <summary>
1909        /// Arcus sinus of this float complex number
1910        /// </summary>
1911        /// <returns>Arcus sinus</returns>
1912        public fcomplex Asin() {
1913            fcomplex ret = Acos(this);
1914            ret.real = (float)(Math.PI / 2 - ret.real);
1915            return ret;
1916        }
1917        /// <summary>
1918        /// Arcus tangens of float complex number
1919        /// </summary>
1920        /// <param name="input">fcomplex input</param>
1921        /// <returns>Arcus tangens of input</returns>
1922        public static fcomplex Atan(fcomplex input) {
1923            fcomplex ret = new fcomplex(0, (float)0.5);
1924            return (ret * Log((fcomplex.i + input) / (fcomplex.i - input)));
1925        }
1926        /// <summary>
1927        /// Round towards next greater integer
1928        /// </summary>
1929        /// <param name="input">fcomplex input</param>
1930        /// <returns>Rounded float complex number</returns>
1931        /// <remarks>Real and imaginary parts are independently rounded
1932        /// towards the next integer value towards positive infinity.</remarks>
1933        public static fcomplex Ceiling (fcomplex input){
1934            return new fcomplex(
1935                    (float)Math.Ceiling(input.real),
1936                    (float)Math.Ceiling(input.imag)
1937            );
1938        }
1939        /// <summary>
1940        /// Round towards next lower integer
1941        /// </summary>
1942        /// <param name="input">fcomplex input</param>
1943        /// <returns>Rounded float complex number</returns>
1944        /// <remarks>Real and imaginary parts are independently rounded
1945        /// towards the next integer value towards negative infinity.</remarks>
1946        public static fcomplex Floor (fcomplex input){
1947            return new fcomplex(
1948                    (float)Math.Floor(input.real),
1949                    (float)Math.Floor(input.imag)
1950            );
1951        }
1952        /// <summary>
1953        /// Round mercantilistic
1954        /// </summary>
1955        /// <param name="input">fcomplex number</param>
1956        /// <returns>Rounded number</returns>
1957        /// <remarks>Real and imaginaty parts are rounded independently. </remarks>
1958        public static fcomplex Round (fcomplex input){
1959            return new fcomplex(
1960                    (float)Math.Round(input.real),
1961                    (float)Math.Round(input.imag)
1962            );
1963        }
1964        /// <summary>
1965        /// Signum function
1966        /// </summary>
1967        /// <param name="input">fcomplex input</param>
1968        /// <returns> Signum of input</returns>
1969        /// <remarks>
1970        /// For numbers a = 0.0 + 0.0i, sign(a)'s real and imag parts are 0.0.
1971        /// For all other numbers sign(a) is the projection onto the unit circle.</remarks>
1972        public static fcomplex Sign(fcomplex input){
1973            if (input.real == 0.0 && input.imag == 0.0)
1974                return new fcomplex();
1975            else {
1976                float mag = (float)Math.Sqrt(input.real * input.real + input.imag * input.imag);
1977                return new fcomplex(
1978                    input.real / mag,
1979                    input.imag / mag);
1980            }
1981        }
1982        /// <summary>
1983        /// Truncate a floating point complex value
1984        /// </summary>
1985        /// <param name="input">fcomplex input</param>
1986        /// <returns>Integer part of input</returns>
1987        /// <remarks>Operates on real and imaginary parts seperately.</remarks>
1988        public static fcomplex Truncate (fcomplex input){
1989            return new fcomplex(
1990                    (float)Math.Truncate(input.real),
1991                    (float)Math.Truncate(input.imag)
1992            );
1993        }
1994        /// <summary>
1995        /// Cosinus
1996        /// </summary>
1997        /// <param name="input">fcomplex input</param>
1998        /// <returns>Cosinus of input</returns>
1999        /// <remarks><para>The cosinus is computed by the trigonometric euler equation: </para>
2000        /// <para>0.5 * [exp(i input) + exp(-i input)]</para></remarks>
2001        public static fcomplex Cos(fcomplex input) {
2002            fcomplex i = new fcomplex(0, 1.0f);
2003            fcomplex ni = new fcomplex(0, -1.0f);
2004            return (Exp(i * input) + Exp(ni * input)) / 2.0f;
2005        }
2006        /// <summary>
2007        /// Cosinus hyperbolicus
2008        /// </summary>
2009        /// <param name="input">fcomplex input</param>
2010        /// <returns>Cosinus hyperbolicus of input</returns>
2011        /// <remarks><para>The cosinus is computed by the trigonometric euler equation: </para>
2012        /// <para>(Exp(input) + Exp(-1.0 * input)) / 2.0</para></remarks>
2013        public static fcomplex Cosh(fcomplex input) {
2014            return (Exp(input) + Exp(-1.0f * input)) / 2.0f;
2015        }
2016        /// <summary>
2017        /// Sinus
2018        /// </summary>
2019        /// <param name="input">fcomplex input</param>
2020        /// <returns>Sinus of input</returns>
2021        /// <remarks><para>The sinus is computed by the trigonometric euler equation: </para>
2022        /// <para>(Exp(i * input) - Exp(-1.0 * i * input)) / (2.0 * i)</para></remarks>
2023        public static fcomplex Sin(fcomplex input) {
2024            fcomplex i = new fcomplex(0, (float)1.0);
2025            fcomplex mi = new fcomplex(0, (float)-1.0);
2026            return (Exp(i * input) - Exp(mi * input)) / (2.0 * i);
2027        }
2028        /// <summary>
2029        /// Sinus hyperbolicus
2030        /// </summary>
2031        /// <param name="input">fcomplex input</param>
2032        /// <returns>Sinus hyperbolicus of input</returns>
2033        /// <remarks><para>The sinus hyperbolicus is computed by the trigonometric euler equation: </para>
2034        /// <para>(Exp(input) - Exp(-1.0 * input)) / 2.0</para></remarks>
2035        public static fcomplex Sinh(fcomplex input) {
2036            fcomplex ret = new fcomplex(0, 2);
2037            fcomplex i = new fcomplex(0, (float)1.0);
2038            fcomplex mi = new fcomplex(0, (float)-1.0);
2039            return (Exp(input) - Exp(-1.0 * input)) / 2.0;
2040        }
2041        /// <summary>
2042        /// Exponential / power of base e
2043        /// </summary>
2044        /// <returns>Power of base e</returns>
2045        public fcomplex Exp() {
2046            return fcomplex.FromPol((float)Math.Exp(real), imag);
2047        }
2048        /// <summary>
2049        /// Power of fcomplex number, real exponent
2050        /// </summary>
2051        /// <param name="exponent">Exponent</param>
2052        /// <returns>New fcomplex number with result</returns>
2053        public fcomplex Pow(double exponent) {
2054            fcomplex ret = Log();
2055            ret.imag *= (float)exponent;
2056            ret.real *= (float)exponent;
2057            return ret.Exp();
2058        }
2059        /// <summary>
2060        /// Power of fcomplex number, complex exponent
2061        /// </summary>
2062        /// <param name="exponent">Exponent</param>
2063        /// <returns>New fcomplex number with result</returns>
2064        public fcomplex Pow(fcomplex exponent) {
2065            fcomplex ret = (Log() * exponent);
2066            return ret.Exp();
2067        }
2068        /// <summary>
2069        /// Square root of fcomplex number
2070        /// </summary>
2071        /// <returns>Square root</returns>
2072        public fcomplex Sqrt() {
2073            // Reference : numerical recipes in C: Appendix C
2074            fcomplex ret = new fcomplex();
2075            double x, y, w, r;
2076            if ( real == 0.0 && imag == 0.0)
2077                return ret;
2078            else {
2079                x = (float)Math.Abs(real);
2080                y = (float)Math.Abs( imag);
2081                if (x >= y) {
2082                    r = y / x;
2083                    w = Math.Sqrt(x) * Math.Sqrt(0.5 * (1.0 + Math.Sqrt(1.0 + r * r)));
2084                } else {
2085                    r = x / y;
2086                    w = Math.Sqrt(y) * Math.Sqrt(0.5 * (r + Math.Sqrt(1.0 + r * r)));
2087                }
2088                if ( real >= 0.0) {
2089                    ret.real = (float)w;
2090                    ret.imag = (float)( imag / (2.0 * w));
2091                } else {
2092                    ret.imag = (float)(( imag >= 0) ? w : -w);
2093                    ret.real = (float)( imag / (2.0 * ret.imag));
2094                }
2095                return ret;
2096            }
2097        }
2098        /// <summary>
2099        /// Logarithm of fcomplex number
2100        /// </summary>
2101        /// <returns>Natural logarithm</returns>
2102        /// <remarks>The logarithm of a complex number A is defined as follows: <br />
2103        /// <list type="none"><item>real part: log(abs(A))</item>
2104        /// <item>imag part: Atan2(imag(A),real(A))</item></list>
2105        /// </remarks>
2106        public fcomplex Log() {
2107            fcomplex ret = new fcomplex();
2108            ret.real = (float)Math.Log(Math.Sqrt( real *  real +  imag *  imag));
2109            ret.imag = (float)Math.Atan2( imag,  real);
2110            return ret;
2111        }
2112        /// <summary>
2113        /// Test if any of real or imaginary parts are NAN's
2114        /// </summary>
2115        /// <param name="input">fcomplex input</param>
2116        /// <returns>true if any of real or imag part is not a number</returns>
2117        public static bool IsNaN(fcomplex input) {
2118            if (Single.IsNaN(input.real) || Single.IsNaN(input.imag))
2119                return true;
2120            else
2121                return false;
2122        }
2123        /// <summary>
2124        /// Test if any of real or imaginary parts are infinite
2125        /// </summary>
2126        /// <param name="input">fcomplex input</param>
2127        /// <returns>true if any of real or imag part is infinite</returns>
2128        public static bool IsInfinity(fcomplex input) { 
2129            if (Single.IsInfinity(input.real) || Single.IsInfinity(input.imag))
2130                return true;
2131            else
2132                return false;
2133        }
2134        /// <summary>
2135        /// Test if any of real or imaginary parts are pos. infinite
2136        /// </summary>
2137        /// <param name="input">fcomplex input</param>
2138        /// <returns>true if any of real or imag part is positive infinite</returns>
2139        public static bool IsPositiveInfinity(fcomplex input) { 
2140            if (Single.IsPositiveInfinity(input.real) || Single.IsPositiveInfinity(input.imag))
2141                return true;
2142            else
2143                return false;
2144        }
2145        /// <summary>
2146        /// Test if any of real or imaginary parts are neg. infinite
2147        /// </summary>
2148        /// <param name="input">fcomplex input</param>
2149        /// <returns>true if any of real or imag part is negative infinite</returns>
2150        public static bool IsNegativeInfinity(fcomplex input) { 
2151            if (Single.IsNegativeInfinity(input.real) || Single.IsNegativeInfinity(input.imag))
2152                return true;
2153            else
2154                return false;
2155        }
2156        /// <summary>
2157        /// Test if any of real or imaginary parts are finite
2158        /// </summary>
2159        /// <param name="input">fcomplex input</param>
2160        /// <returns>true if any of real and imag part is finite</returns>
2161        public static bool IsFinite (fcomplex input) {
2162            if (ILMath.isfinite(input.real) && ILMath.isfinite(input.imag))
2163                return true;
2164            else
2165                return false;
2166        }
2167
2168        #region CAST_OPERATORS
2169        /// <summary>
2170        /// Implicit cast real number into complex number
2171        /// </summary>
2172        /// <param name="a">double</param>
2173        /// <returns>fcomplex number with real part equals a</returns>
2174        public static implicit operator fcomplex(double a) {
2175            return new fcomplex((float)a, 0.0F);
2176        }
2177        /// <summary>
2178        /// Implicit cast real number into complex number
2179        /// </summary>
2180        /// <param name="a">float</param>
2181        /// <returns>fcomplex number with real part equals a</returns>
2182        public static implicit operator fcomplex(float a) {
2183            return new fcomplex(a, 0.0F);
2184        }
2185        /// <summary>
2186        /// Implicit cast real number into complex number
2187        /// </summary>
2188        /// <param name="a">byte</param>
2189        /// <returns>fcomplex number with real part equals a</returns>
2190        public static implicit operator fcomplex(byte a) {
2191            return new fcomplex(a, 0.0F);
2192        }
2193        /// <summary>
2194        /// Implicit cast real number into complex number
2195        /// </summary>
2196        /// <param name="a">char</param>
2197        /// <returns>fcomplex number with real part equals a</returns>
2198        public static implicit operator fcomplex(char a) {
2199            return new fcomplex(a, 0.0F);
2200        }
2201        /// <summary>
2202        /// Implicit cast real number into complex number
2203        /// </summary>
2204        /// <param name="a">Int16</param>
2205        /// <returns>fcomplex number with real part equals a</returns>
2206        public static implicit operator fcomplex(Int16 a) {
2207            return new fcomplex(a, 0.0F);
2208        }
2209        /// <summary>
2210        /// Implicit cast real number into complex number
2211        /// </summary>
2212        /// <param name="a">Int32</param>
2213        /// <returns>fcomplex number with real part equals a</returns>
2214        public static implicit operator fcomplex(Int32 a) {
2215            return new fcomplex((float)a, 0.0F);   
2216        }
2217        /// <summary>
2218        /// Implicit cast real number into complex number
2219        /// </summary>
2220        /// <param name="a">Int64</param>
2221        /// <returns>fcomplex number with real part equals a</returns>
2222        public static implicit operator fcomplex(Int64 a) {
2223            return new fcomplex((float)a, 0.0F);
2224        }
2225        /// <summary>
2226        /// Implicit cast real number into complex number
2227        /// </summary>
2228        /// <param name="a">UInt16</param>
2229        /// <returns>fcomplex number with real part equals a</returns>
2230        public static implicit operator fcomplex(UInt16 a) {
2231            return new fcomplex((float)a, 0.0F);
2232        }
2233        /// <summary>
2234        /// Implicit cast real number into complex number
2235        /// </summary>
2236        /// <param name="a">UInt32</param>
2237        /// <returns>fcomplex number with real part equals a</returns>
2238        public static implicit operator fcomplex(UInt32 a) {
2239            return new fcomplex((float)a, 0.0F);
2240        }
2241        /// <summary>
2242        /// Implicit cast real number into complex number
2243        /// </summary>
2244        /// <param name="a">UInt64</param>
2245        /// <returns>fcomplex number with real part equals a</returns>
2246        public static implicit operator fcomplex(UInt64 a) {
2247            return new fcomplex((float)a, 0.0F);
2248        }
2249
2250        /// <summary>
2251        /// Explicit cast complex number into real number
2252        /// </summary>
2253        /// <param name="a">fcomplex number</param>
2254        /// <returns>Real number with real part of a</returns>
2255        public static explicit operator double(fcomplex a) {
2256            return a.real;
2257        }
2258        /// <summary>
2259        /// Explicit cast complex number into real number
2260        /// </summary>
2261        /// <param name="a">fcomplex number</param>
2262        /// <returns>Real number with real part of a</returns>
2263        public static explicit operator float(fcomplex a) {
2264            return (float)a.real;
2265        }
2266        /// <summary>
2267        /// Explicit cast complex number into real number
2268        /// </summary>
2269        /// <param name="a">fcomplex number</param>
2270        /// <returns>Real number with real part of a</returns>
2271        public static explicit operator byte(fcomplex a) {
2272            return (byte) a.real;
2273        }
2274        /// <summary>
2275        /// Explicit cast complex number into real number
2276        /// </summary>
2277        /// <param name="a">fcomplex number</param>
2278        /// <returns>Real number with real part of a</returns>
2279        public static explicit operator char(fcomplex a) {
2280            return (char) a.real;
2281        }
2282        /// <summary>
2283        /// Explicit cast complex number into real number
2284        /// </summary>
2285        /// <param name="a">fcomplex number</param>
2286        /// <returns>Real number with real part of a</returns>
2287        public static explicit operator Int16(fcomplex a) {
2288            return (Int16) a.real;
2289        }
2290        /// <summary>
2291        /// Explicit cast complex number into real number
2292        /// </summary>
2293        /// <param name="a">complex number</param>
2294        /// <returns>Real number with real part of a</returns>
2295        public static explicit operator Int32(fcomplex a) {
2296            return (Int32) a.real;
2297        }
2298        /// <summary>
2299        /// Explicit cast complex number into real number
2300        /// </summary>
2301        /// <param name="a">fcomplex number</param>
2302        /// <returns>Real number with real part of a</returns>
2303        public static explicit operator Int64(fcomplex a) {
2304            return (Int64) a.real;
2305        }
2306        /// <summary>
2307        /// Explicit cast complex number into real number
2308        /// </summary>
2309        /// <param name="a">fcomplex number</param>
2310        /// <returns>Real number with real part of a</returns>
2311        public static explicit operator UInt16(fcomplex a) {
2312            return (UInt16) a.real;
2313        }
2314        /// <summary>
2315        /// Explicit cast complex number into real number
2316        /// </summary>
2317        /// <param name="a">fcomplex number</param>
2318        /// <returns>Real number with real part of a</returns>
2319        public static explicit operator UInt32(fcomplex a) {
2320            return (UInt32) a.real;
2321        }
2322        /// <summary>
2323        /// Explicit cast complex number into real number
2324        /// </summary>
2325        /// <param name="a">fcomplex number</param>
2326        /// <returns>Real number with real part of a</returns>
2327        public static explicit operator UInt64(fcomplex a) {
2328            return (UInt64) a.real;
2329        }
2330        /// <summary>
2331        /// Test if real and imag part are zero
2332        /// </summary>
2333        /// <returns>true if real and imag parts are zero, false else</returns>
2334        public bool iszero() {
2335            if (real == 0.0f && imag == 0.0f)
2336                return true;
2337            else
2338                return false;
2339        }
2340        #endregion CAST_OPERATORS
2341
2342    }
2343   
2344}
Note: See TracBrowser for help on using the repository browser.