How to fast calculate the normalized l1 and l2 norm of a vector in C++?
<p>I have  a matrix <strong>X</strong> that has <strong>n</strong> column data vectors in <strong>d</strong> dimensional space.
Given a vector <strong>xj</strong>, <strong>v[j]</strong> is its <strong>l1</strong> norm (the summation of all <strong>abs(xji)</strong>), <strong>w[j]</strong> is the square of its <strong>l2</strong> norm (the summation of all <strong>xji^2</strong>), and <strong>pj[i]</strong> is the combination of entries divided by <strong>l1</strong> and <strong>l2</strong> norm. Finally, I need the outputs: <strong>pj, v, w</strong> for subsequet applications.</p>

<pre><code>// 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&lt;n; ++j) {
        jm = j*m;
        jd = j*d;
        for (unsigned long i=0; i&lt;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&lt;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
</code></pre>

<p>My above algorithm takes <strong>O(nd)</strong> Flops/Time-complexity, can any one help me to speed up it by using building-functoin or new implementation in C++? Reducing the constant value in   <strong>O(nd)</strong> is also very helpful  for me. </p>