Skip to content

Commit

Permalink
Merge branch 'master' of github.com:shogun-toolbox/shogun
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Aug 3, 2011
2 parents 6e6794a + 7a745ad commit 9dc8f2b
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 85 deletions.
18 changes: 18 additions & 0 deletions examples/undocumented/csharp_modular/VectorTest.cs
@@ -0,0 +1,18 @@
using System;

public class VectorTest
{
public static void Main(string[] args) {
modshogun.init_shogun_with_defaults();

double[] y = new double[4] {1, 2, 3, 4};
Labels x = new Labels(y);
double[] r = x.get_labels();
for (int i = 0; i < r.Length; i++) {
Console.WriteLine(r[i]);
}

modshogun.exit_shogun();

}
}
29 changes: 29 additions & 0 deletions examples/undocumented/ruby_modular/kernel_linear_byte_modular.rb
@@ -0,0 +1,29 @@
require 'narray'
require 'modshogun'
require 'load'
require 'pp'

traindat = LoadMatrix.load_numbers('../data/fm_train_byte.dat')
testdat = LoadMatrix.load_numbers('../data/fm_test_byte.dat')

parameter_list=[[traindat,testdat],[traindat,testdat]]

def kernel_linear_byte_modular(fm_train_byte=traindat,fm_test_byte=testdat)
pp fm_train_byte
feats_train=Modshogun::ByteFeatures.new(fm_train_byte)
feats_test=Modshogun::ByteFeatures.new(fm_test_byte)

kernel=Modshogun::LinearKernel.new(feats_train, feats_train)
km_train=kernel.get_kernel_matrix()

kernel.init(feats_train, feats_test)
km_test=kernel.get_kernel_matrix()
pp " km_train", km_train
pp " km_test", km_test
return kernel
end

if __FILE__ == $0
print 'LinearByte'
kernel_linear_byte_modular(*parameter_list[0])
end
30 changes: 30 additions & 0 deletions examples/undocumented/ruby_modular/kernel_linear_word_modular.rb
@@ -0,0 +1,30 @@
require 'narray'
require 'modshogun'
require 'load'
require 'pp'

traindat = LoadMatrix.load_numbers('../data/fm_train_word.dat')
testdat = LoadMatrix.load_numbers('../data/fm_test_word.dat')

parameter_list=[[traindat,testdat,1.2],[traindat,testdat,1.2]]

def kernel_linear_word_modular(fm_train_word=traindat,fm_test_word=testdat,scale=1.2)
feats_train=Modshogun::WordFeatures.new(fm_train_word)
feats_test=Modshogun::WordFeatures.new(fm_test_word)

kernel=Modshogun::LinearKernel.new(feats_train, feats_train)
kernel.set_normalizer(Modshogun::AvgDiagKernelNormalizer.new(scale))
kernel.init(feats_train, feats_train)

km_train=kernel.get_kernel_matrix()
kernel.init(feats_train, feats_test)
km_test=kernel.get_kernel_matrix()
pp " km_train", km_train
pp " km_test", km_test
return kernel
end

if __FILE__ == $0
print 'LinearWord'
kernel_linear_word_modular(*parameter_list[0])
end
32 changes: 17 additions & 15 deletions src/interfaces/csharp_modular/swig_typemaps.i
@@ -1,4 +1,3 @@

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -9,22 +8,24 @@
* Based upon code by Baozeng Ding for the modular java interface
*
*/
/*

%define TYPEMAP_SGVECTOR(SGTYPE, CTYPE, CSHARPTYPE)

%typemap(ctype) shogun::SGVector<SGTYPE> %{CTYPE*%} // ctype is the C# equivalent of the javatypemap jni
%typemap(imtype) shogun::SGVector<SGTYPE> %{CSHARPTYPE[]%} // imtype is the C# equivalent of the java typemap jtype
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPArray)]", outattributes="[MarshalAs(UnmanagedType.LPArray)]") shogun::SGVector<SGTYPE> %{CSHARPTYPE[]%} // imtype is the C# equivalent of the java typemap jtype
%typemap(cstype) shogun::SGVector<SGTYPE> %{CSHARPTYPE[]%} // cstype is the C# equivalent of the java typemap cstype

%typemap(in) shogun::SGVector<SGTYPE> (CTYPE *jarr) {
int32_t i, len;
SGTYPE *array;

if (!$input) {
SWIG_CSharpSetPendingException(SWIG_CSharpNullReferenceException, "null array");
return $null;
}
len = ((sizeof($input)) / (sizeof($input[0])));
jarr = (CTYPE *)$input;
len = ((sizeof($input)) / (sizeof($input[0])));
printf("the len is %d\n", len);

array = SG_MALLOC(SGTYPE, len);

Expand Down Expand Up @@ -55,19 +56,21 @@

$result = res;
}

%typemap(csin) shogun::SGVector<SGTYPE> "$csinput"
%enddef


TYPEMAP_SGVECTOR(bool, bool, bool)
TYPEMAP_SGVECTOR(char, char, char)
TYPEMAP_SGVECTOR(uint8_t, uint8_t, byte)
TYPEMAP_SGVECTOR(char, signed char, sbyte)
TYPEMAP_SGVECTOR(uint8_t, unsigned char, byte)
TYPEMAP_SGVECTOR(int16_t, short, short)
TYPEMAP_SGVECTOR(uint16_t, ushort, ushort)
TYPEMAP_SGVECTOR(uint16_t, unsigned short, ushort)
TYPEMAP_SGVECTOR(int32_t, int, int)
TYPEMAP_SGVECTOR(uint32_t, uint, uint)
TYPEMAP_SGVECTOR(int64_t, int64_t, long)
TYPEMAP_SGVECTOR(uint64_t, uint64_t, ulong)
TYPEMAP_SGVECTOR(long long, long, long long)
TYPEMAP_SGVECTOR(uint32_t, unsigned int, uint)
TYPEMAP_SGVECTOR(int64_t, long, int)
TYPEMAP_SGVECTOR(uint64_t, unsigned long, ulong)
TYPEMAP_SGVECTOR(long long, long long, long)
TYPEMAP_SGVECTOR(float32_t, float, float)
TYPEMAP_SGVECTOR(float64_t, double, double)

Expand All @@ -79,7 +82,7 @@ TYPEMAP_SGVECTOR(float64_t, double, double)

///////////////////////////////// SGMATRIX Typemaps - Begin ////////////////////////////////////////

%define TYPEMAP_SGMATRIX(SGTYPE, CTYPE, CSHARPTYPE)
/*%define TYPEMAP_SGMATRIX(SGTYPE, CTYPE, CSHARPTYPE)
%typemap(ctype) shogun::SGMatrix<SGTYPE> %{CSHARPTYPE**%} // CTYPE
%typemap(imtype) shogun::SGMatrix<SGTYPE> %{CSHARPTYPE[][]%}
Expand Down Expand Up @@ -180,6 +183,5 @@ TYPEMAP_SGMATRIX(long long, long, long long)
TYPEMAP_SGMATRIX(float32_t, float, float)
TYPEMAP_SGMATRIX(float64_t, double, double)
#undef TYPEMAP_SGMATRIX
#undef TYPEMAP_SGMATRIX*/
///////////////////////////////// SGMATRIX Typemaps - End //////////////////////////////////////////
*/
157 changes: 134 additions & 23 deletions src/shogun/lib/DataType.h
Expand Up @@ -199,16 +199,16 @@ template<class T> struct SGString
SGString() : string(NULL), slen(0), do_free(false) { }

/** constructor for setting params */
SGString(T* s, index_t l, bool free_string=false)
: string(s), slen(l), do_free(free_string) { }
SGString(T* s, index_t l, bool free=false)
: string(s), slen(l), do_free(free) { }

/** constructor for setting params from a SGVector*/
SGString(SGVector<T> v)
: string(v.vector), slen(v.vlen), do_free(v.do_free) { }

/** constructor to create new matrix in memory */
SGString(index_t len, bool free_string=false) :
slen(len), do_free(free_string)
/** constructor to create new string in memory */
SGString(index_t len, bool free=false) :
slen(len), do_free(free)
{
string=SG_MALLOC(T, len);
}
Expand All @@ -217,6 +217,22 @@ template<class T> struct SGString
SGString(const SGString &orig)
: string(orig.string), slen(orig.slen), do_free(orig.do_free) { }

void free_string()
{
if (do_free)
SG_FREE(string);

string=NULL;
do_free=false;
slen=0;
}

void destroy_string()
{
do_free=true;
free_string();
}

public:
/** string */
T* string;
Expand All @@ -235,11 +251,11 @@ template <class T> struct SGStringList
do_free(false) { }

/** constructor for setting params */
SGStringList(SGString<T>* s, index_t num_s, index_t max_length, bool free_strings=false)
: num_strings(num_s), max_string_length(max_length),
strings(s), do_free(free_strings) { }
SGStringList(SGString<T>* s, index_t num_s, index_t max_length,
bool free_strings=false) : num_strings(num_s),
max_string_length(max_length), strings(s), do_free(free_strings) { }

