Julia untuk Scientific Computing: Multiple Dispatch, Type System, dan High Performance
Julia adalah bahasa pemrograman tingkat tinggi yang dirancang untuk scientific computing dan numerical analysis. Dengan multiple dispatch dan JIT compilation, Julia mencapai kecepatan mendekati C sambil tetap mudah seperti Python.
๐ Daftar Isi
1. Mengapa Julia?
Julia lahir dari frustrasi para ilmuwan komputer yang harus menggunakan Python/MATLAB untuk prototyping lalu rewrite ke C/Fortran untuk performa. Julia memecahkan "two-language problem" ini dengan menjadi bahasa yang mudah ditulis sekaligus cepat dieksekusi.
Sebelum Julia, para peneliti biasanya menulis prototipe di Python/MATLAB (mudah tapi lambat), lalu menulis ulang di C/Fortran (cepat tapi sulit). Julia memberikan keduanya: kemudahan Python dengan kecepatan C.
| Fitur | Julia | Python | MATLAB | R |
|---|---|---|---|---|
| Kecepatan | ~C | ~100x lebih lambat | ~10x lebih lambat | ~100x lebih lambat |
| Multiple Dispatch | โ Built-in | โ | โ | โ ๏ธ Terbatas |
| Parallel | โ Native | โ ๏ธ GIL | โ ๏ธ Toolbox | โ ๏ธ |
| Type System | โ Dynamic + Parametric | โ Dynamic | โ | โ |
| Metaprogramming | โ Macros | โ ๏ธ Terbatas | โ | โ ๏ธ |
2. Instalasi dan Setup
# macOS
brew install julia
# Windows (download dari julialang.org)
# Atau menggunakan juliaup
curl -fsSL https://install.julialang.org | sh
juliaup add latest
juliaup default latest
# Linux
curl -fsSL https://install.julialang.org | sh
# Verifikasi
julia --version
# julia version 1.10.x
# Jalankan REPL
julia
# Install package manager (Pkg)
using Pkg
Pkg.add("Plots")
Pkg.add("DataFrames")
Pkg.add("DifferentialEquations")
# Buat project baru
mkdir myproject && cd myproject
julia --project=.
# Di REPL:
# ] activate .
# ] add Plots LinearAlgebra
3. Dasar-Dasar Julia
Variables dan Basic Types
# Variables
nama = "BeebaneLabs"
umur = 25
tinggi = 1.75
aktif = true
# Unicode support (sangat bagus untuk math!)
ฮฑ = 0.5
ฮฒ = 1.0
ฮธ = ฯ / 4
ฮฃ = sum([1, 2, 3, 4, 5])
# Type checking
typeof(42) # Int64
typeof(3.14) # Float64
typeof("hello") # String
typeof(true) # Bool
typeof('c') # Char
# Type conversion
Float64(42) # 42.0
Int(3.14) # 3
string(42) # "42"
# Arithmetic
2 + 3 # 5
10 / 3 # 3.3333...
10 รท 3 # 3 (integer division, รท = \div)
10 % 3 # 1 (modulo)
2^10 # 1024
# String operations
s1 = "Hello"
s2 = "World"
greeting = "$s1, $s2!" # "Hello, World!"
calc = "2 + 3 = $(2 + 3)" # "2 + 3 = 5"
Control Flow
# If-elseif-else
nilai = 85
if nilai >= 90
grade = "A"
elseif nilai >= 80
grade = "B"
elseif nilai >= 70
grade = "C"
else
grade = "D"
end
# Ternary (inline if)
status = nilai >= 70 ? "Lulus" : "Tidak Lulus"
# For loop
for i in 1:10
println("Iterasi $i")
end
# For dengan step
for i in 0:2:20
println("Genap: $i")
end
# For dengan collection
buah = ["apel", "jeruk", "mangga"]
for (i, b) in enumerate(buah)
println("$i. $b")
end
# While loop
n = 10
while n > 0
global n -= 1
println("n = $n")
end
# List comprehension
kuadrat = [x^2 for x in 1:10]
genap = [x for x in 1:20 if x % 2 == 0]
matriks = [i + j for i in 1:3, j in 1:3]
# Functions
function tambah(a, b)
return a + b
end
# Short form
tambah2(a, b) = a + b
# Optional arguments
function greet(nama; greeting="Halo")
return "$greeting, $nama!"
end
# Varargs
function jumlah(args...)
return sum(args)
end
4. Type System
Type system Julia adalah kombunikasi yang unik: dynamic typing dengan optional type annotations dan parametric types. Ini memungkinkan generic programming yang sangat powerful.
/"No. Type hierarchy"/
Number
Real
/"AbstractFloat"/
/"Float16, Float32, Float64"/
Integer
Bool
/"Signed (Int8, Int16, Int32, Int64)"/
/"Unsigned (UInt8, UInt16, UInt32, UInt64)"/
Complex
/"AbstractString"/
/"AbstractChar"/
/"... (many more)"/