Skip to content

Commit db4ca84

Browse files
jishnubstevengj
authored andcommitted
Reinstate similar for AbstractQ for backward compatibility (#52694)
Co-authored-by: Steven G. Johnson <[email protected]> (cherry picked from commit 7ec60b4)
1 parent 2d7c421 commit db4ca84

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

stdlib/LinearAlgebra/src/LinearAlgebra.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ Note that the [element type](@ref eltype) of the matrix must admit [`norm`](@ref
241241
"""
242242
struct ColumnNorm <: PivotingStrategy end
243243

244+
using Base: DimOrInd
245+
244246
# Check that stride of matrix/vector is 1
245247
# Writing like this to avoid splatting penalty when called with multiple arguments,
246248
# see PR 16416

stdlib/LinearAlgebra/src/abstractq.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ axes(Q::AbstractQ, d::Integer) = d in (1, 2) ? axes(Q)[d] : Base.OneTo(1)
7878
copymutable(Q::AbstractQ{T}) where {T} = lmul!(Q, Matrix{T}(I, size(Q)))
7979
copy(Q::AbstractQ) = copymutable(Q)
8080

81+
# legacy compatibility
82+
similar(Q::AbstractQ) = similar(Q, eltype(Q), size(Q))
83+
similar(Q::AbstractQ, ::Type{T}) where {T} = similar(Q, T, size(Q))
84+
similar(Q::AbstractQ, size::DimOrInd...) = similar(Q, eltype(Q), size...)
85+
similar(Q::AbstractQ, ::Type{T}, size::DimOrInd...) where {T} = similar(Q, T, Base.to_shape(size))
86+
similar(Q::AbstractQ, size::Tuple{Vararg{DimOrInd}}) = similar(Q, eltype(Q), Base.to_shape(size))
87+
similar(Q::AbstractQ, ::Type{T}, size::NTuple{N,Integer}) where {T,N} = Array{T,N}(undef, size)
88+
8189
# getindex
8290
@inline function getindex(Q::AbstractQ, inds...)
8391
@boundscheck Base.checkbounds_indices(Bool, axes(Q), inds) || Base.throw_boundserror(Q, inds)

stdlib/LinearAlgebra/test/abstractq.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ n = 5
102102
@test Q Prect
103103
@test Q Psquare
104104
@test Q F.Q*I
105+
106+
@testset "similar" begin
107+
QS = similar(Q)
108+
@test QS isa Matrix{eltype(Q)}
109+
@test size(QS) == size(Q)
110+
111+
QS = similar(Q, Int8)
112+
@test QS isa Matrix{Int8}
113+
@test size(QS) == size(Q)
114+
115+
QS = similar(Q, 1)
116+
@test QS isa Vector{eltype(Q)}
117+
@test size(QS) == (1,)
118+
119+
QS = similar(Q, Int8, 2)
120+
@test QS isa Vector{Int8}
121+
@test size(QS) == (2,)
122+
123+
QS = similar(Q, Int8, ())
124+
@test QS isa Array{Int8,0}
125+
126+
QS = similar(Q, ())
127+
@test QS isa Array{eltype(Q),0}
128+
end
105129
end
106130

107131
end # module

0 commit comments

Comments
 (0)