Haskell Program to create Complex numbers from given imaginary parts


In this article we are going to use the internal function of Haskell like Data.complex and Prelude to create a complex number from a given imaginary parts. This tutorial will help us in creating the complex number from the given imaginary part. The imaginary part of a complex number is the coefficient of the imaginary unit, typically represented by the symbol "i", in the standard form of the complex number. A complex number can be represented in standard form as a + bi, where a is the real part and b is the imaginary part.

Algorithm

  • Step 1 − The Data.Complex module is imported to work over complex numbers.

  • Step 2 − The createComplexNumber function is defined

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − The variables named, “z1”,”z2” and “z3” are being initialized. It will hold the complex number which is obtained by pasasing imaginary part.

  • Step 5 − The resultant complex number is printed to the console on calling the function.

In this method, the createComplexNumber function takes a double value as an argument (representing the imaginary part) and returns a complex number with a real part of 0 and the provided imaginary part.

Example 1

In this example, we are going to see that how we can print the complex number by having imaginary part of the complex number. This can be done by using user-defined createComplexNumber function.

import Data.Complex

createComplexNumber :: Double -> Complex Double
createComplexNumber imag = 0 :+ imag

main = do
   let z1 = createComplexNumber 2
   let z2 = createComplexNumber 3
   let z3 = createComplexNumber (-4)
   print z1
   print z2
   print z3

Output

0.0 :+ 2.0
0.0 :+ 3.0
0.0 :+ (-4.0)

Example 2

In this example, we are going to see that how we can also use the :+ operator along with the Double type to create complex numbers with given imaginary parts.

import Data.Complex

main = do
   let z1 = 0 :+ 2
   let z2 = 0 :+ 3
   let z3 = 0 :+ (-4)
   print z1
   print z2
   print z3

Output

0 :+ 2
0 :+ 3
0 :+ (-4)

Example 3

In this example, we are going to see that how we can form the complex number by using tuple and then fetch real and imaginary part of the complex number using pattern matching.

createComplexNumber :: Double -> (Double, Double)
createComplexNumber imag = (0,imag)

main = do
   let (real,imag) = createComplexNumber 2
   print real
   print imag

Output

0.0
2.0

Example 4

In this example, we are going to see that how we can form the complex number from imaginary part of the complex number using cis and asin function.

import Prelude
import Data.Complex

createComplexNumber :: Double -> Complex Double
createComplexNumber imag = cis (asin(imag))

main = do
   let z1 = createComplexNumber 0
   print z1

Output

1.0 :+ 0.0

Conclusion

An imaginary part of the Complex number can be traced and Complex number can be formed through it in Haskell, by using user-defined function, or by using tuple or by using cis and asin functions.

Updated on: 24-Apr-2023

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements