Finding the Conjugate of the Complex Number in Golang


In mathematics, complex numbers are numbers that comprise a real part and an imaginary part. The real part is a regular number, and the imaginary part is a multiple of the imaginary unit, represented by the letter i. Golang provides built-in support for complex numbers and allows users to perform various operations on them. In this article, we will discuss how to find the conjugate of a complex number in Golang.

What is the Conjugate of a Complex Number?

The conjugate of a complex number is obtained by changing the sign of its imaginary part. For example, if we have a complex number a + bi, its conjugate would be a - bi. In other words, the conjugate of a complex number a + bi is a - bi.

Finding the Conjugate of a Complex Number in Golang

In Golang, we can find the conjugate of a complex number using the cmplx.Conj() function provided by the built-in cmplx package. This function takes a complex number as its argument and returns its conjugate.

Here is an example program that demonstrates how to find the conjugate of a complex number in Golang −

Example

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   // Create a complex number
   c := complex(3, 4)

   // Find the conjugate
   conj := cmplx.Conj(c)

   // Print the result
   fmt.Println("The conjugate of", c, "is", conj)
}

Output

The conjugate of (3+4i) is (3-4i)

In the above program, we first create a complex number c with real and imaginary parts set to 3 and 4, respectively. Then we use the cmplx.Conj() function to find its conjugate, which is assigned to the variable conj. Finally, we print the result using the fmt.Println() function.

Here is another example −

Example

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   // Creating a complex number with imaginary part as 0
   z1 := complex(5, 0)

   // Taking the conjugate of the complex number
   z2 := cmplx.Conj(z1)

   // Printing the original and conjugate complex numbers
   fmt.Printf("Original Complex Number: %v\n", z1)
   fmt.Printf("Conjugate of Complex Number: %v\n", z2)
}

Output

Original Complex Number: (5+0i)
Conjugate of Complex Number: (5-0i)

In this example, we create a complex number z1 with a real part of 5 and an imaginary part of 0. We then find the conjugate of z1 using the cmplx.Conj function and store it in z2. Finally, we print both the original and conjugate complex numbers. Since the imaginary part of z1 is 0, the imaginary part of the conjugate is also 0, and we see that the result is the same as the original number.

Conclusion

In this article, we have discussed how to find the conjugate of a complex number in Golang using the cmplx.Conj() function provided by the built-in cmplx package. We hope this article helps you understand the concept of complex numbers and how to work with them in Golang.

Updated on: 12-Apr-2023

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements