# Ctrl + ENTER
versioninfo()
pwd()
]st
;ls
function f(x,y)
x+y
end
f(x,y) = x+y
@code_lowered f(5,6)
@code_llvm f(5,6)
@code_native f(5,6)
v = [1,2,3,4]
b = Vector{Int}()
c = Complex(1,4)
typeof(c)
c2=Complex{Float64}(1,4)
typeof(c2)
parse(Float64, "3.7")
one(Float64), zero(Int)
@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")
@show Int <: Any
@show Int <: Union{Int, Float64}
@show typeof(nothing)
@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
k = (1, "k")
typeof(k)
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{Int}(undef, 2, 3)
m = zeros(4,5)
m[1,3] = 66
m
m[:,3] #array slicing
a = reshape(1:12, 3, 4)
a = reshape(1:12, 3, 4)
display(a[:, 1:2]) # 3x2 matrix
display(a[:, 1]) # 3 element vector
display(a[1, :]) # 4 element vector
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
fieldnames(typeof(p))
using Parameters
@with_kw mutable struct Point
x::Int64 = 1
y::Float64 = 2.0
meta
end
p2 = Point(meta = "Origin")
p2
x = Dict{Int, Float64}()
x[5] = 66.6
x[7] = 99
x
y = Dict(1=>5.5, 2=>4.5)
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)
@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
a = "sss"
b = " ddd "
a * b
x = 2+4
tekst = "hello $x and $(x*x+2x)"
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
@show g(1,4)
@show g(1,true)
methods(g)
@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)
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
π
typeof(π)
π
for T in [Float16, Float64, BigFloat]
display([calc_pi(2^n, T) for n in 1:10] .- big(π))
end
calc_pi(10000, BigFloat)- π
setprecision(1000) do
calc_pi(1000, BigFloat)-π
end
setprecision(8000) do
@show BigFloat(π)
end;
[calc_pi(n, Rational) for n in 1:20]
using PyCall
np = pyimport("numpy")
a = np.zeros((4, 5), dtype="int32")
a
a[1,1]=5
np.mean(a)
b = py"{ (x,y):x*y for x in range(1,5) for y in range(1,3) }"
b
using RCall
using Distributions, DataFrames, Random
Random.seed!(0);
dat = permutedims(rand(MvNormal([1 0.75; 0.75 1]), 1000))
df = DataFrame(dat)
@rlibrary ggplot2
ggplot(df,aes(x=:x1,y=:x2)) + geom_point()
methods(ggplot)
R"library(ggplot2)"
R"ggplot($df,aes(x=x1,y=x2)) + geom_point()"