[SciPy-user] matrix multipy ...

Johann Cohen-Tanugi cohen at slac.stanford.edu
Mon May 19 14:44:53 EDT 2008


hi Matteo,

In [1]: import numpy as np

In [2]: aa = np.mat("1,2,3,4,5;6,7,8,9,0")

In [3]: bb = np.mat("1,2,3;4,5,6")

In [4]: np.outer(aa.T,bb)
Out[4]:
array([[ 1,  2,  3,  4,  5,  6],
       [ 6, 12, 18, 24, 30, 36],
       [ 2,  4,  6,  8, 10, 12],
       [ 7, 14, 21, 28, 35, 42],
       [ 3,  6,  9, 12, 15, 18],
       [ 8, 16, 24, 32, 40, 48],
       [ 4,  8, 12, 16, 20, 24],
       [ 9, 18, 27, 36, 45, 54],
       [ 5, 10, 15, 20, 25, 30],
       [ 0,  0,  0,  0,  0,  0]])

In [5]: np.outer(aa,bb)
Out[5]:
array([[ 1,  2,  3,  4,  5,  6],
       [ 2,  4,  6,  8, 10, 12],
       [ 3,  6,  9, 12, 15, 18],
       [ 4,  8, 12, 16, 20, 24],
       [ 5, 10, 15, 20, 25, 30],
       [ 6, 12, 18, 24, 30, 36],
       [ 7, 14, 21, 28, 35, 42],
       [ 8, 16, 24, 32, 40, 48],
       [ 9, 18, 27, 36, 45, 54],
       [ 0,  0,  0,  0,  0,  0]])

Is that what you were looking for?
cheers,
Johann

Matteo Bertini wrote:
> Perhaps it's simple, but...
>
> Suppose I have 2 "vectors" (1xN matrix really)
>
> n [27]: a = mat("1,2,3,4,5")
> In [29]: b = mat("1,2,3")
>
> If I whant the product, no problem.
>
> But suppose I have a list of vectors (a proper matrix)
>
> In [37]: aa = mat("1,2,3,4,5;6,7,8,9,0")
> In [38]: bb = mat("1,2,3;4,5,6")
>
> Can I avoid a loop and have the resulting "cube"?
>
> In [40]: aa[0].T*bb[0]
> Out[40]:
> matrix([[ 1,  2,  3],
>         [ 2,  4,  6],
>         [ 3,  6,  9],
>         [ 4,  8, 12],
>         [ 5, 10, 15]])
>
> I admit, I can't really understand array slicing power/limits!
>
> Thank you,
> Matteo Bertini
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>   



More information about the SciPy-User mailing list