Skip to content

Commit

Permalink
Merge pull request #259 from sploving/master
Browse files Browse the repository at this point in the history
support c# sgvector
  • Loading branch information
Soeren Sonnenburg committed Aug 3, 2011
2 parents 1e48c60 + a8175a3 commit 7a745ad
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 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 //////////////////////////////////////////
*/

0 comments on commit 7a745ad

Please sign in to comment.