[Numpy-discussion] Generalize hstack/vstack --> stack; Block matrices like in matlab
Stefan Otte
stefan.otte at gmail.com
Sun May 10 06:33:30 EDT 2015
Hey,
Just a quick update. I updated the pull request and renamed `stack` into
`block`. Have a look: https://github.com/numpy/numpy/pull/5057
I'm sticking with simple initial implementation because it's simple and
does what you think it does.
Cheers,
Stefan
On Fri, Oct 31, 2014 at 2:13 PM Stefan Otte <stefan.otte at gmail.com> wrote:
> To make the last point more concrete the implementation could look
> something like this (note that I didn't test it and that it still
> takes some work):
>
>
> def bmat(obj, ldict=None, gdict=None):
> return matrix(stack(obj, ldict, gdict))
>
>
> def stack(obj, ldict=None, gdict=None):
> # the old bmat code minus the matrix calls
> if isinstance(obj, str):
> if gdict is None:
> # get previous frame
> frame = sys._getframe().f_back
> glob_dict = frame.f_globals
> loc_dict = frame.f_locals
> else:
> glob_dict = gdict
> loc_dict = ldict
> return _from_string(obj, glob_dict, loc_dict)
>
> if isinstance(obj, (tuple, list)):
> # [[A,B],[C,D]]
> arr_rows = []
> for row in obj:
> if isinstance(row, N.ndarray): # not 2-d
> return concatenate(obj, axis=-1)
> else:
> arr_rows.append(concatenate(row, axis=-1))
> return concatenate(arr_rows, axis=0)
>
> if isinstance(obj, N.ndarray):
> return obj
>
>
> I basically turned the old `bmat` into `stack` and removed the matrix
> calls.
>
>
> Best,
> Stefan
>
>
>
> On Wed, Oct 29, 2014 at 3:59 PM, Stefan Otte <stefan.otte at gmail.com>
> wrote:
> > Hey,
> >
> > there are several ways how to proceed.
> >
> > - My proposed solution covers the 80% case quite well (at least I use
> > it all the time). I'd convert the doctests into unittests and we're
> > done.
> >
> > - We could slightly change the interface to leave out the surrounding
> > square brackets, i.e. turning `stack([[a, b], [c, d]])` into
> > `stack([a, b], [c, d])`
> >
> > - We could extend it even further allowing a "filler value" for non
> > set values and a "shape" argument. This could be done later as well.
> >
> > - `bmat` is not really matrix specific. We could refactor `bmat` a bit
> > to use the same logic in `stack`. Except the `matrix` calls `bmat` and
> > `_from_string` are pretty agnostic to the input.
> >
> > I'm in favor of the first or last approach. The first: because it
> > already works and is quite simple. The last: because the logic and
> > tests of both `bmat` and `stack` would be the same and the feature to
> > specify a string representation of the block matrix is nice.
> >
> >
> > Best,
> > Stefan
> >
> >
> >
> > On Tue, Oct 28, 2014 at 7:46 PM, Nathaniel Smith <njs at pobox.com> wrote:
> >> On 28 Oct 2014 18:34, "Stefan Otte" <stefan.otte at gmail.com> wrote:
> >>>
> >>> Hey,
> >>>
> >>> In the last weeks I tested `np.asarray(np.bmat(....))` as `stack`
> >>> function and it works quite well. So the question persits: If `bmat`
> >>> already offers something like `stack` should we even bother
> >>> implementing `stack`? More code leads to more
> >>> bugs and maintenance work. (However, the current implementation is
> >>> only 5 lines and by using `bmat` which would reduce that even more.)
> >>
> >> In the long run we're trying to reduce usage of np.matrix and ideally
> >> deprecate it entirely. So yes, providing ndarray equivalents of matrix
> >> functionality (like bmat) is valuable.
> >>
> >> -n
> >>
> >>
> >> _______________________________________________
> >> 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/20150510/d5a3d8a3/attachment.html>
More information about the NumPy-Discussion
mailing list