Julia 1.0 Programming Cookbook - Bogumil Kaminski - E-Book

Julia 1.0 Programming Cookbook E-Book

Bogumił Kamiński

0,0
40,81 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Discover the new features and widely used packages in Julia to solve complex computational problems in your statistical applications.




Key Features



  • Address the core problems of programming in Julia with the most popular packages for common tasks


  • Tackle issues while working with Databases and Parallel data processing with Julia


  • Explore advanced features such as metaprogramming, functional programming, and user defined types











Book Description



Julia, with its dynamic nature and high-performance, provides comparatively minimal time for the development of computational models with easy-to-maintain computational code. This book will be your solution-based guide as it will take you through different programming aspects with Julia.







Starting with the new features of Julia 1.0, each recipe addresses a specific problem, providing a solution and explaining how it works. You will work with the powerful Julia tools and data structures along with the most popular Julia packages. You will learn to create vectors, handle variables, and work with functions. You will be introduced to various recipes for numerical computing, distributed computing, and achieving high performance. You will see how to optimize data science programs with parallel computing and memory allocation. We will look into more advanced concepts such as metaprogramming and functional programming. Finally, you will learn how to tackle issues while working with databases and data processing, and will learn about on data science problems, data modeling, data analysis, data manipulation, parallel processing, and cloud computing with Julia.







By the end of the book, you will have acquired the skills to work more effectively with your data





What you will learn



  • Boost your code's performance using Julia's unique features


  • Organize data in to fundamental types of collections: arrays and dictionaries


  • Organize data science processes within Julia and solve related problems


  • Scale Julia computations with cloud computing


  • Write data to IO streams with Julia and handle web transfer


  • Define your own immutable and mutable types


  • Speed up the development process using metaprogramming





Who this book is for



This book is for developers who would like to enhance their Julia programming skills and would like to get some quick solutions to their common programming problems. Basic Julia programming knowledge is assumed.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 505

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Julia 1.0 Programming Cookbook

 

Over 100 numerical and distributed computing recipes for your daily data science workflow

 

 

 

 

 

Bogumił Kamiński
Przemysław Szufel

 

 

 

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Julia 1.0 Programming Cookbook

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Richa TripathiAcquisition Editor: Alok DhuriContent Development Editor: Zeeyan PinheiroTechnical Editor: Gaurav GalaCopy Editor: Safis EditingProject Coordinator: Vaidehi SawantProofreader: Safis EditingIndexer: Rekha NairGraphics: Alishon MendonsaProduction Coordinator: Deepika Naik

First published: November 2018

Production reference: 1281118

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78899-836-9

www.packtpub.com

 

To the memory of my mother for setting me on a journey to explore science.
– Bogumił Kamiński
I would like to thank my wonderful wife Bożena for her support and understanding during the writing of this book and my daughters Zofia and Maria for bringing inspiration to each day of my life.
– Przemysław Szufel
We would like to thank our friend Timothy (Timek) Harrell for proofreading an initial version of this text and for valuable language and technical feedback.
– Bogumił Kamiński and Przemysław Szufel
 
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

Packt.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. 

Contributors

About the authors

Bogumił Kamiński (GitHub username: bkamins) is an associate professor and head of the Decision Support and Analysis Unit at the SGH Warsaw School of Economics, as well as adjunct professor at the data science laboratory, Ryerson University, Toronto. He is co-editor of the Central European Journal of Economic Modeling and Econometrics, and of the Multiple Criteria Decision Making journal. His scientific interests center on operational research and computational social science. He has authored over 50 research articles on simulation, optimization, and prediction methods. He also has 15+ years' experience in the deployment of large-scale advanced analytics solutions for industry and public administration.

 

 

 

 

 

 

 

Przemysław Szufel (GitHub username: pszufe, web: szufel.pl) is an assistant professor in the Decision Support and Analysis Unit at the SGH Warsaw School of Economics. His current research focuses on distributed systems and methods for the execution of large-scale simulations for numerical experiments and optimization. He is working on asynchronous algorithms for the parallel execution of large-scale computations in the cloud and distributed computational environments. He has authored, and co-authored, several open source tools for high-performance and numerical simulation.

About the reviewer

Jalem Raj Rohit is an IIT Jodhpur graduate with a keen interest in recommender systems, machine learning, and serverless and distributed systems. Raj currently works as a data scientist at GEP, prior to which he worked at GEP and Kayako. He contributes to open source projects in Python, Go, and Julia. He also speaks at tech conferences on serverless engineering and machine learning.

 

 

 

 

 

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Julia 1.0 Programming Cookbook

Dedication

About Packt

Why subscribe?

Packt.com

Contributors

About the authors

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Get in touch

Reviews

Installing and Setting Up Julia

Introduction

Installing Julia from binaries

Getting ready

How to do it...

Installing Julia on Linux Ubuntu

Installing Julia on Windows

There's more...

See also

Julia IDEs

Getting ready

How to do it...

Juno

Microsoft Visual Studio Code

Sublime Text

See also

Julia support for text editors

Getting ready

How to do it...

Configuring Julia with Nano

Configuring Julia with Vim

Configuring Julia with Emacs

See also

Building Julia from sources on Linux

Getting ready

How to do it...

Option 1 – build Julia without Intel's MKL

Option 2 – build Julia with Intel MKL (without Intel LIBM)

Option 3 – build Julia with Intel MKL and with Intel LIBM

How it works...

There's more...

See also

Running Julia inside the Cloud9 IDE in the AWS cloud

Getting ready

How to do it...

How it works...

See also

How to customize Julia on startup

Getting ready

How to do it...

Running a script on Julia startup

Running a single Julia command on startup

Running a script every time Julia starts

How it works...

There's more...

See also

Setting up Julia to use multiple cores

Getting ready

How to do it...

Multiple processes

Multiple threads

How it works...

There's more...

See also

Useful options for interaction with Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Displaying computation results in Julia

Getting ready

How to do it…

How it works…

There's more…

See also

Managing packages

Getting ready

How to do it...

How it works...

There's more...

See also

Configuring Julia in Jupyter Notebook

Getting ready

How to do it...

Running Jupyter Notebook from within the Julia environment

Running Jupyter Notebook outside of the Julia environment

How it works...

There's more...

See also

Configuring Julia to work with JupyterLab

Getting ready

How to do it...

How it works...

There's more...

Running JupyterLab with Anaconda on Linux

Running JupyterLab with Anaconda on Windows 

See also

Configuring Julia with Jupyter Notebook in Terminal-only cloud environments

Getting ready

How to do it...

How it works...

There's more...

See also

Data Structures and Algorithms

Introduction

Finding the index of a random minimum element in an array

Getting ready

How to do it...

How it works...

There's more...

See also

Fast matrix multiplication

Getting ready

How to do it...

How it works...

There's more...

See also

Implementing a custom pseudo-random number generator

Getting ready

How to do it...

How it works...

There's more...

See also

Parsing Git logs with regular expressions

Getting ready

How to do it...

How it works...

There's more...

See also

Non-standard ways to sort your data

Getting ready

How to do it...

How it works...

There's more...

See also

Creating a function preimage – understanding how dictionaries and sets work

Getting ready

How to do it...

How it works...

There's more...

See also

Working with UTF-8 strings

Getting ready

How to do it...

How it works...

There's more...

See also

Data Engineering in Julia

Introduction

Managing streams, and reading and writing files

Getting ready

How to do it...

Built-in system streams (stdin, stdout, stderr)

Reading and writing files

How it works...

There's more...

See also

Using IOBuffer to efficiently work with in-memory streams

Getting ready

How to do it...

How it works...

There's more...

See also

Fetching data from the internet

Getting ready

How to do it...

How it works...

There's more...

See also

Writing a simple RESTful service

Getting ready

How to do it...

Building a web service from scratch

Building a high-performance web service integrating ZeroMQ and JuliaWebAPI.jl

How it works...

There's more...

See also

Working with JSON data

Getting ready

How to do it...

How it works...

There's more...

See also

Working with date and time

Getting ready

How to do it...

How it works...

There's more...

See also

Using object serialization in Julia

Getting ready

How to do it...

Serializing Julia objects with Julia Base.Serialization

Serializing Julia objects with JLD2.jl

Serializing Julia objects with BSON.jl

How it works...

There's more...

See also

Running Julia as a background process

Getting ready

How to do it...

How it works...

There's more...

See also

Reading and writing Microsoft Excel files

Getting ready

How to do it...

Manipulating Excel files with PyCall.jl and openpyxl

Manipulating Excel files with XLSX.jl

How it works...

There's more...

See also

Handling Feather data

Getting ready

How to do it...

How it works...

There's more...

See also

Reading CSV and FWF files

Getting ready

How to do it...

How it works...

There's more...

See also

Numerical Computing with Julia

Introduction

Traversing matrices efficiently

Getting ready

How to do it...

How it works...

See also

Executing loops efficiently with conditional statements

Getting ready

How to do it...

How it works...

There's more...

See also

Generating full factorial designs

How to do it...

How it works...

See also

Approximating π using partial series sums

Getting ready

How to do it...

How it works...

There's more...

See also

Running Monte Carlo simulations

Getting ready

How to do it...

How it works...

There's more...

See also

Analyzing a queuing system

Getting ready

How to do it...

How it works...

There's more...

See also

Working with complex numbers

Getting ready

How to do it...

How it works...

There's more...

See also

Writing a simple optimization routine

Getting ready

How to do it...

How it works...

There's more...

See also

Estimating a linear regression

Getting ready

How to do it...

How it works...

There's more...

See also

Understanding broadcasting in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Improving code performance using @inbounds

Getting ready

How to do it...

How it works...

There's more...

See also

Creating a matrix from a set of vectors as rows

Getting ready

How to do it...

How it works...

There's more...

Using array views to avoid memory allocation

Getting ready

How to do it...

How it works...

There's more...

See also

Variables, Types, and Functions

Introduction

Understanding subtyping in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Using multiple dispatch to handle branching behavior

Getting ready

How to do it...

How it works...

There's more...

See also

Using functions as variables in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Functional programming in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Scope of variables in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Handling exceptions in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Working with NamedTuples

Getting ready

How to do it...

How it works...

There's more...

See also

Metaprogramming and Advanced Typing

Introduction

Metaprogramming

Getting ready

How to do it...

Working with the eval function

Abstract Syntax Tree (AST)

How it works...

There's more...

See also

Macros and generated functions

Getting ready

How to do it...

Using macros to cache function results

Loop unrolling with the @generated macro

How it works...

There's more...

See also

Defining your own types – linked list

Getting ready

How to do it...

How it works...

There's more...

See also

Defining primitive types

Getting ready

How to do it...

How it works...

There's more...

See also

Understanding the structure of Julia numeric types with introspection

Getting ready

How to do it...

How it works...

There's more...

See also

Using static arrays

Getting ready

How to do it...

How it works...

There's more...

See also

The efficiency of mutable versus immutable types

Getting ready

How to do it...

How it works...

There's more...

See also

Ensuring type stability of your code

Getting ready

How to do it...

How it works...

See also

Handling Analytical Data

Introduction

Converting data between DataFrame and Matrix

Getting ready

How to do it...

How it works...

There's more...

See also

Investigating the contents of a data frame

Getting ready

How to do it...

How it works...

There's more...

See also

Reading CSV data from the internet

Getting ready

How to do it...

How it works...

There's more...

See also

Working with categorical data

Getting ready

How to do it...

How it works...

There's more...

See also

Handling missing data

Getting ready

How to do it...

How it works...

There's more...

See also

Split-apply-combine in DataFrames

Getting ready

How to do it...

How it works...

There's more...

See also

Converting a data frame between wide and narrow formats

Getting ready

How to do it...

How it works...

There's more...

See also

Comparing data frames for identity

Getting ready

How to do it...

How it works...

There's more...

See also

Transforming rows of DataFrame

Getting ready

How to do it...

How it works...

There's more...

See also

Creating pivot tables by chaining transformations of data frames

Getting ready

How to do it...

How it works...

There's more...

See also

Julia Workflow

Introduction

Julia development workflow with Revise.jl

Getting ready

How to do it...

How it works...

There's more...

See also

Benchmarking code

Getting ready

How to do it...

How it works...

There's more...

See also

Profiling Julia code

Getting ready

How to do it...

How it works...

There's more...

See also

Setting up logging in your code

Getting ready

How to do it...

How it works...

There's more...

See also

Calling Python from Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Calling R from Julia

Getting ready

How to do it...

Option 1 – calling R libraries from Julia

Option 2 – directly calling R from Julia with variable interpolation

Option 3 – using the R command line available inside Julia with variable interpolation

Option 4 – sending variables with their values to R

How it works...

There's more...

See also

Managing project dependencies

Getting ready

How to do it...

How it works...

There's more...

See also

Data Science

Introduction

Working with databases in Julia

Getting ready

Getting ready for MySQL

Getting ready for PostgreSQL

Getting ready for JDBC and Oracle

Configuring JDBC in Julia

Getting Oracle JDBC drivers

How to do it...

Connecting to MySQL Server with MySQL.jl

Connecting to PostgreSQL with LibPQ.jl

Connecting to Oracle with JDBC.jl

How it works...

There's more...

See also

Optimization using JuMP

Getting ready

How to do it...

How it works...

There's more...

See also

Estimation using maximum likelihood

Getting ready

How to do it...

How it works...

There's more...

See also

Complex plotting with Plots.jl

Getting ready

How to do it...

How it works...

There's more...

See also

Building machine learning models with ScikitLearn.jl

Getting ready

How to do it...

How it works...

There's more...

See also

Distributed Computing

Introduction

Multiprocessing in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Sending parameters to remote Julia processes

Getting ready

How to do it...

How it works...

There's more...

See also

Multithreading in Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Distributed computing with Julia

Getting ready

How to do it...

How it works...

There's more...

See also

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

The Julia programming language, with its dynamic nature and high performance, reduces the time that needs to be taken for the development of computational models with easy-to-maintain computational code. Julia 1.0 Programming Cookbook will be your solution-based guide, and will take you through different programming aspects with Julia.

Starting with the new features of Julia 1.0, each recipe addresses a specific problem, along with a discussion that explains the solution and offers insight into how it works. You will work with the powerful Julia tools and data structures, along with the most popular Julia packages. You will learn how to create vectors, handle variables, and work with functions. You will be introduced to  various recipes for numerical computing, distributed computing, and achieving high performance. You'll see how to optimize data science programs with parallel computing and memory allocation. Moving forward, we will look into more advanced concepts, such as metaprogramming and functional programming. Finally, you will learn how to tackle issues while working with databases and data processing, and will learn about data science problems, data modeling, data analysis, data manipulation, parallel processing, and cloud computing with Julia.

By the end of the book, you will have the skills you need to work more effectively with your data.

Who this book is for

The target audience of this book is data scientists or programmers that want to improve their skills in working with the Julia programming language.

It is recommended that the user has a little experience with Julia or intermediate-level experience with other programming languages such as Python, R, or MATLAB.

What this book covers

Chapter 1, Installing and Setting Up Julia, introduces the use of the Julia command line and the setup of the entire Julia computational infrastructure, including building Julia, optimizing performance, and configuring Julia for the cloud. 

Chapter 2, Data Structures and Algorithms, contains practical examples of how custom algorithms can be implemented, while also taking advantage of the built-in functionality.

Chapter 3, Data Engineering in Julia, explains that working with data requires good understanding of streams and data sources. In this chapter, the reader will learn how to write data to IO streams with Julia and how to handle web transfers.

Chapter 4, Numerical Computing with Julia, contains recipes showing how computing tasks can be performed in the Julia language. Each recipe implements a relatively simple and standard algorithm to show a specific feature of the language. Therefore, the reader can concentrate on the implementation issues.

Chapter 5, Variables, Types, and Functions, presents topics related to variables and their scoping, Julia type systems and processing functions, and exceptions in Julia.

Chapter 6, Metaprogramming and Advanced Typing, presents various advanced programming topics in Julia.

Chapter 7, Handling Analytical Data, presents the DataFrames.jl package, providing a rich set of functionalities for working with them—manipulating rows and columns, handling categorical and missing data, and various standard transformations of tables (filtering, sorting, joins, wide-long transformation, and tabulation).

Chapter 8, Julia Workflow, explains the recommended workflow and shows how to build it using modules.

Chapter 9, Data Science, explains that Julia provides great support for various numerical and data science tasks. It allows us to define and optimize models in a very flexible solver-agnostic way. Julia also contains a huge toolbox for visualizing data and machine learning. 

Chapter 10, Distributed Computing, shows how to use Julia for parallel and distributed computing tasks. An important feature of Julia is the ability to scale up computations across many processes, threads, and up to distributed computational clusters.

Download the example code files

The example code files are organized in folders representing chapters and recipes. For each recipe, there is a commands.txt file that contains commands that should be typed-in by the reader. Every entry in this file is prepended by an appropriate prompt (example, $, julia>) to make sure that the user knows in which environment the command should be executed (typically the OS shell, the Julia command line). Most recipes also contain additional files, for example, source codes of Julia programs. A full list of files along with their contents is given in the Getting ready section of every recipe.

You can download the example code files for this book from your account at www.packt.com.If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

 

www.packt.com

.

Select the

 

SUPPORT

 

tab.

Click on

 

Code Downloads & Errata

.

Enter the name of the book in the

 

Search

 

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Julia-1.0-Programming-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it https://www.packtpub.com/sites/default/files/downloads/9781788998369_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, path names, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

html, body, #map { height: 100%; margin: 0; padding: 0}

For each block of code, we explain how it should be used (for example, maybe it should be pasted into a file or executed on the console).

Any command-line input is written in bold and the command-line output is written in normal font:

julia> collect(1:5)

5-element Array{Int64,1}: 1 2 3 4 5

julia> sin(1)

0.8414709848078965

julia>

Code that is executed in an OS shell (for Linux or Windows) is indicated with the $ sign (for Windows, this will be C:\). You should write commands that follow this sign (without the sign itself). For example, this command would give information about files in a current working directory:

$ ls

All single commands passed to the Julia command line are prepended with the julia> prompt marker. For example, this is a minimal Julia session:

$ julia --banner=no

julia> 1+2

3

julia> exit()

$

We have started Julia from the OS shell using the julia command. Then, we have entered 1+2 in the Julia command line and Julia printed 3. Finally, we have entered exit() in Julia to terminate the the Julia command-line session and go back to the shell (so we have the shell $ prompt in the last line of the output). Please note that for the aforementioned blocks of code, separate, instructions might be given that might also include copying and pasting them to the console.

In several recipes, we also discuss non-standard prompts in the Julia command line (for example, package manager mode or shell mode). They are explained in the relevant recipes.

