A quick start guide of Julia for numerical computation
约 1040 字大约 3 分钟
2026-05-13
Here is a practical Julia quick start guide, especially oriented toward scientific/numerical computing.
1. Install Julia
Recommended modern way:
# macOS / Linux
curl -fsSL https://install.julialang.org | shOn Windows, install Julia from the Microsoft Store or use:
winget install julia -s msstoreThe official installer also installs juliaup, Julia’s version manager, and after installation you can start Julia by typing:
juliaJulia’s official docs recommend using the REPL — the interactive Julia shell — for quick experimentation. (The Julia Programming Language)
2. First REPL commands
1 + 2
sqrt(2)
sin(pi / 2)
typeof(3.14)Exit Julia:
exit()or press Ctrl-D.
Julia is dynamically typed, but it compiles efficient machine code using type information inferred at runtime.
3. Variables and basic types
x = 3
y = 2.5
name = "Julia"
typeof(x) # Int64
typeof(y) # Float64
typeof(name) # StringString interpolation:
x = 10
println("x = $x")
println("x^2 = $(x^2)")4. Functions
Long form:
function square(x)
return x^2
endShort form:
square(x) = x^2Anonymous function:
x -> x^2Example:
map(x -> x^2, [1, 2, 3, 4])Julia functions are generic functions; different argument types can dispatch to different methods, which is one of Julia’s central design ideas. (docs.julialang.org)
5. Arrays and indexing
v = [1, 2, 3, 4]
A = [1 2; 3 4]Important: Julia uses 1-based indexing, like MATLAB, not 0-based indexing like Python/C.
v[1] # 1
A[1, 2] # 2
A[:, 1] # first column
A[2, :] # second rowArray comprehensions:
squares = [i^2 for i in 1:10]For numerical work, prefer arrays with concrete element types such as Vector{Float64} or Matrix{Float64}. The official docs note that arrays with specific element types are generally preferable for computational purposes. (docs.julialang.org)
6. Loops and conditionals
for i in 1:5
println(i)
endx = 3
if x > 0
println("positive")
elseif x < 0
println("negative")
else
println("zero")
endWhile loop:
i = 1
while i <= 5
println(i)
i += 1
end7. Packages
Julia has a built-in package manager called Pkg, used for installing, updating, and removing packages. (docs.julialang.org)
Inside Julia:
using Pkg
Pkg.add("Plots")
Pkg.add("DataFrames")Or press ] in the REPL to enter package mode:
pkg> add Plots
pkg> statusPress Backspace or Ctrl-C to return to normal Julia mode.
For a project, use environments:
using Pkg
Pkg.activate(".")
Pkg.add("Plots")This creates a Project.toml and usually a Manifest.toml, similar in spirit to Python’s virtual environments.
8. Plotting
Install:
using Pkg
Pkg.add("Plots")Use:
using Plots
x = range(0, 2pi, length=200)
y = sin.(x)
plot(x, y, label="sin(x)")Notice the dot:
sin.(x)This means “apply sin elementwise.” This is very important in Julia.
Compare:
sin(1.0) # scalar
sin.([1,2,3]) # vectorized / broadcasted9. Linear algebra
using LinearAlgebra
A = [1.0 2.0; 3.0 4.0]
b = [1.0, 2.0]
x = A \ b # solve Ax = b
det(A)
eigvals(A)
norm(b)Matrix multiplication:
A * AElementwise multiplication:
A .* AThis distinction is important.
10. A small numerical example
Solve the ODE:
[ u'(t) = -u(t), \quad u(0)=1 ]
using explicit Euler:
function euler(f, u0, tspan, h)
t0, T = tspan
ts = collect(t0:h:T)
us = similar(ts)
us[1] = u0
for n in 1:length(ts)-1
us[n+1] = us[n] + h * f(ts[n], us[n])
end
return ts, us
end
f(t, u) = -u
ts, us = euler(f, 1.0, (0.0, 5.0), 0.01)
using Plots
plot(ts, us, label="Euler")
plot!(ts, exp.(-ts), label="exact", linestyle=:dash)This shows a very Julia-like workflow: write a normal function, use arrays, broadcast with dots, and plot.
11. Structs and multiple dispatch
Define a type:
struct Point
x::Float64
y::Float64
endUse it:
p = Point(1.0, 2.0)
p.x
p.yDefine behavior by type:
distance(p::Point) = sqrt(p.x^2 + p.y^2)
distance(Point(3.0, 4.0)) # 5.0Multiple dispatch means Julia chooses which method to call based on the types of all arguments, not just the first one. This is one of the reasons Julia is elegant for mathematical software.
12. Performance basics
The most important rules:
# Good: put performance-critical code inside functions
function compute_sum(n)
s = 0.0
for i in 1:n
s += 1 / i
end
return s
endAvoid changing variable types inside tight loops:
# Less good
x = 1
x = 1.5Use broadcasting instead of unnecessary temporary loops:
y = sin.(x) .+ cos.(x)Inspect performance:
@time compute_sum(10^6)For more serious benchmarking:
using Pkg
Pkg.add("BenchmarkTools")
using BenchmarkTools
@btime compute_sum(10^6)13. File structure
A simple Julia project:
my_project/
Project.toml
Manifest.toml
src/
MyProject.jl
scripts/
run_experiment.jlExample script:
# scripts/run_experiment.jl
using LinearAlgebra
A = rand(5, 5)
b = rand(5)
x = A \ b
println(norm(A*x - b))Run from terminal:
julia scripts/run_experiment.jl14. Things that may surprise Python users
# 1-based indexing
v[1]
# ranges are common
1:10
# ! convention means function mutates its argument
sort(v) # returns sorted copy
sort!(v) # sorts v in place
# dot means elementwise/broadcast
x .+ y
sin.(x)
A .* B
# matrix multiply is *
A * B
# solve linear system
A \ b15. Recommended learning path
- Basic syntax: variables, functions, arrays, loops.
- Broadcasting and linear algebra.
- Package environments with
Pkg. - Plotting and data IO.
- Multiple dispatch and custom structs.
- Performance: type stability, allocation, benchmarking.
- Scientific packages:
DifferentialEquations.jl,Optim.jl,JuMP.jl,Tullio.jl,CUDA.jldepending on your direction.
A nice first goal: rewrite a small numerical experiment you already know — for example, an RK method, finite difference solver, or optimization routine — in Julia. That will improve your understanding much faster than only reading syntax.
