ruby-gsl-ng 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,9 @@
1
- === 0.0.1 / 2009-12-09
1
+ === 0.2.0
2
+ * Completed Vector
3
+ * Added Matrix (mostly complete)
4
+ * Added Vector::View and Matrix::View
5
+
6
+ === 0.1.0 / 2009-12-09
2
7
 
3
8
  * Initial version
4
9
  * Only class Vector (but completely documented and implemented)
data/Manifest.txt CHANGED
@@ -5,10 +5,18 @@ README.txt
5
5
  Rakefile
6
6
  ext/extconf.rb
7
7
  ext/gslng_extensions.cpp
8
- lib/gsl.rb
9
- lib/gsl/backend.rb
10
- lib/gsl/finalizer.rb
11
- lib/gsl/vector.rb
8
+ lib/gslng.rb
9
+ lib/gslng/backend.rb
10
+ lib/gslng/backend_components/error_handling.rb
11
+ lib/gslng/backend_components/matrix.rb
12
+ lib/gslng/backend_components/vector.rb
13
+ lib/gslng/finalizer.rb
14
+ lib/gslng/matrix.rb
15
+ lib/gslng/matrix_view.rb
16
+ lib/gslng/vector.rb
17
+ lib/gslng/vector_view.rb
18
+ test/benchmark.rb
19
+ test/matrix_test.rb
12
20
  test/test_gsl.rb
13
21
  test/vector_test.rb
14
22
 
data/README.txt CHANGED
@@ -2,7 +2,10 @@
2
2
 
3
3
  * http://ruby-gsl-ng.googlecode.com
4
4
 
5
- = Description
6
- * New-generation Ruby/GSL wrapper.
5
+ == DESCRIPTION:
7
6
 
7
+ New-generation Ruby/GSL wrapper.
8
8
 
9
+ == LICENSE:
10
+
11
+ GPLv2
data/Rakefile CHANGED
@@ -7,6 +7,7 @@ Hoe.spec('ruby-gsl-ng') do
7
7
  developer('v01d', 'phreakuencies@gmail.com')
8
8
  spec_extras[:extensions] = ["ext/extconf.rb"]
9
9
  extra_deps << [ 'ffi', '>=0' ]
10
+ ENV['NODOT'] = '1'
10
11
  end
11
12
 
12
13
  # vim: syntax=ruby
data/ext/extconf.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/ruby
2
2
  require 'mkmf'
3
3
  gsl_vars = pkg_config('gsl') or raise 'GSL not found!'
4
+ extra_flags='-O3 -march=native'
5
+ with_cppflags("#{$CPPFLAGS} #{extra_flags}") { true }
4
6
  create_makefile('gslng_extensions')
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  #include <gsl/gsl_vector.h>
8
+ #include <gsl/gsl_matrix.h>
8
9
 
9
10
  extern "C" void Init_gslng_extensions(void) { }
10
11
 
@@ -26,3 +27,79 @@ extern "C" void gsl_vector_map_index(gsl_vector* v, gsl_vector_index_callback_t
26
27
  *gsl_vector_ptr(v, i) = (*callback)(i);
27
28
  }
28
29
 
