2
$\begingroup$

If I have the matrix W size (4 x 4)

W = Array[Subscript[a, ##] &, {4, 4}]

$W=\left( \begin{array}{cccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \\ \end{array} \right)$

I use this code

With[{inds = DeleteCases[Range@Length@W, 2]}, 
 Table[W[[i, j]] + If[i != j, 1, 0] W[[i, 2]] W[[2, j]], {i, 
   inds}, {j, inds}]]
% // MatrixForm

$F=\left( \begin{array}{ccc} a_{1,1} & a_{1,3}+a_{1,2} a_{2,3} & a_{1,4}+a_{1,2} a_{2,4} \\ a_{3,1}+a_{2,1} a_{3,2} & a_{3,3} & a_{2,4} a_{3,2}+a_{3,4} \\ a_{4,1}+a_{2,1} a_{4,2} & a_{2,3} a_{4,2}+a_{4,3} & a_{4,4} \\ \end{array} \right)$

I need to repeat this code again to get a 2x2 matrix

With[{inds = DeleteCases[Range@Length@F, 2]}, 
 Table[F[[i, j]] + If[i != j, 1, 0] F[[i, 2]] F[[2, j]], {i, 
   inds}, {j, inds}]]
% // MatrixForm

$\scriptsize D=\left( \begin{array}{cc}a_{1,1} & a_{1,4}+a_{1,2}a_{2,4}+\left(a_{1,3}+a_{1,2}a_{2,3}\right)\left(a_{2,4}a_{3,2}+a_{3,4}\right)\\ a_{4,1}+a_{2,1}a_{4,2}+\left(a_{3,1}+a_{2,1}a_{3,2}\right)\left(a_{2,3}a_{4,2}+a_{4,3}\right) & a_{4,4} \\ \end{array} \right)$

then apply

mi = Expand[Part[D, 1, 2]]

$a_{1,4}+a_{1,2} a_{2,4}+a_{1,3} a_{2,4} a_{3,2}+a_{1,2} a_{2,3}a_{2,4}a_{3,2}+a_{1,3} a_{3,4}+a_{1,2} a_{2,3} a_{3,4}$

Is there a way to write this code in the form of a program executed once without the need to repeat to make in general.

Such as matrix 5x5 we need redundancy 3 times to get 2x2 and the application of the condition

$\endgroup$

2 Answers 2

2
$\begingroup$
ClearAll[f]
f[m_, r_] :=  Module[{inds = DeleteCases[Range @ Length @ m, r]}, 
   Table[m[[i, j]] + If[i != j, 1, 0] m[[i, r]] m[[r, j]], {i, inds}, {j, inds}]]

f[W, 2] // TeXForm

$\left( \begin{array}{ccc} a_{1,1} & a_{1,3}+a_{1,2} a_{2,3} & a_{1,4}+a_{1,2} a_{2,4} \\ a_{3,1}+a_{2,1} a_{3,2} & a_{3,3} & a_{2,4} a_{3,2}+a_{3,4} \\ a_{4,1}+a_{2,1} a_{4,2} & a_{2,3} a_{4,2}+a_{4,3} & a_{4,4} \\ \end{array} \right)$

Nest[f[#, 2] &, W, 2] // TeXForm

$\scriptsize \left( \begin{array}{cc} a_{1,1} & a_{1,4}+a_{1,2} a_{2,4}+\left(a_{1,3}+a_{1,2} a_{2,3}\right) \left(a_{2,4} a_{3,2}+a_{3,4}\right) \\ a_{4,1}+a_{2,1} a_{4,2}+\left(a_{3,1}+a_{2,1} a_{3,2}\right) \left(a_{2,3} a_{4,2}+a_{4,3}\right) & a_{4,4} \\ \end{array} \right)$

$\endgroup$
5
  • $\begingroup$ For W = Array[Subscript[a, ##] &, {5, 5}] I need to repeat three times $\endgroup$ Jul 11, 2017 at 17:12
  • $\begingroup$ @Emad, use Nest[f[#, 2] &, W, 3] $\endgroup$
    – kglr
    Jul 11, 2017 at 17:13
  • $\begingroup$ I put W = Array[Subscript[a, ##] &, {n = 5, n = 5}] .The condition is Nest[f[#, 2] &, W, n - 2] to be in general $\endgroup$ Jul 11, 2017 at 17:25
  • $\begingroup$ How to apply this condition ** mi = Expand[Part[qq, 1, 2]] **with your answer in general $\endgroup$ Jul 11, 2017 at 17:35
  • $\begingroup$ @Emad, if qq is defined as qq =Nest[f[#, 2] &, W, n - 2], just use mi = Expand[Part[qq, 1, 2]] $\endgroup$
    – kglr
    Jul 11, 2017 at 17:52
0
$\begingroup$
func = ExpandAll[Drop[#, {2, 2}, {2, 2}] + ReplacePart[Outer[Times, #, #2] & @@
                 Drop[{#[[All, 2]], #[[2]]}, None, {2, 2}], {a_, a_} -> 0]] &;
Nest[func, W, 2] // MatrixForm

$\scriptsize \left( \begin{array}{cc} a_{1,1} & a_{1,4}+a_{1,2} a_{2,4}+a_{1,3} a_{2,4} a_{3,2}+a_{1,2} a_{2,3} a_{2,4} a_{3,2}+a_{1,3} a_{3,4}+a_{1,2} a_{2,3} a_{3,4} \\ a_{4,1}+a_{2,1} a_{4,2}+a_{2,3} a_{3,1} a_{4,2}+a_{2,1} a_{2,3} a_{3,2} a_{4,2}+a_{3,1} a_{4,3}+a_{2,1} a_{3,2} a_{4,3} & a_{4,4} \\ \end{array} \right)$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.