Riječnik

Odaberite jednu od ključnih riječi s lijeve strane ...

Programming in JuliaIntroduction

Vrijeme čitanja: ~25 min

In subsequent Data Gymnasia courses, we will develop mathematical ideas in concert with corresponding computational skills. This relationship is symbiotic: writing programs is an important ingredient for applying mathematical ideas to real-world problems, but it also helps us explore and visualize math ideas in ways that go beyond what we could achieve with pen, paper, and imagination.

We will use Julia for most of our work in those courses. This is a relatively new entrant to the scientific computing scene, having been introduced publicly in 2012 and reaching its first stable release in August of 2018. Julia is ideally suited to the purposes of this course:

  • Julia is designed for scientific computing. The way that code is written in Julia is influenced heavily by its primary intended application as a scientific computing environment. This means that our code will be succinct and will often look very similar to the corresponding math notation.
  • Julia has benefits as an instructional language. Julia provides tools for inspecting how numbers and other data structures are stored internally, and it also makes it easy to see how the built-in functions work.
  • Julia is simple yet fast. Hand-coded algorithms are generally much faster in Julia than in other user-friendly languages like Python or R. This is not always important in practice, because you can usually use fast code written by other people for the most performance-sensitive parts of your program. But when you're learning fundamental ideas, it's very helpful to be able to write out simple algorithms by hand and examine their behavior on large or small inputs. It can also be helpful in real-world applications where packaged code doesn't fit your problem especially well.
  • Julia is integrated in the broader ecosystem. Julia has excellent tools for interfacing with other languages like C, C++, Python, and R, so can take advantage of the mountain of scientific computing resources developed over the last several decades. (Conversely, if you're working in a Python or R environment in the future, you can write some Julia code and call it from your Python or R program.)

An important note on Julia's speed: because of the way Julia works, functions take longer to run the first time they are called. This means that loading a large package (or calling a function that calls many other functions for the first time) can be quite . The developers are working to mitigate this issue, but it's challenging for reasons which are closely related to the design choices that make Julia fast.

This course contains many exercises. Doing them in earnest is essential for knowledge and skill retention. You should solve each exercise prior to clicking the "Continue" button to see an example solution.

Installation

There are several ways to access Julia:

Inline. This course will let you execute Julia code blocks in the webpage (thanks to Juniper and Binder). So if you don't want to install anything yet, you don't have to. (However, the first cell you run will be slow with this method, like up to 30 seconds, since your environment has to be launched behind the scenes on Binder's servers. If it's taking too long, reload the page.)

Binder. You can also run Julia code in a notebook on the Binder website. To launch with a set of packages tailored to this course, click here. Then, select New (top right corner and Julia). It is highly recommended that you keep a tab with a Binder notebook open while working through this course, because it can serve as a space for scratch work, and it provides more features than the blocks which appear in-page.

Locally. Download Julia for your system here. You want to get the standard version (under "Current stable release"), not JuliaPro, Conda, or any other distribution.

CoCalc. If you want a complete environment without having to install anything locally, CoCalc is a batteries-included, community-oriented platform for open-source mathematical and scientific computing. You can use it for free with limited functionality, and it's $14 per month to support the project and get paid account features.

Some important tips for getting help as you learn:

  • Julia's official documentation is available at https://docs.julialang.org and is excellent. The learning experience you will get in this course is intended to get you up and running quickly, but you can always look at the corresponding section the documentation to learn more details.
  • You can get help within a Julia session by typing a question mark before the name of a function whose documentation you want to see.
  • Similarly, apropos("eigenvalue") returns a list of functions whose documentation mentions "eigenvalue"

Usage

Once you have Julia installed, there are several ways to interact with it.

REPL. Launch a read-eval-print loop from the command line. Any code you enter will be executed immediately, and any values returned by your code will be displayed. To start a session, open your operating system's Terminal and run julia. You can do this in Binder by selecting New > Terminal.

Script. Save a file called example.jl and run julia example.jl from the command line (in the same directory as the file) to execute all the code in the script. You can do this in Binder by selecting New > Text File and then changing the name of the text file to something that ends in .jl.

Jupyter. Like a REPL, but allows inserting text and math expressions, grouping code into blocks, etc. This is the interface provided by default in Binder, and you can launch a notebook locally by running jupyter notebook from the command line (assuming you have Anaconda installed).

Integrated development environment (IDE). Essential for extensive software development projects, an IDE provides an editor for writing code, conveniences to help you code more efficiently, and a debugger to help you fix your mistakes. The main IDE for Julia is Juno.

Exercise.

  • If you just wanted to check how Julia evaluates 8÷2(2+2), the quickest way to do that would probably be to use .

  • If you want to create a single document with text, code, and images, is well suited to that.

  • If you work on an extensive project with thousands of lines of code, you should and .

Bruno
Bruno Bruno