Re: Block diagonal systems
- To: mathgroup at smc.vnet.net
- Subject: [mg13584] Re: Block diagonal systems
- From: "Allan Hayes" <hay at haystack.demon.cc.uk>
- Date: Mon, 3 Aug 1998 03:53:46 -0400
- References: <6puia4$6t2@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
michael ringor wrote in message <6puia4$6t2 at smc.vnet.net>...
>Hello: Does anyone have an efficient way of creating a block
> diagonal matrix, where the matrices along the diagonal are
> identical m by n matrices? I'm looking for some sort of
> generalization to DiagonalMatrix[list] where list is sequence of
> compatible matrices, instead of a scalar list.
>
>Michael Ringor
>Purdue University
>
>
>
Michael:
Here is a slightly more general function:
BlockDiagonal[diag_]:=
Module[{M,c=0,r=0},
M =
Table[0,
Evaluate[Sequence@@List/@Plus@@Dimensions/@diag]
];
Scan[
(M[[
Range[r+1,r+=(Dimensions[#][[1]])],
Range[c+1,c+=(Dimensions[#][[2]])]
]]= #)&,
diag
];
M
]
Test:
BlockDiagonal[{{{1,2},{3,4}},{{2},{4}},{{5,6}}}]//TableForm
1 2 0 0 0
3 4 0 0 0
0 0 2 0 0
0 0 4 0 0
0 0 0 5 6
Allan,
------------------------------------------------------------- Allan
Hayes
Training and Consulting
Leicester UK
http://www.haystack.demon.co.uk
hay at haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642