Skip to content

Commit

Permalink
Reinstate similar for AbstractQ for backward compatibility (#52694)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Nov 11, 2024
1 parent 0dd990e commit a5b3ce0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
vec, view, zero
using Base: IndexLinear, promote_eltype, promote_op, promote_typeof, print_matrix,
@propagate_inbounds, reduce, typed_hvcat, typed_vcat, require_one_based_indexing,
splat
splat, DimOrInd
using Base.Broadcast: Broadcasted, broadcasted
using Base.PermutedDimsArrays: CommutativeOps
using OpenBLAS_jll
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
24 changes: 24 additions & 0 deletions stdlib/LinearAlgebra/test/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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

0 comments on commit a5b3ce0

Please sign in to comment.