Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Julia complex and score


May 14, 2021 Julia


Table of contents


Complex numbers and scores

Julia provides complex and fractional types and supports all standard mathematical operations. When you mix different data types, both underlying and composite, type conversion and type promotion are automatically used.

Plural

The global im or plual i, represents the positive square root of -1. B ecause i often used as an index variable, it is not used to represent complex numbers. Julia allows numeric text to act as an algema coefficient, which also applies to complex numbers:

julia> 1 + 2im
1 + 2im

You can do standard arithmetic for 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.7296244647840084 - 6.960664459571898im

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 ensures that different types of computing objects can be operated together:

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) the text coefficient takes precedence over the divide.

Standard functions for dealing with complex numbers:

julia> real(1 + 2im)
1

julia> imag(1 + 2im)
2

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

julia> abs(1 + 2im)
2.23606797749979

julia> abs2(1 + 2im)
5

julia> angle(1 + 2im)
1.1071487177940904

Typically, the absolute value abs number is the distance from it to zero. T he abs2 the square of the absolute value, specifically used in complex numbers to avoid opening the root. angle function returns the phase of the radian (that is, argument or arg). All basic functions can also be applied to complex numbers:

julia> sqrt(1im)
0.7071067811865476 + 0.7071067811865475im

julia> sqrt(1 + 2im)
1.272019649514069 + 0.7861513777574233im

julia> cos(1 + 2im)
2.0327230070196656 - 3.0518977991518im

julia> exp(1 + 2im)
-1.1312043837568135 + 2.4717266720048188im

julia> sinh(1 + 2im)
-0.4890562590412937 + 1.4031192506220405im

Mathematical functions that act on real numbers, the return value is generally real, and on complex numbers, the return value is complex. For example, sqrt has different results for -1 and -1 + 0im -1 == -1 + 0im

julia> sqrt(-1)
ERROR: DomainError
sqrt will only return a complex result if called with a complex argument.
try sqrt(complex(x))
 in sqrt at math.jl:131

julia> sqrt(-1 + 0im)
0.0 + 1.0im

Algegegeon coefficients cannot be used to construct complex numbers using variables. Multiplication must be written explicitly:

julia> a = 1; b = 2; a + b*im
1 + 2im

However, the above method is not recommended. It is recommended complex complex numbers using the complex function:

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

This construction avoids multiplication and addition.

Inf NaN participate in constructing complex numbers (refer to the Special Floating Points section):

julia> 1 + Inf*im
1.0 + Inf*im

julia> 1 + NaN*im
1.0 + NaN*im

Score Julia has a score type. Construct a score using the // operator:

julia> 2//3
2//3

If the number of conventions is in the molecule and denominator, it is automatically simplified to the simplest fraction, and 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

After the score is unique, you can compare the molecules, denominator to determine whether the two scores are equal. Use num and den to obtain the approximately simple molecules and denominators:

julia> num(2//3)
2

julia> den(2//3)
3

In fact, there is no need to compare scores and denominatores, we have defined standard arithmetic and comparison operations for fractions:

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

Scores can simply be converted to floating points:

julia> float(3//4)
0.75

The conversion of fractions to floating points is followed for any integers a and b with the exception of a s a == 0 b == 0 there are:

julia> isequal(float(a//b), a/b)
true

You can construct a score Inf

julia> 5//0
1//0

julia> -3//0
-1//0

julia> typeof(ans)
Rational{Int64} (constructor with 1 method)

However, a score with a NaN be constructed:

julia> 0//0
ERROR: invalid rational: 0//0
 in Rational at rational.jl:6
 in // at rational.jl:15

The type-boosting system makes it easy for fraction types to interact 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