Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add * for more matrices defined from layouts #241

Merged
merged 6 commits into from
Jul 7, 2024

Conversation

DanielVandH
Copy link
Contributor

Need this to support multiplication between infinite matrices, as found in DanielVandH/InfiniteRandomArrays.jl#5.

To make sure the methods are being tested properly, I extended the minimal InfiniteArrays.jl module in the tests to include a (hopefully minimal) definition of an infinite vector and an infinite matrix, and the resulting named matrices to mimic those from InfiniteRandomMatrices.jl.

InfVec and InfMat were initially just matrices which returned the indices, e.g. InfVec()[i] = i, but I wanted to be sure the methods were properly copying the data and accessing the correct entries, so I convert it into a random vector.

Copy link

codecov bot commented Jul 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.53%. Comparing base (d315ce3) to head (33a160e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #241      +/-   ##
==========================================
+ Coverage   90.43%   90.53%   +0.10%     
==========================================
  Files          11       11              
  Lines        1902     1923      +21     
==========================================
+ Hits         1720     1741      +21     
  Misses        182      182              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@DanielVandH
Copy link
Contributor Author

I'll deal with LTS later

*(A::UpperOrLowerTriangular{<:Any,<:LayoutMatrix}, B::Bidiagonal{<:Any,<:LayoutVector}) = mul(A, B)
*(A::UpperOrLowerTriangular{<:Any,<:LayoutMatrix}, B::Diagonal{<:Any,<:LayoutVector}) = mul(A, B) # ambiguity
*(A::Diagonal{<:Any,<:LayoutVector}, B::SymTridiagonal{<:Any,<:LayoutVector}) = mul(A, B)
*(A::Diagonal{<:Any,<:LayoutVector}, B::UpperOrLowerTriangular{<:Any,<:LayoutMatrix}) = mul(A, B)
Copy link
Member

@jishnub jishnub Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should forward to *(A::Diagonal, B.data::LayoutMatrix), so do we need to specialize the method for the triangular wrapper? Or will specializing it for LayoutMatrix suffice? *(::Diagonal, ::LayoutMatrix) method already exists, which should also make this work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to all four of those methods? I think actually the second and fourth methods in your highlight aren't needed, so I could remove those. Otherwise I'm not sure what you mean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, they are all needed due to ambiguities that these fixes have to introduce

Copy link
Member

@jishnub jishnub Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm referring to the *(A::Diagonal{<:Any,<:LayoutVector}, B::UpperOrLowerTriangular{<:Any,<:LayoutMatrix}) method on line 383, and the other order. Does this lead to ambiguities? If it does, I think this should be resolved in LinearAlgebra as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the complete error from the ambiguity that made me add that line (the other direction doesn't have an ambiguity)

julia> InfDiagonal() * InfUnitUpperTriangular() # has to be unit to get the ambiguity
ERROR: MethodError: *(::Diagonal{Float64, Main.InfiniteArrays.InfVec{Xoshiro}}, ::UnitUpperTriangular{Float64, Main.InfiniteArrays.InfMat{Xoshiro}}) is ambiguous.

Candidates:
  *(D::Diagonal, A::UnitUpperTriangular)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\diagonal.jl:578       
  *(A::Diagonal{<:Any, <:LayoutVector}, B::AbstractMatrix)
    @ ArrayLayouts c:\Users\djv23\.julia\dev\ArrayLayouts.jl\src\ArrayLayouts.jl:220
  *(D::Diagonal, A::LinearAlgebra.AbstractTriangular)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\diagonal.jl:835       
  *(A::AbstractMatrix, B::LinearAlgebra.AbstractTriangular)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\triangular.jl:1500    
  *(D::Diagonal, A::AbstractMatrix)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\diagonal.jl:292       

Possible fix, define
  *(::Diagonal{T, <:LayoutVector{T}} where T, ::UnitUpperTriangular)

Stacktrace:
 [1] top-level scope
   @ REPL[16]:1

I could make the method be Union{UnitUpperTriangular, UnitLowerTriangular} instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could make the method be Union{UnitUpperTriangular, UnitLowerTriangular} instead.

Actually nevermind, that just adds another ambiguity that forces me to add the more general line.. now I remember.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide a MWE? How are InfUnitUpperTriangular and InfDiagonal defined? Ideally, LinearAlgebra should not be introducing these ambiguities, but we may have to add these methods for now, and version-limit these later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are defined in this PR, built from my InfVec and InfMat definitions. Without this PR, you can reproduce it using this MWE:

using ArrayLayouts, LinearAlgebra
struct MyVec <: LayoutVector{Float64} 
    A::Vector{Float64}
end
struct MyMat <: LayoutMatrix{Float64}
    A::Matrix{Float64}
end
Base.size(v::MyVec) = size(v.A)
Base.getindex(v::MyVec, i::Int) = v.A[i] 
Base.size(v::MyMat) = size(v.A)
Base.getindex(v::MyMat, i::Int, j::Int) = v.A[i, j]
D = Diagonal(MyVec(rand(10)))
A = UnitUpperTriangular(MyMat(rand(10, 10)))
D * A
julia> D * A
ERROR: MethodError: *(::Diagonal{Float64, MyVec}, ::UnitUpperTriangular{Float64, MyMat}) is ambiguous.

Candidates:
  *(D::Diagonal, A::UnitUpperTriangular)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\diagonal.jl:578
  *(A::Diagonal{<:Any, <:LayoutVector}, B::AbstractMatrix)
    @ ArrayLayouts C:\Users\djv23\.julia\packages\ArrayLayouts\VzDAX\src\ArrayLayouts.jl:220
  *(D::Diagonal, A::LinearAlgebra.AbstractTriangular)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\diagonal.jl:835
  *(A::AbstractMatrix, B::LinearAlgebra.AbstractTriangular)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\triangular.jl:1500
  *(D::Diagonal, A::AbstractMatrix)
    @ LinearAlgebra C:\Users\djv23\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LinearAlgebra\src\diagonal.jl:292

Possible fix, define
  *(::Diagonal{T, <:LayoutVector{T}} where T, ::UnitUpperTriangular)

Stacktrace:
 [1] top-level scope
   @ Untitled-1:14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything else you want for this to be merged @jishnub? I'm not sure what would be defined on LinearAlgebra.jl's side but I think this would be good to go for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants