From 176a1b2419fd125c621633d3e9fb3b078e508c33 Mon Sep 17 00:00:00 2001 From: Sergey Lisitsyn Date: Mon, 20 Jun 2011 14:08:50 +0400 Subject: [PATCH] Fixes for Alesis code --- src/libshogun/clustering/GMM.cpp | 18 ++++++----- src/libshogun/clustering/GMM.h | 21 ++++-------- src/libshogun/distributions/Gaussian.cpp | 19 +++++------ src/libshogun/distributions/Gaussian.h | 41 +++++++++--------------- 4 files changed, 39 insertions(+), 60 deletions(-) diff --git a/src/libshogun/clustering/GMM.cpp b/src/libshogun/clustering/GMM.cpp index e3d83c605cb..08be2591a31 100644 --- a/src/libshogun/clustering/GMM.cpp +++ b/src/libshogun/clustering/GMM.cpp @@ -69,10 +69,12 @@ bool CGMM::train(CFeatures* data) CEuclidianDistance* dist = new CEuclidianDistance(); CKMeans* init_k_means = new CKMeans(m_n, dist); init_k_means->train(dotdata); - float64_t* init_means; - int32_t init_mean_dim; - int32_t init_mean_size; - init_k_means->get_cluster_centers(&init_means, &init_mean_dim, &init_mean_size); + // sorry ;) + SGMatrix cluster_centers = init_k_means->get_cluster_centers(); + float64_t* init_means = cluster_centers.matrix; + int32_t init_mean_dim = cluster_centers.num_rows; + int32_t init_mean_size = cluster_centers.num_cols; + float64_t* init_cov; int32_t init_cov_rows; @@ -85,8 +87,8 @@ bool CGMM::train(CFeatures* data) for (int i=0; i(&(init_means[i*init_mean_dim]), init_mean_dim), + SGMatrix(init_cov, init_cov_rows, init_cov_cols)); } /** question of faster vs. less memory using */ @@ -155,7 +157,7 @@ bool CGMM::train(CFeatures* data) for (int j=0; jset_mean(mean_sum, num_dim); + m_components[i]->set_mean(SGVector(mean_sum, num_dim)); cov_sum = new float64_t[num_dim*num_dim]; memset(cov_sum, 0, num_dim*num_dim*sizeof(float64_t)); @@ -172,7 +174,7 @@ bool CGMM::train(CFeatures* data) for (int j=0; jset_cov(cov_sum, num_dim, num_dim); + m_components[i]->set_cov(SGMatrix(cov_sum, num_dim, num_dim)); delete[] mean_sum; delete[] cov_sum; diff --git a/src/libshogun/clustering/GMM.h b/src/libshogun/clustering/GMM.h index f378e508e69..7b7f15630fe 100644 --- a/src/libshogun/clustering/GMM.h +++ b/src/libshogun/clustering/GMM.h @@ -94,39 +94,30 @@ class CGMM : public CDistribution /** get nth mean * - * @param mean copy of the mean - * @param mean_length * @param num which mean to retrieve */ - virtual inline void get_nth_mean(float64_t** mean, int32_t* mean_length, int32_t num) + virtual SGVector get_nth_mean(int32_t num) { ASSERT(numget_mean(mean, mean_length); + return m_components[num]->get_mean(); } /** get nth cov * - * @param cov copy of the cov - * @param cov_rows - * @param cov_cols * @param num which covariance to retrieve */ - virtual inline void get_nth_cov(float64_t** cov, int32_t* cov_rows, int32_t* cov_cols, int32_t num) + virtual SGMatrix get_nth_cov(int32_t num) { ASSERT(numget_cov(cov, cov_rows, cov_cols); + return m_components[num]->get_cov(); } /** get coefficients * - * @param coef copy of coeffiecients - * @param coef_length coef vector length */ - virtual inline void get_coef(float64_t** coef, int32_t* coef_length) + virtual SGVector get_coef() { - *coef = new float64_t[m_coef_size]; - memcpy(*coef, m_coefficients, sizeof(float64_t)*m_coef_size); - *coef_length = m_coef_size; + return SGVector(m_coefficients,m_coef_size); } /** @return object name */ diff --git a/src/libshogun/distributions/Gaussian.cpp b/src/libshogun/distributions/Gaussian.cpp index ec2a554794e..b632952c9fd 100644 --- a/src/libshogun/distributions/Gaussian.cpp +++ b/src/libshogun/distributions/Gaussian.cpp @@ -24,19 +24,16 @@ m_mean_length(0) { } -CGaussian::CGaussian(float64_t* mean, int32_t mean_length, - float64_t* cov, int32_t cov_rows, int32_t cov_cols) : CDistribution(), +CGaussian::CGaussian(SGVector mean_vector, SGMatrix cov_matrix) : CDistribution(), m_cov_inverse(NULL) { - ASSERT(mean_length == cov_rows); - ASSERT(cov_rows == cov_cols); - m_mean = new float64_t[mean_length]; - memcpy(m_mean, mean, sizeof(float64_t)*mean_length); - m_cov = new float64_t[cov_rows*cov_cols]; - memcpy(m_cov, cov, sizeof(float64_t)*cov_rows*cov_cols); - m_mean_length = mean_length; - m_cov_rows = cov_rows; - m_cov_cols = cov_cols; + ASSERT(mean_vector.vlen == cov_matrix.num_rows); + ASSERT(cov_matrix.num_rows == cov_matrix.num_cols); + m_mean = mean_vector.vector; + m_cov = cov_matrix.matrix; + m_mean_length = mean_vector.vlen; + m_cov_rows = cov_matrix.num_rows; + m_cov_cols = cov_matrix.num_cols; init(); register_params(); } diff --git a/src/libshogun/distributions/Gaussian.h b/src/libshogun/distributions/Gaussian.h index d1242dc10d1..49c95e2bfbd 100644 --- a/src/libshogun/distributions/Gaussian.h +++ b/src/libshogun/distributions/Gaussian.h @@ -33,8 +33,10 @@ class CDotFeatures; class CGaussian : public CDistribution { public: + /** default constructor */ CGaussian(); + /** constructor * * @param mean mean of the Gaussian @@ -43,8 +45,8 @@ class CGaussian : public CDistribution * @param cov_rows * @param cov_cols */ - CGaussian(float64_t* mean, int32_t mean_length, - float64_t* cov, int32_t cov_rows, int32_t cov_cols); + CGaussian(SGVector mean_vector, SGMatrix cov_matrix); + virtual ~CGaussian(); /** Compute the inverse covariance and constant part */ @@ -110,14 +112,10 @@ class CGaussian : public CDistribution /** get mean * - * @param mean copy of the mean - * @param mean_length */ - virtual inline void get_mean(float64_t** mean, int32_t* mean_length) + virtual SGVector get_mean() { - *mean = new float64_t[m_mean_length]; - memcpy(*mean, m_mean, sizeof(float64_t)*m_mean_length); - *mean_length = m_mean_length; + return SGVector(m_mean,m_mean_length); } /** set mean @@ -125,37 +123,28 @@ class CGaussian : public CDistribution * @param mean new mean * @param mean_length has to match current mean length */ - virtual inline void set_mean(float64_t* mean, int32_t mean_length) + virtual void set_mean(SGVector mean_vector) { - ASSERT(mean_length == m_mean_length); - memcpy(m_mean, mean, sizeof(float64_t)*m_mean_length); + ASSERT(mean_vector.vlen == m_mean_length); + memcpy(m_mean, mean_vector.vector, sizeof(float64_t)*m_mean_length); } /** get cov * - * @param cov copy of the cov - * @param cov_rows - * @param cov_cols */ - virtual inline void get_cov(float64_t** cov, int32_t* cov_rows, int32_t* cov_cols) + virtual SGMatrix get_cov() { - *cov = new float64_t[m_cov_rows*m_cov_cols]; - memcpy(*cov, m_cov, sizeof(float64_t)*m_cov_rows*m_cov_cols); - *cov_rows = m_cov_rows; - *cov_cols = m_cov_cols; + return SGMatrix(m_cov,m_cov_rows,m_cov_cols); } /** set cov * - * @param cov new cov - * @param cov_rows has to match current cov rows - * @param cov_cols has to be equal to cov_rows */ - virtual inline void set_cov(float64_t* cov, int32_t cov_rows, int32_t cov_cols) + virtual inline void set_cov(SGMatrix cov_matrix) { - ASSERT(cov_rows = cov_cols); - ASSERT(cov_rows = m_cov_rows); - memcpy(m_cov, cov, sizeof(float64_t)*m_cov_rows*m_cov_cols); + ASSERT(cov_matrix.num_rows = cov_matrix.num_cols); + ASSERT(cov_matrix.num_rows = m_cov_rows); + memcpy(m_cov, cov_matrix.matrix, sizeof(float64_t)*m_cov_rows*m_cov_cols); init(); }