# [SciPy-user] Easy way to make a block diagonal matrix?

josef.pktd@gmai... josef.pktd@gmai...
Wed May 20 21:09:11 CDT 2009

```On Wed, May 20, 2009 at 9:32 PM, Bruce Southey <bsouthey@gmail.com> wrote:
> On Wed, May 20, 2009 at 7:23 PM,  <josef.pktd@gmail.com> wrote:
>> 2009/5/20 Stéfan van der Walt <stefan@sun.ac.za>:
>>> 2009/5/21  <josef.pktd@gmail.com>:
>>>> scipy.linalg has some matrix creation functions, some look like
>>>
>>> Thanks, that looks like a good spot.
>>>
>>> Please review the attached patch (if anybody does not want it to go
>>> in, now is a good time to voice your concerns).
>>>
>>
>> It might be better to preserve the dtype of the input arrays, e.g. I
>> could think of a use for integer variables, e.g. dummy variables in
>> regression or anova, or to allow an option for the dtype when you
>> create the zeros array.
>>
>> I don't know if anybody would want complex or character matrices.
>>
>> I just checked, np.kron and np.diag preserves integer type, and
>> np.kron converts to float for mixed types, diag preserves character
>> type.
>>
>> otherwise it looks good and useful to me.
>>
>> Josef
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
> Hi,
> What is the definition that you are using for a block diagonal matrix?
>
> Some definitions use square matrices:
> http://en.wikipedia.org/wiki/Block_matrix#Block_diagonal_matrices
> http://mathworld.wolfram.com/BlockDiagonalMatrix.html
>
> But Matlab's blkdiag function does not and, thus, it may not result in
> a diagonal matrix:
> http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/blkdiag.html

Since it's just a useful function and not a mathematical concept, I
think the meaning is clear from the construction and example, although

If all individual component matrices are square, then you get the
wikipedia definition.

But for regression with panel data or with dummy variables, the
analogy to kronecker product is better, component matrices have many
rows (observations) and only a few columns (regressors). I would have
to look it up again, but I think the design matrix for a seemingly
unrelated regression (SUR) would be just block_diag(x1,x2,...xn) and
endogenous variable is vstack(y1,y2,...yn), I'm not sure what matrix
operation (kronecker product) would yield the covariance matrix in one
step.
(there is only a stub at
http://en.wikipedia.org/wiki/Seemingly_unrelated_regression and it
doesn't look completely correct to me)

The wikipedia page has more types of block matrices, and maybe some of
them also have general use cases.

I haven't gotten around yet to program anything for panel data or SUR,
so I don't know what else might be needed.

Josef

>
> So the documentation should reflect the selected definition.
>
> Also, I  support Josef's suggestion  that this function would be
> better suited in numpy rather than scipy.
>
> Bruce
```