VERSION
v"0.5.1"
1 + 2
3
1 - 2
-1
4 * 5
20
7 / 5
1.4
# 3 ** 2
3 ^ 2
9
7 ÷ 5
1
typeof(10)
Int64
typeof(2.718f0)
Float32
typeof("Hello")
String
typeof('c')
Char
typeof(:symbol)
Symbol
x = 10
10
println(x)
10
x = 100
100
println(x)
100
y = 3.14
3.14
x * y
314.0
typeof(x * y)
Float64
a = [1, 2, 3, 4, 5]
5-element Array{Int64,1}: 1 2 3 4 5
length(a)
5
a[1] # Julia は 1-origin
1
a[5]
5
a[5] = 99
99
a
5-element Array{Int64,1}: 1 2 3 4 99
a[1:2]
2-element Array{Int64,1}: 1 2
a[2:end]
4-element Array{Int64,1}: 2 3 4 99
a[1:4]
4-element Array{Int64,1}: 1 2 3 4
a[1:end-1]
4-element Array{Int64,1}: 1 2 3 4
me = Dict("height"=>180)
Dict{String,Int64} with 1 entry: "height" => 180
me["height"]
180
me["weight"] = 70
70
me
Dict{String,Int64} with 2 entries: "height" => 180 "weight" => 70
hungry = true
true
sleepy = false
false
(x, y)
(100,3.14)
typeof((x, y))
Tuple{Int64,Float64}
(x, y, 1, 2, 3, "a", true)
(100,3.14,1,2,3,"a",true)
typeof((x, y, 1, 2, 3, "a", true))
Tuple{Int64,Float64,Int64,Int64,Int64,String,Bool}
if hungry
println("I'm hungry!")
else
println("I'm not hungry")
end
I'm hungry!
for i in [1, 2, 3]
println(i)
end
1 2 3
for v = (x, y, 1, 2, 3, "a", true)
println(v)
end
100 3.14 1 2 3 a true
function hello()
println("Hello World!")
end
hello (generic function with 1 method)
hello()
Hello World!
function hello(object)
println("Hello $(object)!")
end
hello (generic function with 2 methods)
hello("cat")
Hello cat!
type Man
name::String
end
function hello(m::Man)
println("Hello $(m.name)!")
end
hello (generic function with 3 methods)
goodby(m::Man) = println("Good-bye $(m.name)!")
goodby (generic function with 1 method)
m = Man("David")
Man("David")
hello(m)
Hello David!
goodby(m)
Good-bye David!
x = [1.0, 2.0, 3.0] # 普通の1次元配列!
3-element Array{Float64,1}: 1.0 2.0 3.0
typeof(x)
Array{Float64,1}
y = [2.0, 4.0, 6.0]
3-element Array{Float64,1}: 2.0 4.0 6.0
x + y
3-element Array{Float64,1}: 3.0 6.0 9.0
x .+ y # elementwise operation
3-element Array{Float64,1}: 3.0 6.0 9.0
x .- y
3-element Array{Float64,1}: -1.0 -2.0 -3.0
x .* y
3-element Array{Float64,1}: 2.0 8.0 18.0
x ./ y
3-element Array{Float64,1}: 0.5 0.5 0.5
x ./ 2.0 # ブロードキャスト
3-element Array{Float64,1}: 0.5 1.0 1.5
A = [1 2;
3 4]
2×2 Array{Int64,2}: 1 2 3 4
println(A)
[1 2; 3 4]
size(A)
(2,2)
eltype(A)
Int64
B = [3 0; 0 6]
2×2 Array{Int64,2}: 3 0 0 6
A + B # A .+ B でも同じ
2×2 Array{Int64,2}: 4 2 3 10
A .* B
2×2 Array{Int64,2}: 3 0 0 24
A .* 10
2×2 Array{Int64,2}: 10 20 30 40
B = [10 20;] # 行ベクトル(=1行2列の行列)の書き方
1×2 Array{Int64,2}: 10 20
A .* B
2×2 Array{Int64,2}: 10 40 30 80
B2 = [B;B] # 簡易 vcat
2×2 Array{Int64,2}: 10 20 10 20
A .* B2
2×2 Array{Int64,2}: 10 40 30 80
X = [51 14 0; 55 19 4] # 敢えて Python(Numpy) と違う書き方にしてます
2×3 Array{Int64,2}: 51 14 0 55 19 4
X[1] # 行や列ではなく、最初の要素が取れる
51
X[1,:] # 行を取得
3-element Array{Int64,1}: 51 14 0
X[:,1] # 列を取得
2-element Array{Int64,1}: 51 55
X = vec(X) # flatten
6-element Array{Int64,1}: 51 55 14 19 0 4
X[[1,3,5]]
3-element Array{Int64,1}: 51 14 0
X[1:2:5] # Python(NumPy) のスライシングと書式が違う
3-element Array{Int64,1}: 51 14 0
X .> 15
6-element BitArray{1}: true true false true false false
X[X.>15]
3-element Array{Int64,1}: 51 55 19
using PyPlot
const plt = PyPlot
# ↑Python の import matplotlib.pyplot as plt 相当
PyPlot
x = 0:0.1:6
y = sin.(x);
※↑行末が ;
で終わる行でセルが終わると、結果を出力しない(豆)
plt.plot(x, y)
1-element Array{Any,1}: PyObject <matplotlib.lines.Line2D object at 0x7f67a2807f98>
y1 = sin.(x);
y2 = cos.(x);
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle="--", label="cos")
plt.xlabel("x")
plt.ylabel("y")
plt.title("sin & cos")
plt.legend()
PyObject <matplotlib.legend.Legend object at 0x7f67a2fa7cf8>
img = plt.imread("lena.png");
plt.imshow(img)
PyObject <matplotlib.image.AxesImage object at 0x7f67a28f8898>
A
2×2 Array{Int64,2}: 1 2 3 4
B2
2×2 Array{Int64,2}: 10 20 10 20
A .* B2 # elementwise multipicity
2×2 Array{Int64,2}: 10 40 30 80
A * B2 # matrix multiplicity
2×2 Array{Int64,2}: 30 60 70 140
x = [1, 2];
y = [3, 4];
x ⋅ y # ベクトルの内積(ドット積、`dot(x, y)` でも同
11
A * x # 2x2行列と2次元ベクトルの(普通の)積
2-element Array{Int64,1}: 5 11
※Julia ではベクトルは列ベクトルの扱い。だから↑のような計算が出来る。結果も列ベクトル。