# Finding the Sine of Complex Number in Golang

Finding the sine of a complex number is a common mathematical operation that arises in various fields such as engineering, physics, and computer science. In this article, we will explore how to find the sine of a complex number in Golang using the math/cmplx package.

## Understanding Sine of a Complex Number

In mathematics, the sine of a complex number is defined as the ratio of the imaginary part of the complex number to its magnitude. We can express this mathematically as −

sin(z) = (e^(iz) - e^(-iz)) / (2i)


where z is a complex number, e is the mathematical constant Euler's number, and i is the imaginary unit.

## Finding the Sine of a Complex Number in Golang

To find the sine of a complex number in Golang, we can use the cmplx.Sin() function provided by the math/cmplx package. This function takes a complex number as its argument and returns its sine as a complex number.

### Example

Here's an example code snippet −

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
z := complex(2, 3)

// Finding the sine of the complex number
sinZ := cmplx.Sin(z)

// Displaying the result
fmt.Println("Sine of", z, "is", sinZ)
}


### Output

Sine of (2+3i) is (9.154499146911428-4.168906959966565i)


In this example, we first create a complex number z using the complex() function. Then, we use the cmplx.Sin() function to find its sine and store the result in sinZ. Finally, we display the result using the fmt.Println() function.

## Finding the Sine of a Purely Real Number

Let's consider an example of finding the sine of a purely real number using Golang. Suppose we want to find the sine of x = 2.5.

### Example

Here's the code snippet to accomplish this −

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
x := 2.5
z := complex(x, 0)

// Finding the sine of the complex number
sinZ := cmplx.Sin(z)

// Displaying the result
fmt.Println("Sine of", z, "is", sinZ)
}


### Output

Sine of (2.5+0i) is (0.5984721441039564-0i)


In this example, we first create a purely real number x and then convert it to a complex number z using the complex() function. Then, we use the cmplx.Sin() function to find its sine and store the result in sinZ. Finally, we display the result using the fmt.Println() function.

## Finding the Sine of a Purely Imaginary Number

Let's consider an example of finding the sine of a purely imaginary number using Golang. Suppose we want to find the sine of y = 2i.

### Example

Here's the code snippet to accomplish this −

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a purely imaginary number
y := 2i

// Finding the sine of the complex number
sinY := cmplx.Sin(y)

// Displaying the result
fmt.Println("Sine of", y, "is", sinY)
}


### Output

Sine of (0+2i) is (0+3.626860407847019i)


This code creates a purely imaginary number y = 2i and finds its sine using the cmplx.Sin() function. The result is then displayed using fmt.Println().

## Conclusion

We have learned how to find the sine of a complex number using Golang. We started by discussing what complex numbers are and then introduced the cmplx package in Golang. We explored four different examples of finding the sine of a complex number, including a purely imaginary number, a real number, a complex number with positive real and imaginary parts, and a complex number with negative real and imaginary parts.

We also saw that the sine of a complex number is defined as the ratio of the imaginary part of the exponential function to twice the imaginary unit. Finally, we discussed how to use the cmplx.Sin() function in Golang to find the sine of a complex number and saw that it returns a complex number as a result.

Hopefully, this article has helped you understand how to find the sine of a complex number in Golang and provided you with useful examples to apply in your own projects.

Updated on: 17-Apr-2023

47 Views