Sebaall

Complex

Nov 30th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. import java.lang.Math;
  2. class Complex{
  3.     private double Re;
  4.     private double Im;
  5.         Complex(){
  6.             this.Re=0.;
  7.             this.Im=0.;
  8.         }
  9.         Complex(double Re,double Im){
  10.             this.Re=Re;
  11.             this.Im=Im;
  12.         }
  13.         Complex(Complex x){
  14.             this.Re=x.Re;
  15.             this.Im=x.Im;
  16.         }
  17.    
  18.         static Complex add(Complex a,Complex b){
  19.             return new Complex(a.Re+b.Re,a.Im+b.Im);
  20.         }
  21.         static Complex sub(Complex a,Complex b){
  22.             return new Complex(a.Re-b.Re,a.Im-b.Im);
  23.         }
  24.         static Complex mult(Complex a,Complex b){
  25.             return new Complex(a.Re*b.Re-a.Im*b.Im,a.Im*b.Re+a.Re*b.Im);
  26.         }  
  27.         static Complex div(Complex a,Complex b){
  28.             return new Complex((a.Re*b.Re+a.Im*b.Im)/(b.Re*b.Re+b.Im*b.Im),(a.Im*b.Re-a.Re*b.Im)/(b.Re*b.Re+b.Im*b.Im));
  29.         }
  30.         static Complex conj(Complex a){
  31.             return new Complex(a.Re,-a.Im);
  32.         }  
  33.         static Complex pow(Complex a,int n){
  34.             Complex b=new Complex(a);
  35.             for(int i=1;i<n;i++)
  36.                 b=mult(b,a);
  37.                
  38.             return b;
  39.                
  40.         }
  41.         static double abs(Complex a){
  42.             return Math.sqrt(a.Re*a.Re+a.Im*a.Im);
  43.         }
  44.         static double getRe(Complex a){
  45.             return a.Re;
  46.         }  
  47.         static double getIm(Complex a){
  48.                     return a.Im;
  49.                 }  
  50.         public String toString(){
  51.                 return this.Re+"+"+this.Im+"i";
  52.         }
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment