A complex number is a number that contains a real part and an imaginary part. In Julia, they are represented in the form of
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.
The code of the sum of two imaginary numbers calculated in Julia is as described in the code below.
a=1+2imb=3+5imresult=a+bprintln("(",a,") + (",b,") = ",result)
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+2imb=3+5imresult=a-bprintln("(",a,") - (",b,") = ",result)
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+2imb=3+5imresult=a*bprintln("(",a,")(",b,") = ",result)
Likewise, as in other arithmetic operations, the calculation of division in complex datatype can also be performed, as given below:
a=1+2imb=3+5imresult=a/bprintln("(",a,")/(",b,") = ",result)
We can also take the modulus of a complex number in Julia. For this, we use the abs
function,
a=3+4imresult=abs(a)println(result)
Conjugate is multiplying the imaginary part of a complex number with a negative one. It is simply changing a sign of an imaginary part.
We can find the multiplicative inverse of complex numbers using the inv
function as defined in the code below.
a=4-3imresult=inv(a)println("inv(",a,") = ",result)
We can work with
a=1+2imprintln("sin(",a,") = ",sin(a))println("cos(",a,") = ",cos(a))println("tan(",a,") = ",tan(a))println("log(",a,") = ",log(a))
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