nmatrix-atlas 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,8 +47,8 @@
47
47
  */
48
48
  #define NM_RUBYVAL_IS_NUMERIC(val) (FIXNUM_P(val) or (TYPE(val) == T_FLOAT) or (TYPE(val) == T_COMPLEX))
49
49
  #define NMATRIX_CHECK_TYPE(val) \
50
- if (TYPE(val) != T_DATA || (RDATA(val)->dfree != (RUBY_DATA_FUNC)nm_delete && RDATA(val)->dfree != (RUBY_DATA_FUNC)nm_delete_ref)) \
51
- rb_raise(rb_eTypeError, "Expected NMatrix on left-hand side of operation.");
50
+ if (TYPE(val) != T_DATA || (RDATA(val)->dfree != (RUBY_DATA_FUNC)nm_delete && RDATA(val)->dfree != (RUBY_DATA_FUNC)nm_delete_ref)) \
51
+ rb_raise(rb_eTypeError, "Expected NMatrix on left-hand side of operation.");
52
52
 
53
53
  /*
54
54
  * Classes and Functions
@@ -59,45 +59,45 @@ template<typename T, typename U>
59
59
  struct made_from_same_template : std::false_type {};
60
60
 
61
61
  template<template<typename> class Templ, typename Arg1, typename Arg2>
62
- struct made_from_same_template<Templ<Arg1>, Templ<Arg2>> : std::true_type {};
62
+ struct made_from_same_template<Templ<Arg1>, Templ<Arg2> > : std::true_type {};
63
63
 
64
64
  class RubyObject {
65
- public:
66
- VALUE rval;
67
-
68
- /*
69
- * Value constructor.
70
- */
71
- inline RubyObject(VALUE ref = Qnil) : rval(ref) {}
72
-
73
- /*
74
- * Complex number constructor.
75
- */
76
- template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
77
- inline RubyObject(const Complex<FloatType>& other) : rval(rb_complex_new(rb_float_new(other.r), rb_float_new(other.i))) {}
78
-
79
- /*
80
- * Integer constructor.
81
- *
82
- * Does not work as a template.
83
- */
84
- inline RubyObject(uint8_t other) : rval(INT2FIX(other)) {}
85
- inline RubyObject(int8_t other) : rval(INT2FIX(other)) {}
86
- inline RubyObject(int16_t other) : rval(INT2FIX(other)) {}
87
- inline RubyObject(uint16_t other) : rval(INT2FIX(other)) {}
88
- inline RubyObject(int32_t other) : rval(INT2FIX(other)) {}
89
- // there is no uint32_t here because that's a Ruby VALUE type, and we need the compiler to treat that as a VALUE.
90
- inline RubyObject(int64_t other) : rval(INT2FIX(other)) {}
91
- // inline RubyObject(uint64_t other) : rval(INT2FIX(other)) {}
92
-
93
-
94
- /*
95
- * Float constructor.
96
- *
97
- * Does not work as a template.
98
- */
99
- inline RubyObject(float other) : rval(rb_float_new(other)) {}
100
- inline RubyObject(double other) : rval(rb_float_new(other)) {}
65
+ public:
66
+ VALUE rval;
67
+
68
+ /*
69
+ * Value constructor.
70
+ */
71
+ inline RubyObject(VALUE ref = Qnil) : rval(ref) {}
72
+
73
+ /*
74
+ * Complex number constructor.
75
+ */
76
+ template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
77
+ inline RubyObject(const Complex<FloatType>& other) : rval(rb_complex_new(rb_float_new(other.r), rb_float_new(other.i))) {}
78
+
79
+ /*
80
+ * Integer constructor.
81
+ *
82
+ * Does not work as a template.
83
+ */
84
+ inline RubyObject(uint8_t other) : rval(INT2FIX(other)) {}
85
+ inline RubyObject(int8_t other) : rval(INT2FIX(other)) {}
86
+ inline RubyObject(int16_t other) : rval(INT2FIX(other)) {}
87
+ inline RubyObject(uint16_t other) : rval(INT2FIX(other)) {}
88
+ inline RubyObject(int32_t other) : rval(INT2FIX(other)) {}
89
+ // there is no uint32_t here because that's a Ruby VALUE type, and we need the compiler to treat that as a VALUE.
90
+ inline RubyObject(int64_t other) : rval(INT2FIX(other)) {}
91
+ // inline RubyObject(uint64_t other) : rval(INT2FIX(other)) {}
92
+
93
+
94
+ /*
95
+ * Float constructor.
96
+ *
97
+ * Does not work as a template.
98
+ */
99
+ inline RubyObject(float other) : rval(rb_float_new(other)) {}
100
+ inline RubyObject(double other) : rval(rb_float_new(other)) {}
101
101
 
102
102
  /*
103
103
  * Operators for converting RubyObjects to other C types.
@@ -120,178 +120,178 @@ class RubyObject {
120
120
  inline operator Complex64() const { return this->to<Complex64>(); }
121
121
  inline operator Complex128() const { return this->to<Complex128>(); }
122
122
  /*
123
- * Copy constructors.
124
- */
125
- inline RubyObject(const RubyObject& other) : rval(other.rval) {}
123
+ * Copy constructors.
124
+ */
125
+ inline RubyObject(const RubyObject& other) : rval(other.rval) {}
126
126
 
127
127
  /*
128
128
  * Inverse operator.
129
129
  */
130
- inline RubyObject inverse() const {
131
- rb_raise(rb_eNotImpError, "RubyObject#inverse needs to be implemented");
132
- }
133
-
134
- /*
135
- * Absolute value.
136
- */
137
- inline RubyObject abs() const {
138
- return RubyObject(rb_funcall(this->rval, rb_intern("abs"), 0));
139
- }
140
-
141
- /*
142
- * Binary operator definitions.
143
- */
144
-
145
- inline RubyObject operator+(const RubyObject& other) const {
146
- return RubyObject(rb_funcall(this->rval, nm_rb_add, 1, other.rval));
147
- }
148
-
149
- inline RubyObject& operator+=(const RubyObject& other) {
130
+ inline RubyObject inverse() const {
131
+ rb_raise(rb_eNotImpError, "RubyObject#inverse needs to be implemented");
132
+ }
133
+
134
+ /*
135
+ * Absolute value.
136
+ */
137
+ inline RubyObject abs() const {
138
+ return RubyObject(rb_funcall(this->rval, rb_intern("abs"), 0));
139
+ }
140
+
141
+ /*
142
+ * Binary operator definitions.
143
+ */
144
+
145
+ inline RubyObject operator+(const RubyObject& other) const {
146
+ return RubyObject(rb_funcall(this->rval, nm_rb_add, 1, other.rval));
147
+ }
148
+
149
+ inline RubyObject& operator+=(const RubyObject& other) {
150
150
  this->rval = rb_funcall(this->rval, nm_rb_add, 1, other.rval);
151
151
  return *this;
152
- }
152
+ }
153
153
 
154
- inline RubyObject operator-(const RubyObject& other) const {
155
- return RubyObject(rb_funcall(this->rval, nm_rb_sub, 1, other.rval));
156
- }
154
+ inline RubyObject operator-(const RubyObject& other) const {
155
+ return RubyObject(rb_funcall(this->rval, nm_rb_sub, 1, other.rval));
156
+ }
157
157
 
158
- inline RubyObject& operator-=(const RubyObject& other) {
158
+ inline RubyObject& operator-=(const RubyObject& other) {
159
159
  this->rval = rb_funcall(this->rval, nm_rb_sub, 1, other.rval);
160
160
  return *this;
161
- }
162
-
163
- inline RubyObject operator*(const RubyObject& other) const {
164
- return RubyObject(rb_funcall(this->rval, nm_rb_mul, 1, other.rval));
165
- }
161
+ }
162
+
163
+ inline RubyObject operator*(const RubyObject& other) const {
164
+ return RubyObject(rb_funcall(this->rval, nm_rb_mul, 1, other.rval));
165
+ }
166
166
 
167
- inline RubyObject& operator*=(const RubyObject& other) {
167
+ inline RubyObject& operator*=(const RubyObject& other) {
168
168
  this->rval = rb_funcall(this->rval, nm_rb_mul, 1, other.rval);
169
169
  return *this;
170
- }
171
-
172
- inline RubyObject operator/(const RubyObject& other) const {
173
- return RubyObject(rb_funcall(this->rval, nm_rb_div, 1, other.rval));
174
- }
170
+ }
171
+
172
+ inline RubyObject operator/(const RubyObject& other) const {
173
+ return RubyObject(rb_funcall(this->rval, nm_rb_div, 1, other.rval));
174
+ }
175
175
 
176
- inline RubyObject& operator/=(const RubyObject& other) {
176
+ inline RubyObject& operator/=(const RubyObject& other) {
177
177
  this->rval = rb_funcall(this->rval, nm_rb_div, 1, other.rval);
178
178
  return *this;
179
- }
180
-
181
- inline RubyObject operator%(const RubyObject& other) const {
182
- return RubyObject(rb_funcall(this->rval, nm_rb_percent, 1, other.rval));
183
- }
184
-
185
- inline bool operator>(const RubyObject& other) const {
186
- return rb_funcall(this->rval, nm_rb_gt, 1, other.rval) == Qtrue;
187
- }
188
-
189
- inline bool operator<(const RubyObject& other) const {
190
- return rb_funcall(this->rval, nm_rb_lt, 1, other.rval) == Qtrue;
191
- }
192
-
193
- template <typename OtherType>
194
- inline bool operator<(const OtherType& other) const {
195
- return *this < RubyObject(other);
196
- }
197
-
198
- inline bool operator==(const RubyObject& other) const {
199
- return rb_funcall(this->rval, nm_rb_eql, 1, other.rval) == Qtrue;
200
- }
201
-
202
- template <typename OtherType>
203
- inline bool operator==(const OtherType& other) const {
204
- return *this == RubyObject(other);
205
- }
206
-
207
- inline bool operator!=(const RubyObject& other) const {
208
- return rb_funcall(this->rval, nm_rb_neql, 1, other.rval) == Qtrue;
209
- }
210
-
211
- template <typename OtherType>
212
- inline bool operator!=(const OtherType& other) const {
213
- return *this != RubyObject(other);
214
- }
215
-
216
- inline bool operator>=(const RubyObject& other) const {
217
- return rb_funcall(this->rval, nm_rb_gte, 1, other.rval) == Qtrue;
218
- }
219
-
220
- template <typename OtherType>
221
- inline bool operator>=(const OtherType& other) const {
222
- return *this >= RubyObject(other);
223
- }
224
-
225
- inline bool operator<=(const RubyObject& other) const {
226
- return rb_funcall(this->rval, nm_rb_lte, 1, other.rval) == Qtrue;
227
- }
228
-
229
- template <typename OtherType>
230
- inline bool operator<=(const OtherType& other) const {
231
- return *this <= RubyObject(other);
232
- }
233
-
234
- ////////////////////////////
235
- // RUBY-NATIVE OPERATIONS //
236
- ////////////////////////////
179
+ }
180
+
181
+ inline RubyObject operator%(const RubyObject& other) const {
182
+ return RubyObject(rb_funcall(this->rval, nm_rb_percent, 1, other.rval));
183
+ }
184
+
185
+ inline bool operator>(const RubyObject& other) const {
186
+ return rb_funcall(this->rval, nm_rb_gt, 1, other.rval) == Qtrue;
187
+ }
188
+
189
+ inline bool operator<(const RubyObject& other) const {
190
+ return rb_funcall(this->rval, nm_rb_lt, 1, other.rval) == Qtrue;
191
+ }
192
+
193
+ template <typename OtherType>
194
+ inline bool operator<(const OtherType& other) const {
195
+ return *this < RubyObject(other);
196
+ }
197
+
198
+ inline bool operator==(const RubyObject& other) const {
199
+ return rb_funcall(this->rval, nm_rb_eql, 1, other.rval) == Qtrue;
200
+ }
201
+
202
+ template <typename OtherType>
203
+ inline bool operator==(const OtherType& other) const {
204
+ return *this == RubyObject(other);
205
+ }
206
+
207
+ inline bool operator!=(const RubyObject& other) const {
208
+ return rb_funcall(this->rval, nm_rb_neql, 1, other.rval) == Qtrue;
209
+ }
210
+
211
+ template <typename OtherType>
212
+ inline bool operator!=(const OtherType& other) const {
213
+ return *this != RubyObject(other);
214
+ }
215
+
216
+ inline bool operator>=(const RubyObject& other) const {
217
+ return rb_funcall(this->rval, nm_rb_gte, 1, other.rval) == Qtrue;
218
+ }
219
+
220
+ template <typename OtherType>
221
+ inline bool operator>=(const OtherType& other) const {
222
+ return *this >= RubyObject(other);
223
+ }
224
+
225
+ inline bool operator<=(const RubyObject& other) const {
226
+ return rb_funcall(this->rval, nm_rb_lte, 1, other.rval) == Qtrue;
227
+ }
228
+
229
+ template <typename OtherType>
230
+ inline bool operator<=(const OtherType& other) const {
231
+ return *this <= RubyObject(other);
232
+ }
233
+
234
+ ////////////////////////////
235
+ // RUBY-NATIVE OPERATIONS //
236
+ ////////////////////////////
237
237
  /*
238
- template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
239
- inline bool operator==(const NativeType& other) const {
240
- return *this == RubyObject(other);
241
- }
238
+ template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
239
+ inline bool operator==(const NativeType& other) const {
240
+ return *this == RubyObject(other);
241
+ }
242
242
 
243
243
  template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
244
- inline bool operator!=(const NativeType& other) const {
245
- return *this != RubyObject(other);
246
- }
244
+ inline bool operator!=(const NativeType& other) const {
245
+ return *this != RubyObject(other);
246
+ }
247
247
  */
248
- //////////////////////////////
249
- // RUBY-COMPLEX OPERATIONS //
250
- //////////////////////////////
248
+ //////////////////////////////
249
+ // RUBY-COMPLEX OPERATIONS //
250
+ //////////////////////////////
251
251
 
252
- template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
253
- inline bool operator==(const Complex<FloatType>& other) const {
254
- return *this == RubyObject(other);
255
- }
252
+ template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
253
+ inline bool operator==(const Complex<FloatType>& other) const {
254
+ return *this == RubyObject(other);
255
+ }
256
256
 
257
257
  template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
258
- inline bool operator!=(const Complex<FloatType>& other) const {
259
- return *this != RubyObject(other);
260
- }
261
-
262
- /*
263
- * Convert a Ruby object to an integer.
264
- */
265
- template <typename IntType>
266
- inline typename std::enable_if<std::is_integral<IntType>::value, IntType>::type to(void) {
267
- return NUM2INT(this->rval);
268
- }
269
-
270
- /*
271
- * Convert a Ruby object to a floating point number.
272
- */
273
- template <typename FloatType>
274
- inline typename std::enable_if<std::is_floating_point<FloatType>::value, FloatType>::type to(void) {
275
- return NUM2DBL(this->rval);
276
- }
277
-
278
- /*
279
- * Convert a Ruby object to a complex number.
280
- */
281
- template <typename ComplexType>
282
- inline typename std::enable_if<made_from_same_template<ComplexType, Complex64>::value, ComplexType>::type to(void) const {
283
- if (FIXNUM_P(this->rval) or TYPE(this->rval) == T_FLOAT) {
284
- return ComplexType(NUM2DBL(this->rval));
285
-
286
- } else if (TYPE(this->rval) == T_COMPLEX) {
287
- return ComplexType(NUM2DBL(rb_funcall(this->rval, nm_rb_real, 0)), NUM2DBL(rb_funcall(this->rval, nm_rb_imag, 0)));
288
-
289
- } else {
290
- rb_raise(rb_eTypeError, "Invalid conversion to Complex type.");
291
- }
292
- }
258
+ inline bool operator!=(const Complex<FloatType>& other) const {
259
+ return *this != RubyObject(other);
260
+ }
261
+
262
+ /*
263
+ * Convert a Ruby object to an integer.
264
+ */
265
+ template <typename IntType>
266
+ inline typename std::enable_if<std::is_integral<IntType>::value, IntType>::type to(void) {
267
+ return NUM2INT(this->rval);
268
+ }
269
+
270
+ /*
271
+ * Convert a Ruby object to a floating point number.
272
+ */
273
+ template <typename FloatType>
274
+ inline typename std::enable_if<std::is_floating_point<FloatType>::value, FloatType>::type to(void) {
275
+ return NUM2DBL(this->rval);
276
+ }
277
+
278
+ /*
279
+ * Convert a Ruby object to a complex number.
280
+ */
281
+ template <typename ComplexType>
282
+ inline typename std::enable_if<made_from_same_template<ComplexType, Complex64>::value, ComplexType>::type to(void) const {
283
+ if (FIXNUM_P(this->rval) or TYPE(this->rval) == T_FLOAT) {
284
+ return ComplexType(NUM2DBL(this->rval));
285
+
286
+ } else if (TYPE(this->rval) == T_COMPLEX) {
287
+ return ComplexType(NUM2DBL(rb_funcall(this->rval, nm_rb_real, 0)), NUM2DBL(rb_funcall(this->rval, nm_rb_imag, 0)));
288
+
289
+ } else {
290
+ rb_raise(rb_eTypeError, "Invalid conversion to Complex type.");
291
+ }
292
+ }
293
293
  };
294
-
294
+
295
295
  // Negative operator
296
296
  inline RubyObject operator-(const RubyObject& rhs) {
297
297
  return RubyObject(rb_funcall(rhs.rval, nm_rb_negate, 0));
@@ -344,32 +344,32 @@ inline bool operator>(const NativeType left, const RubyObject& right) {
344
344
 
345
345
  template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
346
346
  inline bool operator==(const Complex<FloatType>& left, const RubyObject& right) {
347
- return RubyObject(left) == right;
347
+ return RubyObject(left) == right;
348
348
  }
349
349
 
350
350
  template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
351
351
  inline bool operator!=(const Complex<FloatType>& left, const RubyObject& right) {
352
- return RubyObject(left) != right;
352
+ return RubyObject(left) != right;
353
353
  }
354
354
 
355
355
  template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
356
356
  inline bool operator<=(const Complex<FloatType>& left, const RubyObject& right) {
357
- return RubyObject(left) <= right;
357
+ return RubyObject(left) <= right;
358
358
  }
359
359
 
360
360
  template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
361
361
  inline bool operator>=(const Complex<FloatType>& left, const RubyObject& right) {
362
- return RubyObject(left) >= right;
362
+ return RubyObject(left) >= right;
363
363
  }
364
364
 
365
365
  template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
366
366
  inline bool operator<(const Complex<FloatType>& left, const RubyObject& right) {
367
- return RubyObject(left) < right;
367
+ return RubyObject(left) < right;
368
368
  }
369
369
 
370
370
  template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
371
371
  inline bool operator>(const Complex<FloatType>& left, const RubyObject& right) {
372
- return RubyObject(left) > right;
372
+ return RubyObject(left) > right;
373
373
  }
374
374
 
375
375
  } // end of namespace nm