In [1]:
# Ctrl + ENTER
versioninfo()
Julia Version 1.5.1
Commit 697e782ab8 (2020-08-25 20:08 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:
  JULIA_DEPOT_PATH = c:\JuliaPkg\Julia1.5.1
In [2]:
pwd()
Out[2]:
"C:\\AAABIBLIOTEKA\\dsa"
In [3]:
]st
Status `C:\JuliaPkg\Julia1.5.1\environments\v1.5\Project.toml`
  [6e4b80f9] BenchmarkTools v0.5.0
  [336ed68f] CSV v0.7.7
  [9961bab8] Cbc v0.7.0
  [5ae59095] Colors v0.12.4
  [861a8166] Combinatorics v1.0.2
  [8f4d0f93] Conda v1.4.1
  [a93c6f00] DataFrames v0.21.7
  [31c24e10] Distributions v0.23.9
  [35a29f4d] DocumenterTools v0.1.7
  [cf35fbd7] GeoInterface v0.5.4
  [2e9cd046] Gurobi v0.8.1
  [cd3eb016] HTTP v0.8.17
  [7073ff75] IJulia v1.21.3
  [6a3955dd] ImageFiltering v0.6.15
  [82e4d734] ImageIO v0.3.0
  [6218d12a] ImageMagick v1.1.6
  [916415d5] Images v0.22.4
  [682c06a0] JSON v0.21.0
  [0f8b85d8] JSON3 v1.1.1
  [4076af6c] JuMP v0.21.3
  [b964fa9f] LaTeXStrings v1.1.0
  [093fc24a] LightGraphs v1.3.3
  [b8f27783] MathOptInterface v0.9.14
  [c03570c3] Memoize v0.4.3
  [86cd37e6] OpenStreetMapX v0.2.1
  [d3d4fdd0] OpenStreetMapXPlot v0.1.2
  [f57f5aa1] PNGFiles v0.3.1
  [eadc2687] Pandas v1.4.0
  [d96e819e] Parameters v0.12.1
  [91a5bcdd] Plots v1.6.0
  [c3e4b0f8] Pluto v0.11.12
  [7f904dfe] PlutoUI v0.6.1
  [438e738f] PyCall v1.91.4
  [d330b81b] PyPlot v2.9.0
  [6f49c342] RCall v0.13.7
  [b0e4dd01] RollingFunctions v0.6.2
  [8e980c4a] Shapefile v0.6.2
  [aa4a32ff] SimpleHypergraphs v0.1.12
  [2913bbd2] StatsBase v0.33.0
  [737fac7d] TravelingSalesmanExact v0.3.5
  [8c8f4381] TravelingSalesmanHeuristics v0.3.3
  [bc48ee85] Tullio v0.2.5
In [4]:
;ls
01_Basics.ipynb
02_Vectors_Matrices.ipynb
03_DataFrames.ipynb
04_Plotting.ipynb
05_Pandemic_Sim.ipynb
mokotow.osm
torontoF.osm
In [5]:
function f(x,y)
    x+y
end
f(x,y) = x+y
Out[5]:
f (generic function with 1 method)
In [6]:
@code_lowered f(5,6)
Out[6]:
CodeInfo(
1 ─ %1 = x + y
└──      return %1
)
In [7]:
@code_llvm f(5,6)
;  @ In[5]:4 within `f'
; Function Attrs: uwtable
define i64 @julia_f_1602(i64, i64) #0 {
top:
; ┌ @ int.jl:86 within `+'
   %2 = add i64 %1, %0
; └
  ret i64 %2
}
In [8]:
@code_native f(5,6)
	.text
; ┌ @ In[5]:4 within `f'
	pushq	%rbp
	movq	%rsp, %rbp
; │┌ @ int.jl:86 within `+'
	leaq	(%rcx,%rdx), %rax
; │└
	popq	%rbp
	retq
	nopw	(%rax,%rax)
