アットウィキロゴ

Java > 計算機科学 > その他 > 複素数

複素数

  1. public class Complex {
  2. public double re;
  3. public double im;
  4.  
  5. public static Complex ZERO = new Complex(0.0, 0.0);
  6. public static Complex ONE = new Complex(1.0, 0.0);
  7. public static Complex I = new Complex(0.0, 1.0);
  8. public static Complex NaN = new Complex(Double.NaN, Double.NaN);
  9.  
  10. public Complex(double r, double i){
  11. re = r; im = i;
  12. }
  13. public Complex(double theta){
  14. re = Math.cos(theta);
  15. im = Math.sin(theta);
  16. }
  17. public Complex(Complex z){
  18. re = z.re; im = z.im;
  19. }
  20.  
  21. public String toString() {
  22. DecimalFormat df = new DecimalFormat(" ##0.000;-##0.000");
  23. sb.append(df.format(re));
  24. sb.append(" ");
  25. sb.append(df.format(im));
  26. return sb.toString();
  27. }
  28.  
  29. public double re(){ return re; }
  30. public double im(){ return im; }
  31.  
  32. public Complex copy(){
  33. return new Complex(re,im);
  34. }
  35. public Complex add(Complex z){
  36. return new Complex(re+z.re, im+z.im);
  37. }
  38. public Complex sub(Complex z){
  39. return new Complex(re-z.re, im-z.im);
  40. }
  41. public Complex mul(Complex z){
  42. return new Complex(re*z.re-im*z.im, re*z.im+im*z.re);
  43. }
  44. public Complex mul(double k){
  45. return new Complex(k*re, k*im);
  46. }
  47. public Complex div(Complex z){
  48. double tmp = z.norm();
  49. return new Complex((re*z.re+im*z.im)/tmp, (-re*z.im+im*z.re)/tmp);
  50. }
  51. public Complex conj(){
  52. return new Complex(re,-im);
  53. }
  54. public Complex sqr(){
  55. return this.mul(this);
  56. }
  57. public void addeq(Complex z){
  58. re += z.re;
  59. im += z.im;
  60. }
  61. public void subeq(Complex z){
  62. re -= z.re;
  63. im -= z.im;
  64. }
  65. public void muleq(Complex z){
  66. double tmp = re*z.re - im*z.im;
  67. im = re*z.im + im*z.re;
  68. re = tmp;
  69. }
  70. public void diveq(Complex z){
  71. double tmp = z.norm();
  72. this.muleq(conj(z));
  73. this.muleq(1.0/tmp);
  74. }
  75. public void muleq(double a){
  76. re *= a;
  77. im *= a;
  78. }
  79. public void conjeq(){
  80. im *= -1.0;
  81. }
  82.  
  83. public double abs(){
  84. return Math.hypot(re, im);
  85. }
  86. public double norm(){
  87. return re*re + im*im;
  88. }
  89. public double arg(){
  90. return Math.atan2(im, re);
  91. }
  92.  
  93. public static Complex add(Complex x, Complex y){
  94. return new Complex(x.re+y.re, x.im+y.im);
  95. }
  96. public static Complex sub(Complex x, Complex y){
  97. return new Complex(x.re-y.re, x.im-y.im);
  98. }
  99. public static Complex mul(Complex x, Complex y){
  100. return new Complex(x.re*y.re - x.im*y.im, x.re*y.im + x.im*y.re);
  101. }
  102. public static Complex div(Complex x, Complex y){
  103. return new Complex(x.re+y.re, x.im+y.im);
  104. }
  105. public static Complex mul(double a, Complex x){
  106. return new Complex(a*x.re, a*x.im);
  107. }
  108. public static Complex conj(Complex x){
  109. return new Complex(x.re, -x.im);
  110. }
  111.  
  112. public static double abs(Complex z){
  113. return Math.hypot(z.re, z.im);
  114. }
  115. public static double norm(Complex z){
  116. return z.re*z.re + z.im*z.im;
  117. }
  118. public static double arg(Complex z){
  119. return Math.atan2(z.im, z.re);
  120. }
  121.  
  122. public void print(){
  123. System.out.println(re + "+" + im + "i");
  124. }
  125.  
  126. }
  127.  

タグ:

+ タグ編集
  • タグ:
最終更新:2011年06月04日 21:21