Julia中稀疏复数的矩阵

时间:2015-01-11 17:39:52

标签: matrix julia complex-numbers sparse-matrix

在朱莉娅,我可以创建一个零稀疏矩阵:

julia> a = spzeros(2,2)
2x2 sparse matrix with 0 Float64 entries:

julia> a[1,1] = 1
1

julia> full(a)
2x2 Array{Float64,2}:
 1.0  0.0
 0.0  0.0

我可以创建一个复杂的矩阵:

julia> b = [ 1 ; im ]
2-element Array{Complex{Int64},1}:
 1+0im
 0+1im

如果我尝试将复数值分配给稀疏的零矩阵,我会收到错误:

julia> a[1,1] = im
ERROR: InexactError()
 in setindex! at sparse/sparsematrix.jl:1095

与Float64参数化的spzeros()返回类型一致:

julia> typeof(a)
SparseMatrixCSC{Float64,Int64} (constructor with 1 method)

如何在Julia中创建复杂类型零的稀疏矩阵?

1 个答案:

答案 0 :(得分:6)

查看我们可以传递给spzeros的内容:

julia> methods(spzeros)
# 5 methods for generic function "spzeros":
spzeros(m::Integer,n::Integer) at sparse/sparsematrix.jl:406
spzeros(Tv::Type{T<:Top},m::Integer,n::Integer) at sparse/sparsematrix.jl:407
spzeros(Tv::Type{T<:Top},Ti::Type{T<:Top},m::Integer,n::Integer) at sparse/sparsematrix.jl:409
spzeros(m::Integer) at deprecated.jl:28
spzeros(Tv::Type{T<:Top},m::Integer) at deprecated.jl:28

我们看到我们应该能够传递一个类型作为第一个参数:

julia> a = spzeros(Complex{Float64}, 2,2)
2x2 sparse matrix with 0 Complex{Float64} entries:

julia> full(a)
2x2 Array{Complex{Float64},2}:
 0.0+0.0im  0.0+0.0im
 0.0+0.0im  0.0+0.0im

julia> a[1,1] = 2+3.4im
2.0 + 3.4im

julia> a
2x2 sparse matrix with 1 Complex{Float64} entries:
    [1, 1]  =  2.0+3.4im