MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: DirectSum (feature request)

  • To: mathgroup at
  • Subject: [mg97830] Re: DirectSum (feature request)
  • From: Ray Koopman <koopman at>
  • Date: Mon, 23 Mar 2009 04:02:42 -0500 (EST)
  • References: <gq2eu9$ech$>

If Ms is a list of matrices then this should give their direct sum:

dirsum[Ms_] := Module[{c = FoldList[Plus,0,Length/@First/@Ms]},Join@@

On Mar 21, 3:18 am, Maris Ozols <maroz... at> 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: Help plotting P(x,y,z) = Aexp[r^2]
  • Next by Date: Re: DirectSum (feature request)
  • Previous by thread: Re: DirectSum (feature request)
  • Next by thread: Re: DirectSum (feature request)