Basic use
Compute a lazy Kronecker products between two matrices A and B by either
K = kronecker(A, B)or, by using the binary operator:
K = A ⊗ BNote, ⊗ can be formed by typing \otimes<tab>.
The Kronecker product K behaves like a matrix, for which size(K), eltype(K) works as one would expect. Elements can be accessed via K[i,j]; every element is computed on the fly. The function collect can be used to turn K in a regular, dense matrix.
julia> using Kroneckerjulia> A = randn(4, 4)4×4 Matrix{Float64}: 0.583244 0.844086 -1.18486 -0.745834 0.555777 0.0480946 1.44349 0.00986147 2.23951 1.52215 1.08781 -1.32124 -0.954317 -0.0276531 0.795327 -1.23486julia> B = rand(1:10, 5, 7)5×7 Matrix{Int64}: 6 1 4 7 3 2 4 3 8 5 8 3 3 5 3 9 3 6 5 9 5 6 4 2 9 2 6 2 2 5 8 4 5 10 9julia> K = A ⊗ B20×28 Kronecker.KroneckerProduct{Float64, Matrix{Float64}, Matrix{Int64}}: 3.49946 0.583244 2.33297 4.0827 … -1.49167 -2.98334 1.74973 4.66595 2.91622 4.66595 -2.2375 -3.72917 1.74973 5.24919 1.74973 3.49946 -6.71251 -3.72917 3.49946 2.33297 1.16649 5.24919 -4.475 -1.49167 1.16649 2.91622 4.66595 2.33297 -7.45834 -6.71251 3.33466 0.555777 2.22311 3.89044 … 0.0197229 0.0394459 1.66733 4.44622 2.77888 4.44622 0.0295844 0.0493073 1.66733 5.00199 1.66733 3.33466 0.0887532 0.0493073 3.33466 2.22311 1.11155 5.00199 0.0591688 0.0197229 1.11155 2.77888 4.44622 2.22311 0.0986147 0.0887532 13.437 2.23951 8.95802 15.6765 … -2.64247 -5.28495 6.71852 17.916 11.1975 17.916 -3.96371 -6.60619 6.71852 20.1556 6.71852 13.437 -11.8911 -6.60619 13.437 8.95802 4.47901 20.1556 -7.92742 -2.64247 4.47901 11.1975 17.916 8.95802 -13.2124 -11.8911 -5.7259 -0.954317 -3.81727 -6.68022 … -2.46973 -4.93946 -2.86295 -7.63454 -4.77159 -7.63454 -3.70459 -6.17432 -2.86295 -8.58885 -2.86295 -5.7259 -11.1138 -6.17432 -5.7259 -3.81727 -1.90863 -8.58885 -7.40918 -2.46973 -1.90863 -4.77159 -7.63454 -3.81727 -12.3486 -11.1138julia> K[4, 5]1.1664871420677507julia> eltype(K) # promotionFloat64julia> collect(K)20×28 Matrix{Float64}: 3.49946 0.583244 2.33297 4.0827 … -1.49167 -2.98334 1.74973 4.66595 2.91622 4.66595 -2.2375 -3.72917 1.74973 5.24919 1.74973 3.49946 -6.71251 -3.72917 3.49946 2.33297 1.16649 5.24919 -4.475 -1.49167 1.16649 2.91622 4.66595 2.33297 -7.45834 -6.71251 3.33466 0.555777 2.22311 3.89044 … 0.0197229 0.0394459 1.66733 4.44622 2.77888 4.44622 0.0295844 0.0493073 1.66733 5.00199 1.66733 3.33466 0.0887532 0.0493073 3.33466 2.22311 1.11155 5.00199 0.0591688 0.0197229 1.11155 2.77888 4.44622 2.22311 0.0986147 0.0887532 13.437 2.23951 8.95802 15.6765 … -2.64247 -5.28495 6.71852 17.916 11.1975 17.916 -3.96371 -6.60619 6.71852 20.1556 6.71852 13.437 -11.8911 -6.60619 13.437 8.95802 4.47901 20.1556 -7.92742 -2.64247 4.47901 11.1975 17.916 8.95802 -13.2124 -11.8911 -5.7259 -0.954317 -3.81727 -6.68022 … -2.46973 -4.93946 -2.86295 -7.63454 -4.77159 -7.63454 -3.70459 -6.17432 -2.86295 -8.58885 -2.86295 -5.7259 -11.1138 -6.17432 -5.7259 -3.81727 -1.90863 -8.58885 -7.40918 -2.46973 -1.90863 -4.77159 -7.63454 -3.81727 -12.3486 -11.1138
Constructing Kronecker products
Kronecker.kronecker — Function
kronecker(A::AbstractMatrix, B::AbstractMatrix)Construct a Kronecker product object between two arrays. Does not evaluate the Kronecker product explictly.
kronecker(A::AbstractMatrix, B::AbstractMatrix)Higher-order Kronecker lazy kronecker product, e.g.
kronecker(A, B, C, D)kronecker(A::AbstractMatrix, pow::Int)Kronecker power, computes A ⊗ A ⊗ ... ⊗ A. Returns a lazy KroneckerPower type.
Kronecker.:⊗ — Function
⊗(A::AbstractMatrix, B::AbstractMatrix)Binary operator for kronecker, computes as Lazy Kronecker product. See kronecker for documentation.
⊗(A::AbstractMatrix, pow::Int)Kronecker power, computes A ⊗ A ⊗ ... ⊗ A. Returns a lazy KroneckerPower type.
Base.collect — Method
collect(K::GeneralizedKroneckerProduct)Collects a lazy instance of the GeneralizedKroneckerProduct type into a dense, native matrix. Falls back to the element-wise case when not specialized method is defined.
Basic properties of Kronecker products
Base.getindex — Function
getindex(K::AbstractKroneckerProduct, i1::Integer, i2::Integer)Computes and returns the (i,j)-th element of an AbstractKroneckerProduct K. Uses recursion if K is of an order greater than two.
Base.eltype — Function
eltype(K::GeneralizedKroneckerProduct)Return the type of the elements of a Kronecker product, i.e. the promoted type of the elements of its factors.
Base.collect — Function
collect(K::GeneralizedKroneckerProduct)Collects a lazy instance of the GeneralizedKroneckerProduct type into a dense, native matrix. Falls back to the element-wise case when not specialized method is defined.
collect(K::AbstractKroneckerSum)Collects a lazy instance of the AbstractKroneckerSum type into a full, native matrix. Returns the result as a sparse matrix.
collect(E::Eigen{<:Number, <:Number, <:AbstractKroneckerProduct})Collects eigenvalue decomposition of a AbstractKroneckerProduct type into a matrix.
Kronecker.collect! — Function
collect!(C::AbstractMatrix, K::GeneralizedKroneckerProduct)In-place collection of K in C. If possible, specialized routines are used to speed up the computation. The fallback is an element-wise iteration. In this case, this function might be slow.
collect!(C::AbstractMatrix, K::AbstractKroneckerProduct)In-place collection of K in C where K is an AbstractKroneckerProduct, i.e., K = A ⊗ B. This is equivalent to the broadcasted assignment C .= K.
collect!(f, C::AbstractMatrix, K1::AbstractKroneckerProduct, Ks::AbstractKroneckerProduct...)Evaluate f.(K1, Ks...) and assign it in-place to C. This is equivalent to the broadcasted operation C .= f.(K1, Ks...).
collect!(C::AbstractMatrix, K::AbstractKroneckerSum)In-place collection of K in C where K is an AbstractKroneckerSum, i.e., K = A ⊗ B.
Kronecker.order — Function
order(M::AbstractMatrix)Returns the order of a matrix, i.e. how many matrices are involved in the Kronecker product (default to 1 for general matrices).
Kronecker.getmatrices — Function
getmatrices(K::AbstractKroneckerProduct)Obtain the two matrices of an AbstractKroneckerProduct object.
getmatrices(A::AbstractArray)Returns a matrix itself. Needed for recursion.
getmatrices(K::T) where T <: AbstractKroneckerSumObtain the two matrices of an AbstractKroneckerSum object.
Kronecker.issquare — Function
issquare(A::AbstractMatrix)Checks if an array is a square matrix.
issquare(A::Factorization)Checks if a Factorization struct represents a square matrix.
LinearAlgebra.issymmetric — Function
issymmetric(K::AbstractKroneckerProduct)Checks if a Kronecker product is symmetric.
Linear algebra
Many functions of the LinearAlgebra module are overloaded to work with subtypes of GeneralizedKroneckerProduct.
LinearAlgebra.det — Method
det(K::AbstractKroneckerProduct)Compute the determinant of a Kronecker product.
LinearAlgebra.logdet — Method
logdet(K::AbstractKroneckerProduct)Compute the logarithm of the determinant of a Kronecker product.
LinearAlgebra.tr — Method
tr(K::AbstractKroneckerProduct)Compute the trace of a Kronecker product.
Base.adjoint — Method
adjoint(K::AbstractKroneckerProduct)Compute the adjoint of a Kronecker product.
Base.transpose — Method
transpose(K::AbstractKroneckerProduct)Compute the transpose of a Kronecker product.
LinearAlgebra.isposdef — Method
isposdef(K::AbstractKroneckerProduct)Test whether a Kronecker product is positive definite (and Hermitian) by trying to perform a Cholesky factorization of K.