nmatrix 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. data/.gitignore +27 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +3 -5
  4. data/Guardfile +6 -0
  5. data/History.txt +33 -0
  6. data/Manifest.txt +41 -38
  7. data/README.rdoc +88 -11
  8. data/Rakefile +35 -53
  9. data/ext/nmatrix/data/complex.h +372 -0
  10. data/ext/nmatrix/data/data.cpp +275 -0
  11. data/ext/nmatrix/data/data.h +707 -0
  12. data/ext/nmatrix/data/rational.h +421 -0
  13. data/ext/nmatrix/data/ruby_object.h +446 -0
  14. data/ext/nmatrix/extconf.rb +101 -51
  15. data/ext/nmatrix/new_extconf.rb +56 -0
  16. data/ext/nmatrix/nmatrix.cpp +1609 -0
  17. data/ext/nmatrix/nmatrix.h +265 -849
  18. data/ext/nmatrix/ruby_constants.cpp +134 -0
  19. data/ext/nmatrix/ruby_constants.h +103 -0
  20. data/ext/nmatrix/storage/common.cpp +70 -0
  21. data/ext/nmatrix/storage/common.h +170 -0
  22. data/ext/nmatrix/storage/dense.cpp +665 -0
  23. data/ext/nmatrix/storage/dense.h +116 -0
  24. data/ext/nmatrix/storage/list.cpp +1088 -0
  25. data/ext/nmatrix/storage/list.h +129 -0
  26. data/ext/nmatrix/storage/storage.cpp +658 -0
  27. data/ext/nmatrix/storage/storage.h +99 -0
  28. data/ext/nmatrix/storage/yale.cpp +1601 -0
  29. data/ext/nmatrix/storage/yale.h +208 -0
  30. data/ext/nmatrix/ttable_helper.rb +126 -0
  31. data/ext/nmatrix/{yale/smmp1_header.template.c → types.h} +36 -9
  32. data/ext/nmatrix/util/io.cpp +295 -0
  33. data/ext/nmatrix/util/io.h +117 -0
  34. data/ext/nmatrix/util/lapack.h +1175 -0
  35. data/ext/nmatrix/util/math.cpp +557 -0
  36. data/ext/nmatrix/util/math.h +1363 -0
  37. data/ext/nmatrix/util/sl_list.cpp +475 -0
  38. data/ext/nmatrix/util/sl_list.h +255 -0
  39. data/ext/nmatrix/util/util.h +78 -0
  40. data/lib/nmatrix/blas.rb +70 -0
  41. data/lib/nmatrix/io/mat5_reader.rb +567 -0
  42. data/lib/nmatrix/io/mat_reader.rb +162 -0
  43. data/lib/{string.rb → nmatrix/monkeys.rb} +49 -2
  44. data/lib/nmatrix/nmatrix.rb +199 -0
  45. data/lib/nmatrix/nvector.rb +103 -0
  46. data/lib/nmatrix/version.rb +27 -0
  47. data/lib/nmatrix.rb +22 -230
  48. data/nmatrix.gemspec +59 -0
  49. data/scripts/mac-brew-gcc.sh +47 -0
  50. data/spec/4x4_sparse.mat +0 -0
  51. data/spec/4x5_dense.mat +0 -0
  52. data/spec/blas_spec.rb +47 -0
  53. data/spec/elementwise_spec.rb +164 -0
  54. data/spec/io_spec.rb +60 -0
  55. data/spec/lapack_spec.rb +52 -0
  56. data/spec/math_spec.rb +96 -0
  57. data/spec/nmatrix_spec.rb +93 -89
  58. data/spec/nmatrix_yale_spec.rb +52 -36
  59. data/spec/nvector_spec.rb +1 -1
  60. data/spec/slice_spec.rb +257 -0
  61. data/spec/spec_helper.rb +51 -0
  62. data/spec/utm5940.mtx +83844 -0
  63. metadata +113 -71
  64. data/.autotest +0 -23
  65. data/.gemtest +0 -0
  66. data/ext/nmatrix/cblas.c +0 -150
  67. data/ext/nmatrix/dense/blas_header.template.c +0 -52
  68. data/ext/nmatrix/dense/elementwise.template.c +0 -107
  69. data/ext/nmatrix/dense/gemm.template.c +0 -159
  70. data/ext/nmatrix/dense/gemv.template.c +0 -130
  71. data/ext/nmatrix/dense/rationalmath.template.c +0 -68
  72. data/ext/nmatrix/dense.c +0 -307
  73. data/ext/nmatrix/depend +0 -18
  74. data/ext/nmatrix/generator/syntax_tree.rb +0 -481
  75. data/ext/nmatrix/generator.rb +0 -594
  76. data/ext/nmatrix/list.c +0 -774
  77. data/ext/nmatrix/nmatrix.c +0 -1977
  78. data/ext/nmatrix/rational.c +0 -98
  79. data/ext/nmatrix/yale/complexmath.template.c +0 -71
  80. data/ext/nmatrix/yale/elementwise.template.c +0 -46
  81. data/ext/nmatrix/yale/elementwise_op.template.c +0 -73
  82. data/ext/nmatrix/yale/numbmm.template.c +0 -94
  83. data/ext/nmatrix/yale/smmp1.template.c +0 -21
  84. data/ext/nmatrix/yale/smmp2.template.c +0 -43
  85. data/ext/nmatrix/yale/smmp2_header.template.c +0 -46
  86. data/ext/nmatrix/yale/sort_columns.template.c +0 -56
  87. data/ext/nmatrix/yale/symbmm.template.c +0 -54
  88. data/ext/nmatrix/yale/transp.template.c +0 -68
  89. data/ext/nmatrix/yale.c +0 -726
  90. data/lib/array.rb +0 -67
  91. data/spec/syntax_tree_spec.rb +0 -46
@@ -0,0 +1,134 @@
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 - 2012, Ruby Science Foundation
13
+ // NMatrix is Copyright (c) 2012, 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_symbols.cpp
25
+ //
26
+ // Ruby symbols used throught the NMatrix project.
27
+
28
+ /*
29
+ * Standard Includes
30
+ */
31
+
32
+ #include <ruby.h>
33
+
34
+ /*
35
+ * Project Includes
36
+ */
37
+
38
+ /*
39
+ * Macros
40
+ */
41
+
42
+ /*
43
+ * Global Variables
44
+ */
45
+
46
+ ID nm_rb_real,
47
+ nm_rb_imag,
48
+
49
+ nm_rb_numer,
50
+ nm_rb_denom,
51
+
52
+ nm_rb_complex_conjugate,
53
+ nm_rb_transpose,
54
+ nm_rb_no_transpose,
55
+ nm_rb_left,
56
+ nm_rb_right,
57
+ nm_rb_upper,
58
+ nm_rb_lower,
59
+ nm_rb_unit,
60
+ nm_rb_nonunit,
61
+
62
+ nm_rb_dense,
63
+ nm_rb_list,
64
+ nm_rb_yale,
65
+
66
+ nm_rb_add,
67
+ nm_rb_sub,
68
+ nm_rb_mul,
69
+ nm_rb_div,
70
+
71
+ nm_rb_percent,
72
+ nm_rb_gt,
73
+ nm_rb_lt,
74
+ nm_rb_eql,
75
+ nm_rb_neql,
76
+ nm_rb_gte,
77
+ nm_rb_lte;
78
+
79
+ VALUE cNMatrix,
80
+ cNMatrix_IO,
81
+ cNMatrix_IO_Matlab,
82
+ cNVector,
83
+ cNMatrix_YaleFunctions,
84
+ cNMatrix_BLAS,
85
+ cNMatrix_LAPACK,
86
+
87
+ nm_eDataTypeError,
88
+ nm_eStorageTypeError;
89
+
90
+ /*
91
+ * Forward Declarations
92
+ */
93
+
94
+ /*
95
+ * Functions
96
+ */
97
+
98
+ void nm_init_ruby_constants(void) {
99
+
100
+ nm_rb_real = rb_intern("real");
101
+ nm_rb_imag = rb_intern("imag");
102
+
103
+ nm_rb_numer = rb_intern("numerator");
104
+ nm_rb_denom = rb_intern("denominator");
105
+
106
+ nm_rb_complex_conjugate = rb_intern("complex_conjugate");
107
+ nm_rb_transpose = rb_intern("transpose");
108
+ nm_rb_no_transpose = rb_intern("no_transpose");
109
+
110
+ nm_rb_dense = rb_intern("dense");
111
+ nm_rb_list = rb_intern("list");
112
+ nm_rb_yale = rb_intern("yale");
113
+
114
+ nm_rb_add = rb_intern("+");
115
+ nm_rb_sub = rb_intern("-");
116
+ nm_rb_mul = rb_intern("*");
117
+ nm_rb_div = rb_intern("/");
118
+
119
+ nm_rb_percent = rb_intern("%");
120
+ nm_rb_gt = rb_intern(">");
121
+ nm_rb_lt = rb_intern("<");
122
+ nm_rb_eql = rb_intern("==");
123
+ nm_rb_neql = rb_intern("!=");
124
+ nm_rb_gte = rb_intern(">=");
125
+ nm_rb_lte = rb_intern("<=");
126
+
127
+ nm_rb_left = rb_intern("left");
128
+ nm_rb_right = rb_intern("right");
129
+ nm_rb_upper = rb_intern("upper");
130
+ nm_rb_lower = rb_intern("lower");
131
+ nm_rb_unit = rb_intern("unit");
132
+ nm_rb_nonunit = rb_intern("nonunit");
133
+ }
134
+
@@ -0,0 +1,103 @@
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 - 2012, Ruby Science Foundation
13
+ // NMatrix is Copyright (c) 2012, 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
+ // == data.h
25
+ //
26
+ // Header file for dealing with data types.
27
+
28
+ #ifndef RUBY_CONSTANTS_H
29
+ #define RUBY_CONSTANTS_H
30
+
31
+ /*
32
+ * Standard Includes
33
+ */
34
+
35
+ #include <ruby.h>
36
+
37
+ /*
38
+ * Project Includes
39
+ */
40
+
41
+ /*
42
+ * Macros
43
+ */
44
+
45
+ /*
46
+ * Types
47
+ */
48
+
49
+ /*
50
+ * Data
51
+ */
52
+
53
+ extern ID nm_rb_real,
54
+ nm_rb_imag,
55
+
56
+ nm_rb_numer,
57
+ nm_rb_denom,
58
+
59
+ nm_rb_complex_conjugate,
60
+ nm_rb_transpose,
61
+ nm_rb_no_transpose,
62
+ nm_rb_left,
63
+ nm_rb_right,
64
+ nm_rb_upper,
65
+ nm_rb_lower,
66
+ nm_rb_unit,
67
+ nm_rb_nonunit,
68
+
69
+ nm_rb_dense,
70
+ nm_rb_list,
71
+ nm_rb_yale,
72
+
73
+ nm_rb_add,
74
+ nm_rb_sub,
75
+ nm_rb_mul,
76
+ nm_rb_div,
77
+
78
+ nm_rb_percent,
79
+ nm_rb_gt,
80
+ nm_rb_lt,
81
+ nm_rb_eql,
82
+ nm_rb_neql,
83
+ nm_rb_gte,
84
+ nm_rb_lte;
85
+
86
+ extern VALUE cNMatrix,
87
+ cNMatrix_IO,
88
+ cNMatrix_IO_Matlab,
89
+ cNVector,
90
+ cNMatrix_YaleFunctions,
91
+ cNMatrix_BLAS,
92
+ cNMatrix_LAPACK,
93
+
94
+ nm_eDataTypeError,
95
+ nm_eStorageTypeError;
96
+
97
+ /*
98
+ * Functions
99
+ */
100
+
101
+ void nm_init_ruby_constants(void);
102
+
103
+ #endif // RUBY_CONSTANTS_H
@@ -0,0 +1,70 @@
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 - 2012, Ruby Science Foundation
13
+ // NMatrix is Copyright (c) 2012, 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
+ // == common.cpp
25
+ //
26
+ // Code for the STORAGE struct that is common to all storage types.
27
+
28
+ /*
29
+ * Standard Includes
30
+ */
31
+
32
+ /*
33
+ * Project Includes
34
+ */
35
+
36
+ #include "common.h"
37
+
38
+ /*
39
+ * Macros
40
+ */
41
+
42
+ /*
43
+ * Global Variables
44
+ */
45
+
46
+ /*
47
+ * Forward Declarations
48
+ */
49
+
50
+ /*
51
+ * Functions
52
+ */
53
+
54
+ extern "C" {
55
+ /*
56
+ * Calculate the number of elements in the dense storage structure, based on
57
+ * shape and dim.
58
+ */
59
+ size_t nm_storage_count_max_elements(const STORAGE* storage) {
60
+ unsigned int i;
61
+ size_t count = 1;
62
+
63
+ for (i = storage->dim; i-- > 0;) {
64
+ count *= storage->shape[i];
65
+ }
66
+
67
+ return count;
68
+ }
69
+
70
+ } // end of extern "C" block
@@ -0,0 +1,170 @@
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 - 2012, Ruby Science Foundation
13
+ // NMatrix is Copyright (c) 2012, 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
+ // == common.h
25
+ //
26
+ // Header file for code common to all storage types.
27
+
28
+ #ifndef STORAGE_COMMON_H
29
+ #define STORAGE_COMMON_H
30
+
31
+ /*
32
+ * Standard Includes
33
+ */
34
+
35
+ /*
36
+ * Project Includes
37
+ */
38
+ #include "data/data.h"
39
+ #include "nmatrix.h"
40
+
41
+ /*
42
+ * Macros
43
+ */
44
+
45
+ extern "C" {
46
+
47
+ /*
48
+ * Types
49
+ */
50
+
51
+ // For binary operations involving matrices that need to be casted.
52
+ struct STORAGE_PAIR {
53
+ STORAGE* left;
54
+ STORAGE* right;
55
+ };
56
+
57
+ struct SLICE {
58
+ size_t* coords; // Coordinate of first element
59
+ size_t* lengths; // Lengths of slice
60
+ bool single; // true if all lengths equal to 1 (represents single matrix element)
61
+ };
62
+
63
+ /*
64
+ * Data
65
+ */
66
+
67
+ /*
68
+ * Functions
69
+ */
70
+
71
+ size_t nm_storage_count_max_elements(const STORAGE* storage);
72
+
73
+ } // end of extern "C" block
74
+
75
+ namespace nm {
76
+
77
+ /*
78
+ * Templated helper function for element-wise operations, used by dense, yale, and list.
79
+ */
80
+ template <ewop_t op, typename LDType, typename RDType>
81
+ inline LDType ew_op_switch(LDType left, RDType right) {
82
+ switch (op) {
83
+ case EW_ADD:
84
+ return left + right;
85
+
86
+ case EW_SUB:
87
+ return left - right;
88
+
89
+ case EW_MUL:
90
+ return left * right;
91
+
92
+ case EW_DIV:
93
+ return left / right;
94
+
95
+ case EW_MOD:
96
+ rb_raise(rb_eNotImpError, "Element-wise modulo is currently not supported.");
97
+ break;
98
+
99
+ default:
100
+ rb_raise(rb_eStandardError, "This should not happen.");
101
+ }
102
+ return 0;
103
+ }
104
+
105
+ #define EWOP_INT_INT_DIV(ltype, rtype) template <> \
106
+ inline ltype ew_op_switch<EW_DIV>( ltype left, rtype right) { \
107
+ if (right == 0) rb_raise(rb_eZeroDivError, "cannot divide type by 0, would throw SIGFPE"); \
108
+ if ((left > 0 && right > 0) || (left < 0 && right < 0)) \
109
+ return left / right; \
110
+ else \
111
+ return ( ltype )(std::floor((double)(left) / (double)(right))); \
112
+ }
113
+
114
+ #define EWOP_UINT_UINT_DIV(ltype, rtype) template <> \
115
+ inline ltype ew_op_switch<EW_DIV>( ltype left, rtype right) { \
116
+ if (right == 0) rb_raise(rb_eZeroDivError, "cannot divide type by 0, would throw SIGFPE"); \
117
+ return left / right; \
118
+ }
119
+
120
+ #define EWOP_INT_UINT_DIV(ltype, rtype) template <> \
121
+ inline ltype ew_op_switch<EW_DIV>( ltype left, rtype right) { \
122
+ if (right == 0) rb_raise(rb_eZeroDivError, "cannot divide type by 0, would throw SIGFPE"); \
123
+ if (left > 0 ) return left / right; \
124
+ else return ( ltype )(std::floor((double)(left) / (double)(right))); \
125
+ }
126
+
127
+ #define EWOP_UINT_INT_DIV(ltype, rtype) template <> \
128
+ inline ltype ew_op_switch<EW_DIV>( ltype left, rtype right) { \
129
+ if (right == 0) rb_raise(rb_eZeroDivError, "cannot divide type by 0, would throw SIGFPE"); \
130
+ if (right > 0) return left / right; \
131
+ else return ( ltype )(std::floor((double)(left) / (double)(right))); \
132
+ }
133
+
134
+ #define EWOP_FLOAT_INT_DIV(ltype, rtype) template <> \
135
+ inline ltype ew_op_switch<EW_DIV>( ltype left, rtype right) { \
136
+ return left / (ltype)(right); \
137
+ }
138
+
139
+ // Ensure that divisions are done in the Ruby way, and that (int)x/0 always raises a Ruby error instead
140
+ // of throwing a SIGFPE.
141
+ EWOP_INT_INT_DIV(int64_t, int64_t)
142
+ EWOP_INT_INT_DIV(int32_t, int32_t)
143
+ EWOP_INT_INT_DIV(int32_t, int64_t)
144
+ EWOP_INT_INT_DIV(int16_t, int16_t)
145
+ EWOP_INT_INT_DIV(int16_t, int32_t)
146
+ EWOP_INT_INT_DIV(int16_t, int64_t)
147
+ EWOP_INT_INT_DIV(int8_t, int8_t)
148
+ EWOP_INT_UINT_DIV(int8_t, u_int8_t)
149
+ EWOP_INT_INT_DIV(int8_t, int16_t)
150
+ EWOP_INT_INT_DIV(int8_t, int32_t)
151
+ EWOP_INT_INT_DIV(int8_t, int64_t)
152
+ EWOP_UINT_UINT_DIV(uint8_t, uint8_t)
153
+ EWOP_UINT_INT_DIV(uint8_t, int8_t)
154
+ EWOP_UINT_INT_DIV(uint8_t, int16_t)
155
+ EWOP_UINT_INT_DIV(uint8_t, int32_t)
156
+ EWOP_UINT_INT_DIV(uint8_t, int64_t)
157
+ EWOP_FLOAT_INT_DIV(float, int8_t)
158
+ EWOP_FLOAT_INT_DIV(float, u_int8_t)
159
+ EWOP_FLOAT_INT_DIV(float, int16_t)
160
+ EWOP_FLOAT_INT_DIV(float, int32_t)
161
+ EWOP_FLOAT_INT_DIV(float, int64_t)
162
+ EWOP_FLOAT_INT_DIV(double, int8_t)
163
+ EWOP_FLOAT_INT_DIV(double, u_int8_t)
164
+ EWOP_FLOAT_INT_DIV(double, int16_t)
165
+ EWOP_FLOAT_INT_DIV(double, int32_t)
166
+ EWOP_FLOAT_INT_DIV(double, int64_t)
167
+
168
+ }
169
+
170
+ #endif // STORAGE_COMMON_H