Skip to content

Commit 02468a5

Browse files
jishnubN5N3
authored andcommitted
Reinstate similar for AbstractQ for backward compatibility (#52694)
1 parent 6cda11c commit 02468a5

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

stdlib/LinearAlgebra/src/LinearAlgebra.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
1818
vec, view, zero
1919
using Base: IndexLinear, promote_eltype, promote_op, promote_typeof, print_matrix,
2020
@propagate_inbounds, reduce, typed_hvcat, typed_vcat, require_one_based_indexing,
21-
splat
21+
splat, DimOrInd
2222
using Base.Broadcast: Broadcasted, broadcasted
2323
using Base.PermutedDimsArrays: CommutativeOps
2424
using OpenBLAS_jll

stdlib/LinearAlgebra/src/abstractq.jl

+8
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

+24
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)