30
+ // A fast "each" for cases where there's no expected return value from the block
31
+ typedef void (*gsl_vector_each_callback_t)(double);
32
+
33
+ extern "C" void gsl_vector_each(gsl_vector* v, gsl_vector_each_callback_t callback) {
34
+ for (size_t i = 0; i < v->size; i++)
35
+ (*callback)(*gsl_vector_const_ptr(v, i));
36
+ }
37
+
38
+ // A fast "each_with_index" for cases where there's no expected return value from the block
39
+ typedef void (*gsl_vector_each_with_index_callback_t)(double, size_t);
40
+
41
+ extern "C" void gsl_vector_each_with_index(gsl_vector* v, gsl_vector_each_with_index_callback_t callback) {
42
+ for (size_t i = 0; i < v->size; i++)
43
+ (*callback)(*gsl_vector_const_ptr(v, i), i);
44
+ }
45
+
46
+ // Hide the view in a new vector (gsl_vector_subvector)
47
+ extern "C" gsl_vector* gsl_vector_subvector_with_stride2(gsl_vector* v, size_t offset, size_t stride, size_t n) {
48
+ gsl_vector_view view = gsl_vector_subvector_with_stride(v, offset, stride, n);
49
+ gsl_vector* vector_view = gsl_vector_alloc(view.vector.size);
50
+ *vector_view = view.vector;
51
+ return vector_view;
52
+ }
53
+
54
+ // Hide the view in a new vector (gsl_vector_subvector)
55
+ extern "C" gsl_vector* gsl_vector_subvector2(gsl_vector* v, size_t offset, size_t n) {
56
+ gsl_vector_view view = gsl_vector_subvector(v, offset, n);
57
+ gsl_vector* vector_view = gsl_vector_alloc(view.vector.size);
58
+ *vector_view = view.vector;
59
+ return vector_view;
60
+ }
61
+
62
+ /***** Matrix *****/
63
+ // For Matrix::map!
64
+ typedef double (*gsl_matrix_callback_t)(double);
65
+
66
+ extern "C" void gsl_matrix_map(gsl_matrix* m, gsl_matrix_callback_t callback) {
67
+ size_t size1 = m->size1;
68
+ size_t size2 = m->size2;
69
+
70
+ for (size_t i = 0; i < size1; i++)
71
+ for (size_t j = 0; j < size2; j++)
72
+ *gsl_matrix_ptr(m, i, j) = (*callback)(*gsl_matrix_const_ptr(m, i, j));
73
+ }
74
+
75
+ // For Matrix::map_index!
76
+ typedef double (*gsl_matrix_index_callback_t)(size_t, size_t);
77
+
78
+ extern "C" void gsl_matrix_map_index(gsl_matrix* m, gsl_matrix_index_callback_t callback) {
79
+ size_t size1 = m->size1;
80
+ size_t size2 = m->size2;
81
+
82
+ for (size_t i = 0; i < size1; i++)
83
+ for (size_t j = 0; j < size2; j++)
84
+ *gsl_matrix_ptr(m, i, j) = (*callback)(i, j);
85
+ }
86
+
87
+ // A fast "each" for cases where there's no expected return value from the block
88
+ typedef void (*gsl_matrix_each_callback_t)(double);
89
+
90
+ extern "C" void gsl_matrix_each(gsl_matrix* m, gsl_matrix_each_callback_t callback) {
91
+ size_t size1 = m->size1;
92
+ size_t size2 = m->size2;
93
+
94
+ for (size_t i = 0; i < size1; i++)
95
+ for (size_t j = 0; j < size2; j++)
96
+ (*callback)(*gsl_matrix_const_ptr(m, i, j));
97
+ }
98
+
99
+ // Hide the view in a new matrix (gsl_matrix_submatrix)
100
+ extern "C" gsl_matrix* gsl_matrix_submatrix2(gsl_matrix* m_ptr, size_t x, size_t y, size_t n, size_t m) {
101
+ gsl_matrix_view view = gsl_matrix_submatrix(m_ptr, x, y, n, m);
102
+ gsl_matrix* matrix_view = gsl_matrix_alloc(view.matrix.size1, view.matrix.size2);
103
+ *matrix_view = view.matrix;
104
+ return matrix_view;
105
+ }
@@ -0,0 +1,19 @@
1
+ require 'ffi'
2
+
3
+ module GSLng
4
+ # Anonymous module: avoids exposing this internal module when doing "include GSLng" at the top-level.
5
+ # If ruby had "private" modules I wouldn't have to do this.
6
+ @backend = Module.new do
7
+ extend FFI::Library
8
+ end
9
+
10
+ # Returns the internal backend module
11
+ def GSLng.backend
12
+ @backend
13
+ end
14
+ end
15
+
16
+ require 'gslng_extensions'
17
+ require 'gslng/backend_components/vector'
18
+ require 'gslng/backend_components/matrix'
19
+ require 'gslng/backend_components/error_handling'
@@ -0,0 +1,12 @@
1
+ module GSLng #:nodoc:
2
+ backend.instance_eval do
3
+ # All of this raises an exception when GSL registers an error
4
+ callback :error_handler_callback, [ :string, :string, :int, :int ], :void
5
+ attach_function :gsl_set_error_handler, [ :error_handler_callback ], :error_handler_callback
6
+
7
+ ErrorHandlerCallback = Proc.new {|reason, file, line, errno|
8
+ raise RuntimeError, "#{reason} (errno: #{errno})", caller[2..-1]
9
+ }
10
+ gsl_set_error_handler(ErrorHandlerCallback)
11
+ end
12
+ end
@@ -0,0 +1,73 @@
1
+ module GSLng
2
+ backend.instance_eval do
3
+ # memory handling
4
+ attach_function :gsl_matrix_alloc, [ :size_t, :size_t ], :pointer
5
+ attach_function :gsl_matrix_calloc, [ :size_t, :size_t ], :pointer
6
+ attach_function :gsl_matrix_free, [ :pointer ], :void
7
+
8
+ # initializing
9
+ attach_function :gsl_matrix_set_all, [ :pointer, :double ], :void
10
+ attach_function :gsl_matrix_set_zero, [ :pointer ], :void
11
+ attach_function :gsl_matrix_set_identity, [ :pointer ], :void
12
+
13
+ # copying
14
+ attach_function :gsl_matrix_memcpy, [ :pointer, :pointer ], :int
15
+
16
+ # operations
17
+ attach_function :gsl_matrix_add, [ :pointer, :pointer ], :int
18
+ attach_function :gsl_matrix_sub, [ :pointer, :pointer ], :int
19
+ attach_function :gsl_matrix_mul_elements, [ :pointer, :pointer ], :int
20
+ attach_function :gsl_matrix_div_elements, [ :pointer, :pointer ], :int
21
+ attach_function :gsl_matrix_scale, [ :pointer, :double ], :int
22
+ attach_function :gsl_matrix_add_constant, [ :pointer, :double ], :int
23
+
24
+ # copy rows/columns into
25
+ attach_function :gsl_matrix_get_row, [ :pointer, :pointer, :size_t ], :int
26
+ attach_function :gsl_matrix_get_col, [ :pointer, :pointer, :size_t ], :int
27
+ attach_function :gsl_matrix_set_row, [ :pointer, :size_t, :pointer ], :int
28
+ attach_function :gsl_matrix_set_col, [ :pointer, :size_t, :pointer ], :int
29
+
30
+ # element access
31
+ attach_function :gsl_matrix_get, [ :pointer, :size_t, :size_t ], :double
32
+ attach_function :gsl_matrix_set, [ :pointer, :size_t, :size_t, :double ], :void
33
+
34
+ # properties
35
+ attach_function :gsl_matrix_isnull, [ :pointer ], :int
36
+ attach_function :gsl_matrix_ispos, [ :pointer ], :int
37
+ attach_function :gsl_matrix_isneg, [ :pointer ], :int
38
+ attach_function :gsl_matrix_isnonneg, [ :pointer ], :int
39
+
40
+ # max and min
41
+ attach_function :gsl_matrix_max, [ :pointer ], :double
42
+ attach_function :gsl_matrix_min, [ :pointer ], :double
43
+ attach_function :gsl_matrix_minmax, [ :pointer, :buffer_out, :buffer_out ], :void
44
+ attach_function :gsl_matrix_max_index, [ :pointer, :buffer_out, :buffer_out ], :void
45
+ attach_function :gsl_matrix_min_index, [ :pointer, :buffer_out, :buffer_out ], :void
46
+ attach_function :gsl_matrix_minmax_index, [ :pointer, :buffer_out, :buffer_out, :buffer_out, :buffer_out ], :void
47
+
48
+ # exchange elements
49
+ attach_function :gsl_matrix_swap_rows, [ :pointer, :size_t, :size_t ], :int
50
+ attach_function :gsl_matrix_swap_columns, [ :pointer, :size_t, :size_t ], :int
51
+ attach_function :gsl_matrix_swap_rowcol, [ :pointer, :size_t, :size_t ], :int
52
+ attach_function :gsl_matrix_transpose_memcpy, [ :pointer, :pointer ], :int
53
+ attach_function :gsl_matrix_transpose, [ :pointer ], :int
54
+
55
+ # From local extension
56
+ callback :gsl_matrix_callback, [ :double ], :double
57
+ attach_function :gsl_matrix_map, [ :pointer, :gsl_matrix_callback ], :void
58
+
59
+ callback :gsl_matrix_index_callback, [ :size_t, :size_t ], :double
60
+ attach_function :gsl_matrix_map_index, [ :pointer, :gsl_matrix_index_callback ], :void
61
+
62
+ callback :gsl_matrix_each_callback, [ :double ], :void
63
+ attach_function :gsl_matrix_each, [ :pointer, :gsl_matrix_each_callback ], :void
64
+
65
+ # views
66
+ attach_function :gsl_matrix_submatrix2, [ :pointer, :size_t, :size_t, :size_t, :size_t ], :pointer
67
+
68
+ # BLAS interface
69
+ enum :cblas_transpose_t, [ :no_transpose, 111, :transpose, :conjugate_transpose ]
70
+ attach_function :gsl_blas_dgemv, [ :cblas_transpose_t, :double, :pointer, :pointer, :double, :pointer ], :int
71
+ attach_function :gsl_blas_dgemm, [ :cblas_transpose_t, :cblas_transpose_t, :double, :pointer, :pointer, :double, :pointer ], :int
72
+ end
73
+ end
@@ -1,22 +1,15 @@
1
- require 'ffi'
2
-
3
- module GSL
4
- # TODO: get a way to properly define the type of size_t (assumed to be :uint)
5
- #
6
- module Backend
7
- extend FFI::Library
8
-
9
- ##----- Vector ------##
10
- # memory handling
1
+ module GSLng
2
+ backend.instance_eval do
3
+ # memory handling
11
4
  attach_function :gsl_vector_alloc, [ :size_t ], :pointer
