{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Vector, Matrix, Array" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1; 2; 3]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "println(a)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 Array{Int64,2}:\n", " 1 2 3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1 2 3]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 Array{Int64,2}:\n", " 4 5 6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [4 5 6]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Int64,2}:\n", " 1 2 3\n", " 4 5 6" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [1 2 3; 4 5 6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 세미콜론이 새로운 row를 의미함" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Julia는 index 시작시 1부터 시작함" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1,1]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1,2]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[2,1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- transpose() 또는 '를 사용할 수 이씀" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×2 LinearAlgebra.Transpose{Int64,Array{Int64,2}}:\n", " 1 4\n", " 2 5\n", " 3 6" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transpose(A)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×2 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}:\n", " 1 4\n", " 2 5\n", " 3 6" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- inner product, dot product\n", " - 그냥 곱하는 방법\n", " - LinearAlgebra의 dot을 사용하는 방법" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 7\n", " 8\n", " 9" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1; 2; 3;]\n", "c = [7; 8; 9;]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a'*c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- using이 python의 import랑 비슷한 느낌" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "using LinearAlgebra" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(a, c)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 0.0\n", " 0.0 1.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Matrix(1.0I, 2, 2)\n", "# I는 identity matrix => 단위 행렬. I가 없으면 에러남" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching Array{T,2} where T(::Float64, ::Int64, ::Int64)\nClosest candidates are:\n Array{T,2} where T(!Matched::UndefInitializer, ::Integer, ::Integer) at sysimg.jl:152\n Array{T,2} where T(!Matched::UniformScaling, ::Integer, ::Integer) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/uniformscaling.jl:341", "output_type": "error", "traceback": [ "MethodError: no method matching Array{T,2} where T(::Float64, ::Int64, ::Int64)\nClosest candidates are:\n Array{T,2} where T(!Matched::UndefInitializer, ::Integer, ::Integer) at sysimg.jl:152\n Array{T,2} where T(!Matched::UniformScaling, ::Integer, ::Integer) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/uniformscaling.jl:341", "", "Stacktrace:", " [1] top-level scope at In[20]:1" ] } ], "source": [ "Matrix(1.0, 2, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- zero matrix는 zeros() 사용" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×1 Array{Float64,2}:\n", " 0.0\n", " 0.0\n", " 0.0\n", " 0.0" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zeros(4,1)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 0.0 0.0 0.0\n", " 0.0 0.0 0.0" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zeros(2,3)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×3 Array{Float64,2}:\n", " 1.0 1.0 1.0" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones(1,3)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×3 Array{Float64,2}:\n", " 1.0 1.0 1.0\n", " 1.0 1.0 1.0" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones(2,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- inv() : Inverse" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 1 3 2\n", " 3 2 2\n", " 1 1 1" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = [1 3 2; 3 2 2; 1 1 1]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 1.11022e-16 1.0 -2.0\n", " 1.0 1.0 -4.0\n", " -1.0 -2.0 7.0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inv(B)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 1.0 0.0 0.0\n", " 0.0 1.0 0.0\n", " -2.22045e-16 0.0 1.0" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B * inv(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 0이 아니라 -2.22045e-16라고 나온건 inverse matrix의 연산이 정확하지 않아서 그럼" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0000000000000004" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inv(B)[2,1]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1; 2; 3]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 2.0\n", " 3.0" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [1.0; 2; 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- a는 Int64, b는 Float64" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Array object 특정 타입으로 만들고 싶은 경우\n", " - Array와 undef를 함께 call\n", " - undef는 [공식 문서](https://docs.julialang.org/en/v1/base/arrays/#Core.undef) 참고" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 2.3064115165e-314\n", " 2.3064115323e-314\n", " 2.3091702605e-314" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = Array{Float64}(undef, 3)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1] = 1" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[2] = 2" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[3] = 3" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 1.0\n", " 2.0\n", " 3.0" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- a pair of data types" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Tuple{Int64,Int64},1}:\n", " (4671453744, 4671453888)\n", " (4671453936, 4671453984)\n", " (4671454032, 4678541088)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs = Array{Tuple{Int64, Int64}}(undef, 3)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Tuple{Int64,Int64},1}:\n", " (4671453744, 4671453888)\n", " (4671453936, 4671453984)\n", " (4671454032, 4678541088)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 3)" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs2 = (1, 3)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4671453744, 4671453888)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs[1]" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs[1] = (1,2)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 4)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs[2] = (2,3)\n", "pairs[3] = (3,4)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Tuple{Int64,Int64},1}:\n", " (1, 2)\n", " (2, 3)\n", " (3, 4)" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pairs" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Tuple{Int64,Int64},1}:\n", " (1, 2)\n", " (2, 3)\n", " (3, 4)" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 이거랑 동일함\n", "pairs = [(1,2); (2,3); (3,4)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Tuple은 노드와 링크로 이루어진 network data를 핸들링할 때 유용" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Tuple{Int64,Int64,Int64},1}:\n", " (4662978080, 4669435552, 4669435392)\n", " (1, 4669435632, 4669435712) \n", " (4669435792, 4, 4) " ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ijk_array = Array{Tuple{Int64, Int64, Int64}}(undef, 3)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 4, 2)" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ijk_array[1] = (1, 4, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indices and Ranges" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30\n", " 40\n", " 50\n", " 60\n", " 70\n", " 80\n", " 90" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10; 20; 30; 40; 50; 60; 70; 80; 90]" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 10\n", " 20\n", " 30" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# range\n", "a[1:3]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 10\n", " 40\n", " 70" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1부터 9까지 중 +3\n", "a[1:3:9]" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[3]" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Int64,1}:\n", " 20\n", " 50" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:3:7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- end라는 키워드도 있음" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 70\n", " 80\n", " 90" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[end-2:end]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 200\n", " 300\n", " 400" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [200; 300; 400]" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 200\n", " 300\n", " 400" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:4] = b" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9-element Array{Int64,1}:\n", " 10\n", " 200\n", " 300\n", " 400\n", " 50\n", " 60\n", " 70\n", " 80\n", " 90" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- collect를 사용해도 됨" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Int64,1}:\n", " 1\n", " 3\n", " 5\n", " 7\n", " 9" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = collect(1:2:9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Printing Messages\n", "- println과 print 존재" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "println(\"Hello World\")" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world Again" ] } ], "source": [ "print(\"hello \"); print(\"world\"); print(\" Again\")" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello \n", "world\n", " Again\n" ] } ], "source": [ "println(\"hello \"); println(\"world\"); println(\" Again\")" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123.0" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 123.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Python f-string과 유사한 기능은 `$`변수 로 사용 가능. 혹은 `$`(연산)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of a = 123.0\n" ] } ], "source": [ "println(\"The value of a = \", a)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a is 123.0, and a-10 is 113.0.\n" ] } ], "source": [ "println(\"a is $a, and a-10 is $(a-10).\")" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 1\n", " 3\n", " 10" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [1; 3; 10]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b is [1, 3, 10]\n" ] } ], "source": [ "println(\"b is $b\")" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the second element of b is 3\n" ] } ], "source": [ "println(\"the second element of b is $(b[2])\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Printf라는걸 하면.. python의 string.format(a=a) 이런게 가능한듯\n", " - indeger는 %d" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "using Printf" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of a = 123.000000" ] } ], "source": [ "@printf(\"The %s of a = %f\", \"value\", a)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Float64,1}:\n", " 123.12345 \n", " 10.983 \n", " 1.092312" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = [123.12345; 10.983; 1.092312]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- for문은 end를 꼭 붙여야하고, : <- 이런게 없음" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c[1] = 123.12\n", "c[2] = 10.98\n", "c[3] = 1.09\n" ] } ], "source": [ "for i in 1:length(c)\n", " @printf(\"c[%d] = %7.2f\\n\", i, c[i])\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- %7.2f는 토탈 넘버는 7이고 소수점은 2자리까지\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- @sprintf도 있음 => 다만 리턴이 string(screen 표시 대신)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"The value of a = 123.000000\"" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str = @sprintf(\"The %s of a = %f\", \"value\", a)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of a = 123.000000\n" ] } ], "source": [ "println(str)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Collection, Dictionary, For-Loop" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is number 1\n", "This is number 2\n", "This is number 3\n", "This is number 4\n", "This is number 5\n" ] } ], "source": [ "for i in 1:5\n", " println(\"This is number $i\")\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 구조\n", "\n", "```\n", "for i in I\n", " #do something here for each i\n", "end\n", "```\n", "\n", "- 참고로 I는 collection(보통 Range 또는 Dict)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is number 1\n", "This is number 2\n" ] } ], "source": [ "for i in 1:5\n", " if i >= 3\n", " break\n", " end\n", " println(\"This is number $i\")\n", "end" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{String,1}:\n", " \"football\" \n", " \"pocketmon\"\n", " \"swim\" " ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_keys = [\"hi\", \"bye\", \"cool\"]\n", "my_values = [\"football\", \"pocketmon\",\"swim\"]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "d = Dict()\n", "\n", "for i in 1:length(my_keys)\n", " d[my_keys[i]] = my_values[i]\n", "end" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{Any,Any} with 3 entries:\n", " \"bye\" => \"pocketmon\"\n", " \"hi\" => \"football\"\n", " \"cool\" => \"swim\"" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bye is a pocketmon player\n", "hi is a football player\n", "cool is a swim player\n" ] } ], "source": [ "for (key, value) in d\n", " println(\"$key is a $value player\")\n", "end" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"football\"" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[\"Diego Maradona\"] = \"football\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- '이 아니라 \"을 써야함" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- network" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Tuple{Int64,Int64},1}:\n", " (1, 2)\n", " (3, 4)\n", " (4, 2)" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "links = [(1,2), (3,4), (4,2)]" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{Int64,1}:\n", " 5\n", " 13\n", " 8" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "link_costs = [5, 13, 8]" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{Any,Any} with 0 entries" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "link_dict = Dict()" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [], "source": [ "for i in 1:length(links)\n", " link_dict[links[i]] = link_costs[i]\n", "end" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{Any,Any} with 3 entries:\n", " (1, 2) => 5\n", " (4, 2) => 8\n", " (3, 4) => 13" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "link_dict" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Link (1, 2) has cost of 5\n", "Link (4, 2) has cost of 8\n", "Link (3, 4) has cost of 13\n" ] } ], "source": [ "for (link, cost) in link_dict\n", " println(\"Link $link has cost of $cost\")\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Function" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x,y)\n", " return 3x+y\n", "end" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(1,3)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "96" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*(f(3,2)+f(5,6))" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "my_func (generic function with 1 method)" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function my_func(n, m)\n", " a = zeros(n, 1)\n", " b = ones(m, 1)\n", " return a,b\n", "end" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([0.0; 0.0; 0.0], [1.0; 1.0])" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, y = my_func(3,2)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×1 Array{Float64,2}:\n", " 0.0\n", " 0.0\n", " 0.0" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×1 Array{Float64,2}:\n", " 1.0\n", " 1.0" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqrt(9)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "ename": "DimensionMismatch", "evalue": "DimensionMismatch(\"matrix is not square: dimensions are (1, 2)\")", "output_type": "error", "traceback": [ "DimensionMismatch(\"matrix is not square: dimensions are (1, 2)\")", "", "Stacktrace:", " [1] sqrt(::Array{Int64,2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/LinearAlgebra.jl:214", " [2] top-level scope at In[92]:1" ] } ], "source": [ "sqrt([9 16])" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×2 Array{Float64,2}:\n", " 3.0 4.0" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqrt.([9 16])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 그냥 sqrt => 단일에 적용\n", "- sqrt. => each element에 적용(map이라고 보면 되려나)\n", "- 모든 함수에 적용" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "myfunc (generic function with 1 method)" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfunc(x) = sin(x) + 3*x" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.141120008059866" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfunc(3)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "ename": "DimensionMismatch", "evalue": "DimensionMismatch(\"matrix is not square: dimensions are (1, 2)\")", "output_type": "error", "traceback": [ "DimensionMismatch(\"matrix is not square: dimensions are (1, 2)\")", "", "Stacktrace:", " [1] exp!(::Array{Complex{Float64},2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/LinearAlgebra.jl:214", " [2] sin(::Array{Int64,2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/dense.jl:792", " [3] myfunc(::Array{Int64,2}) at ./In[94]:1", " [4] top-level scope at In[96]:1" ] } ], "source": [ "myfunc([5 10])" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×2 Array{Float64,2}:\n", " 14.0411 29.456" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfunc.([5 10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scope of Variables" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "syntax: extra token \"x\" after end of expression", "output_type": "error", "traceback": [ "syntax: extra token \"x\" after end of expression", "" ] } ], "source": [ "function f(x)\n", " retun x+2\n", "end\n" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "g (generic function with 1 method)" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function g(x)\n", " return 3x+3\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 여기 인자가 둘다 x를 줬는데, 다른 scope block으로 정의되서 충돌하지 않음" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "run (generic function with 1 method)" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " return x+z\n", "end\n", "\n", "function run()\n", " z=10\n", " return f(5)\n", "end" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "ename": "UndefVarError", "evalue": "UndefVarError: z not defined", "output_type": "error", "traceback": [ "UndefVarError: z not defined", "", "Stacktrace:", " [1] f(::Int64) at ./In[100]:2", " [2] run() at ./In[100]:7", " [3] top-level scope at In[101]:1" ] } ], "source": [ "run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- z가 안됨. 리팩토링" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x)\n", " return x+a\n", "end\n", "\n", "function run()\n", " return f(5) \n", "end\n", "\n", "a=10\n", "run()" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f2(x)\n", " a=0\n", " return x+a\n", "end\n", "\n", "a=5" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "println(f2(1))" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "println(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 변수 앞에 _를 붙이면 local variable" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f3 (generic function with 1 method)" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f3(x)\n", " _a = 0\n", " return x + _a\n", "end\n" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "5\n" ] } ], "source": [ "a = 5\n", "println(f3(1))\n", "println(a)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "5\n" ] } ], "source": [ "function f4(x, a)\n", " return x+a\n", "end\n", "\n", "a = 5\n", "println(f4(1, a))\n", "println(a)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "ename": "BoundsError", "evalue": "BoundsError: attempt to access 1×5 Array{Int64,2} at index [6]", "output_type": "error", "traceback": [ "BoundsError: attempt to access 1×5 Array{Int64,2} at index [6]", "", "Stacktrace:", " [1] getindex(::Array{Int64,2}, ::Int64) at ./array.jl:729", " [2] top-level scope at ./In[109]:4" ] } ], "source": [ "a = [1 2 3 4 5]\n", "s = 0\n", "for i in 1:length(A)\n", " s += a[i]\n", "end" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Int64,1}:\n", " 1\n", " 2\n", " 3\n", " 4\n", " 5" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function my_sum(a)\n", " s = 0\n", " for i in 1:5\n", " s += a[i]\n", " end\n", " return s\n", "end\n", "\n", "a = [1; 2; 3; 4; 5]" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_sum(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random Number Generation" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9266495281538778" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand()" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8304477728220296" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand()" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Float64,1}:\n", " 0.8324770166728122\n", " 0.2662287032474082\n", " 0.6933367170942821\n", " 0.2955082361684169\n", " 0.577475034743496 " ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- index 선택" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "93.7998436597625" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand() * 100" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand(1:10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- randn : 정규분포" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Float64,1}:\n", " -1.2142547850901897 \n", " -1.991431544403744 \n", " -1.3196835160607816 \n", " 1.1693537449632032 \n", " 0.28594133262787147" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "randn(5)" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "my_randn (generic function with 1 method)" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function my_randn(n, mu, sigma)\n", " return randn(n) .* sigma .+mu\n", "end" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10-element Array{Float64,1}:\n", " 48.96424536283605 \n", " 45.82637579794923 \n", " 52.22822347403981 \n", " 49.35333388905027 \n", " 48.71153119242729 \n", " 52.760595462606034\n", " 46.632272155091094\n", " 52.03787969109315 \n", " 53.18965634376912 \n", " 55.264539739277296" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_randn(10, 50, 3)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "using Pkg" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n", "\u001b[?25l\u001b[2K\u001b[?25h\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.1/Project.toml`\n", "\u001b[90m [no changes]\u001b[39m\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/environments/v1.1/Manifest.toml`\n", "\u001b[90m [no changes]\u001b[39m\n" ] } ], "source": [ "Pkg.add(\"StatsFuns\")" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Building\u001b[22m\u001b[39m SpecialFunctions → `~/.julia/packages/SpecialFunctions/ne2iw/deps/build.log`\n", "\u001b[32m\u001b[1m Building\u001b[22m\u001b[39m Rmath ───────────→ `~/.julia/packages/Rmath/BoBag/deps/build.log`\n" ] } ], "source": [ "Pkg.build(\"StatsFuns\")" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Info: Precompiling StatsFuns [4c63d2b9-4356-54db-8cca-17b64c39e42c]\n", "└ @ Base loading.jl:1186\n", "ERROR: LoadError: SystemError: opening file \"/Users/byeon/.julia/compiled/v1.1/SpecialFunctions/78gOt.ji\": Permission denied\n", "Stacktrace:\n", " [1] #systemerror#43(::Nothing, ::Function, ::String, ::Bool) at ./error.jl:134\n", " [2] systemerror at ./error.jl:134 [inlined]\n", " [3] #open#309(::Bool, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Function, ::String) at ./iostream.jl:289\n", " [4] #open at ./none:0 [inlined]\n", " [5] open(::String, ::String) at ./iostream.jl:345\n", " [6] stale_cachefile(::String, ::String) at ./loading.jl:1321\n", " [7] _require_search_from_serialized(::Base.PkgId, ::String) at ./loading.jl:693\n", " [8] _require(::Base.PkgId) at ./loading.jl:937\n", " [9] require(::Base.PkgId) at ./loading.jl:858\n", " [10] require(::Module, ::Symbol) at ./loading.jl:853\n", " [11] include at ./boot.jl:326 [inlined]\n", " [12] include_relative(::Module, ::String) at ./loading.jl:1038\n", " [13] include(::Module, ::String) at ./sysimg.jl:29\n", " [14] top-level scope at none:2\n", " [15] eval at ./boot.jl:328 [inlined]\n", " [16] eval(::Expr) at ./client.jl:404\n", " [17] top-level scope at ./none:3\n", "in expression starting at /Users/byeon/.julia/packages/StatsFuns/f6KKB/src/StatsFuns.jl:6\n" ] }, { "ename": "ErrorException", "evalue": "Failed to precompile StatsFuns [4c63d2b9-4356-54db-8cca-17b64c39e42c] to /Users/byeon/.julia/compiled/v1.1/StatsFuns/530lR.ji.", "output_type": "error", "traceback": [ "Failed to precompile StatsFuns [4c63d2b9-4356-54db-8cca-17b64c39e42c] to /Users/byeon/.julia/compiled/v1.1/StatsFuns/530lR.ji.", "", "Stacktrace:", " [1] error(::String) at ./error.jl:33", " [2] compilecache(::Base.PkgId, ::String) at ./loading.jl:1197", " [3] _require(::Base.PkgId) at ./loading.jl:960", " [4] require(::Base.PkgId) at ./loading.jl:858", " [5] require(::Module, ::Symbol) at ./loading.jl:853", " [6] top-level scope at In[123]:1" ] } ], "source": [ "using StatsFuns" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "mu = 50; sigma = 3;" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "ename": "UndefVarError", "evalue": "UndefVarError: normpdf not defined", "output_type": "error", "traceback": [ "UndefVarError: normpdf not defined", "", "Stacktrace:", " [1] top-level scope at In[125]:1" ] } ], "source": [ "normpdf(mu, sigma, 52)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 흠 에러가 난당" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### File input/output" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"data.txt\"" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_file_path = \"data.txt\"" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "IOStream()" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_file = open(data_file_path)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Array{String,1}:\n", " \"This is the first line\"\n", " \"this is the second\" \n", " \"thie is the third\" " ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = readlines(data_file)" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "close(data_file)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the first line\n", "this is the second\n", "thie is the third\n" ] } ], "source": [ "for line in data\n", " println(line)\n", "end" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [], "source": [ "output_file_path = \"results1.txt\"\n", "output_file = open(output_file_path, \"w\")\n", "print(output_file, \"Magic Johnson\")\n", "println(output_file, \"is a basketball player\")\n", "println(output_file, \"Michael jordan is also a basketball palyer\")\n", "close(output_file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 오 print찍으면 저장할 수 있네" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- open시 a 옵션을 주면 append 가능" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### CSV" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [], "source": [ "using DelimitedFiles" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Any[1 2 2 \"\"; 1 3 4.5 \"\"; … ; 2 4 3 \"\"; 3 4 5 \"\"], AbstractString[\"start node\" \" end node link length\" \"\" \"\"])" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "csv_file_name = \"data.csv\"\n", "csv_data = readdlm(csv_file_name, ',', header=true)" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×4 Array{AbstractString,2}:\n", " \"start node\" \" end node link length\" \"\" \"\"" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = csv_data[1]\n", "header = csv_data[2]" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1×4 Array{AbstractString,2}:\n", " \"start node\" \" end node link length\" \"\" \"\"" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "header" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×4 Array{Any,2}:\n", " 1 2 2 \"\"\n", " 1 3 4.5 \"\"\n", " 2 3 6 \"\"\n", " 2 4 3 \"\"\n", " 3 4 5 \"\"" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Int64,1}:\n", " 1\n", " 1\n", " 2\n", " 2\n", " 3" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "start_node = round.(Int, data[:,1])" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Int64,1}:\n", " 2\n", " 3\n", " 3\n", " 4\n", " 4" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "end_node = round.(Int, data[:, 2])" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5-element Array{Any,1}:\n", " 2 \n", " 4.5\n", " 6 \n", " 3 \n", " 5 " ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "link_length = data[:, 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Info: Recompiling stale cache file /Users/byeon/.julia/compiled/v1.1/PyPlot/oatAj.ji for PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]\n", "└ @ Base loading.jl:1184\n" ] } ], "source": [ "using PyPlot" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [], "source": [ "# Preparing a figure object\n", "fig = figure()\n", "\n", "# Data\n", "x = range(0, stop=2*pi, length=1000)\n", "y = sin.(3*x)\n", "\n", "# Plotting with linewidth and linestyle specified\n", "plot(x, y, color=\"blue\", linewidth=2.0, linestyle=\"--\")\n", "\n", "# Labeling the axes\n", "xlabel(L\"value of $x$\")\n", "ylabel(L\"\\sin(3x)\")\n", "\n", "# Title\n", "title(\"Test plotting\")\n", "\n", "# Save the figure as PNG and PDF\n", "savefig(\"plot1.png\")\n", "savefig(\"plot1.pdf\")\n", "\n", "# Close the figure object\n", "close(fig)" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [], "source": [ "lower_bound = [4.0, 4.2, 4.4, 4.8, 4.9, 4.95, 4.99, 5.00]\n", "upper_bound = [5.4, 5.3, 5.3, 5.2, 5.2, 5.15, 5.10, 5.05]\n", "iter = 1:8\n", "\n", "# Creating a new figure object\n", "fig = figure()\n", "\n", "# Plotting two datasets\n", "plot(iter, lower_bound, color=\"red\", linewidth=2.0, linestyle=\"-\",\n", " marker=\"o\", label=L\"Lower Bound $Z^k_L$\")\n", "plot(iter, upper_bound, color=\"blue\", linewidth=2.0, linestyle=\"-.\",\n", " marker=\"D\", label=L\"Upper Bound $Z^k_U$\")\n", "\n", "# Labeling axes\n", "xlabel(L\"iteration clock $k$\", fontsize=\"xx-large\")\n", "ylabel(\"objective function value\", fontsize=\"xx-large\")\n", "\n", "# Putting the legend and determining the location\n", "legend(loc=\"upper right\", fontsize=\"x-large\")\n", "\n", "# Add grid lines\n", "grid(color=\"#DDDDDD\", linestyle=\"-\", linewidth=1.0)\n", "tick_params(axis=\"both\", which=\"major\", labelsize=\"x-large\")\n", "\n", "# Title\n", "title(\"Lower and Upper Bounds\")\n", "\n", "# Save the figure as PNG and PDF\n", "savefig(\"plot2.png\")\n", "savefig(\"plot2.pdf\")\n", "\n", "# Closing the figure object\n", "close(fig)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Warning: `getindex(o::PyObject, s::Symbol)` is deprecated in favor of dot overloading (`getproperty`) so elements should now be accessed as e.g. `o.s` instead of `o[:s]`.\n", "│ caller = top-level scope at In[150]:7\n", "└ @ Core In[150]:7\n" ] } ], "source": [ "# Data\n", "data = randn(100) # Some Random Data\n", "nbins = 10 # Number of bins\n", "\n", "# Creating a new figure object\n", "fig = figure()\n", "\n", "# Histogram\n", "plt[:hist](data, nbins)\n", "\n", "# Title\n", "title(\"Histogram\")\n", "\n", "# Save the figure as PNG and PDF\n", "savefig(\"plot3.png\")\n", "savefig(\"plot3.pdf\")\n", "\n", "# Closing the figure object\n", "close(fig)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.1.1", "language": "julia", "name": "julia-1.1" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.1.1" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }