How to work with a complex data type in Julia

A complex number is a number that contains a real part and an imaginary part. In Julia, they are represented in the form of a+bima+b im. Here aa is the representation of the real part, bb is the representation of the imaginary part, and imim defines the value 1\sqrt{-1}. In this Answer, we discuss how to deal with complex numbers in Julia.

There is a built-in complex data type in Julia that we can use to perform arithmetic operations on complex numbers. The code of each arithmetic operation is given below.

Addition

The code of the sum of two imaginary numbers calculated in Julia is as described in the code below.

a=1+2im
b=3+5im
result=a+b
println("(",a,") + (",b,") = ",result)

Subtraction

Similarly, the code for subtracting numbers in complex data types is given below. You can change the values of the variables for better understanding.

a=1+2im
b=3+5im
result=a-b
println("(",a,") - (",b,") = ",result)

Multiplication

Complex data types in Julia provide us with the opportunity to calculate the multiplication of complex numbers with only one line of code. Remember, that if we are going to solve it on paper, it will take many more steps.

a=1+2im
b=3+5im
result=a*b
println("(",a,")(",b,") = ",result)

Division

Likewise, as in other arithmetic operations, the calculation of division in complex datatype can also be performed, as given below:

a=1+2im
b=3+5im
result=a/b
println("(",a,")/(",b,") = ",result)

Modulus

We can also take the modulus of a complex number in Julia. For this, we use the abs function,

a=3+4im
result=abs(a)
println(result)

Conjugate

Conjugate is multiplying the imaginary part of a complex number with a negative one. It is simply changing a sign of an imaginary part.

a=1+2im
b=3+5im
result=conj(a)
println("Conjugate(",a,") = ",result)

Multiplicative Inverse

We can find the multiplicative inverse of complex numbers using the inv function as defined in the code below.

a=4-3im
result=inv(a)
println("inv(",a,") = ",result)

Trigonometric functions

We can work withsinesine, cosinecosine, tantan, and loglog functions for complex datatype in Julia as well. Read the code below to learn to add such functions.

a=1+2im
println("sin(",a,") = ",sin(a))
println("cos(",a,") = ",cos(a))
println("tan(",a,") = ",tan(a))
println("log(",a,") = ",log(a))

Conclusion

In this Answer, we learned to work with complex data types and apply arithmetic functions to complex numbers in Julia. We also figured out how to find the conjugate, multiplicative inverse, and modulus of a complex number.

Note: Explore this Answer to extract the real and imaginary parts of a complex number.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved