Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33209 > unrolled thread
| Started by | Cleuson Alves <cleuson.o@gmail.com> |
|---|---|
| First post | 2012-11-12 17:00 -0800 |
| Last post | 2012-11-14 01:04 -0800 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Division matrix Cleuson Alves <cleuson.o@gmail.com> - 2012-11-12 17:00 -0800
Re: Division matrix Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-12 18:25 -0700
Re: Division matrix Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-13 13:48 -0500
Re: Division matrix "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2012-11-13 22:14 +0000
Re: Division matrix wxjmfauth@gmail.com - 2012-11-14 01:04 -0800
| From | Cleuson Alves <cleuson.o@gmail.com> |
|---|---|
| Date | 2012-11-12 17:00 -0800 |
| Subject | Division matrix |
| Message-ID | <98b451e1-5cd5-46e9-8be4-59dcc835700b@googlegroups.com> |
Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix.
I await help.
Thank you.
follows the code below incomplete.
m = [[1,2,3],[4,5,6],[7,8,9]]
x = []
for i in [0,1,2]:
y = []
for linha in m:
y.append(linha[i])
x.append(y)
print x
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
def ProdMatrix(x,b):
tamL = len(x)
tamC = len(x[0])
c = nullMatrix(tamL,tamC)
for i in range(tamL):
for j in range(tamC):
val = 0
for k in range(len(b)):
val = val + x[i][l]*b[k][j]
c[i][j]
return c
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-11-12 18:25 -0700 |
| Message-ID | <mailman.3601.1352769970.27098.python-list@python.org> |
| In reply to | #33209 |
On Mon, Nov 12, 2012 at 6:00 PM, Cleuson Alves <cleuson.o@gmail.com> wrote: > Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix. > I await help. > Thank you. > follows the code below incomplete. So what is the specific problem with the code that you're looking for help with? > m = [[1,2,3],[4,5,6],[7,8,9]] > x = [] > for i in [0,1,2]: > y = [] > for linha in m: > y.append(linha[i]) > x.append(y) > > print x > [[1, 4, 7], [2, 5, 8], [3, 6, 9]] This calculates the transpose of the matrix, not the inverse. If the inverse is really what you're after, you should start by reading up on the math to compute that. Note that "for i in [0,1,2]:" could be more generally written as "for i in range(len(m[0])):", and then you don't need to rewrite your for loop if the dimensions of m change. If you actually do want the transpose, then I'll also mention in passing that there is a very nice one-liner using zip() to do this. I'll leave the details as an exercise. ;-) > def ProdMatrix(x,b): > tamL = len(x) > tamC = len(x[0]) > c = nullMatrix(tamL,tamC) > for i in range(tamL): > for j in range(tamC): > val = 0 > for k in range(len(b)): > val = val + x[i][l]*b[k][j] > c[i][j] > return c In the multiplication line you're using the variable 'l' as an index, but you haven't defined it. Probably you wanted 'k' here. You're also discarding 'val' after adding it up. If you want that value in the 'c' matrix, then you need to store it there before going on to the next loop.
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2012-11-13 13:48 -0500 |
| Message-ID | <mailman.3641.1352832471.27098.python-list@python.org> |
| In reply to | #33209 |
On Tue, 13 Nov 2012 10:19:43 -0200, Cleuson Alves <cleuson.o@gmail.com>
declaimed the following in gmane.comp.python.general:
> Thanks, I'm starting to plan now, so I'm still confused with the production
> code, but what I need is to divide array 2x2 or 3x3.
> I still can not!
Divide it by what? A scalar... Another square matrix of the same
size... Or a square matrix of a different size (is that even
possible?)...
Based
http://en.wikipedia.org/wiki/Division_%28mathematics%29#Division_of_matrices
upon, I can understand where the need for the inverse comes from -- and
multiplication by the inverse gives the "division". Next up,
http://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_2.C3.972_matrices
gives direct formulations for 2x2 and 3x3 matrices.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | "R. Michael Weylandt" <michael.weylandt@gmail.com> |
|---|---|
| Date | 2012-11-13 22:14 +0000 |
| Message-ID | <mailman.3649.1352844896.27098.python-list@python.org> |
| In reply to | #33209 |
On Tue, Nov 13, 2012 at 1:00 AM, Cleuson Alves <cleuson.o@gmail.com> wrote: > Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix. I would just point out that in most numerical applications, you rarely need to calculate the intermediate of the matrix inverse directly. See, e.g., http://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/ Of course, if this hasn't been said yet: NumPy. Michael
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2012-11-14 01:04 -0800 |
| Message-ID | <8a13e8d2-1dec-441a-85fe-19ba7a61d1a0@googlegroups.com> |
| In reply to | #33209 |
Le mardi 13 novembre 2012 02:00:28 UTC+1, Cleuson Alves a écrit : > Hello, I need to solve an exercise follows, first calculate the inverse matrix and then multiply the first matrix. > > I await help. > > Thank you. > > follows the code below incomplete. > > > > m = [[1,2,3],[4,5,6],[7,8,9]] > > x = [] > > for i in [0,1,2]: > > y = [] > > for linha in m: > > y.append(linha[i]) > > x.append(y) > > > > print x > > [[1, 4, 7], [2, 5, 8], [3, 6, 9]] > > > > def ProdMatrix(x,b): > > tamL = len(x) > > tamC = len(x[0]) > > c = nullMatrix(tamL,tamC) > > for i in range(tamL): > > for j in range(tamC): > > val = 0 > > for k in range(len(b)): > > val = val + x[i][l]*b[k][j] > > c[i][j] > > return c ------ Pedagogical hint: Before blindly calculating the inverse matrix, it may be a good idea to know if the inverse matrix exists. jmf
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web