Multiplication of two complex numbers given as strings

Given two complex numbers in the form of string. Our task is to print the multiplication of these two complex numbers.

Examples:

Input : str1 = "1+1i"
        str2 = "1+1i"
Output : "0+2i"
Here, (1 + i) * (1 + i) = 
1 + i2 + 2 * i = 2i or "0+2i"

Input : str1 = "1+-1i"
        str2 = "1+-1i"
Output : "0+-2i"
Here, (1 - i) * (1 - i) = 
1 + i2 - 2 * i = -2i or "0+-2i"

Multiplication of two complex numbers can be done as:

(a+ib) \times (x+iy)=ax+i^2by+i(bx+ay)=ax-by+i(bx+ay)

We simply split up the real and the imaginary parts of the given complex strings based on the ‘+’ and the ‘i’ symbols. We store the real parts of the two strings a and b as x[0] and y[0] respectively and the imaginary parts as x[1] and y[1] respectively. Then, we multiply the real and the imaginary parts as required after converting the extracted parts into integers. Then, we again form the return string in the required format and return the result.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to multiply two complex numbers
// given as strings.
import java.util.*;
import java.lang.*;
  
public class GfG{
    public static String complexNumberMultiply(String a, String b) {
  
        // Spiting the real and imaginary parts
        // of the given complex strings based on '+'
        // and 'i' symbols.
        String x[] = a.split("\\+|i");
        String y[] = b.split("\\+|i");
          
        // Storing the real part of complex string a
        int a_real = Integer.parseInt(x[0]);
          
        // Storing the imaginary part of complex string a
        int a_img = Integer.parseInt(x[1]);
          
        // Storing the real part of complex string b
        int b_real = Integer.parseInt(y[0]);
          
        // Storing the imaginary part of complex string b
        int b_img = Integer.parseInt(y[1]);
          
        // Returns the product.
        return (a_real * b_real - a_img * b_img) + "+" +
              (a_real * b_img + a_img * b_real) + "i";
    }
      
    // Driver function 
    public static void main(String argc[]){
        String str1 = "1+1i";
        String str2 = "1+1i";
        System.out.println(complexNumberMultiply(str1, str2));
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to multiply 
// two complex numbers
// given as strings.
  
function complexNumberMultiply($a, $b
{
  
// Spiting the real and 
// imaginary parts of the
// given complex strings 
// based on '+' and 'i' symbols.
$x = preg_split("/[\s+]+|i/" , $a);
$y = preg_split("/[\s+]+|i/" , $b);
      
// Storing the real part
// of complex string a
$a_real = intval($x[0]);
      
// Storing the imaginary 
// part of complex string a
$a_img = intval($x[1]);
      
// Storing the real part 
// of complex string b
$b_real = intval($y[0]);
      
// Storing the imaginary 
// part of complex string b
$b_img = intval($y[1]);
      
// Returns the product.
return ($a_real * $b_real
        $a_img * $b_img) . "+"
       ($a_real * $b_img
        $a_img * $b_real) . "i";
}
  
// Driver Code
$str1 = "1+1i";
$str2 = "1+1i";
echo complexNumberMultiply($str1, $str2);
  
// This code is contributed by mits
?>

chevron_right



Output:

0+2i


My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.