Skip to content

Commit

Permalink
PNorm: changes suggested by Soeren
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Apr 9, 2012
1 parent f539c73 commit 58ebbf0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/shogun/mathematics/Math.cpp
Expand Up @@ -593,3 +593,14 @@ float32_t CMath::dot(const float32_t* v1, const float32_t* v2, int32_t n)
#endif
return r;
}

float64_t CMath::twonorm (const float64_t* v, int32_t n)
{
float64_t norm = 0.0;
#ifdef HAVE_LAPACK
norm = cblas_dnrm2 (n, v, 1);
#else
norm = CMath::sqrt (CMath::dot (vec, vec, vec_len));
#endif
return norm;
}
2 changes: 2 additions & 0 deletions src/shogun/mathematics/Math.h
Expand Up @@ -253,6 +253,8 @@ class CMath : public CSGObject
return CMath::sqrt(result);
}

static float64_t twonorm(const float64_t* v, int32_t n);

/// || x ||_q^q
template <class T>
static inline T qsq(T* x, int32_t len, float64_t q)
Expand Down
10 changes: 2 additions & 8 deletions src/shogun/preprocessor/PNorm.cpp
Expand Up @@ -125,17 +125,11 @@ inline float64_t CPNorm::get_pnorm (float64_t* vec, int32_t vec_len) const
}
else if (m_p == 2.0)
{
#ifdef HAVE_LAPACK
norm = cblas_dnrm2 (vec_len, vec, 1);
#else
norm = CMath::sqrt (CMath::dot (vec, vec, vec_len));
#endif
norm = CMath::twonorm (vec, vec_len);
}
else
{
for (int i = 0; i < vec_len; ++i)
norm += CMath::pow (fabs (vec[i]), m_p);
norm = CMath::pow (norm, 1/m_p);
norm = CMath::qnorm (vec, vec_len, m_p);
}

return norm;
Expand Down

0 comments on commit 58ebbf0

Please sign in to comment.