00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/preprocessor/LogPlusOne.h> 00012 #include <shogun/preprocessor/SimplePreprocessor.h> 00013 #include <shogun/features/Features.h> 00014 #include <shogun/features/SimpleFeatures.h> 00015 #include <shogun/mathematics/Math.h> 00016 00017 using namespace shogun; 00018 00019 CLogPlusOne::CLogPlusOne() 00020 : CSimplePreprocessor<float64_t>() 00021 { 00022 } 00023 00024 00025 CLogPlusOne::~CLogPlusOne() 00026 { 00027 } 00028 00030 bool CLogPlusOne::init(CFeatures* features) 00031 { 00032 ASSERT(features->get_feature_class()==C_SIMPLE); 00033 ASSERT(features->get_feature_type()==F_DREAL); 00034 00035 return true; 00036 } 00037 00039 void CLogPlusOne::cleanup() 00040 { 00041 } 00042 00044 bool CLogPlusOne::load(FILE* f) 00045 { 00046 SG_SET_LOCALE_C; 00047 SG_RESET_LOCALE; 00048 return false; 00049 } 00050 00052 bool CLogPlusOne::save(FILE* f) 00053 { 00054 SG_SET_LOCALE_C; 00055 SG_RESET_LOCALE; 00056 return false; 00057 } 00058 00062 SGMatrix<float64_t> CLogPlusOne::apply_to_feature_matrix(CFeatures* features) 00063 { 00064 SGMatrix<float64_t> feature_matrix = 00065 ((CSimpleFeatures<float64_t>*)features)->get_feature_matrix(); 00066 00067 for (int32_t i=0; i<feature_matrix.num_cols; i++) 00068 { 00069 for (int32_t j=0; j<feature_matrix.num_rows; j++) 00070 feature_matrix.matrix[i*feature_matrix.num_rows+j] = 00071 CMath::log(feature_matrix.matrix[i*feature_matrix.num_rows+j]+1.0); 00072 } 00073 return feature_matrix; 00074 } 00075 00078 SGVector<float64_t> CLogPlusOne::apply_to_feature_vector(SGVector<float64_t> vector) 00079 { 00080 float64_t* log_vec = SG_MALLOC(float64_t, vector.vlen); 00081 00082 for (int32_t i=0; i<vector.vlen; i++) 00083 log_vec[i]=CMath::log(vector.vector[i]+1.0); 00084 00085 return SGVector<float64_t>(log_vec,vector.vlen); 00086 }