In [1]:
x =42


Out[1]:
42

In [2]:
typeof(x)


Out[2]:
Int64

In [3]:
typeof("a")


Out[3]:
ASCIIString

In [4]:
typeof('a')


Out[4]:
Char

In [5]:
100 + "100"


LoadError: MethodError: `+` has no method matching +(::Int64, ::ASCIIString)
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...)
  +(::Int64, !Matched::Int64)
  +(::Integer, !Matched::Ptr{T})
  ...
while loading In[5], in expression starting on line 1

In [6]:
*("100",100)


LoadError: MethodError: `*` has no method matching *(::ASCIIString, ::Int64)
Closest candidates are:
  *(::Any, ::Any, !Matched::Any, !Matched::Any...)
  *{T<:Number}(!Matched::Bool, ::T<:Number)
  *(!Matched::Complex{Bool}, ::Real)
  ...
while loading In[6], in expression starting on line 1

In [7]:
*("foo","bar")


Out[7]:
"foobar"

In [8]:
+("foo","bar")


LoadError: MethodError: `+` has no method matching +(::ASCIIString, ::ASCIIString)
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...)
while loading In[8], in expression starting on line 1

In [9]:
@which *(100, 100)


Out[9]:
*(x::Int64, y::Int64) at int.jl:19

In [10]:
@which *("foo", "bar")


Out[10]:
*(s1::AbstractString, ss::AbstractString...) at strings/basic.jl:50

In [11]:
string("foo","bar")


Out[11]:
"foobar"

In [12]:
isfinite(1.0) #有限なら真


Out[12]:
true

In [13]:
isfinite(Inf)


Out[13]:
false

In [14]:
methods(isfinite)


Out[14]:
11 methods for generic function isfinite:

In [15]:
Float64 <: Real #部分集合かどうか


Out[15]:
true

In [16]:
Complex64 <: Real


Out[16]:
false

In [17]:
Complex64 <: Number


Out[17]:
true

In [18]:
Number <: Any #Anyが一番上位のタイプ


Out[18]:
true

In [19]:
super(super(super( super(Float64))))


Out[19]:
Any

In [20]:
import Base.+
+(x::Integer, y::ASCIIString) = x + parse(Int, y) #parse()は第二引数を第一引数に変換


Out[20]:
+ (generic function with 172 methods)

In [21]:
100 + "20"


Out[21]:
120

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]:
f (generic function with 3 methods)

In [23]:
f(3)


Integer method invoked

In [24]:
f(1.5)


Number method invoked

In [25]:
f("foo")


Generic function invoked

In [26]:
Int == Int64


Out[26]:
true

In [27]:
Int64 == Integer


Out[27]:
false

In [28]:
Int == Int32


Out[28]:
false

In [29]:
Int <: Integer


Out[29]:
true

In [30]:
type Foo end

In [31]:
foo = Foo()


Out[31]:
Foo()

In [32]:
typeof(foo)


Out[32]:
Foo

In [33]:
type Test1 end

In [34]:
t1 =Test1()
t2 = Test1()


Out[34]:
Test1()

In [35]:
t1+t2


LoadError: MethodError: `+` has no method matching +(::Test1, ::Test1)
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...)
while loading In[35], in expression starting on line 1

In [36]:
+(x:: Test1,y::Test1)="Test1"


Out[36]:
+ (generic function with 173 methods)

In [37]:
t1+t2


Out[37]:
"Test1"

In [38]:
type Test2 end

In [39]:
s1 = Test2(2)
s2 = Test2(3)


LoadError: MethodError: `convert` has no method matching convert(::Type{Test2}, ::Int64)
This may have arisen from a call to the constructor Test2(...),
since type constructors fall back to convert methods.
Closest candidates are:
  call{T}(::Type{T}, ::Any)
  convert{T}(::Type{T}, !Matched::T)
  Test2()
while loading In[39], in expression starting on line 1

 in call at essentials.jl:56

In [40]:
s1+s2


LoadError: UndefVarError: s1 not defined
while loading In[40], in expression starting on line 1

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]:
AR1(0.9,1,1,Distributions.Beta(α=5.0, β=5.0))

