1. /* slcomplx.h by K.Tsuru */
  2. /*********************************************************
  3. SN library
  4. SLComplex class
  5. It provides a multi-precision complex number arithmetic.
  6. SLong version is applied to binary splitting method.
  7. *********************************************************/
  8. #ifndef SLCOMPLEX_H
  9. #define SLCOMPLEX_H
  10. class SLComplex{
  11. private:
  12. SLong re, im;
  13. /********* List of unusable functions which have no body.******/
  14. SLComplex operator>(const SLComplex&) const;
  15. SLComplex operator>=(const SLComplex&) const;
  16. SLComplex operator<(const SLComplex&) const;
  17. SLComplex operator<=(const SLComplex&) const;
  18. // division operators are not provided.
  19. SLComplex& operator/=(const SLong& x);
  20. SLComplex& operator/=(const SLComplex& z);
  21. public:
  22. // default constructor do nothing.
  23. SLComplex() {}
  24. // constructor by two SLong values
  25. SLComplex(const SLong& rp, const SLong& ip = 0.0) : re(rp), im(ip){}
  26. // constructor by two double values since ver 2.18
  27. // This enables us to use "return 0.0".
  28. // SLComplex(double rp, double ip = 0.0) : re(rp), im(ip){}
  29. // copy constructor
  30. SLComplex(const SLComplex& z) :re(z.re), im(z.im){}
  31. ~SLComplex(){}
  32. SLong Real() const { return re;} // real part
  33. SLong Imag() const { return im;} // imaginary part
  34. SLong Norm() const;
  35. SLComplex Conj() const;
  36. uint Head() const { return min( re.Head(), im.Head() ); }
  37. uint Tail() const { return min( re.Tail(), im.Tail() ); }
  38. void FigureClear(uint from, uint to) {
  39. re.FigureClear(from, to); im.FigureClear(from, to);
  40. }
  41. void SetInt(int i) { re.SetInt(i); im.SetZero(); }
  42. void SetZero(){ re.SetZero(); im.SetZero(); }
  43. // If you want to change the real part only, use such as z.Set(newValue, z.Imag());
  44. // The statement im = z.Imag(); does not copy, because &im == &ip.
  45. void Set(const SLong& rp, const SLong& ip) { re = rp; im = ip; }
  46. bool IsZero(int id=91) const { return (re.Sign(id) == 0) && (im.Sign(id) == 0); }
  47. //sign operators
  48. SLComplex operator+() const { return *this; }
  49. SLComplex operator-() const;
  50. //Operators whose rhs is scalar.
  51. SLComplex& operator=(const SLong& x);
  52. SLComplex& operator+=(const SLong& x);
  53. SLComplex& operator-=(const SLong& x);
  54. SLComplex& operator*=(const SLong& x);
  55. //Operators whose rhs is complex object.
  56. SLComplex& operator=(const SLComplex& z);
  57. SLComplex& operator+=(const SLComplex& z);
  58. SLComplex& operator-=(const SLComplex& z);
  59. SLComplex& operator*=(const SLComplex& z); // 902
  60. // At the present time it does not support reading from file.
  61. // Please use SLong class' Put(s) and write/read the real and imaginary parts, separately.
  62. // Due to the value of "fmt" it outputs in the following form.
  63. // iFMT : (real part)[+|-]i*(imaginary part)
  64. // BracketFMT : (real part, imaginary part)
  65. enum { iFMT = 0, BracketFMT};
  66. long Put(bool crlf = false, int fmt = iFMT) const; // 901
  67. long Puts(int fmt = iFMT) const{ return Put(true, fmt); }
  68. };
  69. ///// Cautions : All functions having its body must be "inline".///////
  70. /*
  71. inline long SLComplex::Put(bool crlf, int fmt) const {
  72. SComplex r(re, im);
  73. return r.Put(crlf, fmt);
  74. }
  75. */
  76. /**********************************
  77. * Implementation of member functons
  78. ***********************************/
  79. inline SLong SLComplex::Norm() const {
  80. return ( re * re + im * im );
  81. }
  82. inline SLComplex SLComplex::Conj() const { return SLComplex(re, -im); }
  83. inline SLComplex SLComplex::operator-() const {
  84. SLComplex z(*this);
  85. z.re = -z.re; z.im = -z.im;
  86. return z;
  87. }
  88. ////// Operators whose rhs is real scalar. //////
  89. inline SLComplex& SLComplex::operator=(const SLong& x) {
  90. re = x; im = 0.0; return *this;
  91. }
  92. inline SLComplex& SLComplex::operator+=(const SLong& x) {
  93. re += x; return *this;
  94. }
  95. inline SLComplex& SLComplex::operator-=(const SLong& x) {
  96. re -= x; return *this;
  97. }
  98. inline SLComplex& SLComplex::operator*=(const SLong& x) {
  99. re *= x; im *= x; return *this;
  100. }
  101. ///// Operators whose rhs is complex object. /////
  102. inline SLComplex& SLComplex::operator=(const SLComplex& z) {
  103. if(this == &z) return *this; // z = z;
  104. re = z.re; im = z.im;
  105. return *this;
  106. }
  107. inline SLComplex& SLComplex::operator+=(const SLComplex& z) {
  108. re += z.re; im += z.im;
  109. return *this;
  110. }
  111. inline SLComplex& SLComplex::operator-=(const SLComplex& z) {
  112. re -= z.re; im -= z.im;
  113. return *this;
  114. }
  115. /******* Non menber functions in the following *******/
  116. //// Two term operators /////
  117. //// plus operators x + y
  118. inline SLComplex operator+(const SLComplex& x, const SLComplex& y) {
  119. return SLComplex(x.Real() + y.Real(), x.Imag() + y.Imag());
  120. }
  121. inline SLComplex operator+(const SLComplex& x, const SLong& y) {
  122. SLComplex r = x;
  123. return r += y;
  124. }
  125. inline SLComplex operator+(const SLong& x, const SLComplex& y) {
  126. SLComplex r = y;
  127. return r += x;
  128. }
  129. //// minus operators x - y
  130. inline SLComplex operator-(const SLComplex& x, const SLComplex& y) {
  131. return SLComplex(x.Real() - y.Real(), x.Imag() - y.Imag());
  132. }
  133. inline SLComplex operator-(const SLComplex& x, const SLong& y) {
  134. SLComplex r = x;
  135. return r -= y;
  136. }
  137. inline SLComplex operator-(const SLong& x, const SLComplex& y) {
  138. SLComplex r = x;
  139. return r -= y;
  140. }
  141. //// multiplication operators x * y
  142. inline SLComplex operator*(const SLComplex& x, const SLComplex& y) {
  143. SLComplex r = x;
  144. return r *= y;
  145. }
  146. inline SLComplex operator*(const SLComplex& x, const SLong& y) {
  147. SLComplex r = x;
  148. return r *= y;
  149. }
  150. inline SLComplex operator*(const SLong& x, const SLComplex& y) {
  151. SLComplex r = y;
  152. return r *= x;
  153. }
  154. /*****************
  155. basic functions
  156. ******************/
  157. inline SLComplex Conj(const SLComplex& z) { // conjugate
  158. return z.Conj();
  159. }
  160. /*
  161. SLong Cabs(const SLComplex& z); // |z|
  162. SLong Arg(const SLComplex& z); // argument in xy plane
  163. */
  164. inline SLong Real(const SLComplex& z){ return z.Real();} // real part
  165. inline SLong Imag(const SLComplex& z){ return z.Imag();} // imaginary part
  166. inline SLong Norm(const SLComplex& z){ return z.Norm(); } // square of the magnitude
  167. //// relational operators
  168. inline bool operator==(const SLComplex& x, const SLComplex& y){
  169. return (x.Real()==y.Real()) && (x.Imag()==y.Imag());
  170. }
  171. inline bool operator!=(const SLComplex& x, const SLComplex& y){
  172. return !(x == y);
  173. }
  174. inline SComplex SLCtoSC(const SLComplex& x) {
  175. return SComplex(x.Real(), x.Imag());
  176. }
  177. #endif // SLCOMPLEX_H

slcomplx.h : last modifiled at 2016/09/04 14:21:36(6,413 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).