{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "srand(1234);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ベクトル(リスト)の処理" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルを作る" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0-element Array{Any,1}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [] # 何でも入るリスト" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0-element Array{Int64,1}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Int[] # Int型のみのリスト" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0-element Array{Int64,1}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Vector{Int}() # Int[]と同じ" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3] # 3要素からなるリスト" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### すべて同じ値のベクトルを作る" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Int64,1}:\n", " 42\n", " 42\n", " 42\n", " 42\n", " 42\n", " 42\n", " 42\n", " 42\n", " 42\n", " 42" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill(42, 10)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{String,1}:\n", " \"foo\"\n", " \"foo\"\n", " \"foo\"\n", " \"foo\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill(\"foo\", 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルに要素を追加する" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "push!(x, 4) # 右に追加" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Int64,1}:\n", " 0\n", " 1\n", " 2\n", " 3\n", " 4" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unshift!(x, 0) # 左に追加" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8-element Array{Int64,1}:\n", " 0\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 7" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "append!(x, [5,6,7]) # 別のベクトルを追加" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルの要素を削除する" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3,4,5]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pop!(x) # 右端を削除" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shift!(x) # 左端を削除" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 2\n", " 3\n", " 4" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 2\n", " 4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deleteat!(x, 2) # 特定の場所の要素を削除" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルがソート済みかを判定する" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issorted([1,2,3,4])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issorted([1,3,2,4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルをソートする" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7-element Array{Int64,1}:\n", " 1\n", " 5\n", " 4\n", " 3\n", " 7\n", " 6\n", " 2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,5,4,3,7,6,2]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 7" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sort(x) # ベクトルを新しく作る" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7-element Array{Int64,1}:\n", " 7\n", " 6\n", " 5\n", " 4\n", " 3\n", " 2\n", " 1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sort(x, rev=true) # 逆順でソート" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 7" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sort!(x) # ベクトルを上書きする" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルの要素を逆順にする" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 4\n", " 3\n", " 2\n", " 1" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverse(x) # ベクトルを新しく作る" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x == [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Int64,1}:\n", " 4\n", " 3\n", " 2\n", " 1" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverse!(x) # ベクトルを上書きする" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x == [1,2,3,4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルをシャッフルする" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3,4,5,6]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6-element Array{Int64,1}:\n", " 2\n", " 1\n", " 3\n", " 6\n", " 4\n", " 5" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shuffle(x) # ベクトルを新しく作る" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x == [1,2,3,4,5,6]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6-element Array{Int64,1}:\n", " 6\n", " 1\n", " 5\n", " 2\n", " 3\n", " 4" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shuffle!(x) # ベクトルを上書きする" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x == [1,2,3,4,5,6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルを結合する" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vcat([1,2,3], [4,5,6]) # 縦方向 => ベクトル" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×2 Array{Int64,2}:\n", " 1 4\n", " 2 5\n", " 3 6" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hcat([1,2,3], [4,5,6]) # 横方向 => 行列" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 辞書" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 辞書を作る" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{Any,Any} with 0 entries" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Dict() # 何でも入る空の辞書" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 0 entries" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Dict{String,Int}() # Stringがキー、Intが値の空の辞書" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 2 entries:\n", " \"two\" => 2\n", " \"one\" => 1" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Dict(\"one\" => 1, \"two\" => 2) # 初期値のある辞書" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 辞書があるキーを持つか判定する" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "haskey(Dict(\"one\" => 1, \"two\" => 2), \"two\")" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "haskey(Dict(\"one\" => 1), \"two\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 辞書の値を取り出す" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 2 entries:\n", " \"two\" => 2\n", " \"one\" => 1" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = Dict(\"one\" => 1, \"two\" => 2)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[\"one\"]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "\u001b[91mKeyError: key \"three\" not found\u001b[39m", "output_type": "error", "traceback": [ "\u001b[91mKeyError: key \"three\" not found\u001b[39m", "", "Stacktrace:", " [1] \u001b[1mgetindex\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Dict{String,Int64}, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./dict.jl:474\u001b[22m\u001b[22m" ] } ], "source": [ "d[\"three\"] # キーがなければエラー" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### デフォルト値を設定して辞書の値を取り出す" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 2 entries:\n", " \"two\" => 2\n", " \"one\" => 1" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = Dict(\"one\" => 1, \"two\" => 2)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get(d, \"one\", 0) # 第三引数がデフォルト値" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get(d, \"three\", 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 辞書に値を代入する" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 2 entries:\n", " \"two\" => 2\n", " \"one\" => 1" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = Dict(\"one\" => 1, \"two\" => 2)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[\"three\"] = 3" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 3 entries:\n", " \"two\" => 2\n", " \"one\" => 1\n", " \"three\" => 3" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 辞書のキー・値を取り出す" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{String,Int64} with 2 entries:\n", " \"two\" => 2\n", " \"one\" => 1" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = Dict(\"one\" => 1, \"two\" => 2)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Base.KeyIterator for a Dict{String,Int64} with 2 entries. Keys:\n", " \"two\"\n", " \"one\"" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keys(d) # イテレータとして取り出す" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{String,1}:\n", " \"two\"\n", " \"one\"" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collect(keys(d)) # ベクトルとして取り出す" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Base.ValueIterator for a Dict{String,Int64} with 2 entries. Values:\n", " 2\n", " 1" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values(d)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 2\n", " 1" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collect(values(d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 集合" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 集合を作る" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set{Int64}()" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Set{Int}() # 空の集合" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([2, 3, 1])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Set([1,2,3]) # ベクトルから" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set(String[\"bar\", \"baz\", \"foo\"])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Set([\"foo\", \"bar\", \"baz\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 集合にある要素があるか判定する" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in Set([1,2,3])" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 in Set([1,2,3])" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ∈ Set([1,2,3]) # ∈ は in の別名" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 集合に要素を追加する" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([4, 2, 3, 1])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = Set([1,2,3])\n", "push!(s, 4)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([4, 2, 3, 5, 6, 1])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "union!(s, [5, 6])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 和集合・積集合・差集合をとる" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([4, 2, 3, 5])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 = Set([1,2,3])\n", "s2 = Set([2,3,4,5])" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([4, 2, 3, 5, 1])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "union(s1, s2) # 和集合" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([4, 2, 3, 5, 1])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 ∪ s2 # \\cupは和集合" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([2, 3])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intersect(s1, s2) # 積集合" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([2, 3])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 ∩ s2 # \\capは積集合" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Set([1])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "setdiff(s1, s2) # 差集合" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 集合の要素をベクトルとして取り出す" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 2\n", " 3\n", " 1" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = Set([1,2,3])\n", "collect(s) # 順番は順不同" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 文字列処理" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 文字数をカウントする" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "length(\"foo\")" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "length(\"αβγ\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 文字のバイト数を得る" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sizeof(\"foo\")" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sizeof(\"αβγ\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 文字列をUTF-8のバイト列に変換する" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{UInt8,1}:\n", " 0x66\n", " 0x6f\n", " 0x6f" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convert(Vector{UInt8}, \"foo\")" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6-element Array{UInt8,1}:\n", " 0xce\n", " 0xb1\n", " 0xce\n", " 0xb2\n", " 0xce\n", " 0xb3" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convert(Vector{UInt8}, \"αβγ\")" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{UInt8,1}:\n", " 0x66\n", " 0x6f\n", " 0x6f" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vector{UInt8}(\"foo\") # これもOK" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### UTF-8のバイト列を文字列に変換する" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo\"" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convert(String, [0x66, 0x6f, 0x6f])" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"αβγ\"" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convert(String, [0xce, 0xb1, 0xce, 0xb2, 0xce, 0xb3])" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo\"" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String([0x66, 0x6f, 0x6f]) # これもOK" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 特定のパターンを置換する" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"hello, there\"" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "replace(\"hello, world\", \"world\", \"there\")" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abracadabra\"" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "replace(\"a5b4ra221ca1d856ab86r7a7\", r\"\\d+\", \"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 文字列を文字の配列に分割する" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Char,1}:\n", " 'f'\n", " 'o'\n", " 'o'" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collect(\"foo\")" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Char,1}:\n", " 'α'\n", " 'β'\n", " 'γ'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collect(\"αβγ\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 文字列を連結する" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foobar\"" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"foo\" * \"bar\"" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo123\"" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string(\"foo\", 123)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo,bar,baz\"" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "join([\"foo\", \"bar\", \"baz\"], \",\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 文字列を分割する" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{SubString{String},1}:\n", " \"foo\"\n", " \"bar\"\n", " \"baz\"" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "split(\"foo,bar,baz\", \",\")" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{SubString{String},1}:\n", " \"123\"\n", " \"45\" \n", " \"567\"" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "split(\"123 45 567\", r\"\\s+\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 特定の幅で左詰め・右詰めする" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"123 \"" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rpad(123, 5)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\" 123\"" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lpad(123, 5)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " left aligned: 123 \n", "right aligned: 123\n" ] } ], "source": [ "println(\" left aligned: \", rpad(123, 5))\n", "println(\"right aligned: \", lpad(123, 5))" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\" 123\"" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@sprintf(\"%5d\", 123)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 末尾の改行を除く" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo bar\"" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chomp(\"foo bar\\n\")" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo bar\"" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chomp(\"foo bar\\r\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 左右の空白を除く" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo bar\"" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strip(\" foo bar \") # 両側" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo bar \"" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lstrip(\" foo bar \") # 左のみ" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\" foo bar\"" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rstrip(\" foo bar \") # 右のみ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 大文字・小文字に変換する" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"FOO\"" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uppercase(\"foo\")" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"foo\"" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lowercase(\"FOO\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 入出力" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ファイルをテキストとして読み込む" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Juliaは技術計算のための動的言語で、以下の様な機能を備えています。\\n\\n- 多重ディスパッチ\\n- JITコンパイル\\n- マクロなどのメタプログラミング\\n\"" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readstring(\"data/julia.md\")" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{String,1}:\n", " \"Juliaは技術計算のための動的言語で、以下の様な機能を備えています。\"\n", " \"\" \n", " \"- 多重ディスパッチ\" \n", " \"- JITコンパイル\" \n", " \"- マクロなどのメタプログラミング\" " ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readlines(\"data/julia.md\") # 改行で分割" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ファイルを行毎に読み込む" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1: Juliaは技術計算のための動的言語で、以下の様な機能を備えています。\n", "2: \n", "3: - 多重ディスパッチ\n", "4: - JITコンパイル\n", "5: - マクロなどのメタプログラミング\n" ] } ], "source": [ "i = 1\n", "for line in eachline(\"data/julia.md\")\n", " println(i, \": \", line)\n", " i += 1\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ファイルをバイナリデータとして読み込む" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "193-element Array{UInt8,1}:\n", " 0x4a\n", " 0x75\n", " 0x6c\n", " 0x69\n", " 0x61\n", " 0xe3\n", " 0x81\n", " 0xaf\n", " 0xe6\n", " 0x8a\n", " 0x80\n", " 0xe8\n", " 0xa1\n", " ⋮\n", " 0x83\n", " 0xa9\n", " 0xe3\n", " 0x83\n", " 0x9f\n", " 0xe3\n", " 0x83\n", " 0xb3\n", " 0xe3\n", " 0x82\n", " 0xb0\n", " 0x0a" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "read(\"data/julia.md\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ファイルが存在するかを調べる" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isfile(\"data/julia.md\")" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isfile(\"data/python.md\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ファイルサイズを得る" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "193" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filesize(\"data/julia.md\") # 単位はバイト" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ファイルを開閉する" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "IOStream()" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = open(\"data/julia.md\")" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isopen(file) # ファイルが開いているか判定する" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Juliaは技術計算のための動的言語で、以下の様な機能を備えています。\"" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readline(file) # 1行読み込む" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "collapsed": true }, "outputs": [], "source": [ "close(file)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isopen(file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 開いているファイルの現在位置を得る" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "IOStream()" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = open(\"data/julia.md\")" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "position(file) # 最初の位置は0" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "95" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sizeof(readline(file)) # 1行(改行含む)を読む" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "96" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "position(file) # 1行分読み込み位置が進む" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eof(file) # 読み込み位置は末尾か判定" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "97" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sizeof(read(file)) # 残りすべてを読み込む" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eof(file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 開いたファイルを必ず閉じる" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Juliaは技術計算のための動的言語で、以下の様な機能を備えています。\n" ] }, { "ename": "LoadError", "evalue": "\u001b[91mDivideError: integer division error\u001b[39m", "output_type": "error", "traceback": [ "\u001b[91mDivideError: integer division error\u001b[39m", "", "Stacktrace:", " [1] \u001b[1mopen\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::##1#2, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./iostream.jl:152\u001b[22m\u001b[22m" ] } ], "source": [ "# open ... do ... endはdoの中でエラーが起きても必ずファイルを閉じる\n", "open(\"data/julia.md\") do file\n", " println(readline(file))\n", " div(1, 0) # エラー!\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### gzipの圧縮ファイルを読み込む" ] }, { "cell_type": "code", "execution_count": 120, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using CodecZlib # https://github.com/bicycle1885/CodecZlib.jl" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "TranscodingStreams.TranscodingStream{CodecZlib.GzipDecompression,IOStream}()" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = GzipDecompressionStream(open(\"data/julia.md.gz\"))" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{String,1}:\n", " \"Juliaは技術計算のための動的言語で、以下の様な機能を備えています。\"\n", " \"\" \n", " \"- 多重ディスパッチ\" \n", " \"- JITコンパイル\" \n", " \"- マクロなどのメタプログラミング\" " ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readlines(file)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "collapsed": true }, "outputs": [], "source": [ "close(file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### テキストをファイルに書き込む" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "write(\"data/output.txt\", \"アブラカダブラ\\n\")" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"アブラカダブラ\\n\"" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readstring(\"data/output.txt\")" ] }, { "cell_type": "code", "execution_count": 126, "metadata": { "collapsed": true }, "outputs": [], "source": [ "file = open(\"data/output.txt\", \"w\")\n", "println(file, \"寿限無寿限無\")\n", "close(file)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"寿限無寿限無\\n\"" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readstring(\"data/output.txt\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### オブジェクトをファイルにシリアライズする" ] }, { "cell_type": "code", "execution_count": 128, "metadata": { "collapsed": true }, "outputs": [], "source": [ "file = open(\"data/object.dat\", \"w\")\n", "serialize(file, \"テキスト\")\n", "serialize(file, 3.14)\n", "serialize(file, [10, 20, 30])\n", "serialize(file, Dict(\"愛里寿\" => \"センチュリオン\", \"ミカ\" => \"BT-42\"))\n", "close(file)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "deserialize(file) = \"テキスト\"\n", "deserialize(file) = 3.14\n", "deserialize(file) = [10, 20, 30]\n", "deserialize(file) = Dict(\"ミカ\"=>\"BT-42\",\"愛里寿\"=>\"センチュリオン\")\n" ] } ], "source": [ "file = open(\"data/object.dat\")\n", "while !eof(file)\n", " @show deserialize(file)\n", "end\n", "close(file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 外部コマンド" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 外部コマンドを実行する" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "run(`echo \"hello\"`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### コマンドの出力を読み込む" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"julia.md\\njulia.md.gz\\nobject.dat\\noutput.txt\\n\"" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "readstring(`ls data`)" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43-element Array{UInt8,1}:\n", " 0x6a\n", " 0x75\n", " 0x6c\n", " 0x69\n", " 0x61\n", " 0x2e\n", " 0x6d\n", " 0x64\n", " 0x0a\n", " 0x6a\n", " 0x75\n", " 0x6c\n", " 0x69\n", " ⋮\n", " 0x0a\n", " 0x6f\n", " 0x75\n", " 0x74\n", " 0x70\n", " 0x75\n", " 0x74\n", " 0x2e\n", " 0x74\n", " 0x78\n", " 0x74\n", " 0x0a" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "read(`ls data`)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Pipe(RawFD(-1) closed => RawFD(51) open, 0 bytes waiting), Process(`ls data`, ProcessExited(0)))" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe, proc = open(`ls data`) # パイプとプロセス" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "julia.md\n", "julia.md.gz\n", "object.dat\n", "output.txt\n" ] } ], "source": [ "while !eof(pipe)\n", " println(readline(pipe))\n", "end" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Process(`ls data`, ProcessExited(0))" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "proc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 関数型プログラミング" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 条件を満たす要素を残す" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 3\n", " 5" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filter(isodd, [1,2,3,4,5])" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 3\n", " 4\n", " 5" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filter(x->x>2, [1,2,3,4,5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 条件を満たす要素があるか確認する" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "any(isodd, [1,2,3,4])" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "any(isodd, [2,4,6,8])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### すべての要素が条件を満たすかを確認する" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all(isodd, [1,2,3,4])" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all(isodd, [1,3,5,7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 全要素に同じ関数を適用する" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.0 \n", " 0.841471\n", " 0.909297\n", " 0.14112 " ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map(sin, [0.0, 1.0, 2.0, 3.0])" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.0 \n", " 0.841471\n", " 0.909297\n", " 0.14112 " ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sin.([0.0, 1.0, 2.0, 3.0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 関数を適用して畳み込みをする" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foldl(+, 0, [1,2,3]) # 総和の計算" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foldl(+, [1,2,3]) # 第二引数は省略可" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":(if x == 1 # In[146], line 2:\n", " print(1)\n", " else # In[146], line 2:\n", " if x == 2 # In[146], line 2:\n", " print(2)\n", " else # In[146], line 2:\n", " if x == 3 # In[146], line 2:\n", " print(3)\n", " else # In[146], line 2:\n", " nothing\n", " end\n", " end\n", " end)" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 分岐する式の作成\n", "foldr((x, r)->:(if x == $(x); print($(x)); else; $(r); end), :(nothing), [1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ベンチマーク・プロファイル" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 関数の実行時間を測定する" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.008851748" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sort(randn(100000)) # コンパイルのため測定前に一度実行する\n", "@elapsed sort(randn(100000)) # 単位は秒" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Float64,1}:\n", " 0.00876565\n", " 0.0156572 \n", " 0.00870781\n", " 0.00845791\n", " 0.00924334\n", " 0.0104665 \n", " 0.00999714\n", " 0.0109102 \n", " 0.0105973 \n", " 0.00948196" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[@elapsed sort(randn(100000)) for _ in 1:10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 関数のメモリ割り付けを測定する" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1600240" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sort(randn(100000)) # コンパイルのため測定前に一度実行する\n", "@allocated sort(randn(100000)) # 単位はバイト" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Int64,1}:\n", " 1600240\n", " 1600240\n", " 1600240\n", " 1600240\n", " 1600240\n", " 1600240\n", " 1600240\n", " 1600240\n", " 1600240\n", " 1600240" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[@allocated sort(randn(100000)) for _ in 1:10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 関数の実行時間とメモリ割り付けを同時に測定する" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0.008674 seconds (9 allocations: 1.526 MiB)\n" ] } ], "source": [ "sort(randn(100000)) # コンパイルのため測定前に一度実行する\n", "@time sort(randn(100000));" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0.011001 seconds (5 allocations: 1.526 MiB)\n", " 0.010731 seconds (5 allocations: 1.526 MiB)\n", " 0.010590 seconds (5 allocations: 1.526 MiB)\n", " 0.008951 seconds (5 allocations: 1.526 MiB)\n", " 0.009545 seconds (5 allocations: 1.526 MiB)\n", " 0.008640 seconds (5 allocations: 1.526 MiB)\n", " 0.008199 seconds (5 allocations: 1.526 MiB)\n", " 0.008194 seconds (5 allocations: 1.526 MiB)\n", " 0.009020 seconds (5 allocations: 1.526 MiB)\n", " 0.008863 seconds (5 allocations: 1.526 MiB)\n" ] } ], "source": [ "for _ in 1:10\n", " @time sort(randn(100000))\n", "end" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.52587890625" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100000 * sizeof(Float64) * 2 / 1024^2 # 100000要素の倍精度浮動小数点数のメモリ割り当て量 (MiB)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 詳細なベンチマークを行う" ] }, { "cell_type": "code", "execution_count": 154, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using BenchmarkTools # https://github.com/JuliaCI/BenchmarkTools.jl" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BenchmarkTools.Trial: \n", " memory estimate: 1.53 MiB\n", " allocs estimate: 5\n", " --------------\n", " minimum time: 7.214 ms (0.00% GC)\n", " median time: 8.233 ms (0.00% GC)\n", " mean time: 8.661 ms (1.33% GC)\n", " maximum time: 25.404 ms (0.00% GC)\n", " --------------\n", " samples: 577\n", " evals/sample: 1" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@benchmark sort(randn(100000))" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BenchmarkTools.Trial: \n", " memory estimate: 400 bytes\n", " allocs estimate: 3\n", " --------------\n", " minimum time: 421.925 ns (0.00% GC)\n", " median time: 452.987 ns (0.00% GC)\n", " mean time: 554.083 ns (3.35% GC)\n", " maximum time: 19.120 μs (0.00% GC)\n", " --------------\n", " samples: 10000\n", " evals/sample: 199" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@benchmark sort(randn(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### プロファイルを取る" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bubblesort (generic function with 1 method)" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function bubblesort(x)\n", " x = copy(x)\n", " swapped = true\n", " while swapped\n", " swapped = false\n", " for i in 1:endof(x)-1\n", " if x[i+1] < x[i]\n", " x[i+1], x[i] = x[i], x[i+1]\n", " swapped = true\n", " end\n", " end\n", " end\n", " return x\n", "end" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Float64,1}:\n", " -1.79628 \n", " -1.73885 \n", " -0.460956\n", " -0.450571\n", " -0.151273\n", " 0.342973\n", " 0.474332\n", " 0.617031\n", " 0.710638\n", " 0.768515" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bubblesort(randn(10))" ] }, { "cell_type": "code", "execution_count": 159, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = randn(10000)\n", "Profile.clear()\n", "@profile bubblesort(x);" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "196 ./task.jl:335; (::IJulia.##11#14)()\n", " 1 ...Julia/src/eventloop.jl:5; eventloop(::ZMQ.Socket)\n", " 1 ...v0.6/IJulia/src/msg.jl:87; recv_ipython(::ZMQ.Socket)\n", " 1 ....6/JSON/src/Parser.jl:375; #parse#1(::Type{Dict{String,Any}}...\n", " 1 ....6/JSON/src/Parser.jl:158; parse_value(::JSON.Parser.Memory...\n", " 1 ....6/JSON/src/Parser.jl:201; parse_object(::JSON.Parser.Memor...\n", " 195 ...Julia/src/eventloop.jl:8; eventloop(::ZMQ.Socket)\n", " 195 ...rc/execute_request.jl:160; execute_request(::ZMQ.Socket, ::...\n", " 195 ./loading.jl:515; include_string(::String, ::String)\n", " 195 ./:?; anonymous\n", " 195 ./profile.jl:23; macro expansion\n", " 53 ./In[157]:6; bubblesort(::Array{Float64,1})\n", " 109 ./In[157]:7; bubblesort(::Array{Float64,1})\n", " 31 ./In[157]:8; bubblesort(::Array{Float64,1})\n", " 2 ./In[157]:9; bubblesort(::Array{Float64,1})\n" ] } ], "source": [ "Profile.print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 数値の計算" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 奇数・偶数を判定する" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isodd(41) # 奇数" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iseven(42) # 偶数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 素数判定する" ] }, { "cell_type": "code", "execution_count": 163, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using Primes # https://github.com/JuliaMath/Primes.jl" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isprime(42)" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isprime(43)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 素数列を生成する" ] }, { "cell_type": "code", "execution_count": 166, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using Primes # https://github.com/JuliaMath/Primes.jl" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15-element Array{Int64,1}:\n", " 2\n", " 3\n", " 5\n", " 7\n", " 11\n", " 13\n", " 17\n", " 19\n", " 23\n", " 29\n", " 31\n", " 37\n", " 41\n", " 43\n", " 47" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes(50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 商を計算する" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 168, "metadata": {}, "output_type": "execute_result" } ], "source": [ "div(42, 5)" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8.4" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 / 5 # 整数 / 整数 = 浮動小数点数 になるので注意" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 剰余を計算する" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 % 5" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rem(42, 5) # 上と同じ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 絶対値を計算する" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-42)" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 排他的論理和を計算する" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xor(42, 35)" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 ⊻ 35 # 上と同じ(\\xor)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 線形代数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルの和を計算する" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " -1.0\n", " 3.0\n", " 0.0" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1.0, 2.0, 3.0]\n", "y = [-1.0, 3.0, 0.0]" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 0.0\n", " 5.0\n", " 3.0" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + y" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 2.0\n", " -1.0\n", " 3.0" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x - y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ベクトルのスカラー倍を計算する" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 3.2\n", " 6.4\n", " 9.6" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.2x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 行ベクトルを作る" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 RowVector{Float64,Array{Float64,1}}:\n", " 1.0 2.0 3.0" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x'" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 2.0\n", " 3.0" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x'' # 2回でもとに戻る" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 内積を計算する" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14.0" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(x, x)" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14.0" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x ⋅ x # 上と同じ (\\cdot)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14.0" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x'x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 直積を計算する" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 1.0 2.0 3.0\n", " 2.0 4.0 6.0\n", " 3.0 6.0 9.0" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x * x'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 対角行列を作る" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 1.0 0.0 0.0\n", " 0.0 2.0 0.0\n", " 0.0 0.0 3.0" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diagm([1.0, 2.0, 3.0]) # 通常の行列の型" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Diagonal{Float64}:\n", " 1.0 ⋅ ⋅ \n", " ⋅ 2.0 ⋅ \n", " ⋅ ⋅ 3.0" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Diagonal([1.0, 2.0, 3.0]) # 対角行列の型" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 行列積を計算する" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 0.726385 1.6588 1.13122 \n", " 1.01566 1.77963 0.710212\n", " -0.0650975 -1.22412 1.03047 " ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = randn(3, 3)\n", "B = randn(3, 3)" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -1.28874 -3.78644 -1.10194 \n", " -1.70857 -4.33366 -0.816591\n", " 1.54023 2.93319 1.31487 " ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * B # 通常の行列積" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -2.19284 -5.01086 -2.08284 \n", " -1.06601 -3.18224 0.516614\n", " 1.47724 2.94792 1.4728 " ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "At_mul_B(A, B) # Aを転置して掛ける" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -2.19284 -5.01086 -2.08284 \n", " -1.06601 -3.18224 0.516614\n", " 1.47724 2.94792 1.4728 " ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A'B # 上と同じ" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -0.35104 -1.17308 0.938959\n", " -1.26934 -2.02604 2.15484 \n", " 2.2404 2.53849 -1.5252 " ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_mul_Bt(A, B) # Bを転置して掛ける" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -0.35104 -1.17308 0.938959\n", " -1.26934 -2.02604 2.15484 \n", " 2.2404 2.53849 -1.5252 " ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * B' # 上と同じ" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -2.22868 -3.02436 1.7201 \n", " -0.275315 -0.892186 2.39829\n", " 1.98846 2.34479 -1.18667" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "At_mul_Bt(A, B) # AもBも転置して掛ける" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -2.22868 -3.02436 1.7201 \n", " -0.275315 -0.892186 2.39829\n", " 1.98846 2.34479 -1.18667" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A'B' # 上と同じ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 固有値・固有ベクトルを計算する" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 5.0 2.0\n", " -2.0 3.0" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = [5.0 2.0; -2.0 3.0]" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Complex{Float64},1}:\n", " 4.0+1.73205im\n", " 4.0-1.73205im" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eigvals(X)" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Complex{Float64},2}:\n", " 0.707107+0.0im 0.707107-0.0im \n", " -0.353553+0.612372im -0.353553-0.612372im" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eigvecs(X) # 複素数の固有ベクトル" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 12.0557\n", " 29.9443" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eigvals(X'X) # 固有値はすべて正なので正定値行列" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 0.229753 -0.973249\n", " -0.973249 -0.229753" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eigvecs(X'X) # 正定値行列に対しては実数の固有ベクトル" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 線型方程式系を解く" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " -2.0\n", " 0.0" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [3.0 2.0 -1.0\n", " 2.0 -2.0 4.0\n", " -1.0 0.5 -1.0]\n", "b = [1.0, -2.0, 0.0]" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " -2.0\n", " -2.0" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = A \\ b # A x = b の解" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * x ≈ b # 解の確認" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 統計" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 一様分布から乱数を得る" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3867181118018075" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand()" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30-element Array{Float64,1}:\n", " 0.0233823 \n", " 0.584213 \n", " 0.996262 \n", " 0.0486294 \n", " 0.823912 \n", " 0.00980484\n", " 0.506794 \n", " 0.922409 \n", " 0.846705 \n", " 0.685258 \n", " 0.0148934 \n", " 0.101358 \n", " 0.578418 \n", " ⋮ \n", " 0.672831 \n", " 0.167329 \n", " 0.826931 \n", " 0.181764 \n", " 0.401678 \n", " 0.1965 \n", " 0.202258 \n", " 0.0961149 \n", " 0.245319 \n", " 0.919989 \n", " 0.549066 \n", " 0.184145 " ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 標準正規分布から乱数を得る" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.04586384215519224" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "randn()" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30-element Array{Float64,1}:\n", " -0.437116 \n", " 0.385278 \n", " -0.473007 \n", " -0.371043 \n", " -0.0256377\n", " 1.2682 \n", " 0.552556 \n", " -0.0252973\n", " -1.13349 \n", " 2.46311 \n", " -0.902314 \n", " 1.19349 \n", " 0.271862 \n", " ⋮ \n", " -1.3191 \n", " 0.194445 \n", " 2.25805 \n", " 1.87969 \n", " -0.176167 \n", " -0.0395945\n", " 1.24096 \n", " -1.03593 \n", " -0.259864 \n", " 1.2209 \n", " 2.11896 \n", " -0.695827 " ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "randn(30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 様々な分布から乱数を得る" ] }, { "cell_type": "code", "execution_count": 208, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import Distributions ## https://github.com/JuliaStats/Distributions.jl" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Float64,1}:\n", " 0.851551 \n", " 1.8048 \n", " 0.0905379\n", " 0.0643867\n", " 0.464794 \n", " 0.792174 \n", " 2.73824 \n", " 0.332647 \n", " 0.565023 \n", " 1.14877 " ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = Distributions.Gamma(1.0, 2.0) # ガンマ分布\n", "rand(g, 10)" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Int64,1}:\n", " 10\n", " 12\n", " 13\n", " 8\n", " 12\n", " 7\n", " 7\n", " 12\n", " 4\n", " 11" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = Distributions.Poisson(10) # ポアソン分布\n", "rand(p, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### サンプリングを行う" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(1:20) # 1-20の値から1個サンプリング" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Int64,1}:\n", " 10\n", " 8\n", " 17\n", " 7\n", " 17\n", " 3\n", " 10\n", " 18\n", " 7\n", " 10" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(1:20, 10) # 1~20の値から10個サンプリング" ] }, { "cell_type": "code", "execution_count": 213, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using StatsBase # https://github.com/JuliaStats/StatsBase.jl" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Int64,1}:\n", " 7\n", " 18\n", " 17\n", " 8\n", " 20\n", " 12\n", " 14\n", " 5\n", " 4\n", " 19" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample(1:20, 10, replace=false) # 非復元抽出" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 平均・分散を計算する" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.008539475320023372" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean(randn(100))" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.865934027580458" ] }, "execution_count": 216, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var(randn(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 中央値を計算する" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0103736890230341" ] }, "execution_count": 217, "metadata": {}, "output_type": "execute_result" } ], "source": [ "median(exp.(randn(100)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 分位数を計算する" ] }, { "cell_type": "code", "execution_count": 218, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = exp.(randn(100));" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.39879867876339664" ] }, "execution_count": 219, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quantile(x, 0.1)" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6779346947809612" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quantile(x, 0.25)" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0: 0.10576604052523694\n", "0.1: 0.39879867876339664\n", "0.2: 0.5585514635406499\n", "0.3: 0.7252001881297099\n", "0.4: 0.9280416777324269\n", "0.5: 1.1110217625293453\n", "0.6: 1.4068712917385797\n", "0.7: 1.7752452712496667\n", "0.8: 2.9288228171021804\n", "0.9: 4.571210350690497\n", "1.0: 15.1433030232737\n" ] } ], "source": [ "for q in linspace(0, 1, 11)\n", " println(q, \": \", quantile(x, q))\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 行列の最大値・最小値を計算する" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " -0.80238 0.644167 -0.0853525\n", " 0.823909 -0.350321 -0.969769 \n", " 0.463137 2.23558 -0.64375 \n", " 1.85394 0.886008 1.16761 \n", " -0.821372 0.468997 -1.69867 " ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = randn(5, 3)" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.235578644026364" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(A) # 全要素の最大値 (最小値はminimum)" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×1 Array{Float64,2}:\n", " 0.644167\n", " 0.823909\n", " 2.23558 \n", " 1.85394 \n", " 0.468997" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(A, 2) # 行最大" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 Array{Float64,2}:\n", " 1.85394 2.23558 1.16761" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(A, 1) # 列最大" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 行列から平均を引く" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " 10.9184 10.6914 8.21326\n", " 10.2181 10.8977 11.8139 \n", " 9.11668 9.83351 10.3405 \n", " 11.5015 8.74474 9.91524\n", " 11.31 10.1985 8.82895" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = randn(5, 3) + 10" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " 0.977356 0.75038 -1.72774 \n", " -0.758501 -0.0788276 0.837329\n", " -0.646876 0.0699516 0.576924\n", " 1.44766 -1.30908 -0.138582\n", " 1.19753 0.0860069 -1.28354 " ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A .- mean(A, 2) # 行平均を引く" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " 0.305433 0.618205 -1.6091 \n", " -0.394858 0.824564 1.99153 \n", " -1.49624 -0.23966 0.51812 \n", " 0.888563 -1.32843 0.092875\n", " 0.697097 0.125321 -0.993418" ] }, "execution_count": 228, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A .- mean(A, 1) # 列平均を引く" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 共分散行列を計算する" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 3.0 5.8 1.1\n", " 1.0 4.2 0.1" ] }, "execution_count": 229, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = [3.0 5.8 1.1; 1.0 4.2 0.1]" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 2.0 1.6 1.0\n", " 1.6 1.28 0.8\n", " 1.0 0.8 0.5" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cov(X, 1) # 各行がサンプル" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 5.59 4.99 \n", " 4.99 4.64333" ] }, "execution_count": 231, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cov(X, 2) # 各列がサンプル" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 0.979444\n", " 0.979444 1.0 " ] }, "execution_count": 232, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cor(X, 2) # ピアソンの相関係数の行列" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 最小二乗法を行う" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " -1.0\n", " 1.0\n", " 2.0\n", " 3.0" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = randn(20, 4)\n", "b = [-1.0, 1.0, 2.0, 3.0]" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20-element Array{Float64,1}:\n", " 1.8996 \n", " 6.01268 \n", " 1.41041 \n", " -0.542336\n", " -2.5572 \n", " -0.450025\n", " 3.60568 \n", " -3.86358 \n", " -0.819904\n", " 1.11038 \n", " -6.1131 \n", " 1.69123 \n", " -2.1921 \n", " 5.05921 \n", " -1.56521 \n", " 2.39779 \n", " 3.91339 \n", " 0.451056\n", " -1.10424 \n", " 4.80995 " ] }, "execution_count": 234, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = X * b + randn(20) * 0.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "正規方程式 $(X'X)\\beta = X'y$ の解" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " -1.02689 \n", " 0.976217\n", " 2.01937 \n", " 2.98385 " ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(X'X)\\X'*y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "また、$X=QR$と分解すると、$X$が最大列階数のとき正規方程式は\n", "\n", "\n", "\\begin{align}\n", "(X'X)\\beta &= X'y \\\\\n", "(QR)'(QR)\\beta &= (QR)'y \\\\\n", "R'(Q'Q)R\\beta &= R'Q'y \\\\\n", "R\\beta &= Q'y\n", "\\end{align}\n", "\n", "なので次のようにも計算できる" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " -1.02689 \n", " 0.976217\n", " 2.01937 \n", " 2.98385 " ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q, R = qr(X)\n", "R\\(Q'y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 解析" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 関数の根を計算する" ] }, { "cell_type": "code", "execution_count": 237, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using Roots # https://github.com/JuliaMath/Roots.jl" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 238, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fzero(log, 0, 10)" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fzero(x -> sin(x - 3), 2, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### データ点を補完する" ] }, { "cell_type": "code", "execution_count": 240, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using Interpolations" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 241, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x) = sin(x) * exp(-x)" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(::#17) (generic function with 1 method)" ] }, "execution_count": 242, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xs = linspace(-3, 3, 8)\n", "itp = interpolate([f(x) for x in xs], BSpline(Cubic(Line())), OnCell())\n", "scl = scale(itp, xs)\n", "g = x->scl[x]" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3095598756531122" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(1)" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.31573248343343835" ] }, "execution_count": 244, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g(1)" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " x\n", " \n", " \n", " -3\n", " -2\n", " -1\n", " 0\n", " 1\n", " 2\n", " 3\n", " \n", " \n", " \n", " f1\n", " f2\n", " \n", " \n", " \n", " \n", " \n", " \n", " Color\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " -7.5\n", " -5.0\n", " -2.5\n", " 0.0\n", " 2.5\n", " \n", " \n", " y\n", " \n", "\n", "\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n" ], "text/html": [ "\n", "\n", "\n", " \n", " x\n", " \n", " \n", " -10\n", " -9\n", " -8\n", " -7\n", " -6\n", " -5\n", " -4\n", " -3\n", " -2\n", " -1\n", " 0\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 7\n", " 8\n", " 9\n", " 10\n", " -9.0\n", " -8.8\n", " -8.6\n", " -8.4\n", " -8.2\n", " -8.0\n", " -7.8\n", " -7.6\n", " -7.4\n", " -7.2\n", " -7.0\n", " -6.8\n", " -6.6\n", " -6.4\n", " -6.2\n", " -6.0\n", " -5.8\n", " -5.6\n", " -5.4\n", " -5.2\n", " -5.0\n", " -4.8\n", " -4.6\n", " -4.4\n", " -4.2\n", " -4.0\n", " -3.8\n", " -3.6\n", " -3.4\n", " -3.2\n", " -3.0\n", " -2.8\n", " -2.6\n", " -2.4\n", " -2.2\n", " -2.0\n", " -1.8\n", " -1.6\n", " -1.4\n", " -1.2\n", " -1.0\n", " -0.8\n", " -0.6\n", " -0.4\n", " -0.2\n", " 0.0\n", " 0.2\n", " 0.4\n", " 0.6\n", " 0.8\n", " 1.0\n", " 1.2\n", " 1.4\n", " 1.6\n", " 1.8\n", " 2.0\n", " 2.2\n", " 2.4\n", " 2.6\n", " 2.8\n", " 3.0\n", " 3.2\n", " 3.4\n", " 3.6\n", " 3.8\n", " 4.0\n", " 4.2\n", " 4.4\n", " 4.6\n", " 4.8\n", " 5.0\n", " 5.2\n", " 5.4\n", " 5.6\n", " 5.8\n", " 6.0\n", " 6.2\n", " 6.4\n", " 6.6\n", " 6.8\n", " 7.0\n", " 7.2\n", " 7.4\n", " 7.6\n", " 7.8\n", " 8.0\n", " 8.2\n", " 8.4\n", " 8.6\n", " 8.8\n", " 9.0\n", " -10\n", " -5\n", " 0\n", " 5\n", " 10\n", " -9.0\n", " -8.5\n", " -8.0\n", " -7.5\n", " -7.0\n", " -6.5\n", " -6.0\n", " -5.5\n", " -5.0\n", " -4.5\n", " -4.0\n", " -3.5\n", " -3.0\n", " -2.5\n", " -2.0\n", " -1.5\n", " -1.0\n", " -0.5\n", " 0.0\n", " 0.5\n", " 1.0\n", " 1.5\n", " 2.0\n", " 2.5\n", " 3.0\n", " 3.5\n", " 4.0\n", " 4.5\n", " 5.0\n", " 5.5\n", " 6.0\n", " 6.5\n", " 7.0\n", " 7.5\n", " 8.0\n", " 8.5\n", " 9.0\n", " \n", " \n", " \n", " f1\n", " f2\n", " \n", " \n", " \n", " \n", " \n", " \n", " Color\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " -20.0\n", " -17.5\n", " -15.0\n", " -12.5\n", " -10.0\n", " -7.5\n", " -5.0\n", " -2.5\n", " 0.0\n", " 2.5\n", " 5.0\n", " 7.5\n", " 10.0\n", " 12.5\n", " 15.0\n", " -17.5\n", " -17.0\n", " -16.5\n", " -16.0\n", " -15.5\n", " -15.0\n", " -14.5\n", " -14.0\n", " -13.5\n", " -13.0\n", " -12.5\n", " -12.0\n", " -11.5\n", " -11.0\n", " -10.5\n", " -10.0\n", " -9.5\n", " -9.0\n", " -8.5\n", " -8.0\n", " -7.5\n", " -7.0\n", " -6.5\n", " -6.0\n", " -5.5\n", " -5.0\n", " -4.5\n", " -4.0\n", " -3.5\n", " -3.0\n", " -2.5\n", " -2.0\n", " -1.5\n", " -1.0\n", " -0.5\n", " 0.0\n", " 0.5\n", " 1.0\n", " 1.5\n", " 2.0\n", " 2.5\n", " 3.0\n", " 3.5\n", " 4.0\n", " 4.5\n", " 5.0\n", " 5.5\n", " 6.0\n", " 6.5\n", " 7.0\n", " 7.5\n", " 8.0\n", " 8.5\n", " 9.0\n", " 9.5\n", " 10.0\n", " 10.5\n", " 11.0\n", " 11.5\n", " 12.0\n", " 12.5\n", " -20\n", " -10\n", " 0\n", " 10\n", " 20\n", " -18\n", " -17\n", " -16\n", " -15\n", " -14\n", " -13\n", " -12\n", " -11\n", " -10\n", " -9\n", " -8\n", " -7\n", " -6\n", " -5\n", " -4\n", " -3\n", " -2\n", " -1\n", " 0\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 7\n", " 8\n", " 9\n", " 10\n", " 11\n", " 12\n", " 13\n", " \n", " \n", " y\n", " \n", "\n", "\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", "\n" ], "text/plain": [ "Plot(...)" ] }, "execution_count": 245, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Gadfly\n", "plot(layer([f, g], -3, 3), layer(x=xs, y=f.(xs), Geom.point))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1変数関数の微分を計算する" ] }, { "cell_type": "code", "execution_count": 246, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import ForwardDiff # https://github.com/JuliaDiff/ForwardDiff.jl" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f′ (generic function with 1 method)" ] }, "execution_count": 247, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x) = sin(x) * exp(-x)\n", "f′(x) = ForwardDiff.derivative(f, x)" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3095598756531122" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(1.0)" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.11079376530669924" ] }, "execution_count": 249, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f′(1.0)" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " x\n", " \n", " \n", " -3\n", " -2\n", " -1\n", " 0\n", " 1\n", " 2\n", " 3\n", " \n", " \n", " \n", " f1\n", " f2\n", " \n", " \n", " \n", " \n", " \n", " \n", " Color\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " -20\n", " -15\n", " -10\n", " -5\n", " 0\n", " 5\n", " \n", " \n", " f(x)\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", "\n" ], "text/html": [ "\n", "\n", "\n", " \n", " x\n", " \n", " \n", " -10\n", " -9\n", " -8\n", " -7\n", " -6\n", " -5\n", " -4\n", " -3\n", " -2\n", " -1\n", " 0\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 7\n", " 8\n", " 9\n", " 10\n", " -9.0\n", " -8.8\n", " -8.6\n", " -8.4\n", " -8.2\n", " -8.0\n", " -7.8\n", " -7.6\n", " -7.4\n", " -7.2\n", " -7.0\n", " -6.8\n", " -6.6\n", " -6.4\n", " -6.2\n", " -6.0\n", " -5.8\n", " -5.6\n", " -5.4\n", " -5.2\n", " -5.0\n", " -4.8\n", " -4.6\n", " -4.4\n", " -4.2\n", " -4.0\n", " -3.8\n", " -3.6\n", " -3.4\n", " -3.2\n", " -3.0\n", " -2.8\n", " -2.6\n", " -2.4\n", " -2.2\n", " -2.0\n", " -1.8\n", " -1.6\n", " -1.4\n", " -1.2\n", " -1.0\n", " -0.8\n", " -0.6\n", " -0.4\n", " -0.2\n", " 0.0\n", " 0.2\n", " 0.4\n", " 0.6\n", " 0.8\n", " 1.0\n", " 1.2\n", " 1.4\n", " 1.6\n", " 1.8\n", " 2.0\n", " 2.2\n", " 2.4\n", " 2.6\n", " 2.8\n", " 3.0\n", " 3.2\n", " 3.4\n", " 3.6\n", " 3.8\n", " 4.0\n", " 4.2\n", " 4.4\n", " 4.6\n", " 4.8\n", " 5.0\n", " 5.2\n", " 5.4\n", " 5.6\n", " 5.8\n", " 6.0\n", " 6.2\n", " 6.4\n", " 6.6\n", " 6.8\n", " 7.0\n", " 7.2\n", " 7.4\n", " 7.6\n", " 7.8\n", " 8.0\n", " 8.2\n", " 8.4\n", " 8.6\n", " 8.8\n", " 9.0\n", " -10\n", " -5\n", " 0\n", " 5\n", " 10\n", " -9.0\n", " -8.5\n", " -8.0\n", " -7.5\n", " -7.0\n", " -6.5\n", " -6.0\n", " -5.5\n", " -5.0\n", " -4.5\n", " -4.0\n", " -3.5\n", " -3.0\n", " -2.5\n", " -2.0\n", " -1.5\n", " -1.0\n", " -0.5\n", " 0.0\n", " 0.5\n", " 1.0\n", " 1.5\n", " 2.0\n", " 2.5\n", " 3.0\n", " 3.5\n", " 4.0\n", " 4.5\n", " 5.0\n", " 5.5\n", " 6.0\n", " 6.5\n", " 7.0\n", " 7.5\n", " 8.0\n", " 8.5\n", " 9.0\n", " \n", " \n", " \n", " f1\n", " f2\n", " \n", " \n", " \n", " \n", " \n", " \n", " Color\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " -50\n", " -45\n", " -40\n", " -35\n", " -30\n", " -25\n", " -20\n", " -15\n", " -10\n", " -5\n", " 0\n", " 5\n", " 10\n", " 15\n", " 20\n", " 25\n", " 30\n", " 35\n", " -45\n", " -44\n", " -43\n", " -42\n", " -41\n", " -40\n", " -39\n", " -38\n", " -37\n", " -36\n", " -35\n", " -34\n", " -33\n", " -32\n", " -31\n", " -30\n", " -29\n", " -28\n", " -27\n", " -26\n", " -25\n", " -24\n", " -23\n", " -22\n", " -21\n", " -20\n", " -19\n", " -18\n", " -17\n", " -16\n", " -15\n", " -14\n", " -13\n", " -12\n", " -11\n", " -10\n", " -9\n", " -8\n", " -7\n", " -6\n", " -5\n", " -4\n", " -3\n", " -2\n", " -1\n", " 0\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5\n", " 6\n", " 7\n", " 8\n", " 9\n", " 10\n", " 11\n", " 12\n", " 13\n", " 14\n", " 15\n", " 16\n", " 17\n", " 18\n", " 19\n", " 20\n", " 21\n", " 22\n", " 23\n", " 24\n", " 25\n", " 26\n", " 27\n", " 28\n", " 29\n", " 30\n", " -50\n", " 0\n", " 50\n", " -46\n", " -44\n", " -42\n", " -40\n", " -38\n", " -36\n", " -34\n", " -32\n", " -30\n", " -28\n", " -26\n", " -24\n", " -22\n", " -20\n", " -18\n", " -16\n", " -14\n", " -12\n", " -10\n", " -8\n", " -6\n", " -4\n", " -2\n", " 0\n", " 2\n", " 4\n", " 6\n", " 8\n", " 10\n", " 12\n", " 14\n", " 16\n", " 18\n", " 20\n", " 22\n", " 24\n", " 26\n", " 28\n", " 30\n", " \n", " \n", " f(x)\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "Plot(...)" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Gadfly\n", "plot([f, f′], -3, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 多変数関数の勾配を計算する" ] }, { "cell_type": "code", "execution_count": 251, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import ReverseDiff # https://github.com/JuliaDiff/ReverseDiff.jl" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f′ (generic function with 1 method)" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x) = 2dot(x, x) + sum(x)\n", "f′(x) = ReverseDiff.gradient(f, x)" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 1.0\n", " 1.0" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f′(zeros(3))" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 5.0\n", " 5.0\n", " 5.0" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f′(ones(3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 多変数関数の最小化を行う" ] }, { "cell_type": "code", "execution_count": 255, "metadata": { "collapsed": true }, "outputs": [], "source": [ "using Optim # https://github.com/JuliaNLSolvers/Optim.jl" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2変数のRosenbrock関数 https://en.wikipedia.org/wiki/Rosenbrock_function\n", "f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Results of Optimization Algorithm\n", " * Algorithm: Nelder-Mead\n", " * Starting Point: [0.0,0.0]\n", " * Minimizer: [0.9999710322210338,0.9999438685860869]\n", " * Minimum: 1.164323e-09\n", " * Iterations: 74\n", " * Convergence: true\n", " * √(Σ(yᵢ-ȳ)²)/n < 1.0e-08: true\n", " * Reached Maximum Number of Iterations: false\n", " * Objective Function Calls: 108" ] }, "execution_count": 257, "metadata": {}, "output_type": "execute_result" } ], "source": [ "optimize(f, zeros(2))" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Results of Optimization Algorithm\n", " * Algorithm: BFGS\n", " * Starting Point: [0.0,0.0]\n", " * Minimizer: [0.9999999926033423,0.9999999852005353]\n", " * Minimum: 5.471433e-17\n", " * Iterations: 16\n", " * Convergence: true\n", " * |x - x'| < 1.0e-32: false\n", " * |f(x) - f(x')| / |f(x)| < 1.0e-32: false\n", " * |g(x)| < 1.0e-08: true\n", " * f(x) > f(x'): false\n", " * Reached Maximum Number of Iterations: false\n", " * Objective Function Calls: 69\n", " * Gradient Calls: 69" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "optimize(f, zeros(2), BFGS())" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Julia 0.6.0", "language": "julia", "name": "julia-0.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.6.0" }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "532px", "width": "363px" }, "navigate_menu": true, "number_sections": false, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_position": { "height": "737px", "left": "0px", "right": "1098px", "top": "66px", "width": "337px" }, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }