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/DensePreprocessor.h> 00013 #include <shogun/features/Features.h> 00014 #include <shogun/mathematics/Math.h> 00015 00016 using namespace shogun; 00017 00018 CLogPlusOne::CLogPlusOne() 00019 : CDensePreprocessor<float64_t>() 00020 { 00021 } 00022 00023 00024 CLogPlusOne::~CLogPlusOne() 00025 { 00026 } 00027 00029 bool CLogPlusOne::init(CFeatures* features) 00030 { 00031 ASSERT(features->get_feature_class()==C_DENSE); 00032 ASSERT(features->get_feature_type()==F_DREAL); 00033 00034 return true; 00035 } 00036 00038 void CLogPlusOne::cleanup() 00039 { 00040 } 00041 00043 bool CLogPlusOne::load(FILE* f) 00044 { 00045 SG_SET_LOCALE_C; 00046 SG_RESET_LOCALE; 00047 return false; 00048 } 00049 00051 bool CLogPlusOne::save(FILE* f) 00052 { 00053 SG_SET_LOCALE_C; 00054 SG_RESET_LOCALE; 00055 return false; 00056 } 00057 00061 SGMatrix<float64_t> CLogPlusOne::apply_to_feature_matrix(CFeatures* features) 00062 { 00063 SGMatrix<float64_t> feature_matrix = 00064 ((CDenseFeatures<float64_t>*)features)->get_feature_matrix(); 00065 00066 for (int32_t i=0; i<feature_matrix.num_cols; i++) 00067 { 00068 for (int32_t j=0; j<feature_matrix.num_rows; j++) 00069 feature_matrix.matrix[i*feature_matrix.num_rows+j] = 00070 CMath::log(feature_matrix.matrix[i*feature_matrix.num_rows+j]+1.0); 00071 } 00072 return feature_matrix; 00073 } 00074 00077 SGVector<float64_t> CLogPlusOne::apply_to_feature_vector(SGVector<float64_t> vector) 00078 { 00079 float64_t* log_vec = SG_MALLOC(float64_t, vector.vlen); 00080 00081 for (int32_t i=0; i<vector.vlen; i++) 00082 log_vec[i]=CMath::log(vector.vector[i]+1.0); 00083 00084 return SGVector<float64_t>(log_vec,vector.vlen); 00085 }