Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9304 > unrolled thread
| Started by | David <davidbtdt@gmail.com> |
|---|---|
| First post | 2011-07-11 22:39 -0700 |
| Last post | 2011-07-12 06:23 -0700 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
Python bug? Indexing to matrices David <davidbtdt@gmail.com> - 2011-07-11 22:39 -0700
Re: Python bug? Indexing to matrices Chris Rebert <clp2@rebertia.com> - 2011-07-11 22:44 -0700
Re: Python bug? Indexing to matrices Ben Finney <ben+python@benfinney.id.au> - 2011-07-12 15:59 +1000
Re: Python bug? Indexing to matrices sturlamolden <sturlamolden@yahoo.no> - 2011-07-12 05:59 -0700
Re: Python bug? Indexing to matrices sturlamolden <sturlamolden@yahoo.no> - 2011-07-12 06:03 -0700
Re: Python bug? Indexing to matrices David <davidbtdt@gmail.com> - 2011-07-12 06:23 -0700
| From | David <davidbtdt@gmail.com> |
|---|---|
| Date | 2011-07-11 22:39 -0700 |
| Subject | Python bug? Indexing to matrices |
| Message-ID | <394aae4f-92ac-406e-bd44-2d2bd3debd91@l1g2000yql.googlegroups.com> |
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)
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-07-11 22:44 -0700 |
| Message-ID | <mailman.935.1310449448.1164.python-list@python.org> |
| In reply to | #9304 |
On Mon, Jul 11, 2011 at 10:39 PM, David <davidbtdt@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-07-12 15:59 +1000 |
| Message-ID | <87sjqcdoo0.fsf@benfinney.id.au> |
| In reply to | #9304 |
David <davidbtdt@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
[toc] | [prev] | [next] | [standalone]
| From | sturlamolden <sturlamolden@yahoo.no> |
|---|---|
| Date | 2011-07-12 05:59 -0700 |
| Message-ID | <82b0e789-05bc-4e87-bd00-8338a95b2e1e@u42g2000yqm.googlegroups.com> |
| In reply to | #9304 |
On 12 Jul, 07:39, David <davidb...@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | sturlamolden <sturlamolden@yahoo.no> |
|---|---|
| Date | 2011-07-12 06:03 -0700 |
| Message-ID | <0e81b1d3-da91-45ca-8f2f-be2c4790300e@t5g2000yqj.googlegroups.com> |
| In reply to | #9313 |
On 12 Jul, 14:59, sturlamolden <sturlamol...@yahoo.no> wrote: > ma = np.matrix(a) > mb = np.matrix(b) > a*b ma*mb Sorry for the typo.
[toc] | [prev] | [next] | [standalone]
| From | David <davidbtdt@gmail.com> |
|---|---|
| Date | 2011-07-12 06:23 -0700 |
| Message-ID | <cc33294e-bced-402d-9630-07bd893ba971@e7g2000vbw.googlegroups.com> |
| In reply to | #9313 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web