
Hey, one of my new year's resolutions is to get my pull requests accepted (or closed). So here we go... Here is the update pull request: https://github.com/numpy/numpy/pull/5057 Here is the docstring: https://github.com/sotte/numpy/commit/3d4c5d19a8f15b35df50d945b9c8853b683f7a... The new `block` function is very similar to matlab's `[A, B; C, D]`. Pros: - it's very useful (in my experiment) - less friction for people coming from matlab - it's conceptually simple - the implementation is simple - it's documented - it's tested Cons: - the implementation is not super efficient. Temporary copies are created. However, bmat also does that. Feedback is very welcome! Best, Stefan On Sun, May 10, 2015 at 12:33 PM, Stefan Otte <stefan.otte@gmail.com> wrote:
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@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@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@pobox.com> wrote:
On 28 Oct 2014 18:34, "Stefan Otte" <stefan.otte@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@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion