gps_pvt 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/CHANGELOG.md +5 -0
  4. data/CODE_OF_CONDUCT.md +84 -0
  5. data/Gemfile +10 -0
  6. data/README.md +86 -0
  7. data/Rakefile +86 -0
  8. data/bin/console +15 -0
  9. data/bin/setup +8 -0
  10. data/ext/gps_pvt/Coordinate/Coordinate_wrap.cxx +6613 -0
  11. data/ext/gps_pvt/GPS/GPS_wrap.cxx +16019 -0
  12. data/ext/gps_pvt/SylphideMath/SylphideMath_wrap.cxx +21050 -0
  13. data/ext/gps_pvt/extconf.rb +70 -0
  14. data/ext/ninja-scan-light/tool/navigation/EGM.h +2971 -0
  15. data/ext/ninja-scan-light/tool/navigation/GPS.h +2432 -0
  16. data/ext/ninja-scan-light/tool/navigation/GPS_Solver.h +479 -0
  17. data/ext/ninja-scan-light/tool/navigation/GPS_Solver_Base.h +1081 -0
  18. data/ext/ninja-scan-light/tool/navigation/GPS_Solver_MultiFrequency.h +199 -0
  19. data/ext/ninja-scan-light/tool/navigation/GPS_Solver_RAIM.h +210 -0
  20. data/ext/ninja-scan-light/tool/navigation/MagneticField.h +928 -0
  21. data/ext/ninja-scan-light/tool/navigation/NTCM.h +211 -0
  22. data/ext/ninja-scan-light/tool/navigation/RINEX.h +1781 -0
  23. data/ext/ninja-scan-light/tool/navigation/WGS84.h +186 -0
  24. data/ext/ninja-scan-light/tool/navigation/coordinate.h +406 -0
  25. data/ext/ninja-scan-light/tool/param/bit_array.h +145 -0
  26. data/ext/ninja-scan-light/tool/param/complex.h +558 -0
  27. data/ext/ninja-scan-light/tool/param/matrix.h +4049 -0
  28. data/ext/ninja-scan-light/tool/param/matrix_fixed.h +665 -0
  29. data/ext/ninja-scan-light/tool/param/matrix_special.h +562 -0
  30. data/ext/ninja-scan-light/tool/param/quaternion.h +765 -0
  31. data/ext/ninja-scan-light/tool/param/vector3.h +651 -0
  32. data/ext/ninja-scan-light/tool/swig/Coordinate.i +177 -0
  33. data/ext/ninja-scan-light/tool/swig/GPS.i +1102 -0
  34. data/ext/ninja-scan-light/tool/swig/SylphideMath.i +1234 -0
  35. data/ext/ninja-scan-light/tool/swig/extconf.rb +5 -0
  36. data/ext/ninja-scan-light/tool/swig/makefile +53 -0
  37. data/ext/ninja-scan-light/tool/swig/spec/GPS_spec.rb +417 -0
  38. data/ext/ninja-scan-light/tool/swig/spec/SylphideMath_spec.rb +489 -0
  39. data/gps_pvt.gemspec +57 -0
  40. data/lib/gps_pvt/receiver.rb +375 -0
  41. data/lib/gps_pvt/ubx.rb +148 -0
  42. data/lib/gps_pvt/version.rb +5 -0
  43. data/lib/gps_pvt.rb +9 -0
  44. data/sig/gps_pvt.rbs +4 -0
  45. metadata +117 -0
