Home »
Julia
Complex numbers and operations on it in Julia
Julia | Complex Numbers: In this tutorial, we are going to learn how to represent the complex numbers in the Julia programming language? We will also learn various operations on complex numbers in Julia.
Submitted by IncludeHelp, on March 30, 2020
Julia | Complex Numbers
The syntax to represent the complex number in Julia is:
Syntax:
A+Bim
Here, A and B are the values, and im is the global constant which is bound to the complex number i representing the principal square root of -1.
Example 1:
# Julia program to demonstrate the example
# of complex numbers and operations on it
println(1+2im)
println((1+2im)+(2+4im))
# variables
x = 10+23im
println("x: ", x)
println("typeof(x): ", typeof(x))
Output
1 + 2im
3 + 6im
x: 10 + 23im
typeof(x): Complex{Int64}
Various operations on Complex Numbers
Example 2:
# Julia program to demonstrate the example
# of complex numbers and operations on it
x = 1+2im
y = 2.34+78.909im
println("x: ", x)
println("y: ", y)
# operations
add = x+y
sub = x-y
mul = x*y
div = x/y
exp = x^2
# printing
println("add: ", add)
println("sub: ", sub)
println("mul: ", mul)
println("div: ", div)
println("exp: ", exp)
Output
x: 1 + 2im
y: 2.34 + 78.909im
add: 3.34 + 80.909im
sub: -1.3399999999999999 - 76.909im
mul: -155.478 + 83.589im
div: 0.02569885734584168 - 0.011910741155137315im
exp: -3 + 4im
Reference: Julia Complex Numbers