julia - 在 Julia 中,创建一个向量集,并指定该集的类型

标签 julia

在 Julia 中,这按预期工作:

g1 = [1, 1, 0, 0] / sqrt(2)
u1 = [1, -1, 0, 0] / sqrt(2)
g2 = [0, 0, 1, 1] / sqrt(2)
u2 = [0, 0, 1, -1] / sqrt(2)

up = Set()
push!(up, g1, u1, g2, u2)

给出结果:

Set{Any} with 4 elements:
  [0.0, 0.0, 0.7071067811865475, -0.7071067811865475]
  [0.7071067811865475, 0.7071067811865475, 0.0, 0.0]
  [0.0, 0.0, 0.7071067811865475, 0.7071067811865475]
  [0.7071067811865475, -0.7071067811865475, 0.0, 0.0]

但是,该 Set 被认为是 Set{Any},我更喜欢 Set{Array{Float64, 1}} 以避免出现错误我应该错误地推送不同的东西吗?

当我尝试时:

up = Set{Array{Float64, 1}}
push!(up, g1, u1, g2, u2)

我收到以下错误:

ERROR: MethodError: no method matching push!(::Type{Set{Array{Float64,1}}}, ::Array{Float64,1})
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:2158
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2159
  push!(::Array{Any,1}, ::Any) at array.jl:919
  ...
Stacktrace:
 [1] push!(::Type{T} where T, ::Array{Float64,1}, ::Array{Float64,1}) at .\abstractarray.jl:2158
 [2] push!(::Type{T} where T, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at .\abstractarray.jl:2159
 [3] top-level scope at none:0

正确的语法是什么?

最佳答案

你忘记了构造函数,它应该是up = Set{Array{Float64, 1}}(),请参阅下面的代码:

julia> up = Set{Array{Float64, 1}}()
Set{Array{Float64,1}} with 0 elements

julia> push!(up, g1, u1, g2, u2)
Set{Array{Float64,1}} with 4 elements:
  [0.0, 0.0, 0.7071067811865475, -0.7071067811865475]
  [0.7071067811865475, 0.7071067811865475, 0.0, 0.0]
  [0.0, 0.0, 0.7071067811865475, 0.7071067811865475]
  [0.7071067811865475, -0.7071067811865475, 0.0, 0.0]

关于julia - 在 Julia 中,创建一个向量集,并指定该集的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61551880/

相关文章:

宏与接受表达式的函数

python - 一段 Julia 和 Python 代码的优化建议

julia - 不精确错误 : Int64 even when checking divisibility

dictionary - Julia:编写一个返回参数值字典的函数 "paramvalues"

julia - “yield” 关键字在 Julia 中有什么作用?

macos - 现有 jupyter 安装中的 Julia

julia - Julia 有懒惰的 `filter` 吗?

documentation-generation - 如何为 Julia 创建离线文档

julia - 可以对 Julia (julia-lang) 代码进行静态分析以避免运行时类型错误吗?

struct - Julia 结构错误 "no method matching iterate"