Skip to content

Commit 2996541

Browse files
committed
Implement left and right null
1 parent b5c121a commit 2996541

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

src/KroneckerArrays.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,16 @@ using MatrixAlgebraKit:
381381
TruncationStrategy,
382382
default_eig_algorithm,
383383
default_eigh_algorithm,
384+
default_qr_algorithm,
384385
eig_full!,
385386
eig_trunc!,
386387
eig_vals!,
387388
eigh_full!,
388389
eigh_trunc!,
389390
eigh_vals!,
390391
initialize_output,
392+
left_null!,
393+
right_null!,
391394
truncate!
392395

393396
struct KroneckerAlgorithm{A,B} <: AbstractAlgorithm
@@ -461,4 +464,33 @@ function MatrixAlgebraKit.eigh_vals!(a::KroneckerMatrix, F, alg::KroneckerAlgori
461464
return F
462465
end
463466

467+
function MatrixAlgebraKit.default_qr_algorithm(a::KroneckerMatrix; kwargs...)
468+
return KroneckerAlgorithm(
469+
default_qr_algorithm(a.a; kwargs...), default_qr_algorithm(a.b; kwargs...)
470+
)
471+
end
472+
function MatrixAlgebraKit.default_lq_algorithm(a::KroneckerMatrix; kwargs...)
473+
return KroneckerAlgorithm(
474+
default_lq_algorithm(a.a; kwargs...), default_lq_algorithm(a.b; kwargs...)
475+
)
476+
end
477+
478+
function MatrixAlgebraKit.initialize_output(f::typeof(left_null!), a::KroneckerMatrix)
479+
return initialize_output(f, a.a) initialize_output(f, a.b)
480+
end
481+
function MatrixAlgebraKit.left_null!(a::KroneckerMatrix, F; kwargs...)
482+
left_null!(a.a, F.a; kwargs...)
483+
left_null!(a.b, F.b; kwargs...)
484+
return F
485+
end
486+
487+
function MatrixAlgebraKit.initialize_output(f::typeof(right_null!), a::KroneckerMatrix)
488+
return initialize_output(f, a.a) initialize_output(f, a.b)
489+
end
490+
function MatrixAlgebraKit.right_null!(a::KroneckerMatrix, F; kwargs...)
491+
right_null!(a.a, F.a; kwargs...)
492+
right_null!(a.b, F.b; kwargs...)
493+
return F
494+
end
495+
464496
end

test/test_matrixalgebrakit.jl

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using KroneckerArrays:
2-
using LinearAlgebra: Hermitian, diag
2+
using LinearAlgebra: Hermitian, diag, norm
33
using MatrixAlgebraKit:
44
eig_full,
55
eig_trunc,
@@ -24,24 +24,35 @@ using MatrixAlgebraKit:
2424
using Test: @test, @test_throws, @testset
2525

2626
@testset "MatrixAlgebraKit" begin
27-
x = randn(2, 2)
28-
y = randn(3, 3)
29-
a = x y
30-
ah = Hermitian(x) Hermitian(y)
27+
elt = Float32
3128

29+
a = randn(elt, 2, 2) randn(elt, 3, 3)
3230
d, v = eig_full(a)
3331
@test a * v v * d
3432

33+
a = randn(elt, 2, 2) randn(elt, 3, 3)
3534
@test_throws MethodError eig_trunc(a)
3635

36+
a = randn(elt, 2, 2) randn(elt, 3, 3)
3737
d = eig_vals(a)
3838
@test d diag(eig_full(a)[1])
3939

40-
d, v = eigh_full(ah)
41-
@test ah * v v * d
40+
a = Hermitian(randn(elt, 2, 2)) Hermitian(randn(elt, 3, 3))
41+
d, v = eigh_full(a)
42+
@test a * v v * d
43+
44+
a = Hermitian(randn(elt, 2, 2)) Hermitian(randn(elt, 3, 3))
45+
@test_throws MethodError eigh_trunc(a)
46+
47+
a = Hermitian(randn(elt, 2, 2)) Hermitian(randn(elt, 3, 3))
48+
d = eigh_vals(a)
49+
@test d diag(eigh_full(a)[1])
4250

43-
@test_throws MethodError eigh_trunc(ah)
51+
a = randn(elt, 3, 2) randn(elt, 4, 3)
52+
n = left_null(a)
53+
@test norm(n' * a) 0 atol = eps(real(elt))
4454

45-
d = eigh_vals(ah)
46-
@test d diag(eigh_full(ah)[1])
55+
a = randn(elt, 2, 3) randn(elt, 3, 4)
56+
n = right_null(a)
57+
@test norm(a * n') 0 atol = eps(real(elt))
4758
end

0 commit comments

Comments
 (0)