Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix compilation of libshogun examples
use SGMatrix mat(dims, nvec) whereever possible
  • Loading branch information
Soeren Sonnenburg committed May 8, 2012
1 parent de8733d commit 9c81f4e
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 67 deletions.
Expand Up @@ -140,7 +140,6 @@ class CTestClassFloat : public CSGObject

virtual ~CTestClassFloat()
{
m_matrix.destroy_matrix();
SG_UNREF(m_features);
}

Expand Down
Expand Up @@ -140,7 +140,6 @@ class CTestClassFloat : public CSGObject

virtual ~CTestClassFloat()
{
m_matrix.destroy_matrix();
SG_UNREF(m_features);
}

Expand Down
Expand Up @@ -147,7 +147,6 @@ class CTestClassFloat : public CSGObject

virtual ~CTestClassFloat()
{
m_matrix.destroy_matrix();
SG_UNREF(m_features);
}

Expand Down
7 changes: 3 additions & 4 deletions examples/undocumented/libshogun/classifier_conjugateindex.cpp
Expand Up @@ -12,14 +12,13 @@ int main(int argc, char** argv)
init_shogun_with_defaults();

// create some data
float64_t* matrix = SG_MALLOC(float64_t, 6);
SGMatrix<float64_t> matrix(2,3);
for (int32_t i=0; i<6; i++)
matrix[i]=i;
matrix.matrix[i]=i;

// create three 2-dimensional vectors
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>();
features->set_feature_matrix(matrix, 2, 3);
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);

// create three labels
CLabels* labels=new CLabels(3);
Expand Down
Expand Up @@ -12,14 +12,13 @@ int main(int argc, char** argv)
init_shogun_with_defaults();

// create some data
float64_t* matrix = SG_MALLOC(float64_t, 6);
SGMatrix<float64_t> matrix(2,3);
for (int32_t i=0; i<6; i++)
matrix[i]=i;
matrix.matrix[i]=i;

// create three 2-dimensional vectors
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>();
features->set_feature_matrix(matrix, 2, 3);
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);

// create three labels
CLabels* labels=new CLabels(3);
Expand Down
12 changes: 6 additions & 6 deletions examples/undocumented/libshogun/classifier_libsvm.cpp
Expand Up @@ -24,12 +24,12 @@ using namespace shogun;
#define DIST 0.5

float64_t* lab;
float64_t* feat;
SGMatrix<float64_t> feat;

void gen_rand_data()
{
lab=SG_MALLOC(float64_t, NUM);
feat=SG_MALLOC(float64_t, NUM*DIMS);
feat=SGMatrix<float64_t>(DIMS, NUM);

for (int32_t i=0; i<NUM; i++)
{
Expand All @@ -38,18 +38,18 @@ void gen_rand_data()
lab[i]=-1.0;

for (int32_t j=0; j<DIMS; j++)
feat[i*DIMS+j]=CMath::random(0.0,1.0)+DIST;
feat.matrix[i*DIMS+j]=CMath::random(0.0,1.0)+DIST;
}
else
{
lab[i]=1.0;

for (int32_t j=0; j<DIMS; j++)
feat[i*DIMS+j]=CMath::random(0.0,1.0)-DIST;
feat.matrix[i*DIMS+j]=CMath::random(0.0,1.0)-DIST;
}
}
CMath::display_vector(lab,NUM);
CMath::display_matrix(feat,DIMS, NUM);
CMath::display_matrix(feat.matrix,DIMS, NUM);
}

int main()
Expand All @@ -71,7 +71,7 @@ int main()
// create train features
CDenseFeatures<float64_t>* features = new CDenseFeatures<float64_t>(feature_cache);
SG_REF(features);
features->set_feature_matrix(feat, DIMS, NUM);
features->set_feature_matrix(feat);

// create gaussian kernel
CGaussianKernel* kernel = new CGaussianKernel(kernel_cache, rbf_width);
Expand Down
6 changes: 3 additions & 3 deletions examples/undocumented/libshogun/classifier_minimal_svm.cpp
Expand Up @@ -18,14 +18,14 @@ int main(int argc, char** argv)
init_shogun(&print_message);

// create some data
float64_t* matrix = SG_MALLOC(float64_t, 6);
SGMatrix<float64_t> matrix(2,3);
for (int32_t i=0; i<6; i++)
matrix[i]=i;
matrix.matrix[i]=i;

