Haskell Program to Add Two Complex Numbers



This tutorial will discuss writing a program to add two complex numbers in the Haskell Programming Language. Haskell is a declarative, strongly typed, and functional programming language. The computations in Haskell are mathematical functions.

Complex numbers are a combination of real and imaginary values.

Example: 1 + 2i, where i is ?-1

In this tutorial, we see four ways to Add two complex numbers.

  • Computing the addition using the infix operator for addition "+".

  • Implementing addition computation in a separate function.

  • Computing the addition using the infix operator "+" as a function.

  • Computing the addition of complex numbers by extracting the real and imaginary parts.

Syntax

Following the syntax to add two complex numbers

c1 + c2

Where c1 and c2 are the complex numbers and "+" is an infix operator for addition.

To define complex numbers in Haskell we need to import the module Complex from the package Data using syntax Data.Complex, which is necessary to support Complex numbers. We can construct a complex number using the syntax (a :+ b) where a and b numbers. (a :+ b) is the same as (a + ib), and ":+" is a constructor to construct complex numbers.

Algorithmic steps

  • Initialize the complex numbers.

  • Implement the addition logic.

  • Print the output.

Adding Complex Numbers Using The Infix Operator "+"

Example

Program to add Complex numbers using the infix operator "+"

import Data.Complex main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 3 -- computing addition using infix operator let c = a + b print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)

Output

"Complex number 1 = "
1.0 :+ 2.0
"Complex number 2 = "
2.0 :+ 3.0
Addition of Complex numbers a and b is: 3.0 :+ 5.0

In the above program we imported module Complex from package Data. In the main function we initialized two variables a and b with complex numbers using the complex number constructor ":+?. We added the two numbers with the infix operator for addition "+" and loaded the value into a variable c. and finally, we printed the resultant complex number using function print.

Add Complex Numbers Using A Separate Function

Example

Program to add Complex numbers using a separate function

import Data.Complex -- function definition for adding complex numbers addComplex a b = a + b main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 7 print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- computing addition by invoking the addComplex funtion let c = addComplex a b -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)

Output

"Complex number 1 = "
1.0 :+ 2.0
"Complex number 2 = "
2.0 :+ 7.0
Addition of Complex numbers a and b is: 3.0 :+ 9.0

In the above program we imported module Complex from package Data. We defined a function addComplex which takes two complex numbers as input and returns an addition of the two numbers. In the main function we initialized two variables a and b with complex numbers using the complex number constructor ":+?. We invoked the function addComplex with a and b as parameters and loaded the returned value into a variable c. and finally, we printed the resultant complex number using function print.

Add Complex Numbers Using The Infix Operator As A Function.

Example

Program to add Complex numbers using the infix operator as a function.

import Data.Complex main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 10 print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- computing addition using infix operator as function let c = (+) a b -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)

Output

"Complex number 1 = "
1.0 :+ 2.0
"Complex number 2 = "
2.0 :+ 10.0
Addition of Complex numbers a and b is: 3.0 :+ 12.0

In the above program we imported module Complex from package Data. In the main function we initialized two variables a and b with complex numbers using the complex number constructor ":+?. We added the two numbers using the infix operator for addition as function (+) with a and b as parameters and loaded the value into a variable c. As said Haskell is a functional programming language all operators are syntactic sugar for function. We can convert infix operators into functions by encapsulating them in curved brackets.

Example ? addition operator "+" can be converted into a function using the syntax (+). Finally, we printed the resultant complex number using function print.

Adding Complex Numbers By Extracting The Real And Imaginary Parts

Example

Program to compute the addition of complex numbers by extracting the real and imaginary parts.

import Data.Complex main = do -- initializing complex numbers let a = 1 :+ 2 let b = 2 :+ 3 print("Complex number 1 = ") print(a) print("Complex number 2 = ") print(b) -- computing addition using infix operator as function let realSum = (realPart a) + (realPart b) let imagSum = (imagPart a) + (imagPart b) let c = realSum :+ imagSum -- printing the output putStr ("Addition of Complex numbers a and b is: ") print (c)

Output

"Complex number 1 = "
1 :+ 2
"Complex number 2 = "
2 :+ 3
Addition of Complex numbers a and b is: 3 :+ 5

In the above program we imported module Complex from package Data. In the main function we initialized two variables a and b with complex numbers using the complex number constructor ":+?. We are retrieving the real part of the complex numbers using the function realPart which is a function that takes input a complex number and returns the real part of the complex number and loads them into a variable realSum. Next, we are retrieving imaginary parts of the complex numbers using the function imagPart which is a function that takes input a complex number and returns the imaginary part of the complex number and loads them into a variable imagSum. We constructed a complex number from variables realSum and imagSum using constructor ":+" and loaded it into a variable c. Finally, we printed the resultant complex number.

Conclusion

In this tutorial we discussed four different ways to write a program to add complex numbers in Haskell programming Language.

Updated on: 2022-10-27T05:59:49+05:30

830 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements