In [1]:
x =42
Out[1]:
In [2]:
typeof(x)
Out[2]:
In [3]:
typeof("a")
Out[3]:
In [4]:
typeof('a')
Out[4]:
In [5]:
100 + "100"
In [6]:
*("100",100)
In [7]:
*("foo","bar")
Out[7]:
In [8]:
+("foo","bar")
In [9]:
@which *(100, 100)
Out[9]:
In [10]:
@which *("foo", "bar")
Out[10]:
In [11]:
string("foo","bar")
Out[11]:
In [12]:
isfinite(1.0) #有限なら真
Out[12]:
In [13]:
isfinite(Inf)
Out[13]:
In [14]:
methods(isfinite)
Out[14]:
In [15]:
Float64 <: Real #部分集合かどうか
Out[15]:
In [16]:
Complex64 <: Real
Out[16]:
In [17]:
Complex64 <: Number
Out[17]:
In [18]:
Number <: Any #Anyが一番上位のタイプ
Out[18]:
In [19]:
super(super(super( super(Float64))))
Out[19]:
In [20]:
import Base.+
+(x::Integer, y::ASCIIString) = x + parse(Int, y) #parse()は第二引数を第一引数に変換
Out[20]:
In [21]:
100 + "20"
Out[21]:
In [22]:
function f(x)
println("Generic function invoked")
end
function f(x::Number)
println("Number method invoked")
end
function f(x::Integer)
println("Integer method invoked")
end
Out[22]:
In [23]:
f(3)
In [24]:
f(1.5)
In [25]:
f("foo")
In [26]:
Int == Int64
Out[26]:
In [27]:
Int64 == Integer
Out[27]:
In [28]:
Int == Int32
Out[28]:
In [29]:
Int <: Integer
Out[29]:
In [30]:
type Foo end
In [31]:
foo = Foo()
Out[31]:
In [32]:
typeof(foo)
Out[32]:
In [33]:
type Test1 end
In [34]:
t1 =Test1()
t2 = Test1()
Out[34]:
In [35]:
t1+t2
In [36]:
+(x:: Test1,y::Test1)="Test1"
Out[36]:
In [37]:
t1+t2
Out[37]:
In [38]:
type Test2 end
In [39]:
s1 = Test2(2)
s2 = Test2(3)
In [40]:
s1+s2
In [41]:
type AR1
a
b
sigma
phi
end
In [42]:
using Distributions
In [43]:
m = AR1(0.9, 1, 1, Beta(5, 5))
Out[43]:
In [44]:
m.a
Out[44]:
In [45]:
m.phi
Out[45]:
In [46]:
typeof(m.phi)
Out[46]:
In [47]:
immutable AR2
a
b
sigma
phi
end
In [48]:
n = AR2(0.9, 1, 1, Beta(5, 5))
Out[48]:
In [49]:
n.phi=Exponential(0.5)
In [50]:
m.phi=Exponential(0.5)
Out[50]:
In [51]:
type AR3
a::Real
b::Real
sigma::Real
phi::Distribution
end
In [52]:
l = AR3(0.9, 1, "foo", Beta(5, 5))
In [53]:
typeof([10, 20, 30])
Out[53]:
In [54]:
type FooBar{T} #中身のタイプがどっちも同じでなければならない
foo::T
bar::T
end
In [55]:
fb = FooBar(1, 2)
Out[55]:
In [56]:
fb = FooBar(1, 2.0)
In [1]:
function sum_float_array(x::Array{Float64, 1})
sum = 0.0
for i in 1:length(x)
sum += x[i]
end
return sum
end
Out[1]:
In [2]:
x = linspace(0,1,1e6)
Out[2]:
In [3]:
x = collect(x)
Out[3]:
In [4]:
@time sum_float_array(x)
Out[4]:
In [8]:
pi
Out[8]:
In [9]:
sin(pi)
Out[9]:
In [13]:
using Distributions
In [14]:
type AR1
a::Real
b::Real
sigma::Real
phi::Distribution
end
In [16]:
sin(pi/2)
Out[16]:
In [17]:
function simulate(m::AR1, n::Integer, x0::Real)
X = Array(Float64, n)
X[1] = x0
for t in 1:(n-1)
X[t+1] = m.a * X[t] + m.b + rand(m.phi)
end
return X
end
Out[17]:
In [18]:
z=[1,2,3]
Out[18]:
In [19]:
sum(z.^2)
Out[19]:
In [20]:
using PyPlot
In [21]:
plot(X)
Ex02
In [12]:
function f(k::Int64)
r = randn(k)
return sum(r.^2)
end
function f(k_vec::Array)
m = size(k_vec,1)
a = Array(Float64,m)
for i in 1:m
r = randn(k_vec[i])
a[i] = sum(r.^2)
end
return a
end
Out[12]:
In [13]:
f(3)
Out[13]:
In [14]:
f([2, 4, 6])
Out[14]:
In [ ]: