2016년 8월 3일 수요일

Julia 연습 12

In [ ]:
#
# 나는 julia 를 사랑하는가
#
In [ ]:
# 새로운 객체를 만들어내는 함수를 생성자라고 부른다.
# Constructors
In [1]:
type Foo
    bar
    baz
end
In [2]:
foo = Foo( 1, 2)
Out[2]:
Foo(1,2)
In [3]:
foo.bar
Out[3]:
1
In [4]:
foo.baz
Out[4]:
2
In [5]:
# Outer Constructor

Foo(x) = Foo(x, x)
Out[5]:
Foo
In [6]:
foo = Foo(5)
Out[6]:
Foo(5,5)
In [7]:
Foo() = Foo(0)
Out[7]:
Foo
In [8]:
foo = Foo()
Out[8]:
Foo(0,0)
In [9]:
# Outer constructor
# Inner constructor
# 외부 생성자
# 내부 생성자

type OrderedPair
    x::Real
    y::Real
    
    OrderedPair(x, y) = x > y ? error("순서가 틀려먹었다") : new(x,y)
end
In [10]:
OrderedPair(1,2)
Out[10]:
OrderedPair(1,2)
In [11]:
OrderedPair(2,1)
LoadError: 순서가 틀려먹었다
while loading In[11], in expression starting on line 1

 in call at In[9]:10
In [12]:
# 불완전 초기화
# 자기참조 객체
# 재귀적 데이타 구조


type SelfReferential
    obj::SelfReferenctial
end
LoadError: UndefVarError: SelfReferenctial not defined
while loading In[12], in expression starting on line 3
In [14]:
type SelfReferential
    obj::SelfReferential
    
    SelfReferential() = (x = new(); x.obj = x)
end
In [15]:
x = SelfReferential()
Out[15]:
SelfReferential(SelfReferential(#= circular reference =#))
In [16]:
is(x, x)
Out[16]:
true
In [17]:
is(x, x.obj)
Out[17]:
true
In [18]:
is(x, x.obj.obj)
Out[18]:
true
In [19]:
type Incomplete
    xx
    Incomplete() = new()
end
In [20]:
z = Incomplete()
Out[20]:
Incomplete(#undef)
In [21]:
z.xx
LoadError: UndefRefError: access to undefined reference
while loading In[21], in expression starting on line 1
In [22]:
type HasPlain
    n::Int
    HasPlain() = new()
end
In [23]:
h = HasPlain()
Out[23]:
HasPlain(140045355964904)
In [24]:
h.n
Out[24]:
140045355964904
In [25]:
# Parametric Constructor


type Point{T <: Real}
    x::T
    y::T
end
In [26]:
Point(1,2)
Out[26]:
Point{Int64}(1,2)
In [27]:
Point(1.0, 2.5)
Out[27]:
Point{Float64}(1.0,2.5)
In [28]:
Point(1, 2.5)
LoadError: MethodError: `convert` has no method matching convert(::Type{Point{T<:Real}}, ::Int64, ::Float64)
This may have arisen from a call to the constructor Point{T<:Real}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  Point{T<:Real}(::T<:Real, !Matched::T<:Real)
  call{T}(::Type{T}, ::Any)
  convert{T}(::Type{T}, !Matched::T)
while loading In[28], in expression starting on line 1

 in call at essentials.jl:57
In [29]:
Point{Int64}(1.0, 2.5)
LoadError: InexactError()
while loading In[29], in expression starting on line 1

 in call at In[25]:2
In [30]:
Point{Float64}(1.0,2.5)
Out[30]:
Point{Float64}(1.0,2.5)
In [31]:
Point{Float64}(1,2)
Out[31]:
Point{Float64}(1.0,2.0)
In [32]:
x = 1
Out[32]:
1
In [33]:
typeof(x)
Out[33]:
Int64
In [34]:
y = convert(Float64, x)
Out[34]:
1.0
In [35]:
typeof(y)
Out[35]:
Float64
In [36]:
x2, y2 = promote( x, y)
Out[36]:
(1.0,1.0)
In [37]:
( typeof(x2), typeof(y2))
Out[37]:
(Float64,Float64)
In [38]:
typeof(ans)
Out[38]:
Tuple{DataType,DataType}
In [39]:
s = widen(Int32)
Out[39]:
Int64
In [40]:
typeof(s)
Out[40]:
DataType

Firefly Algorithms

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