ml4r 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,174 @@
1
+ #ifndef RubyUtils_h__
2
+ #define RubyUtils_h__
3
+
4
+ #include <string>
5
+ #include <utility>
6
+ using std::pair;
7
+ using std::string;
8
+
9
+ namespace RubyUtils
10
+ {
11
+ //template<class T> inline VALUE toValue(const T& v);
12
+ //template<class T> inline VALUE toValue(const vector<T>& v);
13
+ // template<class T, class U> inline VALUE toValue(const vector<pair<T,U>>& v);
14
+ //template<class T, class U, class V> inline VALUE toValue(const map<pair<T,U>,V>& v);
15
+ //template<class T, class U> inline VALUE toValue(const map<T,U>& v);
16
+ //template<class T, class U> inline VALUE toValue(const pair<T,U>& v);
17
+ inline VALUE toValue(const char* v) { return otRuby->ToNum(v); }
18
+
19
+ //template<class T> inline T fromValue(VALUE v);
20
+ //template<class T> inline void fromValue(VALUE v, T& t);
21
+ //template<class T, class U> inline void fromValue(VALUE v, pair<T,U>& t);
22
+ //template<class T> inline void fromValue(VALUE v, vector<T>& t);
23
+ };
24
+
25
+ template<> inline VALUE RubyUtils::toValue(const int& v)
26
+ {
27
+ return otRuby->ToNum(v);
28
+ }
29
+
30
+ template<> inline VALUE RubyUtils::toValue(const float& v)
31
+ {
32
+ return otRuby->ToNum(v);
33
+ }
34
+
35
+ template<> inline VALUE RubyUtils::toValue(const double& v)
36
+ {
37
+ return otRuby->ToNum(v);
38
+ }
39
+
40
+ template<> inline VALUE RubyUtils::toValue(const string& v)
41
+ {
42
+ return otRuby->ToNum(v.c_str());
43
+ }
44
+
45
+ template<> inline VALUE RubyUtils::toValue(const VALUE& v)
46
+ {
47
+ return v;
48
+ }
49
+
50
+ template<class T> inline VALUE RubyUtils::toValue(const vector<T>& v)
51
+ {
52
+ VALUE returnValue = otRuby->rb_ary_new();
53
+ BOOST_FOREACH(const T& val, v)
54
+ otRuby->rb_ary_push(returnValue, toValue(val));
55
+ return returnValue;
56
+ }
57
+
58
+ template<class T, class U> inline VALUE RubyUtils::toValue(const pair<T,U>& v)
59
+ {
60
+ VALUE returnValue = otRuby->rb_ary_new();
61
+ otRuby->rb_ary_push(returnValue, toValue(v.first));
62
+ otRuby->rb_ary_push(returnValue, toValue(v.second));
63
+ return returnValue;
64
+ }
65
+
66
+
67
+ template<class T, class U> inline VALUE RubyUtils::toValue(const map<T,U>& v)
68
+ {
69
+ VALUE returnValue = otRuby->rb_hash_new();
70
+ BOOST_FOREACH(auto& keyValue, v)
71
+ {
72
+ otRuby->rb_hash_aset(returnValue, toValue(keyValue.first), toValue(keyValue.second));
73
+ }
74
+ return returnValue;
75
+ }
76
+
77
+ //////////////////////////////////////////////////////////////////////////
78
+ // FROM VALUE
79
+ //////////////////////////////////////////////////////////////////////////
80
+
81
+ template<class T> inline T RubyUtils::fromValue(VALUE v)
82
+ {
83
+ T t;
84
+ fromValue(v,t);
85
+ return t;
86
+ }
87
+
88
+ //////////////////////////////////////////////////////////////////////////
89
+ // FROM VALUE Two argument
90
+ //////////////////////////////////////////////////////////////////////////
91
+
92
+ template<> inline void RubyUtils::fromValue(VALUE v, int& i)
93
+ {
94
+ otRuby->rb_check_type(v, TOtRubyInterface::T_FIXNUM);
95
+ i = otRuby->AsLong(v);
96
+ }
97
+
98
+ template<> inline void RubyUtils::fromValue(VALUE v, double& d)
99
+ {
100
+ otRuby->rb_check_type(v, TOtRubyInterface::T_FLOAT);
101
+ d = otRuby->AsDouble(v);
102
+ }
103
+
104
+ template<> inline void RubyUtils::fromValue(VALUE v, float& f)
105
+ {
106
+ otRuby->rb_check_type(v, TOtRubyInterface::T_FLOAT);
107
+ f = (float) otRuby->AsDouble(v);
108
+ }
109
+
110
+ template<> inline void RubyUtils::fromValue(VALUE v, string& s)
111
+ {
112
+ otRuby->rb_check_type(v, TOtRubyInterface::T_STRING);
113
+ s = otRuby->AsString(v);
114
+ }
115
+
116
+ template<> inline void RubyUtils::fromValue(VALUE v, bool& b)
117
+ {
118
+ if (otRuby->rb_obj_is_kind_of(v, otRuby->rb_cTrueClass()))
119
+ b = true;
120
+ else if (otRuby->rb_obj_is_kind_of(v, otRuby->rb_cFalseClass()))
121
+ b = false;
122
+ else
123
+ {
124
+ otRuby->rb_raise(otRuby->rb_eRuntimeError(), "VALUE is not of type bool");
125
+ }
126
+
127
+ }
128
+ template<> inline void RubyUtils::fromValue(VALUE v, TOtSimpleVariable& sv)
129
+ {
130
+ if (otRuby->rb_obj_is_kind_of(v, otRuby->rb_cString()))
131
+ sv = TOtSimpleVariable(fromValue<string>(v));
132
+ else if (otRuby->rb_obj_is_instance_of(v, otRuby->rb_cFixnum()))
133
+ sv = TOtSimpleVariable(fromValue<int>(v));
134
+ else if (otRuby->rb_obj_is_instance_of(v, otRuby->rb_cFloat()))
135
+ sv = TOtSimpleVariable(fromValue<float>(v));
136
+ else
137
+ {
138
+ otRuby->rb_raise(otRuby->rb_eRuntimeError(), "Unknown VALUE type");
139
+ }
140
+ }
141
+
142
+
143
+ template<> inline void RubyUtils::fromValue(VALUE v, TOtMatrix& f)
144
+ {
145
+ //otRuby->rb_check_type(v, TOtRubyInterface::T_FLOAT);
146
+ if (!otRuby->rb_obj_is_kind_of(v, rb_cMatrix))
147
+ {
148
+ throw std::invalid_argument("[RubyUtils::fromValue] expected a matrix");
149
+ }
150
+ Gclib::TGcMatrix *m = (Gclib::TGcMatrix*)otRuby->GetDataPtr(v);
151
+ f.Connect(m);
152
+ }
153
+
154
+ template<class T, class U> inline void RubyUtils::fromValue(unsigned long v, pair<T,U> &returnValue)
155
+ {
156
+ otRuby->rb_check_type(v, TOtRubyInterface::T_ARRAY);
157
+ VALUE* v_array = otRuby->ToArray(v);
158
+ int length = otRuby->ArrayLen(v);
159
+ if (length != 2) throw runtime_error("Expected a pair, got an array with length " + ToString(length));
160
+ fromValue(v_array[0], returnValue.first);
161
+ fromValue(v_array[1], returnValue.second);
162
+ }
163
+
164
+ template<class T> inline void RubyUtils::fromValue(VALUE v, vector<T>& returnValue)
165
+ {
166
+ otRuby->rb_check_type(v, TOtRubyInterface::T_ARRAY);
167
+ VALUE* v_array = otRuby->ToArray(v);
168
+ int length = otRuby->ArrayLen(v);
169
+ returnValue.resize(length);
170
+ for (int i = 0; i < length; ++i)
171
+ fromValue(v_array[i], returnValue.at(i));
172
+ }
173
+
174
+ #endif // RubyUtils_h__
data/lib/ml4r.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  require 'rubygems'
2
- require 'test_cpp_extension'
2
+ require 'ml4r/ml4r'
@@ -0,0 +1,2 @@
1
+ class TestCppExtension
2
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ml4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,17 +10,30 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-24 00:00:00.000000000 Z
13
+ date: 2012-06-24 00:00:00.000000000Z
14
14
  dependencies: []
15
15
  description: A ruby based library of Maching Learning (ML) algorithms
16
16
  email:
17
17
  - jamie@ieee.org
18
18
  - tim.veitch@veitchlister.com.au
19
19
  executables: []
20
- extensions: []
20
+ extensions:
21
+ - ext/ml4r/extconf.rb
21
22
  extra_rdoc_files: []
22
23
  files:
23
24
  - lib/ml4r.rb
25
+ - lib/test_cpp_extension.rb
26
+ - ext/ml4r/swig/example_wrap.c
27
+ - ext/ml4r/example.h
28
+ - ext/ml4r/LinearRegression/ZenithRegression.h
29
+ - ext/ml4r/LinearRegression.h
30
+ - ext/ml4r/MathUtils.h
31
+ - ext/ml4r/MatrixInversion.h
32
+ - ext/ml4r/OLSLinearRegression.h
33
+ - ext/ml4r/swig/example.h
34
+ - ext/ml4r/utils/RubyUtils.h
35
+ - ext/ml4r/Utils.h
36
+ - ext/ml4r/extconf.rb
24
37
  homepage: https://github.com/vlc/ml4r
25
38
  licenses: []
26
39
  post_install_message: