numpy (matrix solver) - python vs. matlab

Kiuhnm kiuhnm03.4t.yahoo.it
Mon Apr 30 06:37:34 EDT 2012


On 4/30/2012 2:17, someone wrote:
> On 04/30/2012 12:39 AM, Kiuhnm wrote:
>
>>> So Matlab at least warns about "Matrix is close to singular or badly
>>> scaled", which python (and I guess most other languages) does not...
>>
>> A is not just close to singular: it's singular!
>
> Ok. When do you define it to be singular, btw?
>
>>> Which is the most accurate/best, even for such a bad matrix? Is it
>>> possible to say something about that? Looks like python has a lot more
>>> digits but maybe that's just a random result... I mean.... Element 1,1 =
>>> 2.81e14 in Python, but something like 3e14 in Matlab and so forth -
>>> there's a small difference in the results...
>>
>> Both results are *wrong*: no inverse exists.
>
> What's the best solution of the two wrong ones? Best least-squares
> solution or whatever?

Trust me. They're both so wrong that it doesn't matter.
Have a look at A*inv(A) and inv(A)*A and you'll see by yourself.

>>> With python, I would also kindly ask about how to avoid this problem in
>>> the future, I mean, this maybe means that I have to check the condition
>>> number at all times before doing anything at all ? How to do that?
>>
>> If cond(A) is high, you're trying to solve your problem the wrong way.
>
> So you're saying that in another language (python) I should check the
> condition number, before solving anything?

Yes, unless you already know that it will always be low by design.

>> You should try to avoid matrix inversion altogether if that's the case.
>> For instance you shouldn't invert a matrix just to solve a linear system.
>
> What then?

Look at the documentation of the library you're using.

> Cramer's rule?

Surprisingly, yes. That's an option. See
"A condensation-based application of Cramerʼs rule for solving 
large-scale linear systems"
Popular linear codes are based on Gaussian elimination or some iterative 
method, though.

Kiuhnm



More information about the Python-list mailing list