ruby-gsl-ngx 0.2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +11 -0
- data/History.txt +62 -0
- data/Manifest +33 -0
- data/README.rdoc +14 -0
- data/Rakefile +27 -0
- data/ext/extconf.rb +20 -0
- data/ext/gslng_extensions.cpp +307 -0
- data/ext/plotting.cpp +22 -0
- data/lib/core_extensions/array.rb +14 -0
- data/lib/gslng.rb +11 -0
- data/lib/gslng/backend.rb +23 -0
- data/lib/gslng/backend_components/error_handling.rb +12 -0
- data/lib/gslng/backend_components/matrix.rb +83 -0
- data/lib/gslng/backend_components/rng.rb +22 -0
- data/lib/gslng/backend_components/special.rb +7 -0
- data/lib/gslng/backend_components/stats.rb +36 -0
- data/lib/gslng/backend_components/vector.rb +64 -0
- data/lib/gslng/matrix.rb +528 -0
- data/lib/gslng/matrix_view.rb +47 -0
- data/lib/gslng/plotter.rb +88 -0
- data/lib/gslng/rng/gaussian.rb +34 -0
- data/lib/gslng/rng/rng.rb +28 -0
- data/lib/gslng/rng/uniform.rb +23 -0
- data/lib/gslng/special.rb +22 -0
- data/lib/gslng/vector.rb +553 -0
- data/lib/gslng/vector_view.rb +44 -0
- data/ruby-gsl-ngx.gemspec +22 -0
- data/test/benchmark.rb +113 -0
- data/test/benchmark_results +92 -0
- data/test/matrix_test.rb +145 -0
- data/test/rng_test.rb +27 -0
- data/test/test_gsl.rb +11 -0
- data/test/test_special.rb +21 -0
- data/test/vector_test.rb +145 -0
- metadata +134 -0
data/.gitignore
ADDED
data/History.txt
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
=== 0.2.6
|
2
|
+
* Features:
|
3
|
+
* Improved Vector#wrap
|
4
|
+
* Performance improvements
|
5
|
+
* Check for pkg-config, to avoid problems
|
6
|
+
* Vector#median (bdigital)
|
7
|
+
* Added Vector.linspace, Vector#hash, Matrix#set, Vector#eql?, Matrix#slide, Vector#quantile
|
8
|
+
* Extend Array to include #to_v and #to_m (bdigital)
|
9
|
+
* Fixes:
|
10
|
+
* Better plotter process closing
|
11
|
+
* Clear C/CXXFLAGS
|
12
|
+
* Pointer conversion bug causing random segfaults
|
13
|
+
* Problem while trying to free GSL memory
|
14
|
+
=== 0.2.4.1
|
15
|
+
* Fixes:
|
16
|
+
* Gnuplot not being correctly executed
|
17
|
+
* plotting.cpp didn't compile on some systems
|
18
|
+
=== 0.2.4
|
19
|
+
* Features:
|
20
|
+
* Plotting support (using gnuplot) (Plotter class and associated methods on Matrix).
|
21
|
+
* Added Matrix#map_array (just like Vector#map_array)
|
22
|
+
* C version of operator []
|
23
|
+
* Support for some statistics operations in Vector
|
24
|
+
* Updated documentation to use YARD's @group feature
|
25
|
+
* Internal Changes:
|
26
|
+
* GSL memory is managed by FFI::AutoPointer now
|
27
|
+
* Fixes
|
28
|
+
* On some systems, with Ruby 1.8 the extension was missing libstdc++ support
|
29
|
+
=== 0.2.3
|
30
|
+
* FIXES:
|
31
|
+
* HUGE memory leak, no GSL object was ever freed.
|
32
|
+
* Remove -march flag for MAC
|
33
|
+
* LOTS of improvements:
|
34
|
+
* Added map_array: a map() version that actually returns an Array instead of a Vector/Matrix
|
35
|
+
* Now you use the & idiom for map methods and such (like map_array(&:to_s))
|
36
|
+
* Added Matrix#each_row / Matrix#each_column (which pass row and column matrices to the block)
|
37
|
+
* Added Matrix#row_vecview / Matrix#column_vecview (construct a row and column Matrix::View)
|
38
|
+
* Added Matrix#each_vec_row / Matrix#each_vec_column (same as the previous ones, but the block will receive a Vector::View instead)
|
39
|
+
* Added x, y, z and w accessors for Vector (this will probably go into Vector2, Vector3 and Vector4 in the future)
|
40
|
+
* "negate" operator
|
41
|
+
* Documentation is now YARD based. This will significantly improve the documentation readability.
|
42
|
+
* Added Vector#map_index! and Vector#wrap!
|
43
|
+
* Initial support for Random Number Generators (only Uniform and Gaussian distributions for the moment)
|
44
|
+
* Added Vector#<, #>, #<= and #>=
|
45
|
+
* Added Matrix#map_with_index!
|
46
|
+
* No more #fast_each and stuff. Now #each IS fast. This is accomplished by writing actual Ruby-C code instead of using FFI for
|
47
|
+
these functions.
|
48
|
+
* Initial support for GSL's special functions
|
49
|
+
|
50
|
+
=== 0.2.1
|
51
|
+
* Fixed for FFI 0.6.0
|
52
|
+
|
53
|
+
=== 0.2.0
|
54
|
+
* Completed Vector
|
55
|
+
* Added Matrix (mostly complete)
|
56
|
+
* Added Vector::View and Matrix::View
|
57
|
+
|
58
|
+
=== 0.1.0 / 2009-12-09
|
59
|
+
|
60
|
+
* Initial version
|
61
|
+
* Only class Vector (but completely documented and implemented)
|
62
|
+
|
data/Manifest
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
ext/extconf.rb
|
6
|
+
ext/gslng_extensions.cpp
|
7
|
+
ext/plotting.cpp
|
8
|
+
lib/core_extensions/array.rb
|
9
|
+
lib/gslng.rb
|
10
|
+
lib/gslng/backend.rb
|
11
|
+
lib/gslng/backend_components/error_handling.rb
|
12
|
+
lib/gslng/backend_components/matrix.rb
|
13
|
+
lib/gslng/backend_components/rng.rb
|
14
|
+
lib/gslng/backend_components/special.rb
|
15
|
+
lib/gslng/backend_components/stats.rb
|
16
|
+
lib/gslng/backend_components/vector.rb
|
17
|
+
lib/gslng/matrix.rb
|
18
|
+
lib/gslng/matrix_view.rb
|
19
|
+
lib/gslng/plotter.rb
|
20
|
+
lib/gslng/rng/gaussian.rb
|
21
|
+
lib/gslng/rng/rng.rb
|
22
|
+
lib/gslng/rng/uniform.rb
|
23
|
+
lib/gslng/special.rb
|
24
|
+
lib/gslng/vector.rb
|
25
|
+
lib/gslng/vector_view.rb
|
26
|
+
ruby-gsl-ng.gemspec
|
27
|
+
test/benchmark.rb
|
28
|
+
test/benchmark_results
|
29
|
+
test/matrix_test.rb
|
30
|
+
test/rng_test.rb
|
31
|
+
test/test_gsl.rb
|
32
|
+
test/test_special.rb
|
33
|
+
test/vector_test.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
= Ruby/GSL-ng
|
2
|
+
|
3
|
+
* http://wiki.github.com/v01d/ruby-gsl-ng/
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
* Note: This fork has a fix to a memory leak. Making a separate gem until that's pulled to the master repo.
|
8
|
+
|
9
|
+
New-generation Ruby/GSL wrapper.
|
10
|
+
|
11
|
+
|
12
|
+
== LICENSE:
|
13
|
+
|
14
|
+
GPLv2
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
__END__
|
5
|
+
require 'rubygems'
|
6
|
+
require 'rake'
|
7
|
+
require 'echoe'
|
8
|
+
require 'yard'
|
9
|
+
|
10
|
+
Echoe.new('ruby-gsl-ng') do |p|
|
11
|
+
p.author = 'v01d'
|
12
|
+
p.summary = "Ruby/GSL new-generation wrapper"
|
13
|
+
p.url = "http://github.com/v01d/ruby-gsl-ng"
|
14
|
+
p.version = "0.2.6"
|
15
|
+
p.dependencies = ['yard', 'ffi']
|
16
|
+
# p.eval = proc { s.has_rdoc = 'yard' }
|
17
|
+
end
|
18
|
+
|
19
|
+
Rake::TaskManager.class_eval do
|
20
|
+
def remove_task(task)
|
21
|
+
@tasks.delete(task.to_s)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake.application.remove_task(:docs)
|
26
|
+
YARD::Rake::YardocTask.new(:docs) {|t| t.options = ['--verbose','--no-private','--hide-void']}
|
27
|
+
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require 'mkmf'
|
3
|
+
|
4
|
+
extra_flags='-O4 -DHAVE_INLINE=1'
|
5
|
+
|
6
|
+
## Check for GSL
|
7
|
+
find_executable('pkg-config') or raise 'pkg-config should be installed'
|
8
|
+
gsl_vars = pkg_config('gsl') or raise 'GSL not found!'
|
9
|
+
|
10
|
+
# Configuration
|
11
|
+
if (RUBY_VERSION =~ /^1\.8/) then $libs << ' -lstdc++' end # Seems necessary in some cases
|
12
|
+
|
13
|
+
## Create Makefile
|
14
|
+
with_cppflags("#{extra_flags}") { true }
|
15
|
+
with_cflags('') { true }
|
16
|
+
CONFIG['CXXFLAGS']=''
|
17
|
+
|
18
|
+
create_makefile('gslng_extensions')
|
19
|
+
|
20
|
+
|
@@ -0,0 +1,307 @@
|
|
1
|
+
/**
|
2
|
+
* gsl_extension module: provides some simple C functions in cases where a faster version of some
|
3
|
+
* Ruby method is really necessary.
|
4
|
+
* This could actually a standard C library (not a Ruby extension), but it's easier this way.
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <ruby.h>
|
8
|
+
#include <gsl/gsl_vector.h>
|
9
|
+
#include <gsl/gsl_matrix.h>
|
10
|
+
#include <iostream>
|
11
|
+
using namespace std;
|
12
|
+
|
13
|
+
/************************* Vector functions *****************************/
|
14
|
+
|
15
|
+
static VALUE gsl_vector_map(VALUE self, VALUE ptr) {
|
16
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
17
|
+
for (size_t i = 0; i < v->size; i++)
|
18
|
+
gsl_vector_set(v, i, NUM2DBL(rb_yield(rb_float_new(gsl_vector_get(v, i)))));
|
19
|
+
|
20
|
+
return self;
|
21
|
+
}
|
22
|
+
|
23
|
+
static VALUE gsl_vector_map_index(VALUE self, VALUE ptr) {
|
24
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
25
|
+
for (size_t i = 0; i < v->size; i++) {
|
26
|
+
VALUE vi = ULONG2NUM(i);
|
27
|
+
gsl_vector_set(v, i, NUM2DBL(rb_yield(vi)));
|
28
|
+
}
|
29
|
+
|
30
|
+
return self;
|
31
|
+
}
|
32
|
+
|
33
|
+
static VALUE gsl_vector_each_with_index(VALUE self, VALUE ptr) {
|
34
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
35
|
+
for (size_t i = 0; i < v->size; i++) {
|
36
|
+
VALUE vi = ULONG2NUM(i);
|
37
|
+
rb_yield_values(2, rb_float_new(gsl_vector_get(v, i)), vi);
|
38
|
+
}
|
39
|
+
|
40
|
+
return self;
|
41
|
+
}
|
42
|
+
|
43
|
+
static VALUE gsl_vector_each(VALUE self, VALUE ptr) {
|
44
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
45
|
+
for (size_t i = 0; i < v->size; i++)
|
46
|
+
rb_yield(rb_float_new(gsl_vector_get(v, i)));
|
47
|
+
|
48
|
+
return self;
|
49
|
+
}
|
50
|
+
|
51
|
+
static VALUE gsl_vector_to_a(VALUE self, VALUE ptr) {
|
52
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
53
|
+
|
54
|
+
VALUE array = rb_ary_new2(v->size);
|
55
|
+
for (size_t i = 0; i < v->size; i++)
|
56
|
+
rb_ary_store(array, i, rb_float_new(gsl_vector_get(v, i)));
|
57
|
+
|
58
|
+
return array;
|
59
|
+
}
|
60
|
+
|
61
|
+
static VALUE gsl_vector_from_array(VALUE self, VALUE ptr, VALUE array) {
|
62
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
63
|
+
if (v->size != RARRAY_LEN(array)) rb_raise(rb_eRuntimeError, "Sizes differ!");
|
64
|
+
|
65
|
+
for (size_t i = 0; i < v->size; i++)
|
66
|
+
gsl_vector_set(v, i, NUM2DBL(rb_ary_entry(array, i)));
|
67
|
+
|
68
|
+
return self;
|
69
|
+
}
|
70
|
+
|
71
|
+
static VALUE gsl_vector_get_operator(VALUE self, VALUE ptr, VALUE element) {
|
72
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
73
|
+
long i = FIX2LONG(element);
|
74
|
+
if (i < 0) { i = v->size + i; }
|
75
|
+
return rb_float_new(gsl_vector_get(v, (size_t)i));
|
76
|
+
}
|
77
|
+
|
78
|
+
static VALUE gsl_vector_set_operator(VALUE self, VALUE ptr, VALUE element, VALUE value) {
|
79
|
+
gsl_vector* v = (gsl_vector*)NUM2ULONG(ptr);
|
80
|
+
long i = FIX2LONG(element);
|
81
|
+
if (i < 0) { i = v->size + i; }
|
82
|
+
gsl_vector_set(v, (size_t)i, NUM2DBL(value));
|
83
|
+
return Qnil;
|
84
|
+
}
|
85
|
+
|
86
|
+
static VALUE gsl_vector_eql(VALUE self, VALUE ptr, VALUE other_ptr) {
|
87
|
+
gsl_vector* v1 = (gsl_vector*)NUM2ULONG(ptr);
|
88
|
+
gsl_vector* v2 = (gsl_vector*)NUM2ULONG(other_ptr);
|
89
|
+
if (v1->size != v2->size) return Qfalse;
|
90
|
+
for (size_t i = 0; i < v1->size; i++) {
|
91
|
+
if (gsl_vector_get(v1, i) != gsl_vector_get(v2, i)) return Qfalse;
|
92
|
+
}
|
93
|
+
return Qtrue;
|
94
|
+
}
|
95
|
+
|
96
|
+
// Hide the view in a new vector (gsl_vector_subvector)
|
97
|
+
extern "C" gsl_vector* gsl_vector_subvector_with_stride2(gsl_vector* v, size_t offset, size_t stride, size_t n) {
|
98
|
+
gsl_vector_view view = gsl_vector_subvector_with_stride(v, offset, stride, n);
|
99
|
+
gsl_vector* vector_view = (gsl_vector*) malloc(sizeof(gsl_vector));
|
100
|
+
*vector_view = view.vector;
|
101
|
+
return vector_view;
|
102
|
+
}
|
103
|
+
|
104
|
+
// Hide the view in a new vector (gsl_vector_subvector)
|
105
|
+
extern "C" gsl_vector* gsl_vector_subvector2(gsl_vector* v, size_t offset, size_t n) {
|
106
|
+
gsl_vector_view view = gsl_vector_subvector(v, offset, n);
|
107
|
+
gsl_vector* vector_view = (gsl_vector*) malloc(sizeof(gsl_vector));
|
108
|
+
*vector_view = view.vector;
|
109
|
+
return vector_view;
|
110
|
+
}
|
111
|
+
|
112
|
+
extern "C" double* gsl_vector_as_array(gsl_vector* v) {
|
113
|
+
return v->data;
|
114
|
+
}
|
115
|
+
|
116
|
+
/************************* Matrix functions *****************************/
|
117
|
+
|
118
|
+
static VALUE gsl_matrix_map(VALUE self, VALUE ptr) {
|
119
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
120
|
+
size_t size1 = m->size1;
|
121
|
+
size_t size2 = m->size2;
|
122
|
+
|
123
|
+
for (size_t i = 0; i < size1; i++)
|
124
|
+
for (size_t j = 0; j < size2; j++)
|
125
|
+
gsl_matrix_set(m, i, j, NUM2DBL(rb_yield(rb_float_new(gsl_matrix_get(m, i, j)))));
|
126
|
+
|
127
|
+
return self;
|
128
|
+
}
|
129
|
+
|
130
|
+
static VALUE gsl_matrix_map_array(VALUE self, VALUE ptr) {
|
131
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
132
|
+
|
133
|
+
VALUE array = rb_ary_new2(m->size1);
|
134
|
+
for (size_t i = 0; i < m->size1; i++) {
|
135
|
+
VALUE row = rb_ary_new2(m->size2);
|
136
|
+
for (size_t j = 0; j < m->size2; j++) {
|
137
|
+
rb_ary_store(row, j, rb_yield(rb_float_new(gsl_matrix_get(m, i, j))));
|
138
|
+
}
|
139
|
+
rb_ary_store(array, i, row);
|
140
|
+
}
|
141
|
+
|
142
|
+
return array;
|
143
|
+
}
|
144
|
+
|
145
|
+
static VALUE gsl_matrix_map_index(VALUE self, VALUE ptr) {
|
146
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
147
|
+
size_t size1 = m->size1;
|
148
|
+
size_t size2 = m->size2;
|
149
|
+
|
150
|
+
for (size_t i = 0; i < size1; i++)
|
151
|
+
for (size_t j = 0; j < size2; j++)
|
152
|
+
gsl_matrix_set(m, i, j, NUM2DBL(rb_yield_values(2, ULONG2NUM(i), ULONG2NUM(j))));
|
153
|
+
|
154
|
+
return self;
|
155
|
+
}
|
156
|
+
|
157
|
+
static VALUE gsl_matrix_map_with_index(VALUE self, VALUE ptr) {
|
158
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
159
|
+
size_t size1 = m->size1;
|
160
|
+
size_t size2 = m->size2;
|
161
|
+
|
162
|
+
for (size_t i = 0; i < size1; i++)
|
163
|
+
for (size_t j = 0; j < size2; j++)
|
164
|
+
gsl_matrix_set(m, i, j, NUM2DBL(rb_yield_values(3, rb_float_new(gsl_matrix_get(m, i, j)), ULONG2NUM(i), ULONG2NUM(j))));
|
165
|
+
|
166
|
+
return self;
|
167
|
+
}
|
168
|
+
|
169
|
+
static VALUE gsl_matrix_each(VALUE self, VALUE ptr) {
|
170
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
171
|
+
size_t size1 = m->size1;
|
172
|
+
size_t size2 = m->size2;
|
173
|
+
|
174
|
+
for (size_t i = 0; i < size1; i++)
|
175
|
+
for (size_t j = 0; j < size2; j++)
|
176
|
+
rb_yield(rb_float_new(gsl_matrix_get(m, i, j)));
|
177
|
+
|
178
|
+
return self;
|
179
|
+
}
|
180
|
+
|
181
|
+
static VALUE gsl_matrix_each_with_index(VALUE self, VALUE ptr) {
|
182
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
183
|
+
size_t size1 = m->size1;
|
184
|
+
size_t size2 = m->size2;
|
185
|
+
|
186
|
+
for (size_t i = 0; i < size1; i++) {
|
187
|
+
VALUE vi = ULONG2NUM(i);
|
188
|
+
for (size_t j = 0; j < size2; j++)
|
189
|
+
rb_yield_values(3, rb_float_new(gsl_matrix_get(m, i, j)), vi, ULONG2NUM(j));
|
190
|
+
}
|
191
|
+
|
192
|
+
return self;
|
193
|
+
}
|
194
|
+
|
195
|
+
static VALUE gsl_matrix_to_a(VALUE self, VALUE ptr) {
|
196
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
197
|
+
|
198
|
+
VALUE array = rb_ary_new2(m->size1);
|
199
|
+
for (size_t i = 0; i < m->size1; i++) {
|
200
|
+
VALUE row = rb_ary_new2(m->size2);
|
201
|
+
for (size_t j = 0; j < m->size2; j++) {
|
202
|
+
rb_ary_store(row, j, rb_float_new(gsl_matrix_get(m, i, j)));
|
203
|
+
}
|
204
|
+
rb_ary_store(array, i, row);
|
205
|
+
}
|
206
|
+
|
207
|
+
return array;
|
208
|
+
}
|
209
|
+
|
210
|
+
static VALUE gsl_matrix_from_array(VALUE self, VALUE ptr, VALUE array) {
|
211
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
212
|
+
if (m->size1 != RARRAY_LEN(array)) rb_raise(rb_eRuntimeError, "Sizes differ!");
|
213
|
+
|
214
|
+
for (size_t i = 0; i < m->size1; i++) {
|
215
|
+
VALUE row = rb_ary_entry(array, i);
|
216
|
+
if (m->size2 != RARRAY_LEN(row)) rb_raise(rb_eRuntimeError, "Sizes differ!");
|
217
|
+
|
218
|
+
for (size_t j = 0; j < m->size2; j++)
|
219
|
+
gsl_matrix_set(m, i, j, NUM2DBL(rb_ary_entry(row, j)));
|
220
|
+
}
|
221
|
+
|
222
|
+
return self;
|
223
|
+
}
|
224
|
+
|
225
|
+
static VALUE gsl_matrix_get_operator(VALUE self, VALUE ptr, VALUE element_i, VALUE element_j) {
|
226
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
227
|
+
size_t i = NUM2ULONG(element_i);
|
228
|
+
size_t j = NUM2ULONG(element_j);
|
229
|
+
return rb_float_new(gsl_matrix_get(m, i, j));
|
230
|
+
}
|
231
|
+
|
232
|
+
static VALUE gsl_matrix_set_operator(VALUE self, VALUE ptr, VALUE element_i, VALUE element_j, VALUE value) {
|
233
|
+
gsl_matrix* m = (gsl_matrix*)NUM2ULONG(ptr);
|
234
|
+
size_t i = NUM2ULONG(element_i);
|
235
|
+
size_t j = NUM2ULONG(element_j);
|
236
|
+
gsl_matrix_set(m, i, j, NUM2DBL(value));
|
237
|
+
return Qnil;
|
238
|
+
}
|
239
|
+
|
240
|
+
// Hide the view in a new matrix (gsl_matrix_submatrix)
|
241
|
+
extern "C" gsl_matrix* gsl_matrix_submatrix2(gsl_matrix* m_ptr, size_t x, size_t y, size_t n, size_t m) {
|
242
|
+
gsl_matrix_view view = gsl_matrix_submatrix(m_ptr, x, y, n, m);
|
243
|
+
gsl_matrix* matrix_view = (gsl_matrix*) malloc(sizeof(gsl_matrix));
|
244
|
+
*matrix_view = view.matrix;
|
245
|
+
return matrix_view;
|
246
|
+
}
|
247
|
+
|
248
|
+
extern "C" gsl_vector* gsl_matrix_row_view(gsl_matrix* m_ptr, size_t row, size_t offset, size_t size) {
|
249
|
+
gsl_vector_view view = gsl_matrix_subrow(m_ptr, row, offset, size);
|
250
|
+
gsl_vector* vector_view = (gsl_vector*) malloc(sizeof(gsl_vector));
|
251
|
+
*vector_view = view.vector;
|
252
|
+
return vector_view;
|
253
|
+
}
|
254
|
+
|
255
|
+
extern "C" gsl_vector* gsl_matrix_column_view(gsl_matrix* m_ptr, size_t column, size_t offset, size_t size) {
|
256
|
+
gsl_vector_view view = gsl_matrix_subcolumn(m_ptr, column, offset, size);
|
257
|
+
gsl_vector* vector_view = (gsl_vector*) malloc(sizeof(gsl_vector));
|
258
|
+
*vector_view = view.vector;
|
259
|
+
return vector_view;
|
260
|
+
}
|
261
|
+
|
262
|
+
extern "C" void gsl_matrix_slide(gsl_matrix* m, ssize_t slide_i, ssize_t slide_j)
|
263
|
+
{
|
264
|
+
gsl_matrix* m2 = gsl_matrix_calloc(m->size1, m->size2);
|
265
|
+
|
266
|
+
for (ssize_t i = 0; (size_t)i < m->size1; i++) {
|
267
|
+
for (ssize_t j = 0; (size_t)j < m->size2; j++) {
|
268
|
+
if (i - slide_i >= 0 && (size_t)(i - slide_i) < m->size1 && j - slide_j >= 0 && (size_t)(j - slide_j) < m->size2) {
|
269
|
+
double v = gsl_matrix_get(m, (size_t)(i - slide_i), (size_t)(j - slide_j));
|
270
|
+
gsl_matrix_set(m2, (size_t)i, (size_t)j, v);
|
271
|
+
}
|
272
|
+
}
|
273
|
+
}
|
274
|
+
|
275
|
+
gsl_matrix_memcpy(m, m2);
|
276
|
+
gsl_matrix_free(m2);
|
277
|
+
}
|
278
|
+
|
279
|
+
/************************* Module initialization *****************************/
|
280
|
+
|
281
|
+
extern "C" void Init_gslng_extensions(void) {
|
282
|
+
VALUE GSLng_module = rb_define_module("GSLng");
|
283
|
+
VALUE Backend_module = rb_funcall(GSLng_module, rb_intern("backend"), 0);
|
284
|
+
|
285
|
+
// vector
|
286
|
+
rb_define_module_function(Backend_module, "gsl_vector_get_operator", (VALUE(*)(ANYARGS))gsl_vector_get_operator, 2);
|
287
|
+
rb_define_module_function(Backend_module, "gsl_vector_set_operator", (VALUE(*)(ANYARGS))gsl_vector_set_operator, 3);
|
288
|
+
rb_define_module_function(Backend_module, "gsl_vector_map!", (VALUE(*)(ANYARGS))gsl_vector_map, 1);
|
289
|
+
rb_define_module_function(Backend_module, "gsl_vector_map_index!", (VALUE(*)(ANYARGS))gsl_vector_map_index, 1);
|
290
|
+
rb_define_module_function(Backend_module, "gsl_vector_each_with_index", (VALUE(*)(ANYARGS))gsl_vector_each_with_index, 1);
|
291
|
+
rb_define_module_function(Backend_module, "gsl_vector_each", (VALUE(*)(ANYARGS))gsl_vector_each, 1);
|
292
|
+
rb_define_module_function(Backend_module, "gsl_vector_to_a", (VALUE(*)(ANYARGS))gsl_vector_to_a, 1);
|
293
|
+
rb_define_module_function(Backend_module, "gsl_vector_from_array", (VALUE(*)(ANYARGS))gsl_vector_from_array, 2);
|
294
|
+
rb_define_module_function(Backend_module, "gsl_vector_eql?", (VALUE(*)(ANYARGS))gsl_vector_eql, 2);
|
295
|
+
|
296
|
+
// matrix
|
297
|
+
rb_define_module_function(Backend_module, "gsl_matrix_map!", (VALUE(*)(ANYARGS))gsl_matrix_map, 1);
|
298
|
+
rb_define_module_function(Backend_module, "gsl_matrix_map_array", (VALUE(*)(ANYARGS))gsl_matrix_map_array, 1);
|
299
|
+
rb_define_module_function(Backend_module, "gsl_matrix_map_index!", (VALUE(*)(ANYARGS))gsl_matrix_map_index, 1);
|
300
|
+
rb_define_module_function(Backend_module, "gsl_matrix_map_with_index!", (VALUE(*)(ANYARGS))gsl_matrix_map_with_index, 1);
|
301
|
+
rb_define_module_function(Backend_module, "gsl_matrix_each_with_index", (VALUE(*)(ANYARGS))gsl_matrix_each_with_index, 1);
|
302
|
+
rb_define_module_function(Backend_module, "gsl_matrix_each", (VALUE(*)(ANYARGS))gsl_matrix_each, 1);
|
303
|
+
rb_define_module_function(Backend_module, "gsl_matrix_to_a", (VALUE(*)(ANYARGS))gsl_matrix_to_a, 1);
|
304
|
+
rb_define_module_function(Backend_module, "gsl_matrix_from_array", (VALUE(*)(ANYARGS))gsl_matrix_from_array, 2);
|
305
|
+
rb_define_module_function(Backend_module, "gsl_matrix_get_operator", (VALUE(*)(ANYARGS))gsl_matrix_get_operator, 3);
|
306
|
+
rb_define_module_function(Backend_module, "gsl_matrix_set_operator", (VALUE(*)(ANYARGS))gsl_matrix_set_operator, 4);
|
307
|
+
}
|