matrix Multiplication

Discussion in 'Python' started by Sssasss, Oct 18, 2006.

  1. Sssasss

    Sssasss Guest

    hi evrybody!

    I wan't to multiply two square matrixes, and i don't understand why it
    doesn't work.
    Could you explain me?

    def multmat(A,B):
    "A*B"
    if len(A)!=len(B): return "error"
    D=[]
    C=[]
    for i in range(len(A)): D.append(0)
    for i in range(len(A)): C.append(D)
    for i in range(len(A)):
    for j in range(len(A)):
    for k in range(len(A)):
    C[j]+=A[k]*B[k][j]
    print C[j]
    print C
    return C

    when i use it on :
    >>> A=[[2,3,4],[5,8,6],[4,5,7]]
    >>> B=[[1,0,0],[0,1,0],[0,0,1]]


    I get :
    2
    2
    2
    0
    3
    3
    0
    0
    4
    [2, 3, 4]
    7
    7
    7
    3
    11
    11
    4
    4
    10
    [7, 11, 10]
    11
    11
    11
    11
    16
    16
    10
    10
    17
    [11, 16, 17]
    [[11, 16, 17], [11, 16, 17], [11, 16, 17]]

    thank you
     
    Sssasss, Oct 18, 2006
    #1
    1. Advertising

  2. "Sssasss" wrote:

    > I wan't to multiply two square matrixes, and i don't understand why it
    > doesn't work.
    >
    > def multmat(A,B):
    > "A*B"
    > if len(A)!=len(B): return "error"
    > D=[]
    > C=[]
    > for i in range(len(A)): D.append(0)
    > for i in range(len(A)): C.append(D)


    append doesn't copy data, so you're basically adding len(A) references to
    the same D list to C. for more on this, see:

    http://pyfaq.infogami.com/how-do-i-create-a-multidimensional-list

    </F>
     
    Fredrik Lundh, Oct 18, 2006
    #2
    1. Advertising

  3. Sssasss

    Sssasss Guest

    Fredrik Lundh wrote:
    > "Sssasss" wrote:
    >
    > > I wan't to multiply two square matrixes, and i don't understand why it
    > > doesn't work.
    > >
    > > def multmat(A,B):
    > > "A*B"
    > > if len(A)!=len(B): return "error"
    > > D=[]
    > > C=[]
    > > for i in range(len(A)): D.append(0)
    > > for i in range(len(A)): C.append(D)

    >
    > append doesn't copy data, so you're basically adding len(A) references to
    > the same D list to C. for more on this, see:
    >
    > http://pyfaq.infogami.com/how-do-i-create-a-multidimensional-list
    >
    > </F>


    Ok!! Tank you very much, i understand now.

    ciao
     
    Sssasss, Oct 18, 2006
    #3
  4. Sssasss

    Gerrit Holl Guest

    On 2006-10-18 14:15:17 +0200, Sssasss wrote:
    > Fredrik Lundh wrote:
    > > "Sssasss" wrote:
    > >
    > > > I wan't to multiply two square matrixes, and i don't understand why it
    > > > doesn't work.
    > > >
    > > > def multmat(A,B):
    > > > "A*B"
    > > > if len(A)!=len(B): return "error"
    > > > D=[]
    > > > C=[]
    > > > for i in range(len(A)): D.append(0)
    > > > for i in range(len(A)): C.append(D)

    > >
    > > append doesn't copy data, so you're basically adding len(A) references to
    > > the same D list to C. for more on this, see:
    > >
    > > http://pyfaq.infogami.com/how-do-i-create-a-multidimensional-list
    > >
    > > </F>

    >
    > Ok!! Tank you very much, i understand now.


    You might also want to look at numpy/numarray.

    Gerrit.
     
    Gerrit Holl, Oct 18, 2006
    #4
  5. Sssasss

    David Guest

    Il 18 Oct 2006 04:17:29 -0700, Sssasss ha scritto:

    > hi evrybody!
    >
    > I wan't to multiply two square matrixes, and i don't understand why it
    > doesn't work.

    Can I suggest a little bit less cumbersome algorithm?

    def multmat2(A,B):
    "A*B"
    if len(A)!=len(B): return "error" # this check is not enough!
    n = range(len(A))
    C = []
    for i in n:
    C.append([0]*len(A)) # add a row to C
    for j in n:
    a = A # get row i from A
    b = [row[j] for row in B] # get col j from B
    C[j] = sum([x*y for x,y in zip(a,b)])
    return C

    regards
    D.
     
    David, Oct 18, 2006
    #5
  6. Sssasss wrote:
    > hi evrybody!
    >
    > I wan't to multiply two square matrixes, and i don't understand why it
    > doesn't work.
    > Could you explain me?
    >
    > def multmat(A,B):
    > "A*B"
    > if len(A)!=len(B): return "error"


    Wrong validation here: you _can_ multiply two matrices with a different
    number of rows! And instead of returning "error" you should raise an
    exception.

    [...]

    I suggest using a linear algebra package, but if you insist in using lists
    of lists:

    >>> b = [[1, 2, 3, 4],

    .... [4, 5, 6, 7],
    .... [7, 8, 9, 10]]
    >>>
    >>> a = [[1, 2, 3],

    .... [4, 5, 6]]
    >>>
    >>> ab = [[sum(i*j for i, j in zip(row, col)) for col in zip(*b)] for row in a]
    >>> ab

    [[30, 36, 42, 48], [66, 81, 96, 111]]

    Straightforward from the definition of matrix multiplication.
    --
    Roberto Bonvallet
     
    Roberto Bonvallet, Oct 18, 2006
    #6
  7. Sssasss

    Sssasss Guest

    David wrote:
    > Il 18 Oct 2006 04:17:29 -0700, Sssasss ha scritto:
    >
    > > hi evrybody!
    > >
    > > I wan't to multiply two square matrixes, and i don't understand why it
    > > doesn't work.

    > Can I suggest a little bit less cumbersome algorithm?
    >
    > def multmat2(A,B):
    > "A*B"
    > if len(A)!=len(B): return "error" # this check is not enough!
    > n = range(len(A))
    > C = []
    > for i in n:
    > C.append([0]*len(A)) # add a row to C
    > for j in n:
    > a = A # get row i from A
    > b = [row[j] for row in B] # get col j from B
    > C[j] = sum([x*y for x,y in zip(a,b)])
    > return C
    >
    > regards
    > D.


    This one is really nice, i didn't knew the zip function, thank you
    ciao
     
    Sssasss, Oct 18, 2006
    #7
  8. Sssasss

    Sssasss Guest

    Roberto Bonvallet wrote:
    > Sssasss wrote:
    > > hi evrybody!
    > >
    > > I wan't to multiply two square matrixes, and i don't understand why it
    > > doesn't work.
    > > Could you explain me?
    > >
    > > def multmat(A,B):
    > > "A*B"
    > > if len(A)!=len(B): return "error"

    >
    > Wrong validation here: you _can_ multiply two matrices with a different
    > number of rows! And instead of returning "error" you should raise an
    > exception.
    >
    > [...]
    >
    > I suggest using a linear algebra package, but if you insist in using lists
    > of lists:
    >
    > >>> b = [[1, 2, 3, 4],

    > ... [4, 5, 6, 7],
    > ... [7, 8, 9, 10]]
    > >>>
    > >>> a = [[1, 2, 3],

    > ... [4, 5, 6]]
    > >>>
    > >>> ab = [[sum(i*j for i, j in zip(row, col)) for col in zip(*b)] for row in a]
    > >>> ab

    > [[30, 36, 42, 48], [66, 81, 96, 111]]
    >
    > Straightforward from the definition of matrix multiplication.
    > --
    > Roberto Bonvallet


    Thank you, this one is very short!
    yes of course we can multiply different kinds of matrices, bu since
    I'm starting with python i started with something quick.

    ciao
     
    Sssasss, Oct 18, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    2
    Views:
    2,322
    joris
    Mar 24, 2010
  2. Michael Bader
    Replies:
    11
    Views:
    12,054
    Michael Bader
    Mar 3, 2004
  3. robix

    matrix multiplication

    robix, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    512
    Rouben Rostamian
    Nov 13, 2003
  4. Replies:
    14
    Views:
    855
    Mark McIntyre
    Jan 26, 2006
  5. William Hughes
    Replies:
    13
    Views:
    1,227
    Ben Bacarisse
    Mar 15, 2010
Loading...

Share This Page