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

Implemented strides for AbstractArray subtypes #30429

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ const AdjOrTransAbsMat{T} = AdjOrTrans{T,<:AbstractMatrix}
wrapperop(A::Adjoint) = adjoint
wrapperop(A::Transpose) = transpose

strides(a::AdjOrTrans) = size_to_strides(1, size(a)...)
stride(a::AdjOrTrans, i::Integer) = strides(a)[i]

# AbstractArray interface, basic definitions
length(A::AdjOrTrans) = length(A.parent)
size(v::AdjOrTransAbsVec) = (1, length(v.parent))
Expand Down
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ function setindex!(D::Diagonal, v, i::Int, j::Int)
return v
end

strides(a::Diagonal) = size_to_strides(1, size(a)...)
stride(a::Diagonal, i::Integer) = strides(a)[i]

## structured matrix methods ##
function Base.replace_in_print_matrix(A::Diagonal,i::Integer,j::Integer,s::AbstractString)
Expand Down
9 changes: 9 additions & 0 deletions stdlib/LinearAlgebra/test/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,15 @@ end
@test broadcast(+, Transpose(sparsevec), 1.0, Transpose(sparsevec)) isa Transpose{Float64,SparseVector{Float64,Int}}
end

@testset "Strides AdjOrTrans Issue#29705" begin
@test strides(rand(3,5)') == (1, 5)
@test strides([1 2 3; 4 5 6]') == (1, 3)
@test strides(transpose(rand(3,4))) == (1, 4)
@test strides(transpose([1 2 3; 4 5 6])) == (1, 3)
@test stride(rand(3,5)', 1) == 1
@test stride(rand(3,5)', 2) == 5
end

@testset "Adjoint/Transpose-wrapped vector multiplication" begin
realvec, realmat = [1, 2, 3], [1 2 3; 4 5 6; 7 8 9]
complexvec, complexmat = [1im, 2, -3im], [1im 2 3; 4 5 -6im; 7im 8 9]
Expand Down
8 changes: 8 additions & 0 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ end
end
end

@testset "Strides Diagonal Issue#29705" begin
@test strides(Diagonal([1 2 3 4; 5 6 7 8; 1 3 5 7])) == (1, 3)
@test strides(Diagonal(rand(5, 10))) == (1, 5)
@test strides(Diagonal(rand(5, 10)')) == (1, 5)
@test strides(Diagonal(rand(5, 10)), 1) == 1
@test strides(Diagonal(rand(5, 10)), 2) == 5
end

@testset "isposdef" begin
@test isposdef(Diagonal(1.0 .+ rand(n)))
@test !isposdef(Diagonal(-1.0 * rand(n)))
Expand Down