[Numpy-discussion] proposing a "beware of [as]matrix()" warning

Skipper Seabold jsseabold at gmail.com
Wed Apr 28 17:28:51 EDT 2010


On Wed, Apr 28, 2010 at 2:12 PM, Alan G Isaac <aisaac at american.edu> wrote:
> On 4/28/2010 12:05 PM, Travis Oliphant wrote:
>> A proposal was made to allow "calling a NumPy array" to infer dot
>> product:
>>
>> a(b) is equivalent to dot(a,b)
>>
>> a(b)(c) would be equivalent to dot(dot(a,b),c)
>
>
> Here is a related ticket that proposes a more
> explicit alternative: adding a ``dot`` method
> to ndarray.
> http://projects.scipy.org/numpy/ticket/1456
>

FWIW, I have borrowed a convenience function chain_dot originally from
pandas that works for me as a stop gap for more readable code.

def chain_dot(*arrs):
    """
    Returns the dot product of the given matrices.

    Parameters
    ----------
    arrs: argument list of ndarrays

    Returns
    -------
    Dot product of all arguments.

    Example
    -------
    >>> import numpy as np
    >>> from scikits.statsmodels.tools import chain_dot
    >>> A = np.arange(1,13).reshape(3,4)
    >>> B = np.arange(3,15).reshape(4,3)
    >>> C = np.arange(5,8).reshape(3,1)
    >>> chain_dot(A,B,C)
    array([[1820],
       [4300],
       [6780]])
    """
    return reduce(lambda x, y: np.dot(y, x), arrs[::-1])

Skipper



More information about the NumPy-Discussion mailing list