reserve i,j,m,n,k for Nat,
  x,y for set,
  K for Field,
  a,a1,a2 for Element of K,
  D for non empty set,
  d,d1,d2 for Element of D,
  M,M1,M2 for (Matrix of D),
  A,A1,A2,B1,B2 for (Matrix of K),
  f,g for FinSequence of NAT;
reserve F,F1,F2 for FinSequence_of_Matrix of D,
  G,G9,G1,G2 for FinSequence_of_Matrix of K;

theorem Th23:
  for M be Matrix of Sum Len (<*M1,M2*>),Sum Width (<*M1,M2*>),D
holds M = block_diagonal(<*M1,M2*>,d) iff for i holds (i in dom M1 implies Line
  (M,i)=Line(M1,i)^(width M2 |-> d)) & (i in dom M2 implies Line(M,i+len M1)=(
  width M1|->d)^Line(M2,i))
proof
  let M be Matrix of Sum Len (<*M1,M2*>),Sum Width (<*M1,M2*>),D;
  set F1=<*M1*>;
  set F2=<*M2*>;
  set F12=<*M1,M2*>;
  set B=block_diagonal(F12,d);
  set L1=len M1;
  set L2=len M2;
  reconsider W1=width M1 as Element of NAT by ORDINAL1:def 12;
  set W2=width M2;
A1: Sum <*W1*>=W1 by RVSUM_1:73;
  len B=Sum Len F12 by Def5;
  then
A2: len B=L1+L2 by Th16;
A3: len F12=len Len F12 by CARD_1:def 7;
A4: (Len F12) |0=<*>REAL;
A5: (Width F12) |0=<*>REAL;
A6: 1-'1=0 by XREAL_1:232;
A7: width B=Sum Width F12 by Def5;
  then
A8: width B=W1+W2 by Th20;
A9: len <*W1*>=1 by FINSEQ_1:40;
A10: len F1=1 by FINSEQ_1:40;
A11: Len F1=<*L1*> by Th15;
A12: Len F12=(Len F1) ^(Len F2) by Th14;
  then
A13: (Len F12).1=L1 by A11,FINSEQ_1:41;
A14: len <*L1*>=1 by FINSEQ_1:40;
  then
A15: (Len F12) |1=<*L1*> by A12,A11,FINSEQ_5:23;
A16: Width F1=<*W1*> by Th19;
A17: Sum <*L1*>=L1 by RVSUM_1:73;
  Width F12=(Width F1)^(Width F2) by Th18;
  then
A18: (Width F12) |1=<*W1*> by A16,A9,FINSEQ_5:23;
A19: Len F2=<*L2*> by Th15;
  then
A20: len Len F12=1+1 by A12,A11,A14,FINSEQ_2:16;
A21: (Len F12).2=L2 by A12,A11,A19,A14,FINSEQ_1:42;
  len Width F12=len F12 by CARD_1:def 7;
  then
A22: (Width F12) |2=Width F12 by A20,A3,FINSEQ_1:58;
A23: dom Len F12={1,2} by A20,FINSEQ_1:2,def 3;
  then
A24: 1 in dom Len F12 by TARSKI:def 2;
  then
A25: (Len F12)/.1=(Len F12).1 by PARTFUN1:def 6;
A26: 2-'1=2-1 by XREAL_1:233;
  Sum Len F12=0 implies Sum Width F12=0 by Th13;
  then
A27: width M=W1+W2 by A7,A8,MATRIX13:1;
A28: 2 in dom Len F12 by A23,TARSKI:def 2;
  then
A29: (Len F12)/.2=(Len F12).2 by PARTFUN1:def 6;
  hereby
    assume
A30: M = B;
    let i;
    thus i in dom M1 implies Line(M,i)=Line(M1,i)^(W2|-> d)
    proof
      set W2d=W2|->d;
      set LM1=Line(M1,i);
      set LM=Line(M,i);
A31:  L1<=L1+L2 by NAT_1:11;
A32:  len LM1=W1 by CARD_1:def 7;
      assume
A33:  i in dom M1;
      then
A34:  1<=i by FINSEQ_3:25;
      i<=L1 by A33,FINSEQ_3:25;
      then i<=L1+L2 by A31,XXREAL_0:2;
      then
A35:  i in dom B by A2,A34,FINSEQ_3:25;
A36:  len W2d=W2 by CARD_1:def 7;
A37:  len (LM1^W2d)= len LM1+len W2d by FINSEQ_1:22;
A38:  len LM=W1+W2 by A27,CARD_1:def 7;
A39:  i in Seg L1 by A33,FINSEQ_1:def 3;
      now
A40:    min(Len F12,i+0)=1 by A24,A6,A13,A25,A4,A39,Th10,RVSUM_1:72;
        let j such that
A41:    1<=j and
A42:    j<=len LM;
A43:    j in Seg width B by A8,A38,A41,A42;
        then
A44:    [i,j] in Indices B by A35,ZFMISC_1:87;
A45:    j in dom (LM1^W2d) by A38,A32,A36,A37,A41,A42,FINSEQ_3:25;
        now
          per cases;
          suppose
A46:        j<=W1;
            then
A47:        j in Seg width M1 by A41;
A48:        j in dom LM1 by A32,A41,A46,FINSEQ_3:25;
            thus LM.j = M*(i,j) by A8,A27,A43,MATRIX_0:def 7
              .= F12.1*(i-'Sum((Len F12) |0),j-'Sum ((Width F12) |0)) by A1,A6
,A18,A30,A41,A44,A40,A46,Def5,RVSUM_1:72
              .= F12.1*(i,j-'0) by NAT_D:40,RVSUM_1:72
              .= F12.1*(i,j) by NAT_D:40
              .= M1*(i,j)
              .= LM1.j by A47,MATRIX_0:def 7
              .= (LM1^W2d).j by A48,FINSEQ_1:def 7;
          end;
          suppose
A49:        j>W1;
A50:        dom W2d=Seg W2 by A36,FINSEQ_1:def 3;
            not j in dom LM1 by A32,A49,FINSEQ_3:25;
            then consider k such that
A51:        k in dom W2d and
A52:        j=len LM1+k by A45,FINSEQ_1:25;
            thus LM.j = M*(i,j) by A8,A27,A43,MATRIX_0:def 7
              .= d by A1,A18,A30,A44,A40,A49,Def5
              .= W2d.k by A51,A50,FINSEQ_2:57
              .= (LM1^W2d).j by A51,A52,FINSEQ_1:def 7;
          end;
        end;
        hence LM.j=(LM1^W2d).j;
      end;
      hence thesis by A38,A32,A36,A37;
    end;
   thus i in dom M2 implies Line(M,i+len M1)=(  width M1|->d)^Line(M2,i)
    proof
      set LM2=Line(M2,i);
      set W1d=W1|->d;
      set LM=Line(M,i+L1);
A53:  len LM2=W2 by CARD_1:def 7;
A54:  len (W1d^LM2)= len W1d + len LM2 by FINSEQ_1:22;
A55:  len W1d=W1 by CARD_1:def 7;
A56:  len LM=W1+W2 by A27,CARD_1:def 7;
      assume i in dom M2;
      then
A57:  i in Seg L2 by FINSEQ_1:def 3;
      then L1+i in Seg (L1+L2) by FINSEQ_1:60;
      then
A58:  L1+i in dom B by A2,FINSEQ_1:def 3;
      now
A59:    min(Len F12,i+L1)=2 by A17,A28,A26,A21,A29,A15,A57,Th10;
        let j such that
A60:    1<=j and
A61:    j<=len LM;
A62:    j in Seg width B by A8,A56,A60,A61;
        then
A63:    [i+L1,j] in Indices B by A58,ZFMISC_1:87;
A64:    j in dom (W1d^LM2) by A56,A53,A55,A54,A60,A61,FINSEQ_3:25;
          per cases;
          suppose
A65:        j<=W1;
A66:        dom W1d=Seg W1 by A55,FINSEQ_1:def 3;
A67:        j in Seg W1 by A60,A65;
            thus LM.j = B*(i+L1,j) by A30,A62,MATRIX_0:def 7
              .= d by A1,A26,A18,A63,A59,A65,Def5
              .= W1d.j by A67,FINSEQ_2:57
              .= (W1d^LM2).j by A67,A66,FINSEQ_1:def 7;
          end;
          suppose
A68:        j>W1;
A69:        dom LM2=Seg W2 by A53,FINSEQ_1:def 3;
            not j in dom W1d by A55,A68,FINSEQ_3:25;
            then consider k such that
A70:        k in dom LM2 and
A71:        j=W1+k by A55,A64,FINSEQ_1:25;
            thus LM.j = B*(i+L1,j) by A30,A62,MATRIX_0:def 7
              .= F12.2*(i+L1-'L1,j-'W1) by A7,A8,A1,A17,A26,A15,A18,A22,A56,A61
,A63,A59,A68,Def5
              .= F12.2*(i,j-'W1) by NAT_D:34
              .= F12.(len F1+1)*(i,k) by A10,A71,NAT_D:34
              .= M2*(i,k) by FINSEQ_1:42
              .= LM2.k by A70,A69,MATRIX_0:def 7
              .= (W1d^LM2).j by A55,A70,A71,FINSEQ_1:def 7;
          end;
      end;
      hence thesis by A56,A53,A55,A54;
    end;
  end;
  assume
A72: for i holds
   (i in dom M1 implies Line(M,i)=Line(M1,i)^(width M2 |-> d)) &
   (i in dom M2 implies Line(M,i+len M1)=(width M1|->d)^Line(M2,i));
A73: Sum ((Width F12) |2)=W1+W2 by A22,Th20;
  now
    set W1d=W1|->d;
    set W2d=W2|->d;
A74: Indices M=Indices B by MATRIX_0:26;
    let i,j such that
A75: [i,j] in Indices M;
A76: i in dom B by A75,A74,ZFMISC_1:87;
    then
A77: 1<=i by FINSEQ_3:25;
A78: j in Seg width B by A75,A74,ZFMISC_1:87;
    then
A79: 1<=j by FINSEQ_1:1;
A80: i in Seg (L1+L2) by A2,A76,FINSEQ_1:def 3;
    set LM1=Line(M1,i);
A81: len LM1=W1 by CARD_1:def 7;
A82: len W1d=W1 by CARD_1:def 7;
A83: len W2d=W2 by CARD_1:def 7;
A84: len (LM1^W2d)=len LM1 + len W2d by FINSEQ_1:22;
A85: j<=width B by A78,FINSEQ_1:1;
    now
      per cases;
      suppose
A86:    i<=L1;
        then i in Seg L1 by A77;
        then
A87:    min(Len F12,i+0)=1 by A24,A6,A13,A25,A4,Th10,RVSUM_1:72;
A88:    now
          per cases;
          suppose
A89:        j<=W1;
            then
A90:        j in Seg width M1 by A79;
A91:        j in dom LM1 by A79,A81,A89,FINSEQ_3:25;
            thus B*(i,j) = F12.1*(i-'0, j-'0) by A1,A6,A4,A5,A18,A75,A74,A79
,A87,A89,Def5,RVSUM_1:72
              .= F12.1*(i,j-'0) by NAT_D:40
              .= F12.1*(i,j) by NAT_D:40
              .= M1*(i,j)
              .= LM1.j by A90,MATRIX_0:def 7
              .= (LM1^W2d).j by A91,FINSEQ_1:def 7;
          end;
          suppose
A92:        j>W1;
            then
A93:        not j in dom LM1 by A81,FINSEQ_3:25;
A94:        dom W2d=Seg W2 by A83,FINSEQ_1:def 3;
            dom (LM1^W2d)=Seg width B by A8,A81,A83,A84,FINSEQ_1:def 3;
            then consider k such that
A95:        k in dom W2d and
A96:        j=len LM1+k by A78,A93,FINSEQ_1:25;
            thus B*(i,j) = d by A1,A18,A75,A74,A87,A92,Def5
              .= W2d.k by A95,A94,FINSEQ_2:57
              .= (LM1^W2d).j by A95,A96,FINSEQ_1:def 7;
          end;
        end;
        i in dom M1 by A77,A86,FINSEQ_3:25;
        then Line(M,i)=LM1^W2d by A72;
        hence M*(i,j)=B*(i,j) by A8,A27,A78,A88,MATRIX_0:def 7;
      end;
      suppose
A97:    i>L1;
        then reconsider iL=i-L1 as Element of NAT by NAT_1:21;
A98:    iL<>0 by A97;
        set LM2=Line(M2,iL);
        i=iL+L1;
        then iL in Seg L2 by A80,A98,FINSEQ_1:61;
        then
A99:    min(Len F12,iL+L1)=2 by A17,A28,A26,A21,A29,A15,Th10;
A100:   now
          per cases;
          suppose
A101:       j<=W1;
            then
A102:       j in Seg W1 by A79;
A103:       j in dom W1d by A79,A82,A101,FINSEQ_3:25;
            thus B*(i,j) = d by A1,A26,A18,A75,A74,A99,A101,Def5
              .= W1d.j by A102,FINSEQ_2:57
              .= (W1d^LM2).j by A103,FINSEQ_1:def 7;
          end;
          suppose
A104:       j>W1;
            len LM2=W2 by MATRIX_0:def 7;
            then
A105:       dom W2d=dom LM2 by A83,FINSEQ_3:29;
A106:       not j in dom LM1 by A81,A104,FINSEQ_3:25;
A107:       dom W2d=Seg W2 by A83,FINSEQ_1:def 3;
            dom (LM1^W2d)=Seg width B by A8,A81,A83,A84,FINSEQ_1:def 3;
            then consider k such that
A108:       k in dom W2d and
A109:       j=W1+k by A78,A81,A106,FINSEQ_1:25;
            thus B*(i,j) = F12.2*(iL+L1-'L1,j-'W1) by A8,A1,A17,A26,A15,A18,A73
,A75,A74,A85,A99,A104,Def5
              .= F12.2*(iL,j-'W1) by NAT_D:34
              .= F12.(len F1+1)*(iL,k) by A10,A109,NAT_D:34
              .= M2*(iL,k) by FINSEQ_1:42
              .= LM2.k by A108,A107,MATRIX_0:def 7
              .= (W1d^LM2).j by A82,A108,A109,A105,FINSEQ_1:def 7;
          end;
        end;
        Seg L2=dom M2 by FINSEQ_1:def 3;
        then Line(M,iL+L1)=(W1|->d)^Line(M2,iL) by A72,A80,A98,FINSEQ_1:61;
        hence M*(i,j)=B*(i,j) by A8,A27,A78,A100,MATRIX_0:def 7;
      end;
    end;
    hence M*(i,j)=B*(i,j);
  end;
  hence thesis by MATRIX_0:27;
end;
