In [1]:
## Arithmetic
1+1
Out[1]:
2
In [2]:
## What's different?
1.0 + 1.0
Out[2]:
2.0
In [3]:
## The typeof to see what it is
typeof(1+1)
Out[3]:
Int64
In [4]:
## A different type
typeof(1.0+1.0)
Out[4]:
Float64
In [5]:
## We can enter matrices
A  = [1 2; 3 4]
Out[5]:
2×2 Array{Int64,2}:
 1  2
 3  4
In [6]:
## And these are auto-typed
B = [1.0 2; 3 4]
Out[6]:
2×2 Array{Float64,2}:
 1.0  2.0
 3.0  4.0
In [7]:
## Automatic promotion
C = A*B
Out[7]:
2×2 Array{Float64,2}:
  7.0  10.0
 15.0  22.0
In [8]:
## More automatic promotion
1+1.0
Out[8]:
2.0
In [9]:
## What about complex numbers?
sqrt(-1.0)
DomainError:
sqrt will only return a complex result if called with a complex argument. Try sqrt(complex(x)).

Stacktrace:
 [1] sqrt(::Float64) at ./math.jl:425
 [2] include_string(::String, ::String) at ./loading.jl:515
In [10]:
## If you want them, you have to ask
sqrt(-1.0+0i)
UndefVarError: i not defined

Stacktrace:
 [1] include_string(::String, ::String) at ./loading.jl:515
In [11]:
## Can't use j either ...
sqrt(-1.0+0j)
UndefVarError: j not defined

Stacktrace:
 [1] include_string(::String, ::String) at ./loading.jl:515
In [12]:
## "im" means imaginary
sqrt(-1.0+0im)
Out[12]:
0.0 + 1.0im
In [13]:
## Lots of handy stuff for linear algebra
eig([1 1; 1 2])
Out[13]:
([0.381966, 2.61803], [-0.850651 0.525731; 0.525731 0.850651])
In [14]:
##
lam, V = eig([1 1; 1 2])
@show lam
@show V
lam = [0.381966, 2.61803]
V = [-0.850651 0.525731; 0.525731 0.850651]
Out[14]:
2×2 Array{Float64,2}:
 -0.850651  0.525731
  0.525731  0.850651
In [15]:
## Singular value decomposition
svd([1 1; 1 2])
Out[15]:
([-0.525731 -0.850651; -0.850651 0.525731], [2.61803, 0.381966], [-0.525731 -0.850651; -0.850651 0.525731])
In [16]:
## If you want to index, you need to use []
C[1,2]
Out[16]:
10.0
In [17]:
## You can't use floating point
C[1.0,2]
ArgumentError: invalid index: 1.0

Stacktrace:
 [1] getindex(::Array{Float64,2}, ::Float64, ::Int64) at ./abstractarray.jl:882
 [2] include_string(::String, ::String) at ./loading.jl:515
In [18]:
## Which is sometimes a problem
C[2/1,2]
ArgumentError: invalid index: 2.0

Stacktrace:
 [1] getindex(::Array{Float64,2}, ::Float64, ::Int64) at ./abstractarray.jl:882
 [2] include_string(::String, ::String) at ./loading.jl:515
In [19]:
## So there are a few ways to fix
@show C[round(Int, 2/1), 2] # round to an integer
@show C[ceil(Int, 2/1), 2] # ceil to an integer
@show C[floor(Int, 2/1)], 2) #
@show C[2 ÷ 1, 2] # fancy division sign, enter in Atom with \div<TAB>
@show C[div(2,1), 2] # same thing without the fancy-ness "div" means integer division
C[round(Int, 2 / 1), 2] = 22.0
In [20]:
## Vectors can be compliated
x = [1 2 3 4]
Out[20]:
1×4 Array{Int64,2}:
 1  2  3  4
In [21]:
## Eek, that was a row-vector
x = [1;2;3;4]
Out[21]:
4-element Array{Int64,1}:
 1
 2
 3
 4
In [22]:
## What is this one?
x = [1,2,3,4]
Out[22]:
4-element Array{Int64,1}:
 1
 2
 3
 4
In [23]:
## I always get confused if I hae to use "," or ";" so don't worry
In [24]:
##
x = [1 2 3 4; zeros(1,4)]
Out[24]:
2×4 Array{Float64,2}:
 1.0  2.0  3.0  4.0
 0.0  0.0  0.0  0.0
In [25]:
##
x = [1,2,3,4; zeros(1,4)]
# this is an error :(
syntax: unexpected semicolon in array expression

Stacktrace:
 [1] include_string(::String, ::String) at ./loading.jl:515
