Julia 1.0 Programming
eBook - ePub

Julia 1.0 Programming

Dynamic and high-performance programming to build fast scientific applications, 2nd Edition

Ivo Balbaert

Share book
  1. 196 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Julia 1.0 Programming

Dynamic and high-performance programming to build fast scientific applications, 2nd Edition

Ivo Balbaert

Book details
Book preview
Table of contents
Citations

About This Book

Enter the exciting world of Julia, a high-performance language for technical computing

Key Features

  • Leverage Julia's high speed and efficiency for your applications
  • Work with Julia in a multi-core, distributed, and networked environment
  • Apply Julia to tackle problems concurrently and in a distributed environment

Book Description

The release of Julia 1.0 is now ready to change the technical world by combining the high productivity and ease of use of Python and R with the lightning-fast speed of C++. Julia 1.0 programming gives you a head start in tackling your numerical and data problems. You will begin by learning how to set up a running Julia platform, before exploring its various built-in types. With the help of practical examples, this book walks you through two important collection types: arrays and matrices. In addition to this, you will be taken through how type conversions and promotions work.

In the course of the book, you will be introduced to the homo-iconicity and metaprogramming concepts in Julia. You will understand how Julia provides different ways to interact with an operating system, as well as other languages, and then you'll discover what macros are. Once you have grasped the basics, you'll study what makes Julia suitable for numerical and scientific computing, and learn about the features provided by Julia. By the end of this book, you will also have learned how to run external programs.

This book covers all you need to know about Julia in order to leverage its high speed and efficiency for your applications.

What you will learn

  • Set up your Julia environment to achieve high productivity
  • Create your own types to extend the built-in type system
  • Visualize your data in Julia with plotting packages
  • Explore the use of built-in macros for testing and debugging, among other uses
  • Apply Julia to tackle problems concurrently
  • Integrate Julia with other languages such as C, Python, and MATLAB

Who this book is for

Julia 1.0 Programming is for you if you are a statistician or data scientist who wants a crash course in the Julia programming language while building big data applications. A basic knowledge of mathematics is needed to understand the various methods that are used or created during the course of the book to exploit the capabilities that Julia is designed with.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Julia 1.0 Programming an online PDF/ePUB?
Yes, you can access Julia 1.0 Programming by Ivo Balbaert in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Languages. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788990059
Edition
2

Variables, Types, and Operations

Julia is an optionally typed language, which means that the user can choose to specify the type of arguments passed to a function and the type of variables used inside a function. Julia's type system is the key for its performance; understanding it well is important, and it can pay off to use type annotations, not only for documentation or tooling, but also for execution speed. This chapter discusses the realm of elementary built-in types in Julia, the operations that can be performed on them, as well as the important concepts of types and scope.
The following topics are covered in this chapter:
  • Variables, naming conventions, and comments
  • Types
  • Integers
  • Floating point numbers
  • Elementary mathematical functions and operations
  • Rational and complex numbers
  • Characters
  • Strings
  • Regular expressions
  • Ranges and arrays
  • Dates and times
  • Scope and constants
You will need to follow along by typing in the examples in the REPL, or executing the code snippets in the code files of this chapter.

Variables, naming conventions, and comments

Data is stored in values such as 1, 3.14, and "Julia", and every other value has a type, for example, the type of 3.14 is Float64. Some other examples of elementary values and their data types are 42 of the Int64 type, true and false of the Bool type, and 'X' of the Char type.
Julia, unlike many modern programming languages, differentiates between single characters and strings. Strings can contain any number of characters, and are specified using double quotes—single quotes are only used for a character literal. Variables are the names that are bound to values by assignments, such as x = 42. They have the type of the value they contain (or reference); this type is given by the typeof function. For example, typeof(x) returns Int64.
The type of a variable can change, because putting x = "I am Julia" now results in typeof(x) returning String. In Julia, we don't have to declare a variable (that indicates its type) such as in C or Java, for instance, but a variable must be initialized (that is, bound to a value) so that Julia can deduce its type:
julia> y = 7 7  typeof(y) # Int64 julia> y + z ERROR: UndefVarError: z not defined 
In the preceding example, z was not assigned a value before we used it, so we got an error. By combining variables through operators and functions such as the + operator (as in the preceding example), we get expressions. An expression always results in a new value after computation. Contrary to many other languages, everything in Julia is an expression, so it returns a value. That's why working in a REPL is so great: because you can see the values at each step.
The type of variables determines what you can do with them, that is, the operators with which they can be combined with. In this sense, Julia is a strongly typed language. In the following example, x is still a String value, so it can't be summed with y, which is of type Int64, but if we give x a float value, the sum can be calculated, as shown in the following example:
julia> x + y ERROR: MethodError: no method matching +(::String, ::Int64) julia> x = 3.5; x + y 10.5 
Here, the semicolon (;) ends the first expression and suppresses its output. Names of the variables are case-sensitive. By convention, lowercase is used with multiple words separated by an underscore. They start with a letter and, after that, you can use letters, digits, underscores, and exclamation points. You can also use Unicode characters. Use clear, short, and to-the-point names. Here are some valid variable names: mass, moon_velocity, current_time, pos3, and ω1. However, the last two are not very descriptive, and they should better be replaced with, for example, particle_position and particle_ang_velocity.
A line of code preceded by a hash sign (#) is a comment, as we can see in the following example:
# Calculate the gravitational acceleration grav_acc: gc = 6.67e-11 # gravitational constant in m3/kg s2 mass_earth = 5.98e24 # in kg radius_earth = 6378100 # in m grav_acc = gc * mass_earth / radius_earth^2 # 9.8049 m/s2  
Multiline comments are helpful for writing comments that span across multiple lines or commenting out code. In Julia, all lines between #= and =# are treated as a comment. For printing out values, use the print or println functions, as follows:
julia> print(x) 3.5 
If you want your printed output to be in color, use printstyled("I love Julia!", color=:red), which returns the argument string in the color indicated by the second argument.
The term object (or instance) is frequently used when dealing with variables of more complex types. However, we will see that, when doing actions on objects, Julia uses functional semantics. We write action(object) instead of object.action(), as we do in more object-oriented languages such as Java or C#.
In a REPL, the value of the last expression is automatically displayed each time a statement is evaluated (unless it ends with a ; sign). In a standalone script, Julia will not display anything unless the script specifically instructs it to. This is achieved with a print or println statement. To display any object in the way the REPL does in code, use display(object) or show(object) (show is a basic function that prints a text representation of an object, which is often more specific than print).

Types

Julia's type system is unique. Julia behaves as a dynamically typed language (such as Python, for instance) most of the time. This means that a variable bound to an integer at one point might later be bound to a string. For example, consider the following:
julia> x = 10 10 julia> x = "hello" "hello" 
However, one can, optionally, add type information to a variable. This causes the variable to only accept values that match that specific type. This is done through a type of annotation. For instance, declaring x::String implies that only strings can be bound to x; in general, it looks like var::TypeName. These are used the most often to qualify the arguments a function can take. The extra type information is useful...

Table of contents