Skip to content

Commit 029ae61

Browse files
committed
Add similarmutable
1 parent c6e87ca commit 029ae61

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

stdlib/LinearAlgebra/src/generic.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,16 @@ function cross(a::AbstractVector, b::AbstractVector)
388388
[a2*b3-a3*b2, a3*b1-a1*b3, a1*b2-a2*b1]
389389
end
390390

391+
"""
392+
similarmutable(A)
393+
394+
Return a mutable `AbstractArray` that shares its structure
395+
with `similar(A)`. The difference with `similar(A)` is that
396+
each index of the result corresponding to the stored ones
397+
of `A` will be mutable.
398+
"""
399+
similarmutable(A) = similar(A)
400+
391401
"""
392402
triu(M)
393403
@@ -411,9 +421,9 @@ julia> triu(a)
411421
```
412422
"""
413423
function triu(M::AbstractMatrix)
414-
d = similar(M)
415-
A = triu!(d) # may return a different type
424+
A = similarmutable(M)
416425
copytrito!(A, M, 'U')
426+
triu!(A)
417427
end
418428

419429
"""
@@ -439,9 +449,9 @@ julia> tril(a)
439449
```
440450
"""
441451
function tril(M::AbstractMatrix)
442-
d = similar(M)
443-
A = tril!(d) # may return a different type
452+
A = similarmutable(M)
444453
copytrito!(A, M, 'L')
454+
tril!(A)
445455
end
446456

447457
"""

stdlib/LinearAlgebra/src/symmetric.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ end
325325
# storage type of A (not wrapped in a symmetry type). The following method covers these cases.
326326
similar(A::Union{Symmetric,Hermitian}, ::Type{T}, dims::Dims{N}) where {T,N} = similar(parent(A), T, dims)
327327

328+
similarmutable(A::Union{Symmetric, Hermitian}) = similar(parent(A))
329+
328330
parent(A::HermOrSym) = A.data
329331
Symmetric{T,S}(A::Symmetric{T,S}) where {T,S<:AbstractMatrix{T}} = A
330332
Symmetric{T,S}(A::Symmetric) where {T,S<:AbstractMatrix{T}} = Symmetric{T,S}(convert(S,A.data),A.uplo)

stdlib/LinearAlgebra/src/tridiag.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ axes(M::SymTridiagonal) = (ax = axes(M.dv, 1); (ax, ax))
157157
similar(S::SymTridiagonal, ::Type{T}) where {T} = SymTridiagonal(similar(S.dv, T), similar(S.ev, T))
158158
similar(S::SymTridiagonal, ::Type{T}, dims::Union{Dims{1},Dims{2}}) where {T} = similar(S.dv, T, dims)
159159

160+
similarmutable(S::SymTridiagonal) = Tridiagonal(similar(_evview(S)), similar(S.dv), similar(_evview(S)))
161+
160162
# copyto! for matching axes
161163
_copyto_banded!(dest::SymTridiagonal, src::SymTridiagonal) =
162164
(copyto!(dest.dv, src.dv); copyto!(dest.ev, _evview(src)); dest)

0 commit comments

Comments
 (0)