12
5
  attach_function :gsl_vector_calloc, [ :size_t ], :pointer
13
6
  attach_function :gsl_vector_free, [ :pointer ], :void
14
-
7
+
15
8
  # initializing
16
9
  attach_function :gsl_vector_set_all, [ :pointer, :double ], :void
17
10
  attach_function :gsl_vector_set_zero, [ :pointer ], :void
18
11
  attach_function :gsl_vector_set_basis, [ :pointer, :size_t ], :int
19
-
12
+
20
13
  # operations
21
14
  attach_function :gsl_vector_add, [ :pointer, :pointer ], :int
22
15
  attach_function :gsl_vector_sub, [ :pointer, :pointer ], :int
@@ -24,17 +17,17 @@ module GSL
24
17
  attach_function :gsl_vector_div, [ :pointer, :pointer ], :int
25
18
  attach_function :gsl_vector_scale, [ :pointer, :double ], :int
26
19
  attach_function :gsl_vector_add_constant, [ :pointer, :double ], :int
27
-
20
+
28
21
  # element access
29
22
  attach_function :gsl_vector_get, [ :pointer, :size_t ], :double
30
23
  attach_function :gsl_vector_set, [ :pointer, :size_t, :double ], :void
31
-
24
+
32
25
  # properties
33
26
  attach_function :gsl_vector_isnull, [ :pointer ], :int
