Skip to content

Commit f7de6d2

Browse files
mcabbottMichael Abbott
andauthored
Make kron(a,b) preserve Adjoint-ness (#35755)
* kron(adjoint vector) * tests * docstring for kron * the implementation * better idea Co-authored-by: Michael Abbott <me@escbook>
1 parent a47cf00 commit f7de6d2

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

stdlib/LinearAlgebra/src/adjtrans.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ similar(A::AdjOrTrans, ::Type{T}, dims::Dims{N}) where {T,N} = similar(A.parent,
221221
# sundry basic definitions
222222
parent(A::AdjOrTrans) = A.parent
223223
vec(v::TransposeAbsVec) = parent(v)
224+
vec(v::AdjointAbsVec{<:Real}) = parent(v)
224225

225226
### concatenation
226227
# preserve Adjoint/Transpose wrapper around vectors

stdlib/LinearAlgebra/src/dense.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,12 @@ end
341341
342342
Kronecker tensor product of two vectors or two matrices.
343343
344-
For vectors v and w, the Kronecker product is related to the outer product by
345-
`kron(v,w) == vec(w*transpose(v))` or
346-
`w*transpose(v) == reshape(kron(v,w), (length(w), length(v)))`.
344+
For real vectors `v` and `w`, the Kronecker product is related to the outer product by
345+
`kron(v,w) == vec(w * transpose(v))` or
346+
`w * transpose(v) == reshape(kron(v,w), (length(w), length(v)))`.
347347
Note how the ordering of `v` and `w` differs on the left and right
348348
of these expressions (due to column-major storage).
349+
For complex vectors, the outer product `w * v'` also differs by conjugation of `v`.
349350
350351
# Examples
351352
```jldoctest
@@ -400,6 +401,9 @@ kron(a::AbstractVector, b::AbstractVector) = vec(kron(reshape(a ,length(a), 1),
400401
kron(a::AbstractMatrix, b::AbstractVector) = kron(a, reshape(b, length(b), 1))
401402
kron(a::AbstractVector, b::AbstractMatrix) = kron(reshape(a, length(a), 1), b)
402403

404+
kron(a::AdjointAbsVec, b::AdjointAbsVec) = adjoint(kron(adjoint(a), adjoint(b)))
405+
kron(a::AdjOrTransAbsVec, b::AdjOrTransAbsVec) = transpose(kron(transpose(a), transpose(b)))
406+
403407
# Matrix power
404408
(^)(A::AbstractMatrix, p::Integer) = p < 0 ? power_by_squaring(inv(A), -p) : power_by_squaring(A, p)
405409
function (^)(A::AbstractMatrix{T}, p::Integer) where T<:Integer

stdlib/LinearAlgebra/test/adjtrans.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ end
271271

272272
@testset "Adjoint and Transpose vector vec methods" begin
273273
intvec = [1, 2]
274-
@test vec(Adjoint(intvec)) == intvec
274+
@test vec(Adjoint(intvec)) === intvec
275275
@test vec(Transpose(intvec)) === intvec
276276
cvec = [1 + 1im]
277277
@test vec(cvec')[1] == cvec[1]'

stdlib/LinearAlgebra/test/dense.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,17 @@ end
395395
@test kron(b',2) == [8 10 12]
396396
end
397397

398+
@testset "kron adjoint" begin
399+
a = [1+im, 2, 3]
400+
b = [4, 5, 6+7im]
401+
@test kron(a', b') isa Adjoint
402+
@test kron(a', b') == kron(a, b)'
403+
@test kron(transpose(a), b') isa Transpose
404+
@test kron(transpose(a), b') == kron(permutedims(a), collect(b'))
405+
@test kron(transpose(a), transpose(b)) isa Transpose
406+
@test kron(transpose(a), transpose(b)) == transpose(kron(a, b))
407+
end
408+
398409
@testset "issue #4796" begin
399410
dim=2
400411
S=zeros(Complex,dim,dim)

0 commit comments

Comments
 (0)