Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Julia Complex and Rational Numbers

Julia Complex and Rational Numbers

The Julia language includes predefined complex and rational number types and supports all their standard mathematical operations and elementary functions.

The type promotion mechanism ensures that mixed operations between complex numbers, rationals, and other numeric types all produce results consistent with mathematical intuition.


Complex numbers

Complex numbers are an extension of the real numbers, making every polynomial equation have a root.

We call a number of the form z=a+bi (where a and b are both real) a complex number. Here a is called the real part, b the imaginary part, and i the imaginary unit, satisfying i² = -1.

Julia uses the global constant im for the imaginary unit i (representing the principal square root of -1). It does not use i or j because those are too commonly used as loop variable names, to avoid conflicts.

Because Julia allows numeric literals as coefficients, this binding is enough to provide a very convenient syntax for complex numbers, similar to traditional mathematical notation:

Example

julia

1

+2im

1

We can perform various arithmetic operations on complex numbers:

Example

julia

(

1

)

(

2

)

8

julia

(

1

)

/

(

1

)

0.6

julia

(

1

)

(

1

)

2

julia

(

1

)

^

2

3

julia

(

1

)

^

2.5

2.729624464784009

julia

3

(

2

)

^

2

63

The type promotion mechanism also ensures you can use combinations of operands of different types:

Example

julia

(

1

)

0.5

1.5

julia

(

2

)

2.0

julia

(

2

)

/

2

1.0

julia

(

1

)

/

(

2

)

0.5

julia

2im^

2

2

julia

1

3

/4im

1.0

Note 3/4im == 3/(4im) == -(3/4im), because literal coefficients have higher precedence than division.

Standard functions for complex numbers

Julia provides some standard functions for operating on complex numbers:

Example

julia

z =

1

1

julia

real

(

z

)

   

real part of z

1

julia

imag

(

z

)

   

imaginary part of z

2

julia

conj

(

z

)

   

complex conjugate of z

1

julia

abs

(

z

)

     

absolute value (modulus) of z

2.23606797749979

julia

abs2

(

z

)

   

square of the absolute value

5

julia

angle

(

z

)

   

phase angle in radians

1.1071487177940904

By convention, the absolute value (abs) of a complex number is its distance from zero. abs2 gives the square of the absolute value, which is very useful for complex numbers because it avoids the square root operation. angle returns the phase angle in radians (also called the argument function).

All other elementary functions are also fully defined for complex numbers:

Example

julia

sqrt

(

1im

)

0.7071067811865476

julia

sqrt

(

1

)

1.272019649514069

julia

cos

(

1

)

2.0327230070196656

julia

exp

(

1

)

1.1312043837568135

julia

sinh

(

1

)

0.4890562590412937

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 on -1 and -1 + 0im, even though -1 == -1 + 0im:

Example

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

)

0.0

Constructing complex numbers from variables

When constructing complex numbers from variables, the literal coefficient notation no longer applies and multiplication must be written explicitly:

Example

julia

a =

1

;

b =

2

;

a + b

im

1

However, this is not recommended; instead use the more efficient complex function to build a complex value directly from its real and imaginary parts:

Example

julia

a =

1

;

b =

2

;

complex

(

a, b

)

1

This construction avoids multiplication and addition operations.

Inf and NaN may appear in the real and imaginary parts of a complex number, as described in the section on special floating-point values:

Example

julia

1

im

1.0

im

julia

1

im

1.0

im


Rational numbers

Rational numbers is a general term for integers (positive integers, 0, negative integers) and fractions, i.e. numbers expressible as a ratio of two integers, e.g. 2/3; by contrast irrational numbers such as √2 cannot be expressed as a ratio of integers.

Julia has a built-in Rational type for exactly representing integer ratios. Fractions are constructed with the // operator:

Example

julia

2

//

3

2

//

3

If a fraction's numerator and denominator share a common factor, they are automatically reduced to lowest terms 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 an integer ratio 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 fraction value can be obtained with the numerator and denominator functions:

Example

julia

numerator

(

2

//

3

)

2

julia

denominator

(

2

//

3

)

3

Direct comparison of numerator and denominator is usually unnecessary because standard arithmetic and comparison operations are also defined for fraction values:

Example

julia

2

//

3

==

6

//

9

true

julia

2

//

3

==

9

//

27

false

julia

3

//

7

<

1

//

2

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

Fractions can be easily converted to floating-point numbers:

Example

julia

float

(

3

//

4

)

0.75

For any integer values a and b (except when a == 0 and b == 0), the conversion from fraction to floating-point obeys the identity float(a//b) == a/b:

Example

julia

a =

1

;

b =

2

;

julia

isequal

(

float

(

a//b

)

, a/b

)

true

Julia accepts constructing infinite fraction values:

Example

julia

5

//

0

1

//

0

julia

x = -

3

//

0

1

//

0

julia

typeof

(

x

)

Rational

{

Int64

}

but does not accept attempting to construct a NaN fraction value:

Example

julia

0

//

0

ERROR: ArgumentError: invalid rational: zero

(

Int64

)

//zero

(

Int64

)

Stacktrace:

[

...

]

As usual, the type promotion system makes it easy for fractions to interact with other numeric types:

Example

julia

3

//

5

1

8

//

5

julia

3

//

5

0.5

0.09999999999999998

julia

2

//

7

(

1

)

2

//

7

4

//

7

im

julia

2

//

7

(

1.5

)

0.42857142857142855

julia

1

//

2

1

//

2

2

//

1

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

Tip:Fraction arithmetic has no floating-point error, so the Rational type can be preferred in scenarios requiring exact computation (such as amounts and ratios). Note that mixing fractions with floating-point numbers makes the result a floating-point number.