Complex and rational numbers
Julia has predefined types for complex and rational numbers, and all standard types are supported for them. mathematical operations and elementary functions. In addition, there is a system transformations and promotions, so that operations with any combination of predefined numeric types, whether primitive or composite, are performed in the expected manner.
Complex numbers
The global constant im
is tied to the complex number i, representing the main value of the square root of --1. (It was decided to abandon the use of the designation adopted in mathematics (i
) or engineering (j
) for this global constant due to the prevalence of index variables as names.) Since Julia allows write the coefficients as numeric literals next to the variable names, this binding is sufficient for convenient writing of complex numbers, just as it is done in mathematics:
julia> 1+2im
1 + 2im
All standard arithmetic operations can be performed with complex numbers.:
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.20689655172413793 + 0.5172413793103449im
The promotion mechanism makes possible combinations of operands of different types.:
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: 3/4im == 3/(4*im) == -(3/4*im)
, since the binding of the literal coefficient takes precedence over the division.
Standard functions for operations with complex values are available.:
julia> z = 1 + 2im
1 + 2im
julia> real(1 + 2im) # вещественная часть z
1
julia> imag(1 + 2im) # imaginary part of z
2
julia> conj(1 + 2im) # complex conjugate value of z
1 - 2im
julia> abs(1 + 2im) # absolute value of z
2.23606797749979
julia> abs2(1 + 2im) # absolute value squared
by 5
julia> angle(1 + 2im) # phase angle in radians
1.1071487177940904
As usual, the absolute value (abs
) of a complex number is its distance from zero. Function abs2
returns the square root of an absolute value and is especially useful for complex numbers because it does not extract the square root. Function angle
returns the phase angle in radians (also called the complex argument or simply the argument). For complex numbers, the whole range of others is also defined. elementary functions:
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 ones when applied to complex numbers. For example, when applied to -1
and -1 + 0im', the function `sqrt
behaves differently, despite the fact that -1 == -1 + 0im
:
julia> sqrt(-1)
ERROR: DomainError with -1.0:
sqrt was called with a negative real argument but 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
Writing coefficients as numeric literals is not supported when constructing complex numbers from variables. In this case, the multiplication must be written explicitly.:
julia> a = 1; b = 2; a + b*im
1 + 2im
However, it is not recommended to do this. Use a more efficient function instead. complex
to form a complex value directly from its real and imaginary parts:
julia> a = 1; b = 2; complex(a, b)
1 + 2im
Multiplication and addition operations are not required.
Inf
and NaN
is passed through the real and imaginary parts of a complex number, as described in the section Special floating point values:
julia> 1 + Inf*im
1.0 + Inf*im
julia> 1 + NaN*im
1.0 + NaN*im
Rational numbers
Julia has a type of rational numbers that represents the exact values of integer fractions. Rational numbers are created using the operator //
:
julia> 2//3
2//3
If the numerator and denominator of a fraction have a common multiplier, they are reduced to an irreducible form so that the denominator is non-negative.:
julia> 6//9
2//3
julia> -4//8
-1//2
julia> 5//-15
-1//3
julia> -4//-12
1//3
This normalized form of an integer fraction is unique, so you can check rational values for equality by checking the equality of their numerators and denominators. The normalized numerator and denominator of a rational value can be obtained using the functions numerator
and denominator
:
julia> numerator(2//3)
2
julia> denominator(2//3)
3
It is usually not necessary to compare the numerator and denominator directly, since standard arithmetic and comparison operations are defined for rational values.:
julia> 2//3 == 6//9
true
julia> 2//3 == 9//27
false
julia> 3//7 < 1//2
true
julia> 3//4 > 2//3
true
julia> 2//4 + 1//6
2//3
julia> 5//12 - 1//4
1//6
julia> 5//8 * 3//12
5//32
julia> 6//5 / 10//7
21//25
Rational numbers can be easily converted to floating point numbers.:
julia> float(3//4)
0.75
When converting rational numbers to floating point numbers for any integer values a
and b
, except for a==0 && b <= 0
, the following identity is observed:
julia> a = 1; b = 2;
julia> isequal(float(a//b), a/b)
true
julia> a, b = 0, 0
(0, 0)
julia> float(a//b)
ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)
Stacktrace:
[...]
julia> a/b
NaN
julia> a, b = 0, -1
(0, -1)
julia> float(a//b), a/b
(0.0, -0.0)
Infinite rational values can be created.:
julia> 5//0
1//0
julia> x = -3//0
-1//0
julia> typeof(x)
Rational{Int64}
However, an attempt to create a rational value NaN
will result in an error:
julia> 0//0
ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)
Stacktrace:
[...]
As usual, the promotion system makes it easy to perform operations with other numeric types.:
julia> 3//5 + 1
8//5
julia> 3//5 - 0.5
0.09999999999999998
julia> 2//7 * (1 + 2im)
2//7 + 4//7*im
julia> 2//7 * (1.5 + 2im)
0.42857142857142855 + 0.5714285714285714im
julia> 3//2 / (1 + 2im)
3//10 - 3//5*im
julia> 1//2 + 2im
1//2 + 2//1*im
julia> 1 + 2//3im
1//1 - 2//3*im
julia> 0.5 == 1//2
true
julia> 0.33 == 1//3
false
julia> 0.33 < 1//3
true
julia> 1//3 - 0.33
0.0033333333333332993