print("Hello World")
Hello World
print(1/3)
0.3333333333333333
parse(Int64, "2"
2
function add(x, y)
return x+y
end
add (generic function with 1 method)
add(2, 3)
5
func(x) = x^2
func (generic function with 1 method)
func(2)
4
map(x -> x^2, 2)
4
println("Hello World")
print("Terminal")
Hello World Terminal
function name()
# tasks
end
name (generic function with 1 method)
# initial type is any
# string concatenation is doing using stars
function multi(a,b)
return a*b
end
multi (generic function with 1 method)
"Hello " * "Julia"
"Hello Julia"
multi(2,3)
6
multi(2.0,3.0)
6.0
# suppose you want to only take specific type arguments
function multi(a::Int64, b::Int64)
a*b
end
function multi2(a::Int64, b::Int64)
a*b
end
multi2 (generic function with 1 method)
print(multi(2.0,3.0))
6.0
print(multi2(2.0,3.0))
MethodError: no method matching multi2(::Float64, ::Float64) Stacktrace: [1] top-level scope @ In[35]:1 [2] eval @ ./boot.jl:373 [inlined] [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base ./loading.jl:1196
# one liner syntax
mutli_one(a::Float64, b::Float64) = a*b
mutli_one (generic function with 1 method)
mutli_one(3.0,2.0)
6.0
function multi(a::Int64, b::Int64)
a*b, a/b, a%b
end
# returns tuple
multi(9,4)
(36, 2.25, 1)
mutliplication, division, modulo = multi(9,4)
print("$mutliplication $division $modulo")
36 2.25 1
# variable argument function
function var_args(x,y,args...)
println("$x $y")
print("$args")
end
var_args (generic function with 1 method)
var_args(10,20)
10 20 ()
var_args(10,20,1,2,3,4)
10 20 (1, 2, 3, 4)
var_args(10,20, [1,2,3,4],[3,4,5])
10 20 ([1, 2, 3, 4], [3, 4, 5])
function nre(d,u::Float64, ρ=1000, μ=0.001)
d*u*ρ/μ
end
nre (generic function with 4 methods)
nre(5e-3,10.0)
50000.0
nre(d,u::Float64, ρ=1000, μ=0.001) = d*u*ρ/μ
nre (generic function with 4 methods)
nre(5e-3,10.0)
50000.0
function key_args(x,y; length, width)
println("$x $y")
print("$length $width")
end
key_args (generic function with 1 method)
# can pass in any order
key_args(10,20,length=15, width=12)
10 20 15 12
# keep initial value if you do not want to sned every time
function key_args(x,y; length=10, width=20)
println("$x $y")
print("$length $width")
end
key_args (generic function with 1 method)
key_args(10,20)
10 20 10 20
key_args(10,20, length=20)
10 20 20 20
function func1(x::Array{Float64},y::Array{Int64})
x = x*2
y = y*2
end
func1 (generic function with 1 method)
# julia returns the last statement
x = [1.0,2.0]
y = [1,2,3]
func1(x,y)
3-element Vector{Int64}: 2 4 6
# values have not changed
x, y
([1.0, 2.0], [1, 2, 3])
# use the "." inplace operator to modify the arguments
# also use "!" to signify that input values have been modified
function func1!(x::Array{Float64},y::Array{Int64})
x .= x*2
y .= y*2
end
func1! (generic function with 1 method)
func1!(x,y)
x, y
([2.0, 4.0], [2, 4, 6])
func1(x,y)
3-element Vector{Int64}: 4 8 12
y
3-element Vector{Int64}: 2 4 6
b = function (x)
x*2
end
#10 (generic function with 1 method)
b(3)
6
(x,y)-> x*y
#12 (generic function with 1 method)
# the first term in map can be anonymous like above
map((x,y)-> x*y, 2, 4)
8
species = ["CH4(g)", "CO(g)", "H(s)", "O(s)", "N2(g)"]
5-element Vector{String}: "CH4(g)" "CO(g)" "H(s)" "O(s)" "N2(g)"
filter(sp->occursin("g",sp), species)
3-element Vector{String}: "CH4(g)" "CO(g)" "N2(g)"
# list comprehension
x = [i^2 for i in 1:10]
10-element Vector{Int64}: 1 4 9 16 25 36 49 64 81 100
# pass a function to another function
function dfdx(f::Function, x, dx)
return (f(x+dx)-f(x-dx))/2dx
end
dfdx (generic function with 1 method)
func = x-> 2x^2+3x+5
dfdx(func, 2, 1e-8)
10.999999844329977
dfdx(x-> 2x^2+3x+5, 2, 1e-5)
11.000000000294106
# function returning a function
function derivative(f::Function)
return function func(x)
dx = sqrt(eps(Float64))
f1 = f(x+dx)
f2 = f(x)
return (f1-f2)/dx
end
end
derivative (generic function with 1 method)
derivative(x-> 2x^2+3x+5)(5)
23.0
function addall(a)
return function add(b)
return a+b
end
end
addall (generic function with 1 method)
addall(3)(4)
7
function plug(p,y,x)
function residual(f, dfdx)
end
syntax: incomplete: "function" at In[8]:1 requires end Stacktrace: [1] top-level scope @ In[8]:1 [2] eval @ ./boot.jl:373 [inlined] [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base ./loading.jl:1196
# recursion
sumall(n) = n == 1 ? 1 : n+sumall(n-1)
sumall (generic function with 1 method)
sumall(5)
15
factall(n) = n == 0 ? 1 : factall(n-1)*n
factall (generic function with 1 method)
factall(4)
24
# broadcasting, using '.'
a = [3.12,1.24,1.0]
sin.(a)
3-element Vector{Float64}: 0.02159097572609596 0.945783999449539 0.8414709848078965
# one more way
@. sin(a)
3-element Vector{Float64}: 0.02159097572609596 0.945783999449539 0.8414709848078965
function sumtwo(a::Int64, b::Int64)
a+b
end
sumtwo (generic function with 1 method)
a = [1,2,3,4]
b = [5,6,7,8]
sumtwo.(a,b)
4-element Vector{Int64}: 6 8 10 12
# julia stores mulitple methods of the function in a table and upon function call, chooses the one with the
# corresponding argument type and description
check(a,b) = println("Base method")
check(a::Int64,b::Int64) = println("Integer method")
check(a::Float64,b::Float64) = println("Float method")
check(a::Int64,b::Float64) = println("Mixed method")
check (generic function with 4 methods)
s = '(-1)22'
s.split('')
syntax: character literal contains multiple characters Stacktrace: [1] top-level scope @ ~/Documents/Chemical Reactor Modelling/Introduction to Julia.ipynb:1