Basics
In Julia, the name of the variables can be very flexible; we can use Unicode as a variable name. You can also use Unicode math symbols as variable names. To do so, type a backslash followed by the Latex Symbol; then press tab to autocomplete. For example, to type the alpha symbol, simply type “\alpha” followed by tab.
# Assigning values to different types to variables
x = 1
안녕하세요 = 2
y = "안녕하세요"
# We can use Unicode math symbols by backslash Latex symbol followed by tab.
# For example to type alpha symbol, you coud simply do \alpha-tab.
α = 3
β = 4
One can also assign values to different variables in a single line.
γ, δ, σ, ϵ, ε, ξ = 1, 2, 3, 4, 5, 6
A variable can hold a matrix and vector too.
# a is a 5x1 vector
a = [1;2;3;4;5]
# b is a 5x2 matrix
b = [1 2;3 4;5 6;7 8; 9 10]
Functions
A function is a mapping of a tuple of arguments to a return value. The following code produces a function called gFunction
that has two arguments, x
and y
and returns their sum.
function gFunction(x,y)
x + y
end
gFunction(1,2)
julia > 3
Another way to define a function is to use “assignment form”, where we simply express the function in mathematical format. See the example below.
fFunction(x, y) = x - y
fFunction(5,2)
julia> 3
One exciting feature of Julia is its argument passing behavior. See the example below excerpted from Julia documentation.
fib(n::Integer) = n ≤ 2 ? one(n) : fib(n-1) + fib(n-2)
Let’s decompose the above codes into smaller components. Below we define a function called fib
. This function takes one argument n
that is declared to be Integer.
fib(n::integer)
The above example uses a “ternary operator”, an operator for conditional expressions that takes following form in Julia, ___ ? ___ : ___
.
Notice that ?
acts as a conditional evaluation. If the condition is met, the function will return component before :
, i.e. one(n)
, otherwise, it will return the component after :
, i.e., fib(n-1) + fib(n-2)
.
fib(n::Integer) = n ≤ 2 ? one(n) : fib(n-1) + fib(n-2)
Notice that this function is recursive. Even if we invoke, for example, fib(5)
, it will recursively run the function until fib(2)
when it can actually return the numeric value.
Some useful operators with special names
To horizontally concatenate, use hcat
.
hcat(a,b)
julia>
1 1 2
2 3 4
3 5 6
4 7 8
5 9 10
To vertically concatenate, use vcat
.
c=[6;7;8;9;10]
vcat(a,c)
julia>
10-element Vector{Int64}:
1
2
3
4
5
6
7
8
9
10
Method vs. Function
Thus, the overall behavior of a function is a patchwork of the behaviors of its various method definitions.
The choice of which method to execute when a function is applied is called dispatch. Julia allows the dispatch process to choose which of a function’s methods to call based on the number of arguments given, and on the types of all of the function’s arguments. This is different than traditional object-oriented languages, where dispatch occurs based only on the first argument, which often has a special argument syntax, and is sometimes implied rather than explicitly written as an argument. Using all of a function’s arguments to choose which method should be invoked, rather than just the first, is known as multiple dispatch.
Anonymous function
Anonymous function is a function without a name. You can create one using either of the following syntax:
x -> x+ 3
function (x)
x+3
end
map
is often used for executing anonymous function.
map(1:10) do x
2x
end
map(x->2x, 1:10)
map(1:10, 11:20) do x, y
x + y
end
map((x,y)->x+y, 1:10, 11:20)
Plots
To create plots, one can use the Plots
or Gadfly
package.
using Plots
x=range(0,10, length=100)
plot(x, x->x^2)
Reference
Julia documentation, https://docs.julialang.org/
Julia Plots documentation, https://docs.juliaplots.org/latest/