Re: DirectSum (feature request)

• To: mathgroup at smc.vnet.net
• Subject: [mg97801] Re: DirectSum (feature request)
• From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
• Date: Sun, 22 Mar 2009 05:46:56 -0500 (EST)
• References: <gq2eu9\$ech\$1@smc.vnet.net>

```Hi,

oh, that's very hard to program

ArraySum[mlst_] :=
Module[{n, zeros},
n = Length[mlst];
zeros = Table[0, {n - 1}];
ArrayFlatten[MapIndexed[
RotateRight[#1, #2[[1]] - 1] & , Prepend[zeros, #] & /@ mlst
]
]
]

and

m1 = {{a, b}, {c, d}}; m2 = {{1, 2}, {3, 4}};

ArraySum[{m1, m2, m1}]

work as expected.

Regards
Jens

Maris Ozols wrote:
> Taking a direct sum of a given list of matrices is a very common task
> (unless you are a quantum physicist and use only KroneckerProduct).
> Unfortunately there is no built-in function (that I know of) for doing
> this in Mathematica. The closest thing we have is ArrayFlatten. So I
> usually do something like this to compute a direct sum:
>
> DirectSum[Ms_] := Module[{n = Length[Ms], z, i},
>   z = ConstantArray[0, n];
>   ArrayFlatten@Table[ReplacePart[z, i -> Ms[[i]]], {i, 1, n}]
> ];
>
> Is there a better way of doing this?
>
> Note: A nice way to implement it would be
>
> DirectSum[Ms_] := ArrayFlatten@DiagonalMatrix[Ms];
>
> Unfortunately this gives "DiagonalMatrix::vector" error, since
> DiagonalMatrix is not flexible enough to accept a list of matrices.
> The way DiagonalMatrix is used in the above code might cause some
> confusion for beginners, but in general I don't see why DiagonalMatrix
> should be limited in this way.
>
> ~Maris Ozols~
>

```

• Prev by Date: Re: ParametricPlot problem
• Next by Date: Re: Orthogonazlie with Method->"Householder"
• Previous by thread: Re: DirectSum (feature request)
• Next by thread: Re: DirectSum (feature request)