# How to Kronecker by row (one dimension only)

**URL:** <https://discourse.julialang.org/t/how-to-kronecker-by-row-one-dimension-only/33942>\
**Category:** General Usage\
**Tags:** tensors\
**Created:** [January 29, 2020, 5:09pm UTC](https://discourse.julialang.org/t/how-to-kronecker-by-row-one-dimension-only/33942 "2020-01-29T17:09:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [January 29, 2020, 5:09pm UTC](https://discourse.julialang.org/t/how-to-kronecker-by-row-one-dimension-only/33942/1 "2020-01-29T17:09:50Z")

</div>

I have two matrices `A` and `B`. Rather than a full Kronecker product `kron(A,B)`, I would like to Kronecker in a row-by-row fashion. That is, the first row of the resulting matrix should have `kron(A[1,:],B[1,:])`, the second row `kron(A[2,:],B[2,:])`, and so on. The two matrices will have the same number of rows of course.

`mapslices` seems to only accept one item, not two. I think I want something like `map(kron,zip(eachrow(A),eachrow(B)))` but this does not work. It is also important that the ordering of the columns is the same as what `kron(A,B)` would produce.

I have a function that works, but when is that ever enough? 😃

```julia
function kronrow(X,Y)
    N,K = size(X)
    L = size(Y,2)
    res = Matrix{Float64}(undef,N,K*L)
    for i=1:N
        res[i,:] = @views kron(X[i,:],Y[i,:])
    end
    return res
end

```

Is there a better way?

---

<div class="post-metadata">

**Author:** ![platawiec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/platawiec/32/31914_2.png) [@platawiec](https://discourse.julialang.org/u/platawiec)\
**Post date:** [January 29, 2020, 7:36pm UTC](https://discourse.julialang.org/t/how-to-kronecker-by-row-one-dimension-only/33942/2 "2020-01-29T19:36:13Z")

</div>

Have you taken a look at [GitHub - MichielStock/Kronecker.jl: A general-purpose toolbox for efficient Kronecker-based algebra.](https://github.com/MichielStock/Kronecker.jl)? It provides lazy evaluation of Kronecker products and may fit your use case, though I don’t believe there is a built-in function which does as your request.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 29, 2020, 7:59pm UTC](https://discourse.julialang.org/t/how-to-kronecker-by-row-one-dimension-only/33942/3 "2020-01-29T19:59:36Z")

</div>

Yes, you can do this just by reshaping and broadcasting:

```julia
X, Y = rand(Int8, 2,2), ones(Int, 2,3);
kronrow(X, Y)

using TensorCast
@cast K[r,(j,i)] := X[r,i] * Y[r,j] # (j,i) to match kron

hkron(X::AbstractMatrix, Y::AbstractMatrix) = reshape(reshape(X, size(X,1), 1, size(X,2)) .* Y, size(X,1), size(X,2) * size(Y,2))
hkron(X,Y)

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [January 29, 2020, 8:38pm UTC](https://discourse.julialang.org/t/how-to-kronecker-by-row-one-dimension-only/33942/4 "2020-01-29T20:38:26Z")

</div>

Thanks! Both of those are faster than `kronrow` – 2-3x for my problem size, but 10x faster for larger problems.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 5, 2024, 9:14pm UTC](https://discourse.julialang.org/t/how-to-kronecker-by-row-one-dimension-only/33942/5 "2024-02-05T21:14:29Z")

</div>

Embarrassing to google a question and have your own thread on Discourse from 4 years ago be the top result!
