Created
April 18, 2013 15:30
-
-
Save vigsterkr/5413663 to your computer and use it in GitHub Desktop.
pnorm templatization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/shogun/preprocessor/PNorm.cpp b/src/shogun/preprocessor/PNorm.cpp | |
index 6158804..bdd28f0 100644 | |
--- a/src/shogun/preprocessor/PNorm.cpp | |
+++ b/src/shogun/preprocessor/PNorm.cpp | |
@@ -19,27 +19,27 @@ | |
using namespace shogun; | |
-CPNorm::CPNorm () | |
-: CDensePreprocessor<float64_t>(), | |
+template <class ST> CPNorm<ST>::CPNorm () | |
+: CDensePreprocessor<ST>(), | |
m_p (2.0) | |
{ | |
register_param (); | |
} | |
-CPNorm::CPNorm (double p) | |
-: CDensePreprocessor<float64_t>(), | |
+template <class ST> CPNorm<ST>::CPNorm (float64_t p) | |
+: CDensePreprocessor<ST>(), | |
m_p (p) | |
{ | |
ASSERT (m_p >= 1.0) | |
register_param (); | |
} | |
-CPNorm::~CPNorm () | |
+template <class ST> CPNorm<ST>::~CPNorm () | |
{ | |
} | |
/// initialize preprocessor from features | |
-bool CPNorm::init (CFeatures* features) | |
+template <class ST> bool CPNorm<ST>::init (CFeatures* features) | |
{ | |
ASSERT(features->get_feature_class()==C_DENSE) | |
ASSERT(features->get_feature_type()==F_DREAL) | |
@@ -48,12 +48,12 @@ bool CPNorm::init (CFeatures* features) | |
} | |
/// clean up allocated memory | |
-void CPNorm::cleanup () | |
+template <class ST> void CPNorm<ST>::cleanup () | |
{ | |
} | |
/// initialize preprocessor from file | |
-bool CPNorm::load (FILE* f) | |
+template <class ST> bool CPNorm<ST>::load (FILE* f) | |
{ | |
SG_SET_LOCALE_C; | |
SG_RESET_LOCALE; | |
@@ -61,7 +61,7 @@ bool CPNorm::load (FILE* f) | |
} | |
/// save preprocessor init-data to file | |
-bool CPNorm::save (FILE* f) | |
+template <class ST> bool CPNorm<ST>::save (FILE* f) | |
{ | |
SG_SET_LOCALE_C; | |
SG_RESET_LOCALE; | |
@@ -71,52 +71,52 @@ bool CPNorm::save (FILE* f) | |
/// apply preproc on feature matrix | |
/// result in feature matrix | |
/// return pointer to feature_matrix, i.e. f->get_feature_matrix(); | |
-SGMatrix<float64_t> CPNorm::apply_to_feature_matrix (CFeatures* features) | |
+template <class ST> SGMatrix<ST> CPNorm<ST>::apply_to_feature_matrix (CFeatures* features) | |
{ | |
- SGMatrix<float64_t> feature_matrix=((CDenseFeatures<float64_t>*)features)->get_feature_matrix(); | |
+ SGMatrix<ST> feature_matrix=((CDenseFeatures<ST>*)features)->get_feature_matrix(); | |
for (int32_t i=0; i<feature_matrix.num_cols; i++) | |
{ | |
- float64_t* vec= &(feature_matrix.matrix[i*feature_matrix.num_rows]); | |
- float64_t norm = get_pnorm (vec, feature_matrix.num_rows); | |
- SGVector<float64_t>::scale_vector(1.0/norm, vec, feature_matrix.num_rows); | |
+ ST* vec= &(feature_matrix.matrix[i*feature_matrix.num_rows]); | |
+ floatmax_t norm = get_pnorm (vec, feature_matrix.num_rows); | |
+ SGVector<ST>::scale_vector(1.0/norm, vec, feature_matrix.num_rows); | |
} | |
return feature_matrix; | |
} | |
/// apply preproc on single feature vector | |
/// result in feature matrix | |
-SGVector<float64_t> CPNorm::apply_to_feature_vector (SGVector<float64_t> vector) | |
+template <class ST> SGVector<ST> CPNorm<ST>::apply_to_feature_vector (SGVector<ST> vector) | |
{ | |
- float64_t* normed_vec = SG_MALLOC(float64_t, vector.vlen); | |
- float64_t norm = get_pnorm (vector.vector, vector.vlen); | |
+ ST* normed_vec = SG_MALLOC(ST, vector.vlen); | |
+ floatmax_t norm = get_pnorm (vector.vector, vector.vlen); | |
for (int32_t i=0; i<vector.vlen; i++) | |
normed_vec[i]=vector.vector[i]/norm; | |
- return SGVector<float64_t>(normed_vec,vector.vlen); | |
+ return SGVector<ST>(normed_vec,vector.vlen); | |
} | |
-void CPNorm::set_pnorm (double pnorm) | |
+template <class ST> void CPNorm<ST>::set_pnorm (float64_t pnorm) | |
{ | |
ASSERT (pnorm >= 1.0) | |
m_p = pnorm; | |
register_param (); | |
} | |
-double CPNorm::get_pnorm () const | |
+template <class ST> float64_t CPNorm<ST>::get_pnorm () const | |
{ | |
return m_p; | |
} | |
-void CPNorm::register_param () | |
+template <class ST> void CPNorm<ST>::register_param () | |
{ | |
- m_parameters->add (&m_p, "norm", "P-norm parameter"); | |
+ //SG_ADD(&m_p, "norm", "P-norm parameter", MS_NOT_AVAILABLE); | |
} | |
-inline float64_t CPNorm::get_pnorm (float64_t* vec, int32_t vec_len) const | |
+template <class ST> inline floatmax_t CPNorm<ST>::get_pnorm (ST* vec, int32_t vec_len) const | |
{ | |
- float64_t norm = 0.0; | |
+ floatmax_t norm = 0.0; | |
if (m_p == 1.0) | |
{ | |
for (int i = 0; i < vec_len; ++i) | |
@@ -124,12 +124,26 @@ inline float64_t CPNorm::get_pnorm (float64_t* vec, int32_t vec_len) const | |
} | |
else if (m_p == 2.0) | |
{ | |
- norm = SGVector<float64_t>::twonorm(vec, vec_len); | |
+ norm = SGVector<ST>::twonorm(vec, vec_len); | |
} | |
else | |
{ | |
- norm = SGVector<float64_t>::qnorm(vec, vec_len, m_p); | |
+ norm = SGVector<ST>::qnorm(vec, vec_len, m_p); | |
} | |
return norm; | |
} | |
+ | |
+template class CPNorm<bool>; | |
+template class CPNorm<char>; | |
+template class CPNorm<int8_t>; | |
+template class CPNorm<uint8_t>; | |
+template class CPNorm<int16_t>; | |
+template class CPNorm<uint16_t>; | |
+template class CPNorm<int32_t>; | |
+template class CPNorm<uint32_t>; | |
+template class CPNorm<int64_t>; | |
+template class CPNorm<uint64_t>; | |
+template class CPNorm<float32_t>; | |
+template class CPNorm<float64_t>; | |
+template class CPNorm<floatmax_t>; | |
diff --git a/src/shogun/preprocessor/PNorm.h b/src/shogun/preprocessor/PNorm.h | |
index 5b63e8e..c913fcf 100644 | |
--- a/src/shogun/preprocessor/PNorm.h | |
+++ b/src/shogun/preprocessor/PNorm.h | |
@@ -8,8 +8,8 @@ | |
* Copyright (C) 2012 Viktor Gal | |
*/ | |
-#ifndef _PNORM_ONE__H__ | |
-#define _PNORM_ONE__H__ | |
+#ifndef _PNORM_H__ | |
+#define _PNORM_H__ | |
#include <shogun/preprocessor/DensePreprocessor.h> | |
#include <shogun/features/Features.h> | |
@@ -28,7 +28,7 @@ namespace shogun | |
* \f] | |
* | |
*/ | |
-class CPNorm : public CDensePreprocessor<float64_t> | |
+template <class ST> class CPNorm : public CDensePreprocessor<ST> | |
{ | |
public: | |
/** default PNorm Constructor */ | |
@@ -37,7 +37,7 @@ class CPNorm : public CDensePreprocessor<float64_t> | |
/** constructor | |
* @param p the norm to calculate. NOTE: has to be greater or equal than 1.0 | |
*/ | |
- CPNorm (double p); | |
+ CPNorm (float64_t p); | |
/** destructor */ | |
virtual ~CPNorm (); | |
@@ -54,11 +54,11 @@ class CPNorm : public CDensePreprocessor<float64_t> | |
/// apply preproc on feature matrix | |
/// result in feature matrix | |
/// return pointer to feature_matrix, i.e. f->get_feature_matrix(); | |
- virtual SGMatrix<float64_t> apply_to_feature_matrix (CFeatures* features); | |
+ virtual SGMatrix<ST> apply_to_feature_matrix (CFeatures* features); | |
/// apply preproc on single feature vector | |
/// result in feature matrix | |
- virtual SGVector<float64_t> apply_to_feature_vector (SGVector<float64_t> vector); | |
+ virtual SGVector<ST> apply_to_feature_vector (SGVector<ST> vector); | |
/** @return object name */ | |
virtual const char* get_name () const { return "PNorm"; } | |
@@ -70,20 +70,20 @@ class CPNorm : public CDensePreprocessor<float64_t> | |
* Set norm | |
* @param pnorm norm value | |
*/ | |
- void set_pnorm (double pnorm); | |
+ void set_pnorm (float64_t pnorm); | |
/** | |
* Get norm value | |
* @return norm | |
*/ | |
- double get_pnorm () const; | |
+ float64_t get_pnorm () const; | |
private: | |
void register_param (); | |
- inline float64_t get_pnorm (float64_t* vec, int32_t vec_len) const; | |
+ inline floatmax_t get_pnorm (ST* vec, int32_t vec_len) const; | |
private: | |
- double m_p; | |
+ float64_t m_p; | |
}; | |
} | |
-#endif /* _PNORM_ONE__H__ */ | |
+#endif /* _PNORM_H__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment