# vector
# tuple
# dictionary
# structb
# namedtuplesFunctions
# matlab style
function fun(a)
r = a*(a+1)
return r # return values
endfun (generic function with 1 method)
fun(1)2
# one-liner
fun2(x) = x*(x+1)fun2 (generic function with 1 method)
fun2(1)2
# anonymous functions
fun3 = x->x*(x+1)
fun3(1)2
# if you use only once locally
map(
i->i^2,
range(start=1,stop=10)
)10-element Vector{Int64}:
1
4
9
16
25
36
49
64
81
100
# different types of arguemnetsfunction example1(pos1, pos2, pos3 )
pos1 + pos2 + pos3*2
end
example1(1,2,5)13
function example2(p1, p2, p3; alpha=1.0)
p1 + p2 + p3*alpha
# keyword arguements come after ;
endexample2 (generic function with 1 method)
example2(1,2,3) # you don't need to specify all arguements6.0
example2(1,2,3; alpha=2.0)9.0
# multiple dispatchf(x,y) = x+yf (generic function with 1 method)
# just-in-time compilation
# (decide what to do with the arguments)
f(1.0, 2.0)3.0
f(1,2)3
@code_llvm f(1.0, 2.0); Function Signature: f(Float64, Float64) ; @ In[39]:1 within `f` define double @julia_f_5263(double %"x::Float64", double %"y::Float64") #0 { top: ; ┌ @ float.jl:495 within `+` %0 = fadd double %"x::Float64", %"y::Float64" ret double %0 ; └ }
@code_llvm f(1, 2); Function Signature: f(Int64, Int64) ; @ In[33]:1 within `f` define i64 @julia_f_5044(i64 signext %"x::Int64", i64 signext %"y::Int64") #0 { top: ; ┌ @ int.jl:87 within `+` %0 = add i64 %"y::Int64", %"x::Int64" ret i64 %0 ; └ }
@code_native f(1, 2) .text
.file "f"
.section .ltext,"axl",@progbits
.globl julia_f_5097 # -- Begin function julia_f_5097
.p2align 4, 0x90
.type julia_f_5097,@function
julia_f_5097: # @julia_f_5097
; Function Signature: f(Int64, Int64)
; ┌ @ In[33]:1 within `f`
# %bb.0: # %top
#DEBUG_VALUE: f:x <- $rdi
#DEBUG_VALUE: f:y <- $rsi
push rbp
mov rbp, rsp
; │┌ @ int.jl:87 within `+`
lea rax, [rdi + rsi]
pop rbp
ret
.Lfunc_end0:
.size julia_f_5097, .Lfunc_end0-julia_f_5097
; └└
# -- End function
.section ".note.GNU-stack","",@progbits
# you can specify what to do with specific types
# -> multiple dispatchfunction g(x::Float64, y::Float64)
x+ y
endg (generic function with 4 methods)
g(x::Int,y::Int) = x*y
g(x::Real, y::Real) = x*y + 0.001
g(x::Int, y::Real) = x-yg (generic function with 4 methods)
g(Float32(1.0), Float32(2.0))2.001
g(Float64(1.0), Float64(2.0))3.0
methods(g)
# 4 methods for generic function g from [35mMain[39m:
- g(x::Float64, y::Float64) in Main at In[54]:1
- g(x::Int64, y::Int64) in Main at In[52]:1
- g(x::Int64, y::Real) in Main at In[52]:3
- g(x::Real, y::Real) in Main at In[52]:2
g(1,2)2
g(1.0, 2.0)2.001
typeof(20)Int64
factorial(25)OverflowError: 25 is too large to look up in the table; consider using `factorial(big(25))` instead Stacktrace: [1] factorial_lookup @ ./combinatorics.jl:26 [inlined] [2] factorial(n::Int64) @ Base ./combinatorics.jl:34 [3] top-level scope @ In[63]:1 [4] eval(m::Module, e::Any) @ Core ./boot.jl:489
factorial(big(2000))331627509245063324117539338057632403828111720810578039457193543706038077905600822400273230859732592255402352941225834109258084817415293796131386633526343688905634058556163940605117252571870647856393544045405243957467037674108722970434684158343752431580877533645127487995436859247408032408946561507233250652797655757179671536718689359056112815871601717232657156110004214012420433842573712700175883547796899921283528996665853405579854903657366350133386550401172012152635488038268152152246920995206031564418565480675946497051552288205234899995726450814065536678969532101467622671332026831552205194494461618239275204026529722631502574752048296064750927394165856283531779574482876314596450373991327334177263608852490093506621610144459709412707821313732563831572302019949914958316470942774473870327985549674298608839376326824152478834387469595829257740574539837501585815468136294217949972399813599481016556563876034227312912250384709872909626622461971076605931550201895135583165357871492290916779049702247094611937607785165110684432255905648736266530377384650390788049524600712549402614566072254136302754913671583406097831074945282217490781347709693241556111339828051358600690594619965257310741177081519922564516778571458056602185654760952377463016679422488444485798349801548032620829890965857381751888619376692828279888453584639896594213952984465291092009103710046149449915828588050761867924946385180879874512891408019340074625920057098729578599643650655895612410231018690556060308783629110505601245908998383410799367902052076858669183477906558544700148692656924631933337612428097420067172846361939249698628468719993450393889367270487127172734561700354867477509102955523953547941107421913301356819541091941462766417542161587625262858089801222443890248677182054959415751991701271767571787495861619665931878855141835782092601482071777331735396034304969082070589958701381980813035590160762908388574561288217698136182483576739218303118414719133986892842344000779246691209766731651433494437473235636572048844478331854941693030124531676232745367879322847473824485092283139952509732505979127031047683601481191102229253372697693823670057565612400290576043852852902937606479533458179666123839605262549107186663869354766108455046198102084050635827676526589492393249519685954171672419329530683673495544004586359838161043059449826627530605423580755894108278880427825951089880635410567917950974017780688782869810219010900148352061688883720250310665922068601483649830532782088263536558043605686781284169217133047141176312175895777122637584753123517230990549829210134687304205898014418063875382664169897704237759406280877253702265426530580862379301422675821187143502918637636340300173251818262076039747369595202642632364145446851113427202150458383851010136941313034856221916631623892632765815355011276307825059969158824533457435437863683173730673296589355199694458236873508830278657700879749889992343555566240682834763784685183844973648873952475103224222110561201295829657191368108693825475764118886879346725191246192151144738836269591643672490071653428228152661247800463922544945170363723627940757784542091048305461656190622174286981602973324046520201992813854882681951007282869701070737500927666487502174775372742351508748246720274170031581122805896178122160747437947510950620938556674581252518376682157712807861499255876132352950422346387878954850885764466136290394127665978044202092281337987115900896264878942413210454925003566670632909441579372986743421470507213588932019580723064781498429522595589012754823971773325722910325760929790733299545056388362640474650245080809469116072632087494143973000704111418595530278827357654819182002449697761111346318195282761590964189790958117338627206088910432945244978535147014112442143055486089639578378347325323595763291438925288393986256273242862775563140463830389168421633113445636309571965978466338551492316196335675355138403425804162919837822266909521770153175338730284610841886554138329171951332117895728541662084823682817932512931237521541926970269703299477643823386483008871530373405666383868294088487730721762268849023084934661194260180272613802108005078215741006054848201347859578102770707780655512772540501674332396066253216415004808772403047611929032210154385353138685538486425570790795341176519571188683739880683895792743749683498142923292196309777090143936843655333359307820181312993455024206044563340578606962471961505603394899523321800434359967256623927196435402872055475012079854331970674797313126813523653744085662263206768837585132782896252333284341812977624697079543436003492343159239674763638912115285406657783646213911247447051255226342701239527018127045491648045932248108858674600952306793175967755581011679940005249806303763141344412269037034987355799916009259248075052485541568266281760815446308305406677412630124441864204108373119093130001154470560277773724378067188899770851056727276781247198832857695844217588895160467868204810010047816462358220838532488134270834079868486632162720208823308727819085378845469131556021728873121907393965209260229101477527080930865364979858554010577450279289814603688431821508637246216967872282169347370599286277112447690920902988320166830170273420259765671709863311216349502171264426827119650264054228231759630874475301847194095524263411498469508073390080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
macros
dump(:(g(2,2)))Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol g
2: Int64 2
3: Int64 2
t1 = time()
g(2,2)
t2 = time()
print(t2-t1)0.00024580955505371094
expr = :(g(2,2)):(g(2, 2))
timed_expr = quote
t1 = time()
$expr
t2 = time()
print(t2-t1)
endquote
#= In[77]:2 =#
t1 = time()
#= In[77]:3 =#
g(2, 2)
#= In[77]:4 =#
t2 = time()
#= In[77]:5 =#
print(t2 - t1)
end
eval(expr)4
eval(timed_expr)5.9604644775390625e-6
# macros call sites with @
@time g(2,2) 0.000000 seconds
4
@macroexpand @time g(2,2)quote
#= timing.jl:353 =#
begin
#= timing.jl:358 =#
local var"#22#ret" = begin
#= timing.jl:682 =#
$(Expr(:meta, :force_compile))
#= timing.jl:683 =#
(Base.Threads).lock_profiling(true)
#= timing.jl:684 =#
local var"#29#lock_conflicts" = (Base.Threads).LOCK_CONFLICT_COUNT[]
#= timing.jl:685 =#
local var"#25#stats" = Base.gc_num()
#= timing.jl:686 =#
local var"#27#elapsedtime" = Base.time_ns()
#= timing.jl:687 =#
Base.cumulative_compile_timing(true)
#= timing.jl:688 =#
local var"#28#compile_elapsedtimes" = Base.cumulative_compile_time_ns()
#= timing.jl:689 =#
local var"#26#val" = $(Expr(:tryfinally, :(g(2, 2)), quote
var"#27#elapsedtime" = Base.time_ns() - var"#27#elapsedtime"
#= timing.jl:691 =#
Base.cumulative_compile_timing(false)
#= timing.jl:692 =#
var"#28#compile_elapsedtimes" = Base.cumulative_compile_time_ns() .- var"#28#compile_elapsedtimes"
#= timing.jl:693 =#
var"#29#lock_conflicts" = (Base.Threads).LOCK_CONFLICT_COUNT[] - var"#29#lock_conflicts"
#= timing.jl:694 =#
(Base.Threads).lock_profiling(false)
end))
#= timing.jl:696 =#
local var"#30#diff" = Base.GC_Diff(Base.gc_num(), var"#25#stats")
#= timing.jl:697 =#
(value = var"#26#val", time = var"#27#elapsedtime" / 1.0e9, bytes = (var"#30#diff").allocd, gctime = (var"#30#diff").total_time / 1.0e9, gcstats = var"#30#diff", lock_conflicts = var"#29#lock_conflicts", compile_time = var"#28#compile_elapsedtimes"[1] / 1.0e9, recompile_time = var"#28#compile_elapsedtimes"[2] / 1.0e9)
end
#= timing.jl:359 =#
local var"#23#_msg" = Base.nothing
#= timing.jl:360 =#
local var"#24#_msg_str" = if var"#23#_msg" === Base.nothing
var"#23#_msg"
else
Base.string(var"#23#_msg")
end
#= timing.jl:361 =#
Base.time_print(Base.stdout, (var"#22#ret").time * 1.0e9, (var"#22#ret").gcstats.allocd, (var"#22#ret").gcstats.total_time, Base.gc_alloc_count((var"#22#ret").gcstats), (var"#22#ret").lock_conflicts, (var"#22#ret").compile_time * 1.0e9, (var"#22#ret").recompile_time * 1.0e9, true; msg = var"#24#_msg_str")
#= timing.jl:362 =#
(var"#22#ret").value
end
end
import Pkg
Pkg.add("BenchmarkTools")Updating registry at `~/.julia/registries/General.toml` Resolving package versions... Installed Xorg_libpciaccess_jll ─ v0.18.1+0 Installed libva_jll ───────────── v2.23.0+0 Installed libdrm_jll ──────────── v2.4.125+1 Installed BenchmarkTools ──────── v1.6.3 Installing 3 artifacts Installed artifact Xorg_libpciaccess 24.8 KiB Installed artifact libva 235.7 KiB Installed artifact libdrm 368.0 KiB Updating `~/Teaching/econobits/Project.toml` [6e4b80f9] + BenchmarkTools v1.6.3 Updating `~/Teaching/econobits/Manifest.toml` [6e4b80f9] + BenchmarkTools v1.6.3 [34da2185] + Compat v4.18.1 [a65dc6b1] + Xorg_libpciaccess_jll v0.18.1+0 [8e53e030] + libdrm_jll v2.4.125+1 [9a156e7d] + libva_jll v2.23.0+0 [9abbd945] + Profile v1.11.0 Precompiling packages... 451.2 ms ✓ Xorg_libpciaccess_jll 407.3 ms ✓ libdrm_jll 832.1 ms ✓ BenchmarkTools 535.4 ms ✓ libva_jll 929.5 ms ✓ GR → IJuliaExt 1674.3 ms ✓ Plots → IJuliaExt 6 dependencies successfully precompiled in 4 seconds. 192 already precompiled.
using BenchmarkTools@benchmark g(2,2)BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample. Range (min … max): 1.271 ns … 15.013 ns ┊ GC (min … max): 0.00% … 0.00% Time (median): 1.419 ns ┊ GC (median): 0.00% Time (mean ± σ): 1.461 ns ± 0.507 ns ┊ GC (mean ± σ): 0.00% ± 0.00% █▃ ▁▃ ▅▆▄▃▃▂▁▂▂██▆▄▄▂▂▂▁██▆▃▃▅▅▄▃▂▃▇▆▄▃▃▄▄▄▃▃▂▆▆▅▃▃▃▄▄▃▂▂▂▂▆▅▄▃▂ ▃ 1.27 ns Histogram: frequency by time 1.65 ns < Memory estimate: 0 bytes, allocs estimate: 0.
## loop
# for x in collection
# whilefor i in 1:10
println(i)
end1
2
3
4
5
6
7
8
9
10
for i=1:10 #matlab style
println(i)
end1
2
3
4
5
6
7
8
9
10
for i ∈ 1:10 #math style
println(i)
end1
2
3
4
5
6
7
8
9
10
undefUndefInitializer(): array initializer with undefined values
Vector{Float64}(undef, 50)50-element Vector{Float64}:
6.9320070182932e-310
6.93236037815376e-310
6.932007018294e-310
6.93236037815376e-310
6.9320070182948e-310
6.93236037815376e-310
6.9320070182956e-310
6.93236037815376e-310
6.9320070182964e-310
6.93236037815376e-310
6.9320070182972e-310
6.93236037815376e-310
6.93200701829797e-310
⋮
6.93200701830587e-310
6.93236037815376e-310
6.9323487775098e-310
6.93236037815376e-310
6.9320070183525e-310
6.93236037815376e-310
6.93200701830824e-310
6.93236037815376e-310
6.93200701832247e-310
6.93236037815376e-310
2.0e-323
2.0e-323
Matrix and broadcasting
v1 = [1,2,4] # vector type is deduced from the elements3-element Vector{Int64}:
1
2
4
v1[1] # indexing starts at 1
v1[1] = 2.3InexactError: InexactError: Int64(2.3)
InexactError: Int64(2.3)
Stacktrace:
[1] Int64
@ ./float.jl:923 [inlined]
[2] convert
@ ./number.jl:7 [inlined]
[3] setindex!(A::Vector{Int64}, x::Float64, i::Int64)
@ Base ./array.jl:985
[4] top-level scope
@ ~/Teaching/econobits/scpo/corrections/jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_Y103sZmlsZQ==.jl:2
v1 = [1,2.0,4] # vector type is deduced from the elements
v1[1] = 32.232.2
["you", "me", 1.23]3-element Vector{Any}:
"you"
"me"
1.23
# empty vector can have a type
[]
Float64[]Float64[]
# matrices have the same memory properties as vectors
zeros(4,3)4×3 Matrix{Float64}:
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
reshape([(1:9)...], 3,3) # create a matrix from vector data3×3 Matrix{Int64}:
1 4 7
2 5 8
3 6 9
M = [ 1 2 3 ; 4 5 6 ; 7 8 9 ] # separate rows with ; and columns with space3×3 Matrix{Int64}:
1 2 3
4 5 6
7 8 9
M*M # matrix multiplication3×3 Matrix{Int64}:
30 36 42
66 81 96
102 126 150
# M^0.3 = # (exp(u) = sum_i u^i/i!) exp(0.3 log(M))
M^2 # M*M
M^0.33×3 Matrix{ComplexF64}:
0.696207+0.603221im 0.435824+0.163635im 0.175461-0.275923im
0.632511+0.0665825im 0.730874+0.0180839im 0.829198-0.0304701im
0.568835-0.470028im 1.02588-0.127523im 1.48295+0.21501im
# . for elementwise operations
M .* M3×3 Matrix{Int64}:
1 4 9
16 25 36
49 64 81
#
M .^ 0.23×3 Matrix{Float64}:
1.0 1.1487 1.24573
1.31951 1.37973 1.43097
1.47577 1.51572 1.55185
fib(n) = n <= 2 ? 1 : fib(n-1) + fib(n-2)
fib.(M)3×3 Matrix{Int64}:
1 1 2
3 5 8
13 21 34