Skip to content

Commit 8e3e773

Browse files
authored
Merge pull request #80 from torfjelde/torfjelde/cholesky
Implementation of `setproperties` for `LinearAlgebra.Cholesky`
2 parents cd24e54 + 6988051 commit 8e3e773

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ConstructionBase"
22
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
33
authors = ["Takafumi Arakaki", "Rafael Schouten", "Jan Weidner"]
4-
version = "1.5.3"
4+
version = "1.5.4"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/nonstandard.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,21 @@ constructorof(::Type{<:LinRange}) = linrange_constructor
5656
### Expr: args get splatted
5757
# ::Expr annotation is to make it type-stable on Julia 1.3-
5858
constructorof(::Type{<:Expr}) = (head, args) -> Expr(head, args...)::Expr
59+
60+
### Cholesky
61+
setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple{()}) = C
62+
function setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple{(:L,),<:Tuple{<:LinearAlgebra.LowerTriangular}})
63+
return LinearAlgebra.Cholesky(C.uplo === 'U' ? copy(patch.L.data') : patch.L.data, C.uplo, C.info)
64+
end
65+
function setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple{(:U,),<:Tuple{<:LinearAlgebra.UpperTriangular}})
66+
return LinearAlgebra.Cholesky(C.uplo === 'L' ? copy(patch.U.data') : patch.U.data, C.uplo, C.info)
67+
end
68+
function setproperties(
69+
C::LinearAlgebra.Cholesky,
70+
patch::NamedTuple{(:UL,),<:Tuple{<:Union{LinearAlgebra.LowerTriangular,LinearAlgebra.UpperTriangular}}}
71+
)
72+
return LinearAlgebra.Cholesky(patch.UL.data, C.uplo, C.info)
73+
end
74+
function setproperties(C::LinearAlgebra.Cholesky, patch::NamedTuple)
75+
throw(ArgumentError("Invalid patch for `Cholesky`: $(patch)"))
76+
end

test/runtests.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,52 @@ end
194194
@inferred constructorof(typeof(lr1))(getfields(lr2)...)
195195
end
196196

197+
@testset "Cholesky" begin
198+
x = randn(3, 3)
199+
X = x * x'
200+
@testset "uplo=$uplo" for uplo in ['L', 'U']
201+
C = Cholesky(X, uplo, 0)
202+
203+
# Empty patch.
204+
C_new = ConstructionBase.setproperties(C, NamedTuple())
205+
@test typeof(C_new) === typeof(C)
206+
for f in propertynames(C)
207+
@test getproperty(C_new, f) == getproperty(C, f)
208+
end
209+
210+
# Update `L`.
211+
C_new = ConstructionBase.setproperties(C, (L=2 * C.L,))
212+
@test typeof(C_new) === typeof(C)
213+
for f in propertynames(C)
214+
@test getproperty(C_new, f) == 2 * getproperty(C, f)
215+
end
216+
217+
# Update `U`.
218+
C_new = ConstructionBase.setproperties(C, (U=2 * C.U,))
219+
@test typeof(C_new) === typeof(C)
220+
for f in propertynames(C)
221+
@test getproperty(C_new, f) == 2 * getproperty(C, f)
222+
end
223+
224+
# Update `UL`
225+
C_new = ConstructionBase.setproperties(C, (UL=2 * C.UL,))
226+
@test typeof(C_new) === typeof(C)
227+
for f in propertynames(C)
228+
@test getproperty(C_new, f) == 2 * getproperty(C, f)
229+
end
230+
231+
# We can only set the properties with `LowerTriangular` or `UpperTriangular` matrices.
232+
@test_throws ArgumentError ConstructionBase.setproperties(C, (L=parent(C.L),))
233+
@test_throws ArgumentError ConstructionBase.setproperties(C, (U=parent(C.U),))
234+
# Can only set one at the time.
235+
@test_throws ArgumentError ConstructionBase.setproperties(C, (L=C.L, U=C.U,))
236+
@test_throws ArgumentError ConstructionBase.setproperties(C, (UL=C.UL, U=C.U,))
237+
@test_throws ArgumentError ConstructionBase.setproperties(C, (UL=C.UL, L=C.L,))
238+
# And make sure any other patch will fail.
239+
@test_throws ArgumentError ConstructionBase.setproperties(C, (asdf=C.UL,))
240+
end
241+
end
242+
197243
@testset "Expr" begin
198244
e = :(a + b)
199245
@test e == @inferred constructorof(typeof(e))(getfields(e)...)

0 commit comments

Comments
 (0)