/** constructor to create new matrix in memory */
/** constructor to create new string list in memory */
SGStringList(index_t num_s, index_t max_length, bool free_strings=false)
: num_strings(num_s), max_string_length(max_length),
do_free(free_strings)
Expand All @@ -248,11 +264,27 @@ template <class T> struct SGStringList
}

/** copy constructor */
SGStringList(const SGStringList &orig)
: num_strings(orig.num_strings),
max_string_length(orig.max_string_length), strings(orig.strings),
do_free(orig.do_free) { }
SGStringList(const SGStringList &orig) :
num_strings(orig.num_strings),
max_string_length(orig.max_string_length),
strings(orig.strings), do_free(orig.do_free) { }

void free_list()
{
if (do_free)
SG_FREE(strings);

strings=NULL;
do_free=false;
num_strings=0;
max_string_length=0;
}

void destroy_list()
{
do_free=true;
free_list();
}

public:
/* number of strings */
Expand Down Expand Up @@ -280,30 +312,106 @@ template <class T> struct SGSparseVectorEntry
/** template class SGSparseVector */
template <class T> struct SGSparseVector
{
public:
/** default constructor */
SGSparseVector() :
features(NULL), num_feat_entries(0), vec_index(0), do_free(false) {}

/** constructor for setting params */
SGSparseVector(SGSparseVectorEntry<T>* feats, index_t num_entries,
index_t index, bool free=false) :
features(feats), num_feat_entries(num_entries), vec_index(index),
do_free(free) {}

/** constructor to create new vector in memory */
SGSparseVector(index_t num_entries, index_t index) :
num_feat_entries(num_entries), vec_index(index)
{
features=SG_MALLOC(SGSparseVectorEntry<T>, num_feat_entries);
}

/** copy constructor */
SGSparseVector(const SGSparseVector& orig) :
vec_index(orig.vec_index), num_feat_entries(orig.num_feat_entries),
features(orig.features) {}

void free_vector()
{
if (do_free)
SG_FREE(features);

features=NULL;
do_free=false;
vec_index=0;
num_feat_entries=0;
}

void destroy_vector()
{
do_free=true;
free_vector();
}

public:
/** vector index */
index_t vec_index;

/** number of feature entries */
index_t num_feat_entries;

/** features */
SGSparseVectorEntry<T>* features;

/** whether vector needs to be freed */
bool do_free;
};

/** template class SGSparseMatrix */
template <class T> class SGSparseMatrix
{
public:
/** default constructor */
SGSparseMatrix() : num_vectors(0), num_features(0), sparse_matrix(NULL) { }
public:
/** default constructor */
SGSparseMatrix() :
num_vectors(0), num_features(0), sparse_matrix(NULL),
do_free(false) { }

/** constructor for setting params */
SGSparseMatrix(SGSparseVector<T>* vecs, index_t num_feat, index_t num_vec)
: num_vectors(num_vec), num_features(num_feat), sparse_matrix(vecs) { }

/** copy constructor */
SGSparseMatrix(const SGSparseMatrix &orig)
: num_vectors(orig.num_vectors), num_features(orig.num_features), sparse_matrix(orig.sparse_matrix) { }
/** constructor for setting params */
SGSparseMatrix(SGSparseVector<T>* vecs, index_t num_feat,
index_t num_vec, bool free=false) :
num_vectors(num_vec), num_features(num_feat),
sparse_matrix(vecs), do_free(free) { }

public:
/** constructor to create new matrix in memory */
SGSparseMatrix(index_t num_vec, index_t num_feat, bool free=false) :
num_vectors(num_vectors), num_features(num_feat), do_free(free)
{
sparse_matrix=SG_MALLOC(SGSparseVector<T>, num_vectors);
}

/** copy constructor */
SGSparseMatrix(const SGSparseMatrix &orig) :
num_vectors(orig.num_vectors), num_features(orig.num_features),
sparse_matrix(orig.sparse_matrix), do_free(orig.do_free) { }

void free_matrix()
{
if (do_free)
SG_FREE(sparse_matrix);

sparse_matrix=NULL;
do_free=false;
num_vectors=0;
num_features=0;
}

void destroy_matrix()
{
do_free=true;
free_matrix();
}

public:
/// total number of vectors
int32_t num_vectors;

Expand All @@ -312,6 +420,9 @@ template <class T> class SGSparseMatrix

/// array of sparse vectors of size num_vectors
SGSparseVector<T>* sparse_matrix;

/** whether vector needs to be freed */
bool do_free;
};

enum EContainerType
Expand Down

0 comments on commit 9dc8f2b

Please sign in to comment.