nmatrix 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/ext/nmatrix/data/complex.h +183 -159
  3. data/ext/nmatrix/data/data.cpp +113 -112
  4. data/ext/nmatrix/data/data.h +306 -292
  5. data/ext/nmatrix/data/ruby_object.h +193 -193
  6. data/ext/nmatrix/extconf.rb +11 -9
  7. data/ext/nmatrix/math.cpp +9 -11
  8. data/ext/nmatrix/math/math.h +3 -2
  9. data/ext/nmatrix/math/trsm.h +152 -152
  10. data/ext/nmatrix/nmatrix.h +30 -0
  11. data/ext/nmatrix/ruby_constants.cpp +67 -67
  12. data/ext/nmatrix/ruby_constants.h +35 -35
  13. data/ext/nmatrix/ruby_nmatrix.c +168 -183
  14. data/ext/nmatrix/storage/common.h +4 -3
  15. data/ext/nmatrix/storage/dense/dense.cpp +50 -50
  16. data/ext/nmatrix/storage/dense/dense.h +8 -7
  17. data/ext/nmatrix/storage/list/list.cpp +16 -16
  18. data/ext/nmatrix/storage/list/list.h +7 -6
  19. data/ext/nmatrix/storage/storage.cpp +32 -32
  20. data/ext/nmatrix/storage/storage.h +12 -11
  21. data/ext/nmatrix/storage/yale/class.h +2 -2
  22. data/ext/nmatrix/storage/yale/iterators/base.h +2 -1
  23. data/ext/nmatrix/storage/yale/iterators/iterator.h +2 -1
  24. data/ext/nmatrix/storage/yale/iterators/row.h +2 -1
  25. data/ext/nmatrix/storage/yale/iterators/row_stored.h +2 -1
  26. data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +1 -0
  27. data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +2 -1
  28. data/ext/nmatrix/storage/yale/yale.cpp +27 -27
  29. data/ext/nmatrix/storage/yale/yale.h +7 -6
  30. data/ext/nmatrix/ttable_helper.rb +10 -10
  31. data/ext/nmatrix/types.h +3 -2
  32. data/ext/nmatrix/util/io.cpp +7 -7
  33. data/ext/nmatrix/util/sl_list.cpp +26 -26
  34. data/ext/nmatrix/util/sl_list.h +19 -18
  35. data/lib/nmatrix/blas.rb +7 -7
  36. data/lib/nmatrix/io/mat5_reader.rb +30 -30
  37. data/lib/nmatrix/math.rb +73 -17
  38. data/lib/nmatrix/nmatrix.rb +10 -8
  39. data/lib/nmatrix/shortcuts.rb +3 -3
  40. data/lib/nmatrix/version.rb +3 -3
  41. data/spec/00_nmatrix_spec.rb +6 -0
  42. data/spec/math_spec.rb +77 -0
  43. data/spec/spec_helper.rb +9 -0
  44. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e74b234f446ff409c7d37952bed15df165907f4d
4
- data.tar.gz: 5f7f575774e923ac5ec7bd7c86147dbd12130d08
3
+ metadata.gz: 862e6785562317ff3ffe71dfa679ab58d3bfd219
4
+ data.tar.gz: 428075f3287f76f862bda8dbf6efa837272e6db9
5
5
  SHA512:
6
- metadata.gz: 81f4bbbc2365d9bf2df8ce924f525599292b2d9177dc5300193c2628c308d8ae9451f7c29a105e0abf4dca7c6b77473e36947132ea4fa39e1eda968fe06fda40
7
- data.tar.gz: e8f9fe883b3c04cd329a081eb2af9d6d9643729be4bc1ee398ec1c5e53dd7b5e77cd7e6acdfd42b93148a4a1353931e2eda880fe81a7656457e0fe7cf7da6f15
6
+ metadata.gz: 3d845e422a71670756115bb9a9c6eb6c448b89c08f471395a4ffec74b71bdb4052732df7e619aabc1f5a825b5e2c8f36cc215aaa4b1b20192c1575729a5698e2
7
+ data.tar.gz: 8737125b9d9778afa68ee7ac195014f286d29bea4fc7d33081a14610995e6df312fd2767a56207352534b718ae1c361a4c9ff2a5ad289043c091e4687be0fec9
@@ -32,6 +32,7 @@
32
32
  * Standard Includes
33
33
  */
34
34
 
35
+ #include <ruby.h>
35
36
  #include <type_traits>
36
37
  #include <iostream>
37
38
  #include <cmath>
@@ -67,23 +68,39 @@ typedef Complex<float64_t> Complex128;
67
68
 
68
69
  template <typename Type>
69
70
  class Complex {
70
- public:
71
- // The real and immaginary parts of the complex number.
72
- Type r;
73
- Type i;
71
+ public:
72
+ // The real and immaginary parts of the complex number.
73
+ Type r;
74
+ Type i;
74
75
 
75
- /*
76
- * Default constructor.
77
- */
78
- inline Complex(Type real = 0, Type imaginary = 0) : r(real), i(imaginary) {}
76
+ /*
77
+ * Default constructor.
78
+ */
79
+ inline Complex(Type real = 0, Type imaginary = 0) : r(real), i(imaginary) {}
80
+
81
+ /*
82
+ * Copy constructors.
83
+ */
84
+ template <typename ComplexType>
85
+ explicit inline Complex(const Complex<ComplexType>& other) : r(other.r), i(other.i) {}
79
86
 
80
- /*
81
- * Copy constructors.
82
- */
83
- template <typename ComplexType>
84
- inline Complex(const Complex<ComplexType>& other) : r(other.r), i(other.i) {}
87
+ template <typename ComplexType>
88
+ inline Complex<Type>& operator=(const Complex<ComplexType>& other) {
89
+ this->r = static_cast<Type>(other.r);
90
+ this->i = static_cast<Type>(other.i);
91
+ return *this;
92
+ }
93
+
94
+ explicit Complex(const RubyObject& other);
85
95
 
86
- Complex(const RubyObject& other);
96
+ Complex<Type>& operator=(const RubyObject& other);
97
+
98
+ template<typename OtherType>
99
+ inline Complex<Type>& operator=(const OtherType& real) {
100
+ this->r = Type(real);
101
+ this->i = Type(0);
102
+ return *this;
103
+ }
87
104
 
88
105
  /*
89
106
  * Complex conjugate function -- creates a copy, but inverted.
@@ -103,20 +120,25 @@ class Complex {
103
120
  return Complex<Type>(conj.r / denom, conj.i / denom);
104
121
  }
105
122
 
123
+ // Negative operator
124
+ inline Complex<Type> operator-() const {
125
+ return Complex<Type>(-this->r, -this->i);
126
+ }
106
127
 
107
128
 
108
- /*
109
- * Binary operator definitions for various types.
110
- */
111
129
 
112
- ////////////////////////////////
113
- // Complex-Complex Operations //
114
- ////////////////////////////////
130
+ /*
131
+ * Binary operator definitions for various types.
132
+ */
115
133
 
116
- template <typename OtherType>
117
- inline Complex<Type> operator+(const Complex<OtherType>& other) const {
118
- return Complex<Type>(this->r + other.r, this->i + other.i);
119
- }
134
+ ////////////////////////////////
135
+ // Complex-Complex Operations //
136
+ ////////////////////////////////
137
+
138
+ template <typename OtherType>
139
+ inline Complex<Type> operator+(const Complex<OtherType>& other) const {
140
+ return Complex<Type>(this->r + other.r, this->i + other.i);
141
+ }
120
142
 
121
143
  template <typename OtherType>
122
144
  inline Complex<Type>& operator+=(const Complex<OtherType>& other) {
@@ -132,15 +154,15 @@ class Complex {
132
154
  return *this;
133
155
  }
134
156
 
135
- template <typename OtherType>
136
- inline Complex<Type> operator-(const Complex<OtherType>& other) const {
137
- return Complex<Type>(this->r - other.r, this->i - other.i);
138
- }
157
+ template <typename OtherType>
158
+ inline Complex<Type> operator-(const Complex<OtherType>& other) const {
159
+ return Complex<Type>(this->r - other.r, this->i - other.i);
160
+ }
139
161
 
140
- template <typename OtherType>
141
- inline Complex<Type> operator*(const Complex<OtherType>& other) const {
142
- return Complex<Type>(this->r * other.r - this->i * other.i, this->r * other.i + this->i * other.r);
143
- }
162
+ template <typename OtherType>
163
+ inline Complex<Type> operator*(const Complex<OtherType>& other) const {
164
+ return Complex<Type>(this->r * other.r - this->i * other.i, this->r * other.i + this->i * other.r);
165
+ }
144
166
 
145
167
  template <typename OtherType>
146
168
  inline Complex<Type>& operator*=(const Complex<OtherType>& other) {
@@ -149,123 +171,125 @@ class Complex {
149
171
  return *this;
150
172
  }
151
173
 
152
- template <typename OtherType>
153
- inline Complex<Type> operator/(const Complex<OtherType>& other) const {
154
- Type new_r, new_i;
155
- Type denom = other.i * other.i + other.r * other.r;
156
-
157
- new_r = (this->r * other.r + this->i * other.i) / denom;
158
- new_i = (this->i * other.r - this->r * other.i) / denom;
159
-
160
- return Complex<Type>(new_r, new_i);
161
- }
162
-
163
- template <typename OtherType>
164
- inline Complex<Type> operator/=(const Complex<OtherType>& other) {
165
- Type new_r, new_i;
166
- Type denom = other.i * other.i + other.r * other.r;
167
-
168
- new_r = (this->r * other.r + this->i * other.i) / denom;
169
- new_i = (this->i * other.r - this->r * other.i) / denom;
170
-
171
- this->r = new_r;
172
- this->i = new_i;
173
- return *this;
174
- }
175
-
176
- template <typename OtherType>
177
- inline bool operator<(const Complex<OtherType>& other) const {
178
- return (this->r < other.r) || ((this->r <= other.r) && (this->i < other.i));
179
- }
180
-
181
- template <typename OtherType>
182
- inline bool operator>(const Complex<OtherType>& other) const {
183
- return (this->r > other.r) || ((this->r >= other.r) && (this->i > other.i));
184
- }
185
-
186
- template <typename OtherType>
187
- inline bool operator==(const Complex<OtherType>& other) const {
188
- return FP_EQUAL(this->r, other.r) && FP_EQUAL(this->i, other.i);
189
- }
190
-
191
- template <typename OtherType>
192
- inline bool operator!=(const Complex<OtherType>& other) const {
193
- return !(*this == other);
194
- }
195
-
196
- template <typename OtherType>
197
- inline bool operator<=(const Complex<OtherType>& other) const {
198
- return (*this < other) || (*this == other);
199
- }
200
-
201
- template <typename OtherType>
202
- inline bool operator>=(const Complex<OtherType>& other) const {
203
- return (*this > other) || (*this == other);
204
- }
205
-
206
- template <typename OtherType>
207
- inline operator Complex<OtherType> () const {
208
- return Complex<OtherType>((OtherType)this->r, (OtherType)this->i);
209
- }
210
-
211
- ///////////////////////////////
212
- // Complex-Native Operations //
213
- ///////////////////////////////
214
-
215
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
216
- inline Complex<Type> operator+(const NativeType& other) const {
217
- return *this + Complex<Type>(other);
218
- }
219
-
220
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
221
- inline Complex<Type> operator-(const NativeType& other) const {
222
- return *this - Complex<Type>(other);
223
- }
224
-
225
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
226
- inline Complex<Type> operator*(const NativeType& other) const {
227
- return *this * Complex<Type>(other);
228
- }
229
-
230
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
231
- inline Complex<Type> operator/(const NativeType& other) const {
232
- return *this / Complex<Type>(other);
233
- }
234
-
235
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
236
- inline bool operator<(const NativeType& other) const {
237
- return *this < Complex<Type>(other);
238
- }
239
-
240
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
241
- inline bool operator>(const NativeType& other) const {
242
- return *this > Complex<Type>(other);
243
- }
244
-
245
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
246
- inline bool operator==(const NativeType& other) const {
247
- return *this == Complex<Type>(other);
248
- }
249
-
250
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
251
- inline bool operator!=(const NativeType& other) const {
252
- return *this != Complex<Type>(other);
253
- }
254
-
255
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
256
- inline bool operator<=(const NativeType& other) const {
257
- return *this <= Complex<Type>(other);
258
- }
259
-
260
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
261
- inline bool operator>=(const NativeType& other) const {
262
- return *this >= Complex<Type>(other);
263
- }
264
-
265
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
266
- inline operator NativeType () const {
267
- return (NativeType)this->r;
268
- }
174
+ template <typename OtherType>
175
+ inline Complex<Type> operator/(const Complex<OtherType>& other) const {
176
+ Type new_r, new_i;
177
+ Type denom = other.i * other.i + other.r * other.r;
178
+
179
+ new_r = (this->r * other.r + this->i * other.i) / denom;
180
+ new_i = (this->i * other.r - this->r * other.i) / denom;
181
+
182
+ return Complex<Type>(new_r, new_i);
183
+ }
184
+
185
+ template <typename OtherType>
186
+ inline Complex<Type> operator/=(const Complex<OtherType>& other) {
187
+ Type new_r, new_i;
188
+ Type denom = other.i * other.i + other.r * other.r;
189
+
190
+ new_r = (this->r * other.r + this->i * other.i) / denom;
191
+ new_i = (this->i * other.r - this->r * other.i) / denom;
192
+
193
+ this->r = new_r;
194
+ this->i = new_i;
195
+ return *this;
196
+ }
197
+
198
+ template <typename OtherType>
199
+ inline bool operator<(const Complex<OtherType>& other) const {
200
+ return (this->r < other.r) || ((this->r <= other.r) && (this->i < other.i));
201
+ }
202
+
203
+ template <typename OtherType>
204
+ inline bool operator>(const Complex<OtherType>& other) const {
205
+ return (this->r > other.r) || ((this->r >= other.r) && (this->i > other.i));
206
+ }
207
+
208
+ template <typename OtherType>
209
+ inline bool operator==(const Complex<OtherType>& other) const {
210
+ return FP_EQUAL(this->r, other.r) && FP_EQUAL(this->i, other.i);
211
+ }
212
+
213
+ template <typename OtherType>
214
+ inline bool operator!=(const Complex<OtherType>& other) const {
215
+ return !(*this == other);
216
+ }
217
+
218
+ template <typename OtherType>
219
+ inline bool operator<=(const Complex<OtherType>& other) const {
220
+ return (*this < other) || (*this == other);
221
+ }
222
+
223
+ template <typename OtherType>
224
+ inline bool operator>=(const Complex<OtherType>& other) const {
225
+ return (*this > other) || (*this == other);
226
+ }
227
+
228
+ template <typename OtherType>
229
+ inline operator Complex<OtherType> () const {
230
+ return Complex<OtherType>((OtherType)this->r, (OtherType)this->i);
231
+ }
232
+
233
+ ///////////////////////////////
234
+ // Complex-Native Operations //
235
+ ///////////////////////////////
236
+
237
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
238
+ inline Complex<Type> operator+(const NativeType& other) const {
239
+ return *this + Complex<Type>(other);
240
+ }
241
+
242
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
243
+ inline Complex<Type> operator-(const NativeType& other) const {
244
+ return *this - Complex<Type>(other);
245
+ }
246
+
247
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
248
+ inline Complex<Type> operator*(const NativeType& other) const {
249
+ return *this * Complex<Type>(other);
250
+ }
251
+
252
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
253
+ inline Complex<Type> operator/(const NativeType& other) const {
254
+ return *this / Complex<Type>(other);
255
+ }
256
+
257
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
258
+ inline bool operator<(const NativeType& other) const {
259
+ return *this < Complex<Type>(other);
260
+ }
261
+
262
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
263
+ inline bool operator>(const NativeType& other) const {
264
+ return *this > Complex<Type>(other);
265
+ }
266
+
267
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
268
+ inline bool operator==(const NativeType& other) const {
269
+ return *this == Complex<Type>(other);
270
+ }
271
+
272
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
273
+ inline bool operator!=(const NativeType& other) const {
274
+ return *this != Complex<Type>(other);
275
+ }
276
+
277
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
278
+ inline bool operator<=(const NativeType& other) const {
279
+ return *this <= Complex<Type>(other);
280
+ }
281
+
282
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
283
+ inline bool operator>=(const NativeType& other) const {
284
+ return *this >= Complex<Type>(other);
285
+ }
286
+
287
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
288
+ inline operator NativeType () const {
289
+ return (NativeType)this->r;
290
+ }
291
+
292
+ operator RubyObject () const;
269
293
  };
270
294
 
271
295
  ///////////////////////////////
@@ -274,52 +298,52 @@ class Complex {
274
298
 
275
299
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
276
300
  inline Complex<ComplexType> operator+(const NativeType& left, const Complex<ComplexType>& right) {
277
- return Complex<ComplexType>(left) + right;
301
+ return Complex<ComplexType>(left) + right;
278
302
  }
279
303
 
280
304
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
281
305
  inline Complex<ComplexType> operator-(const NativeType& left, const Complex<ComplexType>& right) {
282
- return Complex<ComplexType>(left) - right;
306
+ return Complex<ComplexType>(left) - right;
283
307
  }
284
308
 
285
309
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
286
310
  inline Complex<ComplexType> operator*(const NativeType& left, const Complex<ComplexType>& right) {
287
- return Complex<ComplexType>(left) * right;
311
+ return Complex<ComplexType>(left) * right;
288
312
  }
289
313
 
290
314
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
291
315
  inline Complex<ComplexType> operator/(const NativeType& left, const Complex<ComplexType>& right) {
292
- return Complex<ComplexType>(left) / right;
316
+ return Complex<ComplexType>(left) / right;
293
317
  }
294
318
 
295
319
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
296
320
  inline bool operator<(const NativeType left, const Complex<ComplexType>& right) {
297
- return Complex<ComplexType>(left) < right;
321
+ return Complex<ComplexType>(left) < right;
298
322
  }
299
323
 
300
324
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
301
325
  inline bool operator>(const NativeType left, const Complex<ComplexType>& right) {
302
- return Complex<ComplexType>(left) > right;
326
+ return Complex<ComplexType>(left) > right;
303
327
  }
304
328
 
305
329
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
306
330
  inline bool operator==(const NativeType left, const Complex<ComplexType>& right) {
307
- return Complex<ComplexType>(left) == right;
331
+ return Complex<ComplexType>(left) == right;
308
332
  }
309
333
 
310
334
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
311
335
  inline bool operator!=(const NativeType left, const Complex<ComplexType>& right) {
312
- return Complex<ComplexType>(left) != right;
336
+ return Complex<ComplexType>(left) != right;
313
337
  }
314
338
 
315
339
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
316
340
  inline bool operator<=(const NativeType left, const Complex<ComplexType>& right) {
317
- return Complex<ComplexType>(left) <= right;
341
+ return Complex<ComplexType>(left) <= right;
318
342
  }
319
343
 
320
344
  template <typename NativeType, typename ComplexType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
321
345
  inline bool operator>=(const NativeType left, const Complex<ComplexType>& right) {
322
- return Complex<ComplexType>(left) >= right;
346
+ return Complex<ComplexType>(left) >= right;
323
347
  }
324
348
 
325
349
  template <typename Type>
@@ -91,35 +91,82 @@ namespace nm {
91
91
  "negate", "floor", "ceil", "round"
92
92
  };
93
93
 
94
+
95
+ /*
96
+ * Create a RubyObject from a regular C value (given a dtype). Does not return a VALUE! To get a VALUE, you need to
97
+ * look at the rval property of what this function returns.
98
+ */
99
+ nm::RubyObject rubyobj_from_cval(void* val, nm::dtype_t dtype) {
100
+ using namespace nm;
101
+ switch (dtype) {
102
+ case BYTE:
103
+ return RubyObject(*reinterpret_cast<uint8_t*>(val));
104
+
105
+ case INT8:
106
+ return RubyObject(*reinterpret_cast<int8_t*>(val));
107
+
108
+ case INT16:
109
+ return RubyObject(*reinterpret_cast<int16_t*>(val));
110
+
111
+ case INT32:
112
+ return RubyObject(*reinterpret_cast<int32_t*>(val));
113
+
114
+ case INT64:
115
+ return RubyObject(*reinterpret_cast<int64_t*>(val));
116
+
117
+ case FLOAT32:
118
+ return RubyObject(*reinterpret_cast<float32_t*>(val));
119
+
120
+ case FLOAT64:
121
+ return RubyObject(*reinterpret_cast<float64_t*>(val));
122
+
123
+ case COMPLEX64:
124
+ return RubyObject(*reinterpret_cast<Complex64*>(val));
125
+
126
+ case COMPLEX128:
127
+ return RubyObject(*reinterpret_cast<Complex128*>(val));
128
+
129
+ default:
130
+ try {
131
+ throw std::logic_error("Cannot create ruby object");
132
+ }
133
+ catch (std::logic_error err) {
134
+ printf("%s\n", err.what());
135
+ }
136
+
137
+ rb_raise(nm_eDataTypeError, "Conversion to RubyObject requested from unknown/invalid data type (did you try to convert from a VALUE?)");
138
+ }
139
+ return Qnil;
140
+ }
94
141
  } // end of namespace nm
95
142
 
96
143
  extern "C" {
97
144
 
98
145
  const char* const DTYPE_NAMES[nm::NUM_DTYPES] = {
99
- "byte",
100
- "int8",
101
- "int16",
102
- "int32",
103
- "int64",
104
- "float32",
105
- "float64",
106
- "complex64",
107
- "complex128",
108
- "object"
146
+ "byte",
147
+ "int8",
148
+ "int16",
149
+ "int32",
150
+ "int64",
151
+ "float32",
152
+ "float64",
153
+ "complex64",
154
+ "complex128",
155
+ "object"
109
156
  };
110
157
 
111
158
 
112
159
  const size_t DTYPE_SIZES[nm::NUM_DTYPES] = {
113
- sizeof(uint8_t),
114
- sizeof(int8_t),
115
- sizeof(int16_t),
116
- sizeof(int32_t),
117
- sizeof(int64_t),
118
- sizeof(float32_t),
119
- sizeof(float64_t),
120
- sizeof(nm::Complex64),
121
- sizeof(nm::Complex128),
122
- sizeof(nm::RubyObject)
160
+ sizeof(uint8_t),
161
+ sizeof(int8_t),
162
+ sizeof(int16_t),
163
+ sizeof(int32_t),
164
+ sizeof(int64_t),
165
+ sizeof(float32_t),
166
+ sizeof(float64_t),
167
+ sizeof(nm::Complex64),
168
+ sizeof(nm::Complex128),
169
+ sizeof(nm::RubyObject)
123
170
  };
124
171
 
125
172
 
@@ -150,100 +197,54 @@ const nm::dtype_t Upcast[nm::NUM_DTYPES][nm::NUM_DTYPES] = {
150
197
  */
151
198
  void rubyval_to_cval(VALUE val, nm::dtype_t dtype, void* loc) {
152
199
  using namespace nm;
153
- switch (dtype) {
154
- case BYTE:
155
- *reinterpret_cast<uint8_t*>(loc) = static_cast<uint8_t>(RubyObject(val));
156
- break;
157
-
158
- case INT8:
159
- *reinterpret_cast<int8_t*>(loc) = static_cast<int8_t>(RubyObject(val));
160
- break;
161
-
162
- case INT16:
163
- *reinterpret_cast<int16_t*>(loc) = static_cast<int16_t>(RubyObject(val));
164
- break;
165
-
166
- case INT32:
167
- *reinterpret_cast<int32_t*>(loc) = static_cast<int32_t>(RubyObject(val));
168
- break;
169
-
170
- case INT64:
171
- *reinterpret_cast<int64_t*>(loc) = static_cast<int64_t>(RubyObject(val));
172
- break;
173
-
174
- case FLOAT32:
175
- *reinterpret_cast<float32_t*>(loc) = static_cast<float32_t>(RubyObject(val));
176
- break;
177
-
178
- case FLOAT64:
179
- *reinterpret_cast<float64_t*>(loc) = static_cast<float64_t>(RubyObject(val));
180
- break;
181
-
182
- case COMPLEX64:
183
- *reinterpret_cast<Complex64*>(loc) = RubyObject(val).to<Complex64>();
184
- break;
185
-
186
- case COMPLEX128:
187
- *reinterpret_cast<Complex128*>(loc) = RubyObject(val).to<Complex128>();
188
- break;
189
-
190
- case RUBYOBJ:
191
- *reinterpret_cast<VALUE*>(loc) = val;
192
- //rb_raise(rb_eTypeError, "Attempting a bad conversion from a Ruby value.");
193
- break;
194
-
195
- default:
196
- rb_raise(rb_eTypeError, "Attempting a bad conversion.");
197
- break;
198
- }
200
+ switch (dtype) {
201
+ case BYTE:
202
+ *reinterpret_cast<uint8_t*>(loc) = static_cast<uint8_t>(RubyObject(val));
203
+ break;
204
+
205
+ case INT8:
206
+ *reinterpret_cast<int8_t*>(loc) = static_cast<int8_t>(RubyObject(val));
207
+ break;
208
+
209
+ case INT16:
210
+ *reinterpret_cast<int16_t*>(loc) = static_cast<int16_t>(RubyObject(val));
211
+ break;
212
+
213
+ case INT32:
214
+ *reinterpret_cast<int32_t*>(loc) = static_cast<int32_t>(RubyObject(val));
215
+ break;
216
+
217
+ case INT64:
218
+ *reinterpret_cast<int64_t*>(loc) = static_cast<int64_t>(RubyObject(val));
219
+ break;
220
+
221
+ case FLOAT32:
222
+ *reinterpret_cast<float32_t*>(loc) = static_cast<float32_t>(RubyObject(val));
223
+ break;
224
+
225
+ case FLOAT64:
226
+ *reinterpret_cast<float64_t*>(loc) = static_cast<float64_t>(RubyObject(val));
227
+ break;
228
+
229
+ case COMPLEX64:
230
+ *reinterpret_cast<Complex64*>(loc) = RubyObject(val).to<Complex64>();
231
+ break;
232
+
233
+ case COMPLEX128:
234
+ *reinterpret_cast<Complex128*>(loc) = RubyObject(val).to<Complex128>();
235
+ break;
236
+
237
+ case RUBYOBJ:
238
+ *reinterpret_cast<VALUE*>(loc) = val;
239
+ //rb_raise(rb_eTypeError, "Attempting a bad conversion from a Ruby value.");
240
+ break;
241
+
242
+ default:
243
+ rb_raise(rb_eTypeError, "Attempting a bad conversion.");
244
+ break;
245
+ }
199
246
  }
200
247
 
201
- /*
202
- * Create a RubyObject from a regular C value (given a dtype). Does not return a VALUE! To get a VALUE, you need to
203
- * look at the rval property of what this function returns.
204
- */
205
- nm::RubyObject rubyobj_from_cval(void* val, nm::dtype_t dtype) {
206
- using namespace nm;
207
- switch (dtype) {
208
- case BYTE:
209
- return RubyObject(*reinterpret_cast<uint8_t*>(val));
210
-
211
- case INT8:
212
- return RubyObject(*reinterpret_cast<int8_t*>(val));
213
-
214
- case INT16:
215
- return RubyObject(*reinterpret_cast<int16_t*>(val));
216
-
217
- case INT32:
218
- return RubyObject(*reinterpret_cast<int32_t*>(val));
219
-
220
- case INT64:
221
- return RubyObject(*reinterpret_cast<int64_t*>(val));
222
-
223
- case FLOAT32:
224
- return RubyObject(*reinterpret_cast<float32_t*>(val));
225
-
226
- case FLOAT64:
227
- return RubyObject(*reinterpret_cast<float64_t*>(val));
228
-
229
- case COMPLEX64:
230
- return RubyObject(*reinterpret_cast<Complex64*>(val));
231
-
232
- case COMPLEX128:
233
- return RubyObject(*reinterpret_cast<Complex128*>(val));
234
-
235
- default:
236
- try {
237
- throw std::logic_error("Cannot create ruby object");
238
- }
239
- catch (std::logic_error err) {
240
- printf("%s\n", err.what());
241
- }
242
-
243
- rb_raise(nm_eDataTypeError, "Conversion to RubyObject requested from unknown/invalid data type (did you try to convert from a VALUE?)");
244
- }
245
- return Qnil;
246
- }
247
248
 
248
249
 
249
250