forum.alglib.net
http://forum.alglib.net/

L2-norm implementation
http://forum.alglib.net/viewtopic.php?f=2&t=9
Page 1 of 1

Author:  timoveldt [ Thu May 20, 2010 12:05 pm ]
Post subject:  L2-norm implementation

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!

Author:  Sergey.Bochkanov [ Thu May 20, 2010 1:45 pm ]
Post subject:  Re: L2-norm implementation

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.

Author:  timoveldt [ Thu May 20, 2010 1:54 pm ]
Post subject:  Re: L2-norm implementation

Thanks for the quick reply!

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

Author:  Sergey.Bochkanov [ Thu May 20, 2010 2:35 pm ]
Post subject:  Re: L2-norm implementation

Yes, you are right.

Author:  coolgee6 [ Sat Jan 03, 2015 11:07 am ]
Post subject:  Re: L2-norm implementation

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.

Author:  Chancepe [ Thu Jan 29, 2015 7:05 am ]
Post subject:  Re: L2-norm implementation

when dim uper to 13000, it ok ; but when I try dim = 14000, error emergys.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/