Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Division matrix

67 views
Skip to first unread message

Cleuson Alves

unread,
Nov 12, 2012, 8:00:27 PM11/12/12
to
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

Ian Kelly

unread,
Nov 12, 2012, 8:25:33 PM11/12/12
to Python
On Mon, Nov 12, 2012 at 6:00 PM, Cleuson Alves <cleu...@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.
Message has been deleted

R. Michael Weylandt

unread,
Nov 13, 2012, 5:14:32 PM11/13/12
to Cleuson Alves, pytho...@python.org
On Tue, Nov 13, 2012 at 1:00 AM, Cleuson Alves <cleu...@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

wxjm...@gmail.com

unread,
Nov 14, 2012, 4:04:09 AM11/14/12
to
------

Pedagogical hint:
Before blindly calculating the inverse matrix, it may be
a good idea to know if the inverse matrix exists.

jmf
0 new messages