Skip to content

Commit 34200fd

Browse files
committed
left and right polar for Eye
1 parent f16ebfc commit 34200fd

2 files changed

Lines changed: 107 additions & 85 deletions

File tree

ext/MatrixAlgebraKitFillArraysExt.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ end
179179
function MatrixAlgebraKit.left_polar!(A::AbstractZerosMatrix, F, alg::ZerosAlgorithm)
180180
check_input(left_polar!, A, F)
181181
U, S, Vᴴ = svd_compact(A)
182-
return (Eye((axes(U, 1), axes(Vᴴ, 2))), Vᴴ' * S * Vᴴ)
182+
return (Eye((axes(A, 1), axes(A, 2))), Vᴴ' * S * Vᴴ)
183183
end
184184

185185
function MatrixAlgebraKit.check_input(::typeof(right_polar!), A::AbstractZerosMatrix, F)
@@ -370,4 +370,32 @@ function MatrixAlgebraKit.svd_vals!(A::Eye, F, alg::EyeAlgorithm)
370370
return diagview(A)
371371
end
372372

373+
function MatrixAlgebraKit.check_input(::typeof(left_polar!), A::Eye, F)
374+
m, n = size(A)
375+
m >= n ||
376+
throw(ArgumentError("input matrix needs at least as many rows as columns"))
377+
return nothing
378+
end
379+
function MatrixAlgebraKit.left_polar!(A::Eye, F, alg::EyeAlgorithm)
380+
check_input(left_polar!, A, F)
381+
return (Eye((axes(A, 1), axes(A, 2))), Eye((axes(A, 2), axes(A, 2))))
382+
end
383+
function MatrixAlgebraKit.left_polar!(A::SquareEye, F, alg::EyeAlgorithm)
384+
return (A, A)
385+
end
386+
387+
function MatrixAlgebraKit.check_input(::typeof(right_polar!), A::Eye, F)
388+
m, n = size(A)
389+
n >= m ||
390+
throw(ArgumentError("input matrix needs at least as many columns as rows"))
391+
return nothing
392+
end
393+
function MatrixAlgebraKit.right_polar!(A::Eye, F, alg::EyeAlgorithm)
394+
check_input(right_polar!, A, F)
395+
return (Eye((axes(A, 1), axes(A, 1))), Eye((axes(A, 1), axes(A, 2))))
396+
end
397+
function MatrixAlgebraKit.right_polar!(A::SquareEye, F, alg::EyeAlgorithm)
398+
return (A, A)
399+
end
400+
373401
end

test/fillarrays.jl

Lines changed: 78 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,9 @@ using FillArrays: SquareEye
7676
@test iszero(R)
7777
@test R isa Zeros
7878

79-
A = Zeros(3, 4)
80-
L, Q = @constinferred lq_compact(A)
81-
@test L * Q == A
82-
@test size(L) == (3, 3)
83-
@test size(Q) == (3, 4)
84-
@test iszero(L)
85-
@test L isa Zeros
86-
@test Q == Matrix(I, (3, 4))
87-
@test Q isa Eye
88-
8979
for f in (lq_compact, right_orth)
9080
A = Zeros(3, 4)
91-
L, Q = @constinferred lq_compact(A)
81+
L, Q = @constinferred f(A)
9282
@test L * Q == A
9383
@test L == Zeros(3, 3)
9484
@test L isa Zeros
@@ -211,69 +201,73 @@ end
211201
end
212202
end
213203

214-
# A = Eye(4, 3)
215-
# Q, R = @constinferred qr_compact(A)
216-
# @test Q * R == A
217-
# @test size(Q) == (4, 3)
218-
# @test size(R) == (3, 3)
219-
# @test Q == Matrix(I, (4, 3))
220-
# @test Q isa Eye
221-
# @test R == I
222-
# @test R isa Eye
223-
224-
# A = Eye(3)
225-
# Q, R = @constinferred qr_compact(A)
226-
# @test Q * R == A
227-
# @test Q === A
228-
# @test R === A
229-
230-
# A = Eye(4, 3)
231-
# Q, R = @constinferred qr_full(A)
232-
# @test Q * R == A
233-
# @test size(Q) == (4, 4)
234-
# @test size(R) == (4, 3)
235-
# @test Q == I
236-
# @test Q isa Eye
237-
# @test R == I
238-
# @test R isa Eye
239-
240-
# A = Eye(3)
241-
# Q, R = @constinferred qr_full(A)
242-
# @test Q * R == A
243-
# @test Q === A
244-
# @test R === A
245-
246-
# A = Eye(4, 3)
247-
# Q, R = @constinferred left_polar(A)
248-
# @test Q * R == A
249-
# @test size(Q) == (4, 3)
250-
# @test size(R) == (3, 3)
251-
# @test Q == Matrix(I, (4, 3))
252-
# @test Q isa Eye
253-
# @test R == I
254-
# @test R isa Eye
255-
256-
# A = Eye(3)
257-
# Q, R = @constinferred left_polar(A)
258-
# @test Q * R == A
259-
# @test Q === A
260-
# @test R === A
204+
for f in (qr_compact, left_orth)
205+
A = Eye(4, 3)
206+
Q, R = @constinferred f(A)
207+
@test Q * R == A
208+
@test size(Q) == (4, 3)
209+
@test size(R) == (3, 3)
210+
@test Q == Matrix(I, (4, 3))
211+
@test Q isa Eye
212+
@test R == I
213+
@test R isa Eye
261214

262-
A = Eye(3, 4)
263-
L, Q = @constinferred lq_compact(A)
264-
@test L * Q == A
265-
@test size(L) == (3, 3)
266-
@test size(Q) == (3, 4)
267-
@test L == I
268-
@test L isa Eye
269-
@test Q == Matrix(I, (3, 4))
215+
A = Eye(3)
216+
Q, R = @constinferred f(A)
217+
@test Q * R == A
218+
@test Q === A
219+
@test R === A
220+
end
221+
222+
A = Eye(4, 3)
223+
Q, R = @constinferred qr_full(A)
224+
@test Q * R == A
225+
@test size(Q) == (4, 4)
226+
@test size(R) == (4, 3)
227+
@test Q == I
270228
@test Q isa Eye
229+
@test R == Eye(4, 3)
230+
@test R isa Eye
271231

272232
A = Eye(3)
273-
L, Q = @constinferred lq_compact(A)
274-
@test L * Q == A
275-
@test L === A
233+
Q, R = @constinferred qr_full(A)
234+
@test Q * R == A
276235
@test Q === A
236+
@test R === A
237+
238+
A = Eye(4, 3)
239+
Q, R = @constinferred left_polar(A)
240+
@test Q * R == A
241+
@test size(Q) == (4, 3)
242+
@test size(R) == (3, 3)
243+
@test Q == Matrix(I, (4, 3))
244+
@test Q isa Eye
245+
@test R == I
246+
@test R isa Eye
247+
248+
A = Eye(3)
249+
Q, R = @constinferred left_polar(A)
250+
@test Q * R == A
251+
@test Q === A
252+
@test R === A
253+
254+
for f in (lq_compact, right_orth)
255+
A = Eye(3, 4)
256+
L, Q = @constinferred lq_compact(A)
257+
@test L * Q == A
258+
@test size(L) == (3, 3)
259+
@test size(Q) == (3, 4)
260+
@test L == I
261+
@test L isa Eye
262+
@test Q == Matrix(I, (3, 4))
263+
@test Q isa Eye
264+
265+
A = Eye(3)
266+
L, Q = @constinferred lq_compact(A)
267+
@test L * Q == A
268+
@test L === A
269+
@test Q === A
270+
end
277271

278272
A = Eye(3, 4)
279273
L, Q = @constinferred lq_full(A)
@@ -291,21 +285,21 @@ end
291285
@test L === A
292286
@test Q === A
293287

294-
# A = Eye(3, 4)
295-
# L, Q = @constinferred right_polar(A)
296-
# @test L * Q == A
297-
# @test size(L) == (3, 3)
298-
# @test size(Q) == (3, 4)
299-
# @test L == I
300-
# @test L isa Eye
301-
# @test Q == Matrix(I, (3, 4))
302-
# @test Q isa Eye
303-
304-
# A = Eye(3)
305-
# L, Q = @constinferred right_polar(A)
306-
# @test L * Q == A
307-
# @test L === A
308-
# @test Q === A
288+
A = Eye(3, 4)
289+
L, Q = @constinferred right_polar(A)
290+
@test L * Q == A
291+
@test size(L) == (3, 3)
292+
@test size(Q) == (3, 4)
293+
@test L == I
294+
@test L isa Eye
295+
@test Q == Matrix(I, (3, 4))
296+
@test Q isa Eye
297+
298+
A = Eye(3)
299+
L, Q = @constinferred right_polar(A)
300+
@test L * Q == A
301+
@test L === A
302+
@test Q === A
309303

310304
A = Eye(3, 4)
311305
U, S, V = @constinferred svd_compact(A)

0 commit comments

Comments
 (0)