In [44]:
m.a


Out[44]:
0.9

In [45]:
m.phi


Out[45]:
Distributions.Beta(α=5.0, β=5.0)

In [46]:
typeof(m.phi)


Out[46]:
Distributions.Beta

In [47]:
immutable AR2
    a
    b
    sigma
    phi
end

In [48]:
n = AR2(0.9, 1, 1, Beta(5, 5))


Out[48]:
AR2(0.9,1,1,Distributions.Beta(α=5.0, β=5.0))

In [49]:
n.phi=Exponential(0.5)


LoadError: type AR2 is immutable
while loading In[49], in expression starting on line 1

In [50]:
m.phi=Exponential(0.5)


Out[50]:
Distributions.Exponential(θ=0.5)

In [51]:
type AR3
    a::Real
    b::Real
    sigma::Real
    phi::Distribution
end

In [52]:
l = AR3(0.9, 1, "foo", Beta(5, 5))


LoadError: MethodError: `convert` has no method matching convert(::Type{Real}, ::ASCIIString)
This may have arisen from a call to the constructor Real(...),
since type constructors fall back to convert methods.
Closest candidates are:
  call{T}(::Type{T}, ::Any)
  convert{T<:Real}(::Type{T<:Real}, !Matched::Complex{T<:Real})
  convert{T<:Number}(::Type{T<:Number}, !Matched::Char)
  ...
while loading In[52], in expression starting on line 1

 in call at In[51]:2

In [53]:
typeof([10, 20, 30])


Out[53]:
Array{Int64,1}

In [54]:
type FooBar{T}  #中身のタイプがどっちも同じでなければならない
    foo::T
    bar::T
end

In [55]:
fb = FooBar(1, 2)


Out[55]:
FooBar{Int64}(1,2)

In [56]:
fb = FooBar(1, 2.0)


LoadError: MethodError: `convert` has no method matching convert(::Type{FooBar{T}}, ::Int64, ::Float64)
This may have arisen from a call to the constructor FooBar{T}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  FooBar{T}(::T, !Matched::T)
  call{T}(::Type{T}, ::Any)
  convert{T}(::Type{T}, !Matched::T)
while loading In[56], in expression starting on line 1

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]:
sum_float_array (generic function with 1 method)

In [2]:
x = linspace(0,1,1e6)


Out[2]:
linspace(0.0,1.0,1000000)

In [3]:
x = collect(x)


Out[3]:
1000000-element Array{Float64,1}:
 0.0       
 1.0e-6    
 2.0e-6    
 3.0e-6    
 4.0e-6    
 5.00001e-6
 6.00001e-6
 7.00001e-6
 8.00001e-6
 9.00001e-6
 1.0e-5    
 1.1e-5    
 1.2e-5    
 ⋮         
 0.999989  
 0.99999   
 0.999991  
 0.999992  
 0.999993  
 0.999994  
 0.999995  
 0.999996  
 0.999997  
 0.999998  
 0.999999  
 1.0       

In [4]:
@time sum_float_array(x)


  0.005592 seconds (2.04 k allocations: 106.380 KB)
Out[4]:
499999.9999999796

In [8]:
pi


Out[8]:
π = 3.1415926535897...

In [9]:
sin(pi)


Out[9]:
1.2246467991473532e-16

In [13]:
using Distributions

In [14]:
type AR1
    a::Real
    b::Real
    sigma::Real
    phi::Distribution
end

In [16]:
sin(pi/2)


Out[16]:
1.0

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]:
simulate (generic function with 1 method)

In [18]:
z=[1,2,3]


Out[18]:
3-element Array{Int64,1}:
 1
 2
 3

In [19]:
sum(z.^2)


Out[19]:
14

In [20]:
using PyPlot

In [21]:
plot(X)


LoadError: UndefVarError: X not defined
while loading In[21], in expression starting on line 1

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]:
f (generic function with 3 methods)

In [13]:
f(3)


Out[13]:
1.4097214699880847

In [14]:
f([2, 4, 6])


Out[14]:
3-element Array{Float64,1}:
 2.27939
 2.87376
 2.74942

In [ ]: