Julia中的稀疏矩阵和类型约束 [英] Sparse matrices and type constraints in Julia

查看:431
本文介绍了Julia中的稀疏矩阵和类型约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对参数方法在Julia中的工作方式感到困惑.我也希望参数化是在这里使用的正确词.我已经阅读了有关方法的文档,但仍不清楚为什么会发生以下错误.如果我定义如下的函数

I am confused about how parametric methods work in Julia. I also hope that parametric is the right word to use here. I've read the docs on methods but it is still not clear why the following error occurs. If I define a function as below

function Bazinga{T<:Real}(mat ::Union{Array{T,2},SparseMatrixCSC})
    mat^4
end

并运行

Penny = sparse(randn(10,10))
Bazinga(Penny)

我知道

ERROR: MethodError: `Bazinga` has no method matching Bazinga(::SparseMatrixCSC{Float64,Int64})
Closest candidates are:
  Bazinga{T<:Real}(::Union{Array{T<:Real,2},SparseMatrixCSC{Tv,Ti<:Integer}})

但是Int64<:Int是正确的,那么这是什么问题呢? 如果我将功能重新定义为

But Int64<:Int is true so what is the problem here? If I redefine the function as

function Bazinga(mat ::Union{Array{Real,2},SparseMatrixCSC})
    mat^4
end

没有错误产生并且该功能起作用.现在还有两种方法

no error is produced and the function works. Also there are now two methods of the function

Bazinga(mat::Union{Array{Real,2},SparseMatrixCSC{Tv,Ti<:Integer}}) at none:2
Bazinga{T<:Real}(mat::Union{Array{T<:Real,2},SparseMatrixCSC{Tv,Ti<:Integer}}) at none:2

这两个SparseMatrixCSC{Tv,Ti<:Integer}都在其中的

,但仅在第一种情况下,它会导致错误. 任何输入都很好!

in which both SparseMatrixCSC{Tv,Ti<:Integer} is in, but only in the first case it causes an error. Any input is wellcome!

Edit1:为什么Array{Float64,1} <:Array{Real,1}为false? Edit2:Edit1中的问题由 Julia中的数组中断字符串类型解决. .但是我不认为这可以解释该方法的结果.或者至少我看不到.

And why is Array{Float64,1} <:Array{Real,1} false? The question in the Edit1 is solved by Arrays break string types in Julia . But I don't think it explains what happens with the method. Or at least I don't see it.

推荐答案

我不太清楚其背后的原因,但是对于您的用例,使用TypeVar可能是一种解决方法:

I don't quite know the reason under the hood, but using a TypeVar may be a workaround for your use case:

T = TypeVar(:T, Union{}, Real, false)

function Bazinga(mat::Union{Array{T,2},SparseMatrixCSC})
    mat^4
end

julia> Bazinga(sparse(randn(2,2)))
2x2 sparse matrix with 4 Float64 entries:
    [1, 1]  =  0.840151
    [2, 1]  =  -0.503551
    [1, 2]  =  -0.437787
    [2, 2]  =  1.28652

julia> Bazinga(randn(2,2))
2x2 Array{Float64,2}:
 0.203916   -0.261589 
 0.0107211  -0.0137373

更新:

经过一番调查,我发现解决此问题的规范"方法是定义typealias:

typealias RealMatrix{T<:Real} Array{T, 2}

function Bazinga(mat::Union{RealMatrix, SparseMatrixCSC})
    mat^4
end

我猜想原因是Julia无法在不知道所有参数类型到底是什么的情况下完成方法匹配任务.这是另一个示例:

I guess the reason is Julia can't complete method matching tasks without knowing what on earth all of the parametric types are. Here is another example:

Foo{R<:Real,C<:Complex}(x::Union{Array{R}, SparseMatrixCSC{C}}) = x

Foo(sparse([1+im 2; 3 4]))Foo(rand(2,2))均不起作用.因此,编写参数联合方法的正确方法是通过typealias约束那些不同的参数:

Neither Foo(sparse([1+im 2; 3 4])) nor Foo(rand(2,2)) will work. So the correct way to write a parametric-union method is to constrain those different parameters via typealias:

julia> typealias Bar{T<:Real} Array{T,2}
Array{T<:Real,2}

julia> typealias Baz{T<:Complex} SparseMatrixCSC{T}
SparseMatrixCSC{T<:Complex{T<:Real},Ti<:Integer}

julia> Foo(x::Union{Bar, Baz}) = x
Foo (generic function with 1 method)

julia> Foo(rand(2,2))
2x2 Array{Float64,2}:
 0.000739447  0.713386
 0.32024      0.705593

julia> Foo(sparse([1+im; 2im]))
2x1 sparse matrix with 2 Complex{Int64} entries:
    [1, 1]  =  1+1im
    [2, 1]  =  0+2im  

如果联合的参数类型具有相同的类型参数T,我们还可以如下使用typealias:

If argument types of the union have the same type parameter T, we can also use typealias as follows:

julia> typealias MyUnion{T,N<:Integer} Union{Array{T,2}, SparseMatrixCSC{T,N}}
Union{Array{T,2},SparseMatrixCSC{T,N<:Integer}}

julia> Foo{T<:Real}(x::MyUnion{T}) = x
Foo (generic function with 1 method)

julia> Foo(rand(2,2))
2x2 Array{Float64,2}:
 0.0172915  0.587518  
 0.234148   0.00543953

julia> Foo(rand(2))
ERROR: MethodError: `Foo` has no method matching Foo(::Array{Float64,1})

julia> Foo(sparse(rand(2)))
2x1 sparse matrix with 2 Float64 entries:
    [1, 1]  =  0.900029
    [2, 1]  =  0.0634815

julia> Foo(sparse(rand(2,2)))
2x2 sparse matrix with 4 Float64 entries:
    [1, 1]  =  0.592632
    [2, 1]  =  0.066563
    [1, 2]  =  0.805307
    [2, 2]  =  0.923422

julia> Foo(rand(2,2))
2x2 Array{Float64,2}:
 0.167078  0.673194
 0.681418  0.316017

更新2:

在Julia-v0.6 +中,无需定义类型别名:

UPDATE 2:

In Julia-v0.6+, defining the typealias is not necessary:

function Bazinga(mat::Union{Array{<:Real,2},SparseMatrixCSC})
    mat^4
end

有关更多详细信息,请参考此答案.

Please refer to this answer for further details.

这篇关于Julia中的稀疏矩阵和类型约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