English 中文(简体)
Julia - Rational & Complex Numbers
  • 时间:2024-11-03

Rational and Complex Numbers


Previous Page Next Page  

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

Rational Numbers

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

Constructing rational numbers

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


jupa> 4//5
4//5

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


jupa> numerator(8//9)
8

jupa> 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 −


jupa> 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:

jupa> A = 20; B = 30;

jupa> 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 Jupa suffice to provide convenient syntax for complex numbers because Jupa allows numeric pterals to be contrasted with identifiers as coefficients.


jupa> 2+3im
2 + 3im

Performing Standard arithmetic operations

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


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

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

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

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

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

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

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

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

Combining different operands

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


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

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

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

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

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

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

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

jupa> 3im^3
0 - 3im

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

Functions to manipulate complex values

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


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

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

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

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

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

jupa> 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 −


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

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

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

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

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