initial
[prop.git] / include / AD / numeric / gaussint.h
blob54d23c9db56a9faaac3bb4541a068fcfd8f96bd6
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef Gaussian_integers_h
26 #define Gaussian_integers_h
28 #include <math.h>
29 #include <iostream.h>
30 #include <AD/generic/generic.h> // Generic definitions
32 struct GaussInt {
33 int real, imag;
35 /////////////////////////////////////////////////////////////////////
36 // Constructor and initializer
37 /////////////////////////////////////////////////////////////////////
38 GaussInt(int x = 0, int y = 0) : real(x), imag(y) {}
39 GaussInt(GaussInt& c) : real(c.real), imag(c.imag) {}
41 /////////////////////////////////////////////////////////////////////
42 // In place arithmetic
43 /////////////////////////////////////////////////////////////////////
44 void operator += (const GaussInt& c) { real += c.real; imag += c.imag; }
45 void operator -= (const GaussInt& c) { real -= c.real; imag -= c.imag; }
46 void operator *= (const GaussInt& c) { *this = *this * c; }
47 void operator /= (const GaussInt& c) { *this = *this / c; }
48 void operator += (int r) { real += r; }
49 void operator -= (int r) { real -= r; }
50 void operator *= (int r) { real *= r; imag *= r; }
51 void operator /= (int r) { real /= r; imag /= r; }
54 // GaussInt arithmetic
56 friend GaussInt operator - (const GaussInt& c)
57 { return GaussInt(-c.real,-c.imag); }
59 friend GaussInt operator + (const GaussInt& a, const GaussInt& b)
60 { return GaussInt(a.real + b.real, a.imag + b.imag); }
61 friend GaussInt operator + (const GaussInt& a, int b)
62 { return GaussInt(a.real + b, a.imag); }
63 friend GaussInt operator + (int a, const GaussInt& b)
64 { return GaussInt(a + b.real, b.imag); }
66 friend GaussInt operator - (const GaussInt& a, const GaussInt& b)
67 { return GaussInt(a.real - b.real, a.imag - b.imag); }
68 friend GaussInt operator - (const GaussInt& a, int b)
69 { return GaussInt(a.real - b, a.imag); }
70 friend GaussInt operator - (int a, const GaussInt& b)
71 { return GaussInt(a - b.real, -b.imag); }
73 friend GaussInt operator * (const GaussInt& a, const GaussInt& b)
74 { return GaussInt(a.real * b.real - a.imag * b.imag,
75 a.imag * b.real + a.real * b.imag);
77 friend GaussInt operator * (const GaussInt& a, int b)
78 { return GaussInt(a.real * b, a.imag * b); }
79 friend GaussInt operator * (int a, const GaussInt& b)
80 { return GaussInt(a * b.real, a * b.imag); }
82 friend GaussInt operator / (const GaussInt& a, const GaussInt& b);
83 friend GaussInt operator / (const GaussInt& a, int b)
84 { return GaussInt(a.real / b, a.imag / b); }
85 friend GaussInt operator / (int a, const GaussInt& b);
87 //////////////////////////////////////////////////////////////////////
88 // Comparisons between Gaussian integers and int
89 //////////////////////////////////////////////////////////////////////
90 friend Bool operator == (const GaussInt& a, const GaussInt& b)
91 { return a.real == b.real && a.imag == b.imag; }
92 friend Bool operator == (const GaussInt& a, int b)
93 { return a.real == b && a.imag == 0.0; }
94 friend Bool operator == (int a, const GaussInt& b)
95 { return a == b.real && b.imag == 0.0; }
97 friend Bool operator != (const GaussInt& a, const GaussInt& b)
98 { return a.real != b.real || a.imag != b.imag; }
99 friend Bool operator != (const GaussInt& a, int b)
100 { return a.real != b || a.imag != 0.0; }
101 friend Bool operator != (int a, const GaussInt& b)
102 { return a != b.real || b.imag != 0.0; }
104 //////////////////////////////////////////////////////////////////////
105 // Operations on complex numbers: conjugate, polar, norm and abs.
106 //////////////////////////////////////////////////////////////////////
107 GaussInt operator ! () const { return GaussInt(real,-imag); }
108 friend int norm(const GaussInt& c)
109 { return c.real * c.real + c.imag * c.imag; }
110 friend double abs(const GaussInt& c) { return sqrt(norm(c)); }
112 ////////////////////////////////////////////////////////////////
113 // Input and output
114 ////////////////////////////////////////////////////////////////
115 friend ostream& operator << (ostream&, const GaussInt&);
116 friend istream& operator >> (istream&, GaussInt&);
119 #endif