34
27
  attach_function :gsl_vector_ispos, [ :pointer ], :int
35
28
  attach_function :gsl_vector_isneg, [ :pointer ], :int
36
29
  attach_function :gsl_vector_isnonneg, [ :pointer ], :int
37
-
30
+
38
31
  # max and min
39
32
  attach_function :gsl_vector_max, [ :pointer ], :double
40
33
  attach_function :gsl_vector_min, [ :pointer ], :double
@@ -42,15 +35,14 @@ module GSL
42
35
  attach_function :gsl_vector_max_index, [ :pointer ], :size_t
43
36
  attach_function :gsl_vector_min_index, [ :pointer ], :size_t
44
37
  attach_function :gsl_vector_minmax_index, [ :pointer, :buffer_out, :buffer_out ], :void
45
-
38
+
46
39
  # copying
47
40
  attach_function :gsl_vector_memcpy, [ :pointer, :pointer ], :int
48
- attach_function :gsl_vector_swap, [ :pointer, :pointer ], :int
49
-
41
+
50
42
  # exchanging elements
51
43
  attach_function :gsl_vector_swap_elements, [ :pointer, :size_t, :size_t ], :int
52
44
  attach_function :gsl_vector_reverse, [ :pointer ], :int
53
-
45
+
54
46
  # BLAS functions
55
47
  attach_function :gsl_blas_ddot, [ :pointer, :pointer, :buffer_out ], :int
56
48
  attach_function :gsl_blas_dnrm2, [ :pointer ], :double
@@ -60,24 +52,24 @@ module GSL
60
52
  attach_function :gsl_blas_daxpy, [ :double, :pointer, :pointer ], :int
61
53
  attach_function :gsl_blas_dscal, [ :double, :pointer ], :void
62
54
 
55
+ # views
56
+ attach_function :gsl_vector_subvector2, [ :pointer, :size_t, :size_t ], :pointer
57
+ attach_function :gsl_vector_subvector_with_stride2, [ :pointer, :size_t, :size_t, :size_t ], :pointer
58
+
63
59
  # From local extension
64
- callback :gsl_vector_callback, [ :double ], :double
60
+ callback :gsl_vector_callback, [ :double ], :double
65
61
  attach_function :gsl_vector_map, [ :pointer, :gsl_vector_callback ], :void
66
62
 
67
63
  callback :gsl_vector_index_callback, [ :size_t ], :double
68
64
  attach_function :gsl_vector_map_index, [ :pointer, :gsl_vector_index_callback ], :void
69
65
 
66
+ callback :gsl_vector_each_callback, [ :double ], :void
67
+ attach_function :gsl_vector_each, [ :pointer, :gsl_vector_each_callback ], :void
68
+
69
+ callback :gsl_vector_each_with_index_callback, [ :double, :size_t ], :void
70
+ attach_function :gsl_vector_each_with_index, [ :pointer, :gsl_vector_each_with_index_callback ], :void
71
+
70
72
  # Sorting
71
73
  attach_function :gsl_sort_vector, [ :pointer ], :void
72
-
73
- ##----- Error handling ------##
74
- callback :error_handler_callback, [ :string, :string, :int, :int ], :void
75
- attach_function :gsl_set_error_handler, [ :error_handler_callback ], :error_handler_callback
76
-
77
- # TODO: pop this block from the exception stack so that it seems to be coming from the original gsl function
78
- ErrorHandlerCallback = Proc.new {|reason, file, line, errno|
79
- raise RuntimeError, "#{reason} at #{file}:#{line} (errno: #{errno})"
80
- }
81
- gsl_set_error_handler(ErrorHandlerCallback)
82
- end
83
- end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ module GSLng
2
+ def GSLng.set_finalizer(obj, func, ptr) #:nodoc:
3
+ ObjectSpace.define_finalizer(obj, lambda {|id| GSLng.backend.send(func, ptr)})
4
+ end
5
+ end