如何快速计算C 中向量的归一化L1和L2标准

How to fast calculate the normalized l1 and l2 norm of a vector in C++?

本文关键字:L1 标准 L2 计算 何快速 向量      更新时间:2023-10-16

我有一个矩阵 x ,它具有 n 列列数据向量, d dimensional Space。给定一个向量 XJ v [J] 是其 l1 norm(所有 abs> abs(XJI)>), w [j] 是其 l2 norm的平方(所有 xji^2 )和 pj [i] 是条目的组合,除以 l1 l2 norm。最后,我需要输出: pj,v,w 用于supsequet应用程序。

// X = new double [d*n]; is the input.
double alpha = 0.5;
double *pj = new double[d];
double *x_abs = new double[d];
double *x_2 = new double[d];
double *v = new double[n]();
double *w = new double[n]();
for (unsigned long j=0; j<n; ++j) {
        jm = j*m;
        jd = j*d;
        for (unsigned long i=0; i<d; ++i) {
            x_abs[i] = abs(X[i+jd]);
            v[j] += x_abs[i];
            x_2[i] = x_abs[i]*x_abs[i];
            w[j] += x_2[i];    
        }
        for (unsigned long i=0; i<d; ++i){
            pj[i] = alpha*x_abs[i]/v[j]+(1-alpha)*x_2[i]/w[j];     
        }
   // functionA(pj){ ... ...}  for subsequent applications
} 
// functionB(v, w){ ... ...} for subsequent applications

我的上述算法采用 o(nd) flops/time-complexity,任何人都可以通过在C 中使用building-functoin或新实现来帮助我加快它的速度?减少 O(nd)中的常数值对我也非常有帮助。

让我猜测:由于您有与性能相关的问题,因此向量的尺寸很大。
如果是这样,那么值得考虑的" CPU CACE LOCALITY" - 在CPPCON14演示文稿中有关此信息的一些有趣信息。
如果CPU缓存中没有数据,则abs -ing或Squart an CPU只需等待数据。

有了这一点,您可能希望尝试以下解决方案(没有任何可以提高性能的保证 - 编译器在优化代码时实际上可以应用这些技术)

for (unsigned long j=0; j<n; ++j) {
        // use pointer arithmetic - at > -O0 the compiler will do it anyway
        double *start=X+j*d, *end=X+(j+1)*d;
        // this part avoid as much as possible the competition
        // on CPU caches between X and v/w.
        // Don't store the norms in v/w as yet, keep them in registers
        double l1norm=0, l2norm=0;
        for(double *src=start; src!=end; src++) {
            double val=*src;
            l1norm+=abs(src);
            l2norm+= src*src;
        }
        double pl1=alpha/l1norm, pl2=(1-alpha)*l2norm;
        for(double *src=start, *dst=pj; src!=end; src++, dst++) {
          // Yes, recomputing abs/sqr may actually save time by not
          // creating competition on CPU caches with x_abs and x_2
          double val=*src;
          *dst = pl1*abs(val) + pl2*val*val;
        }    
        // functionA(pj){ ... ...}  for subsequent applications
        // Think well if you really need v/w. If you really do,
        // at least there are two values to be sent for storage into memory,
        //meanwhile the CPU can actually load the next vector into cache
        v[j]=l1norm; w[j]=l2norm;
}
// functionB(v, w){ ... ...} for subsequent applications