How to resolve the algorithm Kronecker product step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Kronecker product step by step in the jq 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 jq programming language

Source code in the jq programming language

def kprod(a; b):

  # element-wise multiplication of a matrix by a number, "c"
  def multiply(c): map( map(. * c) );

  # "right" should be a vector with the same length as the input
  def laminate(right):
    [range(0; right|length) as $i
    | (.[$i] + [right[$i]]) ];

  # "matrix" and the input matrix should have the same number of rows
  def addblock(matrix):
    reduce (matrix|transpose)[] as $v (.; laminate($v));

  (a[0]|length) as $m
  | reduce range(0; a|length) as $i ([];
      . + reduce range(0; $m) as $j ([];
        addblock( b | multiply(a[$i][$j]) ) ));

def left:  [[ 1, 2], [3, 4]];
def right: [[ 0, 5], [6, 7]];

kprod(left;right)

def left:  [[0, 1, 0], [1, 1, 1], [0, 1, 0]];
def right: [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]];

kprod(left;right)

  

You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the Ring programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the Dart programming language