narray 0.5.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/src/ChangeLog +614 -0
- data/src/MANIFEST +82 -0
- data/src/README.en +54 -0
- data/src/README.ja +63 -0
- data/src/SPEC.en +300 -0
- data/src/SPEC.ja +284 -0
- data/src/depend +14 -0
- data/src/extconf.rb +111 -0
- data/src/lib/narray_ext.rb +211 -0
- data/src/lib/nmatrix.rb +244 -0
- data/src/mkmath.rb +780 -0
- data/src/mknafunc.rb +190 -0
- data/src/mkop.rb +638 -0
- data/src/na_array.c +644 -0
- data/src/na_func.c +1624 -0
- data/src/na_index.c +988 -0
- data/src/na_linalg.c +616 -0
- data/src/na_random.c +409 -0
- data/src/narray.c +1308 -0
- data/src/narray.def +29 -0
- data/src/narray.h +170 -0
- data/src/narray_local.h +210 -0
- data/src/nimage/README.en +38 -0
- data/src/nimage/demo/fits.rb +97 -0
- data/src/nimage/demo/fits_convol.rb +28 -0
- data/src/nimage/demo/fits_fftdemo.rb +27 -0
- data/src/nimage/demo/fitsdemo1.rb +13 -0
- data/src/nimage/demo/fitsdemo2.rb +30 -0
- data/src/nimage/demo/fitsdemo3.rb +26 -0
- data/src/nimage/demo/fitsmorph.rb +39 -0
- data/src/nimage/demo/life_na.rb +57 -0
- data/src/nimage/demo/mandel.rb +41 -0
- data/src/nimage/extconf.rb +12 -0
- data/src/nimage/lib/nimage.rb +51 -0
- data/src/nimage/nimage.c +328 -0
- data/src/speed/add.py +12 -0
- data/src/speed/add.rb +8 -0
- data/src/speed/add_int.py +12 -0
- data/src/speed/add_int.rb +9 -0
- data/src/speed/lu.m +14 -0
- data/src/speed/lu.rb +22 -0
- data/src/speed/mat.m +23 -0
- data/src/speed/mat.rb +28 -0
- data/src/speed/mul.py +12 -0
- data/src/speed/mul.rb +9 -0
- data/src/speed/mul2.py +15 -0
- data/src/speed/mul2.rb +13 -0
- data/src/speed/mul_comp.py +12 -0
- data/src/speed/mul_comp.rb +9 -0
- data/src/speed/mul_int.py +12 -0
- data/src/speed/mul_int.rb +9 -0
- data/src/speed/mybench.py +15 -0
- data/src/speed/mybench.rb +31 -0
- data/src/speed/solve.m +18 -0
- data/src/speed/solve.py +16 -0
- data/src/speed/solve.rb +21 -0
- data/src/test/statistics.rb +22 -0
- data/src/test/testarray.rb +20 -0
- data/src/test/testbit.rb +27 -0
- data/src/test/testcast.rb +14 -0
- data/src/test/testcomplex.rb +35 -0
- data/src/test/testfftw.rb +16 -0
- data/src/test/testindex.rb +11 -0
- data/src/test/testindexary.rb +26 -0
- data/src/test/testindexset.rb +55 -0
- data/src/test/testmask.rb +40 -0
- data/src/test/testmath.rb +48 -0
- data/src/test/testmath2.rb +46 -0
- data/src/test/testmatrix.rb +13 -0
- data/src/test/testmatrix2.rb +33 -0
- data/src/test/testmatrix3.rb +19 -0
- data/src/test/testminmax.rb +46 -0
- data/src/test/testobject.rb +29 -0
- data/src/test/testpow.rb +19 -0
- data/src/test/testrandom.rb +23 -0
- data/src/test/testround.rb +11 -0
- data/src/test/testsort.rb +37 -0
- data/src/test/teststr.rb +13 -0
- data/src/test/testtrans.rb +18 -0
- data/src/test/testwhere.rb +27 -0
- metadata +127 -0
data/src/na_random.c
ADDED
@@ -0,0 +1,409 @@
|
|
1
|
+
/*
|
2
|
+
na_random.c
|
3
|
+
Numerical Array Extention for Ruby
|
4
|
+
(C) Copyright 2003 by Masahiro TANAKA
|
5
|
+
|
6
|
+
This program is free software.
|
7
|
+
You can distribute/modify this program
|
8
|
+
under the same terms as Ruby itself.
|
9
|
+
NO WARRANTY.
|
10
|
+
|
11
|
+
This is based on ruby/random.c
|
12
|
+
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
13
|
+
*/
|
14
|
+
|
15
|
+
/*
|
16
|
+
This is based on trimmed version of MT19937. To get the original version,
|
17
|
+
contact <http://www.math.keio.ac.jp/~matumoto/emt.html>.
|
18
|
+
|
19
|
+
The original copyright notice follows.
|
20
|
+
|
21
|
+
A C-program for MT19937, with initialization improved 2002/2/10.
|
22
|
+
Coded by Takuji Nishimura and Makoto Matsumoto.
|
23
|
+
This is a faster version by taking Shawn Cokus's optimization,
|
24
|
+
Matthe Bellew's simplification, Isaku Wada's real version.
|
25
|
+
|
26
|
+
Before using, initialize the state by using init_genrand(seed)
|
27
|
+
or init_by_array(init_key, key_length).
|
28
|
+
|
29
|
+
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
30
|
+
All rights reserved.
|
31
|
+
|
32
|
+
Redistribution and use in source and binary forms, with or without
|
33
|
+
modification, are permitted provided that the following conditions
|
34
|
+
are met:
|
35
|
+
|
36
|
+
1. Redistributions of source code must retain the above copyright
|
37
|
+
notice, this list of conditions and the following disclaimer.
|
38
|
+
|
39
|
+
2. Redistributions in binary form must reproduce the above copyright
|
40
|
+
notice, this list of conditions and the following disclaimer in the
|
41
|
+
documentation and/or other materials provided with the distribution.
|
42
|
+
|
43
|
+
3. The names of its contributors may not be used to endorse or promote
|
44
|
+
products derived from this software without specific prior written
|
45
|
+
permission.
|
46
|
+
|
47
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
48
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
49
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
50
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
51
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
52
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
53
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
54
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
55
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
56
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
57
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
58
|
+
|
59
|
+
|
60
|
+
Any feedback is very welcome.
|
61
|
+
http://www.math.keio.ac.jp/matumoto/emt.html
|
62
|
+
email: matumoto@math.keio.ac.jp
|
63
|
+
*/
|
64
|
+
#include "ruby.h"
|
65
|
+
#include "narray.h"
|
66
|
+
#include "narray_local.h"
|
67
|
+
|
68
|
+
/* Period parameters */
|
69
|
+
#define N 624
|
70
|
+
#define M 397
|
71
|
+
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
|
72
|
+
#define UMASK 0x80000000UL /* most significant w-r bits */
|
73
|
+
#define LMASK 0x7fffffffUL /* least significant r bits */
|
74
|
+
#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
|
75
|
+
#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
|
76
|
+
|
77
|
+
static u_int32_t state[N]; /* the array for the state vector */
|
78
|
+
static int left = 1;
|
79
|
+
static int initf = 0;
|
80
|
+
static u_int32_t *next;
|
81
|
+
|
82
|
+
/* initializes state[N] with a seed */
|
83
|
+
static void
|
84
|
+
init_genrand(u_int32_t s)
|
85
|
+
{
|
86
|
+
int j;
|
87
|
+
state[0]= s & 0xffffffffUL;
|
88
|
+
for (j=1; j<N; j++) {
|
89
|
+
state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
|
90
|
+
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
91
|
+
/* In the previous versions, MSBs of the seed affect */
|
92
|
+
/* only MSBs of the array state[]. */
|
93
|
+
/* 2002/01/09 modified by Makoto Matsumoto */
|
94
|
+
state[j] &= 0xffffffffUL; /* for >32 bit machines */
|
95
|
+
}
|
96
|
+
left = 1; initf = 1;
|
97
|
+
}
|
98
|
+
|
99
|
+
static void
|
100
|
+
next_state()
|
101
|
+
{
|
102
|
+
u_int32_t *p=state;
|
103
|
+
int j;
|
104
|
+
|
105
|
+
/* if init_genrand() has not been called, */
|
106
|
+
/* a default initial seed is used */
|
107
|
+
if (initf==0) init_genrand(5489UL);
|
108
|
+
|
109
|
+
left = N;
|
110
|
+
next = state;
|
111
|
+
|
112
|
+
for (j=N-M+1; --j; p++)
|
113
|
+
*p = p[M] ^ TWIST(p[0], p[1]);
|
114
|
+
|
115
|
+
for (j=M; --j; p++)
|
116
|
+
*p = p[M-N] ^ TWIST(p[0], p[1]);
|
117
|
+
|
118
|
+
*p = p[M-N] ^ TWIST(p[0], state[0]);
|
119
|
+
}
|
120
|
+
|
121
|
+
#undef N
|
122
|
+
#undef M
|
123
|
+
|
124
|
+
/* These real versions are due to Isaku Wada, 2002/01/09 added */
|
125
|
+
|
126
|
+
#ifdef HAVE_UNISTD_H
|
127
|
+
#include <unistd.h>
|
128
|
+
#endif
|
129
|
+
#include <time.h>
|
130
|
+
#ifdef HAVE_SYS_TIME_H
|
131
|
+
#include <sys/time.h>
|
132
|
+
#endif
|
133
|
+
|
134
|
+
static int first = 1;
|
135
|
+
|
136
|
+
static int
|
137
|
+
rand_init(seed)
|
138
|
+
u_int32_t seed;
|
139
|
+
{
|
140
|
+
static u_int32_t saved_seed;
|
141
|
+
u_int32_t old;
|
142
|
+
|
143
|
+
first = 0;
|
144
|
+
init_genrand(seed);
|
145
|
+
old = saved_seed;
|
146
|
+
saved_seed = seed;
|
147
|
+
|
148
|
+
return old;
|
149
|
+
}
|
150
|
+
|
151
|
+
static u_int32_t
|
152
|
+
random_seed()
|
153
|
+
{
|
154
|
+
static int n = 0;
|
155
|
+
struct timeval tv;
|
156
|
+
|
157
|
+
gettimeofday(&tv, 0);
|
158
|
+
return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++;
|
159
|
+
}
|
160
|
+
|
161
|
+
static VALUE
|
162
|
+
na_s_srand(int argc, VALUE *argv, VALUE obj)
|
163
|
+
{
|
164
|
+
VALUE sd;
|
165
|
+
u_int32_t seed, old;
|
166
|
+
|
167
|
+
rb_secure(4);
|
168
|
+
if (rb_scan_args(argc, argv, "01", &sd) == 0) {
|
169
|
+
seed = random_seed();
|
170
|
+
}
|
171
|
+
else {
|
172
|
+
seed = NUM2ULONG(sd);
|
173
|
+
}
|
174
|
+
old = rand_init(seed);
|
175
|
+
|
176
|
+
return ULONG2NUM(old);
|
177
|
+
}
|
178
|
+
|
179
|
+
/* - end of the code from ruby/random.c - */
|
180
|
+
|
181
|
+
#define genrand(y) \
|
182
|
+
{ if (--left == 0) next_state();\
|
183
|
+
(y) = *next++;\
|
184
|
+
(y) ^= ((y) >> 11);\
|
185
|
+
(y) ^= ((y) << 7) & 0x9d2c5680UL;\
|
186
|
+
(y) ^= ((y) << 15) & 0xefc60000UL;\
|
187
|
+
(y) ^= ((y) >> 18); }
|
188
|
+
|
189
|
+
#define rand_double(x,y) \
|
190
|
+
(((double)((x)>>5)+(double)((y)>>6)*(1.0/67108864.0)) * (1.0/134217728.0))
|
191
|
+
|
192
|
+
#define rand_single(y) \
|
193
|
+
((double)(y) * (1.0/4294967296.0))
|
194
|
+
|
195
|
+
static int n_bits(int32_t a)
|
196
|
+
{
|
197
|
+
int i, x, xu, xl, n=4;
|
198
|
+
int32_t m;
|
199
|
+
|
200
|
+
if (a==0) return 0;
|
201
|
+
if (a<0) a=-a;
|
202
|
+
|
203
|
+
x = 1<<n;
|
204
|
+
xu = 1<<(n+1);
|
205
|
+
xl = 0;
|
206
|
+
|
207
|
+
for (i=n; i>=0; i--) {
|
208
|
+
m = ~((1<<(x-1))-1);
|
209
|
+
|
210
|
+
if (m & a) {
|
211
|
+
xl = x;
|
212
|
+
x += 1<<(i-1);
|
213
|
+
} else {
|
214
|
+
xu = x;
|
215
|
+
x -= 1<<(i-1);
|
216
|
+
}
|
217
|
+
/* printf("%3i, [%3i, %3i], %x\n", i, xu, xl, m1); */
|
218
|
+
}
|
219
|
+
/* if (xu-xl!=1) printf("*** erorr %d - %d != 1\n", xu, xl); */
|
220
|
+
return xl;
|
221
|
+
}
|
222
|
+
|
223
|
+
static int32_t size_check(double rmax, double limit)
|
224
|
+
{
|
225
|
+
if ( rmax == 0 ) {
|
226
|
+
return (int32_t)(limit-1);
|
227
|
+
}
|
228
|
+
if ( rmax < 0 ) {
|
229
|
+
rmax = -rmax;
|
230
|
+
}
|
231
|
+
rmax -= 1;
|
232
|
+
if ( rmax >= limit ) {
|
233
|
+
rb_raise(rb_eArgError, "rand-max(%.0f) must be <= %.0f", rmax+1, limit);
|
234
|
+
}
|
235
|
+
return (int32_t)rmax;
|
236
|
+
}
|
237
|
+
|
238
|
+
static void TpErr(void) {
|
239
|
+
rb_raise(rb_eTypeError,"illegal operation with this type");
|
240
|
+
}
|
241
|
+
|
242
|
+
static void RndB(int n, char *p1, int i1, double rmax)
|
243
|
+
{
|
244
|
+
u_int32_t y;
|
245
|
+
u_int8_t max;
|
246
|
+
int shift;
|
247
|
+
|
248
|
+
if ( rmax < 0 ) {
|
249
|
+
rb_raise(rb_eArgError, "rand-max must be positive");
|
250
|
+
}
|
251
|
+
max = size_check(rmax,0x100);
|
252
|
+
shift = 32 - n_bits(max);
|
253
|
+
|
254
|
+
if (max<1) {
|
255
|
+
for (; n; n--) {
|
256
|
+
*(u_int8_t*)p1 = 0;
|
257
|
+
p1+=i1;
|
258
|
+
}
|
259
|
+
} else {
|
260
|
+
for (; n; n--) {
|
261
|
+
do {
|
262
|
+
genrand(y);
|
263
|
+
y >>= shift;
|
264
|
+
} while (y > max);
|
265
|
+
*(u_int8_t*)p1 = (u_int8_t)y;
|
266
|
+
p1+=i1;
|
267
|
+
}
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
static void RndI(int n, char *p1, int i1, double rmax)
|
272
|
+
{
|
273
|
+
u_int32_t y;
|
274
|
+
int16_t max;
|
275
|
+
int shift, sign=1;
|
276
|
+
|
277
|
+
if ( rmax < 0 ) { rmax = -rmax; sign = -1; }
|
278
|
+
max = size_check(rmax,0x8000);
|
279
|
+
shift = 32 - n_bits(max);
|
280
|
+
|
281
|
+
if (max<1) {
|
282
|
+
for (; n; n--) {
|
283
|
+
*(int16_t*)p1 = 0;
|
284
|
+
p1+=i1;
|
285
|
+
}
|
286
|
+
} else {
|
287
|
+
for (; n; n--) {
|
288
|
+
do {
|
289
|
+
genrand(y);
|
290
|
+
y >>= shift;
|
291
|
+
} while (y > max);
|
292
|
+
*(int16_t*)p1 = (int16_t)y*sign;
|
293
|
+
p1+=i1;
|
294
|
+
}
|
295
|
+
}
|
296
|
+
}
|
297
|
+
|
298
|
+
static void RndL(int n, char *p1, int i1, double rmax)
|
299
|
+
{
|
300
|
+
u_int32_t y;
|
301
|
+
int32_t max;
|
302
|
+
int shift, sign=1;
|
303
|
+
|
304
|
+
if ( rmax < 0 ) { rmax = -rmax; sign = -1; }
|
305
|
+
max = size_check(rmax,0x80000000);
|
306
|
+
shift = 32 - n_bits(max);
|
307
|
+
|
308
|
+
if (max<1) {
|
309
|
+
for (; n; n--) {
|
310
|
+
*(int32_t*)p1 = 0;
|
311
|
+
p1+=i1;
|
312
|
+
}
|
313
|
+
} else {
|
314
|
+
for (; n; n--) {
|
315
|
+
do {
|
316
|
+
genrand(y);
|
317
|
+
y >>= shift;
|
318
|
+
} while (y > max);
|
319
|
+
*(int32_t*)p1 = (int32_t)y*sign;
|
320
|
+
p1+=i1;
|
321
|
+
}
|
322
|
+
}
|
323
|
+
}
|
324
|
+
|
325
|
+
static void RndF(int n, char *p1, int i1, double rmax)
|
326
|
+
{
|
327
|
+
u_int32_t y;
|
328
|
+
|
329
|
+
for (; n; n--) {
|
330
|
+
genrand(y);
|
331
|
+
*(float*)p1 = rand_single(y) * rmax;
|
332
|
+
p1+=i1;
|
333
|
+
}
|
334
|
+
}
|
335
|
+
|
336
|
+
static void RndD(int n, char *p1, int i1, double rmax)
|
337
|
+
{
|
338
|
+
u_int32_t x,y;
|
339
|
+
|
340
|
+
for (; n; n--) {
|
341
|
+
genrand(x);
|
342
|
+
genrand(y);
|
343
|
+
*(double*)p1 = rand_double(x,y) * rmax;
|
344
|
+
p1+=i1;
|
345
|
+
}
|
346
|
+
}
|
347
|
+
|
348
|
+
static void RndX(int n, char *p1, int i1, double rmax)
|
349
|
+
{
|
350
|
+
u_int32_t y;
|
351
|
+
|
352
|
+
for (; n; n--) {
|
353
|
+
genrand(y);
|
354
|
+
((scomplex*)p1)->r = rand_single(y) * rmax;
|
355
|
+
((scomplex*)p1)->i = 0;
|
356
|
+
p1+=i1;
|
357
|
+
}
|
358
|
+
}
|
359
|
+
|
360
|
+
static void RndC(int n, char *p1, int i1, double rmax)
|
361
|
+
{
|
362
|
+
u_int32_t x,y;
|
363
|
+
|
364
|
+
for (; n; n--) {
|
365
|
+
genrand(x);
|
366
|
+
genrand(y);
|
367
|
+
((dcomplex*)p1)->r = rand_double(x,y) * rmax;
|
368
|
+
((dcomplex*)p1)->i = 0;
|
369
|
+
p1+=i1;
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
na_func_t RndFuncs =
|
374
|
+
{ TpErr, RndB, RndI, RndL, RndF, RndD, RndX, RndC, TpErr };
|
375
|
+
|
376
|
+
static VALUE
|
377
|
+
na_random(int argc, VALUE *argv, VALUE self)
|
378
|
+
{
|
379
|
+
VALUE vmax;
|
380
|
+
struct NARRAY *ary;
|
381
|
+
double rmax;
|
382
|
+
|
383
|
+
rb_scan_args(argc, argv, "01", &vmax);
|
384
|
+
if (first) {
|
385
|
+
rand_init(random_seed());
|
386
|
+
}
|
387
|
+
if (NIL_P(vmax)) {
|
388
|
+
rmax = 1;
|
389
|
+
} else {
|
390
|
+
rmax = NUM2DBL(vmax);
|
391
|
+
}
|
392
|
+
if (isinf(rmax) || isnan(rmax)) {
|
393
|
+
rb_raise(rb_eArgError, "rand-max must be regular value");
|
394
|
+
}
|
395
|
+
|
396
|
+
GetNArray(self,ary);
|
397
|
+
|
398
|
+
(*RndFuncs[ary->type])( ary->total, ary->ptr, na_sizeof[ary->type], rmax );
|
399
|
+
|
400
|
+
return self;
|
401
|
+
}
|
402
|
+
|
403
|
+
void
|
404
|
+
Init_na_random()
|
405
|
+
{
|
406
|
+
rb_define_singleton_method(cNArray,"srand",na_s_srand,-1);
|
407
|
+
rb_define_method(cNArray, "random!", na_random,-1);
|
408
|
+
rb_define_alias(cNArray, "random","random!");
|
409
|
+
}
|