FAQ
Should the following line work for defining a matrix with zeros?

c= [[0]*col]*row

where "col" is the number of columns in the matrix and "row" is of
course the number of rows.

If this a valid way of initializing a matrix in Python 3.2.1, then it
appears to me that a bug surfaces in Python when performing this line:

c[i][j] = c[i][j] + a[i][k] * b[k][j]

It writes to the jth column rather than just the i,j cell.

I'm new at Python and am not sure if I'm just doing something wrong if
there is really a bug in Python. The script works fine if I
initialize the matrix with numpy instead:

c = np.zeros((row,col))

So, I know my matrix multiply algorithm is correct (I know I could use
numpy for matrix multiplication, this was just to learn Python).

I've attached my source code below.

TIA,

David

-----

#!python
# dot.py - Matrix multiply in matricies: [c] = [a] * [b]

import numpy as np

a = [[3, 7], [-2, 1], [2, 4]]
b = [[2, 1, -3, 1], [4, 3, -2, 3]]

print("a = {0}, b = {1}".format(a,b))

row = len(a)
col = len(b[0])

#c = np.zeros((row,col)) # <----- Correct results when using this
line
c= [[0]*col]*row # <----- Incorrect results when using
this line
print(c)

for i in range(row): # Index into rows of [a]
for j in range(col): # Index into columns of [b]
c[i][j] = 0

for k in range(len(b)): # Index into columns of [a] and rows of [b]
print("c[{0}][{1}] + a[{2}][{3}] * b[{4}][{5}] = {6} + {7} *
{8}".format(i,j,i,k,k,j,c[i][j],a[i][k],b[k][j]))
c[i][j] = c[i][j] + a[i][k] * b[k][j]

print(c)

## Search Discussions

•  at Jul 12, 2011 at 5:44 am ⇧

On Mon, Jul 11, 2011 at 10:39 PM, David wrote:
Should the following line work for defining a matrix with zeros?

c= [[0]*col]*row

where "col" is the number of columns in the matrix and "row" is of
course the number of rows.
Nope. See the FAQ:
http://docs.python.org/faq/programming.html#how-do-i-create-a-multidimensional-list

Cheers,
Chris
•  at Jul 12, 2011 at 5:59 am ⇧

David <davidbtdt at gmail.com> writes:

Should the following line work for defining a matrix with zeros?

c= [[0]*col]*row
No. Python lists are not matrixes and are not arrays.

If you want good implementations of arrays and matrices, use NumPy
<URL:http://numpy.scipy.org/>.

--
\ ?Properly read, the Bible is the most potent force for atheism |
`\ ever conceived.? ?Isaac Asimov |
_o__) |
Ben Finney
•  at Jul 12, 2011 at 12:59 pm ⇧

On 12 Jul, 07:39, David wrote:
Should the following line work for defining a matrix with zeros?

c= [[0]*col]*row
No. The rows will be aliased.

This will work:

c = [[0]*col for i in range(row)]

Note that Python lists are not ment to be used as matrices. We have
NumPy or the array module for that.

If this a valid way of initializing a matrix in Python 3.2.1, then it
appears to me that a bug surfaces in Python when performing this line:

c[i][j] = c[i][j] + a[i][k] * b[k][j]

It writes to the jth column rather than just the i,j cell.
That is due to aliasing.

I'm new at Python and am not sure if I'm just doing something wrong if
there is really a bug in Python. ?The script works fine if I
initialize the matrix with numpy instead:

c = np.zeros((row,col))

So, I know my matrix multiply algorithm is correct (I know I could use
numpy for matrix multiplication, this was just to learn Python).
Like so:

np.dot(a,b)

or

ma = np.matrix(a)
mb = np.matrix(b)
a*b

or call BLAS directly:

scipy.linalg.fblas.dgemm

Sturla
•  at Jul 12, 2011 at 1:03 pm ⇧

On 12 Jul, 14:59, sturlamolden wrote:

? ?ma = np.matrix(a)
? ?mb = np.matrix(b)
? ?a*b
ma*mb

Sorry for the typo.
•  at Jul 12, 2011 at 1:23 pm ⇧
Thank all for the very helpful replies. The goal of the matrix
multiply exercise was just to help my son and I learn Python better.
I now understand *why* my initialization of [c] was wrong and I am
continuing to check out numpy and scipy.

Regards,
David

## Related Discussions

Discussion Overview
 group python-list categories python posted Jul 12, '11 at 5:39a active Jul 12, '11 at 1:23p posts 6 users 4 website python.org

### 4 users in discussion

Content

People

Support

Translate

site design / logo © 2022 Grokbase