nmatrix 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/nmatrix/data/complex.h +183 -159
- data/ext/nmatrix/data/data.cpp +113 -112
- data/ext/nmatrix/data/data.h +306 -292
- data/ext/nmatrix/data/ruby_object.h +193 -193
- data/ext/nmatrix/extconf.rb +11 -9
- data/ext/nmatrix/math.cpp +9 -11
- data/ext/nmatrix/math/math.h +3 -2
- data/ext/nmatrix/math/trsm.h +152 -152
- data/ext/nmatrix/nmatrix.h +30 -0
- data/ext/nmatrix/ruby_constants.cpp +67 -67
- data/ext/nmatrix/ruby_constants.h +35 -35
- data/ext/nmatrix/ruby_nmatrix.c +168 -183
- data/ext/nmatrix/storage/common.h +4 -3
- data/ext/nmatrix/storage/dense/dense.cpp +50 -50
- data/ext/nmatrix/storage/dense/dense.h +8 -7
- data/ext/nmatrix/storage/list/list.cpp +16 -16
- data/ext/nmatrix/storage/list/list.h +7 -6
- data/ext/nmatrix/storage/storage.cpp +32 -32
- data/ext/nmatrix/storage/storage.h +12 -11
- data/ext/nmatrix/storage/yale/class.h +2 -2
- data/ext/nmatrix/storage/yale/iterators/base.h +2 -1
- data/ext/nmatrix/storage/yale/iterators/iterator.h +2 -1
- data/ext/nmatrix/storage/yale/iterators/row.h +2 -1
- data/ext/nmatrix/storage/yale/iterators/row_stored.h +2 -1
- data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +1 -0
- data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +2 -1
- data/ext/nmatrix/storage/yale/yale.cpp +27 -27
- data/ext/nmatrix/storage/yale/yale.h +7 -6
- data/ext/nmatrix/ttable_helper.rb +10 -10
- data/ext/nmatrix/types.h +3 -2
- data/ext/nmatrix/util/io.cpp +7 -7
- data/ext/nmatrix/util/sl_list.cpp +26 -26
- data/ext/nmatrix/util/sl_list.h +19 -18
- data/lib/nmatrix/blas.rb +7 -7
- data/lib/nmatrix/io/mat5_reader.rb +30 -30
- data/lib/nmatrix/math.rb +73 -17
- data/lib/nmatrix/nmatrix.rb +10 -8
- data/lib/nmatrix/shortcuts.rb +3 -3
- data/lib/nmatrix/version.rb +3 -3
- data/spec/00_nmatrix_spec.rb +6 -0
- data/spec/math_spec.rb +77 -0
- data/spec/spec_helper.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 862e6785562317ff3ffe71dfa679ab58d3bfd219
|
4
|
+
data.tar.gz: 428075f3287f76f862bda8dbf6efa837272e6db9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d845e422a71670756115bb9a9c6eb6c448b89c08f471395a4ffec74b71bdb4052732df7e619aabc1f5a825b5e2c8f36cc215aaa4b1b20192c1575729a5698e2
|
7
|
+
data.tar.gz: 8737125b9d9778afa68ee7ac195014f286d29bea4fc7d33081a14610995e6df312fd2767a56207352534b718ae1c361a4c9ff2a5ad289043c091e4687be0fec9
|
data/ext/nmatrix/data/complex.h
CHANGED
@@ -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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
public:
|
72
|
+
// The real and immaginary parts of the complex number.
|
73
|
+
Type r;
|
74
|
+
Type i;
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
114
|
-
|
130
|
+
/*
|
131
|
+
* Binary operator definitions for various types.
|
132
|
+
*/
|
115
133
|
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
141
|
-
|
142
|
-
|
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
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
346
|
+
return Complex<ComplexType>(left) >= right;
|
323
347
|
}
|
324
348
|
|
325
349
|
template <typename Type>
|
data/ext/nmatrix/data/data.cpp
CHANGED
@@ -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
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
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
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
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
|
|