Skip to content

Commit 741457c

Browse files
committed
Implement left_null and right_null
1 parent 79944cd commit 741457c

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

src/KroneckerArrays.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,12 @@ using MatrixAlgebraKit:
584584
eigh_vals,
585585
qr_compact,
586586
qr_full,
587+
left_null,
587588
left_orth,
588589
left_polar,
589590
lq_compact,
590591
lq_full,
592+
right_null,
591593
right_orth,
592594
right_polar,
593595
svd_compact,
@@ -748,10 +750,12 @@ for f in [
748750
:eigh_vals,
749751
:qr_compact,
750752
:qr_full,
753+
:left_null,
751754
:left_orth,
752755
:left_polar,
753756
:lq_compact,
754757
:lq_full,
758+
:right_null,
755759
:right_orth,
756760
:right_polar,
757761
:svd_compact,
@@ -795,6 +799,12 @@ end
795799
_initialize_output_squareeye(f::F, a) where {F} = initialize_output(f, a)
796800
_initialize_output_squareeye(f::F, a, alg) where {F} = initialize_output(f, a, alg)
797801

802+
for f in [:left_null!, :right_null!]
803+
@eval begin
804+
_initialize_output_squareeye(::typeof($f), a::SquareEye) = a
805+
_initialize_output_squareeye(::typeof($f), a::SquareEye, alg) = a
806+
end
807+
end
798808
for f in [
799809
:eig_full!,
800810
:eigh_full!,
@@ -861,6 +871,44 @@ for f in [
861871
end
862872
end
863873

874+
for f in [:left_null!, :right_null!]
875+
f′ = Symbol("_", f, "_squareeye")
876+
@eval begin
877+
$f′(a, F; kwargs...) = $f(a, F; kwargs...)
878+
$f′(a::SquareEye, F) = F
879+
end
880+
for T in [:SquareEyeKronecker, :KroneckerSquareEye]
881+
@eval begin
882+
function MatrixAlgebraKit.initialize_output(::typeof($f), a::$T)
883+
return _initialize_output_squareeye($f, a.a) _initialize_output_squareeye($f, a.b)
884+
end
885+
function MatrixAlgebraKit.$f(a::$T, F; kwargs1=(;), kwargs2=(;), kwargs...)
886+
$f′(a.a, F.a; kwargs..., kwargs1...)
887+
$f′(a.b, F.b; kwargs..., kwargs2...)
888+
return F
889+
end
890+
end
891+
end
892+
end
893+
894+
function MatrixAlgebraKit.initialize_output(f::typeof(left_null!), a::SquareEyeSquareEye)
895+
return _initialize_output_squareeye(f, a.a) _initialize_output_squareeye(f, a.b)
896+
end
897+
function MatrixAlgebraKit.left_null!(
898+
a::SquareEyeSquareEye, F; kwargs1=(;), kwargs2=(;), kwargs...
899+
)
900+
return throw(MethodError(left_null!, (a, F)))
901+
end
902+
903+
function MatrixAlgebraKit.initialize_output(f::typeof(right_null!), a::SquareEyeSquareEye)
904+
return _initialize_output_squareeye(f, a.a) _initialize_output_squareeye(f, a.b)
905+
end
906+
function MatrixAlgebraKit.right_null!(
907+
a::SquareEyeSquareEye, F; kwargs1=(;), kwargs2=(;), kwargs...
908+
)
909+
return throw(MethodError(right_null!, (a, F)))
910+
end
911+
864912
for f in [:eig_vals!, :eigh_vals!, :svd_vals!]
865913
@eval begin
866914
_initialize_output_squareeye(::typeof($f), a::SquareEye) = parent(a)

test/test_matrixalgebrakit.jl

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ end
126126
# eig_trunc
127127
# eigh_trunc
128128
# svd_trunc
129-
# left_null
130-
# right_null
131129

132130
for f in (eig_full, eigh_full)
133131
a = Eye(3) parent(hermitianpart(randn(3, 3)))
@@ -177,13 +175,13 @@ end
177175
left_orth, left_polar, lq_compact, lq_full, qr_compact, qr_full, right_orth, right_polar
178176
)
179177
a = Eye(3) randn(3, 3)
180-
x, y = f(a)
178+
x, y = @constinferred f(a)
181179
@test x * y a
182180
@test arguments(x, 1) isa Eye
183181
@test arguments(y, 1) isa Eye
184182

185183
a = randn(3, 3) Eye(3)
186-
x, y = f(a)
184+
x, y = @constinferred f(a)
187185
@test x * y a
188186
@test arguments(x, 2) isa Eye
189187
@test arguments(y, 2) isa Eye
@@ -199,21 +197,21 @@ end
199197

200198
for f in (svd_compact, svd_full)
201199
a = Eye(3) randn(3, 3)
202-
u, s, v = f(a)
200+
u, s, v = @constinferred f(a)
203201
@test u * s * v a
204202
@test arguments(u, 1) isa Eye
205203
@test arguments(s, 1) isa Eye
206204
@test arguments(v, 1) isa Eye
207205

208206
a = randn(3, 3) Eye(3)
209-
u, s, v = f(a)
207+
u, s, v = @constinferred f(a)
210208
@test u * s * v a
211209
@test arguments(u, 2) isa Eye
212210
@test arguments(s, 2) isa Eye
213211
@test arguments(v, 2) isa Eye
214212

215213
a = Eye(3) Eye(3)
216-
u, s, v = f(a)
214+
u, s, v = @constinferred f(a)
217215
@test u * s * v a
218216
@test arguments(u, 1) isa Eye
219217
@test arguments(s, 1) isa Eye
@@ -242,4 +240,33 @@ end
242240
@test d == Ones(3) Ones(3)
243241
@test arguments(d, 1) isa Ones
244242
@test arguments(d, 2) isa Ones
243+
244+
# left_null
245+
a = Eye(3) randn(3, 3)
246+
n = @constinferred left_null(a)
247+
@test norm(n' * a) 0
248+
@test arguments(n, 1) isa Eye
249+
250+
a = randn(3, 3) Eye(3)
251+
n = @constinferred left_null(a)
252+
@test norm(n' * a) 0
253+
@test arguments(n, 2) isa Eye
254+
255+
a = Eye(3) Eye(3)
256+
@test_throws MethodError left_null(a)
257+
258+
# right_null
259+
a = Eye(3) randn(3, 3)
260+
n = @constinferred right_null(a)
261+
@test norm(a * n') 0
262+
@test arguments(n, 1) isa Eye
263+
264+
a = randn(3, 3) Eye(3)
265+
n = @constinferred right_null(a)
266+
@test norm(a * n') 0
267+
@test arguments(n, 2) isa Eye
268+
269+
a = Eye(3) Eye(3)
270+
@test_throws MethodError right_null(a)
271+
245272
end

0 commit comments

Comments
 (0)