// create three 2-dimensional vectors
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>();
features->set_feature_matrix(matrix, 2, 3);
features->set_feature_matrix(matrix);

// create three labels
CLabels* labels=new CLabels(3);
Expand Down
3 changes: 1 addition & 2 deletions examples/undocumented/libshogun/classifier_qda.cpp
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char ** argv)
init_shogun_with_defaults();

SGVector< float64_t > lab(NUM);
SGMatrix< float64_t > feat(NUM, DIMS);
SGMatrix< float64_t > feat(DIMS, NUM);

gen_rand_data(lab, feat);

Expand All @@ -58,7 +58,6 @@ int main(int argc, char ** argv)

// Create train features
CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feature_cache);
features->set_feature_matrix(feat.matrix, DIMS, NUM);

// Create QDA classifier
CQDA* qda = new CQDA(features, labels);
Expand Down
Expand Up @@ -26,9 +26,7 @@ int main()
for (int i=0; i<N*dim; i++)
matrix[i] = i;

const int32_t feature_cache = 0;
CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >( feature_cache );
features->set_feature_matrix(matrix.matrix, dim, N);
CDenseFeatures< float64_t >* features = new CDenseFeatures<float64_t>(matrix);
SG_REF(features);

// Create embedding and set parameters for global strategy
Expand Down
7 changes: 3 additions & 4 deletions examples/undocumented/libshogun/kernel_gaussian.cpp
Expand Up @@ -17,14 +17,13 @@ int main(int argc, char** argv)
init_shogun(&print_message);

// create some data
float64_t* matrix = SG_MALLOC(float64_t, 6);
SGMatrix<float64_t> matrix(2,3);
for (int32_t i=0; i<6; i++)
matrix[i]=i;
matrix.matrix[i]=i;

// create three 2-dimensional vectors
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>();
features->set_feature_matrix(matrix, 2, 3);
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);

// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(features, features, 10, 0.5);
Expand Down
6 changes: 3 additions & 3 deletions examples/undocumented/libshogun/kernel_revlin.cpp
Expand Up @@ -113,14 +113,14 @@ int main(int argc, char** argv)
init_shogun(&print_message);

// create some data
float64_t* matrix = SG_MALLOC(float64_t, 6);
SGMatrix<float64_t> matrix(2,3);
for (int32_t i=0; i<6; i++)
matrix[i]=i;
matrix.matrix[i]=i;

// create three 2-dimensional vectors
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>();
features->set_feature_matrix(matrix, 2, 3);
features->set_feature_matrix(matrix);

// create reverse linear kernel
CReverseLinearKernel* kernel = new CReverseLinearKernel();
Expand Down
Expand Up @@ -53,15 +53,14 @@ CModelSelectionParameters* create_param_tree()
void apply_parameter_tree(CDynamicObjectArray* combinations)
{
/* create some data */
float64_t* matrix=SG_MALLOC(float64_t, 6);
SGMatrix<float64_t> matrix(2,3);
for (index_t i=0; i<6; i++)
matrix[i]=i;
matrix.matrix[i]=i;

/* create three 2-dimensional vectors
* to avoid deleting these, REF now and UNREF when finished */
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> ();
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix);
SG_REF(features);
features->set_feature_matrix(matrix, 2, 3);

/* create three labels, will be handed to svm and automaticall deleted */
CLabels* labels=new CLabels(3);
Expand Down
Expand Up @@ -100,14 +100,14 @@ int main(int argc, char **argv)
int32_t dim_vectors=3;

/* create some data and labels */
float64_t* matrix=SG_MALLOC(float64_t, num_vectors*dim_vectors);
SGMatrix<float64_t> matrix(dim_vectors, num_vectors);
CLabels* labels=new CLabels(num_vectors);

for (int32_t i=0; i<num_vectors*dim_vectors; i++)
matrix[i]=CMath::randn_double();
matrix.matrix[i]=CMath::randn_double();

/* create num_feautres 2-dimensional vectors */
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> ();
features->set_feature_matrix(matrix, dim_vectors, num_vectors);
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix);

/* create labels, two classes */
for (index_t i=0; i<num_vectors; ++i)
Expand Down
Expand Up @@ -48,20 +48,19 @@ int main(int argc, char **argv)

#ifdef HAVE_LAPACK
int32_t num_subsets=5;
int32_t num_features=11;
int32_t num_vectors=11;

/* create some data */
float64_t* matrix=SG_MALLOC(float64_t, num_features*2);
for (int32_t i=0; i<num_features*2; i++)
matrix[i]=i;
SGMatrix<float64_t> matrix(2, num_vectors);
for (int32_t i=0; i<num_vectors*2; i++)
matrix.matrix[i]=i;

/* create num_feautres 2-dimensional vectors */
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> ();
features->set_feature_matrix(matrix, 2, num_features);
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix);

/* create three labels */
CLabels* labels=new CLabels(num_features);
for (index_t i=0; i<num_features; ++i)
CLabels* labels=new CLabels(num_vectors);
for (index_t i=0; i<num_vectors; ++i)
labels->set_label(i, i%2==0 ? 1 : -1);

/* create linear classifier (use -s 2 option to avoid warnings) */
Expand Down
10 changes: 5 additions & 5 deletions examples/undocumented/libshogun/parameter_iterate_float64.cpp
Expand Up @@ -31,15 +31,15 @@ int main(int argc, char** argv)
init_shogun(&print_message);

/* create some random data */
float64_t* matrix = SG_MALLOC(float64_t, n*n);
SGMatrix<float64_t> matrix(n,n);

for(int32_t i=0; i<n*n; ++i)
matrix[i]=CMath::random((float64_t)-n,(float64_t)n);
matrix.matrix[i]=CMath::random((float64_t)-n,(float64_t)n);

CMath::display_matrix(matrix, n, n);
CMath::display_matrix(matrix.matrix, n, n);

/* create n n-dimensional feature vectors */
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>();
features->set_feature_matrix(matrix, n, n);
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);

/* create gaussian kernel with cache 10MB, width will be changed later */
CGaussianKernel* kernel = new CGaussianKernel(10, 0);
Expand Down
10 changes: 5 additions & 5 deletions examples/undocumented/libshogun/parameter_iterate_sgobject.cpp
Expand Up @@ -31,14 +31,14 @@ int main(int argc, char** argv)
init_shogun(&print_message);

/* create some random data and hand it to each kernel */
float64_t* matrix=SG_MALLOC(float64_t, n*n);
SGMatrix<float64_t> matrix(n,n);
for (int32_t k=0; k<n*n; ++k)
matrix[k]=CMath::random((float64_t) -n, (float64_t) n);
matrix.matrix[k]=CMath::random((float64_t) -n, (float64_t) n);

SG_SPRINT("feature data:\n");
CMath::display_matrix(matrix, n, n);
CMath::display_matrix(matrix.matrix, n, n);

CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> ();
features->set_feature_matrix(matrix, n, n);
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix);

/* create n kernels with n features each */
CGaussianKernel** kernels=SG_MALLOC(CGaussianKernel*, n);
Expand Down
12 changes: 4 additions & 8 deletions examples/undocumented/libshogun/regression_gaussian_process.cpp
@@ -1,4 +1,4 @@
#include <shogun/features/SimpleFeatures.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/regression/GaussianProcessRegression.h>
Expand All @@ -23,9 +23,9 @@ int main(int argc, char** argv)
#ifdef HAVE_LAPACK

// create some data
float64_t* matrix = SG_MALLOC(float64_t, 6);
SGMatrix<float64_t> matrix(2,3);
for (int32_t i=0; i<6; i++)
matrix[i]=i;
matrix.matrix[i]=i;

//Labels
CLabels* labels = new CLabels(3);
Expand All @@ -37,9 +37,8 @@ int main(int argc, char** argv)

// create three 2-dimensional vectors
// shogun will now own the matrix created
CSimpleFeatures<float64_t>* features= new CSimpleFeatures<float64_t>();
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);
SG_REF(features);
features->set_feature_matrix(matrix, 2, 3);

// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(10, 0.5);
Expand Down Expand Up @@ -71,6 +70,3 @@ int main(int argc, char** argv)
exit_shogun();
return 0;
}



Expand Up @@ -48,7 +48,6 @@ class CTestClass : public CSGObject

virtual ~CTestClass()
{
m_mat.destroy_matrix();
SG_UNREF(m_features);
}

Expand Down

0 comments on commit 9c81f4e

Please sign in to comment.