Skip to content

Commit

Permalink
Merge pull request #80 from torfjelde/torfjelde/cholesky
Browse files Browse the repository at this point in the history
Implementation of `setproperties` for `LinearAlgebra.Cholesky`
  • Loading branch information
jw3126 authored Sep 8, 2023
2 parents cd24e54 + 6988051 commit 8e3e773
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ConstructionBase"
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
authors = ["Takafumi Arakaki", "Rafael Schouten", "Jan Weidner"]
version = "1.5.3"
version = "1.5.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
18 changes: 18 additions & 0 deletions src/nonstandard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@ constructorof(::Type{<:LinRange}) = linrange_constructor
### Expr: args get splatted
# ::Expr annotation is to make it type-stable on Julia 1.3-
constructorof(::Type{<:Expr}) = (head, args) -> Expr(head, args...)::Expr

### Cholesky
setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple{()}) = C
function setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple{(:L,),<:Tuple{<:LinearAlgebra.LowerTriangular}})
return LinearAlgebra.Cholesky(C.uplo === 'U' ? copy(patch.L.data') : patch.L.data, C.uplo, C.info)
end
function setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple{(:U,),<:Tuple{<:LinearAlgebra.UpperTriangular}})
return LinearAlgebra.Cholesky(C.uplo === 'L' ? copy(patch.U.data') : patch.U.data, C.uplo, C.info)
end
function setproperties(
C::LinearAlgebra.Cholesky,
patch::NamedTuple{(:UL,),<:Tuple{<:Union{LinearAlgebra.LowerTriangular,LinearAlgebra.UpperTriangular}}}
)
return LinearAlgebra.Cholesky(patch.UL.data, C.uplo, C.info)
end
function setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple)
throw(ArgumentError("Invalid patch for `Cholesky`: $(patch)"))
end
46 changes: 46 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,52 @@ end
@inferred constructorof(typeof(lr1))(getfields(lr2)...)
end

@testset "Cholesky" begin
x = randn(3, 3)
X = x * x'
@testset "uplo=$uplo" for uplo in ['L', 'U']
C = Cholesky(X, uplo, 0)

# Empty patch.
C_new = ConstructionBase.setproperties(C, NamedTuple())
@test typeof(C_new) === typeof(C)
for f in propertynames(C)
@test getproperty(C_new, f) == getproperty(C, f)
end

# Update `L`.
C_new = ConstructionBase.setproperties(C, (L=2 * C.L,))
@test typeof(C_new) === typeof(C)
for f in propertynames(C)
@test getproperty(C_new, f) == 2 * getproperty(C, f)
end

# Update `U`.
C_new = ConstructionBase.setproperties(C, (U=2 * C.U,))
@test typeof(C_new) === typeof(C)
for f in propertynames(C)
@test getproperty(C_new, f) == 2 * getproperty(C, f)
end

# Update `UL`
C_new = ConstructionBase.setproperties(C, (UL=2 * C.UL,))
@test typeof(C_new) === typeof(C)
for f in propertynames(C)
@test getproperty(C_new, f) == 2 * getproperty(C, f)
end

# We can only set the properties with `LowerTriangular` or `UpperTriangular` matrices.
@test_throws ArgumentError ConstructionBase.setproperties(C, (L=parent(C.L),))
@test_throws ArgumentError ConstructionBase.setproperties(C, (U=parent(C.U),))
# Can only set one at the time.
@test_throws ArgumentError ConstructionBase.setproperties(C, (L=C.L, U=C.U,))
@test_throws ArgumentError ConstructionBase.setproperties(C, (UL=C.UL, U=C.U,))
@test_throws ArgumentError ConstructionBase.setproperties(C, (UL=C.UL, L=C.L,))
# And make sure any other patch will fail.
@test_throws ArgumentError ConstructionBase.setproperties(C, (asdf=C.UL,))
end
end

@testset "Expr" begin
e = :(a + b)
@test e == @inferred constructorof(typeof(e))(getfields(e)...)
Expand Down

3 comments on commit 8e3e773

@jw3126
Copy link
Member Author

@jw3126 jw3126 commented on 8e3e773 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/91074

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.4 -m "<description of version>" 8e3e773c7ee65a664fae6af11dd0ccdc5ef6478a
git push origin v1.5.4

@jw3126
Copy link
Member Author

@jw3126 jw3126 commented on 8e3e773 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.