In [1]:
x = 3
typeof(x)
Out[1]:
In [2]:
y = 3.0
typeof(y)
Out[2]:
In [3]:
z = 1. + 3.im
typeof(z)
Out[3]:
In [4]:
w = true
typeof(w)
Out[4]:
In [5]:
# Strings are double-quoted
s = "This is a string"
typeof(s)
Out[5]:
In [6]:
# floating point division
5 / 2
Out[6]:
In [ ]:
# integer division
div(5, 2)
In [ ]:
# power
5^2
In [7]:
# booleans
!true || 1 == 1
Out[7]:
In [8]:
# Comparisons can be chained
1 != 2 < 3 < 4
Out[8]:
In [ ]:
println("x = ", x) # appends a new line; print() does not.
In [9]:
# string interpolation
name = "julia"
"Hi, I'm $name"
Out[9]:
In [10]:
# more complex expressions are wrapped in parentheses
"The sine of $x is $(sin(x))"
Out[10]:
In [ ]:
# formatted printing
@printf "%d is less than %4.2f" 4.5 5.3
In [28]:
# if/else
x = 2
if x < 3
println("x is less then 3")
end
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
In [12]:
# for loops
total = 0
for i = 1:10
total += i
end
println("Sum is $total")
In [13]:
# iterate over array
for item in ["hi", "bye"]
println(item)
end
In [14]:
# standard syntax
function f(x)
return 2x^2 + 3x + 1
end
f(3)
Out[14]:
In [15]:
# one-line syntax
f(x) = 2x^2 + 3x + 1
g(x) = f(x) - (2x+1) * (x+1)
g(3.5)
Out[15]:
In [16]:
# fibonacci sequence in one line
fib(n) = n < 2? n : fib(n-2) + fib(n-1)
fib(20)
Out[16]:
In [17]:
# Homogeneous array
a = [3, 4, 5]
Out[17]:
In [18]:
typeof(a)
Out[18]:
In [19]:
# Array data is stored contiguously
sizeof(a) # 3 8-byte ints
Out[19]:
In [22]:
# Array of Heterogeneous types (similar to Python list)
b = [3., "hello", 2]
sizeof(b)
Out[22]:
In [21]:
# Explicitly set the type
Int64[3., 4, 5]
Out[21]:
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.
In [23]:
# 1-based indexing. It will be OK.
l = [3, 4, 5]
l[1]
Out[23]:
In [24]:
# ranges are inclusive at both ends
l[1:2]
Out[24]:
In [25]:
# end must be explicit
l[2:end]
Out[25]:
In [26]:
# indexing from the end
l[1:end-1]
Out[26]:
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)
In [27]:
a = [1.1, 2.2, 3.3]
b = [4.4, 5.5, 6.6]
a + b
Out[27]:
In [28]:
3.5 * a
Out[28]:
By default, operations are matrix operations, not element-wise!
In [29]:
# matrix multiply doesn't work on two vectors:
a * b
In [30]:
# it works if we transpose a:
println(a')
a' * b
Out[30]:
In [31]:
# Instead, there are explicit element-wise operators
a .* b
Out[31]:
In [32]:
# You can do array-wise operations:
a.^2 .+ b.^2
Out[32]:
In [33]:
squares = [i^2 for i in 1:2:10]
Out[33]:
In [34]:
sums = [i+j for i=1:5, j=1:5]
Out[34]:
In [35]:
# You can create a dictionary using a literal
d = ["one"=>1, "two"=>2]
Out[35]:
In [36]:
# look up
d["one"]
Out[36]:
In [ ]:
# assignment
d["three"] = 3
d