Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
numpy (matrix solver) - python vs. matlab

Subject: numpy (matrix solver) - python vs. matlab

From: someone

Date: 29 Apr, 2012 22:17:12

Message: 1 of 18

Hi,

Notice cross-post, I hope you bear over with me for doing that (and I
imagine that some of you also like python in the matlab-group like
myself)...

------------------------------------------
Python vs. Matlab:
------------------------------------------

Python:
========
from numpy import matrix
from numpy import linalg
A = matrix( [[1,2,3],[11,12,13],[21,22,23]] )
print "A="
print A
print "A.I (inverse of A)="
print A.I

A.I (inverse of A)=
[[ 2.81466387e+14 -5.62932774e+14 2.81466387e+14]
  [ -5.62932774e+14 1.12586555e+15 -5.62932774e+14]
  [ 2.81466387e+14 -5.62932774e+14 2.81466387e+14]]


Matlab:
========
 >> A=[1 2 3; 11 12 13; 21 22 23]

A =

      1 2 3
     11 12 13
     21 22 23

 >> inv(A)
Warning: Matrix is close to singular or badly scaled.
          Results may be inaccurate. RCOND = 1.067522e-17.

ans =

    1.0e+15 *

     0.3002 -0.6005 0.3002
    -0.6005 1.2010 -0.6005
     0.3002 -0.6005 0.3002

------------------------------------------
Python vs. Matlab:
------------------------------------------

So Matlab at least warns about "Matrix is close to singular or badly
scaled", which python (and I guess most other languages) does not...

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

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?

I hope you matlabticians like this topic, at least I myself find it
interesting and many of you probably also program in some other language
and then maybe you'll find this worthwhile to read about.

Subject: numpy (matrix solver) - python vs. matlab

From: Kiuhnm

Date: 29 Apr, 2012 22:39:25

Message: 2 of 18

On 4/30/2012 0:17, someone wrote:
> Hi,
>
> Notice cross-post, I hope you bear over with me for doing that (and I
> imagine that some of you also like python in the matlab-group like
> myself)...
>
> ------------------------------------------
> Python vs. Matlab:
> ------------------------------------------
>
> Python:
> ========
> from numpy import matrix
> from numpy import linalg
> A = matrix( [[1,2,3],[11,12,13],[21,22,23]] )
> print "A="
> print A
> print "A.I (inverse of A)="
> print A.I
>
> A.I (inverse of A)=
> [[ 2.81466387e+14 -5.62932774e+14 2.81466387e+14]
> [ -5.62932774e+14 1.12586555e+15 -5.62932774e+14]
> [ 2.81466387e+14 -5.62932774e+14 2.81466387e+14]]
>
>
> Matlab:
> ========
> >> A=[1 2 3; 11 12 13; 21 22 23]
>
> A =
>
> 1 2 3
> 11 12 13
> 21 22 23
>
> >> inv(A)
> Warning: Matrix is close to singular or badly scaled.
> Results may be inaccurate. RCOND = 1.067522e-17.
>
> ans =
>
> 1.0e+15 *
>
> 0.3002 -0.6005 0.3002
> -0.6005 1.2010 -0.6005
> 0.3002 -0.6005 0.3002
>
> ------------------------------------------
> Python vs. Matlab:
> ------------------------------------------
>
> 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!

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

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

Kiuhnm

Subject: numpy (matrix solver) - python vs. matlab

From: someone

Date: 30 Apr, 2012 00:17:44

Message: 3 of 18

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?

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

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

Cramer's rule?

Subject: numpy (matrix solver) - python vs. matlab

From: Nasser M. Abbasi

Date: 30 Apr, 2012 00:38:15

Message: 4 of 18

On 04/29/2012 05:17 PM, someone wrote:

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

I hope you'll check the condition number all the time.

You could be designing a building where people will live in it.

If do not check the condition number, you'll end up with a building that
will fall down when a small wind hits it and many people will die all
because you did not bother to check the condition number when you solved
the equations you used in your design.

Also, as was said, do not use INV(A) directly to solve equations.

--Nasser

Subject: numpy (matrix solver) - python vs. matlab

From: someone

Date: 30 Apr, 2012 00:59:18

Message: 5 of 18

On 04/30/2012 02:38 AM, Nasser M. Abbasi wrote:
> On 04/29/2012 05:17 PM, someone wrote:
>
>> 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?
>>
>
> I hope you'll check the condition number all the time.

So how big can it (cond-number) be before I should do something else?
And what to do then? Cramers rule or pseudoinverse or something else?

> You could be designing a building where people will live in it.
>
> If do not check the condition number, you'll end up with a building that
> will fall down when a small wind hits it and many people will die all
> because you did not bother to check the condition number when you solved
> the equations you used in your design.
>
> Also, as was said, do not use INV(A) directly to solve equations.

In Matlab I used x=A\b.

I used inv(A) in python. Should I use some kind of pseudo-inverse or
what do you suggest?

Subject: numpy (matrix solver) - python vs. matlab

From: Nasser M. Abbasi

Date: 30 Apr, 2012 01:35:54

Message: 6 of 18

On 04/29/2012 07:59 PM, someone wrote:

>>
>> Also, as was said, do not use INV(A) directly to solve equations.
>
> In Matlab I used x=A\b.
>

good.

> I used inv(A) in python. Should I use some kind of pseudo-inverse or
> what do you suggest?
>

I do not use python much myself, but a quick google showed that pyhton
scipy has API for linalg, so use, which is from the documentation, the
following code example

           X = scipy.linalg.solve(A, B)

But you still need to check the cond(). If it is too large, not good.
How large and all that, depends on the problem itself. But the rule of
thumb, the lower the better. Less than 100 can be good in general, but I
really can't give you a fixed number to use, as I am not an expert in
this subjects, others who know more about it might have better
recommendations.

--Nasser

Subject: numpy (matrix solver) - python vs. matlab

From: Nasser M. Abbasi

Date: 30 Apr, 2012 01:50:10

Message: 7 of 18

On 04/29/2012 07:17 PM, someone wrote:

> Ok. When do you define it to be singular, btw?
>

There are things you can see right away about a matrix A being singular
without doing any computation. By just looking at it.

For example, If you see a column (or row) being a linear combination of
other column(s) (or row(s)) then this is a no no.

In your case you have

      1 2 3
      11 12 13
      21 22 23

You can see right away that if you multiply the second row by 2, and
subtract from that one times the first row, then you obtain the third row.

Hence the third row is a linear combination of the first row and the
second row. no good.

When you get a row (or a column) being a linear combination of others
rows (or columns), then this means the matrix is singular.

--Nasser

Subject: numpy (matrix solver) - python vs. matlab

From: Kiuhnm

Date: 30 Apr, 2012 10:37:34

Message: 8 of 18

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

Subject: numpy (matrix solver) - python vs. matlab

From: Kiuhnm

Date: 30 Apr, 2012 10:48:21

Message: 9 of 18

On 4/30/2012 3:35, Nasser M. Abbasi wrote:
> But you still need to check the cond(). If it is too large, not good.
> How large and all that, depends on the problem itself. But the rule of
> thumb, the lower the better. Less than 100 can be good in general, but I
> really can't give you a fixed number to use, as I am not an expert in
> this subjects, others who know more about it might have better
> recommendations.

Alas, there's no fixed number and as if that wasn't enough, there are
many condition numbers, each one with different properties. For
instance, the Skeel condition number is scale-invariant and it's useful
when a matrix is ill-conditioned just because its rows are out of scale.

Kiuhnm

Subject: numpy (matrix solver) - python vs. matlab

From: Russ P.

Date: 1 May, 2012 06:56:48

Message: 10 of 18

On Apr 29, 5:17 pm, someone <newsbo...@gmail.com> 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?
>
> >> 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?
>
> > 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?
>
> Cramer's rule?

If you really want to know just about everything there is to know
about a matrix, take a look at its Singular Value Decomposition (SVD).
I've never used numpy, but I assume it can compute an SVD.

Subject: numpy (matrix solver) - python vs. matlab

From: Eelco

Date: 1 May, 2012 13:26:16

Message: 11 of 18

There is linalg.pinv, which computes a pseudoinverse based on SVD that
works on all matrices, regardless of the rank of the matrix. It merely
approximates A*A.I = I as well as A permits though, rather than being
a true inverse, which may not exist.

Anyway, there are no general answers for this kind of thing. In all
non-textbook problems I can think of, the properties of your matrix
are highly constrained by the problem you are working on; which
additional tests are required to check for corner cases thus depends
on the problem. Often, if you have found an elegant solution to your
problem, no such corner cases exist. In that case, MATLAB is just
wasting your time with its automated checks.

Subject: numpy (matrix solver) - python vs. matlab

From: someone

Date: 1 May, 2012 18:49:29

Message: 12 of 18

On 05/01/2012 08:56 AM, Russ P. wrote:
> On Apr 29, 5:17 pm, someone<newsbo...@gmail.com> wrote:
>> On 04/30/2012 12:39 AM, Kiuhnm wrote:
>>> 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?
>>
>> Cramer's rule?
>
> If you really want to know just about everything there is to know
> about a matrix, take a look at its Singular Value Decomposition (SVD).

I know a bit about SVD - I used it for a short period of time in Matlab,
though I'm definately not an expert in it and I don't understand the
whole theory with orthogality behind making it work so elegant as it
is/does work out.

> I've never used numpy, but I assume it can compute an SVD.

I'm making my first steps now with numpy, so there's a lot I don't know
and haven't tried with numpy...

Subject: numpy (matrix solver) - python vs. matlab

From: someone

Date: 1 May, 2012 18:52:49

Message: 13 of 18

On 04/30/2012 03:35 AM, Nasser M. Abbasi wrote:
> On 04/29/2012 07:59 PM, someone wrote:
> I do not use python much myself, but a quick google showed that pyhton
> scipy has API for linalg, so use, which is from the documentation, the
> following code example
>
> X = scipy.linalg.solve(A, B)
>
> But you still need to check the cond(). If it is too large, not good.
> How large and all that, depends on the problem itself. But the rule of
> thumb, the lower the better. Less than 100 can be good in general, but I
> really can't give you a fixed number to use, as I am not an expert in
> this subjects, others who know more about it might have better
> recommendations.

Ok, that's a number...

Anyone wants to participate and do I hear something better than "less
than 100 can be good in general" ?

If I don't hear anything better, the limit is now 100...

What's the limit in matlab (on the condition number of the matrices), by
the way, before it comes up with a warning ???

Subject: numpy (matrix solver) - python vs. matlab

From: Russ P.

Date: 1 May, 2012 20:54:41

Message: 14 of 18

On May 1, 11:52 am, someone <newsbo...@gmail.com> wrote:
> On 04/30/2012 03:35 AM, Nasser M. Abbasi wrote:
>
> > On 04/29/2012 07:59 PM, someone wrote:
> > I do not use python much myself, but a quick google showed that pyhton
> > scipy has API for linalg, so use, which is from the documentation, the
> > following code example
>
> > X = scipy.linalg.solve(A, B)
>
> > But you still need to check the cond(). If it is too large, not good.
> > How large and all that, depends on the problem itself. But the rule of
> > thumb, the lower the better. Less than 100 can be good in general, but I
> > really can't give you a fixed number to use, as I am not an expert in
> > this subjects, others who know more about it might have better
> > recommendations.
>
> Ok, that's a number...
>
> Anyone wants to participate and do I hear something better than "less
> than 100 can be good in general" ?
>
> If I don't hear anything better, the limit is now 100...
>
> What's the limit in matlab (on the condition number of the matrices), by
> the way, before it comes up with a warning ???

The threshold of acceptability really depends on the problem you are
trying to solve. I haven't solved linear equations for a long time,
but off hand, I would say that a condition number over 10 is
questionable.

A high condition number suggests that the selection of independent
variables for the linear function you are trying to fit is not quite
right. For a poorly conditioned matrix, your modeling function will be
very sensitive to measurement noise and other sources of error, if
applicable. If the condition number is 100, then any input on one
particular axis gets magnified 100 times more than other inputs.
Unless your inputs are very precise, that is probably not what you
want.

Or something like that.

Subject: numpy (matrix solver) - python vs. matlab

From: someone

Date: 1 May, 2012 21:21:19

Message: 15 of 18

On 05/01/2012 10:54 PM, Russ P. wrote:
> On May 1, 11:52 am, someone<newsbo...@gmail.com> wrote:
>> On 04/30/2012 03:35 AM, Nasser M. Abbasi wrote:

>> What's the limit in matlab (on the condition number of the matrices), by
>> the way, before it comes up with a warning ???
>
> The threshold of acceptability really depends on the problem you are
> trying to solve. I haven't solved linear equations for a long time,
> but off hand, I would say that a condition number over 10 is
> questionable.

Anyone knows the threshold for Matlab for warning when solving x=A\b ? I
tried "edit slash" but this seems to be internal so I cannot see what
criteria the warning is based upon...

> A high condition number suggests that the selection of independent
> variables for the linear function you are trying to fit is not quite
> right. For a poorly conditioned matrix, your modeling function will be
> very sensitive to measurement noise and other sources of error, if
> applicable. If the condition number is 100, then any input on one
> particular axis gets magnified 100 times more than other inputs.
> Unless your inputs are very precise, that is probably not what you
> want.
>
> Or something like that.

Ok. So it's like a frequency-response-function, output divided by input...

Subject: numpy (matrix solver) - python vs. matlab

From: Eric Diaz

Date: 2 May, 2012 01:11:07

Message: 16 of 18

Regarding the condition number (K). I'm not an expert but I did put together a presentation with a slide on it. One way of looking at it is how sensitive parameter solutions are to noise. Indeed, when K is high then even the tiniest bit of noise can cause huge changes in parameter estimates. This is, in essence, the gain of sensitivity to relative error in the signal of our fitting model. From my past investigations, I believe that one way to get a practical sense for the impact of your condition number on your parameter estimates is to use the log base 2 of K. This result tells you the "digits of lost precision". So, with a K=1, there is no loss of precision, with a K~50 there is loss of ~5 digits of precision, and with a K~1000 there is loss of ~10 digits of precision. Obviously without precision, there is no reliability in the repeatability of your solution.

Subject: numpy (matrix solver) - python vs. matlab

From: Steven_Lord

Date: 2 May, 2012 14:47:57

Message: 17 of 18



"Russ P." <russ.paielli@gmail.com> wrote in message
news:2275231f-405f-4ee3-966a-40c821b7c0a8@2g2000yqp.googlegroups.com...
> On May 1, 11:52 am, someone <newsbo...@gmail.com> wrote:
>> On 04/30/2012 03:35 AM, Nasser M. Abbasi wrote:
>>
>> > On 04/29/2012 07:59 PM, someone wrote:
>> > I do not use python much myself, but a quick google showed that pyhton
>> > scipy has API for linalg, so use, which is from the documentation, the
>> > following code example
>>
>> > X = scipy.linalg.solve(A, B)
>>
>> > But you still need to check the cond(). If it is too large, not good.
>> > How large and all that, depends on the problem itself. But the rule of
>> > thumb, the lower the better. Less than 100 can be good in general, but
>> > I
>> > really can't give you a fixed number to use, as I am not an expert in
>> > this subjects, others who know more about it might have better
>> > recommendations.
>>
>> Ok, that's a number...
>>
>> Anyone wants to participate and do I hear something better than "less
>> than 100 can be good in general" ?
>>
>> If I don't hear anything better, the limit is now 100...
>>
>> What's the limit in matlab (on the condition number of the matrices), by
>> the way, before it comes up with a warning ???

I'm not going to answer, and the reason why is that saying "the limit is X"
may lead you to believe that "as long as my condition number is less than X,
I'm safe." That's not the case. The threshold above which MATLAB warns is
the fence that separates the tourists from the edge of the cliff of
singularity -- just because you haven't crossed the fence doesn't mean
you're not in danger of tripping and falling into the ravine.

> The threshold of acceptability really depends on the problem you are
> trying to solve.

I agree with this statement, although you generally don't want it to be too
big. The definition of "too big" is somewhat fluid, though.

> I haven't solved linear equations for a long time,
> but off hand, I would say that a condition number over 10 is
> questionable.

That seems pretty low as a general bound IMO.

> A high condition number suggests that the selection of independent
> variables for the linear function you are trying to fit is not quite
> right. For a poorly conditioned matrix, your modeling function will be
> very sensitive to measurement noise and other sources of error, if
> applicable. If the condition number is 100, then any input on one
> particular axis gets magnified 100 times more than other inputs.
> Unless your inputs are very precise, that is probably not what you
> want.
>
> Or something like that.

Russ, you and the OP (and others) may be interested in one of the books that
Cleve Moler has written and made freely available on the MathWorks website:

http://www.mathworks.com/moler/

The chapter Linear Equations in "Numerical Computing with MATLAB" includes a
section (section 2.9, starting on page 14 if I remember correctly) that
discusses norm and condition number and gives a more formal statement of
what you described. The code included in the book is written in MATLAB, but
even if you don't use MATLAB (since I know this has been cross-posted to
comp.lang.python) there's plenty of good, crunchy mathematics in that
section.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: numpy (matrix solver) - python vs. matlab

From: someone

Date: 2 May, 2012 20:32:28

Message: 18 of 18

On 05/02/2012 04:47 PM, Steven_Lord wrote:
>
> Russ, you and the OP (and others) may be interested in one of the books
> that Cleve Moler has written and made freely available on the MathWorks
> website:
>
> http://www.mathworks.com/moler/
>
> The chapter Linear Equations in "Numerical Computing with MATLAB"
> includes a section (section 2.9, starting on page 14 if I remember
> correctly) that discusses norm and condition number and gives a more
> formal statement of what you described. The code included in the book is
> written in MATLAB, but even if you don't use MATLAB (since I know this
> has been cross-posted to comp.lang.python) there's plenty of good,
> crunchy mathematics in that section.

I use matlab more than python. I just want to learn python, which seems
extremely powerful and easy but yet, I'm a python beginner.

Thank you very much for the reference, Mr. Lord. I'll look closely at
that Moler-book, in about 1/2 hour or so.... Looking forward to learning
more about this...

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us