; └
In [9]:
v = [1,2,3,4]
Out[9]:
4-element Array{Int64,1}:
 1
 2
 3
 4
In [10]:
b = Vector{Int}()
Out[10]:
Int64[]
In [11]:
c = Complex(1,4)
Out[11]:
1 + 4im
In [12]:
typeof(c)
Out[12]:
Complex{Int64}
In [13]:
c2=Complex{Float64}(1,4)
Out[13]:
1.0 + 4.0im
In [14]:
typeof(c2)
Out[14]:
Complex{Float64}

Data types

In [15]:
parse(Float64, "3.7")
Out[15]:
3.7
In [16]:
one(Float64), zero(Int)
Out[16]:
(1.0, 0)
In [17]:
@show Int64('a') 	# character to integer
@show Int64(2.0) 	# float to integer
#Int64(1.3) 		# inexact error
#Int64("a") 		# error no conversion possible
@show Float64(1) 	# integer to float
@show Bool(1) 		# boolean true
@show Bool(0) 		# boolean false
@show Char(89) 		# integer to char
@show zero(10.0) 	# zero of  arg type (Float64)
@show one(Int64) 	# one of type Int64
@show convert(Int64, 1.0)	# float to integer
@show parse(Int64, "1")
Int64('a') = 97
Int64(2.0) = 2
Float64(1) = 1.0
Bool(1) = true
Bool(0) = false
Char(89) = 'Y'
zero(10.0) = 0.0
one(Int64) = 1
convert(Int64, 1.0) = 1
parse(Int64, "1") = 1
Out[17]:
1
In [18]:
@show Int <: Any
@show Int <: Union{Int, Float64}
@show typeof(nothing)
Int <: Any = true
Int <: Union{Int, Float64} = true
typeof(nothing) = Nothing
Out[18]:
Nothing

Tuples

In [19]:
@show () 		# empty tuple
@show (1,) 		# one element tuple
@show ("a", 1) 	# two element tuple
@show ('a', false)::Tuple{Char, Bool} # tuple type assertion
@show x = (1, 2, 3)
@show x[1] 		# first element
@show x[1:2] 	# (1, 2) (tuple)
# x[4] 		# bounds error
# x[1] = 1 	# error - tuple is not mutable
@show a, b = x 	# tuple unpacking a==1, b==2
() = ()
(1,) = (1,)
("a", 1) = ("a", 1)
('a', false)::Tuple{Char, Bool} = ('a', false)
x = (1, 2, 3) = (1, 2, 3)
x[1] = 1
x[1:2] = (1, 2)
(a, b) = x = (1, 2, 3)
Out[19]:
(1, 2, 3)
In [20]:
k = (1, "k")
typeof(k)
Out[20]:
Tuple{Int64,String}

Matrices

In [21]:
Array{Char}(undef, 2, 3, 4) 	# 2x3x4 array of Chars
@show Array{Any}(undef, 2, 3) 	# 2x3 array of Any
@show zeros(5) 				# vector of Float64 zeros
@show ones(Int64, 2, 1) 	# 2x1 array of Int64 ones
@show trues(3), falses(3) 	# tuple of vector of trues and of falses

@show x = range(1, stop=2, length=5)  # iterator having 5 equally spaced elements
@show collect(x) 	# converts iterator to vector
@show 1:10 			# iterable from 1 to 10
@show 1:2:10 		# iterable from 1 to 9 with 2 skip
@show reshape(1:12, 3, 4)	# 3x4 array filled with 1:12 values
Array{Any}(undef, 2, 3) = Any[#undef #undef #undef; #undef #undef #undef]
zeros(5) = [0.0, 0.0, 0.0, 0.0, 0.0]
ones(Int64, 2, 1) = [1; 1]
(trues(3), falses(3)) = (Bool[1, 1, 1], Bool[0, 0, 0])
x = range(1, stop = 2, length = 5) = 1.0:0.25:2.0
collect(x) = [1.0, 1.25, 1.5, 1.75, 2.0]
1:10 = 1:10
1:2:10 = 1:2:9
reshape(1:12, 3, 4) = [1 4 7 10; 2 5 8 11; 3 6 9 12]
Out[21]:
3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64:
 1  4  7  10
 2  5  8  11
 3  6  9  12
In [22]:
Array{Int}(undef, 2, 3)
Out[22]:
2×3 Array{Int64,2}:
 839202992  839203472  839901072
 839203184  839203568  335514464
In [23]:
m = zeros(4,5)
m[1,3] = 66
m
Out[23]:
4×5 Array{Float64,2}:
 0.0  0.0  66.0  0.0  0.0
 0.0  0.0   0.0  0.0  0.0
 0.0  0.0   0.0  0.0  0.0
 0.0  0.0   0.0  0.0  0.0
In [24]:
m[:,3] #array slicing
Out[24]:
4-element Array{Float64,1}:
 66.0
  0.0
  0.0
  0.0
In [25]:
a = reshape(1:12, 3, 4)
Out[25]:
3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64:
 1  4  7  10
 2  5  8  11
 3  6  9  12
In [26]:
a = reshape(1:12, 3, 4)
display(a[:, 1:2]) # 3x2 matrix
display(a[:, 1]) # 3 element vector
display(a[1, :]) # 4 element vector
3×2 Array{Int64,2}:
 1  4
 2  5
 3  6
3-element Array{Int64,1}:
 1
 2
 3
4-element Array{Int64,1}:
  1
  4
  7
 10

Data structures

In [27]:
mutable struct Point
  x::Int64
  y::Float64
  meta
end
p = Point(0, 0.0, "Origin")
@show p
@show p.x 		# access field
p.meta = 2		# change field value
@show fieldnames(typeof(p)) # get names of instance fields
@show fieldnames(Point); 	# get names of type fields
p = Point(0, 0.0, "Origin")
p.x = 0
fieldnames(typeof(p)) = (:x, :y, :meta)
fieldnames(Point) = (:x, :y, :meta)
In [28]:
fieldnames(typeof(p))
Out[28]:
(:x, :y, :meta)
In [29]:
using Parameters
@with_kw mutable struct Point
  x::Int64 = 1
  y::Float64 = 2.0
  meta
end
p2 = Point(meta = "Origin")
p2
Out[29]:
Point
  x: Int64 1
  y: Float64 2.0
  meta: String "Origin"

Dictionaries

In [30]:
x = Dict{Int, Float64}() 
x[5] = 66.6
x[7] = 99

x
Out[30]:
Dict{Int64,Float64} with 2 entries:
  7 => 99.0
  5 => 66.6
In [31]:
y = Dict(1=>5.5, 2=>4.5)
Out[31]:
Dict{Int64,Float64} with 2 entries:
  2 => 4.5
  1 => 5.5
In [32]:
x = Dict{Int, Float64}() # empty dictionary mapping integers to floats
y = Dict(1=>5.5, 2=>4.5) 	# created dictionary
@show y[2] 					# get element
@show y[3] = 30.0 			# add element
@show haskey(y, 2) 			# check if key exists
@show keys(y), values(y) # iterators returning dictionary keys and values
@show delete!(y, 2)   # removing elements see also: pop!
@show get(y, 2, 777) # returns y[2] or 777 if not haskey(y,2)
y[2] = 4.5
y[3] = 30.0 = 30.0
haskey(y, 2) = true
(keys(y), values(y)) = ([2, 3, 1], [4.5, 30.0, 5.5])
delete!(y, 2) = Dict(3 => 30.0,1 => 5.5)
get(y, 2, 777) = 777
Out[32]:
777

Text processing

In [33]:
@show "Hi " * "there!" 		# concatenation
@show "Ho " ^ 3 			# multiplication
@show string("a= ", 123.3) 	# joining elements to text
@show occursin("CD","ABCD") 	# occurence
@show "\"\n\t\$" # C-like escaping, additionally escape \$ 
                          # escaping - similiar to Matlab, Python, R
x = 123
@show "$x + 3 = $(x+3)"  # $ is an interpolation operator
@show "\$199" # that is why you need escaping

r = r"A|B" # full support for regular expressions
@show occursin(r, "CD") # false, not found
@show m = match(r, "ACBD"); # first regexp match
"Hi " * "there!" = "Hi there!"
"Ho " ^ 3 = "Ho Ho Ho "
string("a= ", 123.3) = "a= 123.3"
occursin("CD", "ABCD") = true
"\"\n\t\$" = "\"\n\t\$"
"$(x) + 3 = $(x + 3)" = "123 + 3 = 126"
"\$199" = "\$199"
occursin(r, "CD") = false
m = match(r, "ACBD") = RegexMatch("A")
In [34]:
a = "sss"
b = " ddd "
a * b

x = 2+4

tekst = "hello $x and $(x*x+2x)"
Out[34]:
"hello 6 and 48"

Functions

In [35]:
f(x, y = 10) = x + y 
# for this function the default value of y is 10
@show f(3, 2) # 5 is returned
@show f(3)    # 13 is returned
function g(x::Int, y::Int) # limit arg type
  return y, x # tuple returned 
end

@show g(x::Int, y::Bool) = x * y # multiple dispatch
@show g(2, true) # 2nd definition will be called
@show methods(g); # list of methods for g
f(3, 2) = 5
f(3) = 13
g(x::Int, y::Bool) = begin
        #= In[35]:9 =#
        x * y
    end = g
g(2, true) = 2
methods(g) = # 2 methods for generic function "g":
[1] g(x::Int64, y::Bool) in Main at In[35]:9
[2] g(x::Int64, y::Int64) in Main at In[35]:5
In [36]:
@show g(1,4)
@show g(1,true)
methods(g)
g(1, 4) = (4, 1)
g(1, true) = 1
Out[36]:
# 2 methods for generic function g:
  • g(x::Int64, y::Bool) in Main at In[35]:9
  • g(x::Int64, y::Int64) in Main at In[35]:5

Operators

In [37]:
@show true || false # binary or operator (singeltons only)
@show 1 < 2 < 3     # condition chaining  is OK 
@show [1 2] .< [2 1] # vectorization via dot operator "."
@show a = 5
@show 2a + 2(a+1) # multiplication can be ommited 
@show x = [1 2 3]	  #matrix 1×3 Array{Int64,2}    
@show y = [1, 2, 3]  #matrix 3-elements Array{Int64,1}
# vectors are vecticar and are different concept than horizontal 1-row matrix
#x + y # error
@show x .+ y # 3x3 matrix, dimension broadcasting
@show x + y' # 1x3 matrix
@show x * y; # array multiplication, 1-element vector (not scalar)
true || false = true
1 < 2 < 3 = true
[1 2] .< [2 1] = Bool[1 0]
a = 5 = 5
2a + 2 * (a + 1) = 22
x = [1 2 3] = [1 2 3]
y = [1, 2, 3] = [1, 2, 3]
x .+ y = [2 3 4; 3 4 5; 4 5 6]
x + y' = [2 4 6]
x * y = [14]

Simple demo

In [38]:
function calc_pi(n, T)
    # n is the number of replications
    # T is the result type
    s = one(T)
    f = one(T)
    for i::T in 1:n
        f *= i/(2i+1)
        s += f
    end
    2s
end
Out[38]:
calc_pi (generic function with 1 method)
In [39]:
π
typeof(π)
Out[39]:
Irrational{:π}
In [40]:
π
Out[40]:
π = 3.1415926535897...
In [41]:
for T in [Float16, Float64, BigFloat]
    display([calc_pi(2^n, T) for n in 1:10] .- big(π))
end
10-element Array{BigFloat,1}:
 -0.207998903589793238462643383279502884197169399375105820974944592307816406286198
 -0.04393640358979323846264338327950288419716939937510582097494459230781640628619803
 -0.002920778589793238462643383279502884197169399375105820974944592307816406286198029
 -0.0009676535897932384626433832795028841971693993751058209749445923078164062861980295
 -0.0009676535897932384626433832795028841971693993751058209749445923078164062861980295
 -0.0009676535897932384626433832795028841971693993751058209749445923078164062861980295
 -0.0009676535897932384626433832795028841971693993751058209749445923078164062861980295
 -0.0009676535897932384626433832795028841971693993751058209749445923078164062861980295
 -0.0009676535897932384626433832795028841971693993751058209749445923078164062861980295
 -0.0009676535897932384626433832795028841971693993751058209749445923078164062861980295
10-element Array{BigFloat,1}:
 -0.208259320256460112370941313308723763275080776328230820974944592307816406286198
 -0.04317995517709517539637050332880069379799947750010582097494459230781640628619803
 -0.002122972943642620687196138639994458007746791953230820974944592307816406286198029
 -6.257552732991451957385031566516208582973593855820974944592307816406286198029454e-06
 -7.004720171274999130895783071511373531260582097494459230781640628619802945362503e-11
 -1.010643099614860550061511927700156355820974944592307816406286198029453625031821e-15
 -1.010643099614860550061511927700156355820974944592307816406286198029453625031821e-15
 -1.010643099614860550061511927700156355820974944592307816406286198029453625031821e-15
 -1.010643099614860550061511927700156355820974944592307816406286198029453625031821e-15
 -1.010643099614860550061511927700156355820974944592307816406286198029453625031821e-15
10-element Array{BigFloat,1}:
 -0.2082593202564599051293100499461695508638360660417724876416112589744830729528555
 -0.04317995517709482576423068486680447149875670096240740827653189389511799358779893
 -0.002122972943642004076114879103939885104758542258366351294298441073429877782059472
 -6.257552731792070508295743788487240468397453131043771934107556099005139032410049e-06
 -7.004625201779280778523170847860187316653933480300725911129361546127247477203004e-11
 -1.176247251749589002199551915966763734093321056007856229508136919374480975152605e-20
 -4.555467456395812571863354028257417095625126183776878405020450217779608346301807e-40
  1.036340226611333355046362223536047948533920043732353766202844416420231016379491e-76
  1.036340226611333355046362223536047948533920043732353766202844416420231016379491e-76
  1.036340226611333355046362223536047948533920043732353766202844416420231016379491e-76
In [42]:
calc_pi(10000, BigFloat)- π
Out[42]:
1.036340226611333355046362223536047948533920043732353766202844416420231016379491e-76
In [43]:
setprecision(1000) do
    calc_pi(1000, BigFloat)-π
end
Out[43]:
3.73305447401287551596035817889526867846836578548683209848685735918386764390310253781776130839152440943837995972129697049686195008541612957936608326881572302493764266455330060109598030394360732604440196318506045247296205005918373516322071308450166041524279351541770592447787925691464383688807065164177119e-301
In [44]:
setprecision(8000) do
    @show BigFloat(π)
    end;
BigFloat(π) = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485863278865936153381827968230301952035301852968995773622599413891249721775283479131515574857242454150695950829533116861727855889075098381754637464939319255060400927701671139009848824012858361603563707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104752162056966024058038150193511253382430035587640247496473263914199272604269922796782354781636009341721641219924586315030286182974555706749838505494588586926995690927210797509302955321165344987202755960236480665499119881834797753566369807426542527862551818417574672890977772793800081647060016145249192173217214772350141441973568548161361157352552133475741849468438523323907394143334547762416862518983569485562099219222184272550254256887671790494601653466804988627232791786085784383827967976681454100953883786360950680064225125205117392984896084128488626945604241965285022210661186306744278622039194945047123713786960956364371917287467764657573962413890865832645995813390478027590099465764078951269468398352595709825822620522489407726719478268482601476990902640136394437455305068203496252451749399651431429809190659250937221696461515709858387410597885959772975498930161753928468138268683868942774155991855925245953959431049972524680845987273644695848653836736222626099124608051243884390451244136549762780797715691435997700129616089441694868555848406353422072225828488648158456028506016842731
In [45]:
[calc_pi(n, Rational) for n in 1:20]
Out[45]:
20-element Array{Rational{Int64},1}:
                8//3
               44//15
               64//21
              976//315
            10816//3465
           141088//45045
            47104//15015
          2404096//765765
         45693952//14549535
         45701632//14549535
         80863232//25741485
       5256312832//1673196525
       3153846272//1003917915
     457311809536//145568097675
     833925152768//265447707525
    4725585805312//1504203675975
   14176771899392//4512611027925
  524540820979712//166966608033225
  104908189597696//33393321606645
 4301236281540608//1369126185872445

Integration with other programming languages

In [46]:
using PyCall
np = pyimport("numpy")
Out[46]:
PyObject <module 'numpy' from 'c:\\JuliaPkg\\Julia1.5.1\\conda\\3\\lib\\site-packages\\numpy\\__init__.py'>
In [47]:
a = np.zeros((4, 5), dtype="int32")
a
a[1,1]=5
np.mean(a)
Out[47]:
0.25
In [48]:
b = py"{ (x,y):x*y for x in range(1,5) for y in range(1,3) }"
b
Out[48]:
Dict{Any,Any} with 8 entries:
  (1, 2) => 2
  (3, 1) => 3
  (3, 2) => 6
  (2, 2) => 4
  (1, 1) => 1
  (4, 1) => 4
  (4, 2) => 8
  (2, 1) => 2
In [49]:
using RCall
using Distributions, DataFrames, Random
Random.seed!(0);
dat = permutedims(rand(MvNormal([1 0.75; 0.75 1]), 1000))
df = DataFrame(dat)
Out[49]:

1,000 rows × 2 columns

x1x2
Float64Float64
10.6791071.05727
2-0.353007-0.353953
30.5866170.636632
40.0649475-0.0233976
5-0.514210.655664
6-0.688907-1.02123
70.3974820.834954
8-0.346355-0.383834
9-1.60726-2.84633
102.276231.85249
11-0.117138-0.485545
121.142280.798093
130.2794660.283298
14-0.3578840.0449199
150.300234-0.279288
161.423051.33741
170.5886210.245497
180.6911110.853598
19-0.0569299-1.21412
201.590622.11704
210.4815560.148222
220.2463430.0664993
23-1.47788-1.23106
241.269720.844807
25-0.06718670.331446
260.891315-0.849176
27-1.17303-1.46178
28-0.494043-0.0319027
29-0.758701-1.07698
30-0.0303032-0.895319
In [50]:
@rlibrary ggplot2
ggplot(df,aes(x=:x1,y=:x2)) + geom_point()
Out[50]:
RObject{VecSxp}
In [51]:
methods(ggplot)
Out[51]:
# 1 method:
In [52]:
R"library(ggplot2)"
R"ggplot($df,aes(x=x1,y=x2)) + geom_point()"
base64 binary data: 4pSMIFdhcm5pbmc6IFJDYWxsLmpsOiBXYXJuaW5nOiBwYWtpZXQgJ2dncGxvdDInIHpvc3RhsyB6YnVkb3dhbnkgdyB3ZXJzamkgUiAzLjYuMwrilJQgQCBSQ2FsbCBjOlxKdWxpYVBrZ1xKdWxpYTEuNS4xXHBhY2thZ2VzXFJDYWxsXFF6c3N4XHNyY1xpby5qbDoxNjAK
Out[52]:
RObject{VecSxp}