In [26]:
## Make the vector again
x = [1,2,3,4]
Out[26]:
4-element Array{Int64,1}:
 1
 2
 3
 4
In [27]:
## Vectors can have a single index
x[2]
Out[27]:
2
In [28]:
## Or two indices because they are _column_ vectors!
x[2,1]
Out[28]:
2
In [29]:
##
x[1,2]
BoundsError: attempt to access 4-element Array{Int64,1} at index [1, 2]

Stacktrace:
 [1] getindex(::Array{Int64,1}, ::Int64, ::Int64) at ./array.jl:521
 [2] include_string(::String, ::String) at ./loading.jl:515
In [30]:
## We can do matrix-vector products
ones(4,4)*x
Out[30]:
4-element Array{Float64,1}:
 10.0
 10.0
 10.0
 10.0
In [31]:
## But we can't multiply them against each other!
x*x
DimensionMismatch("Cannot multiply two vectors")

Stacktrace:
 [1] *(::Array{Int64,1}, ::Array{Int64,1}) at ./linalg/rowvector.jl:184
 [2] include_string(::String, ::String) at ./loading.jl:515
In [32]:
## Element-wise
x.*x
Out[32]:
4-element Array{Int64,1}:
  1
  4
  9
 16
In [33]:
## Inner-products
x'*x
Out[33]:
30
In [34]:
## Outer-products
x*x'
Out[34]:
4×4 Array{Int64,2}:
 1  2   3   4
 2  4   6   8
 3  6   9  12
 4  8  12  16
In [35]:
## How do you see the entire output?
@show x'*rand(4,4)
x' * rand(4, 4) = [5.4292 3.98808 4.91807 6.78601]
Out[35]:
1×4 RowVector{Float64,Array{Float64,1}}:
 5.4292  3.98808  4.91807  6.78601
In [36]:
## If you want full precision
foreach(println, x'*rand(4,4))
3.0108787583167933
3.1355942677058204
3.7622907270169264
2.992257829562376
In [37]:
##
foreach(println, eye(4))
1.0
0.0
0.0
0.0
0.0
1.0
0.0
0.0
0.0
0.0
1.0
0.0
0.0
0.0
0.0
1.0
In [38]:
## Okay, it seems I was wrong earlier! You can do this in Julia
x+5
Out[38]:
4-element Array{Int64,1}:
 6
 7
 8
 9
In [39]:
##
x.+5
Out[39]:
4-element Array{Int64,1}:
 6
 7
 8
 9
In [40]:
## Loop variables are removed
for i=1:10
  @show i
end
@show i # this gives an error because i was only valid inside the loop
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
UndefVarError: i not defined

Stacktrace:
 [1] include_string(::String, ::String) at ./loading.jl:515
In [41]:
## You can create functions whenever
function add5(x)
  return x+5
end

function add6(x)
  x+6
end

add7(x) = x+7 # shorthand that avoids functions

add8 = x -> x + 8
Out[41]:
(::#5) (generic function with 1 method)
In [42]:
## Plotting requires some setup, just once
using Plots
pyplot()
Out[42]:
Plots.PyPlotBackend()
In [43]:
##
plot(1:100)
Accessibility encountered a DBus error: QDBusError("org.a11y.Bus.Error", "Failed to execute child process \"/bin/dbus-daemon\" (No such file or directory)")
Out[43]:
In [44]:
## A bigger plot

# setup plot data
k = 1:60

# Linear
seq = 0.85.^k
plot(k, seq, lab="Linear", yaxis=:log10, color="black", linewidth=2)

# Arithmetic
plot!(k, 1./k, lab="Arithmetic", yaxis=:log10, color="green", linewidth=2)

seq = 1./(k.^2)
plot!(k, seq, lab="Arithmetic2", yaxis=:log10, color="green", linewidth=2)

# Superlinear
seq = (2*k).^(-0.05*k)
plot!(k, seq, lab="Superlinear", yaxis=:log10, color="red", linewidth=2)

# Quadratic
# overflow for k = 60
t = 1:18
seq = (0.99).^(2.^t)
plot!(t, seq, lab="Quadratic", yaxis=(:log10, (1e-8, 1)), color="magenta", linewidth=2)

# Cubic
t = 1:12
seq = (0.99).^(3.^t)
plot!(t, seq, lab="Cubic", yaxis=(:log10, (1e-8, 1)), color="yellow", linewidth=2)
Out[44]:
In [45]:
##
gui()
In [46]:
##
plot(1:100)
title!("Test")
gui() # This shows a window if you are in Atom.