quicksort(xs) = quicksort!(copy(xs)) quicksort!(xs) = quicksort!(xs, 1, length(xs)) function quicksort!(xs, lo, hi) if lo < hi p = partition(xs, lo, hi) quicksort!(xs, lo, p - 1) quicksort!(xs, p + 1, hi) end return xs end function partition(xs, lo, hi) pivot = div(lo + hi, 2) pvalue = xs[pivot] xs[pivot], xs[hi] = xs[hi], xs[pivot] j = lo @inbounds for i in lo:hi-1 if xs[i] <= pvalue xs[i], xs[j] = xs[j], xs[i] j += 1 end end xs[j], xs[hi] = xs[hi], xs[j] return j end quicksort([3, 6, 2, 4, 5, 1]) quicksort([-2.1, 3.4, 5.0, -1.2, 3.1, 0.1]) using Random: seed! seed!(0xdeadbeef) xs = randn(10_000_000) @time quicksort(xs) quicksort(['B', 'A', 'D', 'E', 'C']) quicksort(["Bob", "Alice", "Dave", "Eve", "Charlie"]) x = 100 η = 0.01 漢字変数 = η for i in 1:10 xx = i end xx if true yy = 10 end yy Int64 Float64 Complex{Float64} π isa(π, Irrational) # isa(x, typ)は値xが型typの値であるかどうかを返す関数 typeof(42) typeof(42.0) typeof(3 // 2) typeof(2 + 3im) typeof('A') typeof('漢') typeof("deadbeef") typeof("漢字") typeof(true) typeof(nothing) isa(1, Int) 1 isa Int # 中置もできる [1,2,3] # 5要素のベクトル xs = [1,2,3,4,5] xs[1] xs[5] xs[2] = 200 xs xs[end] xs[end] == xs[5] xs[end-1] == xs[4] xs[2:3] xs[end-2:end] typeof([1,2,3]) isa([1,2,3], Vector) [1 2 3; 4 5 6] x = [1 2 3; 4 5 6] x[1,2] x[2,end] isa([1 2 3; 4 5 6], Matrix) length([1,2,3]) length([1 2 3; 4 5 6]) (1,2,3) typeof((1,2,3)) (1,2,3)[2] (1,2,3)[end] 1,2,3 size([1,2,3]) size([1 2 3; 4 5 6]) 1:10 'a':'z' for i in 3:6 println(i) end x = [1,2,3,4,5,6] x[3:6] for i in 0:10:90 println(i) end for i in 5:-1:1 println(i) end x = Dict("foo" => 1, "bar" => 2) x["foo"] isconcretetype(Int64) isconcretetype(Int32) isconcretetype(Integer) Int64 <: Integer Int32 <: Integer Bool <: Integer supertype(Bool) subtypes(Integer) subtypes(Signed) function tracetype(t) print(t) while t != supertype(t) print(" <: ") t = supertype(t) print(t) end println() end tracetype(Int64) tracetype(Bool) tracetype(BigInt) tracetype(Float64) tracetype(String) typeof([1,2,3]) [1.0, 2.0, 3.0] typeof([1.0, 2.0, 3.0]) [1 2 3] typeof([1 2 3]) [1 2 3; 4 5 6] typeof([1 2 3; 4 5 6]) mutable struct Person name::String age::Int end struct Location x::Float64 y::Float64 end person = Person("ヤマダ田中", 34) person.name, person.age person.age += 1 person loc = Location(1.0, 2.0) loc.x, loc.y loc.x += 3.0 # 2次元空間でのpとq間のユークリッド距離 function dist(p, q) dx = p[1] - q[1] dy = p[2] - q[2] sqrt((dx)^2 + (dy)^2) end dist((0, 0), (3, 4)) dist(p, q) = sqrt((p[1] - q[1])^2 + (p[2] - q[2])^2) dist((0, 0), (3, 4)) # オプション引数: '=' ktop(xs, k=3) = sort(xs)[1:k] ktop([1,5,3,2,6]) ktop([1,5,3,2,6], 2) # キーワード引数: ';' '=' function ktop(xs; k=3, rev=false) sort(xs, rev=rev)[1:k] end ktop([1,5,3,2,6], k=2) ktop([1,5,3,2,6], k=4, rev=true) function pathlength(p, q, rs...) len = dist(p, q) for r in rs len += dist(q, r) q = r end return len end pathlength((0, 0), (1, 1)) pathlength((0, 0), (1, 1), (1, 2)) pathlength((0, 0), (1, 1), (1, 2), (0, 0)) function add(x::Int, y::Int) return x + y end add(1, 2) add(1.0, 2.0) function add(x::Float64, y::Float64) return x + y end add(1.0, 2.0) add(1, 2) function foo(x::Int) println("foo Int: $x") end function foo(x::Int, y::Int) println("foo Int Int: $x $y") end function foo(x::Float64, y::Float64) println("foo Float64 Float64: $x $y") end function foo(x::Int, y::Float64) println("foo Int Float64: $x $y") end foo(1) foo(1, 2) foo(1.0, 3.14) foo(1, 3.14) add(BigInt(1), BigFloat(1)) # これにマッチするメソッドはまだない function add(x::Number, y::Number) return x + y end add(BigInt(1), BigFloat(1)) # 今度は呼び出せる add(1, π), add(π, 1.0), add(true, false) # 様々な型の組み合わせが使える struct RGB r::UInt8 g::UInt8 b::UInt8 function RGB(r, g, b) @assert 0 <= r <= 255 @assert 0 <= g <= 255 @assert 0 <= b <= 255 return new(r, g, b) end end RGB(1, 2, 3) RGB(2, 300, 2) RGB(x) = RGB(x, x, x) RGB() = RGB(0) RGB(), RGB(10), RGB(10, 20, 30) x = 2.1 2x 4x^2 + 3x - 2 x = range(0, stop=1, length=50); 4sin.(2x) - 3cos.(4x) [x for x in 1:4] [x * y for x in 1:4, y in 1:5] [x for x in 1:10 if isodd(x)] Dict(c => i for (i, c) in enumerate('a':'z')) x = [1 2 3; 4 5 6] x' transpose(x) x -> 2x map(x -> 2x, [1,2,3]) map([1,2,3]) do x 2x end open("sample.txt") do io for (n, line) in enumerate(eachline(io)) println("$n: $line") end end r"\w-\d \w+" match(r"\w-\d \w+", "B-2 Spirit") v"1.2.3" `ls -la` run(`ls -la`) run(`cat -n sample.txt`) x = 3.14 @show x insertionsort(xs) = insertionsort!(copy(xs)) function insertionsort!(xs) @show xs for i in 2:length(xs) j = i while j > 1 && xs[j-1] > xs[j] xs[j-1], xs[j] = xs[j], xs[j-1] j -= 1 end @show xs end xs end insertionsort([6,5,3,1,8,7,2,4]) x = 1 @assert x == 1 @assert x == 2 @assert x == 2 "xが$(x)ですよ! xは2じゃないとダメです!" insertionsort(xs) = insertionsort!(copy(xs)) function insertionsort!(xs) #@show xs @inbounds for i in 2:length(xs) j = i while j > 1 && xs[j-1] > xs[j] xs[j-1], xs[j] = xs[j], xs[j-1] j -= 1 end #@show xs end xs end insertionsort([1.0, 0.0]) xs = randn(10); GC.gc() @time for _ in 1:100000; insertionsort(xs); end GC.gc() @time for _ in 1:100000; quicksort(xs); end xs = randn(10000); GC.gc() @time insertionsort(xs); GC.gc() @time quicksort(xs); x = zeros(Float64, 4) x = zeros(Float64, (4, 3)) x = zeros(Float64, (4, 3, 2)) ones(Float64, 4) using LinearAlgebra zeros(3, 3) + 4I [Float64(i > j) for i in 1:4, j in 1:3] Array{Float64}(undef, (3, 4)) ones((3, 4)) x = [1.0, 2.0, 3.0] x .+ 1.0 x .- 1.0 x * 5 5x x / 2 x + 4x 0.3 * (x .- 1) + 0.7 * (x .+ 1) x * 2x # 2つの列ベクトルの積は定義されていない x .* 2x # 2つの列ベクトルの要素毎の積は定義されている 2x ./ x dot(x, x) A = [Float64(i + j) for i in 1:2, j in 1:3] A * x A' * A A'A b = [20.0, 26.0] A \ b module ModFoo struct Foo x end function show(x) print("!! " * string(x) * " !!") end end f = ModFoo.Foo("something") ModFoo.show(f) show(f) Main Main.quicksort([3,1,2]) Main.f Main.ModFoo module Geo export Point, distance, iswithin, move struct Point x::Float64 y::Float64 z::Float64 end distance(a::Point, b::Point) = sqrt(sqdist(a, b)) iswithin(p::Point, c::Point, r::Real) = sqdist(p, c) <= r^2 function move(p::Point, dx, dy, dz) return Point(p.x + dx, p.y + dy, p.z + dz) end sqdist(a::Point, b::Point) = (a.x - b.x)^2 + (a.y - b.y)^2 + (a.z - b.z)^2 end using .Geo po = Point(0, 0, 0) p1 = Point(1, 1, 1) distance(po, p1) iswithin(p1, po, 1.7) move(p1, -1, 0, 0) iswithin(p1, po, 1.7) sqdist(po, p1) Geo.sqdist(po, p1) # 最初に実行するときだけ、下のコメントを消して実行して下さい using Pkg Pkg.update() Pkg.add("Optim") using Optim # インストール直後の読み込みはやや時間がかかる func(x, a=1, b=100) = (a - x[1])^2 + b * (x[2] - x[1]^2)^2 optimize(func, [10.0, 10.0]) versioninfo()