用于 Julia 数组的 new()

new() for Julia Arrays

我努力为我的结构 PERK 编写一个合适的构造函数。给我带来麻烦的是 new() 与我的成员数组的用法。代码如下所示:

abstract type PERKTYPE end

struct PERK <: PERKTYPE
    NumStages::Int

    ACoeffs::Array{BigFloat}
    c::Array{BigFloat}
  
    function PERK()

      ACoeffsFile = open("some/existing/file.txt", "r")
      NumStages = countlines(ACoeffsFile)
      close(ACoeffsFile)
      #println(NumStages)

      ACoeffs = Array{BigFloat}(undef, NumStages, 2)
      
      # Fille ACoeffs with data from file, omitted here

      c = Array{BigFloat}(undef, NumStages)
      for i in 1:NumStages
        c[i] = (i-1)/(2*(NumStages-1))
      end

      new(NumStages) # Fine
      new(ACoeffs, c) # Not working if un-commented

    end # PERK()
  end # struct PERK

我收到错误消息

ERROR: LoadError: MethodError: Cannot `convert` an object of type Matrix{BigFloat} to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::LLVM.GenericValue) where T<:Signed at ~/.julia/packages/LLVM/gE6U9/src/execution.jl:27
  convert(::Type{T}, ::LLVM.ConstantInt) where T<:Signed at ~/.julia/packages/LLVM/gE6U9/src/core/value/constant.jl:89
  convert(::Type{T}, ::Ptr) where T<:Integer at ~/Software/julia-1.7.2/share/julia/base/pointer.jl:23
  ...
Stacktrace:
 [1] PERK()
   @ Trixi ~/.../methods_PERK.jl:26

这是怎么回事?当然,我对数组的任何转换都不感兴趣。

我在这里可能是错的,但是 new() 创建了一个 PERK 结构。表示具有三个参数的结构。如果你只用 NumStages 调用 new() 就没问题,因为它是结构的第一个参数。不过

new(ACoeffs, c)

尝试将数组 ACoeffs 馈送到 NumStages 中,这会产生您看到的错误。 您可以使用

来克服这个问题
new(NumStages, ACoeffs, c)

希望对您有所帮助。