Skip to content

Commit 4e6b383

Browse files
committed
bigcomplex now working
1 parent 8162be0 commit 4e6b383

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

libsrc/eclib/bigcomplex.h

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,13 @@ class bigcomplex{
3636
RR imag() const {return im;};
3737
RR norm() const {return sqr(re)+sqr(im);};
3838
RR arg() const { return atan2(im, re);};
39-
RR abs2() const {return sqr(re)+sqr(im);};
40-
RR abs() const {return ::NTL::sqrt(abs2());};
39+
RR abs() const {return ::NTL::sqrt(norm());};
4140
bigcomplex conj() const {return bigcomplex(re,-im);};
4241
bigcomplex timesI() const {return bigcomplex(-im,re);};
4342
int IsReal() const {return ::NTL::IsZero(im);}
4443
int IsImaginary() const {return ::NTL::IsZero(re);}
4544
int IsZero() const {return ::NTL::IsZero(re) && ::NTL::IsZero(im);}
4645

47-
// same again, as functions
48-
friend RR real(const bigcomplex& z) {return z.re;};
49-
friend RR imag(const bigcomplex& z) {return z.im;};
50-
friend RR norm(const bigcomplex& z) {return sqr(z.re)+sqr(z.im);};
51-
friend RR arg(const bigcomplex& z) { return atan2(z.im, z.re);};
52-
friend RR abs2(const bigcomplex& z) {return sqr(z.re)+sqr(z.im);};
53-
friend RR abs(const bigcomplex& z) {return ::NTL::sqrt(z.abs2());};
54-
friend bigcomplex conj(const bigcomplex& z) {return bigcomplex(z.re,-z.im);};
55-
int IsReal(const bigcomplex& z) {return z.IsReal();}
56-
int IsImaginary(const bigcomplex& z) {return z.IsImaginary();}
57-
int IsZero(const bigcomplex& z) {return z.IsZero();}
5846

5947
bigcomplex operator= (const RR& r) {re=r; im=RR(); return *this;}
6048
bigcomplex operator+=(const RR& r) {re+=r; return *this;};
@@ -68,37 +56,32 @@ class bigcomplex{
6856

6957
bigcomplex operator=(const bigcomplex& z) {re=z.re; im=z.im; return *this;};
7058
bigcomplex operator+=(const bigcomplex& z) {re+=z.re; im+=z.im; return *this;};
71-
bigcomplex operator+(const bigcomplex& z) const {bigcomplex w(*this); w+=z; return w;};
59+
bigcomplex operator+(const bigcomplex& z) const {return bigcomplex(re+z.re, im+z.im);};
7260
bigcomplex operator+() {return *this;};
7361
bigcomplex operator-=(const bigcomplex& z) {re-=z.re; im-=z.im; return *this;};
74-
bigcomplex operator-(const bigcomplex& z) const {bigcomplex w(*this); w-=z; return w;};
62+
bigcomplex operator-(const bigcomplex& z) const {return bigcomplex(re-z.re, im-z.im);};
7563
bigcomplex operator-() const {return bigcomplex(-re,-im);};
76-
bigcomplex operator*=(const bigcomplex& z) {RR x = re; re=x*z.re-im*z.re; im = x*z.im+im*z.re; return *this;};
64+
bigcomplex operator*=(const bigcomplex& z) {RR x = re; re=x*z.re-im*z.im; im = x*z.im+im*z.re; return *this;};
7765
bigcomplex operator*(const bigcomplex& z) const {bigcomplex w(*this); w*=z; return w;};
78-
bigcomplex operator/=(const bigcomplex& z) {RR r=z.abs2(), x = re; re=(x*z.re+im*z.re)/r; im = (-x*z.im+im*z.re)/r; return *this;};
66+
bigcomplex operator/=(const bigcomplex& z) {RR r=z.norm(), x = re; re=(x*z.re+im*z.im)/r; im = (-x*z.im+im*z.re)/r; return *this;};
7967
bigcomplex operator/(const bigcomplex& z) const {bigcomplex w(*this); w/=z; return w;};
8068

81-
// operators with left operand RR
82-
friend bigcomplex operator+(const RR& r, const bigcomplex& z) {return bigcomplex(r)+z;};
83-
friend bigcomplex operator-(const RR& r, const bigcomplex& z) {return bigcomplex(r)-z;};
84-
friend bigcomplex operator*(const RR& r, const bigcomplex& z) {return bigcomplex(r)*z;};
85-
friend bigcomplex operator/(const RR& r, const bigcomplex& z) {return bigcomplex(r)/z;};
86-
8769
// standard functions as class methods:
8870
bigcomplex sqrt() const {RR r = abs(); RR u = ::NTL::sqrt((r+re)/2), v=::NTL::sqrt((r-re)/2); if (im<0) v=-v; return bigcomplex(u,v);};
8971
bigcomplex cos() const {bigcomplex z(this->timesI()); return (z+z.conj())/to_RR(2);};
9072
bigcomplex sin() const {bigcomplex z(this->timesI()); return (z-z.conj())/bigcomplex(RR(),to_RR(2));};
9173
bigcomplex exp() const {RR e = ::NTL::exp(re); return bigcomplex(e * ::NTL::cos(im), e * ::NTL::sin(im));};
9274
bigcomplex log() const {return bigcomplex(::NTL::log(abs()), arg());}
9375

94-
// standard functions as functions:
95-
friend bigcomplex sqrt(const bigcomplex& z) {bigcomplex w(z); return w.sqrt();};
96-
friend bigcomplex cos(const bigcomplex& z) {bigcomplex w(z); return w.cos();};
97-
friend bigcomplex sin(const bigcomplex& z) {bigcomplex w(z); return w.sin();};
98-
friend bigcomplex exp(const bigcomplex& z) {bigcomplex w(z); return w.exp();};
99-
friend bigcomplex log(const bigcomplex& z) {bigcomplex w(z); return w.log();};
10076

10177
operator string() const
78+
{
79+
ostringstream s;
80+
s << "(" << re << "," << im << ")";
81+
return s.str();
82+
}
83+
84+
string pretty_string() const
10285
{
10386
ostringstream s;
10487
if (IsReal())
@@ -132,7 +115,7 @@ class bigcomplex{
132115

133116
friend ostream& operator<<(ostream& s, const bigcomplex& z)
134117
{
135-
s << (string)z;
118+
s << string(z);
136119
return s;
137120
}
138121

@@ -141,9 +124,32 @@ class bigcomplex{
141124
RR re, im;
142125
};
143126

144-
inline void swap(bigcomplex& z1, bigcomplex& z2)
145-
{
146-
bigcomplex z3(z1); z1=z2; z2=z3;
147-
}
127+
128+
// inline functions
129+
inline RR real(const bigcomplex& z) {return z.real();};
130+
inline RR imag(const bigcomplex& z) {return z.imag();};
131+
inline RR norm(const bigcomplex& z) {return z.norm();};
132+
inline RR arg(const bigcomplex& z) { return z.arg();};
133+
inline RR abs(const bigcomplex& z) {return z.abs();};
134+
inline bigcomplex conj(const bigcomplex& z) {return z.conj();};
135+
inline int IsReal(const bigcomplex& z) {return z.IsReal();}
136+
inline int IsImaginary(const bigcomplex& z) {return z.IsImaginary();}
137+
inline int IsZero(const bigcomplex& z) {return z.IsZero();}
138+
139+
// operators with left operand RR
140+
inline bigcomplex operator+(const RR& r, const bigcomplex& z) {return bigcomplex(r)+z;};
141+
inline bigcomplex operator-(const RR& r, const bigcomplex& z) {return bigcomplex(r)-z;};
142+
inline bigcomplex operator*(const RR& r, const bigcomplex& z) {return bigcomplex(r)*z;};
143+
inline bigcomplex operator/(const RR& r, const bigcomplex& z) {return bigcomplex(r)/z;};
144+
145+
// standard method functions as functions:
146+
inline bigcomplex sqrt(const bigcomplex& z) {bigcomplex w(z); return w.sqrt();};
147+
inline bigcomplex cos(const bigcomplex& z) {bigcomplex w(z); return w.cos();};
148+
inline bigcomplex sin(const bigcomplex& z) {bigcomplex w(z); return w.sin();};
149+
inline bigcomplex exp(const bigcomplex& z) {bigcomplex w(z); return w.exp();};
150+
inline bigcomplex log(const bigcomplex& z) {bigcomplex w(z); return w.log();};
151+
152+
153+
inline void swap(bigcomplex& z1, bigcomplex& z2) {bigcomplex z3(z1); z1=z2; z2=z3;}
148154

149155
#endif

tests/out_ntl/telog.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ Checking...
5454
Curve [1,1,0,-202,1025]
5555

5656
The point Q is = [-8:51:1]
57-
Elliptic log of P is (2.15251566696160241628533178186449561190793579506,0)
57+
Elliptic log of P is (2.15251566696160241628533178186449561190793579504,0)
5858
Reconstructed P = [-8:51:1]
5959
The point P3 is = [8:-3:1]
60-
Elliptic log of P is (3.40172041025849963266962742312340879857800215119,0)
60+
Elliptic log of P is (3.40172041025849963266962742312340879857800215114,0)
6161
Reconstructed P = [8:-3:1]
6262
(m*[8:-3:1])/m = [ [8:-3:1] ]
6363
Checking...

0 commit comments

Comments
 (0)