Skip to content

Commit f928246

Browse files
amilstedViralBShah
authored andcommitted
Enhance documentation for kron() (#28697)
* Enhance documentation for kron() Due to column-major ordering, using kron() together with reshape() can produce counter-intuitive results. Document this to avoid unnecessary pain. * Remove trailing whitespace. * Simplify reshape(kron()) example Use two vectors instead of two matrices to illustrate the reshape(kron()) behavior. This also has the benefit of providing a vector example for kron(). * Remove subjective stuff, add outer-product example The relationship between the outer product, and the Kronecker product, of two vectors illustrates the argument-ordering differences due to vec/reshape and column-major storage.
1 parent c158ff2 commit f928246

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

stdlib/LinearAlgebra/src/dense.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +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)))`.
347+
Note how the ordering of `v` and `w` differs on the left and right
348+
of these expressions (due to column-major storage).
349+
344350
# Examples
345351
```jldoctest
346352
julia> A = [1 2; 3 4]
@@ -359,6 +365,20 @@ julia> kron(A, B)
359365
1+0im 0-1im 2+0im 0-2im
360366
0+3im 3+0im 0+4im 4+0im
361367
3+0im 0-3im 4+0im 0-4im
368+
369+
julia> v = [1, 2]; w = [3, 4, 5];
370+
371+
julia> w*transpose(v)
372+
3×2 Array{Int64,2}:
373+
3 6
374+
4 8
375+
5 10
376+
377+
julia> reshape(kron(v,w), (length(w), length(v)))
378+
3×2 Array{Int64,2}:
379+
3 6
380+
4 8
381+
5 10
362382
```
363383
"""
364384
function kron(a::AbstractMatrix{T}, b::AbstractMatrix{S}) where {T,S}

0 commit comments

Comments
 (0)