The fundamental collection and data structure in a language.
x = []
0-element Array{Any,1}
Homogeneous: 同質性,Array 中只能放入屬於同一型別的物件
Any[]
0-element Array{Any,1}
Int64[]
0-element Array{Int64,1}
x = [1, 2, 3]
3-element Array{Int64,1}: 1 2 3
x = [1, 1.2]
2-element Array{Float64,1}: 1.0 1.2
Int8[1, 2, 3, 4]
4-element Array{Int8,1}: 1 2 3 4
Array{Int8, 1}(undef, 5) # uninitialized
5-element Array{Int8,1}: -112 -8 86 -1 -27
Index starts from 1.
☐☐☐
1 2 3
x
2-element Array{Float64,1}: 1.0 1.2
x[1]
1.0
x[2]
1.2
length(x)
2
x = [6.0, 3.2, 7.6, 0.9, 2.3];
x[1:2]
2-element Array{Float64,1}: 6.0 3.2
x[3:end]
3-element Array{Float64,1}: 7.6 0.9 2.3
x[begin:3]
3-element Array{Float64,1}: 6.0 3.2 7.6
x[1:2:end]
3-element Array{Float64,1}: 6.0 7.6 2.3
view(x, 1:3)
3-element view(::Array{Float64,1}, 1:3) with eltype Float64: 6.0 3.2 7.6
x[2] = 7.5
7.5
x
5-element Array{Float64,1}: 6.0 7.5 7.6 0.9 2.3
push!(x, 9.0)
6-element Array{Float64,1}: 6.0 7.5 7.6 0.9 2.3 9.0
y = [10.0, 3.4]
append!(x, y)
8-element Array{Float64,1}: 6.0 7.5 7.6 0.9 2.3 9.0 10.0 3.4
x
8-element Array{Float64,1}: 6.0 7.5 7.6 0.9 2.3 9.0 10.0 3.4
pop!(x)
3.4
x
7-element Array{Float64,1}: 6.0 7.5 7.6 0.9 2.3 9.0 10.0
popfirst!(x)
6.0
x
6-element Array{Float64,1}: 7.5 7.6 0.9 2.3 9.0 10.0
pushfirst!(x, 6.0)
7-element Array{Float64,1}: 6.0 7.5 7.6 0.9 2.3 9.0 10.0
x = rand(5)
5-element Array{Float64,1}: 0.09810929831480641 0.46112452279406857 0.9618021721401293 0.19292860821288849 0.41253510699062246
sort(x)
5-element Array{Float64,1}: 0.09810929831480641 0.19292860821288849 0.41253510699062246 0.46112452279406857 0.9618021721401293
x
5-element Array{Float64,1}: 0.09810929831480641 0.46112452279406857 0.9618021721401293 0.19292860821288849 0.41253510699062246
sort!(x)
5-element Array{Float64,1}: 0.09810929831480641 0.19292860821288849 0.41253510699062246 0.46112452279406857 0.9618021721401293
x
5-element Array{Float64,1}: 0.09810929831480641 0.19292860821288849 0.41253510699062246 0.46112452279406857 0.9618021721401293
sort(x, rev=true)
5-element Array{Float64,1}: 0.9618021721401293 0.46112452279406857 0.41253510699062246 0.19292860821288849 0.09810929831480641
x = randn(10)
10-element Array{Float64,1}: -0.04449731782053265 -0.24067299751804175 0.42097259514342966 -1.2941611564169055 0.9251854732954373 3.265601659774667 1.2862744750362891 0.06376759068522922 -0.6098980867393012 -0.08696207979292975
sort(x, by=abs)
10-element Array{Float64,1}: -0.04449731782053265 0.06376759068522922 -0.08696207979292975 -0.24067299751804175 0.42097259514342966 -0.6098980867393012 0.9251854732954373 1.2862744750362891 -1.2941611564169055 3.265601659774667
for i in x
println(i)
end
-0.04449731782053265 -0.24067299751804175 0.42097259514342966 -1.2941611564169055 0.9251854732954373 3.265601659774667 1.2862744750362891 0.06376759068522922 -0.6098980867393012 -0.08696207979292975
請造出一個陣列,當中的數值是均勻分佈,從-345到957.6
提示: $\LARGE y = \frac{x - min(x)}{max(x) - min(x)}$
請造出一個陣列,當中的數值是服從常態分佈
請造出一個陣列,當中的數值是服從常態分佈,μ=3.5,σ=2.5
提示: $\LARGE y = \frac{x - \mu}{\sigma}$
Mathematical set.
x = Set([1, 2, 3, 4])
Set{Int64} with 4 elements: 4 2 3 1
push!(x, 5)
Set{Int64} with 5 elements: 4 2 3 5 1
pop!(x)
4
x
Set{Int64} with 4 elements: 2 3 5 1
3 in x
true
4 in x
false
x == Set([3, 2, 1, 5])
true
for i in x
println(i)
end
2 3 5 1
請告訴我以下資料有幾種數值
[8, 4, 1, 2, 9, 4, 5, 4, 5, ...]
A data structure stores key-value pairs.
x = Dict("1" => 1, "2" => 2, "3" => 3)
Dict{String,Int64} with 3 entries: "1" => 1 "2" => 2 "3" => 3
x["1"]
1
x["A"]
KeyError: key "A" not found Stacktrace: [1] getindex(::Dict{String,Int64}, ::String) at ./dict.jl:477 [2] top-level scope at In[52]:1
x["4"] = 4
4
x
Dict{String,Int64} with 4 entries: "4" => 4 "1" => 1 "2" => 2 "3" => 3
x["1"] = 5
5
x
Dict{String,Int64} with 4 entries: "4" => 4 "1" => 5 "2" => 2 "3" => 3
keys(x)
Base.KeySet for a Dict{String,Int64} with 4 entries. Keys: "4" "1" "2" "3"
values(x)
Base.ValueIterator for a Dict{String,Int64} with 4 entries. Values: 4 5 2 3
for (k, v) in x
println(k, "->", v)
end
4->4 1->5 2->2 3->3
Linear systems are most common in scientific computing. We have a linear system such as follow:
We usually express in matrix form:
$$ \begin{bmatrix} w_{11}& w_{12}& w_{13} \\ w_{21}& w_{22}& w_{23} \\ w_{31}& w_{32}& w_{33} \end{bmatrix} \begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} a \\ b \\ c \end{bmatrix} $$And we use symbols to represent matrices and vectors.
$$ Ax = b $$Inner product is defined as following, and transpose is needed to calculate inner product.
A = [1 2 3; 4 5 6; 7 8 9]
B = [12 11 10 9; 8 7 6 5; 4 3 2 1]
A' * B
3×4 Array{Int64,2}: 72 60 48 36 96 81 66 51 120 102 84 66
Determinant (行列式) is calculated by det
. Here we need built-in LinearAlgebra package.
using LinearAlgebra
det(A)
6.661338147750939e-16
Rank (秩) is calculated by rank
. LinearAlgebra package is needed.
rank(A)
2
Trace is calculated by tr
. LinearAlgebra package is needed.
tr(A)
15
Norm is used to calculate the "length" of a vector. Usually, we use Euclidean norm (歐幾里得範數). LinearAlgebra package is needed.
b = [5, 5, 5]
norm(b)
8.660254037844387
If a norm is calculated over a matrix, Frobenius norm is calculated. LinearAlgebra package is needed.
norm(A)
16.881943016134134
P-norm is the norm which is generalized to the power of p
.
norm(b, 5)
6.228654698077587
A Manhattan Distance (or Taxicab norm, $l_1$ norm) is often used in machine learning models.
norm(b, 1)
15.0
norm(b, Inf)
5.0
Identity matrix is really easy to use in Julia, unlike eye
in other languages. One can represent 3x3 identity matrix as follow:
Matrix{Float64}(I, 3, 3)
3×3 Array{Float64,2}: 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0
A more convenient way is to use I
to represent identity. The dimension of an identity matrix is determined automatically.
A * I
3×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9
An inverse matrix (反矩陣) is calculated by inv
.
inv(A)
3×3 Array{Float64,2}: -4.5036e15 9.0072e15 -4.5036e15 9.0072e15 -1.80144e16 9.0072e15 -4.5036e15 9.0072e15 -4.5036e15
If a matrix is not a square and full-rank, Moore-Penrose pseudo-inverse is calculated by pinv
.
pinv(B)
4×3 Array{Float64,2}: -0.1125 0.1 0.3125 -0.0166667 0.0333333 0.0833333 0.0791667 -0.0333333 -0.145833 0.175 -0.1 -0.375
To solve a linear system, we express a linear system as follow.
If matrix A
is invertible (可逆的), \
operation is used to solve the system.
x = A \ b
3-element Array{Float64,1}: -8.999999999999998 12.999999999999998 -4.0
Eigenvalue decomposition (特徵值分解) is very important and widely used. eigen
returns the eigenvalues (特徵值) and eigenvectors (特徵向量) of a matrix.
vals, vecs = eigen(A)
Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}} values: 3-element Array{Float64,1}: -1.1168439698070427 -1.3036777264747022e-15 16.116843969807043 vectors: 3×3 Array{Float64,2}: -0.78583 0.408248 -0.231971 -0.0867513 -0.816497 -0.525322 0.612328 0.408248 -0.818673
eigvals
returns eigenvalues only.
eigvals(A)
3-element Array{Float64,1}: -1.1168439698070427 -1.3036777264747022e-15 16.116843969807043
eigvecs
returns only eigenvectors, which are in column-wise manner.
eigvecs(A)
3×3 Array{Float64,2}: -0.78583 0.408248 -0.231971 -0.0867513 -0.816497 -0.525322 0.612328 0.408248 -0.818673
eigmax
returns maximum of eigenvalues.
eigmax(A)
16.116843969807043
eigmin
returns minimum of eigenvalues.
eigmin(A)
-1.1168439698070427
Singular value decomposition (奇異值分解) is also a widely used matrix decomposition method. svd
is used to get the results.
U, Σ, V = svd(A)
SVD{Float64,Float64,Array{Float64,2}} U factor: 3×3 Array{Float64,2}: -0.214837 0.887231 0.408248 -0.520587 0.249644 -0.816497 -0.826338 -0.387943 0.408248 singular values: 3-element Array{Float64,1}: 16.84810335261421 1.0683695145547103 1.472808250397788e-16 Vt factor: 3×3 Array{Float64,2}: -0.479671 -0.572368 -0.665064 -0.776691 -0.0756865 0.625318 0.408248 -0.816497 0.408248
svdvals
returns singular values (奇異值) in descending order.
svdvals(A)
3-element Array{Float64,1}: 16.84810335261421 1.0683695145547103 1.4728082503977878e-16
using Statistics
A = [1.2 5.21 5.8;
1.3 8.6 7.4;
12. 6. 3.]
b = [5.2, 4.6, 7.2]
3-element Array{Float64,1}: 5.2 4.6 7.2
cor(b)
1.0
cor(A)
3×3 Array{Float64,2}: 1.0 -0.286874 -0.930333 -0.286874 1.0 0.618192 -0.930333 0.618192 1.0
cor(A, b)
3×1 Array{Float64,2}: 0.973610396858362 -0.4979278975048893 -0.9894723165936354
B = [1.3 5.6 5.7 4.1;
5.4 2.3 4.5 7.7;
1.2 1.4 8.7 9.5]
cor(A, B)
3×4 Array{Float64,2}: -0.511052 -0.671761 0.958503 0.761178 0.970029 -0.516922 -0.548068 0.402921 0.79066 0.353308 -0.996271 -0.470317