How to resolve the algorithm Kronecker product step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Kronecker product step by step in the Nim programming language
Table of Contents
Problem Statement
Implement the Kronecker product of two matrices (arbitrary sized) resulting in a block matrix.
Show results for each of the following two samples:
Sample 1 (from Wikipedia): Sample 2:
See implementations and results below in JavaScript and PARI/GP languages.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Kronecker product step by step in the Nim programming language
Source code in the nim programming language
import strutils
type Matrix[M, N: static Positive; T: SomeNumber] = array[M, array[N, T]]
func kroneckerProduct[M, N, P, Q: static int; T: SomeNumber](
a: Matrix[M, N, T], b: Matrix[P, Q, T]): Matrix[M * P, N * Q, T] =
for i in 0..<M:
for j in 0..<N:
for k in 0..<P:
for l in 0..<Q:
result[i * P + k][j * Q + l] = a[i][j] * b[k][l]
proc `$`(m: Matrix): string =
for row in m:
result.add '['
let length = result.len
for val in row:
result.addSep(" ", length)
result.add ($val).align(2)
result.add "]\n"
when isMainModule:
const
A1: Matrix[2, 2, int] = [[1, 2], [3, 4]]
B1: Matrix[2, 2, int] = [[0, 5], [6, 7]]
echo "Matrix A:\n", A1
echo "Matrix B:\n", B1
echo "Kronecker product:\n", kroneckerProduct(A1, B1)
const
A2: Matrix[3, 3, int] = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
B2: Matrix[3, 4, int] = [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]]
echo "Matrix A:\n", A2
echo "Matrix B:\n", B2
echo "Kronecker product:\n", kroneckerProduct(A2, B2)
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Ultimate++ programming language
You may also check:How to resolve the algorithm Abelian sandpile model/Identity step by step in the Python programming language
You may also check:How to resolve the algorithm Padovan n-step number sequences step by step in the REXX programming language
You may also check:How to resolve the algorithm Successive prime differences step by step in the Prolog programming language