Skip to content

Commit

Permalink
fix for Kronecker product scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Guevara Vasquez authored and dpo committed Nov 12, 2022
1 parent 3f54456 commit 9326bdc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/kron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ function kron(A::AbstractLinearOperator, B::AbstractLinearOperator)
S = promote_type(T, eltype(x))
X = reshape(convert(Vector{S}, x), q, n)
if β == zero(T2)
res .= Matrix(B * X * transpose(A))[:]
res .= α .* Matrix(B * X * transpose(A))[:]
else
res .= Matrix(B * X * transpose(A))[:] .+ β .* res
res .= α .* Matrix(B * X * transpose(A))[:] .+ β .* res
end
end
function tprod!(res, x, α, β::T2) where {T2}
S = promote_type(T, eltype(x))
X = reshape(convert(Vector{S}, x), p, m)
if β == zero(T2)
res .= Matrix(transpose(B) * X * A)[:]
res .= α .* Matrix(transpose(B) * X * A)[:]
else
res .= Matrix(transpose(B) * X * A)[:] .+ β .* res
res .= α .* Matrix(transpose(B) * X * A)[:] .+ β .* res
end
end
function ctprod!(res, x, α, β::T2) where {T2}
S = promote_type(T, eltype(x))
X = reshape(convert(Vector{S}, x), p, m)
if β == zero(T2)
res .= Matrix(B' * X * conj(A))[:]
res .= α .* Matrix(B' * X * conj(A))[:]
else
res .= Matrix(B' * X * conj(A))[:] .+ β .* res
res .= α .* Matrix(B' * X * conj(A))[:] .+ β .* res
end
end
symm = issymmetric(A) && issymmetric(B)
Expand Down
10 changes: 10 additions & 0 deletions test/test_kron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ function test_kron()
y = K * x
@test eltype(y) == Complex{Float64}
end

@testset ExtendedTestSet "kron scaling" begin
A =2kron(opEye(2),I(1))
x = randn(2)
y1=A*x
y2=A'*x
y3=transpose(A)*x
err = norm(y1-2x) + norm(y2-2x) + norm(y3-2x)
@test err < 1e-12
end
end

test_kron()

0 comments on commit 9326bdc

Please sign in to comment.