Julia Complex And Rational Numbers
In this chapter, we will mainly learn about complex numbers and rational numbers in Julia.
The Julia language includes predefined complex and rational number types and supports various standard mathematical operations and elementary functions for them.
### Complex Numbers
A complex number is an extension of real numbers that ensures any polynomial equation has roots.
We call a number in the form z=a+bi (where both a and b are real numbers) a complex number. Among these, a is called the real part, b is called the imaginary part, and i is called the imaginary unit, which has the property!(https://static.jyshare.com/images/svg/cab082841288a1395251d6d7ab06156bbd1dbee2.svg). When the imaginary part b of z is 0, then z is a real number; when the imaginary part b of z is not 0 and the real part a is 0, z is often called a pure imaginary number.
The global constant im is bound to the complex number i, representing the principal square root of -1.
Since Julia allows numeric literals as numeric literal coefficients, this binding is sufficient to provide very convenient syntax for complex numbers, similar to traditional mathematical notation:
## Example
julia>1+2im
1 + 2im
We can also perform various arithmetic operations on complex numbers:
## Example
julia>(1 + 2im)*(2 - 3im)
8 + 1im
julia>(1 + 2im)/(1 - 2im)
-0.6 + 0.8im
julia>(1 + 2im) + (1 - 2im)
2 + 0im
julia>(-3 + 2im) - (5 - 1im)
-8 + 3im
julia>(-1 + 2im)^2
-3 - 4im
julia>(-1 + 2im)^2.5
2.729624464784009 - 6.9606644595719im
julia>(-1 + 2im)^(1 + 1im)
-0.27910381075826657 + 0.08708053414102428im
julia>3(2 - 5im)
6 - 15im
julia>3(2 - 5im)^2
-63 - 60im
julia>3(2 - 5im)^-1.0
0.20689655172413796 + 0.5172413793103449im
The type promotion mechanism also ensures that you can use combinations of operands of different types:
## Example
julia>2(1 - 1im)
2 - 2im
julia>(2 + 3im) - 1
1 + 3im
julia>(1 + 2im) + 0.5
1.5 + 2.0im
julia>(2 + 3im) - 0.5im
2.0 + 2.5im
julia>0.75(1 + 2im)
0.75 + 1.5im
julia>(2 + 3im) / 2
1.0 + 1.5im
julia>(1 - 3im) / (2 + 2im)
-0.5 - 1.0im
julia> 2im^2
-2 + 0im
julia>1 + 3/4im
1.0 - 0.75im
Note that 3/4im == 3/(4*im) == -(3/4*im), because the coefficient has higher precedence than division.
Julia provides some standard functions for operating on complex numbers:
## Example
julia> z = 1 + 2im
1 + 2im
julia> real(1 + 2im)# real part of z
1
julia> imag(1 + 2im)# imaginary part of z
2
julia> conj(1 + 2im)# complex conjugate of z
1 - 2im
julia> abs(1 + 2im)# absolute value of z
2.23606797749979
julia> abs2(1 + 2im)# absolute value squared
5
julia> angle(1 + 2im)# phase angle in radians
1.1071487177940904
By convention, the absolute value (abs) of a complex number is the distance from zero to it. abs2 gives the squared absolute value, which is very useful when working with complex numbers because it avoids taking a square root. angle returns the phase angle in radians (also known as the argument function). All other elementary functions are also fully defined for complex numbers:
## Example
julia> sqrt(1im)
0.7071067811865476 + 0.7071067811865475im
julia> sqrt(1 + 2im)
1.272019649514069 + 0.7861513777574233im
julia> cos(1 + 2im)
2.0327230070196656 - 3.0518977991517997im
julia> exp(1 + 2im)
-1.1312043837568135 + 2.4717266720048188im
julia> sinh(1 + 2im)
-0.4890562590412937 + 1.4031192506220405im
Note that mathematical functions usually return real values when applied to real numbers and complex values when applied to complex numbers. For example, sqrt behaves differently when applied to -1 and -1 + 0im, although -1 == -1 + 0im:
## Example
julia> sqrt(-1)
ERROR: DomainError with -1.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
[...]
julia> sqrt(-1 + 0im)
0.0 + 1.0im
When constructing complex numbers from variables, the literal numeric coefficient notation no longer applies. Instead, multiplication must be written explicitly:
## Example
julia> a = 1; b = 2; a + b*im
1 + 2im
However, we do not recommend doing this. Instead, you should use the more efficient complex function to directly construct a complex value from the real and imaginary parts:
## Example
julia> a = 1; b = 2; complex(a, b)
1 + 2im
This construction avoids multiplication and addition operations.
Inf and NaN may appear in the real and imaginary parts of complex numbers, as described in the special floating-point values section:
## Example
julia>1 + Inf*im
1.0 + Inf*im
julia>1 + NaN*im
1.0 + NaN*im
### Rational Numbers
Rational numbers are the collective term for integers (positive integers, zero, negative integers) and fractions, and are the set of integers and fractions.
In mathematics, numbers that can be expressed as the ratio of two integers (!(https://static.jyshare.com/images/svg/9fbb66e57f89debc3cde3213de12228971148a93.svg), !(https://static.jyshare.com/images/svg/ad073253b4c817f2ec7e3dd7517b7f89a8e581dc.svg)) are defined as rational numbers, for example!(https://static.jyshare.com/images/svg/f6fe52a498e9788c548326304098d2122ca4645d.svg), 0.75 (which can be expressed as!(https://static.jyshare.com/images/svg/7572f1241ec7c2f9311985bc3dfb0b7d6f491e44.svg)). Integers and fractions are collectively called rational numbers. The opposite of rational numbers is irrational numbers, such as!(https://static.jyshare.com/images/svg/b4afc1e27d418021bf10898eb44a7f5f315735ff.svg), which cannot be expressed as a ratio of integers.
Julia has a fraction type for representing exact ratios of integers. Fractions are constructed using the // operator:
## Example
julia>2//3
2//3
If a fraction's numerator and denominator have common factors, they are reduced to simplest form with a non-negative denominator:
## Example
julia>6//9
2//3
julia> -4//8
-1//2
julia>5//-15
-1//3
julia> -4//-12
1//3
This normalized form of integer ratios is unique, so equality of fraction values can be tested by checking that both numerator and denominator are equal. The normalized numerator and denominator of a
YouTip