简体   繁体   English

如何在 Julia 中定义任意浮点类型的数组?

[英]How to define an array of any float type in Julia?

I tried Array{AbstractFloat,1} , but this does not work as Array{Float64,1} <: Array{AbstractFloat,1} is false , even though Float64 <: AbstractFloat is true .我试过Array{AbstractFloat,1} ,但这不起作用,因为Array{Float64,1} <: Array{AbstractFloat,1}false ,即使Float64 <: AbstractFloattrue

If you want an vector which can contain any kind of floating-point value, then the correct type is Vector{AbstractFloat} , which can be constructed like this:如果您想要一个可以包含任何类型的浮点值的向量,那么正确的类型是Vector{AbstractFloat} ,可以这样构造:

julia> v = AbstractFloat[]
AbstractFloat[]

julia> push!(v, 1.5)
1-element Vector{AbstractFloat}:
 1.5

julia> push!(v, big(2.0)^1000)
2-element Vector{AbstractFloat}:
 1.5
 1.071508607186267320948425049060001810561404811705533607443750388370351051124936e+301

julia> map(typeof, v)
2-element Vector{DataType}:
 Float64
 BigFloat

julia> push!(v, "oy, I'm not a float!")
ERROR: MethodError: Cannot `convert` an object of type String to an object

You are correct that Vector{Float64} is not a subtype of Vector{AbstractFloat} .你是正确的Vector{Float64}不是Vector{AbstractFloat}的子类型。 This is because subtyping of parametric types in Julia is invariant (not covariant or even contravariant).这是因为 Julia 中的参数类型的子类型是不变的(不是协变的,甚至不是逆变的)。 Even more generally, no concrete type is a subtype of any other concrete type and Vector{Float64} and Vector{AbstractFloat} are both concrete types since they are the types of actual objects:更一般地说,没有具体类型是任何其他具体类型的子类型,并且Vector{Float64}Vector{AbstractFloat}都是具体类型,因为它们是实际对象的类型:

julia> typeof(v)
Vector{AbstractFloat} = Array{AbstractFloat,1}

julia> typeof([1.5])
Vector{Float64} = Array{Float64,1}

If you want an abstract type which includes both of these concrete types as well as any other vector of floating-point values, that can be expressed as Vector{<:AbstractFloat} :如果您想要一个包含这两种具体类型以及任何其他浮点值向量的抽象类型,则可以表示为Vector{<:AbstractFloat}

julia> Vector{AbstractFloat} <: Vector{<:AbstractFloat}
true

julia> Vector{Float64} <: Vector{<:AbstractFloat}
true

julia> Vector{Union{Float64, BigFloat}} <: Vector{<:AbstractFloat}
true

julia> Vector{String} <: Vector{<:AbstractFloat}
false

However, this type is abstract and you cannot create an instance of it.但是,这种类型是抽象的,您不能创建它的实例。 You can, however, use it for dispatch and write a method that will apply to any vector of floating-point values:但是,您可以使用它进行调度并编写一个适用于任何浮点值向量的方法:

f(v::Vector{<:AbstractFloat}) = "a vector of floats"
f(v::Vector) = "a vector but not of floats"
f(x::Any) = "not a vector at all"

Here's this function in action:这是 function 的实际操作:

julia> f(v)
"a vector of floats"

julia> f([1.5])
"a vector of floats"

julia> f(Union{Float64,BigFloat}[1.5, big(2.0)])
"a vector of floats"

julia> f([1, 2, 3])
"a vector but not of floats"

julia> f("nope")
"not a vector at all"

In short, the element type of a vector has to be an actual type, such as Float64 or AbstractFloat .简而言之,向量的元素类型必须是实际类型,例如Float64AbstractFloat You can expess the set of types of all floating-point vectors as Vector{<:AbstractFloat} but this cannot be the type of any vector because <:AbstractFloat is not a type, it's an upper bound on types.您可以将所有浮点向量的类型集表示为Vector{<:AbstractFloat}但这不能是任何向量的类型,因为<:AbstractFloat不是类型,它是类型的上限。 You can, however, use Vector{<:AbstractFloat} to define methods which operate on any vector of floats, regardless of specific element type (as long as the element type is some kind of float).但是,您可以使用Vector{<:AbstractFloat}来定义对任何浮点向量进行操作的方法,而不管特定的元素类型如何(只要元素类型是某种浮点数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM