pnmatrix 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/nmatrix/binary_format.txt +53 -0
- data/ext/nmatrix/data/complex.h +388 -0
- data/ext/nmatrix/data/data.cpp +274 -0
- data/ext/nmatrix/data/data.h +651 -0
- data/ext/nmatrix/data/meta.h +64 -0
- data/ext/nmatrix/data/ruby_object.h +386 -0
- data/ext/nmatrix/extconf.rb +70 -0
- data/ext/nmatrix/math/asum.h +99 -0
- data/ext/nmatrix/math/cblas_enums.h +36 -0
- data/ext/nmatrix/math/cblas_templates_core.h +507 -0
- data/ext/nmatrix/math/gemm.h +241 -0
- data/ext/nmatrix/math/gemv.h +178 -0
- data/ext/nmatrix/math/getrf.h +255 -0
- data/ext/nmatrix/math/getrs.h +121 -0
- data/ext/nmatrix/math/imax.h +82 -0
- data/ext/nmatrix/math/laswp.h +165 -0
- data/ext/nmatrix/math/long_dtype.h +62 -0
- data/ext/nmatrix/math/magnitude.h +54 -0
- data/ext/nmatrix/math/math.h +751 -0
- data/ext/nmatrix/math/nrm2.h +165 -0
- data/ext/nmatrix/math/rot.h +117 -0
- data/ext/nmatrix/math/rotg.h +106 -0
- data/ext/nmatrix/math/scal.h +71 -0
- data/ext/nmatrix/math/trsm.h +336 -0
- data/ext/nmatrix/math/util.h +162 -0
- data/ext/nmatrix/math.cpp +1368 -0
- data/ext/nmatrix/nm_memory.h +60 -0
- data/ext/nmatrix/nmatrix.cpp +285 -0
- data/ext/nmatrix/nmatrix.h +476 -0
- data/ext/nmatrix/ruby_constants.cpp +151 -0
- data/ext/nmatrix/ruby_constants.h +106 -0
- data/ext/nmatrix/ruby_nmatrix.c +3130 -0
- data/ext/nmatrix/storage/common.cpp +77 -0
- data/ext/nmatrix/storage/common.h +183 -0
- data/ext/nmatrix/storage/dense/dense.cpp +1096 -0
- data/ext/nmatrix/storage/dense/dense.h +129 -0
- data/ext/nmatrix/storage/list/list.cpp +1628 -0
- data/ext/nmatrix/storage/list/list.h +138 -0
- data/ext/nmatrix/storage/storage.cpp +730 -0
- data/ext/nmatrix/storage/storage.h +99 -0
- data/ext/nmatrix/storage/yale/class.h +1139 -0
- data/ext/nmatrix/storage/yale/iterators/base.h +143 -0
- data/ext/nmatrix/storage/yale/iterators/iterator.h +131 -0
- data/ext/nmatrix/storage/yale/iterators/row.h +450 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored.h +140 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +169 -0
- data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +124 -0
- data/ext/nmatrix/storage/yale/math/transpose.h +110 -0
- data/ext/nmatrix/storage/yale/yale.cpp +2074 -0
- data/ext/nmatrix/storage/yale/yale.h +203 -0
- data/ext/nmatrix/types.h +55 -0
- data/ext/nmatrix/util/io.cpp +279 -0
- data/ext/nmatrix/util/io.h +115 -0
- data/ext/nmatrix/util/sl_list.cpp +627 -0
- data/ext/nmatrix/util/sl_list.h +144 -0
- data/ext/nmatrix/util/util.h +78 -0
- data/lib/nmatrix/blas.rb +378 -0
- data/lib/nmatrix/cruby/math.rb +744 -0
- data/lib/nmatrix/enumerate.rb +253 -0
- data/lib/nmatrix/homogeneous.rb +241 -0
- data/lib/nmatrix/io/fortran_format.rb +138 -0
- data/lib/nmatrix/io/harwell_boeing.rb +221 -0
- data/lib/nmatrix/io/market.rb +263 -0
- data/lib/nmatrix/io/point_cloud.rb +189 -0
- data/lib/nmatrix/jruby/decomposition.rb +24 -0
- data/lib/nmatrix/jruby/enumerable.rb +13 -0
- data/lib/nmatrix/jruby/error.rb +4 -0
- data/lib/nmatrix/jruby/math.rb +501 -0
- data/lib/nmatrix/jruby/nmatrix_java.rb +840 -0
- data/lib/nmatrix/jruby/operators.rb +283 -0
- data/lib/nmatrix/jruby/slice.rb +264 -0
- data/lib/nmatrix/lapack_core.rb +181 -0
- data/lib/nmatrix/lapack_plugin.rb +44 -0
- data/lib/nmatrix/math.rb +953 -0
- data/lib/nmatrix/mkmf.rb +100 -0
- data/lib/nmatrix/monkeys.rb +137 -0
- data/lib/nmatrix/nmatrix.rb +1172 -0
- data/lib/nmatrix/rspec.rb +75 -0
- data/lib/nmatrix/shortcuts.rb +1163 -0
- data/lib/nmatrix/version.rb +39 -0
- data/lib/nmatrix/yale_functions.rb +118 -0
- data/lib/nmatrix.rb +28 -0
- data/spec/00_nmatrix_spec.rb +892 -0
- data/spec/01_enum_spec.rb +196 -0
- data/spec/02_slice_spec.rb +407 -0
- data/spec/03_nmatrix_monkeys_spec.rb +80 -0
- data/spec/2x2_dense_double.mat +0 -0
- data/spec/4x4_sparse.mat +0 -0
- data/spec/4x5_dense.mat +0 -0
- data/spec/blas_spec.rb +215 -0
- data/spec/elementwise_spec.rb +311 -0
- data/spec/homogeneous_spec.rb +100 -0
- data/spec/io/fortran_format_spec.rb +88 -0
- data/spec/io/harwell_boeing_spec.rb +98 -0
- data/spec/io/test.rua +9 -0
- data/spec/io_spec.rb +159 -0
- data/spec/lapack_core_spec.rb +482 -0
- data/spec/leakcheck.rb +16 -0
- data/spec/math_spec.rb +1363 -0
- data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
- data/spec/nmatrix_yale_spec.rb +286 -0
- data/spec/rspec_monkeys.rb +56 -0
- data/spec/rspec_spec.rb +35 -0
- data/spec/shortcuts_spec.rb +474 -0
- data/spec/slice_set_spec.rb +162 -0
- data/spec/spec_helper.rb +172 -0
- data/spec/stat_spec.rb +214 -0
- data/spec/test.pcd +20 -0
- data/spec/utm5940.mtx +83844 -0
- metadata +295 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == meta.h
|
25
|
+
//
|
26
|
+
// Header file for dealing with template metaprogramming.
|
27
|
+
|
28
|
+
#ifndef META_H
|
29
|
+
# define META_H
|
30
|
+
|
31
|
+
namespace nm {
|
32
|
+
/*
|
33
|
+
* Template Metaprogramming
|
34
|
+
*/
|
35
|
+
template <typename T> struct ctype_to_dtype_enum {
|
36
|
+
static const nm::dtype_t value_type = nm::BYTE;
|
37
|
+
};
|
38
|
+
template <> struct ctype_to_dtype_enum<uint8_t> { static const nm::dtype_t value_type = nm::BYTE; };
|
39
|
+
template <> struct ctype_to_dtype_enum<int8_t> { static const nm::dtype_t value_type = nm::INT8; };
|
40
|
+
template <> struct ctype_to_dtype_enum<int16_t> { static const nm::dtype_t value_type = nm::INT16; };
|
41
|
+
template <> struct ctype_to_dtype_enum<int32_t> { static const nm::dtype_t value_type = nm::INT32; };
|
42
|
+
template <> struct ctype_to_dtype_enum<int64_t> { static const nm::dtype_t value_type = nm::INT64; };
|
43
|
+
template <> struct ctype_to_dtype_enum<float> { static const nm::dtype_t value_type = nm::FLOAT32; };
|
44
|
+
template <> struct ctype_to_dtype_enum<double> { static const nm::dtype_t value_type = nm::FLOAT64; };
|
45
|
+
template <> struct ctype_to_dtype_enum<Complex64> { static const nm::dtype_t value_type = nm::COMPLEX64; };
|
46
|
+
template <> struct ctype_to_dtype_enum<Complex128> { static const nm::dtype_t value_type = nm::COMPLEX128; };
|
47
|
+
template <> struct ctype_to_dtype_enum<RubyObject> { static const nm::dtype_t value_type = nm::RUBYOBJ; };
|
48
|
+
|
49
|
+
|
50
|
+
template <nm::dtype_t Enum> struct dtype_enum_T;
|
51
|
+
template <> struct dtype_enum_T<nm::BYTE> { typedef uint8_t type; };
|
52
|
+
template <> struct dtype_enum_T<nm::INT8> { typedef int8_t type; };
|
53
|
+
template <> struct dtype_enum_T<nm::INT16> { typedef int16_t type; };
|
54
|
+
template <> struct dtype_enum_T<nm::INT32> { typedef int32_t type; };
|
55
|
+
template <> struct dtype_enum_T<nm::INT64> { typedef int64_t type; };
|
56
|
+
template <> struct dtype_enum_T<nm::FLOAT32> { typedef float type; };
|
57
|
+
template <> struct dtype_enum_T<nm::FLOAT64> { typedef double type; };
|
58
|
+
template <> struct dtype_enum_T<nm::COMPLEX64> { typedef nm::Complex64 type; };
|
59
|
+
template <> struct dtype_enum_T<nm::COMPLEX128> { typedef nm::Complex128 type; };
|
60
|
+
template <> struct dtype_enum_T<nm::RUBYOBJ> { typedef nm::RubyObject type; };
|
61
|
+
|
62
|
+
} // end namespace nm
|
63
|
+
|
64
|
+
#endif
|
@@ -0,0 +1,386 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == ruby_object.h
|
25
|
+
//
|
26
|
+
// Functions and classes for dealing with Ruby objects.
|
27
|
+
|
28
|
+
#ifndef RUBY_OBJECT_H
|
29
|
+
#define RUBY_OBJECT_H
|
30
|
+
|
31
|
+
/*
|
32
|
+
* Standard Includes
|
33
|
+
*/
|
34
|
+
|
35
|
+
#include <ruby.h>
|
36
|
+
#include <iostream>
|
37
|
+
#include <type_traits>
|
38
|
+
|
39
|
+
/*
|
40
|
+
* Project Includes
|
41
|
+
*/
|
42
|
+
|
43
|
+
#include "ruby_constants.h"
|
44
|
+
|
45
|
+
/*
|
46
|
+
* Macros
|
47
|
+
*/
|
48
|
+
#define NM_RUBYVAL_IS_NUMERIC(val) (FIXNUM_P(val) or RB_FLOAT_TYPE_P(val) or RB_TYPE_P(val, T_COMPLEX))
|
49
|
+
|
50
|
+
/*
|
51
|
+
* Classes and Functions
|
52
|
+
*/
|
53
|
+
|
54
|
+
namespace nm {
|
55
|
+
template<typename T, typename U>
|
56
|
+
struct made_from_same_template : std::false_type {};
|
57
|
+
|
58
|
+
template<template<typename> class Templ, typename Arg1, typename Arg2>
|
59
|
+
struct made_from_same_template<Templ<Arg1>, Templ<Arg2> > : std::true_type {};
|
60
|
+
|
61
|
+
class RubyObject {
|
62
|
+
public:
|
63
|
+
VALUE rval;
|
64
|
+
|
65
|
+
/*
|
66
|
+
* Value constructor.
|
67
|
+
*/
|
68
|
+
inline RubyObject(VALUE ref = Qnil) : rval(ref) {}
|
69
|
+
|
70
|
+
/*
|
71
|
+
* Complex number constructor.
|
72
|
+
*/
|
73
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
74
|
+
inline RubyObject(const Complex<FloatType>& other) : rval(rb_complex_new(rb_float_new(other.r), rb_float_new(other.i))) {}
|
75
|
+
|
76
|
+
/*
|
77
|
+
* Integer constructor.
|
78
|
+
*
|
79
|
+
* Does not work as a template.
|
80
|
+
*/
|
81
|
+
inline RubyObject(uint8_t other) : rval(INT2FIX(other)) {}
|
82
|
+
inline RubyObject(int8_t other) : rval(INT2FIX(other)) {}
|
83
|
+
inline RubyObject(int16_t other) : rval(INT2FIX(other)) {}
|
84
|
+
inline RubyObject(uint16_t other) : rval(INT2FIX(other)) {}
|
85
|
+
inline RubyObject(int32_t other) : rval(INT2FIX(other)) {}
|
86
|
+
// there is no uint32_t here because that's a Ruby VALUE type, and we need the compiler to treat that as a VALUE.
|
87
|
+
inline RubyObject(int64_t other) : rval(INT2FIX(other)) {}
|
88
|
+
// inline RubyObject(uint64_t other) : rval(INT2FIX(other)) {}
|
89
|
+
|
90
|
+
|
91
|
+
/*
|
92
|
+
* Float constructor.
|
93
|
+
*
|
94
|
+
* Does not work as a template.
|
95
|
+
*/
|
96
|
+
inline RubyObject(float other) : rval(rb_float_new(other)) {}
|
97
|
+
inline RubyObject(double other) : rval(rb_float_new(other)) {}
|
98
|
+
|
99
|
+
/*
|
100
|
+
* Operators for converting RubyObjects to other C types.
|
101
|
+
*/
|
102
|
+
|
103
|
+
#define RETURN_OBJ2NUM(mac) if (this->rval == Qtrue) return 1; else if (this->rval == Qfalse) return 0; else return mac(this->rval);
|
104
|
+
|
105
|
+
inline operator int8_t() const { RETURN_OBJ2NUM(NUM2INT) }
|
106
|
+
inline operator uint8_t() const { RETURN_OBJ2NUM(NUM2UINT) }
|
107
|
+
inline operator int16_t() const { RETURN_OBJ2NUM(NUM2INT) }
|
108
|
+
inline operator uint16_t() const { RETURN_OBJ2NUM(NUM2UINT) }
|
109
|
+
inline operator int32_t() const { RETURN_OBJ2NUM(NUM2LONG) }
|
110
|
+
inline operator VALUE() const { return rval; }
|
111
|
+
//inline operator uint32_t() const { return NUM2ULONG(this->rval); }
|
112
|
+
inline operator int64_t() const { RETURN_OBJ2NUM(NUM2LONG) }
|
113
|
+
//inline operator uint64_t() const { RETURN_OBJ2NUM(NUM2ULONG) }
|
114
|
+
inline operator double() const { RETURN_OBJ2NUM(NUM2DBL) }
|
115
|
+
inline operator float() const { RETURN_OBJ2NUM(NUM2DBL) }
|
116
|
+
|
117
|
+
inline operator Complex64() const { return this->to<Complex64>(); }
|
118
|
+
inline operator Complex128() const { return this->to<Complex128>(); }
|
119
|
+
/*
|
120
|
+
* Copy constructors.
|
121
|
+
*/
|
122
|
+
inline RubyObject(const RubyObject& other) : rval(other.rval) {}
|
123
|
+
|
124
|
+
/*
|
125
|
+
* Inverse operator.
|
126
|
+
*/
|
127
|
+
inline RubyObject inverse() const {
|
128
|
+
rb_raise(rb_eNotImpError, "RubyObject#inverse needs to be implemented");
|
129
|
+
}
|
130
|
+
|
131
|
+
/*
|
132
|
+
* Absolute value.
|
133
|
+
*/
|
134
|
+
inline RubyObject abs() const {
|
135
|
+
return RubyObject(rb_funcall(this->rval, rb_intern("abs"), 0));
|
136
|
+
}
|
137
|
+
|
138
|
+
/*
|
139
|
+
* Binary operator definitions.
|
140
|
+
*/
|
141
|
+
|
142
|
+
inline RubyObject operator+(const RubyObject& other) const {
|
143
|
+
return RubyObject(rb_funcall(this->rval, nm_rb_add, 1, other.rval));
|
144
|
+
}
|
145
|
+
|
146
|
+
inline RubyObject& operator+=(const RubyObject& other) {
|
147
|
+
this->rval = rb_funcall(this->rval, nm_rb_add, 1, other.rval);
|
148
|
+
return *this;
|
149
|
+
}
|
150
|
+
|
151
|
+
inline RubyObject operator-(const RubyObject& other) const {
|
152
|
+
return RubyObject(rb_funcall(this->rval, nm_rb_sub, 1, other.rval));
|
153
|
+
}
|
154
|
+
|
155
|
+
inline RubyObject& operator-=(const RubyObject& other) {
|
156
|
+
this->rval = rb_funcall(this->rval, nm_rb_sub, 1, other.rval);
|
157
|
+
return *this;
|
158
|
+
}
|
159
|
+
|
160
|
+
inline RubyObject operator*(const RubyObject& other) const {
|
161
|
+
return RubyObject(rb_funcall(this->rval, nm_rb_mul, 1, other.rval));
|
162
|
+
}
|
163
|
+
|
164
|
+
inline RubyObject& operator*=(const RubyObject& other) {
|
165
|
+
this->rval = rb_funcall(this->rval, nm_rb_mul, 1, other.rval);
|
166
|
+
return *this;
|
167
|
+
}
|
168
|
+
|
169
|
+
inline RubyObject operator/(const RubyObject& other) const {
|
170
|
+
return RubyObject(rb_funcall(this->rval, nm_rb_div, 1, other.rval));
|
171
|
+
}
|
172
|
+
|
173
|
+
inline RubyObject& operator/=(const RubyObject& other) {
|
174
|
+
this->rval = rb_funcall(this->rval, nm_rb_div, 1, other.rval);
|
175
|
+
return *this;
|
176
|
+
}
|
177
|
+
|
178
|
+
inline RubyObject operator%(const RubyObject& other) const {
|
179
|
+
return RubyObject(rb_funcall(this->rval, nm_rb_percent, 1, other.rval));
|
180
|
+
}
|
181
|
+
|
182
|
+
inline bool operator>(const RubyObject& other) const {
|
183
|
+
return rb_funcall(this->rval, nm_rb_gt, 1, other.rval) == Qtrue;
|
184
|
+
}
|
185
|
+
|
186
|
+
inline bool operator<(const RubyObject& other) const {
|
187
|
+
return rb_funcall(this->rval, nm_rb_lt, 1, other.rval) == Qtrue;
|
188
|
+
}
|
189
|
+
|
190
|
+
template <typename OtherType>
|
191
|
+
inline bool operator<(const OtherType& other) const {
|
192
|
+
return *this < RubyObject(other);
|
193
|
+
}
|
194
|
+
|
195
|
+
inline bool operator==(const RubyObject& other) const {
|
196
|
+
return rb_funcall(this->rval, nm_rb_eql, 1, other.rval) == Qtrue;
|
197
|
+
}
|
198
|
+
|
199
|
+
template <typename OtherType>
|
200
|
+
inline bool operator==(const OtherType& other) const {
|
201
|
+
return *this == RubyObject(other);
|
202
|
+
}
|
203
|
+
|
204
|
+
inline bool operator!=(const RubyObject& other) const {
|
205
|
+
return rb_funcall(this->rval, nm_rb_neql, 1, other.rval) == Qtrue;
|
206
|
+
}
|
207
|
+
|
208
|
+
template <typename OtherType>
|
209
|
+
inline bool operator!=(const OtherType& other) const {
|
210
|
+
return *this != RubyObject(other);
|
211
|
+
}
|
212
|
+
|
213
|
+
inline bool operator>=(const RubyObject& other) const {
|
214
|
+
return rb_funcall(this->rval, nm_rb_gte, 1, other.rval) == Qtrue;
|
215
|
+
}
|
216
|
+
|
217
|
+
template <typename OtherType>
|
218
|
+
inline bool operator>=(const OtherType& other) const {
|
219
|
+
return *this >= RubyObject(other);
|
220
|
+
}
|
221
|
+
|
222
|
+
inline bool operator<=(const RubyObject& other) const {
|
223
|
+
return rb_funcall(this->rval, nm_rb_lte, 1, other.rval) == Qtrue;
|
224
|
+
}
|
225
|
+
|
226
|
+
template <typename OtherType>
|
227
|
+
inline bool operator<=(const OtherType& other) const {
|
228
|
+
return *this <= RubyObject(other);
|
229
|
+
}
|
230
|
+
|
231
|
+
////////////////////////////
|
232
|
+
// RUBY-NATIVE OPERATIONS //
|
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 == RubyObject(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 != RubyObject(other);
|
243
|
+
}
|
244
|
+
*/
|
245
|
+
//////////////////////////////
|
246
|
+
// RUBY-COMPLEX OPERATIONS //
|
247
|
+
//////////////////////////////
|
248
|
+
|
249
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
250
|
+
inline bool operator==(const Complex<FloatType>& other) const {
|
251
|
+
return *this == RubyObject(other);
|
252
|
+
}
|
253
|
+
|
254
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
255
|
+
inline bool operator!=(const Complex<FloatType>& other) const {
|
256
|
+
return *this != RubyObject(other);
|
257
|
+
}
|
258
|
+
|
259
|
+
/*
|
260
|
+
* Convert a Ruby object to an integer.
|
261
|
+
*/
|
262
|
+
template <typename IntType>
|
263
|
+
inline typename std::enable_if<std::is_integral<IntType>::value, IntType>::type to(void) {
|
264
|
+
return NUM2INT(this->rval);
|
265
|
+
}
|
266
|
+
|
267
|
+
/*
|
268
|
+
* Convert a Ruby object to a floating point number.
|
269
|
+
*/
|
270
|
+
template <typename FloatType>
|
271
|
+
inline typename std::enable_if<std::is_floating_point<FloatType>::value, FloatType>::type to(void) {
|
272
|
+
return NUM2DBL(this->rval);
|
273
|
+
}
|
274
|
+
|
275
|
+
/*
|
276
|
+
* Convert a Ruby object to a complex number.
|
277
|
+
*/
|
278
|
+
template <typename ComplexType>
|
279
|
+
inline typename std::enable_if<made_from_same_template<ComplexType, Complex64>::value, ComplexType>::type to(void) const {
|
280
|
+
if (FIXNUM_P(this->rval) or TYPE(this->rval) == T_FLOAT) {
|
281
|
+
return ComplexType(NUM2DBL(this->rval));
|
282
|
+
|
283
|
+
} else if (TYPE(this->rval) == T_COMPLEX) {
|
284
|
+
return ComplexType(NUM2DBL(rb_funcall(this->rval, nm_rb_real, 0)), NUM2DBL(rb_funcall(this->rval, nm_rb_imag, 0)));
|
285
|
+
|
286
|
+
} else {
|
287
|
+
rb_raise(rb_eTypeError, "Invalid conversion to Complex type.");
|
288
|
+
}
|
289
|
+
}
|
290
|
+
};
|
291
|
+
|
292
|
+
// Negative operator
|
293
|
+
inline RubyObject operator-(const RubyObject& rhs) {
|
294
|
+
return RubyObject(rb_funcall(rhs.rval, nm_rb_negate, 0));
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
////////////////////////////
|
299
|
+
// NATIVE-RUBY OPERATIONS //
|
300
|
+
////////////////////////////
|
301
|
+
|
302
|
+
template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
|
303
|
+
inline RubyObject operator/(const NativeType left, const RubyObject& right) {
|
304
|
+
return RubyObject(left) / right;
|
305
|
+
}
|
306
|
+
|
307
|
+
template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
|
308
|
+
inline bool operator==(const NativeType left, const RubyObject& right) {
|
309
|
+
return RubyObject(left) == right;
|
310
|
+
}
|
311
|
+
|
312
|
+
template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
|
313
|
+
inline bool operator!=(const NativeType left, const RubyObject& right) {
|
314
|
+
return RubyObject(left) != right;
|
315
|
+
}
|
316
|
+
|
317
|
+
template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
|
318
|
+
inline bool operator<=(const NativeType left, const RubyObject& right) {
|
319
|
+
return RubyObject(left) <= right;
|
320
|
+
}
|
321
|
+
|
322
|
+
template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
|
323
|
+
inline bool operator>=(const NativeType left, const RubyObject& right) {
|
324
|
+
return RubyObject(left) >= right;
|
325
|
+
}
|
326
|
+
|
327
|
+
template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
|
328
|
+
inline bool operator<(const NativeType left, const RubyObject& right) {
|
329
|
+
return RubyObject(left) < right;
|
330
|
+
}
|
331
|
+
|
332
|
+
template <typename NativeType, typename = typename std::enable_if<std::is_arithmetic<NativeType>::value>::type>
|
333
|
+
inline bool operator>(const NativeType left, const RubyObject& right) {
|
334
|
+
return RubyObject(left) > right;
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
/////////////////////////////
|
339
|
+
// COMPLEX-RUBY OPERATIONS //
|
340
|
+
/////////////////////////////
|
341
|
+
|
342
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
343
|
+
inline bool operator==(const Complex<FloatType>& left, const RubyObject& right) {
|
344
|
+
return RubyObject(left) == right;
|
345
|
+
}
|
346
|
+
|
347
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
348
|
+
inline bool operator!=(const Complex<FloatType>& left, const RubyObject& right) {
|
349
|
+
return RubyObject(left) != right;
|
350
|
+
}
|
351
|
+
|
352
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
353
|
+
inline bool operator<=(const Complex<FloatType>& left, const RubyObject& right) {
|
354
|
+
return RubyObject(left) <= right;
|
355
|
+
}
|
356
|
+
|
357
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
358
|
+
inline bool operator>=(const Complex<FloatType>& left, const RubyObject& right) {
|
359
|
+
return RubyObject(left) >= right;
|
360
|
+
}
|
361
|
+
|
362
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
363
|
+
inline bool operator<(const Complex<FloatType>& left, const RubyObject& right) {
|
364
|
+
return RubyObject(left) < right;
|
365
|
+
}
|
366
|
+
|
367
|
+
template <typename FloatType, typename = typename std::enable_if<std::is_floating_point<FloatType>::value>::type>
|
368
|
+
inline bool operator>(const Complex<FloatType>& left, const RubyObject& right) {
|
369
|
+
return RubyObject(left) > right;
|
370
|
+
}
|
371
|
+
|
372
|
+
} // end of namespace nm
|
373
|
+
|
374
|
+
namespace std {
|
375
|
+
inline nm::RubyObject abs(const nm::RubyObject& obj) {
|
376
|
+
return obj.abs();
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
inline nm::RubyObject sqrt(const nm::RubyObject& obj) {
|
381
|
+
VALUE cMath = rb_const_get(rb_cObject, rb_intern("Math"));
|
382
|
+
return nm::RubyObject(rb_funcall(cMath, rb_intern("sqrt"), 1, obj.rval));
|
383
|
+
}
|
384
|
+
}
|
385
|
+
|
386
|
+
#endif // RUBY_OBJECT_H
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# = NMatrix
|
2
|
+
#
|
3
|
+
# A linear algebra library for scientific computation in Ruby.
|
4
|
+
# NMatrix is part of SciRuby.
|
5
|
+
#
|
6
|
+
# NMatrix was originally inspired by and derived from NArray, by
|
7
|
+
# Masahiro Tanaka: http://narray.rubyforge.org
|
8
|
+
#
|
9
|
+
# == Copyright Information
|
10
|
+
#
|
11
|
+
# SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
13
|
+
#
|
14
|
+
# Please see LICENSE.txt for additional copyright notices.
|
15
|
+
#
|
16
|
+
# == Contributing
|
17
|
+
#
|
18
|
+
# By contributing source code to SciRuby, you agree to be bound by
|
19
|
+
# our Contributor Agreement:
|
20
|
+
#
|
21
|
+
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
|
+
#
|
23
|
+
# == extconf.rb
|
24
|
+
#
|
25
|
+
# This file checks for ATLAS and other necessary headers, and
|
26
|
+
# generates a Makefile for compiling NMatrix.
|
27
|
+
|
28
|
+
require File.expand_path("../../../lib/nmatrix/mkmf", __FILE__)
|
29
|
+
|
30
|
+
$INSTALLFILES = [
|
31
|
+
['nmatrix.h' , '$(archdir)'],
|
32
|
+
['nmatrix.hpp' , '$(archdir)'],
|
33
|
+
['nmatrix_config.h', '$(archdir)'],
|
34
|
+
['nm_memory.h' , '$(archdir)'],
|
35
|
+
['ruby_constants.h', '$(archdir)']
|
36
|
+
]
|
37
|
+
|
38
|
+
if /cygwin|mingw/ =~ RUBY_PLATFORM
|
39
|
+
$INSTALLFILES << ['libnmatrix.a', '$(archdir)']
|
40
|
+
end
|
41
|
+
|
42
|
+
$DEBUG = true
|
43
|
+
$CFLAGS = ["-Wall -Werror=return-type",$CFLAGS].join(" ")
|
44
|
+
$CXXFLAGS = ["-Wall -Werror=return-type",$CXXFLAGS].join(" ")
|
45
|
+
$CPPFLAGS = ["-Wall -Werror=return-type",$CPPFLAGS].join(" ")
|
46
|
+
|
47
|
+
# When adding objects here, make sure their directories are included in CLEANOBJS down at the bottom of extconf.rb.
|
48
|
+
basenames = %w{nmatrix ruby_constants data/data util/io math util/sl_list storage/common storage/storage storage/dense/dense storage/yale/yale storage/list/list}
|
49
|
+
$objs = basenames.map { |b| "#{b}.o" }
|
50
|
+
$srcs = basenames.map { |b| "#{b}.cpp" }
|
51
|
+
|
52
|
+
#$libs += " -lprofiler "
|
53
|
+
|
54
|
+
create_conf_h("nmatrix_config.h")
|
55
|
+
create_makefile("nmatrix")
|
56
|
+
|
57
|
+
Dir.mkdir("data") unless Dir.exists?("data")
|
58
|
+
Dir.mkdir("util") unless Dir.exists?("util")
|
59
|
+
Dir.mkdir("storage") unless Dir.exists?("storage")
|
60
|
+
Dir.chdir("storage") do
|
61
|
+
Dir.mkdir("yale") unless Dir.exists?("yale")
|
62
|
+
Dir.mkdir("list") unless Dir.exists?("list")
|
63
|
+
Dir.mkdir("dense") unless Dir.exists?("dense")
|
64
|
+
end
|
65
|
+
|
66
|
+
# to clean up object files in subdirectories:
|
67
|
+
open('Makefile', 'a') do |f|
|
68
|
+
clean_objs_paths = %w{data storage storage/dense storage/yale storage/list util}.map { |d| "#{d}/*.#{CONFIG["OBJEXT"]}" }
|
69
|
+
f.write("CLEANOBJS := $(CLEANOBJS) #{clean_objs_paths.join(' ')}")
|
70
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - present, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - present, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == asum.h
|
25
|
+
//
|
26
|
+
// CBLAS asum function
|
27
|
+
//
|
28
|
+
|
29
|
+
/*
|
30
|
+
* Automatically Tuned Linear Algebra Software v3.8.4
|
31
|
+
* (C) Copyright 1999 R. Clint Whaley
|
32
|
+
*
|
33
|
+
* Redistribution and use in source and binary forms, with or without
|
34
|
+
* modification, are permitted provided that the following conditions
|
35
|
+
* are met:
|
36
|
+
* 1. Redistributions of source code must retain the above copyright
|
37
|
+
* notice, this list of conditions and the following disclaimer.
|
38
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
39
|
+
* notice, this list of conditions, and the following disclaimer in the
|
40
|
+
* documentation and/or other materials provided with the distribution.
|
41
|
+
* 3. The name of the ATLAS group or the names of its contributers may
|
42
|
+
* not be used to endorse or promote products derived from this
|
43
|
+
* software without specific written permission.
|
44
|
+
*
|
45
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
46
|
+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
47
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
48
|
+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ATLAS GROUP OR ITS CONTRIBUTORS
|
49
|
+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
50
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
51
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
52
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
53
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
54
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
55
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
56
|
+
*
|
57
|
+
*/
|
58
|
+
|
59
|
+
#ifndef ASUM_H
|
60
|
+
#define ASUM_H
|
61
|
+
|
62
|
+
|
63
|
+
#include "math/magnitude.h"
|
64
|
+
|
65
|
+
namespace nm { namespace math {
|
66
|
+
|
67
|
+
/*
|
68
|
+
* Level 1 BLAS routine which sums the absolute values of a vector's contents. If the vector consists of complex values,
|
69
|
+
* the routine sums the absolute values of the real and imaginary components as well.
|
70
|
+
*
|
71
|
+
* So, based on input types, these are the valid return types:
|
72
|
+
* int -> int
|
73
|
+
* float -> float or double
|
74
|
+
* double -> double
|
75
|
+
* complex64 -> float or double
|
76
|
+
* complex128 -> double
|
77
|
+
*/
|
78
|
+
template <typename DType, typename MDType = typename MagnitudeDType<DType>::type>
|
79
|
+
inline MDType asum(const int N, const DType* X, const int incX) {
|
80
|
+
MDType sum = 0;
|
81
|
+
if ((N > 0) && (incX > 0)) {
|
82
|
+
for (int i = 0; i < N; ++i) {
|
83
|
+
sum += magnitude(X[i*incX]);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
return sum;
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
template <typename DType, typename MDType = typename MagnitudeDType<DType>::type>
|
91
|
+
inline void cblas_asum(const int N, const void* X, const int incX, void* sum) {
|
92
|
+
*reinterpret_cast<MDType*>( sum ) = asum<DType,MDType>( N, reinterpret_cast<const DType*>(X), incX );
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
}} // end of namespace nm::math
|
98
|
+
|
99
|
+
#endif // ASUM_H
|
@@ -0,0 +1,36 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2015, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2015, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == cblas_enums.h
|
25
|
+
//
|
26
|
+
// CBLAS definitions for when CBLAS is not available.
|
27
|
+
//
|
28
|
+
|
29
|
+
#ifndef CBLAS_ENUM_DEFINED_H
|
30
|
+
#define CBLAS_ENUM_DEFINED_H
|
31
|
+
enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102};
|
32
|
+
enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113};
|
33
|
+
enum CBLAS_UPLO {CblasUpper=121, CblasLower=122};
|
34
|
+
enum CBLAS_DIAG {CblasNonUnit=131, CblasUnit=132};
|
35
|
+
enum CBLAS_SIDE {CblasLeft=141, CblasRight=142};
|
36
|
+
#endif
|