Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Julia

You're reading from   Mastering Julia Enhance your analytical and programming skills for data modeling and processing with Julia

Arrow left icon
Product type Paperback
Published in Jan 2024
Publisher Packt
ISBN-13 9781805129790
Length 506 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Malcolm Sherrington Malcolm Sherrington
Author Profile Icon Malcolm Sherrington
Malcolm Sherrington
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Chapter 1: The Julia Environment 2. Chapter 2: Developing in Julia FREE CHAPTER 3. Chapter 3: The Julia Type System 4. Chapter 4: The Three Ms 5. Chapter 5: Interoperability 6. Chapter 6: Working with Data 7. Chapter 7: Scientific Programming 8. Chapter 8: Visualization 9. Chapter 9: Database Access 10. Chapter 10: Networks and Multitasking 11. Chapter 11: Julia’s Back Pages 12. Index 13. Other Books You May Enjoy

Complex and rational numbers

We have met the syntax for rational numbers in the previous chapter, and we will review operations on them here. Also, we will introduce another arithmetic type: complex numbers.

Complex numbers

There are two ways to define a complex number in Julia—first, using the Complex type definition as its associated Complex() constructor:

# Note the difference in these two definitions
julia> c = Complex(1, 2); typeof(c)
Complex{Int64}
julia> c = Complex(1, 2.0); typeof(c)Complex{Float64}
julia> c = ComplexF32(1,2.0); typeof(c)Complex{Float32}

Because in the second example, the complex number consists of an ordered pair of two reals, its size is 128 bits, whereas ComplexF32 has 2x Float32 arguments and ComplexF16 will have 2x Float16 arguments.

The Complex(0.0,1.0) number corresponds to the imaginary number 'I'—that is, sqrt(-1.0)—but Julia uses the 'im' symbol rather than 'I' to avoid confusion with an I variable, frequently used as an index iterator.

Hence, Complex(1, 2) is exactly equivalent to 1 + 2*im, but normally the '*' operator is omitted, and this would be expressed as 1 + 2im.

The complex number supports all normal arithmetic operations, as illustrated here:

julia> c = 1 + 2im;
julia> d = 3 + 4im;
julia> c*d
-5 + 10im
julia> c/d
0.44 + 0.08im
julia> c\d
2.2 - 0.4im

The c/d and c\d divisions produce real arguments even when the components are integers.

This is like Julia’s behavior with a simple division of integers. Also, it defines real(), imag(), conj(), abs(), and angle() complex functions.

abs and angle can be used to convert complex arguments to polar form:

julia> c = 1.0 + 2im; abs(c)
2.23606797749979
julia> angle(c)
1.1071487177940904 # (in radians)

Complex versions of many mathematical functions can be applied:

julia> c = 1 + 2im;
julia> sin(c)
3.1657 + 1.9596im
julia> log(c)
0.8047 + 1.10715im
julia> sqrt(c)
1.272 + 0.78615im

Rationals

Julia has a rational number type to represent exact ratios of integers. A rational is defined by the use of the // operator—for example, 5//7. If the numerator and denominator have a common factor, then the number is reduced to its simplest form; for example, 21//35 reduces to 5//7.

Operations on rationals or on mixed rationals and integers return a rational result:

julia> x = 3; y = 5//7;
julia> x*y
15//7
julia> y^2
25/49
julia> y/x
5//21

The numerator() and denominator() functions return the numerator and denominator of a rational, and float() can be used to convert a rational to a float:

julia> x = 17//100;
julia> numerator(x)
17
julia> denominator(x)
100
julia> float(x) => 0.17

Constructing infinite rational values, both positive and negative, is acceptable:

julia> 5//0
1//0
julia> -5//0
-1//0

Notice that both computations reduce the numerator to 1. It is possible to construct rationals of complex numbers, as in this example:

julia> c = (1 + 2im)//(4 + 3im)
2//5 + 1//5*im

This output is a little confusing as the actual value is (2 + 1im)//5, which arises by multiplying the top and bottom values by the complex conjugate of the denominator (4 – 3im).

The typeof(c) value is Complex{Rational{Int64}}, and as of now, the numerator() and denominator() functions fail, even though these should return (2 + 1im) and 5 respectively:

julia> numerator(c)
ERROR: MethodError: no method matching
       numerator(::Complex{Rational{Int64}})

Closest candidates are:

  numerator(::Integer) at rational.jl:236
  numerator(::Rational) at rational.jl:237
You have been reading a chapter from
Mastering Julia - Second Edition
Published in: Jan 2024
Publisher: Packt
ISBN-13: 9781805129790
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime