Skip to content

Commit

Permalink
Move mean/linspace from SGVector to CStatistics/CMath respectively
Browse files Browse the repository at this point in the history
  • Loading branch information
sanuj committed Dec 21, 2014
1 parent 2913e7b commit 285ad04
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 79 deletions.
3 changes: 2 additions & 1 deletion src/shogun/evaluation/StructuredAccuracy.cpp
Expand Up @@ -13,6 +13,7 @@
#include <shogun/structure/MulticlassSOLabels.h>
#include <shogun/structure/MultilabelSOLabels.h>
#include <shogun/evaluation/MultilabelAccuracy.h>
#include <shogun/mathematics/Statistics.h>

using namespace shogun;

Expand Down Expand Up @@ -121,7 +122,7 @@ float64_t CStructuredAccuracy::evaluate_sequence(CStructuredLabels * predicted,
SG_UNREF(pred_seq);
}

return accuracies.mean();
return CStatistics::mean(accuracies);
}

float64_t CStructuredAccuracy::evaluate_sparse_multilabel(CStructuredLabels * predicted,
Expand Down
40 changes: 0 additions & 40 deletions src/shogun/lib/SGVector.cpp
Expand Up @@ -958,23 +958,6 @@ void SGVector<T>::scale(T alpha)
scale_vector(alpha, vector, vlen);
}

template<class T> float64_t SGVector<T>::mean() const
{
float64_t cum = 0;

for ( index_t i = 0 ; i < vlen ; ++i )
cum += vector[i];

return cum/vlen;
}

template <>
float64_t SGVector<complex128_t>::mean() const
{
SG_SNOTIMPLEMENTED
return float64_t(0.0);
}

template<class T> void SGVector<T>::load(CFile* loader)
{
ASSERT(loader)
Expand Down Expand Up @@ -1010,29 +993,6 @@ void SGVector<complex128_t>::save(CFile* saver)
SG_SERROR("SGVector::save():: Not supported for complex128_t\n");
}

template <class T>
SGVector<float64_t> SGVector<T>::linspace_vec(T start, T end, int32_t n)
{
return SGVector<float64_t>(linspace(start, end, n), n);
}

template <class T>
float64_t* SGVector<T>::linspace(T start, T end, int32_t n)
{
float64_t* output = SG_MALLOC(float64_t, n);
CMath::linspace(output, start, end, n);

return output;
}

template <>
float64_t* SGVector<complex128_t>::linspace(complex128_t start, complex128_t end, int32_t n)
{
float64_t* output = SG_MALLOC(float64_t, n);
SG_SERROR("SGVector::linspace():: Not supported for complex128_t\n");
return output;
}

template <class T> SGVector<float64_t> SGVector<T>::get_real()
{
SGVector<float64_t> real(vlen);
Expand Down
24 changes: 0 additions & 24 deletions src/shogun/lib/SGVector.h
Expand Up @@ -126,24 +126,6 @@ template<class T> class SGVector : public SGReferencedData
*/
void random(T min_value, T max_value);

/** Returns a vector with n linearly spaced elements between start and end.
*
* @param start beginning of the interval to divide
* @param end upper bound of the interval to divide
* @param n number of elements used to divide the interval
* @return vector with linearly spaced elements within the interval
*/
static SGVector<float64_t> linspace_vec(T start, T end, int32_t n);

/** Returns an array with n linearly spaced elements between start and end.
*
* @param start beginning of the interval to divide
* @param end upper bound of the interval to divide
* @param n number of elements used to divide the interval
* @return array with linearly spaced elements within the interval
*/
static float64_t* linspace(T start, T end, int32_t n);

/** For a sorted (ascending) vector, gets the index after the first
* element that is smaller than the given one
*
Expand Down Expand Up @@ -593,12 +575,6 @@ template<class T> class SGVector : public SGReferencedData
/// Scale vector inplace
void scale(T alpha);

/** Compute the mean value of the vector
*
* @return the mean value
*/
float64_t mean() const;

/** Load vector from file
*
* @param loader File object via which to load data
Expand Down
36 changes: 36 additions & 0 deletions src/shogun/mathematics/Math.h
Expand Up @@ -823,6 +823,35 @@ class CMath : public CSGObject
*/
static void linspace(float64_t* output, float64_t start, float64_t end, int32_t n = 100);

/** Returns an array with n linearly spaced elements between start and end.
*
* @param start beginning of the interval to divide
* @param end upper bound of the interval to divide
* @param n number of elements used to divide the interval
* @return array with linearly spaced elements within the interval
*/
template <class T>
static float64_t* linspace(T start, T end, int32_t n)
{
float64_t* output = SG_MALLOC(float64_t, n);
linspace(output, start, end, n);

return output;
}

/** Returns a vector with n linearly spaced elements between start and end.
*
* @param start beginning of the interval to divide
* @param end upper bound of the interval to divide
* @param n number of elements used to divide the interval
* @return vector with linearly spaced elements within the interval
*/
template <class T>
static SGVector<float64_t> linspace_vec(T start, T end, int32_t n)
{
return SGVector<float64_t>(linspace(start, end, n), n);
}

/** Computes \f$\log(\sum_{i=1}^n \exp(x_i))\f$ for given \f$x_i\f$
* using the log-sum-exp trick which avoids numerical problems.
*
Expand Down Expand Up @@ -1777,6 +1806,13 @@ void CMath::min(float64_t* output, T* index, int32_t size)
swap(index[0], index[min_index]);
}

template <>
inline float64_t* CMath::linspace<complex128_t>(complex128_t start, complex128_t end, int32_t n)
{
SG_SERROR("SGVector::linspace():: Not supported for complex128_t\n");
return NULL;
}

#define COMPLEX128_ERROR_ONEARG_T(function) \
template <> \
inline complex128_t CMath::function<complex128_t>(complex128_t a) \
Expand Down
12 changes: 0 additions & 12 deletions src/shogun/mathematics/Statistics.cpp
Expand Up @@ -37,18 +37,6 @@ using namespace Eigen;

using namespace shogun;

float64_t CStatistics::mean(SGVector<float64_t> values)
{
ASSERT(values.vlen>0)
ASSERT(values.vector)

float64_t sum=0;
for (index_t i=0; i<values.vlen; ++i)
sum+=values.vector[i];

return sum/values.vlen;
}

float64_t CStatistics::median(SGVector<float64_t> values, bool modify,
bool in_place)
{
Expand Down
20 changes: 19 additions & 1 deletion src/shogun/mathematics/Statistics.h
Expand Up @@ -19,6 +19,8 @@
#include <math.h>
#include <shogun/lib/config.h>
#include <shogun/base/SGObject.h>
#include <shogun/lib/SGVector.h>
#include <shogun/io/SGIO.h>

namespace shogun
{
Expand All @@ -39,7 +41,16 @@ class CStatistics: public CSGObject
* @param values vector of values
* @return mean of given values
*/
static float64_t mean(SGVector<float64_t> values);
template<class T>
static floatmax_t mean(SGVector<T> vec)
{
floatmax_t sum = 0;

for ( index_t i = 0 ; i < vec.vlen ; ++i )
sum += vec[i];

return sum/vec.vlen;
}

/** Calculates median of given values. The median is the value that one
* gets when the input vector is sorted and then selects the middle value.
Expand Down Expand Up @@ -654,6 +665,13 @@ class CStatistics: public CSGObject
static inline bool greater_equal(float64_t a, float64_t b) { return a>=b; }
};

template <>
inline floatmax_t CStatistics::mean<complex128_t>(SGVector<complex128_t> vec)
{
SG_SNOTIMPLEMENTED
return floatmax_t(0.0);
}

}

#endif /* __STATISTICS_H_ */
3 changes: 2 additions & 1 deletion src/shogun/structure/HMSVMModel.cpp
Expand Up @@ -12,6 +12,7 @@
#include <shogun/features/MatrixFeatures.h>
#include <shogun/structure/TwoStateModel.h>
#include <shogun/structure/Plif.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand Down Expand Up @@ -558,7 +559,7 @@ void CHMSVMModel::init_training()
}

// Choose the supporting points so that roughly the same number of points fall in each bin
SGVector< float64_t > a = SGVector< float64_t >::linspace_vec(1, N, m_num_plif_nodes+1);
SGVector< float64_t > a = CMath::linspace_vec(1, N, m_num_plif_nodes+1);
SGVector< index_t > signal_idxs(m_num_plif_nodes);
for ( int32_t i = 0 ; i < signal_idxs.vlen ; ++i )
signal_idxs[i] = (index_t) CMath::round( (a[i] + a[i+1]) / 2 ) - 1;
Expand Down

0 comments on commit 285ad04

Please sign in to comment.