473,379 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

matrix Multiplication

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[i][j]+=A[i][k]*B[k][j]
print C[i][j]
print C[i]
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

Oct 18 '06 #1
7 5083
"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-c...mensional-list

</F>

Oct 18 '06 #2

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-c...mensional-list

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

ciao

Oct 18 '06 #3
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-c...mensional-list

</F>

Ok!! Tank you very much, i understand now.
You might also want to look at numpy/numarray.

Gerrit.
Oct 18 '06 #4
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[i] # get row i from A
b = [row[j] for row in B] # get col j from B
C[i][j] = sum([x*y for x,y in zip(a,b)])
return C

regards
D.
Oct 18 '06 #5
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
Oct 18 '06 #6

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[i] # get row i from A
b = [row[j] for row in B] # get col j from B
C[i][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

Oct 18 '06 #7

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

Oct 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
3
by: robix | last post by:
Hi again. I'm now asking your help because of a smal problem i'm getting with my multiplication matrix code. I'll try to give you as many details as possible. My matrix structure: typedef...
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
0
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
6
by: amitsoni.1984 | last post by:
Hi, Is there any direct function for matrix multiplication in Python or any of its packages? or do we have to multiply element by element? Thank you, Amit
3
by: crazygrey | last post by:
Hello, I'm a newbie to C++ so excuse me if my question was trivial but it is important to me. I'm implementing a simple code to find the forward kinematics of a robot: #include "stdafx.h"...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
8
by: joegao1 | last post by:
can some one give me a hint? I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible. my task is to calculate the matrix...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.