All examples in this book have been tested on Linux Ubuntu 18.04 LTS and Windows 10. Please note that users of other Linux distributions will need to update their scripts (for example, Linux distributions from the Red Hat family use yum instead of apt). Most Linux-related commands should also work on macOS; however, they have not been tested on this OS.

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Installing and Setting Up Julia

In this chapter, we present recipes covering the following topics:

Installation issues:

How to install Julia in different environments

How to compile your own Julia binaries

How to use Julia in the cloud in

Amazon Web Services

(

AW

S

) using Cloud9

Basic usage of Julia:

The various ways you can customize the start-up of Julia

How to set up Julia to work with multiple cores

How to perform the standard steps comprising a daily workflow using the Julia command line (the Julia language shell also referred to as the

Julia REPL

)

How to display computational results in Julia

How to manage packages

More advanced configurations of Julia usage:

How to launch Julia in Jupyter Notebook

How to use Julia with JupyterLab

How to connect to Jupyter Notebook in Terminal-only cloud environments

Introduction

A key condition for successfully working with any programming language is the careful configuration of the development environment. Julia, being an open source language, offers integration with several popular tools. This means that developers have a number of alternatives at hand when setting up the complete Julia toolbox.

The first decision to be made is the choice of Julia distribution. Available options include binary and source code forms. For non-typical hardware configurations, or when one wishes to use all the latest compiler features, Julia source code can be downloaded. Another decision concerns which compiler to use in order to build Julia (GCC or Intel) and whether to link Julia against Intel's mathematical libraries.

The second decision lies in the choice of IDE. Julia can be integrated with various editors, with Atom plus the Juno plugin being the most popular choice. Yet another option is to use a browser-based Julia IDE—Jupyter Notebook, JupyterLab, or Cloud9.

In this chapter, we discuss all the preceding options and show how to set up the complete Julia programmer's environment, along with the most important technical tips and recommendations.

Installing Julia from binaries

The goal of this recipe is to present how to install and configure the Julia environment with the development toolbox. We show basic installation instructions for Linux and Windows.

Getting ready

In this recipe, we present how to install and configure Julia on Windows and Linux.

All Linux examples in this book have been tested on Linux Ubuntu 18.04.1 LTS and Windows 10. All Linux Ubuntu commands have been run as the user ubuntu. Please note that users of other Linux distributions will need to update their scripts (for example, Linux distributions from the Red Hat family use yum instead of apt).

For Windows examples in this book, we use Windows 10 Professional.

In the GitHub repository for this recipe you will find the commands.txt file that contains the presented sequence of shell commands.

How to do it...

For most users, the recommended way to start with Julia is to use a binary version.

In this section, we present the following options:

Installing Julia on Linux Ubuntu

Installing Julia on Windows

Installing Julia on Linux Ubuntu

Installing the binary release is the easiest way to proceed with Julia on Linux. Here, we show how to install Julia on Ubuntu, although the steps will be very similar for other Linux distributions.

Before the installation and use of Julia, we recommend installing a standard set of build tools for the Linux platform. Although this is not required for running Julia itself, several Julia package installers assume that the standard build tools set is present on the operating system. Hence, run the following commands in bash:

$ sudo apt update

$ sudo apt -y install build-essential

In order to install Julia, simply download the binary archive from julialang.org, uncompress it, and finally create a symbolic link named Julia. These steps are shown in the following three bash commands (we assume that these commands are run in the /home/ubuntu folder):

$ wget https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.0.1-linux-x86_64.tar.gz

$ tar xvfz julia-1.0.1-linux-x86_64.tar.gz

$ sudo ln -s /home/ubuntu/julia-1.0.1/bin/julia /usr/local/bin/julia

Please note that the last command creates a symbolic link to the Julia binaries. After this installation, it is sufficient to run the julia command in the OS shell to start working with Julia. 

