# forum.alglib.net

ALGLIB forum
 It is currently Mon Aug 08, 2022 12:28 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.

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: L2-norm implementationPosted: Thu May 20, 2010 12:05 pm

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?

Top

 Post subject: Re: L2-norm implementationPosted: Thu May 20, 2010 1:45 pm

Joined: Fri May 07, 2010 7:06 am
Posts: 878
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

 Post subject: Re: L2-norm implementationPosted: Thu May 20, 2010 1:54 pm

Joined: Thu May 20, 2010 12:03 pm
Posts: 3

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

Top

 Post subject: Re: L2-norm implementationPosted: Thu May 20, 2010 2:35 pm

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

Top

 Post subject: Re: L2-norm implementationPosted: Sat Jan 03, 2015 11:07 am

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

 Post subject: Re: L2-norm implementationPosted: Thu Jan 29, 2015 7:05 am

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 6 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for: