Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1.  
  2. public class Complex {
  3.  
  4.     private final double re;
  5.     private final double im;
  6.    
  7.     public Complex() {
  8.         re = 0;
  9.         im = 0;
  10.     }
  11.    
  12.     public Complex(double real, double imaginary) {
  13.         re = real;
  14.         im = imaginary;
  15.     }
  16.    
  17.     public Complex(double real) {
  18.         re = real;
  19.         im = 0;
  20.     }
  21.    
  22.     public Complex(Complex z) {
  23.         re = z.re;
  24.         im = z.im;
  25.     }
  26.    
  27.     public String toString() {
  28.         if (im == 0) return re + "";
  29.         if (re == 0) return im + "i";
  30.         if (im <  0) return re + " - " + (-im) + "i";
  31.         return re + " + " + im + "i";
  32.     }
  33.    
  34.     public double re() {
  35.         return re;
  36.     }
  37.    
  38.     public double im() {
  39.         return im;
  40.     }
  41.    
  42.     public boolean isReal() {
  43.         return im == 0;
  44.     }
  45.    
  46.     public double abs() {
  47.         return Math.hypot(re, im);
  48.     }
  49.    
  50.     public double phase() {
  51.         return Math.atan2(im, re);
  52.     }
  53.        
  54.     public boolean equals(Complex z) {
  55.         return (re == z.re) && (im == z.im);
  56.     }
  57.    
  58.     public Complex add(Complex z) {
  59.         double real = re + z.re;
  60.         double imaginary = im + z.im;
  61.         return new Complex(real, imaginary);
  62.     }
  63.    
  64.     public Complex substract(Complex z) {
  65.         double real = re - z.re;
  66.         double imaginary = im - z.im;
  67.         return new Complex(real, imaginary);       
  68.     }
  69.    
  70.     public Complex multiply(double x) {
  71.         return new Complex(x * re, x * im);
  72.     }
  73.    
  74.     public Complex multiply(Complex z) {
  75.         double real = re * z.re - im * z.im;
  76.         double imaginary = re * z.im + im * z.re;
  77.         return new Complex(real, imaginary);
  78.     }
  79.    
  80.     public Complex conjugate() {
  81.         return new Complex(re, -im);
  82.     }
  83.    
  84.     public Complex reciprocal() {
  85.         double scale = re * re + im * im;
  86.         return new Complex(re / scale, -im / scale);
  87.     }
  88.    
  89.     public Complex divide(double x) {
  90.         return new Complex(re / x, im / x);
  91.     }
  92.    
  93.     public Complex divide(Complex z) {
  94.         return multiply(z.reciprocal());
  95.     }
  96.    
  97.     public Complex exp() {
  98.         return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
  99.     }
  100.  
  101.     public Complex sin() {
  102.         return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
  103.     }
  104.  
  105.     public Complex cos() {
  106.         return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
  107.     }
  108.  
  109.     public Complex tan() {
  110.         return sin().divide(cos());
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement