2016년 8월 3일 수요일

Julia 연습 10

In [ ]:
#
# 나는 julia 를 사랑하는가
#
In [ ]:
# Types
In [ ]:
# Type Declaration
# 타입 선언

# :: "is an instance of"
In [1]:
# 변수, 함수인자는 물론,
# 표현식에도 타입선언을 할수있다.

# 타입이 안맞을경우 이런꼴을 보게된다.


(1+2)::AbstractFloat
LoadError: TypeError: typeassert: expected AbstractFloat, got Int64
while loading In[1], in expression starting on line 1
In [2]:
# 타입이 맞을 경우

(1+2)::Int
Out[2]:
3
In [4]:
#변수에 타입선언.


function foo()
    x::Int8 = 100
    x
end


typeof( foo())
Out[4]:
Int8
In [6]:
let
    x::Int8 = 0
    local y::Int8
    z::Int8 = 0
end
Out[6]:
0
In [9]:
# <: "is subtype of"


let
    Integer <: Number
end
Out[9]:
false
In [10]:
let
    Integer <: AbstractFloat
end
Out[10]:
false
In [13]:
# <: "is subtype of"


Int8 <: Signed
Out[13]:
true
In [ ]:
# Julia 에서 모든 값들은 객체(object)다.

# 객체의 멤버함수는 없다.
# object 에 속해있는 member function 은 없다.

# 멤버함수가 없는것이 
# 이득을 가져온다.

# Julia 는 multiple dispatch 가 있으므로
# 객체에 한정된 멤버함수는 필요치 않다.
In [1]:
# composite type
# structure

# C++ 언어에 있는 구조체(struct) 를 만들어보자.
# type 키워드를 쓴다. struct 가 아니다.


type Foo
    bar
    baz::Int
    qux::Float64
end
In [2]:
foo = Foo("Hello, world.", 23, 1.5)
Out[2]:
Foo("Hello, world.",23,1.5)
In [3]:
typeof( foo)
Out[3]:
Foo
In [4]:
fieldnames( foo)
Out[4]:
3-element Array{Symbol,1}:
 :bar
 :baz
 :qux
In [5]:
fieldnames( Foo)
Out[5]:
3-element Array{Symbol,1}:
 :bar
 :baz
 :qux
In [6]:
foo.bar
Out[6]:
"Hello, world."
In [7]:
foo.baz
Out[7]:
23
In [8]:
foo.qux
Out[8]:
1.5
In [9]:
foo.qux = 2
Out[9]:
2.0
In [10]:
foo.bar = 1//2
Out[10]:
1//2
In [11]:
# filed 가 없는 composite type 은 singletone 이다.
# 오직 하나의 인스턴스만 존재할 수 있다.

type NoFields
end
In [12]:
is( NoFields(), NoFields())
Out[12]:
true
In [13]:
# immutable composite type
# 변경불가 구조체

# type 이 아니라, immutable 을 쓴다.

immutable Complex
    real::Float64
    imag::Float64
end
In [14]:
typeof(Real)
Out[14]:
DataType
In [15]:
typeof(Int)
Out[15]:
DataType
In [18]:
# type union

IntOrString = Union{Int, AbstractString}
Out[18]:
Union{AbstractString,Int64}
In [19]:
1 :: IntOrString
Out[19]:
1
In [20]:
"Hello" :: IntOrString
Out[20]:
"Hello"
In [21]:
1.0 :: IntOrString
LoadError: TypeError: typeassert: expected Union{AbstractString,Int64}, got Float64
while loading In[21], in expression starting on line 1
In [22]:
typeof(Union)
Out[22]:
DataType
In [23]:
fieldnames(Union)
Out[23]:
1-element Array{Symbol,1}:
 :types
In [25]:
fieldnames(IntOrString)
Out[25]:
1-element Array{Symbol,1}:
 :types
In [26]:
IntOrString.types
Out[26]:
svec(Int64,AbstractString)
In [27]:
# Parametric Types
# Parametric Composite Types

type Point{T}
    x::T
    y::T
end
In [28]:
typeof(Point)
Out[28]:
DataType
In [29]:
fieldnames(Point)
Out[29]:
2-element Array{Symbol,1}:
 :x
 :y
In [30]:
t1 = Point{Float64}
Out[30]:
Point{Float64}
In [31]:
typeof( t1)
Out[31]:
DataType
In [32]:
fieldnames( t1)
Out[32]:
2-element Array{Symbol,1}:
 :x
 :y
In [33]:
t2 = Point{AbstractString}
Out[33]:
Point{AbstractString}
In [34]:
typeof( t2)
Out[34]:
DataType
In [35]:
Point
Out[35]:
Point{T}
In [36]:
typeof( Point)
Out[36]:
DataType
In [37]:
Point{Float64} <: Point
Out[37]:
true
In [38]:
Point{AbstractString} <: Point
Out[38]:
true
In [39]:
IntOrString <: Point
Out[39]:
false
In [40]:
function norm(p::Point{Real})
    sqrt( p.x^2 + p.y^2)
end
Out[40]:
norm (generic function with 1 method)
In [41]:
function norm{ T <: Real }( p::Point{T} )
    sqrt( p.x^2 + p.y^2)
end
Out[41]:
norm (generic function with 2 methods)
In [42]:
Point{Float64}(1.0, 2.0)
Out[42]:
Point{Float64}(1.0,2.0)
In [43]:
typeof( ans)
Out[43]:
Point{Float64}
In [44]:
# 인자의 타입으로부터 유추할수 있으므로
# 이렇게 쓸수있다.
# 컴파일러는 안다.
# 이것이 
# Point{Float64}(1.0, 2.0) 라는것을....


Point( 1.0, 2.0)
Out[44]:
Point{Float64}(1.0,2.0)
In [45]:
typeof( ans)
Out[45]:
Point{Float64}
In [46]:
# 컴파일러는 안다.
# 이것이 
# Point{Int64}(1,2) 라는것을....


Point(1, 2)
Out[46]:
Point{Int64}(1,2)
In [47]:
typeof( ans)
Out[47]:
Point{Int64}
In [1]:
# Parametric Abstract Type

abstract Pointy{T}
In [49]:
Pointy{Int64} <: Pointy
Out[49]:
true
In [50]:
Pointy{1} <: Pointy
Out[50]:
true
In [51]:
Pointy{Float64} <: Pointy{Real}
Out[51]:
false
In [52]:
Pointy{Real} <: Pointy{Float64}
Out[52]:
false
In [2]:
type Point{T} <: Pointy{T}
    x::T
    y::T
end
In [3]:
Point{Float64} <: Pointy{Float64}
Out[3]:
true
In [4]:
Point{Real} <: Pointy{Real}
Out[4]:
true
In [5]:
Point{AbstractString} <: Pointy{AbstractString}
Out[5]:
true
In [6]:
Point{Float64} <: Pointy{Real}
Out[6]:
false
In [7]:
type DiagPoint{T} <: Pointy{T}
    x::T
end
In [1]:
# parameter T 의 타입을 제한할수있다.


abstract Pointy{ T <: Real}
In [2]:
typeof((1, "foo", 2.5))
Out[2]:
Tuple{Int64,ASCIIString,Float64}
In [3]:
typeof( Ptr)
Out[3]:
DataType
In [4]:
# Parametric Bits Types

bitstype 32 my_Ptr{T}
In [1]:
bitstype 64 my_Ptr{T}
In [2]:
Ptr{Float64} <: Ptr
Out[2]:
true
In [3]:
Ptr{Float64} <: Ptr
Out[3]:
true
In [4]:
UInt
Out[4]:
UInt64
In [5]:
typeof( UInt)
Out[5]:
DataType
In [7]:
# type 은 object 이다.
In [8]:
# UInt 가 Integer 의 subtype 이냐
# Integer 가 UInt 의 supertype 이냐.


