2016년 8월 2일 화요일

Julia 연습 7

In [ ]:
#
# 나는 julia 를 사랑하는가.
#
In [ ]:
# Functions
# 함수
In [1]:
# 함수는 이렇게 만든다.

function f(x, y)
    x + y
end
Out[1]:
f (generic function with 1 method)
In [2]:
# 한줄짜리도 함수를 만들 수 있다.


f(x, y) = x + y
Out[2]:
f (generic function with 1 method)
In [3]:
f(2, 3)
Out[3]:
5
In [4]:
# 함수는 일급객체(first-class object) 이므로,
# 변수에 할당할 수 있다.


g = f
Out[4]:
f (generic function with 1 method)
In [5]:
g(2, 3)
Out[5]:
5
In [6]:
# 함수이름에 쓰이는 글자에는 제한이 없다.

Σ(x,y) = x + y
Out[6]:
Σ (generic function with 1 method)
In [7]:
# 함수이름에 쓰이는 글자에는 제한이 없다.

더해라(x,y) = x + y
Out[7]:
더해라 (generic function with 1 method)
In [8]:
# 함수이름에 쓰이는 글자에는 제한이 없다.

합쳐라(x,y) = x + y
Out[8]:
합쳐라 (generic function with 1 method)
In [93]:
# 함수의 마지막 라인이 리턴값이다.
# 명시적으로 return 을 사용하여,
# 마지막 라인이 아닌것을 리턴할수있다.


function h(x,y)
    return x * y
    print( x + y)
    ( x + y)
end
Out[93]:
h (generic function with 1 method)
In [94]:
h(2, 3)
Out[94]:
6
In [17]:
# 명시적으로 리턴을 쓸 수 있다.


function hypot(x,y)
    x = abs(x)
    y = abs(y)
    if x > y
        r = y/x
        return x*sqrt(1+r*r)
    end
    if y == 0
        return zero(x)
    end
    r = x/y
    return y*sqrt(1+r*r)
end
Out[17]:
hypot (generic function with 1 method)
In [18]:
hypot(1,2)
Out[18]:
2.23606797749979
In [19]:
hypot(1,0)
Out[19]:
1.0
In [20]:
hypot(2,1)
Out[20]:
2.23606797749979
In [21]:
1 + 2 + 3
Out[21]:
6
In [22]:
+(1, 2, 3)
Out[22]:
6
In [24]:
# 연산자는 함수다.
# 함수는 일급객체이므로 할당 할 수 있다.


f1 = +
Out[24]:
+ (generic function with 171 methods)
In [26]:
f1(1, 2, 3)
Out[26]:
6
In [27]:
# anonymous function
# 무명 함수
# 이름없는 함수


x -> x^2 + 2x - 1
Out[27]:
(anonymous function)
In [28]:
# anonymous function
# 무명 함수
# 이름없는 함수


function (x)
    x^2 + 2x -1
end
Out[28]:
(anonymous function)
In [29]:
map(round, [1.2, 3.5, 1.7])
Out[29]:
3-element Array{Float64,1}:
 1.0
 4.0
 2.0
In [30]:
# 간편하게 무명함수를 즉시 만들어 쓰는 예.


map( x -> x^2 + 2x -1, [1, 3, -1])
Out[30]:
3-element Array{Int64,1}:
  2
 14
 -2
In [31]:
# 한개가 아니라, 여러개를 리턴할 수 있다.


function foo(a, b)
    a+b, a*b
end
Out[31]:
foo (generic function with 1 method)
In [32]:
foo(2, 3)
Out[32]:
(5,6)
In [33]:
# destructing
# 리턴값을 받아온다.


r1, r2 = foo(2, 3)
Out[33]:
(5,6)
In [34]:
r1
Out[34]:
5
In [35]:
r2
Out[35]:
6
In [36]:
# 여러개를 리턴.

function foo(a, b)
    return a+b, a*b
end
Out[36]:
foo (generic function with 1 method)
In [37]:
# Varargs Function
# variable number of arguments
# 가변인자 함수

bar(a, b, x...) = (a, b, x)
Out[37]:
bar (generic function with 1 method)
In [38]:
bar(1,2,3)
Out[38]:
(1,2,(3,))
In [39]:
typeof(ans)
Out[39]:
Tuple{Int64,Int64,Tuple{Int64}}
In [40]:
bar(1,2)
Out[40]:
(1,2,())
In [41]:
bar(1,2,3)
Out[41]:
(1,2,(3,))
In [42]:
bar(1,2,3,4)
Out[42]:
(1,2,(3,4))
In [43]:
bar(1,2,3,4,5,6)
Out[43]:
(1,2,(3,4,5,6))
In [ ]:
# 위에서,
# x 에는 인자로 넘어온 모든 값이 tuple 로 들어있다.
In [44]:
x = (3, 4)
Out[44]:
(3,4)
In [45]:
# 함수호출할때 ...를 쓰면,
# x 에 들어있는 것을 쪼개준다. (splice)


bar(1, 2, x...)
Out[45]:
(1,2,(3,4))
In [46]:
x = (2, 3, 4)
Out[46]:
(2,3,4)
In [47]:
bar(1, x...)
Out[47]:
(1,2,(3,4))
In [48]:
x = (1,2,3,4)
Out[48]:
(1,2,3,4)
In [49]:
bar(x...)
Out[49]:
(1,2,(3,4))
In [50]:
# tuple 이 아니어도, iterable object 이면 된다.

x = [3, 4]
Out[50]:
2-element Array{Int64,1}:
 3
 4
In [51]:
bar(1, 2, x...)
Out[51]:
(1,2,(3,4))
In [52]:
# tuple 이 아니어도, iterable object 이면 된다.

x = [1, 2, 3, 4]
Out[52]:
4-element Array{Int64,1}:
 1
 2
 3
 4
In [53]:
bar(x...)
Out[53]:
(1,2,(3,4))
In [54]:
# tuple 이 아니어도, iterable object 이면 된다.

x = "12345"
Out[54]:
"12345"
In [55]:
bar(x...)
Out[55]:
('1','2',('3','4','5'))
In [56]:
# tuple 이 아니어도, iterable object 이면 된다.

x = "밥은먹고다니냐"
Out[56]:
"밥은먹고다니냐"
In [57]:
bar(x...)
Out[57]:
('밥','은',('먹','고','다','니','냐'))
In [58]:
# 가변인자 함수가 이니어도,

baz(a, b) = a + b
Out[58]:
baz (generic function with 1 method)
In [59]:
args = [1, 2]
Out[59]:
2-element Array{Int64,1}:
 1
 2
In [60]:
baz(args...)
Out[60]:
3
In [61]:
args = [1,2,3]
Out[61]:
3-element Array{Int64,1}:
 1
 2
 3
In [62]:
# 인자 갯수가 다를때,
# 이런꼴을 보게된다.


baz(args...)
LoadError: MethodError: `baz` has no method matching baz(::Int64, ::Int64, ::Int64)
Closest candidates are:
  baz(::Any, ::Any)
while loading In[62], in expression starting on line 1
In [66]:
# Optional Arguments
# 선택적 인자
# 인자에 기본값을 지정할 수 있다.
# 기본값이 있으면, 생략가능하다.


function 파싱( 타입, 숫자, 밑수=10)
    ( 타입, 숫자, 밑수)
end
Out[66]:
파싱 (generic function with 2 methods)
In [67]:
파싱(Int, "12", 10)
Out[67]:
(Int64,"12",10)
In [68]:
파싱(Int, "12", 3)
Out[68]:
(Int64,"12",3)
In [69]:
파싱(Int, "12")
Out[69]:
(Int64,"12",10)
In [71]:
# Keyword Arguments
# 인자를 이름으로 부르기
# 인자에 이름 지정.


function plot( x, y ; style="solid", width=1, color="black")
    (x, y, style, width, color)
end
Out[71]:
plot (generic function with 1 method)
In [72]:
plot( 1, 2, width=2)
Out[72]:
(1,2,"solid",2,"black")
In [73]:
plot( 1, 2; width=3)
Out[73]:
(1,2,"solid",3,"black")
In [74]:
# keyword argument 의 타입을
# 명시적으로 지정할 수있다.
# :: 을 쓴다.


function plot2( ; width::Int64=1 )
    (width)
end
Out[74]:
plot2 (generic function with 1 method)
In [86]:
plot2( width = 3)
Out[86]:
3
In [87]:
plot2( ; (:width, 4))
Out[87]:
4
In [88]:
plot2( ; :width => 5)
Out[88]:
5
In [91]:
# Do-Block

# 함수를 인자로 넘길때.
# 그함수가 여러줄일때.
# 다음과 같이 하면, 코드가 이쁘지가 않다.


map( x -> begin
        if x < 0 && iseven(x)
            return 0
        elseif x == 0
            return 1
        else
            return x
        end
    end,
    [1, 2, 3])
Out[91]:
3-element Array{Int64,1}:
 1
 2
 3
In [92]:
# do 를 쓰면, 좀 낫다.


map([1, 2, 3]) do x
    if x < 0 && iseven(x)
        return 0
    elseif x == 0
        return 1
    else
        return x
    end
end
Out[92]:
3-element Array{Int64,1}:
 1
 2
 3

Firefly Algorithms

firefly algorithm 001 Firefly Algorithms ¶ 반딧불 알고리즘 번역 요약 ¶ References [1] X. S. Y...