[已解决]SymTridiagonal 函数创建矩阵出错

julia:1.0.2
Windows10 : 17134.407
我希望创建一个主对角相等,次对角均为1的三对角矩阵,查到了SymTridiagonal函数.代码如下

using LinearAlgebra # 导入线性代数库
function gen(c,n::Int64) # 生成一个特殊的三对角阵
    dv=[c for i=1:n]
    du=[1 for i =1: n-1]
    SymTridiagonal(dv, du)
end
gen(1,5)

可以正常输出

5×5 SymTridiagonal{Int64,Array{Int64,1}}:
 1  1  ⋅  ⋅  ⋅
 1  1  1  ⋅  ⋅
 ⋅  1  1  1  ⋅
 ⋅  ⋅  1  1  1
 ⋅  ⋅  ⋅  1  1

但是输入gen(1.5,5)就会报错

MethodError: no method matching SymTridiagonal(::Array{Float64,1}, ::Array{Int64,1})
Closest candidates are:
  SymTridiagonal(::V<:AbstractArray{T,1}, !Matched::V<:AbstractArray{T,1}) where {T, V<:AbstractArray{T,1}} at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\LinearAlgebra\src\tridiag.jl:49

Stacktrace:
 [1] gen(::Float64, ::Int64) at .\In[2]:4
 [2] top-level scope at In[12]:1 

我尝试过其他的数,只要第一个参数是小数就会报错.
求解答

image
数组里面的类型要统一,要么全是整数,要么全是浮点数

1 个赞