Skip to content

Commit

Permalink
Reinstate similar for AbstractQ for backward compatibility (#52694)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven G. Johnson <stevenj@alum.mit.edu>
  • Loading branch information
jishnub and stevengj authored May 22, 2024
1 parent 3beb168 commit 7ec60b4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ Note that the [element type](@ref eltype) of the matrix must admit [`norm`](@ref
"""
struct ColumnNorm <: PivotingStrategy end

using Base: DimOrInd

# 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 @@ -78,6 +78,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
25 changes: 25 additions & 0 deletions stdlib/LinearAlgebra/test/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ 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

# matrix division
q, r = F
R = randn(Float64, 5, 5)
Expand Down

0 comments on commit 7ec60b4

Please sign in to comment.