2017-03-21 32 views
7

ここではUnicodeのオペレータに失敗した簡単な例です:型推論は

odot(A::Matrix, B::Matrix) = A * B 

# using unicode alias ⊙ 
odot(A::Matrix, B::Vector) = reshape(A ⊙ reshape(B,size(A)), length(B)) 
⊙ = odot 

julia> @code_warntype rand(3,3) ⊙ rand(9) 
Variables: 
    #self#::#odot 
    A::Array{Float64,2} 
    B::Array{Float64,1} 

Body: 
    begin 
     return (Main.reshape)((A::Array{Float64,2} ⊙ $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,1}, ::Tuple{Int64,Int64}), :(Main.reshape), :(B), :((Core.tuple)((Base.arraysize)(A,1)::Int64,(Base.arraysize)(A,2)::Int64)::Tuple{Int64,Int64}))))::Any,(Base.arraylen)(B::Array{Float64,1})::Int64)::Any 
    end::Any 

# using odot 
odot(A::Matrix, B::Vector) = reshape(odot(A,reshape(B,size(A))), length(B)) 

julia> @code_warntype rand(3,3) ⊙ rand(9) 
Variables: 
    #self#::#odot 
    A::Array{Float64,2} 
    B::Array{Float64,1} 
    TS::Type{Float64} 

Body: 
    begin 
     SSAValue(0) = $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,1}, ::Tuple{Int64,Int64}), :(Main.reshape), :(B), :((Core.tuple)((Base.arraysize)(A,1)::Int64,(Base.arraysize)(A,2)::Int64)::Tuple{Int64,Int64}))) 
     # meta: location REPL[1] odot 1 
     # meta: location linalg/matmul.jl * 128 
     TS::Type{Float64} = $(QuoteNode(Float64)) # line 129: 
     SSAValue(1) = (Core.tuple)((Base.arraysize)(A::Array{Float64,2},1)::Int64,(Base.arraysize)(SSAValue(0),2)::Int64)::Tuple{Int64,Int64} 
     SSAValue(2) = (Core.ccall)(:jl_new_array,(Core.apply_type)(Core.Array,Float64,2)::Type{Array{Float64,2}},(Core.svec)(Core.Any,Core.Any)::SimpleVector,Array{Float64,2},0,SSAValue(1),0)::Array{Float64,2} 
     # meta: pop location 
     # meta: pop location 
     SSAValue(4) = $(Expr(:invoke, LambdaInfo for gemm_wrapper!(::Array{Float64,2}, ::Char, ::Char, ::Array{Float64,2}, ::Array{Float64,2}), :(Base.LinAlg.gemm_wrapper!), SSAValue(2), 'N', 'N', :(A), SSAValue(0))) 
     SSAValue(3) = (Core.tuple)((Base.arraylen)(B::Array{Float64,1})::Int64)::Tuple{Int64} 
     return $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,2}, ::Tuple{Int64}), :(Base.reshape), SSAValue(4), SSAValue(3))) 
    end::Array{Float64,1} 

型推論は、最初のケースではユニコード⊙に失敗した理由を、私は思ったんだけど。

+4

'const⊙= odot'? –

+0

@ChrisRackauckasありがとう!答えを書いてください。私はその質問に答えます。 – Gnimuc

+0

ユニコード文字を直接使用して関数を定義できることに注意してください: '⊙(A :: Matrix、B :: Matrix)= A * B'。 – MBaz

答えて

5

変数形式で関数を定義するときは、正しい型情報をグローバル変数として持つために定数として宣言する必要があります。

const ⊙ = odot