Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass mutable copies to inplace LinearAlgebra functions #52504

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ julia> triu(a)
0.0 0.0 0.0 1.0
```
"""
triu(M::AbstractMatrix) = triu!(copy(M))
triu(M::AbstractMatrix) = triu!(copymutable(M))

"""
tril(M)
Expand All @@ -362,7 +362,7 @@ julia> tril(a)
1.0 1.0 1.0 1.0
```
"""
tril(M::AbstractMatrix) = tril!(copy(M))
tril(M::AbstractMatrix) = tril!(copymutable(M))

"""
triu(M, k::Integer)
Expand Down Expand Up @@ -393,7 +393,7 @@ julia> triu(a,-3)
1.0 1.0 1.0 1.0
```
"""
triu(M::AbstractMatrix,k::Integer) = triu!(copy(M),k)
triu(M::AbstractMatrix,k::Integer) = triu!(copymutable(M),k)

"""
tril(M, k::Integer)
Expand Down Expand Up @@ -424,7 +424,7 @@ julia> tril(a,-3)
1.0 0.0 0.0 0.0
```
"""
tril(M::AbstractMatrix,k::Integer) = tril!(copy(M),k)
tril(M::AbstractMatrix,k::Integer) = tril!(copymutable(M),k)

"""
triu!(M)
Expand Down Expand Up @@ -1760,7 +1760,7 @@ Calculates the determinant of a matrix using the
[Bareiss Algorithm](https://en.wikipedia.org/wiki/Bareiss_algorithm).
Also refer to [`det_bareiss!`](@ref).
"""
det_bareiss(M) = det_bareiss!(copy(M))
det_bareiss(M) = det_bareiss!(copymutable(M))



Expand Down
13 changes: 13 additions & 0 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ using .Main.OffsetArrays
isdefined(Main, :DualNumbers) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "DualNumbers.jl"))
using .Main.DualNumbers

isdefined(Main, :FillArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FillArrays.jl"))
using .Main.FillArrays

Random.seed!(123)

n = 5 # should be odd
Expand Down Expand Up @@ -647,4 +650,14 @@ end
end
end

@testset "immutable arrays" begin
A = FillArrays.Fill(big(3), (4, 4))
M = Array(A)
@test triu(A) == triu(M)
@test triu(A, -1) == triu(M, -1)
@test tril(A) == tril(M)
@test tril(A, 1) == tril(M, 1)
@test det(A) == det(M)
end

end # module TestGeneric
2 changes: 2 additions & 0 deletions test/testhelpers/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Fill(v, size::Vararg{Integer}) = Fill(v, size)

Base.size(F::Fill) = F.size

Base.copy(F::Fill) = F

@inline getindex_value(F::Fill) = F.value

@inline function Base.getindex(F::Fill{<:Any,N}, i::Vararg{Int,N}) where {N}
Expand Down