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)

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

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…