Advertisement
mrAnderson33

пятая лаба(класс комплЕксного числа)

Nov 24th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Complex {
  4.  
  5.  
  6.     public Complex()
  7.     {
  8.  
  9.     }
  10.  
  11.     public  Complex(double real , double imaginary)
  12.     {
  13.         this.real = real;
  14.         this.imaginary = imaginary;
  15.     }
  16.  
  17.     public  static  Complex sum(Complex left, Complex right)
  18.     {
  19.         return  new Complex(left.getReal()+right.getReal(),left.getImaginary()+right.getImaginary());
  20.     }
  21.  
  22.     public  static  Complex mmultiply(Complex left, Complex right)
  23.     {
  24.         return  new Complex(left.getReal()*right.getReal() - left.getImaginary()*right.getImaginary(),
  25.                         left.getImaginary()*right.getReal()+ left.getReal()*right.getImaginary());
  26.     }
  27.  
  28.     public  static Complex division (Complex left, Complex right)
  29.     {
  30.         if (right.magnitude() == 0) throw  new IllegalArgumentException("Division by zero!");
  31.         return  new Complex((left.getReal()*right.getReal() + left.getImaginary()*right.getImaginary())/(right.getReal() *right.getReal()),
  32.                 (left.getImaginary()*right.getReal()+ left.getReal()*right.getImaginary())/(right.getReal() *right.getReal())) ;
  33.     }
  34.  
  35.     public  static  Complex pow(Complex val, double degree)
  36.     {
  37.         double tempReal = val.getReal() == 0 ? 0 : Math.pow(val.magnitude(),degree) * Math.cos(degree * val.phase());
  38.         double tempImaginary = val.getImaginary() == 0 ? 0 : Math.pow(val.magnitude(),degree) * Math.sin(degree * val.phase());
  39.  
  40.         return new Complex(tempReal,tempImaginary);
  41.     }
  42.  
  43.     public  static Complex sqrt(Complex val)
  44.     {
  45.         return Complex.fromPolarCoordinates(Math.sqrt(val.magnitude()),val.phase()/2);
  46.     }
  47.  
  48.     public  double magnitude()
  49.     {
  50.         if (real > 0) return Math.sqrt(real*real+imaginary*imaginary);
  51.         if (real < 0 && imaginary >= 0)  return Math.PI + Math.sqrt(real*real+imaginary*imaginary);
  52.         if ( real < 0 && imaginary < 0) return  -Math.PI + Math.sqrt(real*real+imaginary*imaginary);
  53.         if(real == 0 && imaginary >0) return Math.PI/2;
  54.         if(real == 0 && imaginary <0) return -Math.PI/2;
  55.         return 0;
  56.     }
  57.  
  58.     public double phase()
  59.     {
  60.         return Math.atan2(imaginary,real);
  61.     }
  62.  
  63.     public static  Complex fromPolarCoordinates(double magnitude, double phase)
  64.     {
  65.         double tempReal = magnitude * Math.cos(phase);
  66.         double temoImaginary = magnitude * Math.sin(phase);
  67.  
  68.         return new Complex(tempReal,temoImaginary);
  69.     }
  70.  
  71.     @Override
  72.     public String toString() {
  73.         final StringBuffer sb = new StringBuffer("Complex{");
  74.         sb.append("real=").append(real);
  75.         sb.append(", imaginary=").append(imaginary);
  76.         sb.append('}');
  77.         return sb.toString();
  78.     }
  79.  
  80.     public double getImaginary() {
  81.         return imaginary;
  82.     }
  83.  
  84.     public void setImaginary(double imaginary) {
  85.         this.imaginary = imaginary;
  86.     }
  87.  
  88.     public double getReal() {
  89.  
  90.         return real;
  91.     }
  92.  
  93.     public void setReal(double real) {
  94.         this.real = real;
  95.     }
  96.  
  97.     private double real = 0;
  98.     private  double imaginary = 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement