Re: Block diagonal systems
- To: mathgroup at smc.vnet.net
- Subject: [mg13597] Re: Block diagonal systems
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Fri, 7 Aug 1998 03:08:02 -0400
- Organization: University of Western Australia
- References: <6puia4$6t2@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
michael ringor wrote:
> Does anyone have an efficient way of creating a block
> diagonal matrix, where the matrices along the diagonal are
> identical m by n matrices?
After loading
In[1]:= Needs["LinearAlgebra`MatrixManipulation`"]
the following code should do what you want:
In[2]:= DiagonalBlockMatrix[(a_)?MatrixQ,p_] :=
Module[{m,n,q}, {m,n} = Dimensions[a];
BlockMatrix[DiagonalMatrix[Table[q, {p}]] /.
{0 -> ZeroMatrix[m,n], q -> a}]]
For example,
In[3]:= DiagonalBlockMatrix[{{1, 2, 7}, {3, 4, 9}}, 2]
Out[3]= {{1, 2, 7, 0, 0, 0}, {3, 4, 9, 0, 0, 0}, {0, 0, 0, 1, 2, 7},
{0, 0, 0, 3, 4, 9}}
> I'm looking for some sort of
> generalization to DiagonalMatrix[list] where list is sequence of
> compatible matrices, instead of a scalar list.
This is a more general request. Here is code that generalizes
DiagonalMatrix where list is sequence of compatible matrices
In[4]:= DiagonalBlockMatrix[l:{_?MatrixQ..}] :=
Module[{m, n, q, i}, {m, n} = Dimensions[First[l]];
BlockMatrix[DiagonalMatrix[Table[q[i], {i, Length[l]}]] /.
{0 -> ZeroMatrix[m, n], q[i_] :> l[[i]]}]] /;
Length[Dimensions[l]] == 3
In[5]:= DiagonalBlockMatrix[
{{{1, 1, 1}, {3, 3, 3}}, {{3, 5, 7}, {9, 9, 9}}}]
Out[5]= {{1, 1, 1, 0, 0, 0}, {3, 3, 3, 0, 0, 0}, {0, 0, 0, 3, 5, 7},
{0, 0, 0, 9, 9, 9}}
There should be an elegant generalization for arbitrary matrices along
the diagonal.
Cheers,
Paul
____________________________________________________________________
Paul Abbott Phone: +61-8-9380-2734
Department of Physics Fax: +61-8-9380-1014
The University of Western Australia Nedlands WA 6907
mailto:paul at physics.uwa.edu.au AUSTRALIA
http://www.physics.uwa.edu.au/~paul
God IS a weakly left-handed dice player
____________________________________________________________________