Skip to content

Commit 9326bdc

Browse files
Fernando Guevara Vasquezdpo
authored andcommitted
fix for Kronecker product scaling
1 parent 3f54456 commit 9326bdc

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/kron.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ function kron(A::AbstractLinearOperator, B::AbstractLinearOperator)
1515
S = promote_type(T, eltype(x))
1616
X = reshape(convert(Vector{S}, x), q, n)
1717
if β == zero(T2)
18-
res .= Matrix(B * X * transpose(A))[:]
18+
res .= α .* Matrix(B * X * transpose(A))[:]
1919
else
20-
res .= Matrix(B * X * transpose(A))[:] .+ β .* res
20+
res .= α .* Matrix(B * X * transpose(A))[:] .+ β .* res
2121
end
2222
end
2323
function tprod!(res, x, α, β::T2) where {T2}
2424
S = promote_type(T, eltype(x))
2525
X = reshape(convert(Vector{S}, x), p, m)
2626
if β == zero(T2)
27-
res .= Matrix(transpose(B) * X * A)[:]
27+
res .= α .* Matrix(transpose(B) * X * A)[:]
2828
else
29-
res .= Matrix(transpose(B) * X * A)[:] .+ β .* res
29+
res .= α .* Matrix(transpose(B) * X * A)[:] .+ β .* res
3030
end
3131
end
3232
function ctprod!(res, x, α, β::T2) where {T2}
3333
S = promote_type(T, eltype(x))
3434
X = reshape(convert(Vector{S}, x), p, m)
3535
if β == zero(T2)
36-
res .= Matrix(B' * X * conj(A))[:]
36+
res .= α .* Matrix(B' * X * conj(A))[:]
3737
else
38-
res .= Matrix(B' * X * conj(A))[:] .+ β .* res
38+
res .= α .* Matrix(B' * X * conj(A))[:] .+ β .* res
3939
end
4040
end
4141
symm = issymmetric(A) && issymmetric(B)

test/test_kron.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ function test_kron()
4646
y = K * x
4747
@test eltype(y) == Complex{Float64}
4848
end
49+
50+
@testset ExtendedTestSet "kron scaling" begin
51+
A =2kron(opEye(2),I(1))
52+
x = randn(2)
53+
y1=A*x
54+
y2=A'*x
55+
y3=transpose(A)*x
56+
err = norm(y1-2x) + norm(y2-2x) + norm(y3-2x)
57+
@test err < 1e-12
58+
end
4959
end
5060

5161
test_kron()

0 commit comments

Comments
 (0)