Kronecker product: Difference between revisions

no edit summary
(add ooRexx)
imported>Maxima enthusiast
No edit summary
Line 2,742:
0 0 0 0 1 0 0 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0</pre>
 
=={{header|Maxima}}==
There is a built-in function kronecker autoloaded from linearalgebra package.
 
Here comes a naive implementation.
<syntaxhighlight lang="maxima">
 
/* Function to map first, second and so on, over a list of lists without recurring corresponding built-in functions */
auxkron(n,lst):=makelist(lst[k][n],k,1,length(lst));
 
/* Function to subdivide a list into lists of equal lengths */
lst_equally_subdivided(lst,n):=if mod(length(lst),n)=0 then makelist(makelist(lst[i],i,j,j+n-1),j,1,length(lst)-1,n);
 
/* Kronecker product implementation */
/* input: lists of lists trivially convertible to matrices */
my_kronecker(Alist,Blist):=block(auxlength1:length(first(Alist)),auxlength2:length(first(Blist)),L:makelist(i*Blist,i,flatten(Alist)),
L1:makelist(auxkron(j,L),j,1,auxlength1),L2:makelist(lst_equally_subdivided(L1[i],auxlength1),i,1,length(L1)),makelist(auxkron(j,L2),j,1,auxlength1),
flatten(%%),lst_equally_subdivided(%%,auxlength1*auxlength2),apply(matrix,%%));
</syntaxhighlight>
{{out}}<pre>
A:matrix([0,1,0],[1,1,1],[0,1,0])$
B:matrix([1,1,1,1],[1,0,0,1],[1,1,1,1])$
 
C:matrix([1,2],[3,4])$
D:matrix([0,5],[6,7])$
 
my_kronecker(args(A),args(B));
/* matrix(
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0]
) */
 
my_kronecker(args(C),args(D));
/* matrix(
[0, 5, 0, 10],
[6, 7, 12, 14],
[0, 15, 0, 20],
[18, 21, 24, 28]
) */
 
</pre>
 
=={{header|Nim}}==