linalg 1.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.
- checksums.yaml +7 -0
- data/LICENSE +24 -0
- data/ext/g2c_typedefs.h +5 -0
- data/ext/lapack/extconf.rb +309 -0
- data/ext/lapack/include/BLAS.h +154 -0
- data/ext/lapack/include/LAPACK.h +1314 -0
- data/ext/lapack/main.c +49 -0
- data/ext/lapack/rb_lapack.h +36 -0
- data/ext/lapack/rb_lapack_c.c +11614 -0
- data/ext/lapack/rb_lapack_d.c +12663 -0
- data/ext/lapack/rb_lapack_s.c +12649 -0
- data/ext/lapack/rb_lapack_x.c +208 -0
- data/ext/lapack/rb_lapack_z.c +11614 -0
- data/ext/linalg/dcomplex.c +359 -0
- data/ext/linalg/dcomplex.h +40 -0
- data/ext/linalg/ddata.c +194 -0
- data/ext/linalg/extconf.rb +324 -0
- data/ext/linalg/linalg.c +55 -0
- data/ext/linalg/linalg.h +21 -0
- data/ext/linalg/xdata.c +21 -0
- data/ext/linalg/xdata.h +33 -0
- data/ext/linalg/xmatrix.c.tmpl +1630 -0
- data/ext/linalg/xmatrix.h.tmpl +77 -0
- data/ext/linalg/xmatrixc.c.tmpl +138 -0
- data/ext/linalg/xmatrixr.c.tmpl +130 -0
- data/lib/lapack.rb +87 -0
- data/lib/linalg.rb +9 -0
- data/lib/linalg/dcomplex.rb +17 -0
- data/lib/linalg/dmatrix.rb +29 -0
- data/lib/linalg/dmatrix/alias.rb +32 -0
- data/lib/linalg/dmatrix/cholesky.rb +52 -0
- data/lib/linalg/dmatrix/cond.rb +80 -0
- data/lib/linalg/dmatrix/det.rb +36 -0
- data/lib/linalg/dmatrix/eigen.rb +153 -0
- data/lib/linalg/dmatrix/fit.rb +281 -0
- data/lib/linalg/dmatrix/inverse.rb +78 -0
- data/lib/linalg/dmatrix/lu.rb +120 -0
- data/lib/linalg/dmatrix/main.rb +244 -0
- data/lib/linalg/dmatrix/norms.rb +88 -0
- data/lib/linalg/dmatrix/nullspace.rb +114 -0
- data/lib/linalg/dmatrix/qr.rb +129 -0
- data/lib/linalg/dmatrix/schur.rb +88 -0
- data/lib/linalg/dmatrix/solve.rb +78 -0
- data/lib/linalg/dmatrix/svd.rb +125 -0
- data/lib/linalg/exception.rb +32 -0
- data/lib/linalg/iterators.rb +221 -0
- data/lib/linalg/math.rb +23 -0
- data/lib/linalg/scomplex.rb +15 -0
- data/lib/linalg/version.rb +3 -0
- data/lib/linalg/xdata.rb +123 -0
- metadata +94 -0
@@ -0,0 +1,359 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2004-2008 by James M. Lawrence
|
3
|
+
*
|
4
|
+
* See LICENSE
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include "dcomplex.h"
|
9
|
+
|
10
|
+
/*******************************************************
|
11
|
+
*
|
12
|
+
* globals
|
13
|
+
*
|
14
|
+
*******************************************************/
|
15
|
+
|
16
|
+
VALUE rb_cDComplex ;
|
17
|
+
|
18
|
+
/*******************************************************
|
19
|
+
*
|
20
|
+
* utils
|
21
|
+
*
|
22
|
+
*******************************************************/
|
23
|
+
|
24
|
+
#define get_doublecomplex(ptr, obj) Data_Get_Struct(obj, doublecomplex, ptr)
|
25
|
+
#define wrap_doublecomplex(obj) Data_Wrap_Struct(rb_cDComplex, 0, rb_dcomplex_free, obj)
|
26
|
+
|
27
|
+
/*******************************************************
|
28
|
+
*
|
29
|
+
* alloc/free
|
30
|
+
*
|
31
|
+
*******************************************************/
|
32
|
+
|
33
|
+
void rb_dcomplex_free(doublecomplex* c)
|
34
|
+
{
|
35
|
+
free(c) ;
|
36
|
+
}
|
37
|
+
|
38
|
+
VALUE rb_dcomplex_allocate(VALUE klass)
|
39
|
+
{
|
40
|
+
doublecomplex* c ;
|
41
|
+
VALUE rc = Data_Make_Struct(klass,
|
42
|
+
doublecomplex,
|
43
|
+
0,
|
44
|
+
rb_dcomplex_free,
|
45
|
+
c) ;
|
46
|
+
return rc ;
|
47
|
+
}
|
48
|
+
|
49
|
+
/*******************************************************
|
50
|
+
*
|
51
|
+
* conversions
|
52
|
+
*
|
53
|
+
*******************************************************/
|
54
|
+
|
55
|
+
VALUE rb_dcomplex_new(doublecomplex a)
|
56
|
+
{
|
57
|
+
doublecomplex* c = ALLOC(doublecomplex) ;
|
58
|
+
*c = a ;
|
59
|
+
return wrap_doublecomplex(c) ;
|
60
|
+
}
|
61
|
+
|
62
|
+
doublecomplex rb_num2doublecomplex(VALUE ra)
|
63
|
+
{
|
64
|
+
if( rb_obj_is_kind_of(ra, rb_cDComplex) )
|
65
|
+
{
|
66
|
+
doublecomplex* a ;
|
67
|
+
get_doublecomplex(a, ra) ;
|
68
|
+
return *a ;
|
69
|
+
}
|
70
|
+
else
|
71
|
+
{
|
72
|
+
doublecomplex c ;
|
73
|
+
c.r = (doublereal)NUM2DBL(ra) ;
|
74
|
+
c.i = (doublereal)0.0 ;
|
75
|
+
return c ;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
/*******************************************************
|
80
|
+
*
|
81
|
+
* instance methods
|
82
|
+
*
|
83
|
+
*******************************************************/
|
84
|
+
|
85
|
+
/*
|
86
|
+
*
|
87
|
+
*/
|
88
|
+
VALUE rb_dcomplex_initialize(int argc, VALUE* argv, VALUE self)
|
89
|
+
{
|
90
|
+
VALUE rre ;
|
91
|
+
VALUE rim ;
|
92
|
+
doublecomplex* a ;
|
93
|
+
int n = rb_scan_args(argc, argv, "02", &rre, &rim) ;
|
94
|
+
get_doublecomplex(a, self) ;
|
95
|
+
|
96
|
+
if( n == 2 )
|
97
|
+
{
|
98
|
+
a->r = (doublereal)NUM2DBL(rre) ;
|
99
|
+
a->i = (doublereal)NUM2DBL(rim) ;
|
100
|
+
}
|
101
|
+
else if( n == 1 )
|
102
|
+
{
|
103
|
+
a->r = (doublereal)NUM2DBL(rre) ;
|
104
|
+
a->i = (doublereal)0.0 ;
|
105
|
+
}
|
106
|
+
else
|
107
|
+
{
|
108
|
+
a->r = (doublereal)0.0 ;
|
109
|
+
a->i = (doublereal)0.0 ;
|
110
|
+
}
|
111
|
+
|
112
|
+
return self ;
|
113
|
+
}
|
114
|
+
|
115
|
+
VALUE rb_dcomplex_real(VALUE self)
|
116
|
+
{
|
117
|
+
doublecomplex* a ;
|
118
|
+
get_doublecomplex(a, self) ;
|
119
|
+
return rb_float_new((double)a->r) ;
|
120
|
+
}
|
121
|
+
|
122
|
+
VALUE rb_dcomplex_imag(VALUE self)
|
123
|
+
{
|
124
|
+
doublecomplex* a ;
|
125
|
+
get_doublecomplex(a, self) ;
|
126
|
+
return rb_float_new((double)a->i) ;
|
127
|
+
}
|
128
|
+
|
129
|
+
VALUE rb_dcomplex_add(VALUE self, VALUE rb)
|
130
|
+
{
|
131
|
+
doublecomplex* a ;
|
132
|
+
doublecomplex* c ;
|
133
|
+
VALUE rc ;
|
134
|
+
|
135
|
+
get_doublecomplex(a, self) ;
|
136
|
+
c = ALLOC(doublecomplex) ;
|
137
|
+
rc = wrap_doublecomplex(c) ;
|
138
|
+
|
139
|
+
if( rb_obj_is_kind_of(rb, rb_cDComplex) )
|
140
|
+
{
|
141
|
+
doublecomplex* b ;
|
142
|
+
get_doublecomplex(b, rb) ;
|
143
|
+
c->r = a->r + b->r ;
|
144
|
+
c->i = a->i + b->i ;
|
145
|
+
}
|
146
|
+
else
|
147
|
+
{
|
148
|
+
doublereal b = (doublereal)NUM2DBL(rb) ;
|
149
|
+
c->r = a->r + b ;
|
150
|
+
c->i = a->i ;
|
151
|
+
}
|
152
|
+
|
153
|
+
return rc ;
|
154
|
+
}
|
155
|
+
|
156
|
+
VALUE rb_dcomplex_sub(VALUE self, VALUE rb)
|
157
|
+
{
|
158
|
+
doublecomplex* a ;
|
159
|
+
doublecomplex* c ;
|
160
|
+
VALUE rc ;
|
161
|
+
|
162
|
+
get_doublecomplex(a, self) ;
|
163
|
+
c = ALLOC(doublecomplex) ;
|
164
|
+
rc = wrap_doublecomplex(c) ;
|
165
|
+
|
166
|
+
if( rb_obj_is_kind_of(rb, rb_cDComplex) )
|
167
|
+
{
|
168
|
+
doublecomplex* b ;
|
169
|
+
get_doublecomplex(b, rb) ;
|
170
|
+
c->r = a->r - b->r ;
|
171
|
+
c->i = a->i - b->i ;
|
172
|
+
}
|
173
|
+
else
|
174
|
+
{
|
175
|
+
doublereal b = (doublereal)NUM2DBL(rb) ;
|
176
|
+
c->r = a->r - b ;
|
177
|
+
c->i = a->i ;
|
178
|
+
}
|
179
|
+
|
180
|
+
return rc ;
|
181
|
+
}
|
182
|
+
|
183
|
+
VALUE rb_dcomplex_mul(VALUE self, VALUE rb)
|
184
|
+
{
|
185
|
+
doublecomplex* a ;
|
186
|
+
doublecomplex* c ;
|
187
|
+
VALUE rc ;
|
188
|
+
|
189
|
+
get_doublecomplex(a, self) ;
|
190
|
+
c = ALLOC(doublecomplex) ;
|
191
|
+
rc = wrap_doublecomplex(c) ;
|
192
|
+
|
193
|
+
if( rb_obj_is_kind_of(rb, rb_cDComplex) )
|
194
|
+
{
|
195
|
+
doublecomplex* b ;
|
196
|
+
get_doublecomplex(b, rb) ;
|
197
|
+
c->r = a->r*b->r - a->i*b->i ;
|
198
|
+
c->i = a->r*b->i + a->i*b->r ;
|
199
|
+
}
|
200
|
+
else
|
201
|
+
{
|
202
|
+
doublereal b = (doublereal)NUM2DBL(rb) ;
|
203
|
+
c->r = a->r*b ;
|
204
|
+
c->i = a->i*b ;
|
205
|
+
}
|
206
|
+
|
207
|
+
return rc ;
|
208
|
+
}
|
209
|
+
|
210
|
+
VALUE rb_dcomplex_div(VALUE self, VALUE rb)
|
211
|
+
{
|
212
|
+
doublecomplex* a ;
|
213
|
+
doublecomplex* c ;
|
214
|
+
VALUE rc ;
|
215
|
+
|
216
|
+
get_doublecomplex(a, self) ;
|
217
|
+
c = ALLOC(doublecomplex) ;
|
218
|
+
rc = wrap_doublecomplex(c) ;
|
219
|
+
|
220
|
+
if( rb_obj_is_kind_of(rb, rb_cDComplex) )
|
221
|
+
{
|
222
|
+
doublecomplex* b ;
|
223
|
+
doublereal bottom ;
|
224
|
+
get_doublecomplex(b, rb) ;
|
225
|
+
|
226
|
+
bottom = b->r*b->r + b->i*b->i ;
|
227
|
+
|
228
|
+
c->r = a->r*b->r + a->i*b->i ;
|
229
|
+
c->i = a->i*b->r - a->r*b->i ;
|
230
|
+
|
231
|
+
c->r /= bottom ;
|
232
|
+
c->i /= bottom ;
|
233
|
+
}
|
234
|
+
else
|
235
|
+
{
|
236
|
+
doublereal b = (doublereal)NUM2DBL(rb) ;
|
237
|
+
c->r = a->r/b ;
|
238
|
+
c->i = a->i/b ;
|
239
|
+
}
|
240
|
+
|
241
|
+
return rc ;
|
242
|
+
}
|
243
|
+
|
244
|
+
VALUE rb_dcomplex_within(VALUE self, VALUE repsilon, VALUE rb)
|
245
|
+
{
|
246
|
+
doublecomplex* a ;
|
247
|
+
doublereal epsilon ;
|
248
|
+
|
249
|
+
get_doublecomplex(a, self) ;
|
250
|
+
epsilon = (doublereal)NUM2DBL(repsilon) ;
|
251
|
+
|
252
|
+
if( rb_obj_is_kind_of(rb, rb_cDComplex) )
|
253
|
+
{
|
254
|
+
doublecomplex* b ;
|
255
|
+
get_doublecomplex(b, rb) ;
|
256
|
+
if( fabs(a->r - b->r) > epsilon ||
|
257
|
+
fabs(a->i - b->i) > epsilon )
|
258
|
+
{
|
259
|
+
return Qfalse ;
|
260
|
+
}
|
261
|
+
}
|
262
|
+
else
|
263
|
+
{
|
264
|
+
doublereal b = (doublereal)NUM2DBL(rb) ;
|
265
|
+
if( fabs(a->r - b) > epsilon ||
|
266
|
+
fabs(a->i - (doublereal)0.0) > epsilon )
|
267
|
+
{
|
268
|
+
return Qfalse ;
|
269
|
+
}
|
270
|
+
}
|
271
|
+
|
272
|
+
return Qtrue ;
|
273
|
+
}
|
274
|
+
|
275
|
+
VALUE rb_dcomplex_conj(VALUE self)
|
276
|
+
{
|
277
|
+
doublecomplex* a ;
|
278
|
+
doublecomplex* c ;
|
279
|
+
|
280
|
+
get_doublecomplex(a, self) ;
|
281
|
+
c = ALLOC(doublecomplex) ;
|
282
|
+
|
283
|
+
c->r = a->r ;
|
284
|
+
c->i = -a->i ;
|
285
|
+
|
286
|
+
return wrap_doublecomplex(c) ;
|
287
|
+
}
|
288
|
+
|
289
|
+
VALUE rb_dcomplex_abs(VALUE self)
|
290
|
+
{
|
291
|
+
doublecomplex* c ;
|
292
|
+
get_doublecomplex(c, self) ;
|
293
|
+
return rb_float_new((double)(sqrt(c->r*c->r + c->i*c->i))) ;
|
294
|
+
}
|
295
|
+
|
296
|
+
VALUE rb_dcomplex_abs2(VALUE self)
|
297
|
+
{
|
298
|
+
doublecomplex* c ;
|
299
|
+
get_doublecomplex(c, self) ;
|
300
|
+
return rb_float_new((double)(c->r*c->r + c->i*c->i)) ;
|
301
|
+
}
|
302
|
+
|
303
|
+
VALUE rb_dcomplex_coerce(VALUE self, VALUE other)
|
304
|
+
{
|
305
|
+
return rb_assoc_new(self, other) ;
|
306
|
+
}
|
307
|
+
|
308
|
+
VALUE rb_dcomplex_to_doublecomplex_ptr(VALUE self)
|
309
|
+
{
|
310
|
+
doublecomplex* c ;
|
311
|
+
get_doublecomplex(c, self) ;
|
312
|
+
return ULONG2NUM((unsigned long)c) ;
|
313
|
+
}
|
314
|
+
|
315
|
+
VALUE rb_dcomplex_doubleequals(VALUE self, VALUE rb)
|
316
|
+
{
|
317
|
+
doublecomplex* a ;
|
318
|
+
get_doublecomplex(a, self) ;
|
319
|
+
|
320
|
+
if( rb_obj_is_kind_of(rb, rb_cDComplex) )
|
321
|
+
{
|
322
|
+
doublecomplex* b ;
|
323
|
+
get_doublecomplex(b, rb) ;
|
324
|
+
|
325
|
+
return (a->r == b->r && a->i == b->i) ? Qtrue : Qfalse ;
|
326
|
+
}
|
327
|
+
else
|
328
|
+
{
|
329
|
+
doublereal br = (doublereal)NUM2DBL(rb) ;
|
330
|
+
return (a->r == br && a->i == ((doublereal)0.0)) ? Qtrue : Qfalse ;
|
331
|
+
}
|
332
|
+
}
|
333
|
+
|
334
|
+
void Init_dcomplex()
|
335
|
+
{
|
336
|
+
/* Linalg */
|
337
|
+
rb_cLinalg = rb_define_module("Linalg") ;
|
338
|
+
|
339
|
+
rb_cDComplex = rb_define_class_under(rb_cLinalg, "DComplex", rb_cObject) ;
|
340
|
+
|
341
|
+
rb_define_alloc_func(rb_cDComplex, rb_dcomplex_allocate) ;
|
342
|
+
rb_define_method(rb_cDComplex, "initialize", rb_dcomplex_initialize, -1) ;
|
343
|
+
rb_define_method(rb_cDComplex, "real", rb_dcomplex_real, 0) ;
|
344
|
+
rb_define_method(rb_cDComplex, "imag", rb_dcomplex_imag, 0) ;
|
345
|
+
rb_define_method(rb_cDComplex, "+", rb_dcomplex_add, 1) ;
|
346
|
+
rb_define_method(rb_cDComplex, "-", rb_dcomplex_sub, 1) ;
|
347
|
+
rb_define_method(rb_cDComplex, "*", rb_dcomplex_mul, 1) ;
|
348
|
+
rb_define_method(rb_cDComplex, "/", rb_dcomplex_div, 1) ;
|
349
|
+
rb_define_method(rb_cDComplex, "conj", rb_dcomplex_conj, 0) ;
|
350
|
+
rb_define_method(rb_cDComplex, "abs", rb_dcomplex_abs, 0) ;
|
351
|
+
rb_define_method(rb_cDComplex, "abs2", rb_dcomplex_abs2, 0) ;
|
352
|
+
rb_define_method(rb_cDComplex, "within", rb_dcomplex_within, 2) ;
|
353
|
+
rb_define_method(rb_cDComplex, "coerce", rb_dcomplex_coerce, 1) ;
|
354
|
+
rb_define_method(rb_cDComplex, "to_doublecomplex_ptr",
|
355
|
+
rb_dcomplex_to_doublecomplex_ptr, 0) ;
|
356
|
+
rb_define_method(rb_cDComplex, "==", rb_dcomplex_doubleequals, 1) ;
|
357
|
+
}
|
358
|
+
|
359
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2004-2008 by James M. Lawrence
|
3
|
+
*
|
4
|
+
* See LICENSE
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef DCOMPLEX_H
|
9
|
+
#define DCOMPLEX_H
|
10
|
+
|
11
|
+
#include <stdlib.h>
|
12
|
+
#include <math.h>
|
13
|
+
|
14
|
+
#include <f2c.h>
|
15
|
+
|
16
|
+
#include "ruby.h"
|
17
|
+
|
18
|
+
#include "linalg.h"
|
19
|
+
|
20
|
+
VALUE rb_dcomplex_new(doublecomplex c) ;
|
21
|
+
doublecomplex rb_num2doublecomplex(VALUE num) ;
|
22
|
+
|
23
|
+
VALUE rb_dcomplex_new(doublecomplex a) ;
|
24
|
+
doublecomplex rb_num2doublecomplex(VALUE ra) ;
|
25
|
+
VALUE rb_dcomplex_initialize(int argc, VALUE* argv, VALUE self) ;
|
26
|
+
VALUE rb_dcomplex_real(VALUE self) ;
|
27
|
+
VALUE rb_dcomplex_imag(VALUE self) ;
|
28
|
+
VALUE rb_dcomplex_add(VALUE self, VALUE rb) ;
|
29
|
+
VALUE rb_dcomplex_sub(VALUE self, VALUE rb) ;
|
30
|
+
VALUE rb_dcomplex_mul(VALUE self, VALUE rb) ;
|
31
|
+
VALUE rb_dcomplex_div(VALUE self, VALUE rb) ;
|
32
|
+
VALUE rb_dcomplex_within(VALUE self, VALUE repsilon, VALUE rb) ;
|
33
|
+
VALUE rb_dcomplex_conj(VALUE self) ;
|
34
|
+
VALUE rb_dcomplex_abs(VALUE self) ;
|
35
|
+
VALUE rb_dcomplex_abs2(VALUE self) ;
|
36
|
+
VALUE rb_dcomplex_coerce(VALUE self, VALUE other) ;
|
37
|
+
VALUE rb_dcomplex_to_doublecomplex_ptr(VALUE self) ;
|
38
|
+
VALUE rb_dcomplex_doubleequals(VALUE self, VALUE rb) ;
|
39
|
+
|
40
|
+
#endif
|
data/ext/linalg/ddata.c
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2004-2008 by James M. Lawrence
|
3
|
+
*
|
4
|
+
* See LICENSE
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include "xdata.h"
|
9
|
+
|
10
|
+
struct DData_
|
11
|
+
{
|
12
|
+
doublereal* data ;
|
13
|
+
int size ;
|
14
|
+
} ;
|
15
|
+
|
16
|
+
typedef struct DData_ DData ;
|
17
|
+
|
18
|
+
VALUE rb_cDData ;
|
19
|
+
|
20
|
+
#define get_ddata(ptr, obj) Data_Get_Struct(obj, DData, ptr)
|
21
|
+
#define wrap_ddata(obj) Data_Wrap_Struct(rb_cDData, 0, DData_free, obj)
|
22
|
+
|
23
|
+
void DData_free( DData* a )
|
24
|
+
{
|
25
|
+
free(a->data) ;
|
26
|
+
free(a) ;
|
27
|
+
}
|
28
|
+
|
29
|
+
static VALUE rb_ddata_s_alloc(VALUE klass)
|
30
|
+
{
|
31
|
+
DData* a ;
|
32
|
+
VALUE obj = Data_Make_Struct(klass,
|
33
|
+
DData,
|
34
|
+
0,
|
35
|
+
DData_free,
|
36
|
+
a) ;
|
37
|
+
a->data = 0 ;
|
38
|
+
a->size = 0 ;
|
39
|
+
return obj ;
|
40
|
+
}
|
41
|
+
|
42
|
+
/*
|
43
|
+
* call-seq:
|
44
|
+
* new(size)
|
45
|
+
* new(size) { |i| ... }
|
46
|
+
*
|
47
|
+
* Create a contiguous array of LAPACK type <tt>doublereal</tt> of the
|
48
|
+
* given size, assigning elements to the block. If no block is given,
|
49
|
+
* the data is left uninitialized.
|
50
|
+
*
|
51
|
+
*/
|
52
|
+
VALUE rb_ddata_initialize(VALUE self, VALUE size)
|
53
|
+
{
|
54
|
+
DData* a ;
|
55
|
+
get_ddata(a, self) ;
|
56
|
+
a->size = NUM2INT(size) ;
|
57
|
+
a->data = (doublereal*)ALLOC_N(doublereal, a->size) ;
|
58
|
+
|
59
|
+
if( rb_block_given_p() )
|
60
|
+
{
|
61
|
+
int i ;
|
62
|
+
for( i = 0 ; i != a->size ; ++i )
|
63
|
+
{
|
64
|
+
VALUE rval = rb_yield(INT2FIX(i)) ;
|
65
|
+
a->data[i] = NUM2DBL(rval) ;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
return self ;
|
70
|
+
}
|
71
|
+
|
72
|
+
/*
|
73
|
+
* Array element read
|
74
|
+
*
|
75
|
+
*/
|
76
|
+
VALUE rb_ddata_aref(int argc, VALUE* argv, VALUE self)
|
77
|
+
{
|
78
|
+
DData* a ;
|
79
|
+
VALUE rpos ;
|
80
|
+
int pos ;
|
81
|
+
|
82
|
+
rb_scan_args(argc, argv, "1", &rpos) ;
|
83
|
+
pos = NUM2INT(rpos) ;
|
84
|
+
get_ddata(a, self) ;
|
85
|
+
|
86
|
+
if( pos < 0 || pos >= a->size )
|
87
|
+
{
|
88
|
+
rb_raise(rb_eIndexError, "") ;
|
89
|
+
}
|
90
|
+
|
91
|
+
return rb_float_new((double)a->data[pos]) ;
|
92
|
+
}
|
93
|
+
|
94
|
+
/*
|
95
|
+
* Array element write
|
96
|
+
*
|
97
|
+
*/
|
98
|
+
VALUE rb_ddata_aset(int argc, VALUE* argv, VALUE self)
|
99
|
+
{
|
100
|
+
DData* a ;
|
101
|
+
VALUE rpos ;
|
102
|
+
VALUE rval ;
|
103
|
+
int pos ;
|
104
|
+
|
105
|
+
rb_scan_args(argc, argv, "2", &rpos, &rval) ;
|
106
|
+
pos = NUM2INT(rpos) ;
|
107
|
+
get_ddata(a, self) ;
|
108
|
+
|
109
|
+
if( pos < 0 || pos >= a->size )
|
110
|
+
{
|
111
|
+
rb_raise(rb_eIndexError, "") ;
|
112
|
+
}
|
113
|
+
|
114
|
+
a->data[pos] = NUM2DBL(rval) ;
|
115
|
+
|
116
|
+
return rval ;
|
117
|
+
}
|
118
|
+
|
119
|
+
/*
|
120
|
+
* The entry point into the Lapack module. Returns actual pointer
|
121
|
+
* value of the data.
|
122
|
+
*
|
123
|
+
*/
|
124
|
+
VALUE rb_ddata_to_doublereal_ptr( VALUE self )
|
125
|
+
{
|
126
|
+
DData* a ;
|
127
|
+
get_ddata(a, self) ;
|
128
|
+
return ULONG2NUM((unsigned long)a->data) ;
|
129
|
+
}
|
130
|
+
|
131
|
+
/*
|
132
|
+
* Fill the array with this value.
|
133
|
+
*
|
134
|
+
*/
|
135
|
+
VALUE rb_ddata_fill( VALUE self, VALUE rval )
|
136
|
+
{
|
137
|
+
DData* a ;
|
138
|
+
get_ddata(a, self) ;
|
139
|
+
|
140
|
+
{
|
141
|
+
doublereal val = NUM2DBL(rval) ;
|
142
|
+
doublereal* p = a->data ;
|
143
|
+
doublereal* end = p + a->size ;
|
144
|
+
|
145
|
+
for( ; p != end ; ++p )
|
146
|
+
{
|
147
|
+
*p = val ;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
return self ;
|
152
|
+
}
|
153
|
+
|
154
|
+
/*
|
155
|
+
* For +Enumerable+
|
156
|
+
*
|
157
|
+
*/
|
158
|
+
VALUE rb_ddata_each( VALUE self )
|
159
|
+
{
|
160
|
+
DData* a ;
|
161
|
+
get_ddata(a, self) ;
|
162
|
+
|
163
|
+
{
|
164
|
+
doublereal* p = a->data ;
|
165
|
+
doublereal* end = p + a->size ;
|
166
|
+
|
167
|
+
for( ; p != end ; ++p )
|
168
|
+
{
|
169
|
+
rb_yield(rb_float_new((double)*p)) ;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
return self ;
|
174
|
+
}
|
175
|
+
|
176
|
+
void Init_ddata()
|
177
|
+
{
|
178
|
+
rb_cLinalg = rb_define_module("Linalg") ;
|
179
|
+
rb_cXData = rb_define_module_under(rb_cLinalg, "XData") ;
|
180
|
+
rb_cDData = rb_define_class_under(rb_cXData, "DData", rb_cObject) ;
|
181
|
+
rb_include_module(rb_cDData, rb_mEnumerable) ;
|
182
|
+
|
183
|
+
rb_define_alloc_func(rb_cDData, rb_ddata_s_alloc) ;
|
184
|
+
|
185
|
+
rb_define_method(rb_cDData, "initialize", rb_ddata_initialize, 1) ;
|
186
|
+
rb_define_method(rb_cDData, "[]", rb_ddata_aref, -1) ;
|
187
|
+
rb_define_method(rb_cDData, "[]=", rb_ddata_aset, -1) ;
|
188
|
+
rb_define_method(rb_cDData, "to_doublereal_ptr",
|
189
|
+
rb_ddata_to_doublereal_ptr, 0) ;
|
190
|
+
rb_define_method(rb_cDData, "each", rb_ddata_each, 0) ;
|
191
|
+
rb_define_method(rb_cDData, "fill", rb_ddata_fill, 1) ;
|
192
|
+
}
|
193
|
+
|
194
|
+
|