计算两个矩阵的克罗内克总和


9

在下面的例子中,AB将是2×2矩阵,和矩阵是一个索引。

一个克罗内克产品具有以下特性:

A⊗B =  A(1,1)*B   A(1,2)*B
        A(2,1)*B   A(2,2)*B

     =  A(1,1)*B(1,1)   A(1,1)*B(1,2)   A(1,2)*B(1,1)   A(1,2)*B(1,2)
        A(1,1)*B(2,1)   A(1,1)*B(2,2)   A(1,2)*B(2,1)   A(1,2)*B(2,2)
        A(2,1)*B(1,1)   A(2,1)*B(1,2)   A(2,2)*B(1,1)   A(2,2)*B(1,2)
        A(2,2)*B(2,1)   A(2,2)*B(1,2)   A(2,2)*B(2,1)   A(2,2)*B(2,2)

一个Kronecker和具有以下特性:

A⊕B = A⊗Ib + Ia⊗B

IaIb单位矩阵与的尺寸AB分别。A并且B是平方矩阵。请注意,A并且B可以具有不同的大小。

A⊕B =  A(1,1)+B(1,1)  B(1,2)         A(1,2)         0
        B(2,1)         A(1,1)+B(2,2)  0              A(1,2)
        A(2,1)         0              A(2,2)+B(1,1)  B(1,2)
        0              A(2,1)         B(2,1)         A(2,2)+B(2,2)

给定两个平方矩阵AB,计算两个矩阵的Kronecker和。

  • 矩阵的大小至少为2-by-2。默认情况下,最大大小将是您的计算机/语言可以处理的大小,但是最小5-by-5输入(5 MB输出)。
  • 所有输入值均为非负整数
  • 不允许使用用于计算Kronecker和或Kronecker乘积的内置函数
  • 一般而言:有关I / O格式,程序和功能,漏洞等的标准规则。

测试用例:

A =
     1     2
     3     4
B =
     5    10
     7     9

A⊕B =
     6    10     2     0
     7    10     0     2
     3     0     9    10
     0     3     7    13

----

A =
    28    83    96
     5    70     4
    10    32    44
B =
    39    19    65
    77    49    71
    80    45    76

A⊕B =
    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

----

A =
    76    57    54
    76     8    78
    39     6    94
B =
    59    92
    55    29

A⊕B =
   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

Answers:


2

果冻26 21 20 19 字节

æ*9Bs2¤×€€/€S;"/€;/

输入是两个2D列表的列表,输出是单个2D列表。在线尝试!验证所有测试用例

怎么运行的

æ*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.

这么多欧元...您的程序很丰富!
路易斯·门多

5

CJam,40 39 38字节

9Yb2/q~f{.{_,,_ff=?}:ffff*::.+:~}:..+p

输入格式是包含AB作为2D列表的列表,例如

[[[1 2] [3 4]] [[5 10] [7 9]]]

输出格式是单个CJam样式的2D列表。

测试套件。(具有更易读的输出格式。)

说明

此代码是复合(或中缀)运算符的练习。这些通常对数组操作很有用,但是这一挑战加剧了对它们的需求。快速概述:

  • f期望有一个列表和堆栈上的其他内容,然后在列表上映射以下二进制运算符,并传入另一个元素作为第二个参数。例如[1 2 3] 2 f*2 [1 2 3] f*都给[2 4 6]。如果两个元素都是列表,则第一个元素将被映射,第二个元素将用于咖喱数运算符。
  • :有两个用途:如果后面的运算符是一元的,则这是一个简单的映射。例如,[1 0 -1 4 -3] :z[1 0 1 4 3],其中z获得数字的模数。如果跟在它后面的运算符是二进制的,则它将折叠运算符。例如[1 2 3 4] :+10
  • .向量化二进制运算符。它期望两个列表作为参数,并将运算符应用于相应的对。例如[1 2 3] [5 7 11] .*[5 14 33]

需要注意的是:本身始终是一元运算符,而f.自己总是二元运算符。这些可以任意嵌套(前提是它们具有正确的Arities)。这就是我们要做的...

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.

fffffffffff到底是什么...我希望高尔夫球能够生存下来,以便您最终解释一下:P
FryAmTheEggman

@FryAmTheEggman :ffff*可能是我曾经在CJam使用时间最长的(化合物)操作......对于一个字节一个可以去更疯狂,但:9Yb2/Q~f.{\{,,_ff=}&}::ffff*:::.+::~:..+p(和是的,当我完成了高尔夫将增加的解释)。
Martin Ender

