2016년 8월 3일 수요일

Julia 연습 11

In [ ]:
#
# 나는 julia 를 사랑하는가
#
In [ ]:
# 함수는 같은 이름으로 여러개를 만들수있다.
# 버전별로 함수의 기능은 인자갯수에따라 다른 기능을 제공한다.
# 구현은 다르지만 같은이름의 함수는 같은 개념을 표현하게된다.
# 여러개의 function 중 하나의 정의를 method 라고 부른다.


# 함수를 호출했을때
# 여러개의 함수중 어떤것을 실행할것인가 선택하는 것을
# dispatch 라고 부른다.


# Julia 는 인자의 갯수와 인자의 타입에 기반하여 
# function의 method 를 선택한다.
# 

# method 를 선택할때 
# 함수의 일부 arguments 만 사용하지 않고
# 함수의 모든 arguments 를 사용하는것을 multiple dispatch 라고 부른다.
In [1]:
#
# parameter 의 type 을 선택적으로 제한할 수 있다.
#
# :: type-assertion operator

#f(x::Float64, y::Float64) = println("fun1"); 2x + y;


function f(x::Float64, y::Float64)
    println("fun1")
    2x + y;
end
Out[1]:
f (generic function with 1 method)
In [2]:
# 타입이 맞으면 기대한대로 동작한다.

f(2.0, 3.0)
fun1
Out[2]:
7.0
In [3]:
# 타입이 안맞으면 안된다.


f(2.0, 3)
LoadError: MethodError: `f` has no method matching f(::Float64, ::Int64)
Closest candidates are:
  f(::Float64, !Matched::Float64)
while loading In[3], in expression starting on line 4
In [4]:
# 더 일반화된 메소드

# f(x::Number, y::Number) = 2x - y

function f(x::Number, y::Number)
    println("func2")
    2x - y
end
Out[4]:
f (generic function with 2 methods)
In [5]:
# 이제는 된다.

f(2.0, 3)
func2
Out[5]:
1.0
In [6]:
f(2.0, 3.0)
fun1
Out[6]:
7.0
In [7]:
f
Out[7]:
f (generic function with 2 methods)
In [8]:
methods( f)
Out[8]:
2 methods for generic function f:
  • f(x::Float64, y::Float64) at In[1]:10
  • f(x::Number, y::Number) at In[4]:6
In [9]:
function f(x, y)
    println("밥은먹고다니냐")
end
Out[9]:
f (generic function with 3 methods)
In [11]:
methods( f)
Out[11]:
3 methods for generic function f:
  • f(x::Float64, y::Float64) at In[1]:10
  • f(x::Number, y::Number) at In[4]:6
  • f(x, y) at In[9]:2
In [12]:
f( "후아", 1)
밥은먹고다니냐
In [13]:
myappend{T}(v::Vector{T}, x::T) = [v..., x]
Out[13]:
myappend (generic function with 1 method)
In [14]:
myappend([1,2,3],4)
Out[14]:
4-element Array{Int64,1}:
 1
 2
 3
 4
In [15]:
myappend([1,2,3],4.0)
LoadError: MethodError: `myappend` has no method matching myappend(::Array{Int64,1}, ::Float64)
Closest candidates are:
  myappend{T}(::Array{T,1}, !Matched::T)
while loading In[15], in expression starting on line 1
In [16]:
myappend([1.0,2.0,3.0],4.0)
Out[16]:
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
In [17]:
myappend([1.0,2.0,3.0],4)
LoadError: MethodError: `myappend` has no method matching myappend(::Array{Float64,1}, ::Int64)
Closest candidates are:
  myappend{T}(::Array{T,1}, !Matched::T)
while loading In[17], in expression starting on line 1
In [18]:
mytypeof{T}(x::T) = T
Out[18]:
mytypeof (generic function with 1 method)
In [19]:
mytypeof( 1)
Out[19]:
Int64
In [20]:
mytypeof( 1.0)
Out[20]:
Float64
In [21]:
mytypeof2( x::T ) = T
LoadError: UndefVarError: T not defined
while loading In[21], in expression starting on line 1
In [23]:
mytypeof2( x::T ) = 1
LoadError: UndefVarError: T not defined
while loading In[23], in expression starting on line 1
In [24]:
mytypeof2( x ) = 1
Out[24]:
mytypeof2 (generic function with 1 method)
In [25]:
# 호출할수있는 객체를 함수자라고 부른다.
# callable objects are called functors.


# call overloading
Base.call( x::Number, arg) = x * arg

x = 7

# object call
x(10)
Out[25]:
70

Firefly Algorithms

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