Skip to content

Commit c846fa0

Browse files
committed
Fix polar
1 parent fa786cb commit c846fa0

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

ext/MatrixAlgebraKitFillArraysExt.jl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414

1515
struct ZerosAlgorithm <: AbstractAlgorithm end
1616

17-
for f in [:eig, :eigh, :lq, :qr, :svd]
17+
for f in [:eig, :eigh, :lq, :polar, :qr, :svd]
1818
ff = Symbol("default_", f, "_algorithm")
1919
@eval begin
2020
function MatrixAlgebraKit.$ff(::Type{<:AbstractZerosMatrix}; kwargs...)
@@ -170,6 +170,30 @@ function MatrixAlgebraKit.svd_vals!(A::AbstractZerosMatrix, F, alg::ZerosAlgorit
170170
return diagview(A)
171171
end
172172

173+
function MatrixAlgebraKit.check_input(::typeof(left_polar!), A::AbstractZerosMatrix, F)
174+
m, n = size(A)
175+
m >= n ||
176+
throw(ArgumentError("input matrix needs at least as many rows as columns"))
177+
return nothing
178+
end
179+
function MatrixAlgebraKit.left_polar!(A::AbstractZerosMatrix, F, alg::ZerosAlgorithm)
180+
check_input(left_polar!, A, F)
181+
U, S, Vᴴ = svd_compact(A)
182+
return (Eye((axes(U, 1), axes(Vᴴ, 2))), Vᴴ' * S * Vᴴ)
183+
end
184+
185+
function MatrixAlgebraKit.check_input(::typeof(right_polar!), A::AbstractZerosMatrix, F)
186+
m, n = size(A)
187+
n >= m ||
188+
throw(ArgumentError("input matrix needs at least as many columns as rows"))
189+
return nothing
190+
end
191+
function MatrixAlgebraKit.right_polar!(A::AbstractZerosMatrix, F, alg::ZerosAlgorithm)
192+
check_input(right_polar!, A, F)
193+
U, S, Vᴴ = svd_compact(A)
194+
return (U * S * U', Eye((axes(U, 1), axes(Vᴴ, 2))))
195+
end
196+
173197
struct EyeAlgorithm <: AbstractAlgorithm end
174198

175199
for f in [:eig, :eigh, :lq, :qr, :polar, :svd]

test/fillarrays.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ using FillArrays: SquareEye
6464
@test iszero(R)
6565
@test R isa Zeros
6666

67-
# A = Zeros(4, 3)
68-
# Q, R = @constinferred left_polar(A)
69-
# @test Q * R == A
70-
# @test size(Q) == (4, 3)
71-
# @test size(R) == (3, 3)
72-
# @test Q == Matrix(I, (4, 3))
73-
# @test Q isa Eye
74-
# @test iszero(R)
75-
# @test R isa Zeros
67+
A = Zeros(4, 3)
68+
Q, R = @constinferred left_polar(A)
69+
@test Q * R == A
70+
@test size(Q) == (4, 3)
71+
@test size(R) == (3, 3)
72+
@test Q == Matrix(I, (4, 3))
73+
@test Q isa Eye
74+
@test iszero(R)
75+
@test R isa Zeros
7676

7777
A = Zeros(3, 4)
7878
L, Q = @constinferred lq_compact(A)
@@ -109,15 +109,15 @@ using FillArrays: SquareEye
109109
@test Q == Eye(4)
110110
@test Q isa Eye
111111

112-
# A = Zeros(3, 4)
113-
# L, Q = @constinferred right_polar(A)
114-
# @test L * Q == A
115-
# @test size(L) == (3, 3)
116-
# @test size(Q) == (3, 4)
117-
# @test iszero(L)
118-
# @test L isa Zeros
119-
# @test Q == Matrix(I, (3, 4))
120-
# @test Q isa Eye
112+
A = Zeros(3, 4)
113+
L, Q = @constinferred right_polar(A)
114+
@test L * Q == A
115+
@test size(L) == (3, 3)
116+
@test size(Q) == (3, 4)
117+
@test iszero(L)
118+
@test L isa Zeros
119+
@test Q == Matrix(I, (3, 4))
120+
@test Q isa Eye
121121

122122
A = Zeros(3, 4)
123123
U, S, V = @constinferred svd_compact(A)

0 commit comments

Comments
 (0)