如何编写此具有约束的泛型/参数化函数的类型



以下代码不起作用。我怀疑这是由于类型参数的协方差。

编写以下函数类型的最佳方法是什么,使其既适用于密集型和非密集型;稀疏矩阵?

using LinearAlgebra
using SparseArrays

function test(A::Adjoint{T, AbstractMatrix{T}}) where T <: Real
@show 1
end

x = sparse(rand(5, 5))
test(x')
# MethodError: no method matching test(::Adjoint{Float64, SparseMatrixCSC{Float64, Int64}})

我最初误解了您的要求。因此,正如DNF所建议的,这将适用于密集和稀疏伴随矩阵:

function test(A::Adjoint{T, <:AbstractMatrix{T}}) where T <: Real
@show 1
end
x = sparse(rand(5, 5))
julia> test(x')
1 = 1
1
function test(A::Adjoint{T, M}) where {T<:Real, M<:AbstractMatrix{T}}
@show 1
end

最新更新