Please note that on the Julia web page (https://julialang.org/downloads/), a newer version of the installer could be available. You can update the preceding script accordingly by simply updating the filename. Additionally, nightly build Julia versions are available on the preceding website. These versions are only recommended for testing new language features.

Installing Julia on Windows

The most convenient way to install Julia on Windows is by using the binary version installer available from the JuliaLang website.

The following steps are required to install Julia on a Windows system:

Download the

Windows Self-Extracting Archive (.exe)

from

https://julialang.org/downloads/

. It is recommended to select the

64-bit

version.

Run the downloaded

*.exe

file to unpack Julia. We recommend extracting Julia into a directory path that does not contain any spaces, for example,

C:\Julia-1.0.1

.

After a successful installation, a Julia shortcut will be added to your start menu—at this point, you can select the shortcut to see whether Julia loads correctly.

Add

julia.exe

to your system path, as follows:

Open

Windows Explorer

, right-click on the

This PC

computer

icon, and select

Properties

.

Click

Advanced system settings

and go to

Environment

 Variables...

.

Select the

Path

variable

and click

Edit...

.

To the

 v

ariable value,

add

C:\Julia-1.0.1\bin

(in this instruction, we assume that Julia has been installed to 

C:\Julia-1.0.1

). Please note that, depending on your Windows version, there is either one

Path

value per line or a semicolon 

;

 is used to separate values in the

Path

list.

Click

OK

to confirm. Now, Julia can be run anywhere from the console.

When adding julia.exe to your system path, please note that there are two variable groups on this screen, User variablesandSystem variables. We recommend using User variables. Please note that adding julia.exe to the system Path makes it possible for other tools, such as Juno, toautomatically locate Julia (Juno also allows for manual Julia path configuration—this can be found in the option marked Packages | Julia client | Settings).

For the most convenient workflow, we recommend installing the ConEmu terminal emulator for Windows users.  The steps to install ConEmu on your Windows system are as follows:Go to theConEmu website (http://conemu.github.io/)and click theDownloadlink. SelectDownload ConEmu Stable Installer.Once you download the *.exe file, run it to install ConEmu. Select the 64-bit version in the installer; other settings can keep their default values.Once ConEmu is installed, a new link in the start menu and on the desktop will be created.During the first application run, ConEmu asks about color settings—just stay with the defaults. Run ConEmu, type julia, and press Enter, and you should see a Julia prompt.

There's more...

Those users who want to try Julia without the installation process should try JuliaBox.

JuliaBox is a ready-made pre-installed Julia environment accessible from the web browser. It is available at https://juliabox.com/. You can use this version to play with a preconfigured and installed version of the Julia environment. Julia is available in a web browser via the Jupyter Notebook environment. The website is free to use, though it does require registration. JuliaBox comes with a set of pre-configured popular Julia libraries and is therefore ready for immediate use. This is an ideal solution for people wanting to try out the language or for using Julia inside a classroom.

See also

Excellent documentation on how to install Julia on various systems can be found on the JuliaLang website: https://julialang.org/downloads/platform.html.

Julia IDEs

Integrated Desktop Environments (IDEs) are integrated tools that provide a complete environment for software development and testing. IDEs provide visual support for the development process, including syntax highlighting, interactive code editing, and visual debugging.

Getting ready

Before installing an IDE, you should have Julia installed (either from binaries or source), following the instructions given in previous recipes.

In the GitHub repository for this recipe, you will find the SublimeText.txt file that contains configuration for Sublime Text described in this recipe. The configuration process of other IDEs described in this recipe is completely done with a point and click interface.

How to do it...

The three most popular Julia IDEs are Juno, Microsoft Visual Studio Code, and Sublime Text. In subsequent sections, we discuss the installation process for each particular IDE. 

Juno

Juno is the recommended IDE for Julia development.The Juno IDE is available athttp://junolab.org/. However, Juno runs as a plugin to Atom (https://atom.io/). Hence, in order to install Juno, you need to take the following steps:

Make sure that you have installed Julia and added it to the command path (following the instructions given in previous sections).

Download and install Atom, available at

https://atom.io/

.

Once the installation is complete, Atom will start automatically.

Press

Ctrl

+

,

(

Ctrl

 key +

comma

key)

to open the Atom settings screen.

Select the

Install

tab.

In the

Search packages

field, type

uber-juno

and press

Enter

.

You will see the

uber-juno

package developed by JunoLab—click

Install

to install the package.

In order to test your installation, click the

Show console

tab on the left.

Please note that when being run for the first time from Atom, Julia takes longer to start. This happens because Juno is installed along with several other packages that are being compiled before their first use. 

Microsoft Visual Studio Code

Note that at the time of publishing this book the Microsoft Visual Studio Code does not yet support Julia 1.0. However, since we believe that this support will be available very soon, we provide the instructions below.

The Microsoft Visual Studio Code editor can be obtained fromhttps://code.visualstudio.com/.Simply download the installer executable and install using the default settings. After launching Visual Studio Code, perform the following steps:

ClickExtensionstab (or press Ctrl + Shift + X).

In the search box, typejulia. You will see Julia Language Support on the list. Click the green Install button to start the installation.

Click File | New File to create a new, empty file.

Click File | Save As... to save the newly created file. In the Save As... type drop-down list, select Julia(please note that the file type list might not be sorted alphabetically and Julia type might be at the bottom of the list).

Open the Terminal tab and issue thejuliacommand.

After following these steps, you will have a Julia file open in the editor and an active Julia Terminal. PressingCtrl + Enter will now send the currently highlighted code line to the Terminal to execute it.

Sublime Text

Another option for the IDE is utilizing the functionality of Sublime Text:

If you are using Sublime Text, then add the package named

Julia

through

Package Control

.

Next, the simplest thing to add is a custom build system for Julia (

Tools

|

Build System

|

New Build System

):

{

"cmd": ["ConEmu64", "/cmd", "julia -i", "$file"],

"selector": "source.julia"

}

Now, you can execute an opened Julia script by pressing

Ctrl

B

 

in the console in interactive mode (

-i

switch).

The preceding example assumes thatConEmu64andjuliaare defined in the search path. 

The only inconvenience of this method is that if there is an error in the Julia script, the console will be immediately terminated (a cleaner way to test your scripts is to keep your Terminal with Julia open) and use theinclude command, as explained in the recipe Useful options for interaction with Julia in this chapter. 

See also

For integration with other editors and IDEs, take a look at the https://github.com/JuliaEditorSupport project.

Julia support for text editors

On some computational environments, no desktop is available and so users may want to use Julia in a text-only mode.

Getting ready

Before installing Julia support for text editors, you should have Julia preinstalled (either from binaries or source), in accordance with the instructions given in previous recipes.

In the GitHub repository for this recipe, you will find the commands.txt file that contains the presented sequence of shell commands.

How to do it...

The three most popular text editors used by Julia developers include Nano, Vim, and Emacs. Here, we provide some hints on how to configure Julia with these popular text-mode editors. All the following examples have been tested with Ubuntu 18.0.4.1 LTS.

Configuring Julia with Nano

Nano is a popular Linux text editor for beginners. By default, nano does not provide syntax highlighting for Julia. However, this can easily be remedied by adding appropriate lines to the .nanorc configuration file, which should be located in the user's home directory. The following commands will update the .nanorc file with the appropriate syntax coloring for Julia. Firstly, download syntax highlighting for Julia (https://stackoverflow.com/questions/35188420/syntax-highlighting-support-for-julia-in-nano):

$ wget -P ~/ https://raw.githubusercontent.com/Naereen/nanorc/master/julia.nanorc

Secondly, add highlighting to the nano configuration file, using the bash command, as follows:

$ echo include \"~/julia.nanorc\" >> ~/.nanorc

Configuring Julia with Vim

In order to configure Julia for Vim, you need to use the files available at the git://github.com/JuliaEditorSupport/julia-vim.git project. All you need to do for this is to copy them to the Vim configuration folder. On a Linux platform, this can be achieved by running the following commands:

git clone git://github.com/JuliaEditorSupport/julia-vim.git

mkdir -p ~/.vim

cp -R julia-vim/* ~/.vim

Once julia-vim is installed, one interesting feature is the support for LaTeX-style special characters. Try running vim file.jl and type \alpha, then press the Tab key. You will observe the text changing to the corresponding α character.

Further information and other useful options can be found on the julia-vim project website at git://github.com/JuliaEditorSupport/julia-vim.git.

Configuring Julia with Emacs

Since Emacs is not present by default in Ubuntu, the following instruction assumes that it has been installed by the sudo apt install emacs25 command. In order to configure Emacs support for Julia, you need to activate the julia-mode mode. This can be achieved with the following bash commands:

wget -P ~/julia-emacs/ https://raw.githubusercontent.com/JuliaEditorSupport/julia-emacs/master/julia-mode.el

echo "(

add-to-list

'load-path

\

"~/

julia-emacs

\

"

)" >> ~/.emacs echo "(

require

'julia-mode

)" >> ~/.emacs

See also

For integration with other editors and IDEs, take a look at the Julia Editor Support project, which is available at https://github.com/JuliaEditorSupport.

Building Julia from sources on Linux

Building Julia allows you to test the latest developments and includes bug fixes. Moreover, when Julia is compiled, it is optimized for the hardware that the compilation is performed on. Consequently, building Julia from source code is the recommended option for those production environments where performance is strongly affected by platform-specific features. These instructions will also be valuable for those Julia users who would like to check out and experiment with the latest source versions from the Julia Git repository. 

In the following examples, we show how to install and build a stable version of Julia 1.0.1.

Getting ready

All the following examples have been tested on Ubuntu 18.04.1 LTS.

Here is a list of steps to be followed:

Open the console and install all the prerequisites. Please refer to the following script (run each shell command shown as follows):

$ sudo

apt update

$ sudo

apt install

--yes

build-essential python-minimal gfortran m4 cmake pkg-config

libssl-dev

Download the source code (run each shell command shown as follows; we assume that the commands are run in your

home

folder):

$

git

clone

git

://github.com/JuliaLang/julia.git

$ cd

julia

$ git

checkout v1.0.1

In the GitHub repository for this recipe you will find the commands.txt file that contains the presented sequence of shell commands. 

How to do it...

In this section, we describe how to build Julia in three particular variations:

With open source mathematical libraries

With Intel's

Math Kernel Library

(

MKL

), but without Intel LIBM (Math Library)—this scenario requires registration on Intel's website

With Intel's Math Kernel Library (MKL) and with Intel LIBM (Math Library)—a commercial license from Intel is required

The libraries from Intel (MKL and LIBM) provide an implementation for a number of mathematical operations optimized for Intel's processor architecture. In particular, the Intel MKL library contains optimized routines for BLAS, LAPACK, ScaLAPACK, sparse solvers, fast Fourier transforms, and vector math (for more details see https://software.intel.com/en-us/mkl). On the other hand, the Intel LIBM library provides highly optimized scalar math functions that serve as direct replacements for the standard C calls—this includes optimized versions of standard math library functions, such as exp, log, sin, and cos). More information on Intel LIBM can be found at https://software.intel.com/en-us/articles/implement-the-libm-math-library.

Before running each of the recipes, please make sure that you are inside the folder where you ran the checkout command for Julia (see the Getting ready section).

Option 1 – build Julia without Intel's MKL

Once you have followed the steps in the Getting ready section and Julia has been checked out from the Git repository, perform the following steps:

In order to build Julia, simply run the following bash shell command:

$

make

-j

$((`nproc`-1))

1

>build_log.txt

2

>build_error.txt

The build logs will be available in the build_log.txt and build_error.txt files.

Once the Julia environment has been built, you can run the

./julia

command and use

versioninfo()

to check your installation:

julia> versioninfo()

Julia Version 1.0.1Commit 0d713926f8* (2018-09-29 19:05 UTC)Platform Info: OS: Linux (x86_64-linux-gnu) CPU: Intel(R) Xeon(R) Platinum 8124M CPU @ 3.00GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-6.0.0 (ORCJIT, skylake)

There's more...

Once Julia is installed on your system, it can be run by giving the full path to the Julia executable (for example,~/julia/julia). This is not always convenient—many users simply want to typejuliato get Julia running:

$ sudo

ln

-s

/home/ubuntu/julia/usr/bin/julia /usr/local/bin/julia

The preceding path assumes that you installed Julia as theubuntuuser in your home folder. If you have installed Julia to a different location, please update the path accordingly.

See also

If you want to build Julia in a supercomputing environment (for example, Cray), please follow the online tutorial written by one of the authors of this book, available at https://github.com/pszufe/Building_Julia_On_Cray_and_Clusters.

Running Julia inside the Cloud9 IDE in the AWS cloud

Cloud9 is an integrated programming environment that can be run inside a web browser. We will demonstrate how to configure this environment for programming with Julia. The web page of Cloud9 can be reached at https://aws.amazon.com/cloud9/.

Getting ready

In order to use Cloud9, you must have an active Amazon Web Services (AWS) account and log in to the AWS management console.

Cloud9 can create a new EC2 instance running Amazon Linux or can connect to any existing Linux server that allows SSH connections and has Node.js installed.In order to start working with Julia on Cloud9, complete the following steps:

Prepare a Linux machine with Julia installed (you can follow the instructions in the previous sections).

Install Node.js. In Ubuntu 18.04.1 LTS, for example, the only step needed is to run

sudo apt install nodejs

.

Make sure that your server is accepting external SSH connections. For an AWS EC2 instance, you need to configure the instance security group to accept SSH connections from

0.0.0.0/0

—in order to do this, click on the EC2 instance in the AWS console, select

Security Groups

 |

 

Inbound

 

|

 

Edit

,

and add a new rule that accepts all traffic.

In the GitHub repository for this recipe, you will find the JuliaRunner.run file that contains the configuration for the AWS Cloud9.

How to do it...

Once you have prepared a server with Julia and Node.js, you can take the following steps to use Cloud9:

In the AWS console, go to the Cloud9 service and create a new environment.

Select the

Connect and run in remote server (SSH)

option.

For the username, type

ubuntu

 if you use Ubuntu Linux, o

r

ec2-user

 

if you are running Amazon Linux, CentOS, or Red Hat (please note that this recipe has been tested with Ubuntu).

Provide the hostname (public DNS) of your EC2 instance.

Configure SSH authorization.

In the

Environment settings

screen, select

Copy key to clipboard

to copy the key.

Open an SSH connection to your remote server in a Terminal window.

Execute the 

nano ~/.ssh/authorized_keys

 command 

to edit the file.

Create an empty line and paste the public key content that you have just copied.

Press

Ctrl

+

X

and confirm the changes with

Y

to exit.

Now, you are ready to click the

Next step

button in the Cloud9 console. Cloud9 will connect to your server and automatically install all the required software. After a few minutes, you will see your Cloud9 IDE. By default, Cloud9 does not support running programs in Julia. 

Go to the 

Run

menu and select

Run with