Skip to main content

Complex Number Arithemetric Operations in JAVA

Addition and subtraction

Addition of two complex numbers can be done geometrically by constructing a parallelogram.
Complex numbers are added by adding the real and imaginary parts of the summands. That is to say:
(a+bi) + (c+di) = (a+c) + (b+d)i.\
Similarly, subtraction is defined by
(a+bi) - (c+di) = (a-c) + (b-d)i.\
Using the visualization of complex numbers in the complex plane, the addition has the following geometric interpretation: the sum of two complex numbers A and B, interpreted as points of the complex plane, is the point X obtained by building a parallelogram three of whose vertices are O, A and B. Equivalently, X is the point such that the triangles with vertices O, A, B, and X, B, A, are congruent.

Multiplication and division

The multiplication of two complex numbers is defined by the following formula:
(a+bi) (c+di) = (ac-bd) + (bc+ad)i.\
In particular, the square of the imaginary unit is −1:
i^2 = i \times i = -1.\
The preceding definition of multiplication of general complex numbers follows naturally from this fundamental property of the imaginary unit. Indeed, if i is treated as a number so that di means d times i, the above multiplication rule is identical to the usual rule for multiplying two sums of two terms.
(a+bi) (c+di) = ac + bci + adi + bidi (distributive law)
 = ac + bidi + bci + adi (commutative law of addition—the order of the summands can be changed)
 = ac + bdi^2 + (bc+ad)i (commutative and distributive laws)
 = (ac-bd) + (bc + ad)i (fundamental property of the imaginary unit).
The division of two complex numbers is defined in terms of complex multiplication, which is described above, and real division. When at least one of c and d is non-zero, we have
\,\frac{a + bi}{c + di} = \left({ac + bd \over c^2 + d^2}\right) + \left( {bc - ad \over c^2 + d^2} \right)i.
Division can be defined in this way because of the following observation:
\,\frac{a + bi}{c + di} = \frac{\left(a + bi\right) \cdot \left(c - di\right)}{\left (c + di\right) \cdot \left (c - di\right)} = \left({ac + bd \over c^2 + d^2}\right) + \left( {bc - ad \over c^2 + d^2} \right)i.
As shown earlier, cdi is the complex conjugate of the denominator c + di. At least one of the real part c and the imaginary part d of the denominator must be nonzero for division to be defined. This is called "rationalization" of the denominator (although the denominator in the final expression might be an irrational real number).
 
 
 
public class Complex {
 
 private double real;
 private double imaginary;
 
 public double getReal() {
  return this.real;
 }

 public void setReal(double real) {
  this.real = real;
 }

 public double getImaginary() {
  return this.imaginary;
 }

 public void setImaginary(double imaginary) {
  this.imaginary = imaginary;
 }

 public Complex() {
  this.setReal(0);
  this.setImaginary(0);
 }
 
 public Complex(double real, double imaginary){
  this.setReal(real);
  this.setImaginary(imaginary);
 }
 
 public void setRealAndImaginary(double real, double imaginary){
  this.setReal(real);
  this.setImaginary(imaginary);
 }
 
 
 @Override
 public String toString() {
  return "Values : [ (" + real + " ) + (" + imaginary + " i ) ]";
 }
  
    public Complex addition(Complex temp){
     Complex result = new Complex();
     result.real = this.getReal() + temp.getReal();
     result.imaginary = this.getImaginary() + temp.getImaginary();
     return result;
    }
 
    public Complex subtraction(Complex temp){
     Complex result = new Complex();
     result.real = this.getReal() - temp.getReal();
     result.imaginary = this.getImaginary() - temp.getImaginary();
     return result;
    }
    
    public Complex multiplication(Complex temp){
     Complex result = new Complex();
     result.real = ( this.getReal() * temp.getReal() ) - ( this.getImaginary() * temp.getImaginary());
     result.imaginary = ( this.getReal() * temp.getImaginary() ) + ( this.getImaginary() * temp.getReal());
     return result;
    }
    
    public Complex division(Complex temp){
     Complex result = new Complex();
     result.real = ((( this.getReal() * temp.getReal() ) + ( this.getImaginary() * temp.getImaginary())) / (Math.pow(temp.getReal(), 2) + Math.pow(temp.getImaginary(), 2)));
     result.imaginary =((( this.getReal() * temp.getImaginary() ) - ( this.getImaginary() * temp.getReal() )) / (Math.pow(temp.getReal(), 2) + Math.pow(temp.getImaginary(), 2)));
     return result;
    }
    
 public static void main(String[] args) {
  
  Complex complex = new Complex();
 
  Complex complex1 = new Complex(2,-1);
  Complex complex2 = new Complex(3,4);
  
  System.out.println("complex  " + complex);
  System.out.println("complex1  " + complex1);
  System.out.println("complex2  " + complex2);
  
  System.out.println("\n\n\nAddition  " + complex1.addition(complex2));
  System.out.println("Subtraction  " + complex1.subtraction(complex2));
  System.out.println("Multiplication " + complex1.multiplication(complex2));
  System.out.println("Division  " + complex1.division(complex2));
  
 }

}

Comments

Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as