Sorry for the double posting.
Actually I was only interested for "decimal" inputs (single and double
precision) is there a better way to express this instead of using Real?
thanks
On Sunday, December 27, 2015 at 6:09:30 PM UTC-2, Ismael Venegas Castelló
wrote:
>
> For calcF your variables:
>
> Variables:
> X::Array{Float64,1}
> F::F
> n::Int64
> y::Array{Float64,1}
> #s76::Int64
> i::Int64
> ##dims#7321::Tuple{Int64}
>
> and return value:
>
> return y::Array{Float64,1}
> end::Array{Float64,1}
>
> Are correctly inferred if I use floats so it seems fine.
>
> But if I use int's as arguments to main I get an InexactError, are you
> sure it should accept `T <: Real`? Maybe you just need Float64?
>
>
>
> El domingo, 27 de diciembre de 2015, 11:21:35 (UTC-6), alan souza escribió:
>>
>> Hi. I was wondering what is the correct way to pass a function as
>> argument preserving the return type information of this function
>>
>> For instance in the following code:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *function sinc{T<:Real}(x::T) return T((x != zero(T)
>> )?(sin(pi*x)/(pi*x)):(one(T)))endfunction calcF{T<:Real}(X::Array{T,1},
>> F::Function) n = length(X); @assert(n > 0) y = zeros(T, n) for
>> i in eachindex(X) y[i] = F(X[i]) end return yendfunction
>> calcSinc{T<:Real}(X::Array{T,1}) n = length(X); @assert(n > 0) y
>> = zeros(T, n) for i in eachindex(X) y[i] = sinc(X[i]) end
>> return yendfunction main{T<:Real}(min::T, max::T, d::T) X =
>> collect(min:d:max) calcF(X, sinc) calcSinc(X) @code_warntype
>> calcF(X, sinc) @time calcF(X, sinc) @code_warntype calcSinc(X)
>> @time calcSinc(X) YF = calcF(X, sinc) YS = calcSinc(X)end*
>>
>> Running this code with julia (versions 0.5+dev or 0.4.2) I got these
>> results:
>>
>>
>>
>>
>>
>>
>> * #s1 = GenSym(5) # /home/me/scratch/julia_fd/teste.jl, line
>> 10: GenSym(2) =
>> (F::F)((Base.arrayref)(X::Array{Float64,1},i::Int64)::Float64)::ANY
>> (Base.arrayset)(y::Array{Float64,1},(top(typeassert))((Base.convert)(Float64,GenSym(2))::ANY,Float64)::Float64,i::Int64)::Array{Float64,1}
>>
>> 5: unless
>> (Base.box)(Base.Bool,(Base.not_int)((Base.box)(Base.Bool,(Base.not_int)(#s1::Int64
>>
>> ===
>> (Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool))))
>>
>> goto 4 3: 0.080181 seconds (1.88 M allocations: 33.556 MB, 6.41% gc
>> time)*
>>
>> and for the calcSinc function:
>>
>>
>>
>>
>>
>>
>>
>> * #s1 = GenSym(5) # /home/me/scratch/julia_fd/teste.jl, line 20:
>> GenSym(2) =
>> (Main.sinc)((Base.arrayref)(X::Array{Float64,1},i::Int64)::Float64)::Float64
>>
>> (Base.arrayset)(y::Array{Float64,1},GenSym(2),i::Int64)::Array{Float64,1}
>>
>> 5: unless
>> (Base.box)(Base.Bool,(Base.not_int)((Base.box)(Base.Bool,(Base.not_int)(#s1::Int64
>>
>> ===
>> (Base.box)(Base.Int,(Base.add_int)((top(getfield))(GenSym(0),:stop)::Int64,1))::Bool))))
>>
>> goto 4 3: 0.031224 seconds (2 allocations: 4.794 MB)*
>> Is there a way to prevent the result of function to "evaluate" to ::Any
>> on the first case?
>>
>> thanks
>>
>>