Rational and Complex Numbers


Advertisements

In this chapter, we shall discuss rational and complex numbers.

Rational Numbers

Julia represents exact ratios of integers with the help of rational number type. Let us understand about rational numbers in Julia in further sections −

Constructing rational numbers

In Julia REPL, the rational numbers are constructed by using the operator //. Below given is the example for the same −

julia> 4//5
4//5

You can also extract the standardized numerator and denominator as follows −

julia> numerator(8//9)
8

julia> denominator(8//9)
9

Converting to floating-point numbers

It is very easy to convert the rational numbers to floating-point numbers. Check out the following example −

julia> float(2//3)
0.6666666666666666
Converting rational to floating-point numbers does not loose the following identity for any integral values of A and B. For example:

julia> A = 20; B = 30;

julia> isequal(float(A//B), A/B)
true

Complex Numbers

As we know that the global constant im, which represents the principal square root of -1, is bound to the complex number. This binding in Julia suffice to provide convenient syntax for complex numbers because Julia allows numeric literals to be contrasted with identifiers as coefficients.

julia> 2+3im
2 + 3im

Performing Standard arithmetic operations

We can perform all the standard arithmetic operations on complex numbers. The example are given below −

julia> (2 + 3im)*(1 - 2im)
8 - 1im

julia> (2 + 3im)/(1 - 2im)
-0.8 + 1.4im

julia> (2 + 3im)+(1 - 2im)
3 + 1im

julia> (2 + 3im)-(1 - 2im)
1 + 5im

julia> (2 + 3im)^2
-5 + 12im

julia> (2 + 3im)^2.6
-23.375430842463754 + 15.527174176755075im

julia> 2(2 + 3im)
4 + 6im

julia> 2(2 + 3im)^-2.0
-0.059171597633136105 - 0.14201183431952663im

Combining different operands

The promotion mechanism in Julia ensures that combining different kind of operators works fine on complex numbers. Let us understand it with the help of the following example −

julia> 2(2 + 3im)
4 + 6im

julia> (2 + 3im)-1
1 + 3im

julia> (2 + 3im)+0.7
2.7 + 3.0im

julia> (2 + 3im)-0.7im
2.0 + 2.3im

julia> 0.89(2 + 3im)
1.78 + 2.67im

julia> (2 + 3im)/2
1.0 + 1.5im

julia> (2 + 3im)/(1-3im)
-0.7000000000000001 + 0.8999999999999999im

julia> 3im^3
0 - 3im

julia> 1+2/5im
1.0 - 0.4im

Functions to manipulate complex values

In Julia, we can also manipulate the values of complex numbers with the help of standard functions. Below are given some example for the same −

julia> real(4+7im) #real part of complex number
4

julia> imag(4+7im) #imaginary part of complex number
7

julia> conj(4+7im) # conjugate of complex number
4 - 7im

julia> abs(4+7im) # absolute value of complex number
8.06225774829855

julia> abs2(4+7im) #squared absolute value
65

julia> angle(4+7im) #phase angle in radians
1.0516502125483738

Let us check out the use of Elementary Functions for complex numbers in the below example −

julia> sqrt(7im) #square root of imaginary part
1.8708286933869707 + 1.8708286933869707im

julia> sqrt(4+7im) #square root of complex number
2.455835677350843 + 1.4251767869809258im

julia> cos(4+7im) #cosine of complex number
-358.40393224005317 + 414.96701031076253im

julia> exp(4+7im) #exponential of complex number
41.16166839296141 + 35.87025288661357im

julia> sinh(4+7im) #Hyperbolic sine value of complex number
20.573930095756726 + 17.941143007955223im
Advertisements