Julia 1.0 Programming
eBook - ePub

Julia 1.0 Programming

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

Ivo Balbaert

Compartir libro
  1. 196 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Julia 1.0 Programming

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

Ivo Balbaert

Detalles del libro
Vista previa del libro
Índice
Citas

Información del 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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Julia 1.0 Programming un PDF/ePUB en línea?
Sí, puedes acceder a Julia 1.0 Programming de Ivo Balbaert en formato PDF o ePUB, así como a otros libros populares de Computer Science y Programming Languages. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2018
ISBN
9781788990059
Edición
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...

Índice