Re: Division matrix

2012-11-14 Thread wxjmfauth
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Division matrix

2012-11-13 Thread R. Michael Weylandt
On Tue, Nov 13, 2012 at 1:00 AM, Cleuson Alves  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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Division matrix

2012-11-13 Thread Cleuson Alves
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!



2012/11/12 Joshua Landau 

> On 13 November 2012 01:00, Cleuson Alves  wrote:
>
>> Hello, I need to solve an exercise follows, first calculate the inverse
>> matrix and then multiply the first matrix.
>>
>
> This list isn't to give answers for homeworks, and this sounds like one.
> We *do* give help to those who have a specific problem and who preferably
> show that they are trying to help up help them.
>
>
>> I await help.
>>
>
> For what?! You haven't asked a question.
> 1) What do you need. Answer precisely, preferably giving an example output
> for the input you want
> 2) What techniques have you tried? You have given code below in an
> extremely un-obvious manner. What does it do, crash? Is it the wrong
> output? What is wrong about the output?
> 3) What do you *think* has gone wrong? What about the output seems to be
> wrong?
>
>
>> Thank you.
>> follows the code below incomplete.
>>
>
> On the basis that we can get some foreign people who maybe don't have
> English as a commonly used language, muddled grammar isn't that big deal.
> However, when asking for help it is worth checking that you've asked in a
> clear way.
>
>
>> 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]]
>>
>
> Is this the right output? Is this what you *meant* by inverse? As Ian
> Kelly (who is undoubtedly more learned in this area) said, this is a
> transpose, so you have just swapped the one axis with another.
>
>
>> 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
>>
>
> You haven't given the full code. This crashes because we don't have
> "nullMatrix" defined. It would be nice if we had something we could try to
> run.
>
> Then, the error is: "NameError: global name 'l' is not defined". On the
> line "val = val + x[i][l]*b[k][j]" you use a variable "l" which doesn't
> exist. What should you be using, or should you have created it?
>
> *Then* you get an output of pure 0s. This is because you forgot to put
> your result into c, your new matrix.
> You probably just forgot to finish the line "c[i][j]", which does nothing
> now.
>
> You're actually really close on the second part. I have no idea if you're
> doing what you want for the first part, but it's not what's traditionally
> called an inverse.
>



-- 
Cleuson de Oliveira Alves
Rio de Janeiro - RJ
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Division matrix

2012-11-12 Thread Joshua Landau
On 13 November 2012 01:00, Cleuson Alves  wrote:

> Hello, I need to solve an exercise follows, first calculate the inverse
> matrix and then multiply the first matrix.
>

This list isn't to give answers for homeworks, and this sounds like one. We
*do* give help to those who have a specific problem and who preferably show
that they are trying to help up help them.


> I await help.
>

For what?! You haven't asked a question.
1) What do you need. Answer precisely, preferably giving an example output
for the input you want
2) What techniques have you tried? You have given code below in an
extremely un-obvious manner. What does it do, crash? Is it the wrong
output? What is wrong about the output?
3) What do you *think* has gone wrong? What about the output seems to be
wrong?


> Thank you.
> follows the code below incomplete.
>

On the basis that we can get some foreign people who maybe don't have
English as a commonly used language, muddled grammar isn't that big deal.
However, when asking for help it is worth checking that you've asked in a
clear way.


> 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]]
>

Is this the right output? Is this what you *meant* by inverse? As Ian Kelly
(who is undoubtedly more learned in this area) said, this is a transpose,
so you have just swapped the one axis with another.


> 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
>

You haven't given the full code. This crashes because we don't have
"nullMatrix" defined. It would be nice if we had something we could try to
run.

Then, the error is: "NameError: global name 'l' is not defined". On the
line "val = val + x[i][l]*b[k][j]" you use a variable "l" which doesn't
exist. What should you be using, or should you have created it?

*Then* you get an output of pure 0s. This is because you forgot to put your
result into c, your new matrix.
You probably just forgot to finish the line "c[i][j]", which does nothing
now.

You're actually really close on the second part. I have no idea if you're
doing what you want for the first part, but it's not what's traditionally
called an inverse.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Division matrix

2012-11-12 Thread Ian Kelly
On Mon, Nov 12, 2012 at 6:00 PM, Cleuson Alves  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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Division matrix

2012-11-12 Thread Cleuson Alves
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
-- 
http://mail.python.org/mailman/listinfo/python-list