Java Program to Perform Complex Number Multiplication

«
»
This is the java implementation of multiplication of two matrices consisting of complex numbers. Complex numbers are of the form a+bi.

Here is the source code of the Java Program to Perform Complex Number Multiplication. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a sample program to find the multiplication of two matrices consisting of complex numbers of any dimension
  2. import java.util.Scanner;
  3.  
  4. public class Complex_Multiplication_Matrix 
  5. {
  6.     private double real=0.0, img=0.0;
  7.     public Complex_Multiplication_Matrix(double real, double img)
  8.     {
  9.         this.real = real;
  10.         this.img = img;
  11.     }
  12.     public Complex_Multiplication_Matrix()
  13.     {
  14.         this.real = 0;
  15.         this.img = 0;
  16.     }
  17.  
  18.     public Complex_Multiplication_Matrix complex_Form(double re, double im)
  19.     {
  20.         Complex_Multiplication_Matrix res = new Complex_Multiplication_Matrix();
  21.         res.real = re;
  22.         res.img = im;
  23.         return res;
  24.     }
  25.     public Complex_Multiplication_Matrix multiplication(Complex_Multiplication_Matrix C2)
  26.     {
  27.         Complex_Multiplication_Matrix Res = new Complex_Multiplication_Matrix();
  28.         Res.real = (this.real * C2.real) - (this.img * C2.img);
  29.         Res.img = (this.real * C2.img) + (this.img * C2.real); 
  30.         return Res;
  31.     }	
  32.     public Complex_Multiplication_Matrix addtion(Complex_Multiplication_Matrix C2)
  33.     {
  34.         Complex_Multiplication_Matrix Res = new Complex_Multiplication_Matrix();
  35.         this.real += C2.real;
  36.         this.img  += C2.img;
  37.         Res.real = this.real;
  38.         Res.img = this.img;
  39.         return Res;
  40.     }
  41.     public Complex_Multiplication_Matrix[][] matrix_multiplication(Complex_Multiplication_Matrix[][] a, Complex_Multiplication_Matrix[][] b, Complex_Multiplication_Matrix[][] res, int n)
  42.     {
  43.         for (int i = 0; i < n; i++) 
  44.             for (int j = 0; j < n; j++) 
  45.                 for (int k = 0; k < n; k++)
  46.                     res[i][j] = res[i][j].addtion(a[i][k].multiplication(b[k][j]));
  47.         return res;
  48.     }
  49.     public static void main(String args[])
  50.     {
  51.         Scanner sc = new Scanner(System.in);
  52.         System.out.println("Enter the dimension of the square matrix: ");
  53.         int n = sc.nextInt();
  54.         double re,im;
  55.         Complex_Multiplication_Matrix[][] a = new Complex_Multiplication_Matrix[n][n];
  56.         Complex_Multiplication_Matrix[][] b = new Complex_Multiplication_Matrix[n][n];
  57.         Complex_Multiplication_Matrix[][] res = new Complex_Multiplication_Matrix[n][n];
  58.         Complex_Multiplication_Matrix C = new Complex_Multiplication_Matrix();
  59.  
  60.         System.out.println("Enter the complex elements of 1st matrix: ");
  61.         for(int i=0; i<n; i++)
  62.         {
  63.             for(int j=0; j<n; j++)
  64.             {
  65.                 re = sc.nextDouble();
  66.                 im = sc.nextDouble();
  67.                 a[i][j] = C.complex_Form(re, im);
  68.             }
  69.         }
  70.  
  71.         System.out.println("Enter the complex elements of matrix: ");
  72.         for(int i=0; i<n; i++)
  73.         {
  74.             for(int j=0; j<n; j++)
  75.             {
  76.                 re = sc.nextDouble();
  77.                 im = sc.nextDouble();
  78.                 b[i][j] = C.complex_Form(re, im);
  79.             }
  80.         }
  81.  
  82.         for(int i=0; i<n; i++)
  83.         {
  84.             for(int j=0; j<n; j++)
  85.             {
  86.                 re = 0.0;
  87.                 im = 0.0;
  88.                 res[i][j] = C.complex_Form(re, im);
  89.             }
  90.         }
  91.  
  92.         res = C.matrix_multiplication(a, b, res, n);
  93.         System.out.println("The Multiplication is:");
  94.         for(int i=0; i<n; i++)
  95.         {
  96.             for(int j=0; j<n; j++)
  97.                 System.out.print(res[i][j].real+"+"+res[i][j].img+"i ");
  98.             System.out.println();
  99.         }    
  100.         sc.close();
  101.     }
  102. }

Output:

advertisement
$ javac Complex_Multiplication_Matrix.java
$ java Complex_Multiplication_Matrix
 
Enter the dimension of the square matrix: 
2
Enter the complex elements of matrix: 
1 2 1 2 
1 2 1 2 
Enter the complex elements of matrix: 
1 2 1 2 
1 2 1 2 
The Multiplication is:
-6.0+8.0i -6.0+8.0i 
-6.0+8.0i -6.0+8.0i

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.