Julia 1.0 Programming
eBook - ePub

Julia 1.0 Programming

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

Ivo Balbaert

Condividi libro
  1. 196 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Julia 1.0 Programming

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

Ivo Balbaert

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Julia 1.0 Programming è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Julia 1.0 Programming di Ivo Balbaert in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Computer Science e Programming Languages. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781788990059
Edizione
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...

Indice dei contenuti