Scientific Computing, TU Berlin, WS 2019/2020, Lecture 02
Jürgen Fuhrmann, WIAS Berlin
Use the Cheat Sheet to see a compact and rather comprehensive list of basic things Julia. This notebook tries to discuss some concepts behind Julia.
Start REPL by calling julia
in terminal
julia>
prompt. Type backspace in other modes to enter default mode. help?>
prompt. Type ?
to enter help mode. Search via ?search_term
shell>
prompt. Type ;
to enter shell mode. Pkg>
prompt. Type ]
to enter package mode. quit()
or Ctrl+D
: exit Julia. Ctrl+C
: interrupt execution. Ctrl+L
: clear screen. ;
to suppress displaying output from a command include("filename.jl")
: source a Julia code file. Package.jl
, e.g. AbstractTrees Use package manager: itself a package installed by default
using Pkg
Add package AbstractTrees
:
Pkg.add("AbstractTrees")
List installed packages
Pkg.status()
Test package AbstractTrees
:
Pkg.test("AbstractTrees")
Remove package AbstractTrees
:
Pkg.rm("AbstractTrees")
See Cheat Sheet for more
.julia/packages
subdirectory of your home folder .julia
on inconsistencies and re-install packages (aka "reinstall windows"...) Integers
i=1
typeof(i)
Floating point numbers
y=1.0
typeof(y)
Rational numbers
r=3//7
Unicode variable names: type \pi<tab>
@show pi
println(1.0*pi)
typeof(pi)
Vector creation by explicit list of elements
v1=[1,2,3,4,]
Type of a vector element
@show eltype(v1);
If on element in the initializer is float, the vector becomes float
v2=[1.0,2,3,4,]
@show v2[2]
Create integer vector of zeros
v3=zeros(Int,4)
Create float vector of zeros
v3=zeros(Float64,4)
Fill vector with constant data
fill!(v3,10)
See Cheat Sheet for more
Ranges describe sequences of numbers and can be used in loops, array constructors etc.
r1=1:10
@show r1
@show typeof(r1)
r2=1:2:10
@show r2
@show typeof(r2)
Create vector from range
collect(r1)
collect(1:2:5)
collect(1:0.1:2)
Create vector from list comprehension containing range
v=[sin(i) for i=1:5]
v=collect(1:2:100);
Size of a vector is a tuple with one element
@show size(v);
Length is the overall number of elemnts in a vector
@show length(v);
Subarrays are copies of parts of arrays
v=collect(1:2:10)
@show v;
Subvector for indices 2 to 4 contains a copy of data of v
vsub=v[2:4]
@show vsub;
Changing elements in vsub does not affect v
vsub[1]=100
@show vsub
@show v;
Array views allow to access of a part of an array.
v=collect(1:2:10)
@show v;
Subvector for indices 2 to 4 contains a copy of data of v
vview=view(v,2:4)
@show vview;
Changing elements in vview also changes v
vview[1]=100
@show vview
@show v;
@views macro
v=collect(1:2:10)
@show v;
Subvector for indices 2 to 4 contains a copy of data of v
@views vview=v[2:4]
@show vview;
Changing elements in vview also changes v
vview[1]=1000
@show vview;
@show v;
Element-wise operations on arrays
v=collect(0:0.1:1)
@show sin.(v)
@show 2 .*v;
m1=zeros(4,3)
m2=Matrix{Float64}(undef, 5, 3)
fill!(m2,17)
Matrix from list comprehension
m3=[cos(x)*exp(y) for x=0:0.1:10, y=-1:0.1:1]
size: tuple of dimensions
@show size(m3);
length: number of elements
@show length(m3);
Random vector (normal distribution)
u=randn(5)
v=randn(5);
Mean square norm
using LinearAlgebra
@show norm(u)
@show norm(v);
Dot product
@show dot(u,v);
Random matrix (normal distribution)
m=randn(5,5)
@show m;
Matrix vector multiplication
@show m*u;
Trace
@show tr(m);
Determinant
@show det(m);
See Cheat Sheet for more
Conditional execution
condition1=false
condition2=true
if condition1
println("cond1")
elseif condition2
println("cond2")
else
println("nothing")
end
for loop
for i in 1:10
println(i)
end
Nested for loop
for i in 1:10
for j in 1:5
println(i * j)
end
end
Same as
for i in 1:10, j in 1:5
println(i * j)
end
Preliminary exit of loop
for i in 1:10
println(i)
if i==3
break # skip remaining loop
end
end
Preliminary exit of iteration
for i in 1:10
if i==5
continue # skip to next iteration
end
println(i)
end
Structure of function definition
function func(req1, req2; key1=dflt1, key2=dflt2)
# do stuff
return out1, out2, out3
end
Function definition
function func0(x; y=0)
println(x+2*y)
end
func0(1)
func0(1,y=1000);
Assignment
f=func0
f(1);
One line function definition
sin2(x)=sin(2*x)
@show sin(5)
@show sin2(5);
Nested function definition
function outerfunction(n)
function innerfunction(i)
println(i)
end
for i=1:n
innerfunction(i)
end
end
outerfunction(13);
Functions as parameters of other function
function ff(f0)
f0(5)
end
fx(x)=sin(x)
@show ff(fx);
Anonymous functions
@show ff(x->sin(x));
Dot syntax can be used to make any function work on vectors
x=collect(0:0.1:1)
myf(x)=sin(x)*exp(-x)
@show myf.(x);
map: apply function to each element of a collection (e.g. matrix, vector)
map(x->sin(x^2), collect(0:0.1:1))
Equivalent:
map(collect(0:0.1:1)) do x
return sin(x^2)
end
mapreduce: apply function to each element of a collection and apply reduction
println(mapreduce(x->sin(x^2),*, collect(0:0.1:1)))
println(mapreduce(x->sin(x^2),+, collect(0:0.1:1)))
sum: apply function to each element of a collection and add up
sum(x->sin(x^2), collect(0:0.1:1))
This notebook was generated using Literate.jl.