@@ -0,0 +1,1234 @@
1
+ /**
2
+ * @file SWIG interface file for header files in param directory
3
+ *
4
+ */
5
+
6
+ %module SylphideMath
7
+
8
+ #define ENABLE_IOSTREAM 1
9
+
10
+ %{
11
+ #include <string>
12
+ #include <sstream>
13
+ #include <vector>
14
+ #include <exception>
15
+
16
+ #if defined(SWIGRUBY) && defined(isfinite)
17
+ #undef isfinite_
18
+ #undef isfinite
19
+ #endif
20
+
21
+ #include "param/complex.h"
22
+ #include "param/matrix.h"
23
+
24
+ #if defined(SWIGRUBY) && defined(isfinite_)
25
+ #undef isfinite_
26
+ #define isfinite(x) finite(x)
27
+ #endif
28
+ %}
29
+
30
+ %include std_common.i
31
+ %include std_string.i
32
+ //%include std_vector.i
33
+ %include exception.i
34
+
35
+ #if !defined(SWIGIMPORTED)
36
+ %header {
37
+ struct native_exception : public std::exception {
38
+ #if defined(SWIGRUBY)
39
+ int state;
40
+ native_exception(const int &state_) : std::exception(), state(state_) {}
41
+ void regenerate() const {rb_jump_tag(state);}
42
+ #else
43
+ void regenerate() const {}
44
+ #endif
45
+ };
46
+ }
47
+ %exception {
48
+ try {
49
+ $action
50
+ } catch (const native_exception &e) {
51
+ e.regenerate();
52
+ SWIG_fail;
53
+ } catch (const std::exception& e) {
54
+ SWIG_exception_fail(SWIG_RuntimeError, e.what());
55
+ }
56
+ }
57
+ #endif
58
+
59
+ %define MAKE_ACCESSOR(name, type)
60
+ %rename(%str(name ## =)) set_ ## name;
61
+ type set_ ## name (const type &v) {
62
+ return (self->name() = v);
63
+ }
64
+ %rename(%str(name)) get_ ## name;
65
+ type get_ ## name () {
66
+ return self->name();
67
+ }
68
+ %enddef
69
+ %define MAKE_TO_S(type)
70
+ %extend type{
71
+ std::string __str__() const {
72
+ std::stringstream s;
73
+ s << (*self);
74
+ return s.str();
75
+ }
76
+ };
77
+ %enddef
78
+
79
+ #ifdef SWIGRUBY
80
+ %header {
81
+ static VALUE funcall_throw_if_error(VALUE (*func)(VALUE), VALUE arg) {
82
+ int state;
83
+ VALUE res = rb_protect(func, arg, &state);
84
+ if(state != 0){throw native_exception(state);}
85
+ return res;
86
+ }
87
+ static VALUE yield_throw_if_error(const int &argc, const VALUE *argv) {
88
+ struct yield_t {
89
+ const int &argc;
90
+ const VALUE *argv;
91
+ static VALUE run(VALUE v){
92
+ yield_t *arg(reinterpret_cast<yield_t *>(v));
93
+ return rb_yield_values2(arg->argc, arg->argv);
94
+ }
95
+ } arg = {argc, argv};
96
+ return funcall_throw_if_error(yield_t::run, reinterpret_cast<VALUE>(&arg));
97
+ }
98
+ static std::string inspect_str(const VALUE &v){
99
+ VALUE v_inspect(rb_inspect(v));
100
+ return std::string(RSTRING_PTR(v_inspect), RSTRING_LEN(v_inspect));
101
+ }
102
+ }
103
+ #endif
104
+
105
+ %feature("autodoc", "1");
106
+
107
+ %ignore Complex::real;
108
+ %ignore Complex::imaginary;
109
+ %ignore Complex::check_infinity_t;
110
+ %ignore operator<<(std::ostream &, const Complex &);
111
+
112
+ template <class FloatT>
113
+ class Complex;
114
+
115
+ %copyctor Complex;
116
+
117
+ %fragment(SWIG_Traits_frag(ComplexGeneric), "header", fragment="StdTraits"){
118
+ // SWIG_Traits_frag(Complex) is invalid, which will be hidden by SWIG_Traits_frag(Complex<T>)
119
+ #ifdef SWIGRUBY
120
+ namespace swig {
121
+ template <class T> struct traits< Complex<T> > {
122
+ typedef value_category category;
123
+ };
124
+ template <class T> struct traits_asval< Complex<T> > {
125
+ typedef Complex<T> value_type;
126
+ static int asval(VALUE obj, value_type *v) {
127
+ if(RB_TYPE_P(obj, T_COMPLEX)){
128
+ #if RUBY_API_VERSION_CODE < 20600
129
+ static const ID id_r(rb_intern("real")), id_i(rb_intern("imag"));
130
+ int res = swig::asval(rb_funcall(obj, id_r, 0), &(v->real()));
131
+ if(!SWIG_IsOK(res)){return res;}
132
+ return swig::asval(rb_funcall(obj, id_i, 0), &(v->imaginary()));
133
+ #else
134
+ int res = swig::asval(rb_complex_real(obj), &(v->real()));
135
+ if(!SWIG_IsOK(res)){return res;}
136
+ return swig::asval(rb_complex_imag(obj), &(v->imaginary()));
137
+ #endif
138
+ }else{
139
+ v->imaginary() = T(0);
140
+ return swig::asval(obj, &(v->real()));
141
+ }
142
+ }
143
+ };
144
+ template <class T> struct traits_from< Complex<T> > {
145
+ typedef Complex<T> value_type;
146
+ static VALUE from(const value_type &v) {
147
+ return rb_complex_new(swig::from(v.real()), swig::from(v.imaginary()));
148
+ }
149
+ };
150
+ template <class T> struct traits_check< Complex<T>, value_category> {
151
+ static bool check(VALUE obj) {
152
+ if(RB_TYPE_P(obj, T_COMPLEX)){
153
+ #if RUBY_API_VERSION_CODE < 20600
154
+ static const ID id_r(rb_intern("real")), id_i(rb_intern("imag"));
155
+ return swig::check<T>(rb_funcall(obj, id_r, 0))
156
+ && swig::check<T>(rb_funcall(obj, id_i, 0));
157
+ #else
158
+ return swig::check<T>(rb_complex_real(obj))
159
+ && swig::check<T>(rb_complex_imag(obj));
160
+ #endif
161
+ }else{
162
+ return swig::check<T>(obj);
163
+ }
164
+ }
165
+ };
166
+ }
167
+ #endif
168
+ }
169
+
170
+ %extend Complex {
171
+ %fragment(SWIG_Traits_frag(Complex<FloatT>), "header",
172
+ fragment=SWIG_Traits_frag(FloatT),
173
+ fragment=SWIG_Traits_frag(ComplexGeneric)){
174
+ namespace swig {
175
+ template <>
176
+ inline swig_type_info *type_info<Complex<FloatT> >() {
177
+ return $descriptor(Complex<FloatT> *);
178
+ }
179
+ template <>
180
+ inline swig_type_info *type_info<Complex<FloatT> *>() {
181
+ return $descriptor(Complex<FloatT> *);
182
+ }
183
+ }
184
+ }
185
+ %fragment(SWIG_Traits_frag(Complex<FloatT>));
186
+ %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) const Complex<FloatT> & {
187
+ $1 = swig::check<$1_ltype >($input) || swig::check<$*1_ltype >($input);
188
+ }
189
+ %typemap(in) const Complex<FloatT> & (Complex<FloatT> temp) {
190
+ if((!SWIG_IsOK(swig::asptr($input, &$1)))
191
+ && (!SWIG_IsOK(swig::asval($input, ($1 = &temp))))){
192
+ SWIG_exception(SWIG_TypeError, "in method '$symname', expecting type $*1_ltype");
193
+ }
194
+ }
195
+ %typemap(out) Complex<FloatT> {
196
+ $result = swig::from($1);
197
+ }
198
+
199
+ static Complex<FloatT> rectangular(const FloatT &r, const FloatT &i = FloatT(0)) noexcept {
200
+ return Complex<FloatT>(r, i);
201
+ }
202
+
203
+ MAKE_ACCESSOR(real, FloatT);
204
+ MAKE_ACCESSOR(imaginary, FloatT);
205
+
206
+ #if defined(SWIGRUBY)
207
+ %alias power "**";
208
+ %alias arg "angle,phase";
209
+ %alias conjugate "conj";
210
+ %alias operator/ "fdiv";
211
+ %rename("finite?") isfinite;
212
+ %rename("infinite?") isinf;
213
+ %alias set_imaginary "imag=";
214
+ %alias get_imaginary "imag";
215
+ %alias abs "magnitude"
216
+ #endif
217
+ };
218
+
219
+ %include param/complex.h
220
+
221
+ MAKE_TO_S(Complex);
222
+
223
+ %define INSTANTIATE_COMPLEX(type, suffix)
224
+ %template(Complex ## suffix) Complex<type>;
225
+ #if defined(SWIGRUBY)
226
+ %fragment("init"{ComplexInit<type>}, "init") {
227
+ { /* work around of %alias rectangular "rect"; %alias cannot be applied to singleton method */
228
+ VALUE singleton = rb_singleton_class(
229
+ ((swig_class *)$descriptor(Complex<type> *)->clientdata)->klass);
230
+ rb_define_alias(singleton, "rect", "rectangular");
231
+ }
232
+ }
233
+ %fragment("init"{ComplexInit<type>});
234
+ #endif
235
+ %enddef
236
+
237
+ INSTANTIATE_COMPLEX(double, D);
238
+
239
+ #undef INSTANTIATE_COMPLEX
240
+
241
+ #define DO_NOT_INSTANTIATE_SCALAR_MATRIX
242
+ #define USE_MATRIX_VIEW_FILTER
243
+
244
+ #if defined(USE_MATRIX_VIEW_FILTER)
245
+ %{
246
+ template <class BaseView = MatrixViewBase<> >
247
+ struct MatrixViewFilter : public BaseView {
248
+ typedef MatrixViewFilter<BaseView> self_t;
249
+
250
+ struct {
251
+ unsigned int row_offset, column_offset;
252
+ unsigned int rows, columns;
253
+ bool transposed;
254
+ bool conjugated;
255
+ } prop;
256
+
257
+ MatrixViewFilter() : BaseView() {
258
+ prop.row_offset = prop.column_offset = 0;
259
+ prop.rows = prop.columns = 0; // to be configured
260
+ prop.transposed = false;
261
+ prop.conjugated = false;
262
+ }
263
+ MatrixViewFilter(const self_t &view)
264
+ : BaseView((const BaseView &)view), prop(view.prop) {
265
+ }
266
+
267
+ void transpose() {
268
+ prop.transposed = !prop.transposed;
269
+ }
270
+ void conjugate() {
271
+ prop.conjugated = !prop.conjugated;
272
+ }
273
+ void partial(
274
+ const unsigned int &new_rows, const unsigned int &new_columns,
275
+ const unsigned int &row_offset, const unsigned int &column_offset) {
276
+ if(prop.transposed){
277
+ prop.row_offset += column_offset;
278
+ prop.column_offset += row_offset;
279
+ prop.rows = new_columns;
280
+ prop.columns = new_rows;
281
+ }else{
282
+ prop.row_offset += row_offset;
283
+ prop.column_offset += column_offset;
284
+ prop.rows = new_rows;
285
+ prop.columns = new_columns;
286
+ }
287
+ }
288
+
289
+ template <class T, class Array2D_Type>
290
+ struct mat_t : public Matrix_Frozen<T, Array2D_Type, self_t> {
291
+ typedef Matrix_Frozen<T, Array2D_Type, self_t> super_t;
292
+ mat_t(const Matrix_Frozen<T, Array2D_Type, BaseView> &orig) : super_t(orig) {
293
+ super_t::view.prop.rows = orig.rows();
294
+ super_t::view.prop.columns = orig.columns();
295
+ }
296
+ mat_t(const Matrix_Frozen<T, Array2D_Type, self_t> &orig) : super_t(orig) {}
297
+ self_t &view() {return super_t::view;}
298
+ };
299
+
300
+ template <class T, class Array2D_Type, class ViewType>
301
+ static Matrix_Frozen<T, Array2D_Type, self_t> transpose(
302
+ const Matrix_Frozen<T, Array2D_Type, ViewType> &orig){
303
+ mat_t<T, Array2D_Type> res(orig);
304
+ res.view().transpose();
305
+ return res;
306
+ }
307
+ template <class T, class Array2D_Type, class ViewType>
308
+ static Matrix_Frozen<T, Array2D_Type, self_t> conjugate(
309
+ const Matrix_Frozen<T, Array2D_Type, ViewType> &orig){
310
+ return mat_t<T, Array2D_Type>(orig);
311
+ }
312
+ template <class T, class Array2D_Type, class ViewType>
313
+ static Matrix_Frozen<Complex<T>, Array2D_Type, self_t> conjugate(
314
+ const Matrix_Frozen<Complex<T>, Array2D_Type, ViewType> &orig){
315
+ mat_t<Complex<T>, Array2D_Type> res(orig);
316
+ res.view().conjugate();
317
+ return res;
318
+ }
319
+ template <class T, class Array2D_Type, class ViewType>
320
+ static Matrix_Frozen<T, Array2D_Type, self_t> partial(
321
+ const Matrix_Frozen<T, Array2D_Type, ViewType> &orig,
322
+ const unsigned int &new_rows, const unsigned int &new_columns,
323
+ const unsigned int &row_offset, const unsigned int &column_offset) {
324
+ if(new_rows + row_offset > orig.rows()){
325
+ throw std::out_of_range("Row size exceeding");
326
+ }else if(new_columns + column_offset > orig.columns()){
327
+ throw std::out_of_range("Column size exceeding");
328
+ }
329
+ mat_t<T, Array2D_Type> res(orig);
330
+ res.view().partial(new_rows, new_columns, row_offset, column_offset);
331
+ return res;
332
+ }
333
+ template <class T, class Array2D_Type, class ViewType>
334
+ static Matrix_Frozen<T, Array2D_Type, self_t> partial(
335
+ const Matrix_Frozen<T, Array2D_Type, ViewType> &orig,
336
+ const unsigned int &new_rows, const unsigned int &new_columns) {
337
+ return partial(orig, new_rows, new_columns, 0, 0);
338
+ }
339
+
340
+ template<class CharT, class Traits>
341
+ friend std::basic_ostream<CharT, Traits> &operator<<(
342
+ std::basic_ostream<CharT, Traits> &out, const self_t &view){
343
+ out
344
+ << (view.prop.transposed
345
+ ? (view.prop.conjugated ? "[*] " : "[T] ")
346
+ : (view.prop.conjugated ? "[~] " : ""))
347
+ << "[Size](" << view.prop.rows << "," << view.prop.columns << ") ";
348
+ if((view.prop.row_offset > 0) || (view.prop.column_offset > 0)){
349
+ out << "[Offset](" << view.prop.row_offset << "," << view.prop.column_offset << ") ";
350
+ }
351
+ return out << (const BaseView &)view;
352
+ }
353
+
354
+ inline const unsigned int rows(
355
+ const unsigned int &_rows, const unsigned int &_columns) const noexcept {
356
+ return prop.transposed ? prop.columns : prop.rows;
357
+ }
358
+ inline const unsigned int columns(
359
+ const unsigned int &_rows, const unsigned int &_columns) const noexcept {
360
+ return prop.transposed ? prop.rows : prop.columns;
361
+ }
362
+
363
+ template <class T>
364
+ struct conjugate_t {
365
+ static T run(const T &v, const bool &conjugated = false){return v;}
366
+ };
367
+ template <class T>
368
+ struct conjugate_t<Complex<T> > {
369
+ static Complex<T> run(const Complex<T> &v, const bool &conjugated = false){
370
+ return conjugated ? v.conjugate() : v;
371
+ }
372
+ };
373
+
374
+ template <class T, class Array2D_Type>
375
+ inline T operator()(
376
+ Array2D_Type &storage, const unsigned int &i, const unsigned int &j) const {
377
+ return conjugate_t<T>::run(
378
+ prop.transposed
379
+ ? BaseView::template operator()<T, Array2D_Type>(
380
+ storage, (j + prop.column_offset), (i + prop.row_offset))
381
+ : BaseView::template operator()<T, Array2D_Type>(
382
+ storage, (i + prop.row_offset), (j + prop.column_offset)),
383
+ prop.conjugated);
384
+ }
385
+ };
386
+ %}
387
+ #endif /* USE_MATRIX_VIEW_FILTER */
388
+
389
+ template <class T, class Array2D_Type, class ViewType = MatrixViewBase<> >
390
+ class Matrix_Frozen {
391
+ protected:
392
+ Matrix_Frozen();
393
+ public:
394
+ const unsigned int rows();
395
+ const unsigned int columns();
396
+
397
+ template <class T2, class Array2D_Type2, class ViewType2>
398
+ bool operator==(const Matrix_Frozen<T2, Array2D_Type2, ViewType2> &matrix) const noexcept;
399
+ // bool operator!= // automatically defined
400
+
401
+ bool isSquare() const noexcept;
402
+ bool isDiagonal() const noexcept;
403
+ bool isSymmetric() const noexcept;
404
+
405
+ T trace(const bool &do_check = true) const;
406
+ T sum() const noexcept;
407
+
408
+ // bool isLU() const noexcept
409
+
410
+ T determinant(const bool &do_check = true) const;
411
+ };
412
+
413
+ template <class T, class Array2D_Type, class ViewType = MatrixViewBase<> >
414
+ class Matrix : public Matrix_Frozen<T, Array2D_Type, ViewType> {
415
+ public:
416
+ #if !defined(DO_NOT_INSTANTIATE_SCALAR_MATRIX)
417
+ typedef Matrix_Frozen<T, Array2D_ScaledUnit<T> > scalar_matrix_t;
418
+ static scalar_matrix_t getScalar(const unsigned int &size, const T &scalar);
419
+ static scalar_matrix_t getI(const unsigned int &size);
420
+ #endif
421
+
422
+ typedef Matrix<T, Array2D_Type, ViewType> self_t;
423
+ self_t &swapRows(const unsigned int &row1, const unsigned int &row2);
424
+ self_t &swapColumns(const unsigned int &column1, const unsigned int &column2);
425
+ };
426
+
427
+ %inline {
428
+ typedef MatrixViewBase<> MatViewBase;
429
+ #if defined(USE_MATRIX_VIEW_FILTER)
430
+ typedef MatrixViewFilter<MatrixViewBase<> > MatView_f;
431
+ #else
432
+ typedef MatrixViewTranspose<MatrixViewBase<> > MatView_t;
433
+ typedef MatrixViewSizeVariable<MatrixViewOffset<MatrixViewBase<> > > MatView_p;
434
+ typedef MatrixViewTranspose<MatrixViewSizeVariable<MatrixViewOffset<MatrixViewBase<> > > > MatView_pt;
435
+ #endif
436
+ }
437
+
438
+ %define INSTANTIATE_MATRIX_FUNC(func_orig, func_new)
439
+ #if !defined(DO_NOT_INSTANTIATE_SCALAR_MATRIX)
440
+ %template(func_new) func_orig<T, Array2D_ScaledUnit<T>, MatViewBase>;
441
+ #if defined(USE_MATRIX_VIEW_FILTER)
442
+ %template(func_new) func_orig<T, Array2D_ScaledUnit<T>, MatView_f>;
443
+ #else
444
+ %template(func_new) func_orig<T, Array2D_ScaledUnit<T>, MatView_p>;
445
+ %template(func_new) func_orig<T, Array2D_ScaledUnit<T>, MatView_pt>;
446
+ #endif
447
+ #endif
448
+ %template(func_new) func_orig<T, Array2D_Dense<T>, MatViewBase>;
449
+ #if defined(USE_MATRIX_VIEW_FILTER)
450
+ %template(func_new) func_orig<T, Array2D_Dense<T>, MatView_f>;
451
+ #else
452
+ %template(func_new) func_orig<T, Array2D_Dense<T>, MatView_t>;
453
+ %template(func_new) func_orig<T, Array2D_Dense<T>, MatView_p>;
454
+ %template(func_new) func_orig<T, Array2D_Dense<T>, MatView_pt>;
455
+ #endif
456
+ %enddef
457
+
458
+ %{
459
+ struct MatrixUtil {
460
+ enum each_which_t {
461
+ EACH_ALL,
462
+ EACH_DIAGONAL,
463
+ EACH_OFF_DIAGONAL,
464
+ EACH_LOWER,
465
+ EACH_UPPER,
466
+ EACH_STRICT_LOWER,
467
+ EACH_STRICT_UPPER,
468
+ };
469
+ template <class T,
470
+ class Array2D_Type, class ViewType,
471
+ class Array2D_Type2 = Array2D_Dense<T>, class ViewType2 = MatrixViewBase<> >
472
+ static void each(
473
+ const Matrix_Frozen<T, Array2D_Type, ViewType> &src,
474
+ void (*each_func)(
475
+ const T &src_elm, T *dst_elm,
476
+ const unsigned int &i, const unsigned int &j),
477
+ const each_which_t &each_which = EACH_ALL,
478
+ Matrix<T, Array2D_Type2, ViewType2> *dst = NULL){
479
+ unsigned int i_max(src.rows()), j_max(src.columns());
480
+ switch(each_which){
481
+ case EACH_DIAGONAL:
482
+ for(unsigned int k(0), k_max(i_max >= j_max ? j_max : i_max); k < k_max; ++k){
483
+ (*each_func)(src(k, k), (dst ? &((*dst)(k, k)) : NULL), k, k);
484
+ }
485
+ break;
486
+ case EACH_OFF_DIAGONAL:
487
+ for(unsigned int i(0); i < i_max; ++i){
488
+ for(unsigned int j(0); j < j_max; ++j){
489
+ if(i != j){
490
+ (*each_func)(src(i, j), (dst ? &((*dst)(i, j)) : NULL), i, j);
491
+ }
492
+ }
493
+ }
494
+ break;
495
+ case EACH_LOWER:
496
+ for(unsigned int i(0); i < i_max; ++i){
497
+ for(unsigned int j(0); j <= i; ++j){
498
+ (*each_func)(src(i, j), (dst ? &((*dst)(i, j)) : NULL), i, j);
499
+ }
500
+ }
501
+ break;
502
+ case EACH_UPPER:
503
+ for(unsigned int i(0); i < i_max; ++i){
504
+ for(unsigned int j(i); j < j_max; ++j){
505
+ (*each_func)(src(i, j), (dst ? &((*dst)(i, j)) : NULL), i, j);
506
+ }
507
+ }
508
+ break;
509
+ case EACH_STRICT_LOWER:
510
+ for(unsigned int i(1); i < i_max; ++i){
511
+ for(unsigned int j(0); j < i; ++j){
512
+ (*each_func)(src(i, j), (dst ? &((*dst)(i, j)) : NULL), i, j);
513
+ }
514
+ }
515
+ break;
516
+ case EACH_STRICT_UPPER:
517
+ for(unsigned int i(0); i < i_max; ++i){
518
+ for(unsigned int j(i + 1); j < j_max; ++j){
519
+ (*each_func)(src(i, j), (dst ? &((*dst)(i, j)) : NULL), i, j);
520
+ }
521
+ }
522
+ break;
523
+ case EACH_ALL:
524
+ default:
525
+ for(unsigned int i(0); i < i_max; ++i){
526
+ for(unsigned int j(0); j < j_max; ++j){
527
+ (*each_func)(src(i, j), (dst ? &((*dst)(i, j)) : NULL), i, j);
528
+ }
529
+ }
530
+ break;
531
+ }
532
+ }
533
+ #if defined(SWIGRUBY)
534
+ static const each_which_t &sym2each_which(const VALUE &value){
535
+ if(!RB_TYPE_P(value, T_SYMBOL)){
536
+ std::runtime_error("Symbol is required");
537
+ }
538
+ static const struct {
539
+ VALUE sym;
540
+ each_which_t which;
541
+ } cmp[] = {
542
+ {ID2SYM(rb_intern("all")), EACH_ALL},
543
+ {ID2SYM(rb_intern("diagonal")), EACH_DIAGONAL},
544
+ {ID2SYM(rb_intern("off_diagonal")), EACH_OFF_DIAGONAL},
545
+ {ID2SYM(rb_intern("lower")), EACH_LOWER},
546
+ {ID2SYM(rb_intern("upper")), EACH_UPPER},
547
+ {ID2SYM(rb_intern("strict_lower")), EACH_STRICT_LOWER},
548
+ {ID2SYM(rb_intern("strict_upper")), EACH_STRICT_UPPER},
549
+ };
550
+ unsigned int i(0);
551
+ while(value != cmp[i].sym){
552
+ if(++i >= (sizeof(cmp) / sizeof(cmp[0]))){break;}
553
+ }
554
+ if(i >= (sizeof(cmp) / sizeof(cmp[0]))){
555
+ std::runtime_error("Unknown enumerate direction");
556
+ }
557
+ return cmp[i].which;
558
+ }
559
+ #endif
560
+
561
+ template <class T, class Array2D_Type, class ViewType>
562
+ static bool replace(
563
+ Matrix<T, Array2D_Type, ViewType> &dst,
564
+ const void *src = NULL){
565
+ unsigned int r(dst.rows()), c(dst.columns()), len(r * c);
566
+ bool replaced(true);
567
+ #if defined(SWIGRUBY)
568
+ struct bracket_read_t {
569
+ static VALUE run(VALUE v) {
570
+ VALUE *values = reinterpret_cast<VALUE *>(v);
571
+ static const ID id_func(rb_intern("[]"));
572
+ return rb_funcall2(values[0], id_func, 2, &values[1]);
573
+ }
574
+ static bool is_accessible(const VALUE &v) {
575
+ static const ID id_func(rb_intern("[]"));
576
+ return rb_respond_to(v, id_func) != 0;
577
+ }
578
+ static VALUE read(
579
+ const VALUE &v, const unsigned int &row = 0, const unsigned int &column = 0) {
580
+ int state;
581
+ VALUE values[3] = {v, UINT2NUM(row), UINT2NUM(column)};
582
+ return funcall_throw_if_error(run, reinterpret_cast<VALUE>(values));
583
+ }
584
+ };
585
+ const VALUE *value(static_cast<const VALUE *>(src));
586
+ unsigned int i(0), j(0), i_elm(0);
587
+ VALUE v_elm;
588
+ if(value && RB_TYPE_P(*value, T_ARRAY)){
589
+ if(RB_TYPE_P(RARRAY_AREF(*value, 0), T_ARRAY)){ // [[r0c0, r0c1, ...], ...]
590
+ if((unsigned int)RARRAY_LEN(*value) < r){
591
+ throw std::runtime_error("Length is too short");
592
+ }
593
+ VALUE value_r;
594
+ for(; i_elm < len; i_elm++){
595
+ if(j == 0){
596
+ value_r = RARRAY_AREF(*value, i);
597
+ if(!RB_TYPE_P(value_r, T_ARRAY)){
598
+ throw std::runtime_error("double array [[...], ...] is required");
599
+ }else if((unsigned int)RARRAY_LEN(value_r) < c){
600
+ throw std::runtime_error("Length is too short");
601
+ }
602
+ }
603
+ v_elm = RARRAY_AREF(value_r, j);
604
+ if(!SWIG_IsOK(swig::asval(v_elm, &dst(i, j)))){break;}
605
+ if(++j >= c){j = 0; ++i;}
606
+ }
607
+ }else{ // [r0c0, r0c1, ...]
608
+ if((unsigned int)RARRAY_LEN(*value) < len){
609
+ throw std::runtime_error("Length is too short");
610
+ }
611
+ for(; i_elm < len; i_elm++){
612
+ v_elm = RARRAY_AREF(*value, i_elm);
613
+ if(!SWIG_IsOK(swig::asval(v_elm, &dst(i, j)))){break;}
614
+ if(++j >= c){j = 0; ++i;}
615
+ }
616
+ }
617
+ }else if(value && bracket_read_t::is_accessible(*value)){
618
+ for(; i_elm < len; i_elm++){
619
+ v_elm = bracket_read_t::read(*value, i, j);
620
+ if(!SWIG_IsOK(swig::asval(v_elm, &dst(i, j)))){break;}
621
+ if(++j >= c){j = 0; ++i;}
622
+ }
623
+ }else if(rb_block_given_p()){
624
+ for(; i_elm < len; i_elm++){
625
+ VALUE args[2] = {UINT2NUM(i), UINT2NUM(j)};
626
+ v_elm = yield_throw_if_error(2, args);
627
+ if(!SWIG_IsOK(swig::asval(v_elm, &dst(i, j)))){break;}
628
+ if(++j >= c){j = 0; ++i;}
629
+ }
630
+ }else{
631
+ replaced = false;
632
+ }
633
+ if(replaced && (i_elm < len)){
634
+ std::stringstream s;
635
+ s << "Unexpected input [" << i << "," << j << "]: ";
636
+ throw std::runtime_error(s.str().append(inspect_str(v_elm)));
637
+ }
638
+ #endif
639
+ return replaced;
640
+ }
641
+ template <class T, class Array2D_Type, class ViewType>
642
+ static bool replace(
643
+ Matrix<T, Array2D_Type, ViewType> &dst,
644
+ const T *src){
645
+ if(!src){return false;}
646
+ for(unsigned int i(0), r(dst.rows()); i < r; ++i){
647
+ for(unsigned int j(0), c(dst.columns()); j < c; ++j){
648
+ dst(i, j) = *(src++);
649
+ }
650
+ }
651
+ return true;
652
+ }
653
+ };
654
+ %}
655
+
656
+ %extend Matrix_Frozen {
657
+ T __getitem__(const unsigned int &row, const unsigned int &column) const {
658
+ return ($self)->operator()(row, column);
659
+ }
660
+
661
+ Matrix<T, Array2D_Dense<T> > copy() const {
662
+ return $self->operator Matrix<T, Array2D_Dense<T> >();
663
+ }
664
+
665
+ Matrix<T, Array2D_Dense<T> > circular(
666
+ const unsigned int &row_offset, const unsigned int &column_offset,
667
+ const unsigned int &new_rows, const unsigned int &new_columns) const noexcept {
668
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->circular(
669
+ row_offset, column_offset, new_rows, new_columns));
670
+ }
671
+ Matrix<T, Array2D_Dense<T> > circular(
672
+ const unsigned int &row_offset, const unsigned int &column_offset) const noexcept {
673
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->circular(row_offset, column_offset));
674
+ }
675
+
676
+ INSTANTIATE_MATRIX_FUNC(operator==, __eq__);
677
+
678
+ template <class T2, class Array2D_Type2, class ViewType2>
679
+ bool isDifferentSize(const Matrix_Frozen<T2, Array2D_Type2, ViewType2> &matrix) const {
680
+ return ($self)->isDifferentSize(matrix);
681
+ }
682
+ INSTANTIATE_MATRIX_FUNC(isDifferentSize, isDifferentSize);
683
+
684
+ Matrix<T, Array2D_Dense<T> > operator*(const T &scalar) const noexcept {
685
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator*(scalar));
686
+ }
687
+ Matrix<T, Array2D_Dense<T> > operator/(const T &scalar) const noexcept {
688
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator/(scalar));
689
+ }
690
+ Matrix<T, Array2D_Dense<T> > operator-() const noexcept {
691
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator-());
692
+ }
693
+
694
+ template <class T2, class Array2D_Type2, class ViewType2>
695
+ Matrix<T, Array2D_Dense<T> > operator+(
696
+ const Matrix_Frozen<T2, Array2D_Type2, ViewType2> &matrix) const {
697
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator+(matrix));
698
+ }
699
+ INSTANTIATE_MATRIX_FUNC(operator+, __add__);
700
+ Matrix<T, Array2D_Dense<T> > operator+(const T &scalar) const {
701
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator+(scalar));
702
+ }
703
+
704
+ template <class T2, class Array2D_Type2, class ViewType2>
705
+ Matrix<T, Array2D_Dense<T> > operator-(
706
+ const Matrix_Frozen<T2, Array2D_Type2, ViewType2> &matrix) const {
707
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator-(matrix));
708
+ }
709
+ INSTANTIATE_MATRIX_FUNC(operator-, __sub__);
710
+ Matrix<T, Array2D_Dense<T> > operator-(const T &scalar) const {
711
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator-(scalar));
712
+ }
713
+
714
+ template <class T2, class Array2D_Type2, class ViewType2>
715
+ Matrix<T, Array2D_Dense<T> > operator*(
716
+ const Matrix_Frozen<T2, Array2D_Type2, ViewType2> &matrix) const {
717
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator*(matrix));
718
+ }
719
+ INSTANTIATE_MATRIX_FUNC(operator*, __mul__);
720
+
721
+ %typemap(in,numinputs=0)
722
+ Matrix<T, Array2D_Dense<T> > &output_L (Matrix<T, Array2D_Dense<T> > temp),
723
+ Matrix<T, Array2D_Dense<T> > &output_U (Matrix<T, Array2D_Dense<T> > temp),
724
+ Matrix<T, Array2D_Dense<T> > &output_P (Matrix<T, Array2D_Dense<T> > temp),
725
+ Matrix<T, Array2D_Dense<T> > &output_D (Matrix<T, Array2D_Dense<T> > temp),
726
+ Matrix<T, Array2D_Dense<T> > &output_Q (Matrix<T, Array2D_Dense<T> > temp),
727
+ Matrix<T, Array2D_Dense<T> > &output_R (Matrix<T, Array2D_Dense<T> > temp) %{
728
+ $1 = &temp;
729
+ %}
730
+ %typemap(argout)
731
+ Matrix<T, Array2D_Dense<T> > &output_L,
732
+ Matrix<T, Array2D_Dense<T> > &output_U,
733
+ Matrix<T, Array2D_Dense<T> > &output_P,
734
+ Matrix<T, Array2D_Dense<T> > &output_D,
735
+ Matrix<T, Array2D_Dense<T> > &output_Q,
736
+ Matrix<T, Array2D_Dense<T> > &output_R {
737
+ %append_output(SWIG_NewPointerObj((new $*1_ltype(*$1)), $1_descriptor, SWIG_POINTER_OWN));
738
+ }
739
+ void lup(
740
+ Matrix<T, Array2D_Dense<T> > &output_L,
741
+ Matrix<T, Array2D_Dense<T> > &output_U,
742
+ Matrix<T, Array2D_Dense<T> > &output_P) const {
743
+ struct buf_t {
744
+ unsigned int pivot_len, pivot_num;
745
+ unsigned int *pivot;
746
+ buf_t(const unsigned int &size)
747
+ : pivot_len(size), pivot(new unsigned int[size]) {}
748
+ ~buf_t(){
749
+ delete [] pivot;
750
+ }
751
+ Matrix<T, Array2D_Dense<T> > P() const {
752
+ Matrix<T, Array2D_Dense<T> > res(pivot_len, pivot_len);
753
+ for(unsigned int i(0); i < pivot_len; ++i){
754
+ res(i, pivot[i]) = 1;
755
+ }
756
+ return res;
757
+ }
758
+ } buf($self->rows());
759
+ Matrix<T, Array2D_Dense<T> > LU($self->decomposeLUP(buf.pivot_num, buf.pivot));
760
+ output_L = LU.partial($self->rows(), $self->columns()).copy();
761
+ output_U = LU.partial($self->rows(), $self->columns(), 0, $self->rows()).copy();
762
+ output_P = buf.P();
763
+ }
764
+ void ud(
765
+ Matrix<T, Array2D_Dense<T> > &output_U,
766
+ Matrix<T, Array2D_Dense<T> > &output_D) const {
767
+ Matrix<T, Array2D_Dense<T> > UD($self->decomposeUD());
768
+ output_U = UD.partial($self->rows(), $self->columns()).copy();
769
+ output_D = UD.partial($self->rows(), $self->columns(), 0, $self->rows()).copy();
770
+ }
771
+ void qr(
772
+ Matrix<T, Array2D_Dense<T> > &output_Q,
773
+ Matrix<T, Array2D_Dense<T> > &output_R) const {
774
+ Matrix<T, Array2D_Dense<T> > QR($self->decomposeQR());
775
+ output_Q = QR.partial($self->rows(), $self->rows()).copy();
776
+ output_R = QR.partial($self->rows(), $self->columns(), 0, $self->rows()).copy();
777
+ }
778
+
779
+ Matrix<T, Array2D_Dense<T> > inverse() const {
780
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->inverse());
781
+ }
782
+ template <class T2, class Array2D_Type2, class ViewType2>
783
+ Matrix<T, Array2D_Dense<T> > operator/(
784
+ const Matrix_Frozen<T2, Array2D_Type2, ViewType2> &matrix) const {
785
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->operator/(matrix));
786
+ }
787
+ INSTANTIATE_MATRIX_FUNC(operator/, __div__);
788
+
789
+ std::string debug() const {
790
+ std::stringstream s;
791
+ s << $self->inspect();
792
+ return s.str();
793
+ }
794
+
795
+ #ifdef SWIGRUBY
796
+ %rename("square?") isSquare;
797
+ %rename("diagonal?") isDiagonal;
798
+ %rename("symmetric?") isSymmetric;
799
+ %rename("different_size?") isDifferentSize;
800
+ %alias trace "tr";
801
+ %alias determinant "det";
802
+ %alias inverse "inv";
803
+ %alias transpose "t";
804
+ %alias lup "lup_decomposition";
805
+ %alias ud "ud_decomposition";
806
+ %alias qr "qr_decomposition";
807
+
808
+ %fragment(SWIG_From_frag(Matrix_Frozen_Helper<T>), "header",
809
+ fragment=SWIG_Traits_frag(T)){
810
+ template <bool with_index = false, bool assign = false>
811
+ static inline void matrix_yield_internal(
812
+ const T &src, T *dst, const unsigned int &i, const unsigned int &j){
813
+ SWIG_Object v;
814
+ if(with_index){
815
+ VALUE values[] = {swig::from(src), UINT2NUM(i), UINT2NUM(j)};
816
+ v = yield_throw_if_error(3, values);
817
+ }else{
818
+ VALUE values[] = {swig::from(src)};
819
+ v = yield_throw_if_error(1, values);
820
+ }
821
+ if(assign && !SWIG_IsOK(swig::asval(v, dst))){
822
+ std::stringstream s;
823
+ s << "Unknown input (T expected) [" << i << "," << j << "]: ";
824
+ throw std::runtime_error(s.str().append(inspect_str(v)));
825
+ }
826
+ }
827
+ static void matrix_yield(
828
+ const T &src, T *dst, const unsigned int &i, const unsigned int &j){
829
+ matrix_yield_internal<false, false>(src, dst, i, j);
830
+ }
831
+ static void matrix_yield_with_index(
832
+ const T &src, T *dst, const unsigned int &i, const unsigned int &j){
833
+ matrix_yield_internal<true, false>(src, dst, i, j);
834
+ }
835
+ static void matrix_yield_get(
836
+ const T &src, T *dst, const unsigned int &i, const unsigned int &j){
837
+ matrix_yield_internal<false, true>(src, dst, i, j);
838
+ }
839
+ static void matrix_yield_get_with_index(
840
+ const T &src, T *dst, const unsigned int &i, const unsigned int &j){
841
+ matrix_yield_internal<true, true>(src, dst, i, j);
842
+ }
843
+ static void (*matrix_each(const T *))
844
+ (const T &, T *, const unsigned int &, const unsigned int &) {
845
+ ID id_thisf(rb_frame_this_func()), id_callee(rb_frame_callee());
846
+ static const ID
847
+ id_map(rb_intern("map")), id_mapb(rb_intern("map!")),
848
+ id_eachwi(rb_intern("each_with_index"));
849
+ if((id_thisf == id_map) || (id_thisf == id_mapb)){
850
+ static const ID with_index[] = {
851
+ rb_intern("map_with_index"), rb_intern("map_with_index!"),
852
+ rb_intern("collect_with_index"), rb_intern("collect_with_index!")};
853
+ for(int i(0); i < sizeof(with_index) / sizeof(with_index[0]); ++i){
854
+ if(id_callee == with_index[i]){
855
+ return matrix_yield_get_with_index;
856
+ }
857
+ }
858
+ return matrix_yield_get;
859
+ }else if(id_callee == id_eachwi){
860
+ return matrix_yield_with_index;
861
+ }else{
862
+ return matrix_yield;
863
+ }
864
+ }
865
+ }
866
+ %typemap(in,numinputs=0, fragment=SWIG_From_frag(Matrix_Frozen_Helper<T>))
867
+ void (*each_func)(const T &src, T *dst, const unsigned int &i, const unsigned int &j) {
868
+ if(!rb_block_given_p()){
869
+ return rb_enumeratorize(self, ID2SYM(rb_frame_callee()), argc, argv);
870
+ }
871
+ $1 = matrix_each((const T *)0);
872
+ }
873
+ %typemap(typecheck) const typename MatrixUtil::each_which_t &each_which {
874
+ $1 = RB_TYPE_P($input, T_SYMBOL);
875
+ }
876
+ %typemap(in) const typename MatrixUtil::each_which_t &each_which {
877
+ try{
878
+ $1 = &const_cast<typename MatrixUtil::each_which_t &>(MatrixUtil::sym2each_which($input));
879
+ }catch(std::runtime_error &e){
880
+ SWIG_exception(SWIG_TypeError, e.what());
881
+ }
882
+ }
883
+ const Matrix_Frozen<T, Array2D_Type, ViewType> &each(
884
+ void (*each_func)(
885
+ const T &src, T *dst,
886
+ const unsigned int &i, const unsigned int &j),
887
+ const typename MatrixUtil::each_which_t &each_which = MatrixUtil::EACH_ALL) const {
888
+ MatrixUtil::each(*$self, each_func, each_which);
889
+ return *$self;
890
+ }
891
+ %alias each "each_with_index";
892
+
893
+ Matrix<T, Array2D_Dense<T> > map(
894
+ void (*each_func)(
895
+ const T &src, T *dst,
896
+ const unsigned int &i, const unsigned int &j),
897
+ const typename MatrixUtil::each_which_t &each_which = MatrixUtil::EACH_ALL) const {
898
+ Matrix<T, Array2D_Dense<T> > res($self->operator Matrix<T, Array2D_Dense<T> >());
899
+ MatrixUtil::each(*$self, each_func, each_which, &res);
900
+ return res;
901
+ }
902
+ %alias map "collect,map_with_index,collect_with_index";
903
+
904
+ SWIG_Object to_a() const {
905
+ unsigned int i_max($self->rows()), j_max($self->columns());
906
+ SWIG_Object res = rb_ary_new2(i_max);
907
+ for(unsigned int i(0); i < i_max; ++i){
908
+ SWIG_Object row = rb_ary_new2(j_max);
909
+ for(unsigned int j(0); j < j_max; ++j){
910
+ rb_ary_store(row, j, swig::from((*($self))(i, j)));
911
+ }
912
+ rb_ary_store(res, i, row);
913
+ }
914
+ return res;
915
+ }
916
+ #endif
917
+ };
918
+
919
+ #if defined(SWIGRUBY)
920
+ %mixin Matrix_Frozen "Enumerable";
921
+ #endif
922
+
923
+ MAKE_TO_S(Matrix_Frozen)
924
+
925
+ %extend Matrix {
926
+ %typemap(typecheck,precedence=SWIG_TYPECHECK_VOIDPTR) const void *replacer {
927
+ #if defined(SWIGRUBY)
928
+ $1 = rb_block_given_p() ? 0 : 1;
929
+ #else
930
+ $1 = 0;
931
+ #endif
932
+ }
933
+ %typemap(in) const void *replacer {
934
+ $1 = &$input;
935
+ }
936
+ %fragment(SWIG_Traits_frag(T));
937
+
938
+ Matrix(const unsigned int &rows, const unsigned int &columns,
939
+ const void *replacer = NULL){
940
+ Matrix<T, Array2D_Type, ViewType> res(rows, columns);
941
+ MatrixUtil::replace(res, replacer);
942
+ return new Matrix<T, Array2D_Type, ViewType>(res);
943
+ }
944
+ Matrix(const unsigned int &rows, const unsigned int &columns,
945
+ const T *serialized){
946
+ Matrix<T, Array2D_Type, ViewType> res(rows, columns);
947
+ MatrixUtil::replace(res, serialized);
948
+ return new Matrix<T, Array2D_Type, ViewType>(res);
949
+ }
950
+ #if defined(SWIGRUBY)
951
+ %fragment(SWIG_AsVal_frag(unsigned int));
952
+ Matrix(const void *replacer){
953
+ const SWIG_Object *value(static_cast<const SWIG_Object *>(replacer));
954
+ static const ID id_r(rb_intern("row_size")), id_c(rb_intern("column_size"));
955
+ if(value && RB_TYPE_P(*value, T_ARRAY) && RB_TYPE_P(RARRAY_AREF(*value, 0), T_ARRAY)){
956
+ Matrix<T, Array2D_Type, ViewType> res(
957
+ (unsigned int)RARRAY_LEN(*value),
958
+ (unsigned int)RARRAY_LEN(RARRAY_AREF(*value, 0)));
959
+ MatrixUtil::replace(res, replacer);
960
+ return new Matrix<T, Array2D_Type, ViewType>(res);
961
+ }else if(value && rb_respond_to(*value, id_r) && rb_respond_to(*value, id_c)){
962
+ /* "unsigned" is remove because SWIG_AsVal(unsigned int)
963
+ * can not detect less than zero in Windows Ruby devkit.
964
+ */
965
+ int r, c;
966
+ VALUE v_r(rb_funcall(*value, id_r, 0, 0)), v_c(rb_funcall(*value, id_c, 0, 0));
967
+ if(!SWIG_IsOK(SWIG_AsVal(int)(v_r, &r)) || (r < 0)
968
+ || !SWIG_IsOK(SWIG_AsVal(int)(v_c, &c)) || (c < 0)){
969
+ throw std::runtime_error(
970
+ std::string("Unexpected length [")
971
+ .append(inspect_str(v_r)).append(", ")
972
+ .append(inspect_str(v_c)).append("]"));
973
+ }
974
+ Matrix<T, Array2D_Type, ViewType> res(r, c);
975
+ MatrixUtil::replace(res, replacer);
976
+ return new Matrix<T, Array2D_Type, ViewType>(res);
977
+ }else{
978
+ throw std::runtime_error("double array [[...], ...] or Matrix is required");
979
+ }
980
+ }
981
+ #endif
982
+
983
+ %typemap(out) self_t & "$result = self;"
984
+
985
+ T &__setitem__(const unsigned int &row, const unsigned int &column, const T &value) {
986
+ return (($self)->operator()(row, column) = value);
987
+ }
988
+ #if defined(DO_NOT_INSTANTIATE_SCALAR_MATRIX)
989
+ static Matrix<T, Array2D_Dense<T> > getScalar(const unsigned int &size, const T &scalar) {
990
+ return Matrix<T, Array2D_Dense<T> >(
991
+ Matrix_Frozen<T, Array2D_Type, ViewType>::getScalar(size, scalar));
992
+ }
993
+ static Matrix<T, Array2D_Dense<T> > getI(const unsigned int &size) {
994
+ return Matrix<T, Array2D_Dense<T> >(
995
+ Matrix_Frozen<T, Array2D_Type, ViewType>::getI(size));
996
+ }
997
+ #endif
998
+ %rename("scalar") getScalar;
999
+ %rename("I") getI;
1000
+ %rename("swap_rows") swapRows;
1001
+ %rename("swap_columns") swapColumns;
1002
+
1003
+ template <class T2, class Array2D_Type2, class ViewType2>
1004
+ self_t &replace(const Matrix_Frozen<T2, Array2D_Type2, ViewType2> &matrix){
1005
+ return $self->replace(matrix);
1006
+ }
1007
+ INSTANTIATE_MATRIX_FUNC(replace, replace);
1008
+
1009
+ self_t &replace(const void *replacer = NULL){
1010
+ if(!MatrixUtil::replace(*$self, replacer)){
1011
+ throw std::runtime_error("Unsupported replacement");
1012
+ }
1013
+ return *$self;
1014
+ }
1015
+
1016
+ self_t &replace(const T *serialized){
1017
+ if(!MatrixUtil::replace(*$self, serialized)){
1018
+ throw std::runtime_error("Unsupported replacement");
1019
+ }
1020
+ return *$self;
1021
+ }
1022
+
1023
+ #ifdef SWIGRUBY
1024
+ %bang swapRows(const unsigned int &, const unsigned int &);
1025
+ %bang swapColumns(const unsigned int &, const unsigned int &);
1026
+ %rename("replace!") replace;
1027
+
1028
+ self_t &map_bang(
1029
+ void (*each_func)(
1030
+ const T &src, T *dst,
1031
+ const unsigned int &i, const unsigned int &j),
1032
+ const typename MatrixUtil::each_which_t &each_which = MatrixUtil::EACH_ALL){
1033
+ MatrixUtil::each(*$self, each_func, each_which, $self);
1034
+ return *$self;
1035
+ }
1036
+ %rename("map!") map_bang;
1037
+ %alias map_bang "collect!,map_with_index!,collect_with_index!";
1038
+ #endif
1039
+ };
1040
+
1041
+ %define INSTANTIATE_MATRIX_TRANSPOSE(type, storage, view_from, view_to)
1042
+ %extend Matrix_Frozen<type, storage, view_from> {
1043
+ Matrix_Frozen<type, storage, view_to> transpose() const {
1044
+ #if defined(USE_MATRIX_VIEW_FILTER)
1045
+ return MatView_f::transpose(*$self);
1046
+ #else
1047
+ return $self->transpose();
1048
+ #endif
1049
+ }
1050
+ };
1051
+ %enddef
1052
+
1053
+ %define INSTANTIATE_MATRIX_PARTIAL(type, storage, view_from, view_to)
1054
+ %extend Matrix_Frozen<type, storage, view_from> {
1055
+ Matrix_Frozen<type, storage, view_to> partial(
1056
+ const unsigned int &new_rows, const unsigned int &new_columns,
1057
+ const unsigned int &row_offset, const unsigned int &column_offset) const {
1058
+ #if defined(USE_MATRIX_VIEW_FILTER)
1059
+ return MatView_f::partial(*$self, new_rows, new_columns, row_offset, column_offset);
1060
+ #else
1061
+ return $self->partial(new_rows, new_columns, row_offset, column_offset);
1062
+ #endif
1063
+ }
1064
+ Matrix_Frozen<type, storage, view_to> row_vector(const unsigned int &row) const {
1065
+ #if defined(USE_MATRIX_VIEW_FILTER)
1066
+ return MatView_f::partial(*$self, 1, $self->columns(), row, 0);
1067
+ #else
1068
+ return $self->rowVector(row);
1069
+ #endif
1070
+ }
1071
+ Matrix_Frozen<type, storage, view_to> column_vector(const unsigned int &column) const {
1072
+ #if defined(USE_MATRIX_VIEW_FILTER)
1073
+ return MatView_f::partial(*$self, $self->rows(), 1, 0, column);
1074
+ #else
1075
+ return $self->columnVector(column);
1076
+ #endif
1077
+ }
1078
+ };
1079
+ %enddef
1080
+
1081
+ %define INSTANTIATE_MATRIX_EIGEN2(type, ctype, storage, view)
1082
+ %extend Matrix_Frozen<type, storage, view> {
1083
+ %typemap(in,numinputs=0)
1084
+ Matrix<ctype, Array2D_Dense<ctype > > &output_D
1085
+ (Matrix<ctype, Array2D_Dense<ctype > > temp),
1086
+ Matrix<ctype, Array2D_Dense<ctype > > &output_V
1087
+ (Matrix<ctype, Array2D_Dense<ctype > > temp) %{
1088
+ $1 = &temp;
1089
+ %}
1090
+ %typemap(argout)
1091
+ Matrix<ctype, Array2D_Dense<ctype > > &output_D,
1092
+ Matrix<ctype, Array2D_Dense<ctype > > &output_V {
1093
+ %append_output(SWIG_NewPointerObj((new $*1_ltype(*$1)), $1_descriptor, SWIG_POINTER_OWN));
1094
+ }
1095
+ void eigen(
1096
+ Matrix<ctype, Array2D_Dense<ctype > > &output_V,
1097
+ Matrix<ctype, Array2D_Dense<ctype > > &output_D) const {
1098
+ typedef typename Matrix_Frozen<type, storage, view >::complex_t::m_t cmat_t;
1099
+ cmat_t VD($self->eigen());
1100
+ output_V = VD.partial($self->rows(), $self->rows()).copy();
1101
+ cmat_t D($self->rows(), $self->rows());
1102
+ for(unsigned int i(0); i < $self->rows(); ++i){
1103
+ D(i, i) = VD(i, $self->rows());
1104
+ }
1105
+ output_D = D;
1106
+ }
1107
+ };
1108
+ %enddef
1109
+ %define INSTANTIATE_MATRIX_EIGEN(type, ctype)
1110
+ #if !defined(DO_NOT_INSTANTIATE_SCALAR_MATRIX)
1111
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_ScaledUnit<type >, MatViewBase);
1112
+ #if defined(USE_MATRIX_VIEW_FILTER)
1113
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_ScaledUnit<type >, MatView_f);
1114
+ #else
1115
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_ScaledUnit<type >, MatView_p);
1116
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_ScaledUnit<type >, MatView_pt);
1117
+ #endif
1118
+ #endif
1119
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_Dense<type >, MatViewBase);
1120
+ #if defined(USE_MATRIX_VIEW_FILTER)
1121
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_Dense<type >, MatView_f);
1122
+ #else
1123
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_Dense<type >, MatView_p);
1124
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_Dense<type >, MatView_t);
1125
+ INSTANTIATE_MATRIX_EIGEN2(type, ctype, Array2D_Dense<type >, MatView_pt);
1126
+ #endif
1127
+ %enddef
1128
+
1129
+ #if defined(USE_MATRIX_VIEW_FILTER)
1130
+ %extend Matrix_Frozen {
1131
+ Matrix_Frozen<T, Array2D_Type, MatView_f> conjugate() const {
1132
+ return MatView_f::conjugate(*$self);
1133
+ }
1134
+ Matrix_Frozen<T, Array2D_Type, MatView_f> adjoint() const {
1135
+ return MatView_f::conjugate(MatView_f::transpose(*$self));
1136
+ }
1137
+ };
1138
+ #else
1139
+ %extend Matrix_Frozen {
1140
+ Matrix<T, Array2D_Dense<T> > conjugate() const {
1141
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->conjugate());
1142
+ }
1143
+ Matrix<T, Array2D_Dense<T> > adjoint() const {
1144
+ return (Matrix<T, Array2D_Dense<T> >)(($self)->adjoint());
1145
+ }
1146
+ };
1147
+ #endif
1148
+
1149
+ %define INSTANTIATE_MATRIX(type, suffix)
1150
+ #if !defined(DO_NOT_INSTANTIATE_SCALAR_MATRIX)
1151
+ %extend Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatViewBase> {
1152
+ const Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatViewBase> &transpose() const {
1153
+ return *($self);
1154
+ }
1155
+ Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatViewBase> inverse() const {
1156
+ return $self->inverse();
1157
+ }
1158
+ };
1159
+ #if defined(USE_MATRIX_VIEW_FILTER)
1160
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_ScaledUnit<type >, MatView_f, MatView_f);
1161
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_ScaledUnit<type >, MatViewBase, MatView_f);
1162
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_ScaledUnit<type >, MatView_f, MatView_f);
1163
+
1164
+ %template(Matrix_Scalar ## suffix) Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatViewBase>;
1165
+ %template(Matrix_Scalar ## suffix ## _f) Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatView_f>;
1166
+ #else
1167
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_ScaledUnit<type >, MatView_p, MatView_pt);
1168
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_ScaledUnit<type >, MatView_pt, MatView_p);
1169
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_ScaledUnit<type >, MatViewBase, MatView_p);
1170
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_ScaledUnit<type >, MatView_p, MatView_p);
1171
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_ScaledUnit<type >, MatView_pt, MatView_pt);
1172
+
1173
+ %template(Matrix_Scalar ## suffix) Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatViewBase>;
1174
+ %template(Matrix_Scalar ## suffix ## _p) Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatView_p>;
1175
+ %template(Matrix_Scalar ## suffix ## _pt) Matrix_Frozen<type, Array2D_ScaledUnit<type >, MatView_pt>;
1176
+ #endif
1177
+ #endif
1178
+
1179
+ #if defined(USE_MATRIX_VIEW_FILTER)
1180
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_Dense<type >, MatViewBase, MatView_f);
1181
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_Dense<type >, MatView_f, MatView_f);
1182
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_Dense<type >, MatViewBase, MatView_f);
1183
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_Dense<type >, MatView_f, MatView_f);
1184
+
1185
+ %template(Matrix_Frozen ## suffix) Matrix_Frozen<type, Array2D_Dense<type >, MatViewBase>;
1186
+ %template(Matrix_Frozen ## suffix ## _f) Matrix_Frozen<type, Array2D_Dense<type >, MatView_f>;
1187
+ #else
1188
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_Dense<type >, MatViewBase, MatView_t);
1189
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_Dense<type >, MatView_t, MatViewBase);
1190
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_Dense<type >, MatView_p, MatView_pt);
1191
+ INSTANTIATE_MATRIX_TRANSPOSE(type, Array2D_Dense<type >, MatView_pt, MatView_p);
1192
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_Dense<type >, MatViewBase, MatView_p);
1193
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_Dense<type >, MatView_p, MatView_p);
1194
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_Dense<type >, MatView_t, MatView_pt);
1195
+ INSTANTIATE_MATRIX_PARTIAL(type, Array2D_Dense<type >, MatView_pt, MatView_pt);
1196
+
1197
+ %template(Matrix_Frozen ## suffix) Matrix_Frozen<type, Array2D_Dense<type >, MatViewBase>;
1198
+ %template(Matrix_Frozen ## suffix ## _t) Matrix_Frozen<type, Array2D_Dense<type >, MatView_t>;
1199
+ %template(Matrix_Frozen ## suffix ## _p) Matrix_Frozen<type, Array2D_Dense<type >, MatView_p>;
1200
+ %template(Matrix_Frozen ## suffix ## _pt) Matrix_Frozen<type, Array2D_Dense<type >, MatView_pt>;
1201
+ #endif
1202
+
1203
+ %template(Matrix ## suffix) Matrix<type, Array2D_Dense<type > >;
1204
+ #if defined(SWIGRUBY)
1205
+ %fragment("init"{Matrix<type, Array2D_Dense<type > >}, "init") {
1206
+ { /* work around of %alias I "unit,identity"; %alias cannot be applied to singleton method */
1207
+ VALUE singleton = rb_singleton_class(
1208
+ ((swig_class *)$descriptor(Matrix<type, Array2D_Dense<type > > *)->clientdata)->klass);
1209
+ rb_define_alias(singleton, "identity", "I");
1210
+ rb_define_alias(singleton, "unit", "I");
1211
+ }
1212
+ }
1213
+ %fragment("init"{Matrix<type, Array2D_Dense<type > >});
1214
+ #endif
1215
+ %enddef
1216
+
1217
+ INSTANTIATE_MATRIX(double, D);
1218
+ INSTANTIATE_MATRIX_EIGEN(double, Complex<double>);
1219
+ INSTANTIATE_MATRIX(Complex<double>, ComplexD);
1220
+ INSTANTIATE_MATRIX_EIGEN(Complex<double>, Complex<double>);
1221
+
1222
+ #undef INSTANTIATE_MATRIX_FUNC
1223
+ #undef INSTANTIATE_MATRIX_TRANSPOSE
1224
+ #undef INSTANTIATE_MATRIX_PARTIAL
1225
+ #undef INSTANTIATE_MATRIX_EIGEN2
1226
+ #undef INSTANTIATE_MATRIX_EIGEN
1227
+ #undef INSTANTIATE_MATRIX
1228
+
1229
+ #if defined(DO_NOT_INSTANTIATE_SCALAR_MATRIX)
1230
+ #undef DO_NOT_INSTANTIATE_SCALAR_MATRIX
1231
+ #endif
1232
+
1233
+ #undef MAKE_ACCESSOR
1234
+ #undef MAKE_TO_S