4

J- 38 33 31字节

i=:=@i.@#
[:,./^:2(*/i)+(*/~i)~

用法

   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

如果其中一个矩阵是奇异的,则使用矩阵除法将失败。例如,(2 2 $ 1 2 3 4) f (2 2 $ 1 1 1 1)将引发域错误。
丹尼斯

@Dennis不错,我只是针对随机值进行测试? 4 4 $ 100。我不确定这里是否有使用dyad撰写x f&g y = (g x) f (g y)或其他方式的方法。
2016年

2

朱莉娅60 59 58 56字节

A%B=hvcat(sum(A^0),sum(i->map(a->a*B^i,A'^-~-i),0:1)...)

在线尝试!

怎么运行的

  • 对于矩阵ABmap(a->a*B,A')计算Kronecker积A⊗B

    结果是维度为B的矩阵块的向量。

    由于矩阵以列优先顺序存储,因此我们必须转置A(带有')。

  • 由于具有二进制补码的按位NOT不能满足所有整数n的恒等式〜n =-(n + 1),因此我们具有-〜-n =-(〜(-n))=-(((-n)+ 1) = 1-n,所以-〜-0 = 1-〜-1 = 0

    通过这种方式,匿名函数i->map(a->a*B^i,A'^-~-i)应用上述地图B⁰(与单位矩阵的尺寸)和A'= AI = 0,以及B 1A⁰I = 1

  • sum(i->map(a->a*B^i,A'^-~-i),0:1)使用上述匿名函数对{0,1}求和,将Kronecker和A⊕B计算为AAB⊗+A⁰⊗B⁰⊗

    结果是维度为B的矩阵块的向量。

  • sum(A^0)计算A维度的单位矩阵的所有条目的总和。对于n×n的矩阵A,得出n

  • 最后,hvcat(sum(A^0),sum(i->map(a->a*B^i,A'^-~-i),0:1)...)连接形成A⊕B的矩阵块。

    使用第一个参数n,水平地hvcat连接n个矩阵块,而垂直地连接结果(较大)的块。


0

露比102

->a,b{r=0..-1+a.size*q=b.size
r.map{|i|r.map{|j|(i/q==j/q ?b[i%q][j%q]:0)+(i%q==j%q ?a[i/q][j/q]:0)}}}

在测试程序中

f=->a,b{r=0..-1+a.size*q=b.size
r.map{|i|r.map{|j|(i/q==j/q ?b[i%q][j%q]:0)+(i%q==j%q ?a[i/q][j/q]:0)}}}

aa =[[1,2],[3,4]]
bb =[[5,10],[7,9]]
f[aa,bb].each{|e|p e}
puts

aa =[[28,83,96],[5,70,4],[10,32,44]]
bb =[[39,19,65],[77,49,71],[80,45,76]]
f[aa,bb].each{|e|p e}
puts

aa =[[76,57,54],[76,8,78],[39,6,94]]
bb =[[59,92],[55,29]]
f[aa,bb].each{|e|p e}
puts

需要两个2D数组作为输入并返回一个2D数组。

可能有更好的方法:使用一个函数来避免重复;使用单个循环并打印输出。稍后再调查。


0

JavaScript(ES6),109

建立在应对其他挑战的答案上

(a,b)=>a.map((a,k)=>b.map((b,i)=>a.map((y,l)=>b.map((x,j)=>r.push(y*(i==j)+x*(k==l))),t.push(r=[]))),t=[])&&t

测试

f=(a,b)=>a.map((a,k)=>b.map((b,i)=>a.map((y,l)=>b.map((x,j)=>r.push(y*(i==j)+x*(k==l))),t.push(r=[]))),t=[])&&t

console.log=x=>O.textContent+=x+'\n'

function show(label, mat)
{
  console.log(label)
  console.log(mat.join`\n`)
}

;[ 
  {a:[[1,2],[3,4]], b:[[5,10],[7,9]]},
  {a:[[28,83,96],[5,70,4],[10,32,44]], b:[[39,19,65],[77,49,71],[80,45,76]]},
  {a:[[76,57,54],[76,8,78],[39,6,94]], b:[[59,92],[55,29]]}
].forEach(t=>{
  show('A',t.a)  
  show('B',t.b)
  show('A⊕B',f(t.a,t.b))
  show('B⊕A',f(t.b,t.a))  
  console.log('-----------------')
})
<pre id=O></pre>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.