Julia syntax

A few primitive datatypes


In [1]:
x = 3
typeof(x)


Out[1]:
Int64

In [2]:
y = 3.0
typeof(y)


Out[2]:
Float64

In [3]:
z = 1. + 3.im
typeof(z)


Out[3]:
Complex{Float64} (constructor with 1 method)

In [4]:
w = true
typeof(w)


Out[4]:
Bool

In [5]:
# Strings are double-quoted
s = "This is a string"
typeof(s)


Out[5]:
ASCIIString (constructor with 2 methods)

Select Mathematical operators


In [6]:
# floating point division
5 / 2


Out[6]:
2.5

In [ ]:
# integer division
div(5, 2)

In [ ]:
# power
5^2

In [7]:
# booleans
!true || 1 == 1


Out[7]:
true

In [8]:
# Comparisons can be chained
1 != 2 < 3 < 4


Out[8]:
true

Printing


In [ ]:
println("x = ", x)  # appends a new line; print() does not.

In [9]:
# string interpolation
name = "julia"
"Hi, I'm $name"


Out[9]:
"Hi, I'm julia"

In [10]:
# more complex expressions are wrapped in parentheses
"The sine of $x is $(sin(x))"


Out[10]:
"The sine of 3 is 0.1411200080598672"

In [ ]:
# formatted printing
@printf "%d is less than %4.2f" 4.5 5.3

Control flow


In [28]:
# if/else
x = 2
if x < 3
    println("x is less then 3")
end


x less then 3

In [ ]:
# C-style ternary if-else
a = 3
a == 3 ? println("It's true!") : println("It's not true")

In [11]:
# while loops
i = 0
while i < 5
    println("$i")
    i += 1
end


0
1
2
3
4

In [12]:
# for loops
total = 0
for i = 1:10
    total += i
end
println("Sum is $total")


Sum is 55

In [13]:
# iterate over array
for item in ["hi", "bye"]
    println(item)
end


hi
bye

Functions


In [14]:
# standard syntax
function f(x)
    return 2x^2 + 3x + 1
end

f(3)


Out[14]:
28

In [15]:
# one-line syntax
f(x) = 2x^2 + 3x + 1
g(x) = f(x) - (2x+1) * (x+1)

g(3.5)


Out[15]:
0.0

In [16]:
# fibonacci sequence in one line
fib(n) = n < 2? n : fib(n-2) + fib(n-1)

fib(20)


Out[16]:
6765

Arrays


In [17]:
# Homogeneous array
a = [3, 4, 5]


Out[17]:
3-element Array{Int64,1}:
 3
 4
 5

In [18]:
typeof(a)


Out[18]:
Array{Int64,1}

In [19]:
# Array data is stored contiguously
sizeof(a)  # 3 8-byte ints


Out[19]:
24

In [22]:
# Array of Heterogeneous types (similar to Python list)
b = [3., "hello", 2]
sizeof(b)


Out[22]:
24

In [21]:
# Explicitly set the type
Int64[3., 4, 5]


Out[21]:
3-element Array{Int64,1}:
 3
 4
 5

Multi-dimensional arrays

Square brackets with commas gives a one-dimensional vector. This is printed in a way that treats it as if it were a column vector (although there is in fact no difference between a one-dimensional row vector and column vector).


In [ ]:
[1, 2, 3]

To create explicit matrices, Matlab-style notation is used. If we omit the commas, something different happens: we now obtain a two-dimensional Array, i.e. a matrix, of size $1 \times n$. [Recall that in the standard notation for matrices, an $m \times n$ matrix has $m$ rows and $n$ columns.]


In [ ]:
[1 2 3]

In [ ]:
# Semicolon can be used to write 2-d array literals
a = [1 2 3;
     4 5 6]

In [ ]:
# Python-like syntax gives you something different!
a = [[1, 2, 3], [4, 5, 6]]

In [ ]:
# Lots of functions for creating arrays...
ones((2, 3))
zeros((2, 3))
trues((2, 3))
falses((2, 3))
Array(Float64, (2, 3))  # uninitialized
# ... and many more.

Array Indexing


In [23]:
# 1-based indexing. It will be OK.
l = [3, 4, 5]
l[1]


Out[23]:
3

In [24]:
# ranges are inclusive at both ends
l[1:2]


Out[24]:
2-element Array{Int64,1}:
 3
 4

In [25]:
# end must be explicit
l[2:end]


Out[25]:
2-element Array{Int64,1}:
 4
 5

In [26]:
# indexing from the end
l[1:end-1]


Out[26]:
2-element Array{Int64,1}:
 3
 4

In [ ]:
# append an element
push!(l, 6)

Ending a function name with "!" is a convention to tell the user that the funciton modifies its input(s).


In [ ]:
# types are converted automatically if possible & exact
l = [3, 4, 5]
push!(l, 12.0)

Array operations

Arrays work as mathematical vectors, with the sum of two vectors and scalar multiplication being defined:


In [27]:
a = [1.1, 2.2, 3.3]
b = [4.4, 5.5, 6.6]

a + b


Out[27]:
3-element Array{Float64,1}:
 5.5
 7.7
 9.9

In [28]:
3.5 * a


Out[28]:
3-element Array{Float64,1}:
  3.85
  7.7 
 11.55

By default, operations are matrix operations, not element-wise!


In [29]:
# matrix multiply doesn't work on two vectors:
a * b


`*` has no method matching *(::Array{Float64,1}, ::Array{Float64,1})
while loading In[29], in expression starting on line 2

In [30]:
# it works if we transpose a:
println(a')
a' * b


[1.1 2.2 3.3]
Out[30]:
1-element Array{Float64,1}:
 38.72

In [31]:
# Instead, there are explicit element-wise operators
a .* b


Out[31]:
3-element Array{Float64,1}:
  4.84
 12.1 
 21.78

In [32]:
# You can do array-wise operations:
a.^2 .+ b.^2


Out[32]:
3-element Array{Float64,1}:
 20.57
 35.09
 54.45

Array comprehensions

There is an equivalent of list comprehensions in Python, as follows. Note that the array construction syntax is quite flexible.


In [33]:
squares = [i^2 for i in 1:2:10]


Out[33]:
5-element Array{Int64,1}:
  1
  9
 25
 49
 81

In [34]:
sums = [i+j for i=1:5, j=1:5]


Out[34]:
5x5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

Dictionaries


In [35]:
# You can create a dictionary using a literal
d = ["one"=>1, "two"=>2]


Out[35]:
Dict{ASCIIString,Int64} with 2 entries:
  "two" => 2
  "one" => 1

In [36]:
# look up
d["one"]


Out[36]:
1

In [ ]:
# assignment
d["three"] = 3
d