In this tutorial, we will familiarize ourselves with using
Julia, both on the command line and in Jupyter. Before we begin, please run the following command in the terminal:
cp -r /nb/m3976/.local ~/
If you open a terminal, you can run julia by typing
This opens what's called a REPL prompt: a Read-Evaluate-Print loop, where Julia waits for you to type a command and hit enter.
Try typing some commands at the Julia REPL prompt, such as:
The lab matchines default to 32 bit integers, so your output will be different. Can you predict what the output of the following will be before hitting return?
multiple lines can be input on the same line using
;. For example, the following assigns the value of 2 to
x and then prints out
; also has the feature of surpressing the output. What do you think the output of the following is:
Julia can also run a script that is in a file. At a Julia prompt type
to go back to the unix terminal. We can create a simple script. Type
emacs myfirstscript.jl &
to create a Julia script file named
myfirstscript.jl and edit it. In the
emacs window, type
Save the file by typing
We can now run the script by evaluating:
from the terminal.
We can also create files that define Julia functions, that can be loaded from the REPL. To see this, type
emacs myfirstfunction.jl &
in the terminal and type the following to create a function called
sq, just like in lecture:
function sq(x) x^2 end
Ctrl-X Ctrl-S) the file.
Nothing happens if we type
in the terminal. Why is this?
to open up the Julia REPL. Typing
in Julia is the same as evaluating the contents of the file on the command line prompt. We can now use
sq, by typing
sq(5) # returns 25
Excercise 1 Edit
myfirstfunction.jl to add a function
sqbits(x), that returns the bits of
myfirstfunction.jl in a new REPL, and check that
Excercise 2 Create a new function that sets the 10th bit of an Int32 to 1, and returns an Int32, assuming that the input is a positive integer. Two strings can be concatenated with
"hi"*"bye" # returns the string "hibye"
The string consisting of the first nine characters can be found using
str is any string:
str="hibye0123445556" str[1:9] # returns "hibye0123"
The string consisting of the 11th through last character can be found using
str="hibye0123445556" str[11:end] # returns "45556"
converts from a string in Base-2 to an
Excercise 3 (advanced) Modify the previous function to also work with negative numbers. Recall that
will reinterpret the bits of number
x which is an unsigned integer
UInt32 as if it were a signed integer
The interface we will use mostly is the Jupyter notebook. Julia support is provided by the Julia package
To open up a Jupyter notebook, type the following:
using IJulia notebook()
This will open up a new firefox window showing the contents of the current directory. If you click on
New in the top right and select
Julia 0.4.3, a new window will open that let's you type in Julia commands.
Try typing in some of the commands at the start of the notebook. You can create new boxes by clicking on the
+ in the toolbar, or hitting
Ctrl-m b. Cells can be deleted by clicking the scissors in the tool bar, or
This tutorial guide is itself a Jupyter notebook. To download, click the image that looks like a down arrow. A cryptic bunch of text should come up. Hit
Ctrl-S and save the file in your home directory as "01.ipynb"
You can now open this tutorial from the Jupyter notebook website. Click on
jupyter in the top left from the Jupyter window to return to the directory window, and click on "01.ipynb". Try evaluating some of the boxes above. Scroll down to here in the file.
using PyPlot # Loads the PyPlot Package
x=0.:.01:1. # Represents a range of x values, with step size 0.01 y=cos(10*x) # returns [cos(10*0.),cos(10*0.01),cos(10*0.02),cos(10*0.03),…,cos(10*1.)] plot(x,y) # plots cos(10*x) between 0 and 1;
Multiple plots can be combined. Note that the variables x and y are saved.
We can plot points by specifying linestyle='.':
Excercise 4 Plot the digits of a random integer in base-10. Hint: the function
rand(Int) creates a random integer, and
digits(x) returns a vector of the digits.
Excercise 5 Plot the digits of 5 random integers in base-10, using a for-loop. Hint: remember the syntax
for k=1:5 # do stuff end