daikini-octave-ruby 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/History.txt +54 -0
  2. data/LICENSE.txt +674 -0
  3. data/Manifest.txt +39 -0
  4. data/README.txt +58 -0
  5. data/Rakefile +18 -0
  6. data/ext/octave_api/MANIFEST +4 -0
  7. data/ext/octave_api/Makefile +149 -0
  8. data/ext/octave_api/extconf.rb +30 -0
  9. data/ext/octave_api/octave-includes.h +11 -0
  10. data/ext/octave_api/octave-ruby.cpp +62 -0
  11. data/ext/octave_api/octave-ruby.h +18 -0
  12. data/ext/octave_api/octave_api.c +22 -0
  13. data/ext/octave_api/octave_api.h +13 -0
  14. data/ext/octave_api/or-array.cpp +56 -0
  15. data/ext/octave_api/or-array.h +20 -0
  16. data/ext/octave_api/or-boolean_matrix.cpp +61 -0
  17. data/ext/octave_api/or-boolean_matrix.h +20 -0
  18. data/ext/octave_api/or-cell_matrix.cpp +66 -0
  19. data/ext/octave_api/or-cell_matrix.h +22 -0
  20. data/ext/octave_api/or-hash.cpp +23 -0
  21. data/ext/octave_api/or-hash.h +19 -0
  22. data/ext/octave_api/or-matrix.cpp +89 -0
  23. data/ext/octave_api/or-matrix.h +22 -0
  24. data/ext/octave_api/or-string.cpp +13 -0
  25. data/ext/octave_api/or-string.h +22 -0
  26. data/ext/octave_api/or-struct_matrix.cpp +88 -0
  27. data/ext/octave_api/or-struct_matrix.h +22 -0
  28. data/ext/octave_api/or-variable.cpp +58 -0
  29. data/ext/octave_api/or-variable.h +22 -0
  30. data/lib/octave.rb +5 -0
  31. data/lib/octave/driver/native/driver.rb +26 -0
  32. data/lib/octave/engine.rb +75 -0
  33. data/lib/octave/matrix.rb +84 -0
  34. data/lib/octave/version.rb +14 -0
  35. data/octave-ruby.gemspec +21 -0
  36. data/setup.rb +1333 -0
  37. data/test/driver/native/test_conversions.rb +203 -0
  38. data/test/driver/native/test_driver.rb +32 -0
  39. data/test/test_engine.rb +50 -0
  40. metadata +99 -0
@@ -0,0 +1,20 @@
1
+ #ifndef OR_ARRAY_DEFINED_H_
2
+ #define OR_ARRAY_DEFINED_H_
3
+
4
+ #include "ruby.h"
5
+ #include "octave-includes.h"
6
+
7
+ class OR_Array
8
+ {
9
+ private:
10
+ VALUE ruby_val;
11
+
12
+ bool should_convert_to_cell_matrix();
13
+ public:
14
+ OR_Array(VALUE r) : ruby_val(r) { };
15
+ ~OR_Array();
16
+
17
+ octave_value to_octave();
18
+ };
19
+
20
+ #endif /* OR_ARRAY_DEFINED_H_ */
@@ -0,0 +1,61 @@
1
+ #include "or-boolean_matrix.h"
2
+
3
+ OR_BooleanMatrix::~OR_BooleanMatrix() {}
4
+
5
+ VALUE OR_BooleanMatrix::to_ruby()
6
+ {
7
+ Matrix matrix;
8
+ MArray<double> values;
9
+ double cell;
10
+ int i, number_of_values;
11
+ VALUE argv[2];
12
+
13
+ matrix = octave_val.matrix_value();
14
+ int number_of_rows = matrix.rows();
15
+ int number_of_columns = matrix.columns();
16
+
17
+ if (number_of_rows == 1) {
18
+ values = matrix.row(0);
19
+ } else if (number_of_columns == 1) {
20
+ values = matrix.column(0);
21
+ } else {
22
+ argv[0] = INT2FIX(number_of_rows);
23
+ argv[1] = INT2FIX(number_of_columns);
24
+ ruby_val = rb_class_new_instance(2, argv, rb_path2class("Octave::Matrix"));
25
+
26
+ int row_index, column_index = 0;
27
+ VALUE cells, row;
28
+ cells = rb_ary_new2(number_of_rows);
29
+ for (row_index = 0; row_index < number_of_rows; row_index++) {
30
+ row = rb_ary_new2(number_of_columns);
31
+ values = matrix.row(row_index);
32
+
33
+ for (column_index = 0; column_index < number_of_columns; column_index++) {
34
+ cell = values(column_index);
35
+ if (xisnan(cell) || octave_is_NA(cell)) {
36
+ rb_ary_push(row, Qnil);
37
+ } else {
38
+ rb_ary_push(row, (int(cell) == 1 ? Qtrue : Qfalse));
39
+ }
40
+ }
41
+
42
+ rb_ary_push(cells, row);
43
+ }
44
+
45
+ rb_iv_set(ruby_val, "@cells", cells);
46
+ return ruby_val;
47
+ }
48
+
49
+ number_of_values = values.length();
50
+ ruby_val = rb_ary_new2(number_of_values);
51
+ for (i = 0; i < number_of_values; i++) {
52
+ cell = values(i);
53
+ if (xisnan(cell) || octave_is_NA(cell)) {
54
+ rb_ary_push(ruby_val, Qnil);
55
+ } else {
56
+ rb_ary_push(ruby_val, (int(cell) == 1 ? Qtrue : Qfalse));
57
+ }
58
+ }
59
+
60
+ return ruby_val;
61
+ }
@@ -0,0 +1,20 @@
1
+ #ifndef OR_BOOLEAN_MATRIX_DEFINED_H_
2
+ #define OR_BOOLEAN_MATRIX_DEFINED_H_
3
+
4
+ #include "ruby.h"
5
+ #include "octave-includes.h"
6
+
7
+ class OR_BooleanMatrix
8
+ {
9
+ private:
10
+ VALUE ruby_val;
11
+ octave_value octave_val;
12
+
13
+ public:
14
+ OR_BooleanMatrix(octave_value o) : octave_val(o) { };
15
+ ~OR_BooleanMatrix();
16
+
17
+ VALUE to_ruby();
18
+ };
19
+
20
+ #endif /* OR_BOOLEAN_MATRIX_DEFINED_H_ */
@@ -0,0 +1,66 @@
1
+ #include "or-cell_matrix.h"
2
+ #include "or-variable.h"
3
+
4
+ OR_CellMatrix::~OR_CellMatrix() {}
5
+
6
+ VALUE OR_CellMatrix::to_ruby()
7
+ {
8
+ int i, number_of_values;
9
+ int number_of_rows = octave_val.rows();
10
+ int number_of_columns = octave_val.columns();
11
+ Cell cell = octave_val.cell_value();
12
+
13
+ if (number_of_rows == 1 && number_of_columns == 1) {
14
+ return OR_Variable(cell(0)).to_ruby();
15
+ } else if (number_of_columns == 1) {
16
+ number_of_values = number_of_rows;
17
+ } else {
18
+ VALUE argv[2];
19
+ argv[0] = INT2FIX(number_of_rows);
20
+ argv[1] = INT2FIX(number_of_columns);
21
+ ruby_val = rb_class_new_instance(2, argv, rb_path2class("Octave::CellMatrix"));
22
+
23
+ int row_index, column_index = 0;
24
+ VALUE cells, row;
25
+ cells = rb_ary_new2(number_of_rows);
26
+ for (row_index = 0; row_index < number_of_rows; row_index++) {
27
+ row = rb_ary_new2(number_of_columns);
28
+
29
+ for (column_index = 0; column_index < number_of_columns; column_index++) {
30
+ rb_ary_push(row, OR_Variable(cell(row_index, column_index)).to_ruby());
31
+ }
32
+
33
+ rb_ary_push(cells, row);
34
+ }
35
+
36
+ rb_iv_set(ruby_val, "@cells", cells);
37
+ return ruby_val;
38
+ }
39
+
40
+ ruby_val = rb_ary_new2(number_of_values);
41
+ for (i = 0; i < number_of_values; i++) {
42
+ rb_ary_push(ruby_val, OR_Variable(cell(i)).to_ruby());
43
+ }
44
+ return ruby_val;
45
+ }
46
+
47
+ octave_value OR_CellMatrix::to_octave()
48
+ {
49
+ int i, row_index, column_index;
50
+ int number_of_rows = FIX2INT(rb_iv_get(ruby_val, "@m"));
51
+ int number_of_columns = FIX2INT(rb_iv_get(ruby_val, "@n"));
52
+ VALUE cells = rb_iv_get(ruby_val, "@cells");
53
+ VALUE row, cell;
54
+ Cell matrix = Cell(number_of_rows, number_of_columns);
55
+
56
+ for (row_index = 0; row_index < number_of_rows; row_index++) {
57
+ row = RARRAY(cells)->ptr[row_index];
58
+
59
+ for (column_index = 0; column_index < number_of_columns; column_index++) {
60
+ cell = RARRAY(row)->ptr[column_index];
61
+ matrix(row_index, column_index) = OR_Variable(cell).to_octave();
62
+ }
63
+ }
64
+
65
+ return matrix;
66
+ }
@@ -0,0 +1,22 @@
1
+ #ifndef OR_CELL_MATRIX_DEFINED_H_
2
+ #define OR_CELL_MATRIX_DEFINED_H_
3
+
4
+ #include "ruby.h"
5
+ #include "octave-includes.h"
6
+
7
+ class OR_CellMatrix
8
+ {
9
+ private:
10
+ VALUE ruby_val;
11
+ octave_value octave_val;
12
+
13
+ public:
14
+ OR_CellMatrix(VALUE r) : ruby_val(r) { };
15
+ OR_CellMatrix(octave_value o) : octave_val(o) { };
16
+ ~OR_CellMatrix();
17
+
18
+ VALUE to_ruby();
19
+ octave_value to_octave();
20
+ };
21
+
22
+ #endif /* OR_CELL_MATRIX_DEFINED_H_ */
@@ -0,0 +1,23 @@
1
+ #include "or-hash.h"
2
+ #include "or-variable.h"
3
+
4
+ OR_Hash::~OR_Hash() {}
5
+
6
+ octave_value OR_Hash::to_octave()
7
+ {
8
+ int i;
9
+ VALUE names = rb_funcall(ruby_val, rb_intern("keys"), 0);
10
+ int number_of_keys = RARRAY(names)->len;
11
+
12
+ string_vector keys = string_vector();
13
+ for (i = 0; i < number_of_keys; i++) {
14
+ keys.append(std::string(RSTRING(RARRAY(names)->ptr[i])->ptr));
15
+ }
16
+
17
+ Octave_map struct_matrix = Octave_map(dim_vector(1, 1), Cell(keys));
18
+ for (i = 0; i < number_of_keys; i++) {
19
+ struct_matrix.contents(std::string(RSTRING(RARRAY(names)->ptr[i])->ptr)) = OR_Variable(rb_hash_aref(ruby_val, rb_str_new2(RSTRING(RARRAY(names)->ptr[i])->ptr))).to_octave();
20
+ }
21
+
22
+ return struct_matrix;
23
+ }
@@ -0,0 +1,19 @@
1
+ #ifndef OR_HASH_DEFINED_H_
2
+ #define OR_HASH_DEFINED_H_
3
+
4
+ #include "ruby.h"
5
+ #include "octave-includes.h"
6
+
7
+ class OR_Hash
8
+ {
9
+ private:
10
+ VALUE ruby_val;
11
+
12
+ public:
13
+ OR_Hash(VALUE r) : ruby_val(r) { };
14
+ ~OR_Hash();
15
+
16
+ octave_value to_octave();
17
+ };
18
+
19
+ #endif /* OR_HASH_DEFINED_H_ */
@@ -0,0 +1,89 @@
1
+ #include "or-matrix.h"
2
+
3
+ OR_Matrix::~OR_Matrix() {}
4
+
5
+ VALUE OR_Matrix::to_ruby()
6
+ {
7
+ Matrix matrix;
8
+ MArray<double> values;
9
+ double cell;
10
+ int i, number_of_values;
11
+ VALUE argv[2];
12
+
13
+ matrix = octave_val.matrix_value();
14
+ int number_of_rows = matrix.rows();
15
+ int number_of_columns = matrix.columns();
16
+
17
+ if ((number_of_rows == 0) && (number_of_columns == 0)) {
18
+ return rb_ary_new2(0);
19
+ } else if (number_of_columns == 1) {
20
+ values = matrix.column(0);
21
+ } else {
22
+ argv[0] = INT2FIX(number_of_rows);
23
+ argv[1] = INT2FIX(number_of_columns);
24
+ ruby_val = rb_class_new_instance(2, argv, rb_path2class("Octave::Matrix"));
25
+
26
+ int row_index, column_index = 0;
27
+ VALUE cells, row;
28
+ cells = rb_ary_new2(number_of_rows);
29
+ for (row_index = 0; row_index < number_of_rows; row_index++) {
30
+ row = rb_ary_new2(number_of_columns);
31
+ values = matrix.row(row_index);
32
+
33
+ for (column_index = 0; column_index < number_of_columns; column_index++) {
34
+ cell = values(column_index);
35
+ if (xisnan(cell) || octave_is_NA(cell)) {
36
+ rb_ary_push(row, Qnil);
37
+ } else {
38
+ rb_ary_push(row, rb_float_new(cell));
39
+ }
40
+ }
41
+
42
+ rb_ary_push(cells, row);
43
+ }
44
+
45
+ rb_iv_set(ruby_val, "@cells", cells);
46
+ return ruby_val;
47
+ }
48
+
49
+ number_of_values = values.length();
50
+ ruby_val = rb_ary_new2(number_of_values);
51
+ for (i = 0; i < number_of_values; i++) {
52
+ cell = values(i);
53
+ if (xisnan(cell) || octave_is_NA(cell)) {
54
+ rb_ary_push(ruby_val, Qnil);
55
+ } else {
56
+ rb_ary_push(ruby_val, rb_float_new(cell));
57
+ }
58
+ }
59
+
60
+ return ruby_val;
61
+ }
62
+
63
+ octave_value OR_Matrix::to_octave()
64
+ {
65
+ int row_index, column_index;
66
+ int number_of_rows = FIX2INT(rb_iv_get(ruby_val, "@m"));
67
+ int number_of_columns = FIX2INT(rb_iv_get(ruby_val, "@n"));
68
+ VALUE cells = rb_iv_get(ruby_val, "@cells");
69
+ VALUE row, cell;
70
+ Matrix matrix = Matrix(number_of_rows, number_of_columns);
71
+
72
+ for (row_index = 0; row_index < number_of_rows; row_index++) {
73
+ row = RARRAY(cells)->ptr[row_index];
74
+
75
+ for (column_index = 0; column_index < number_of_columns; column_index++) {
76
+ cell = RARRAY(row)->ptr[column_index];
77
+
78
+ if (rb_type(cell) == T_FLOAT) {
79
+ matrix(row_index, column_index) = RFLOAT(cell)->value;
80
+ } else if (rb_type(cell) == T_FIXNUM) {
81
+ matrix(row_index, column_index) = FIX2LONG(cell);
82
+ } else {
83
+ matrix(row_index, column_index) = octave_NaN;
84
+ }
85
+ }
86
+ }
87
+
88
+ return matrix;
89
+ }
@@ -0,0 +1,22 @@
1
+ #ifndef OR_MATRIX_DEFINED_H_
2
+ #define OR_MATRIX_DEFINED_H_
3
+
4
+ #include "ruby.h"
5
+ #include "octave-includes.h"
6
+
7
+ class OR_Matrix
8
+ {
9
+ private:
10
+ VALUE ruby_val;
11
+ octave_value octave_val;
12
+
13
+ public:
14
+ OR_Matrix(VALUE r) : ruby_val(r) { };
15
+ OR_Matrix(octave_value o) : octave_val(o) { };
16
+ ~OR_Matrix();
17
+
18
+ VALUE to_ruby();
19
+ octave_value to_octave();
20
+ };
21
+
22
+ #endif /* OR_MATRIX_DEFINED_H_ */
@@ -0,0 +1,13 @@
1
+ #include "or-string.h"
2
+
3
+ OR_String::~OR_String() {}
4
+
5
+ VALUE OR_String::to_ruby()
6
+ {
7
+ return rb_str_new2(octave_val.string_value().c_str());
8
+ }
9
+
10
+ octave_value OR_String::to_octave()
11
+ {
12
+ return std::string(RSTRING(ruby_val)->ptr);
13
+ }
@@ -0,0 +1,22 @@
1
+ #ifndef OR_STRING_DEFINED_H_
2
+ #define OR_STRING_DEFINED_H_
3
+
4
+ #include "ruby.h"
5
+ #include "octave-includes.h"
6
+
7
+ class OR_String
8
+ {
9
+ private:
10
+ VALUE ruby_val;
11
+ octave_value octave_val;
12
+
13
+ public:
14
+ OR_String(VALUE r) : ruby_val(r) { };
15
+ OR_String(octave_value o) : octave_val(o) { };
16
+ ~OR_String();
17
+
18
+ VALUE to_ruby();
19
+ octave_value to_octave();
20
+ };
21
+
22
+ #endif /* OR_STRING_DEFINED_H_ */
@@ -0,0 +1,88 @@
1
+ #include "or-struct_matrix.h"
2
+ #include "or-variable.h"
3
+
4
+ OR_StructMatrix::~OR_StructMatrix() {}
5
+
6
+ VALUE OR_StructMatrix::to_ruby()
7
+ {
8
+ Octave_map struct_matrix = Octave_map(octave_val.map_value());
9
+ string_vector keys = struct_matrix.keys();
10
+ int number_of_keys = keys.length();
11
+ int number_of_rows = struct_matrix.rows();
12
+ int number_of_columns = struct_matrix.columns();
13
+ int i;
14
+ VALUE cells, row, cell;
15
+
16
+ if (number_of_rows == 0 && number_of_columns == 1) {
17
+ ruby_val = rb_hash_new();
18
+ for (i = 0; i < number_of_keys; i++) {
19
+ rb_hash_aset(ruby_val, rb_str_new2(keys[i].c_str()), OR_Variable(struct_matrix.contents(keys[i])).to_ruby());
20
+ }
21
+ } else if (number_of_rows == 1 && number_of_columns == 1) {
22
+ ruby_val = rb_hash_new();
23
+ for (i = 0; i < number_of_keys; i++) {
24
+ rb_hash_aset(ruby_val, rb_str_new2(keys[i].c_str()), OR_Variable(struct_matrix.contents(keys[i])(0)).to_ruby());
25
+ }
26
+ } else {
27
+ int number_of_arguments_to_struct_matrix = number_of_keys + 2;
28
+ VALUE argv[number_of_arguments_to_struct_matrix];
29
+ argv[0] = INT2FIX(number_of_rows);
30
+ argv[1] = INT2FIX(number_of_columns);
31
+
32
+ for (i = 0; i < number_of_keys; i++) {
33
+ argv[2 + i] = rb_str_new2(keys[i].c_str());
34
+ }
35
+ ruby_val = rb_class_new_instance(number_of_arguments_to_struct_matrix, argv, rb_path2class("Octave::StructMatrix"));
36
+ cells = rb_ary_new2(number_of_rows);
37
+
38
+ int row_index, column_index;
39
+ for (row_index = 0; row_index < number_of_rows; row_index++) {
40
+ row = rb_ary_new2(number_of_columns);
41
+
42
+ for (column_index = 0; column_index < number_of_columns; column_index++) {
43
+ cell = rb_hash_new();
44
+ for (i = 0; i < number_of_keys; i++) {
45
+ rb_hash_aset(cell, rb_str_new2(keys[i].c_str()), OR_Variable(struct_matrix.contents(keys[i])(row_index, column_index)).to_ruby());
46
+ }
47
+ rb_ary_push(row, cell);
48
+ }
49
+
50
+ rb_ary_push(cells, row);
51
+ }
52
+
53
+ rb_iv_set(ruby_val, "@cells", cells);
54
+ }
55
+
56
+ return ruby_val;
57
+ }
58
+
59
+ octave_value OR_StructMatrix::to_octave()
60
+ {
61
+ int i, row_index, column_index;
62
+ VALUE row, cell;
63
+ VALUE cells = rb_iv_get(ruby_val, "@cells");
64
+ VALUE names = rb_iv_get(ruby_val, "@names");
65
+ int number_of_keys = RARRAY(names)->len;
66
+ int number_of_rows = FIX2INT(rb_iv_get(ruby_val, "@m"));
67
+ int number_of_columns = FIX2INT(rb_iv_get(ruby_val, "@n"));
68
+
69
+ string_vector keys = string_vector();
70
+ for (i = 0; i < number_of_keys; i++) {
71
+ keys.append(std::string(RSTRING(RARRAY(names)->ptr[i])->ptr));
72
+ }
73
+
74
+ Octave_map struct_matrix = Octave_map(dim_vector(number_of_rows, number_of_columns), Cell(keys));
75
+ for (row_index = 0; row_index < number_of_rows; row_index++) {
76
+ row = RARRAY(cells)->ptr[row_index];
77
+
78
+ for (column_index = 0; column_index < number_of_columns; column_index++) {
79
+ cell = RARRAY(row)->ptr[column_index];
80
+
81
+ for (i = 0; i < number_of_keys; i++) {
82
+ struct_matrix.contents(std::string(RSTRING(RARRAY(names)->ptr[i])->ptr))(row_index, column_index) = OR_Variable(rb_hash_aref(cell, rb_str_new2(RSTRING(RARRAY(names)->ptr[i])->ptr))).to_octave();
83
+ }
84
+ }
85
+ }
86
+
87
+ return struct_matrix;
88
+ }