Calculate the Kronecker sum of two matrices
CJam, 40 39 38 bytes
9Yb2/q~f{.{_,,_ff=?}:ffff*::.+:~}:..+p
Input format is a list containing A
and B
as 2D lists, e.g.
[[[1 2] [3 4]] [[5 10] [7 9]]]
Output format is a single CJam-style 2D list.
Test suite. (With more readable output format.)
Explanation
This code is an exercise in compound (or infix) operators. These are generally useful for array manipulation, but this challenge exacerbated the need for them. Here is a quick overview:
f
expects a list and something else on the stack and maps the following binary operator over the list, passing in the other element as the second argument. E.g.[1 2 3] 2 f*
and2 [1 2 3] f*
both give[2 4 6]
. If both elements are lists, the first one is mapped over and the second one is used to curry the binary operator.:
has two uses: if the operator following it is unary, this is a simple map. E.g.[1 0 -1 4 -3] :z
is[1 0 1 4 3]
, wherez
gets the modulus of a number. If the operator following it is binary, this will fold the operator instead. E.g.[1 2 3 4] :+
is10
..
vectorises a binary operator. It expects two lists as arguments and applies the operator to corresponding pairs. E.g.[1 2 3] [5 7 11] .*
gives[5 14 33]
.
Note that :
itself is always a unary operator, whereas f
and .
themselves are always binary operators. These can be arbitrarily nested (provided they have the right arities). And that's what we'll do...
9Yb e# Push the binary representation of 9, i.e. [1 0 0 1].
2/ e# Split into pairs, i.e. [[1 0] [0 1]]. We'll use these to indicate
e# which of the two inputs we turn into an identity matrix.
q~ e# Read and evaluate input, [A B].
f{ e# This block is mapped over the [[1 0] [0 1]] list, also pushing
e# [A B] onto the stack for each iteration.
.{ e# The stack has either [1 0] [A B] or [0 1] [A B]. We apply this
e# block to corresponding pairs, e.g. 1 A and 0 B.
_, e# Duplicate the matrix and get its length/height N.
,_ e# Turn into a range [0 1 ... N-1] and duplicate it.
ff= e# Double f on two lists is an interesting idiom to compute an
e# outer product: the first f means that we map over the first list
e# with the second list as an additional parameter. That means for
e# the remaining operator the two arguments are a single integer
e# and a list. The second f then maps over the second list, passing
e# in the the number from the outer map as the first parameter.
e# That means the operator following ff is applied to every possible
e# pair of values in the two lists, neatly laid out in a 2D list.
e# The operator we're applying is an equality check, which is 1
e# only along the diagonal and 0 everywhere else. That is, we've
e# created an NxN identity matrix.
? e# Depending on whether the integer we've got along with the matrix
e# is 0 or 1, either pick the original matrix or the identity.
}
e# At this point, the stack contains either [A Ib] or [Ia B].
e# Note that A, B, Ia and Ib are all 2D matrices.
e# We now want to compute the Kronecker product of this pair.
:ffff* e# The ffff* is the important step for the Kronecker product (but
e# not the whole story). It's an operator which takes two matrices
e# and replaces each cell of the first matrix with the second matrix
e# multiplied by that cell (so yeah, we'll end up with a 4D list of
e# matrices nested inside a matrix).
e# The leading : is a fold operation, but it's a bit of a degenerate
e# fold operation that is only used to apply the following binary operator
e# to the two elements of a list.
e# Now the ffff* works essentially the same as the ff= above, but
e# we have to deal with two more dimensions now. The first ff maps
e# over the cells of the first matrix, passing in the second matrix
e# as an additional argument. The second ff then maps over the second
e# matrix, passing in the cell from the outer map. We multiply them
e# with *.
e# Just to recap, we've essentially got the Kronecker product on the
e# stack now, but it's still a 4D list not a 2D list.
e# The four dimensions are:
e# 1. Columns of the outer matrix.
e# 2. Rows of the outer matrix.
e# 3. Columns of the submatrices.
e# 4. Rows of the submatrices.
e# We need to unravel that into a plain 2D matrix.
::.+ e# This joins the rows of submatrices across columns of the outer matrix.
e# It might be easiest to read this from the right:
e# + Takes two rows and concatenates them.
e# .+ Takes two matrices and concatenates corresponding rows.
e# :.+ Takes a list of matrices and folds .+ over them, thereby
e# concatenating the corresponding rows of all matrices.
e# ::.+ Maps this fold operation over the rows of the outer matrix.
e# We're almost done now, we just need to flatten the outer-most level
e# in order to get rid of the distinction of rows of the outer matrix.
:~ e# We do this by mapping ~ over those rows, which simply unwraps them.
}
e# Phew: we've now got a list containing the two Kronecker products
e# on the stack. The rest is easy, just perform pairwise addition.
:..+ e# Again, the : is a degenerate fold which is used to apply a binary
e# operation to the two list elements. The ..+ then simply vectorises
e# addition twice, such that we add corresponding cells of the 2D matrices.
p e# All done, just pretty-print the matrix.
J - 38 33 31 bytes
i=:=@i.@#
[:,./^:2(*/i)+(*/~i)~
Usage
f =: [:,./^:2(*/i)+(*/~i)~
(2 2 $ 1 2 3 4) f (2 2 $ 5 10 7 9)
6 10 2 0
7 10 0 2
3 0 9 10
0 3 7 13
(3 3 $ 28 83 96 5 70 4 10 32 44) f (3 3 $ 39 19 65 77 49 71 80 45 76)
67 19 65 83 0 0 96 0 0
77 77 71 0 83 0 0 96 0
80 45 104 0 0 83 0 0 96
5 0 0 109 19 65 4 0 0
0 5 0 77 119 71 0 4 0
0 0 5 80 45 146 0 0 4
10 0 0 32 0 0 83 19 65
0 10 0 0 32 0 77 93 71
0 0 10 0 0 32 80 45 120
(3 3 $ 76 57 54 76 8 78 39 6 94) f (2 2 $ 59 92 55 29)
135 92 57 0 54 0
55 105 0 57 0 54
76 0 67 92 78 0
0 76 55 37 0 78
39 0 6 0 153 92
0 39 0 6 55 123
Jelly, 26 21 20 19 bytes
æ*9Bs2¤×€€/€S;"/€;/
Input is a list of two 2D lists, output is a single 2D list. Try it online! or verify all test cases.
How it works
æ*9Bs2¤×€€/€S;"/€;/ Main link.
Argument: [A, B] (matrices of dimensions n×n and m×m)
¤ Evaluate the four links to the left as a niladic chain.
9B Convert 9 to base 2, yielding [1, 0, 0, 1].
s2 Split into sublists of length 2, yielding [[1, 0], [0, 1]].
æ* Vectorized matrix power.
This yields [[A¹, B⁰], [A⁰, B¹]], where B⁰ and A⁰ are the
identity matrices of dimensions m×m and n×n.
/€ Reduce each pair by the following:
€€ For each entry of the first matrix:
× Multiply the second matrix by that entry.
S Sum the two results, element by element.
This yields the Kronecker sum, in form of a n×n matrix of
m×m matrices.
/€ Reduce each row of the outer matrix...
;" by zipwith-concatenation.
This concatenates the columns of the matrices in each row,
yielding a list of length n of n×nm matrices.
;/ Concatenate the lists, yielding a single nm×nm matrix.