UInt <: Integer
Out[8]:
true
In [9]:
isa( 1, Int)
Out[9]:
true
In [10]:
isa( 1, AbstractFloat)
Out[10]:
false
In [11]:
# object 는 type 을 가진다.
# type 은 object 이다.
# 따라서, type 은 type 을 가진다.

typeof( Rational)
Out[11]:
DataType
In [13]:
typeof( Union{Real, Float64, Rational})
Out[13]:
DataType
In [14]:
typeof( Union{Real, ASCIIString})
Out[14]:
Union
In [15]:
typeof(Union)
Out[15]:
DataType
In [16]:
typeof( DataType)
Out[16]:
DataType
In [17]:
super(UInt)
Out[17]:
Unsigned
In [18]:
super(Unsigned)
Out[18]:
Integer
In [20]:
super(Integer)
Out[20]:
Real
In [21]:
super(Real)
Out[21]:
Number
In [22]:
super(Number)
Out[22]:
Any
In [24]:
super(Any)
Out[24]:
Any
In [25]:
super(Any)
Out[25]:
Any
In [26]:
super( Float64)
Out[26]:
AbstractFloat
In [27]:
super(AbstractFloat)
Out[27]:
Real
In [28]:
super(Real)
Out[28]:
Number
In [29]:
super(Real)
Out[29]:
Number
In [30]:
super(Number)
Out[30]:
Any
In [31]:
super(Any)
Out[31]:
Any
In [32]:
1 != 1
Out[32]:
false
In [33]:
1 == 1
Out[33]:
true
In [9]:
# 특정타입의 상속 가계도를 알아보자.
# 어디까지 올라가는지 한번 보자.


let

    sub_type = Float64

    while true
    
        println( sub_type)

        super_type = super( sub_type)
    
        if( super_type == sub_type)
            break
        end

        sub_type = super_type
    end

end
Float64
AbstractFloat
Real
Number
Any
In [10]:
# Any 가 끝인가.
In [11]:
Any == Any
Out[11]:
true
In [ ]:
# Value types
In [12]:
# 이런 코드를 만들어 냈었지.


function firstlast(b::Bool)
    return b ? "First" : "Last"
end

println(firstlast(true))
First
In [15]:
# 조건문 평가를 런타임이 아니라
# 컴파일타임에 수행하도록 할수가 있지.

firstlast(::Type{Val{true}}) = "First"
firstlast(::Type{Val{false}}) = "Last"

println(firstlast(Val{true}))
First
In [17]:
# 모든 파라메터는 Val 을 통해서 넘어간다.

function f1( a)
    println( a)
end
Out[17]:
f1 (generic function with 1 method)
In [37]:
f1( Val{1})
Val{1}
In [20]:
typeof( Val{1})
Out[20]:
DataType
In [21]:
fieldnames( Val{1})
Out[21]:
0-element Array{Symbol,1}
In [22]:
Val{1}()
Out[22]:
Val{1}()
In [23]:
typeof( Val{1}())
Out[23]:
Val{1}
In [24]:
println( Val{1}())
Val{1}()
In [25]:
x1 = Nullable{Int64}()
Out[25]:
Nullable{Int64}()
In [26]:
x2 = Nullable{Float64}
Out[26]:
Nullable{Float64}
In [27]:
x3 = Nullable{Vector{Int64}}()
Out[27]:
Nullable{Array{Int64,1}}()
In [28]:
isnull(Nullable{Float64}())
Out[28]:
true
In [29]:
isnull(Nullable(0.0))
Out[29]:
false
In [30]:
get(Nullable{Float64}())
LoadError: NullException()
while loading In[30], in expression starting on line 1

 in get at nullable.jl:32
In [31]:
get(Nullable(1.0))
Out[31]:
1.0
In [32]:
get(Nullable{Float64}(), 0)
Out[32]:
0.0
In [33]:
get(Nullable(1.0), 0)
Out[33]:
1.0

Firefly Algorithms

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