Props to numpy programmers!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Felch

    Props to numpy programmers!

    So I wanted to matrixmultiply

    [[a,b],
    [c,d]]

    *

    [e,f]

    Of course this is impossible, because the number of columns in the
    first factor is not equal to the number of rows in the second. Wrong!
    It is impossible because the second matrix is rank1! So numpy
    happily converts it to a column vector so the multiplication will
    work, and converts the answer back into a rank1 vector!!!!

    I love NUMPY!!!!!!!!!! !

    I was reading my code and thought I had a bug, I couldn't figure out
    why the code was still working right! It's because the numpy people
    are geniouses!

    Hooray numpy!!!!!!!!!! Numpy is smarter than me!
  • Duncan Smith

    #2
    Re: Props to numpy programmers!


    "Andrew Felch" <andrewfelch@ya hoo.com> wrote in message
    news:9ed149b8.0 311191728.117b4 566@posting.goo gle.com...[color=blue]
    > So I wanted to matrixmultiply
    >
    > [[a,b],
    > [c,d]]
    >
    > *
    >
    > [e,f]
    >
    > Of course this is impossible, because the number of columns in the
    > first factor is not equal to the number of rows in the second. Wrong!
    > It is impossible because the second matrix is rank1! So numpy
    > happily converts it to a column vector so the multiplication will
    > work, and converts the answer back into a rank1 vector!!!!
    >
    > I love NUMPY!!!!!!!!!! !
    >
    > I was reading my code and thought I had a bug, I couldn't figure out
    > why the code was still working right! It's because the numpy people
    > are geniouses!
    >
    > Hooray numpy!!!!!!!!!! Numpy is smarter than me![/color]

    I love Numpy too. But I wouldn't want this multiplication to work if the
    second array was a row vector (and there's no explicit distinction because
    of the array's shape).
    [color=blue][color=green][color=darkred]
    >>> a[/color][/color][/color]
    array([[0, 1],
    [2, 3]])[color=blue][color=green][color=darkred]
    >>> b[/color][/color][/color]
    array([4, 5])[color=blue][color=green][color=darkred]
    >>> Numeric.matrixm ultiply(a,b)[/color][/color][/color]
    array([ 5, 23])[color=blue][color=green][color=darkred]
    >>> Numeric.matrixm ultiply(b,a)[/color][/color][/color]
    array([10, 19])

    One of these multiplications should fail, depending on whether b is a column
    or row vector.

    Make it a column vector by reshaping,
    [color=blue][color=green][color=darkred]
    >>> b = Numeric.reshape (b, (2,1))
    >>> b[/color][/color][/color]
    array([[4],
    [5]])

    and it does the right thing.
    [color=blue][color=green][color=darkred]
    >>> Numeric.matrixm ultiply(a,b)[/color][/color][/color]
    array([[ 5],
    [23]])[color=blue][color=green][color=darkred]
    >>> Numeric.matrixm ultiply(b,a)[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#6 0>", line 1, in -toplevel-
    Numeric.matrixm ultiply(b,a)
    File "C:\Python23\li b\site-packages\Numeri c\Numeric.py", line 335, in dot
    return multiarray.matr ixproduct(a, b)
    ValueError: matrices are not aligned[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Much safer IMHO.

    Duncan



    Comment

    Working...