forum.alglib.net

ALGLIB forum
It is currently Sun Jun 26, 2022 4:19 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: L2-norm implementation
PostPosted: Thu May 20, 2010 12:05 pm 
Offline

Joined: Thu May 20, 2010 12:03 pm
Posts: 3
I have a question on the implementation of the L2-norm. If I'm not mistaken, the implementation resides in the file blas.cpp and is the first function (line 24, fingerprint: double vectornorm2(const ap::real_1d_array& x, int i1, int i2).

For convenience:
Code:
double vectornorm2(const ap::real_1d_array& x, int i1, int i2)
{
    double result;
    int n;
    int ix;
    double absxi;
    double scl;
    double ssq;

    n = i2-i1+1;
    if( n<1 )
    {
        result = 0;
        return result;
    }
    if( n==1 )
    {
        result = fabs(x(i1));
        return result;
    }
    scl = 0;
    ssq = 1;
    for(ix = i1; ix <= i2; ix++)
    {
        if( ap::fp_neq(x(ix),0) )
        {
            absxi = fabs(x(ix));
            if( ap::fp_less(scl,absxi) )
            {
                ssq = 1+ssq*ap::sqr(scl/absxi);
                scl = absxi;
            }
            else
            {
                ssq = ssq+ap::sqr(absxi/scl);
            }
        }
    }
    result = scl*sqrt(ssq);
    return result;
}

On http://mathworld.wolfram.com/VectorNorm.html I've read that the L2-norm of a vector is a really simple function, but I can't see how the implementation executes this method.

Could anybody explain it to me?

Thanks in advance!


Top
 Profile  
 
 Post subject: Re: L2-norm implementation
PostPosted: Thu May 20, 2010 1:45 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 871
Straightforward implementation of 2-norm will overflow when trying to calculate norm of large vector. For example:
Code:
norm({1E200, 1E200}) = sqrt(1E200^2 + 1E200^2) = sqrt(1E400 + 1E400) = sqrt(overflow)

but the final result is sqrt(2)*1E200, which is still representable by double.

ALGLIB implementation (which was actually taken from LAPACK) won't overflow until final result is close to or beyond double capacity.


Top
 Profile  
 
 Post subject: Re: L2-norm implementation
PostPosted: Thu May 20, 2010 1:54 pm 
Offline

Joined: Thu May 20, 2010 12:03 pm
Posts: 3
Thanks for the quick reply!

So it is the same formula, only optimized so it will run (longer) on larger numbers.


Top
 Profile  
 
 Post subject: Re: L2-norm implementation
PostPosted: Thu May 20, 2010 2:35 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 871
Yes, you are right.


Top
 Profile  
 
 Post subject: Re: L2-norm implementation
PostPosted: Sat Jan 03, 2015 11:07 am 
Offline

Joined: Sat Jan 03, 2015 10:58 am
Posts: 1
Do line have to pass exactly through all points, which are "part of it"? If answers are "probability" and "exactly", then ALGLIB can't solve this task. And I've never heard of regression problem like this.


Top
 Profile  
 
 Post subject: Re: L2-norm implementation
PostPosted: Thu Jan 29, 2015 7:05 am 
Offline

Joined: Thu Jan 29, 2015 6:49 am
Posts: 1
when dim uper to 13000, it ok ; but when I try dim = 14000, error emergys.

_________________
second last


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group