Generically Creating Views of Equal Dimensions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Generically Creating Views of Equal Dimensions

Alexander Michael
Is there an already existing method to create views that add as many
dimensions as required to bring a collection of arrays to the same
dimensionality by adding the appropriate number of numpy.newaxis's to
the ends? For example:

In [1]: a = numpy.array([1, 2, 3, 4])
In [2]: b = numpy.array([[1,10], [1,10], [1,10], [1,10]])

In [3]: a_,b_ = dimensionalize(a,b) # returns a[:,numpy.newaxis],b
In [4]: a_*b_
array([[ 1, 10],
       [ 2, 20],
       [ 3, 30],
       [ 4, 40]])

Or perhaps there is better way to do the same thing.

Thanks,
Alex
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Generically Creating Views of Equal Dimensions

Robert Kern-2
On Tue, Apr 15, 2008 at 9:54 AM, Alexander Michael <[hidden email]> wrote:
> Is there an already existing method to create views that add as many
>  dimensions as required to bring a collection of arrays to the same
>  dimensionality by adding the appropriate number of numpy.newaxis's to
>  the ends? For example:

The usual broadcasting rule goes the other way; newaxis's are
*prepended* to the beginning of the shape. I wouldn't put a function
into numpy to do something the opposite of that convention and risk
confusing people. However, if you would like a utility function for
your own code:


from numpy import newaxis

def dimensionalize(a, b):
    """ Try to make the ranks of two arrays compatible for
    broadcasting by *appending* new axes.

    This is the opposite of the usual broadcasting convention which
    *prepends* new axes.
    """
    ranka = len(a.shape)
    rankb = len(b.shape)
    if ranka > rankb:
        b = b[(Ellipsis,)+(newaxis,)*(ranka-rankb)]
    elif rankb > ranka:
        a = a[(Ellipsis,)+(newaxis,)*(rankb-ranka)]
    return a, b

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Generically Creating Views of Equal Dimensions

Alexander Michael
On Tue, Apr 15, 2008 at 3:38 PM, Robert Kern <[hidden email]> wrote:

> On Tue, Apr 15, 2008 at 9:54 AM, Alexander Michael <[hidden email]> wrote:
>  > Is there an already existing method to create views that add as many
>  >  dimensions as required to bring a collection of arrays to the same
>  >  dimensionality by adding the appropriate number of numpy.newaxis's to
>  >  the ends? For example:
>
>  The usual broadcasting rule goes the other way; newaxis's are
>  *prepended* to the beginning of the shape. I wouldn't put a function
>  into numpy to do something the opposite of that convention and risk
>  confusing people.

Ah, I see. If my convention was the transpose of what it currently is,
I wouldn't
need to do anything.

Thanks!
Alex
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion