Sunday, 7 February 2016

Julia: Complex numbers

A complex number is expressed on the form a+bi, where a and b are real numbers and ‘i’ is imaginary number. Value of i=sqrt(-1). In Julia global constant ‘im’ is used to represent the imaginary number ‘i’.
You can perform Arithmetic operations on complex numbers.
julia> a=3+4im
3 + 4im

julia> b=5im+4
4 + 5im

julia> a+b
7 + 9im

julia> a-b
-1 - 1im

julia> a/b
0.7804878048780489 + 0.02439024390243905im

julia> a*b
-8 + 31im

julia> a\b
1.28 - 0.04im

Construct complex numbers using complex() function
‘complex(r[, i])’ function convert the real numbers to complex numbers. Second argument is optional.

julia> complex(1,2)
1 + 2im

julia> complex(20,2)
20 + 2im

julia> complex(1)
1 + 0im


Get real and imaginary values of complex number
real(), imag() functions are used to get real and imaginary part of a complex number.

julia> real(99+123im)
99

julia> imag(99+123im)
123


Absolute value of complex number

Absolute value of complex number is the distance from 0. ‘abs()’ function is used to get the absolute value of complex number.

julia> abs(1+2im)
2.23606797749979

julia> abs(3+4im)
5.0


Get the square of absolute value
abs2() function return the square of absolute value of a complex number.

julia> abs2(1+2im)
5

julia> abs2(3+4im)
25


Get the conjugate of complex number
conj(z) computes the complex conjugate of a complex number z

julia> z=1+2im
1 + 2im

julia> conj(z)
1 - 2im


Compute the phase angle in radians
angle(z) computes the phase angle in radians of a complex number z.

julia> angle(1+2im)
1.1071487177940904

julia> angle(2+3im)
0.982793723247329









Previous                                                 Next                                                 Home

No comments:

Post a Comment