[Numpy-discussion] Generalize hstack/vstack --> stack; Block matrices like in matlab

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Sep 9 08:30:13 EDT 2014


On Tue, Sep 9, 2014 at 5:42 AM, Stefan Otte <stefan.otte at gmail.com> wrote:

> Hey,
>
> @Josef, I wasn't aware of `bmat` and `np.asarray(np.bmat(....))` does
> basically what I want and what I'm already using.
>

I never needed any tetris or anything similar except for the matched block
version.

Just to point out two more related functions

scipy.sparse also has `bmat` for sparse block matrices
scipy.linalg and scipy.sparse have `block_diag` to complement bmat.


What I sometimes wish for is a sparse pseudo kronecker product as
convenience to bmat, where the first (or the second) matrix contains (0,1)
flags where the 1's specify where to put the blocks.
(I'm not sure what I really mean, similar to block_diag but with a
different filling pattern.)

Josef



>
> Regarding the Tetris problem: that never happened to me, but stack, as
> Josef pointed out, can handle that already :)
>
> I like the idea of removing the redundant square brackets:
>     stack([[a, b], [c, d]]) --> stack([a, b], [c, d])
> However, if the brackets are there there is no difference between
> creating a `np.array` and stacking arrays with `np.stack`.
>
> If we want to get fancy and turn this PR into something bigger
> (working our way up to a NP-complete problem ;)) then how about this.
> I sometimes have arrays that look like:
>   AB0
>   0 C
> Where 0 is a scalar but is supposed to fill the rest of the array.
> Having something like 0 in there might lead to ambiguities though. What
> does
>   ABC
>   0D0
> mean? One could limit the "filler" to appear only on the left or the right:
>   AB0
>   0CD
> But even then the shape is not completely determined. So we could
> require to have one row that only consists of arrays and determines
> the shape. Alternatively we could have a keyword parameter `shape`:
>   stack([A, B, 0], [0, C, D], shape=(8, 8))
>
> Colin, with `bmat` you can do what you're asking for. Directly taken
> from the example:
> >>> np.bmat('A,B; C,D')
> matrix([[1, 1, 2, 2],
>         [1, 1, 2, 2],
>         [3, 4, 7, 8],
>         [5, 6, 9, 0]])
>
>
> General question: If `bmat` already offers something like `stack`
> should we even bother implementing `stack`? More code leads to more
> bugs and maintenance work.
>
>
> Best,
>  Stefan
>
>
>
> On Tue, Sep 9, 2014 at 12:14 AM, cjw <cjw at ncf.ca> wrote:
> >
> > On 08-Sep-14 4:40 PM, Joseph Martinot-Lagarde wrote:
> >> Le 08/09/2014 15:29, Stefan Otte a écrit :
> >>> Hey,
> >>>
> >>> quite often I work with block matrices. Matlab offers the convenient
> notation
> >>>
> >>>       [ a b; c d ]
> > This would appear to be a desirable way to go.
> >
> > Numpy has something similar for strings.  The above is neater.
> >
> > Colin W.
> >>> to stack matrices. The numpy equivalent is kinda clumsy:
> >>>
> >>> vstack([hstack([a,b]), hstack([c,d])])
> >>>
> >>> I wrote the little function `stack` that does exactly that:
> >>>
> >>>       stack([[a, b], [c, d]])
> >>>
> >>> In my case `stack` replaced `hstack` and `vstack` almost completely.
> >>>
> >>> If you're interested in including it in numpy I created a pull request
> >>> [1]. I'm looking forward to getting some feedback!
> >>>
> >>>
> >>> Best,
> >>>    Stefan
> >>>
> >>>
> >>>
> >>> [1] https://github.com/numpy/numpy/pull/5057
> >>>
> >> The outside brackets are redundant, stack([[a, b], [c, d]]) should be
> >> stack([a, b], [c, d])
> >>
> >> _______________________________________________
> >> NumPy-Discussion mailing list
> >> NumPy-Discussion at scipy.org
> >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140909/05b85246/attachment.html>


More information about the NumPy-Discussion mailing list