Hi Aleksandr,
Some comments below...
On Saturday, November 28, 2015 at 4:38:41 PM UTC+8, Aleksandr Mikheev wrote:
>
> 1. I still don't understand what exactly parametric types do. I mean what
> is the reason to use
>
> type A{Int64}
> A1::Int64
> A2::Int64
> end
>
>
> instead of
>
> type A
> A1::Int64
> A2::Int64
> end
>
>
First, I can't think of any reason you would ever use the former. The
purpose of parametric types, as I understand, is to help with dispatching
so you would do something like this instead:
type A{T<:Integer}
A1::T
A2::T
end
This function will work for any integer type, e.g. Int64, UInt64, etc.
Your second type will work,
type A
A1::Int64
A2::Int64
end
but is very specific to Int64. It will not work with any other integer type.
> 2. How should I use types like Int16 or Float16 correctly? I have some
> function
>
> GeometryFunc(L::Int16,phi::Float16,distribution::Array{Float16,2})
>
>
> But when I try to input something like this:
>
> GeometryFunc(100, 0.3, [0.95 1.01 1.03; 0.25 0.5 0.25])
>
>
> I have got the error.
>
> ERROR: MethodError: 'GeometryFunc' has no method match no method matching
>> GeometryFunc(::Int64, ::Float64, ::Array{Float64,2})
>
>
This is related to your question 1, i.e. your function is defined for those
exact types and will not work for other types.
The function "typeof" is helpful.
julia> typeof(100)
Int64
julia> typeof(0.3)
Float64
julia> typeof([0.95 1.01 1.03; 0.25 0.5 0.25])
Array{Float64,2}
This is why you get the error:
ERROR: MethodError: 'GeometryFunc' has no method match no method matching
GeometryFunc(::Int64, ::Float64, ::Array{Float64,2})
Your GeometryFunc is looking for
GeometryFunc(::Int16, ::Float16, ::Array{Float16,2})
so there is no match.
If you REALLY want your function to be so specific that it only works with
those specific types, you'll need to convert the inputs before calling it,
e.g.
GeometryFunc(Int16(100), Float16(0.3), Array{Float16,2}([0.95 1.01 1.03;
0.25 0.5 0.25]))
Alternatively, you can make your function less specific using parameters,
e.g.
GeometryFunc{T<:Integer,U<:AbstractFloat}(L::T,phi::U,distribution::Array{U,
2})
> 3. How can I delete elements with specific indexes in the array? Or how
> can I delete all elements, except ones with specific indexes (this is what
> I am trying to do actually)? For example, I have s = [2, 4, 3, 6, 8, 9, 11]
> and I want to delete 3rd, 5th and 6th elements (or save 1st, 2nd, 4th and
> 7th).
>
Is this what you want?
julia> s = [2, 4, 3, 6, 8, 9, 11]
7-element Array{Int64,1}:
2
4
3
6
8
9
11
julia> ix = [1,2,4,7]
4-element Array{Int64,1}:
1
2
4
7
julia> s[ix]
4-element Array{Int64,1}:
2
4
6
11
Hope this helps!