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

Reinstate similar for AbstractQ for backward compatibility #52694

Merged
merged 4 commits into from
May 22, 2024
Merged
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
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ struct RowNonZero <: PivotingStrategy end
struct RowMaximum <: PivotingStrategy end
struct ColumnNorm <: PivotingStrategy end

const DimOrInd = Union{Integer, AbstractUnitRange}
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved

# Check that stride of matrix/vector is 1
# Writing like this to avoid splatting penalty when called with multiple arguments,
# see PR 16416
Expand Down
8 changes: 8 additions & 0 deletions stdlib/LinearAlgebra/src/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ axes(Q::AbstractQ, d::Integer) = d in (1, 2) ? axes(Q)[d] : Base.OneTo(1)
copymutable(Q::AbstractQ{T}) where {T} = lmul!(Q, Matrix{T}(I, size(Q)))
copy(Q::AbstractQ) = copymutable(Q)

# legacy compatibility
similar(Q::AbstractQ) = similar(Q, eltype(Q), size(Q))
similar(Q::AbstractQ, ::Type{T}) where {T} = similar(Q, T, size(Q))
similar(Q::AbstractQ, size::DimOrInd...) = similar(Q, eltype(Q), size...)
similar(Q::AbstractQ, ::Type{T}, size::DimOrInd...) where {T} = similar(Q, T, Base.to_shape(size))
similar(Q::AbstractQ, size::Tuple{Vararg{DimOrInd}}) = similar(Q, eltype(Q), Base.to_shape(size))
similar(Q::AbstractQ, ::Type{T}, size::NTuple{N,Integer}) where {T,N} = Array{T,N}(undef, size)

# getindex
@inline function getindex(Q::AbstractQ, inds...)
@boundscheck Base.checkbounds_indices(Bool, axes(Q), inds) || Base.throw_boundserror(Q, inds)
Expand Down
24 changes: 24 additions & 0 deletions stdlib/LinearAlgebra/test/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ n = 5
@test Q ≈ Prect
@test Q ≈ Psquare
@test Q ≈ F.Q*I

@testset "similar" begin
QS = similar(Q)
@test QS isa Matrix{eltype(Q)}
@test size(QS) == size(Q)

QS = similar(Q, Int8)
@test QS isa Matrix{Int8}
@test size(QS) == size(Q)

QS = similar(Q, 1)
@test QS isa Vector{eltype(Q)}
@test size(QS) == (1,)

QS = similar(Q, Int8, 2)
@test QS isa Vector{Int8}
@test size(QS) == (2,)

QS = similar(Q, Int8, ())
@test QS isa Array{Int8,0}

QS = similar(Q, ())
@test QS isa Array{eltype(Q),0}
end
end

end # module