Module 重写自定义结构的反斜杠

Module 重写自定义结构的反斜杠,module,julia,linear-algebra,Module,Julia,Linear Algebra,我通过扩展linearlgebra模块中的功能来创建一个自定义矩阵库。我一直在通过在自定义MyLinearAlgebra模块中创建自定义结构来实现这一点,该模块直接导入默认线性代数结构并覆盖许多常见的LA功能。我的问题是关于如何重写反斜杠函数的。这是我的“MyLinearAlgebra.jl”: 只关注LocalMatrix.jl现在,我有: """ struct LocalMatrix{T} <: AbstractMatrix{T} Block di

我通过扩展
linearlgebra
模块中的功能来创建一个自定义矩阵库。我一直在通过在自定义
MyLinearAlgebra
模块中创建自定义结构来实现这一点,该模块直接导入默认线性代数结构并覆盖许多常见的LA功能。我的问题是关于如何重写反斜杠函数的。这是我的
“MyLinearAlgebra.jl”

只关注
LocalMatrix.jl
现在,我有:

"""
    struct LocalMatrix{T} <: AbstractMatrix{T}
Block diagonal structure for local matrix. `A[:,:,iS,iK]` is a block matrix for
state iS and element iK
"""
struct LocalMatrix{T} <: AbstractMatrix{T}
    data::Array{T,4}
    factorizations::Array{Any,2}

    function LocalMatrix(data::Array{T,4}) where {T}
        new{T}(data,Array{Any}(undef, size(data,3), size(data,4)))
    end
end

# [... a lot of things that are already working including: ldiv!]

"""
    ldiv!(A::LocalMatrix, x::SolutionVector)
In-place linear solve A\\x using block-diagonal LU factorizations. Compute this
block-diagonal factorization if not yet computed.
"""
function LinearAlgebra.ldiv!(A::LocalMatrix, x::SolutionVector)
    println("my ldiv! works fine")
    x
end
    
# [ ... and yet this does not work ]
"""
    A::LocalMatrix \\ x::SolutionVector
Linear solve A\\x using block-diagonal LU factorizations. Compute this
block-diagonal factorization if not yet computed.
"""
function (\)(A::LocalMatrix, x::SolutionVector)
    println("my \\ never prints out in any tests")
    (m,n,ns,ne) = size(A.data)
    (nx,nsx,nex) = size(x.data)
    @assert n == nx && ne == nex && m == n
    b = deepcopy(x)
    LinearAlgebra.ldiv!(A, b)
end
“”“

结构LocalMatrix{T}
\
在Base中定义,因此:

julia> "a" \ "b"
ERROR: MethodError: no method matching adjoint(::String)

julia> Base.:\(::String, ::String) = "hello"

julia> "a" \ "b"
"hello"
但是,当它被导入到
LinearAlgebra
时,以下内容也适用于我(我正在使用一个新会话):


由于Julia将向同一个函数(在Base中定义)添加一个方法。

您需要使用签名
LinearAlgebra.:\(x,y)
来避免无效的函数名错误。但是,即使这样做,我仍然无法覆盖默认行为(这就是为什么这是一个注释而不是答案)这很奇怪-它对我有效。@BogumiłKamiński啊!是我的错。我在测试代码中颠倒了参数的顺序,并且正在工作,所以没有时间去弄清楚结果错误的含义:-)谢谢。所以我基本上有了正确的想法,但我在标题
linearagebra.\(…)中缺少了冒号
正如科林在评论中所建议的。是的,或者您可以只编写
导入库:\
,然后使用
\(…)=…
(这是
线性代数
模块内部所做的)
julia> "a" \ "b"
ERROR: MethodError: no method matching adjoint(::String)

julia> Base.:\(::String, ::String) = "hello"

julia> "a" \ "b"
"hello"
julia> using LinearAlgebra

julia> "a" \ "b"
ERROR: MethodError: no method matching adjoint(::String)

julia> LinearAlgebra.:\(::String, ::String) = "hello"

julia> "a" \ "b"
"hello"