title: Bash - Multiply Bidimensional Matrix
body: <p>Supposing that I have bidimensional matrix n x n, and we already know n=4 and values 1 - 16 like <img src="https://s7.postimg.org/eqw4qbzvv/prog.jpg" alt="this"> </p>
  
  <p>I must multiply the parts like:</p>
  
  <p>Array1*Array2 = result</p>
  
  <p>Array3*result = result1</p>
  
  <p>Array4*result1 = result2</p>
  
  <pre><code>ShowMatrix()
  {
  echo "MyMatrix is:"
  for((i=0;i&lt;$n;i++))do
     for((j=0;j&lt;$n;j++))do
  printf '  '${myArray[i*n+j]}
  done
  printf '\n';
  done
  }
  </code></pre>
  
  <p>I tried to divide it into 4 parts like in the schema, and assign each part to Array1, Array2, Array3 and Array4</p>
  
  <pre><code>cut1()
  {
  for((i=0;i&lt;$n/2;i++))do
     for((j=0;j&lt;$n/2;j++))do
  Array1[i*n+j]=${myArray[i*n+j]}
  done
  done
  }
  
  cut2()
  {
  for((i=0;i&lt;$n/2;i++))do
     for((j=$n/2;j&lt;$n;j++))do
  Array2[i*n+j]=${myArray[i*n+j]}
  done
  done
  }
  
  cut3()
  {
  for((i=$n/2;i&lt;$n;i++))do
     for((j=0;j&lt;$n/2;j++))do
  Array3[i*n+j]=${myArray[i*n+j]}
  done
  done
  }
  
  cut4()
  {
  for((i=$n/2;i&lt;$n;i++))do
     for((j=$n/2;j&lt;$n;j++))do
  Array4[i*n+j]=${myArray[i*n+j]}
  done
  done
  }
  </code></pre>
  
  <p>After that, I tried to multiply them like in the schema:</p>
  
  <p>Array1*Array2 = result</p>
  
  <p>Array3*result = result1</p>
  
  <p>Array4*result1 = result2</p>
  
  <pre><code>multiply()
  {
   for((i=0;i&lt;$n;i++))do
     for((j=0;j&lt;$n;j++))do
       result[i*n+j]=0
       for((k=0;k&lt;$n;k++))do
  let "result[i*n+j]=${result[i*n+j]}+${Array1[i*n+k]}*${Array2[k*n+j]}"
  done
  done
  done
  }
  
  multiply1()
  {
   for((i=0;i&lt;$n;i++))do
     for((j=0;j&lt;$n;j++))do
       result1[i*n+j]=0
       for((k=0;k&lt;$n;k++))do
  let "result1[i*n+j]=${result1[i*n+j]}+${result[i*n+k]}*${Array3[k*n+j]}"
  done
  done
  done
  }
  
  multiply2()
  {
   for((i=0;i&lt;$n;i++))do
     for((j=0;j&lt;$n;j++))do
       result2[i*n+j]=0
       for((k=0;k&lt;$n;k++))do
  let "result2[i*n+j]=${result2[i*n+j]}+${result1[i*n+k]}*${Array4[k*n+j]}"
  done
  done
  done
  }
  </code></pre>
  
  <p>And the results, after I called functions:</p>
  
  <pre><code>cut1
  cut2
  cut3
  cut4
  multiply
  multiply1
  multiply2
  
  
  echo "result is:"
  for((i=0;i&lt;$n;i++))do
     for((j=0;j&lt;$n;j++))do
  printf '  '${result[i*n+j]}
  done
  printf '\n';
  done
  
  echo "result1 is:"
  for((i=0;i&lt;$n;i++))do
     for((j=0;j&lt;$n;j++))do
  printf '  '${result1[i*n+j]}
  done
  printf '\n';
  done
  
  echo "result2 is:"
  for((i=0;i&lt;$n;i++))do
     for((j=0;j&lt;$n;j++))do
  printf '  '${result2[i*n+j]}
  done
  printf '\n';
  done
  </code></pre>