This is the mail archive of the gsl-discuss@sourceware.org mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Not clear to understand :)


The way you propose is not numerically stable. For example suppose
the x vector consists of 2 elements which are both huge numbers
close to the maximum floating point limit. If you just add them
like you propose you will get an overflow, even though their mean
is a representable number.

Computing sums and sums of squares etc can be a challenging numerical
problem. I believe Knuth's book discusses ways to do these things
stably.

On Thu, Nov 22, 2007 at 01:34:21PM +0200, Serhiy Lisovenko wrote:
> in file fit/linear.c
> 
>   for (i = 0; i < n; i++)
>     {
>       m_x += (x[i * xstride] - m_x) / (i + 1.0);
>       m_y += (y[i * ystride] - m_y) / (i + 1.0);
>     }
> 
> and other same...
> 
> code equivalent to
> 
>   for (i = 0; i < n; i++)
>     {
>       m_x +=x[i * xstride];
>       m_y +=y[i * ystride];
>     }
> m_x/=(double)n;
> m_y/=(double)n;
> 
> But the second code is more clear and some faster (no extra divisions).
> 
> 
> 
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]