carray 1.4.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSES +22 -0
  3. data/README.md +18 -14
  4. data/Rakefile +1 -1
  5. data/carray.gemspec +5 -13
  6. data/{ca_iter_block.c → ext/ca_iter_block.c} +13 -13
  7. data/{ca_iter_dimension.c → ext/ca_iter_dimension.c} +16 -16
  8. data/{ca_iter_window.c → ext/ca_iter_window.c} +10 -10
  9. data/{ca_obj_array.c → ext/ca_obj_array.c} +60 -55
  10. data/{ca_obj_bitarray.c → ext/ca_obj_bitarray.c} +12 -12
  11. data/{ca_obj_bitfield.c → ext/ca_obj_bitfield.c} +7 -7
  12. data/{ca_obj_block.c → ext/ca_obj_block.c} +42 -42
  13. data/{ca_obj_fake.c → ext/ca_obj_fake.c} +7 -7
  14. data/{ca_obj_farray.c → ext/ca_obj_farray.c} +18 -18
  15. data/{ca_obj_field.c → ext/ca_obj_field.c} +15 -15
  16. data/{ca_obj_grid.c → ext/ca_obj_grid.c} +27 -27
  17. data/{ca_obj_mapping.c → ext/ca_obj_mapping.c} +9 -9
  18. data/{ca_obj_object.c → ext/ca_obj_object.c} +37 -37
  19. data/{ca_obj_reduce.c → ext/ca_obj_reduce.c} +1 -1
  20. data/{ca_obj_refer.c → ext/ca_obj_refer.c} +33 -33
  21. data/{ca_obj_repeat.c → ext/ca_obj_repeat.c} +43 -43
  22. data/{ca_obj_select.c → ext/ca_obj_select.c} +2 -2
  23. data/{ca_obj_shift.c → ext/ca_obj_shift.c} +23 -23
  24. data/{ca_obj_transpose.c → ext/ca_obj_transpose.c} +23 -23
  25. data/{ca_obj_unbound_repeat.c → ext/ca_obj_unbound_repeat.c} +55 -55
  26. data/{ca_obj_window.c → ext/ca_obj_window.c} +26 -26
  27. data/{carray.h → ext/carray.h} +73 -51
  28. data/{carray_access.c → ext/carray_access.c} +83 -83
  29. data/{carray_attribute.c → ext/carray_attribute.c} +12 -12
  30. data/{carray_call_cfunc.c → ext/carray_call_cfunc.c} +0 -0
  31. data/{carray_cast.c → ext/carray_cast.c} +6 -6
  32. data/{carray_cast_func.rb → ext/carray_cast_func.rb} +0 -0
  33. data/{carray_class.c → ext/carray_class.c} +0 -0
  34. data/{carray_conversion.c → ext/carray_conversion.c} +8 -10
  35. data/{carray_copy.c → ext/carray_copy.c} +19 -19
  36. data/{carray_core.c → ext/carray_core.c} +2 -2
  37. data/ext/carray_data_type.c +66 -0
  38. data/{carray_element.c → ext/carray_element.c} +9 -9
  39. data/{carray_generate.c → ext/carray_generate.c} +0 -0
  40. data/{carray_iterator.c → ext/carray_iterator.c} +40 -38
  41. data/{carray_loop.c → ext/carray_loop.c} +24 -24
  42. data/{carray_mask.c → ext/carray_mask.c} +17 -6
  43. data/{carray_math.rb → ext/carray_math.rb} +3 -3
  44. data/ext/{mathfunc/carray_mathfunc.c → carray_mathfunc.c} +0 -0
  45. data/{carray_numeric.c → ext/carray_numeric.c} +1 -1
  46. data/{carray_operator.c → ext/carray_operator.c} +9 -9
  47. data/{carray_order.c → ext/carray_order.c} +2 -153
  48. data/{carray_sort_addr.c → ext/carray_sort_addr.c} +0 -0
  49. data/{carray_stat.c → ext/carray_stat.c} +5 -5
  50. data/{carray_stat_proc.rb → ext/carray_stat_proc.rb} +23 -23
  51. data/{carray_test.c → ext/carray_test.c} +22 -21
  52. data/{carray_undef.c → ext/carray_undef.c} +0 -0
  53. data/{carray_utils.c → ext/carray_utils.c} +0 -0
  54. data/{extconf.rb → ext/extconf.rb} +0 -5
  55. data/{mkmath.rb → ext/mkmath.rb} +0 -0
  56. data/{ruby_carray.c → ext/ruby_carray.c} +9 -2
  57. data/{ruby_ccomplex.c → ext/ruby_ccomplex.c} +0 -0
  58. data/{ruby_float_func.c → ext/ruby_float_func.c} +0 -0
  59. data/{version.h → ext/version.h} +5 -5
  60. data/{version.rb → ext/version.rb} +0 -0
  61. data/lib/carray.rb +48 -37
  62. data/lib/carray/{base/autoload.rb → autoload.rb} +1 -1
  63. data/lib/carray/autoload/autoload_base.rb +1 -1
  64. data/lib/carray/autoload/autoload_gem_numo_narray.rb +4 -3
  65. data/lib/carray/autoload/autoload_gem_random.rb +8 -0
  66. data/lib/carray/basic.rb +193 -0
  67. data/lib/carray/compose.rb +291 -0
  68. data/lib/carray/construct.rb +445 -0
  69. data/lib/carray/convert.rb +115 -0
  70. data/lib/carray/{base/inspect.rb → inspect.rb} +6 -6
  71. data/lib/carray/io/imagemagick.rb +1 -1
  72. data/lib/carray/{base/iterator.rb → iterator.rb} +3 -3
  73. data/lib/carray/mask.rb +91 -0
  74. data/lib/carray/{base/math.rb → math.rb} +16 -46
  75. data/lib/carray/math/histogram.rb +7 -7
  76. data/lib/carray/mkmf.rb +8 -0
  77. data/lib/carray/object/ca_obj_pack.rb +8 -8
  78. data/lib/carray/obsolete.rb +272 -0
  79. data/lib/carray/ordering.rb +157 -0
  80. data/lib/carray/{base/serialize.rb → serialize.rb} +28 -53
  81. data/lib/carray/{base/string.rb → string.rb} +12 -64
  82. data/lib/carray/{base/struct.rb → struct.rb} +16 -16
  83. data/lib/carray/{io/table.rb → table.rb} +1 -10
  84. data/lib/carray/testing.rb +56 -0
  85. data/lib/carray/time.rb +78 -0
  86. data/lib/carray/transform.rb +100 -0
  87. data/misc/Methods.ja.md +182 -0
  88. data/{NOTE → misc/NOTE} +0 -0
  89. data/test/test_ALL.rb +0 -2
  90. data/test/test_order.rb +7 -7
  91. data/test/test_ref_store.rb +13 -13
  92. data/test/test_stat.rb +7 -15
  93. data/{devel → utils}/guess_shape.rb +0 -0
  94. data/utils/{diff_method.rb → monkey_patch_methods.rb} +17 -7
  95. metadata +88 -151
  96. data/COPYING +0 -56
  97. data/GPL +0 -340
  98. data/LEGAL +0 -50
  99. data/TODO +0 -5
  100. data/carray_random.c +0 -531
  101. data/devel/im2col.rb +0 -17
  102. data/ext/calculus/carray_calculus.c +0 -931
  103. data/ext/calculus/carray_interp.c +0 -358
  104. data/ext/calculus/extconf.rb +0 -12
  105. data/ext/calculus/lib/math/calculus.rb +0 -119
  106. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +0 -31
  107. data/ext/mathfunc/extconf.rb +0 -18
  108. data/ext/mathfunc/test/test_hypot.rb +0 -5
  109. data/ext/mathfunc/test/test_j0.rb +0 -22
  110. data/ext/mathfunc/test/test_jn.rb +0 -8
  111. data/ext/mathfunc/test/test_sph.rb +0 -9
  112. data/lib/carray/autoload/autoload_io_table.rb +0 -1
  113. data/lib/carray/autoload/autoload_math_interp.rb +0 -4
  114. data/lib/carray/base/basic.rb +0 -1146
  115. data/lib/carray/base/obsolete.rb +0 -131
  116. data/lib/carray/math/interp.rb +0 -57
  117. data/lib/carray/math/interp/adapter_gsl_spline.rb +0 -47
  118. data/mt19937ar.c +0 -182
  119. data/mt19937ar.h +0 -86
  120. data/rdoc_main.rb +0 -27
  121. data/rdoc_math.rb +0 -5
  122. data/rdoc_stat.rb +0 -31
  123. data/test/test_narray.rb +0 -64
  124. data/test/test_random.rb +0 -15
  125. data/utils/create_rdoc.sh +0 -9
  126. data/utils/make_tgz.sh +0 -3
@@ -1,17 +0,0 @@
1
- #require "numo/narray"
2
- require "carray"
3
- require "carray-narray"
4
- require "carray-numo-narray"
5
-
6
- im = CArray.int(5,5).seq
7
-
8
- col = CArray.int(9,25)
9
-
10
- x = col.to_na
11
-
12
- y = x.ca.numo
13
-
14
- x[1,nil] = 1
15
-
16
- p x
17
- p y
@@ -1,931 +0,0 @@
1
- /* ---------------------------------------------------------------------------
2
-
3
- carray/carray_calculus.c
4
-
5
- This file is part of Ruby/CArray extension library.
6
- You can redistribute it and/or modify it under the terms of
7
- the Ruby Licence.
8
-
9
- Copyright (C) 2005 Hiroki Motoyoshi
10
-
11
- ---------------------------------------------------------------------------- */
12
-
13
- #include "ruby.h"
14
- #include "carray.h"
15
- #include <math.h>
16
- #include <float.h>
17
-
18
- /* ----------------------------------------------------------------- */
19
-
20
- static double
21
- simpson (double *x, double *y, ca_size_t n)
22
- {
23
- double s;
24
-
25
- if ( n < 2 ) {
26
- return 0.0/0.0;
27
- }
28
- else if ( n == 2 ) {
29
- s = (x[1]-x[0])*(y[1]+y[0])*0.5;
30
- return s;
31
- }
32
- else if ( n % 2 == 0 ) {
33
- double x0, x1, x2, x3;
34
- double h, m, a1, a2, c0, c1, c2, c3;
35
- x0 = x[0];
36
- x1 = x[1];
37
- x2 = x[2];
38
- x3 = x[3];
39
- h = x3 - x0;
40
- m = (x3 + x0)/2;
41
- a1 = x1 - m;
42
- a2 = x2 - m;
43
- c0 = 1.0 + 2.0*a1*a2/((x0-x1)*(x0-x2));
44
- c1 = h*h*a2/((x1-x2)*(x1-x0)*(x1-x3));
45
- c2 = h*h*a1/((x2-x1)*(x2-x0)*(x2-x3));
46
- c3 = 1.0 + 2.0*a1*a2/((x3-x1)*(x3-x2));
47
- s = (c0*y[0]+c1*y[1]+c2*y[2]+c3*y[3])*h/6.0;
48
- if ( n > 4 ) {
49
- s += simpson(x+3, y+3, n-3);
50
- }
51
- return s;
52
- }
53
- else {
54
- double x0, x1, x2;
55
- double h, m, c0, c1, c2;
56
- ca_size_t i;
57
- s = 0.0;
58
- for (i=0; i<n-2; i+=2) {
59
- x0 = x[i];
60
- x1 = x[i+1];
61
- x2 = x[i+2];
62
- h = x2-x0;
63
- m = 0.5*(x2+x0);
64
- c0 = 3.0 - h/(x1-x0);
65
- c1 = h*(x2-x0)/((x2-x1)*(x1-x0));
66
- c2 = 3.0 - h/(x2-x1);
67
- s += (c0*y[i]+c1*y[i+1]+c2*y[i+2])*h/6.0;
68
- }
69
- return s;
70
- }
71
- }
72
-
73
- static VALUE
74
- rb_ca_integrate (volatile VALUE self, volatile VALUE vsc)
75
- {
76
- CArray *sc, *ca;
77
- double ans;
78
-
79
- ca = ca_wrap_readonly(self, CA_DOUBLE);
80
- sc = ca_wrap_readonly(vsc, CA_DOUBLE);
81
-
82
- if ( ca->elements != sc->elements ) {
83
- rb_raise(rb_eRuntimeError, "data num mismatch");
84
- }
85
-
86
- if ( ca_is_any_masked(ca) || ca_is_any_masked(sc) ) {
87
- rb_raise(rb_eRuntimeError,
88
- "can't calculate integrattion when masked elements exist");
89
- }
90
-
91
- ca_attach_n(2, ca, sc);
92
-
93
- ans = simpson((double*)sc->ptr, (double*)ca->ptr, ca->elements);
94
-
95
- ca_detach_n(2, ca, sc);
96
-
97
- return rb_float_new(ans);
98
- }
99
-
100
-
101
- /* ----------------------------------------------------------------- */
102
-
103
- static ca_size_t
104
- linear_index (ca_size_t n, double *y, double yy, double *idx)
105
- {
106
- ca_size_t a, b, c, x1;
107
- double ya, yb, yc;
108
- double y1, y2;
109
- double rest;
110
-
111
- if ( yy <= y[0] ) {
112
- x1 = 0;
113
- goto found;
114
- }
115
-
116
- if ( yy >= y[n-1] ) {
117
- x1 = n-2;
118
- goto found;
119
- }
120
-
121
- /* check for equally spaced scale */
122
-
123
- a = (ca_size_t)((yy-y[0])/(y[n-1]-y[0])*(n-1));
124
-
125
- if ( a >= 0 && a < n-1 ) {
126
- if ( (y[a] - yy) * (y[a+1] - yy) <= 0 ) { /* lucky case */
127
- x1 = a;
128
- goto found;
129
- }
130
- }
131
-
132
- /* binary section method */
133
-
134
- a = 0;
135
- b = n-1;
136
-
137
- ya = y[a];
138
- yb = y[b];
139
-
140
- if ( ya > yb ) {
141
- return -1; /* input scale array should have accending order */
142
- }
143
-
144
- while ( (b - a) >= 1 ) {
145
-
146
- c = (a + b)/2;
147
- yc = y[c];
148
- if ( a == c ) {
149
- break;
150
- }
151
-
152
- if ( yc == yy ) {
153
- a = c;
154
- break;
155
- }
156
- else if ( (ya - yy) * (yc - yy) <= 0 ) {
157
- b = c;
158
- yb = yc;
159
- }
160
- else {
161
- a = c;
162
- ya = yc;
163
- }
164
-
165
- if ( ya > yb ) {
166
- return -1; /* input scale array should have accending order */
167
- }
168
- }
169
-
170
- x1 = a;
171
-
172
- found:
173
-
174
- y1 = y[x1];
175
- y2 = y[x1+1];
176
- rest = (yy-y1)/(y2-y1);
177
-
178
- if ( fabs(y2-yy)/fabs(y2) < DBL_EPSILON*100 ) {
179
- *idx = (double) (x1 + 1);
180
- }
181
- else if ( fabs(y1-yy)/fabs(y1) < DBL_EPSILON*100 ) {
182
- *idx = (double) x1;
183
- }
184
- else {
185
- *idx = rest + (double) x1;
186
- }
187
-
188
- return 0;
189
- }
190
-
191
- static VALUE
192
- rb_ca_binary_search_linear_index (volatile VALUE self, volatile VALUE vx)
193
- {
194
- volatile VALUE out, out0;
195
- CArray *ca, *sc, *cx, *co0, *co;
196
- ca_size_t n;
197
- double *x;
198
- double *px;
199
- double *po;
200
- ca_size_t i;
201
-
202
- Data_Get_Struct(self, CArray, ca);
203
-
204
- if ( rb_ca_is_any_masked(self) ) {
205
- rb_raise(rb_eRuntimeError, "self should not have any masked elements");
206
- }
207
-
208
- sc = ca_wrap_readonly(self, CA_FLOAT64);
209
- cx = ca_wrap_readonly(vx, CA_FLOAT64);
210
-
211
- co0 = carray_new(ca->data_type, cx->rank, cx->dim, 0, NULL);
212
- out = out0 = ca_wrap_struct(co0);
213
- co = ca_wrap_writable(out, CA_FLOAT64);
214
-
215
- ca_attach_n(3, sc, cx, co);
216
-
217
- n = sc->elements;
218
- x = (double*) sc->ptr;
219
- px = (double*) cx->ptr;
220
- po = (double*) co->ptr;
221
-
222
- ca_update_mask(cx);
223
- if ( cx->mask ) {
224
- boolean8_t *mx, *mo;
225
- ca_create_mask(co);
226
- mx = (boolean8_t *) cx->mask->ptr;
227
- mo = (boolean8_t *) co->mask->ptr;
228
- for (i=0; i<cx->elements; i++) {
229
- if ( ! *mx ) {
230
- linear_index(n, x, *px, po);
231
- }
232
- else {
233
- *mo = 1;
234
- }
235
- mx++; mo++; px++, po++;
236
- }
237
- }
238
- else {
239
- for (i=0; i<cx->elements; i++) {
240
- linear_index(n, x, *px, po);
241
- px++, po++;
242
- }
243
- }
244
-
245
- ca_sync(co);
246
- ca_detach_n(3, sc, cx, co);
247
-
248
- if ( rb_ca_is_scalar(vx) ) {
249
- return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
250
- }
251
- else {
252
- return out0;
253
- }
254
- }
255
-
256
- static double
257
- interp_lin (double *x, double *y, double xx)
258
- {
259
- double a, b;
260
- double xa, xb;
261
- double ab;
262
- double fa, fb;
263
- a = x[0];
264
- b = x[1];
265
- fa = y[0];
266
- fb = y[1];
267
- xa = xx - a;
268
- xb = xx - b;
269
- ab = a - b;
270
- return -xa*fb/ab + xb*fa/ab;
271
- }
272
-
273
- static double
274
- deriv_lin (double *x, double *y, double xx)
275
- {
276
- double a, b;
277
- double ab;
278
- double fa, fb;
279
- a = x[0];
280
- b = x[1];
281
- fa = y[0];
282
- fb = y[1];
283
- ab = a - b;
284
- return -fb/ab + fa/ab;
285
- }
286
-
287
- static double
288
- interp_qual (double *x, double *y, double xx)
289
- {
290
- double a, b, c;
291
- double xa, xb, xc;
292
- double ab, bc, ca;
293
- double fa, fb, fc;
294
- a = x[0];
295
- b = x[1];
296
- c = x[2];
297
- fa = y[0];
298
- fb = y[1];
299
- fc = y[2];
300
- xa = xx - a;
301
- xb = xx - b;
302
- xc = xx - c;
303
- ab = a - b;
304
- bc = b - c;
305
- ca = c - a;
306
- return -(xa*xb*fc/ca/bc + xb*xc*fa/ab/ca + xc*xa*fb/bc/ab);
307
- }
308
-
309
- static double
310
- deriv_qual (double *x, double *y, double xx)
311
- {
312
- double a, b, c;
313
- double xa, xb, xc;
314
- double ab, bc, ca;
315
- double fa, fb, fc;
316
- a = x[0];
317
- b = x[1];
318
- c = x[2];
319
- fa = y[0];
320
- fb = y[1];
321
- fc = y[2];
322
- xa = xx - a;
323
- xb = xx - b;
324
- xc = xx - c;
325
- ab = a - b;
326
- bc = b - c;
327
- ca = c - a;
328
- return - (xa+xb)*fc/ca/bc
329
- - (xb+xc)*fa/ab/ca
330
- - (xc+xa)*fb/bc/ab;
331
- }
332
-
333
- static double
334
- interp_cubic (double *x, double *y, double xx)
335
- {
336
- double a, b, c, d;
337
- double xa, xb, xc, xd;
338
- double ab, bc, cd, da, db, ac;
339
- double fa, fb, fc, fd;
340
- a = x[0];
341
- b = x[1];
342
- c = x[2];
343
- d = x[3];
344
- fa = y[0];
345
- fb = y[1];
346
- fc = y[2];
347
- fd = y[3];
348
- xa = xx - a;
349
- xb = xx - b;
350
- xc = xx - c;
351
- xd = xx - d;
352
- ab = a - b;
353
- bc = b - c;
354
- cd = c - d;
355
- da = d - a;
356
- db = d - b;
357
- ac = a - c;
358
- return -xa*xb*xc*fd/da/db/cd - xb*xc*xd*fa/ab/ac/da +
359
- xc*xd*xa*fb/bc/db/ab + xd*xa*xb*fc/cd/ac/bc;
360
- }
361
-
362
- static double
363
- deriv_cubic (double *x, double *y, double xx)
364
- {
365
- double a, b, c, d;
366
- double xa, xb, xc, xd;
367
- double ab, bc, cd, da, db, ac;
368
- double fa, fb, fc, fd;
369
- a = x[0];
370
- b = x[1];
371
- c = x[2];
372
- d = x[3];
373
- fa = y[0];
374
- fb = y[1];
375
- fc = y[2];
376
- fd = y[3];
377
- xa = xx - a;
378
- xb = xx - b;
379
- xc = xx - c;
380
- xd = xx - d;
381
- ab = a - b;
382
- bc = b - c;
383
- cd = c - d;
384
- da = d - a;
385
- db = d - b;
386
- ac = a - c;
387
- return - (xb*xc+xa*xc+xa*xb)*fd/da/db/cd
388
- - (xc*xd+xb*xd+xb*xc)*fa/ab/ac/da
389
- + (xd*xa+xc*xa+xc*xd)*fb/bc/db/ab
390
- + (xa*xb+xd*xb+xd*xa)*fc/cd/ac/bc;
391
- }
392
-
393
- static double
394
- interp_penta (double *x, double *y, double xx)
395
- {
396
- double a, b, c, d, e, f;
397
- double xa, xb, xc, xd, xe, xf;
398
- double ya, yb, yc, yd, ye, yf;
399
- double ab, ac, ad, ae, af;
400
- double bc, bd, be, bf;
401
- double cd, ce, cf;
402
- double de, df;
403
- double ef;
404
- a = x[0];
405
- b = x[1];
406
- c = x[2];
407
- d = x[3];
408
- e = x[4];
409
- f = x[5];
410
- ya = y[0];
411
- yb = y[1];
412
- yc = y[2];
413
- yd = y[3];
414
- ye = y[4];
415
- yf = y[5];
416
- xa = xx - a;
417
- xb = xx - b;
418
- xc = xx - c;
419
- xd = xx - d;
420
- xe = xx - e;
421
- xf = xx - f;
422
- ab = a - b;
423
- ac = a - c;
424
- ad = a - d;
425
- ae = a - e;
426
- af = a - f;
427
- bc = b - c;
428
- bd = b - d;
429
- be = b - e;
430
- bf = b - f;
431
- cd = c - d;
432
- ce = c - e;
433
- cf = c - f;
434
- de = d - e;
435
- df = d - f;
436
- ef = e - f;
437
- return ya*xb*xc*xd*xe*xf/(ab*ac*ad*ae*af)
438
- - xa*yb*xc*xd*xe*xf/(ab*bc*bd*be*bf)
439
- + xa*xb*yc*xd*xe*xf/(ac*bc*cd*ce*cf)
440
- - xa*xb*xc*yd*xe*xf/(ad*bd*cd*de*df)
441
- + xa*xb*xc*xd*ye*xf/(ae*be*ce*de*ef)
442
- - xa*xb*xc*xd*xe*yf/(af*bf*cf*df*ef);
443
- }
444
-
445
-
446
- static double
447
- deriv_penta (double *x, double *y, double xx)
448
- {
449
- double a, b, c, d, e, f;
450
- double xa, xb, xc, xd, xe, xf;
451
- double ya, yb, yc, yd, ye, yf;
452
- double ab, ac, ad, ae, af;
453
- double bc, bd, be, bf;
454
- double cd, ce, cf;
455
- double de, df;
456
- double ef;
457
- a = x[0];
458
- b = x[1];
459
- c = x[2];
460
- d = x[3];
461
- e = x[4];
462
- f = x[5];
463
- ya = y[0];
464
- yb = y[1];
465
- yc = y[2];
466
- yd = y[3];
467
- ye = y[4];
468
- yf = y[5];
469
- xa = xx - a;
470
- xb = xx - b;
471
- xc = xx - c;
472
- xd = xx - d;
473
- xe = xx - e;
474
- xf = xx - f;
475
- ab = a - b;
476
- ac = a - c;
477
- ad = a - d;
478
- ae = a - e;
479
- af = a - f;
480
- bc = b - c;
481
- bd = b - d;
482
- be = b - e;
483
- bf = b - f;
484
- cd = c - d;
485
- ce = c - e;
486
- cf = c - f;
487
- de = d - e;
488
- df = d - f;
489
- ef = e - f;
490
- return (xc*xd*xe*xf+xb*xd*xe*xf+xb*xc*xe*xf+xb*xc*xd*xf+xb*xc*xd*xe)*ya/(ab*ac*ad*ae*af)
491
- - (xc*xd*xe*xf+xa*xd*xe*xf+xa*xc*xe*xf+xa*xc*xd*xf+xa*xc*xd*xe)*yb/(ab*bc*bd*be*bf)
492
- + (xb*xd*xe*xf+xa*xd*xe*xf+xa*xb*xe*xf+xa*xb*xd*xf+xa*xb*xd*xe)*yc/(ac*bc*cd*ce*cf)
493
- - (xb*xc*xe*xf+xa*xc*xe*xf+xa*xb*xe*xf+xa*xb*xc*xf+xa*xb*xc*xe)*yd/(ad*bd*cd*de*df)
494
- + (xb*xc*xd*xf+xa*xc*xd*xf+xa*xb*xd*xf+xa*xb*xc*xf+xa*xb*xc*xd)*ye/(ae*be*ce*de*ef)
495
- - (xb*xc*xd*xe+xa*xc*xd*xe+xa*xb*xd*xe+xa*xb*xc*xe+xa*xb*xc*xd)*yf/(af*bf*cf*df*ef);
496
- }
497
-
498
- static double
499
- interpolate_linear (double *x, double *y, ca_size_t n, double xx)
500
- {
501
- double xt[2];
502
- double ri;
503
- ca_size_t i0;
504
- if ( n == 1) {
505
- return y[0];
506
- }
507
- if ( x != NULL ) {
508
- if ( xx == x[0] ) {
509
- return y[0];
510
- }
511
- if ( xx == x[1] ) {
512
- return y[1];
513
- }
514
- if ( n == 2 ) {
515
- return interp_lin(x, y, xx);
516
- }
517
- linear_index(n, x, xx, &ri);
518
- i0 = floor(ri);
519
- if ( i0 <= 0 ) {
520
- i0 = 0;
521
- }
522
- else if ( i0 + 1 >= n - 1 ) {
523
- i0 = n - 2;
524
- }
525
- return interp_lin(&x[i0], &y[i0], xx);
526
- }
527
- else {
528
- if ( xx == 0 ) {
529
- return y[0];
530
- }
531
- if ( xx == 1 ) {
532
- return y[1];
533
- }
534
- if ( n == 2 ) {
535
- xt[0] = 0.0;
536
- xt[1] = 1.0;
537
- return interp_lin(xt, y, xx);
538
- }
539
- i0 = floor(xx);
540
- if ( i0 <= 0 ) {
541
- i0 = 0;
542
- }
543
- else if ( i0 + 1 >= n - 1 ) {
544
- i0 = n - 2;
545
- }
546
- xt[0] = i0;
547
- xt[1] = i0+1;
548
- return interp_lin(xt, &y[i0], xx);
549
- }
550
- }
551
-
552
- static double
553
- interpolate_cubic (double *x, double *y, ca_size_t n, double xx)
554
- {
555
- static double xt[4];
556
- double ri;
557
- ca_size_t i0;
558
- if ( n == 1) {
559
- return y[0];
560
- }
561
- if ( x != NULL ) {
562
- if ( xx == x[0] ) {
563
- return y[0];
564
- }
565
- if ( xx == x[1] ) {
566
- return y[1];
567
- }
568
- if ( xx == x[2] ) {
569
- return y[2];
570
- }
571
- if ( n == 2 ) {
572
- return interp_lin(x, y, xx);
573
- }
574
- if ( n == 3 ) {
575
- return interp_qual(x, y, xx);
576
- }
577
- linear_index(n, x, xx, &ri);
578
- i0 = floor(ri) - 1;
579
- if ( i0 <= 0 ) {
580
- i0 = 0;
581
- }
582
- else if ( i0 + 3 >= n - 1 ) {
583
- i0 = n - 4;
584
- }
585
- return interp_cubic(&x[i0], &y[i0], xx);
586
- }
587
- else {
588
- if ( xx == 0 ) {
589
- return y[0];
590
- }
591
- if ( xx == 1 ) {
592
- return y[1];
593
- }
594
- if ( xx == 2 ) {
595
- return y[2];
596
- }
597
- if ( n == 2 ) {
598
- xt[0] = 0.0;
599
- xt[1] = 1.0;
600
- return interp_lin(xt, y, xx);
601
- }
602
- if ( n == 3 ) {
603
- xt[0] = 0.0;
604
- xt[1] = 1.0;
605
- xt[2] = 2.0;
606
- return interp_qual(xt, y, xx);
607
- }
608
- ri = xx;
609
- i0 = floor(ri) - 1;
610
- if ( i0 <= 0 ) {
611
- i0 = 0;
612
- }
613
- else if ( i0 + 3 >= n - 1 ) {
614
- i0 = n - 4;
615
- }
616
- xt[0] = i0;
617
- xt[1] = i0+1;
618
- xt[2] = i0+2;
619
- xt[3] = i0+3;
620
- return interp_cubic(xt, &y[i0], xx);
621
- }
622
- }
623
-
624
- static double
625
- differentiate (double *x, double *y, ca_size_t n, double xx)
626
- {
627
- double ri;
628
- ca_size_t i0;
629
- switch ( n ) {
630
- case 1:
631
- return 0.0/0.0;
632
- case 2:
633
- return deriv_lin(x, y, xx);
634
- case 3:
635
- return deriv_qual(x, y, xx);
636
- case 4:
637
- return deriv_cubic(x, y, xx);
638
- }
639
- linear_index(n, x, xx, &ri);
640
- i0 = floor(ri) - 1;
641
- if ( i0 <= 0 ) {
642
- i0 = 0;
643
- }
644
- else if ( n == 5 && i0 + 4 > n - 1 ) {
645
- i0 = n - 5;
646
- }
647
- else if ( i0 + 5 > n - 1 ) {
648
- i0 = n - 6;
649
- }
650
- if ( n == 5 ) {
651
- return deriv_cubic(&x[i0], &y[i0], xx);
652
- }
653
- else {
654
- return deriv_penta(&x[i0], &y[i0], xx);
655
- }
656
- }
657
-
658
- static VALUE
659
- rb_ca_interpolate (int argc, VALUE *argv, VALUE self)
660
- {
661
- volatile VALUE rval = self;
662
- volatile VALUE vsc, vx, ropt, rtype = Qnil, out0, out;
663
- CArray *ca, *sc, *cv, *cx, *co0, *co;
664
- char *typename = NULL;
665
- int type = 0;
666
- double *px, *po;
667
- ca_size_t i;
668
-
669
- Data_Get_Struct(self, CArray, ca);
670
-
671
- rb_scan_args(argc, argv, "21", &vsc, &vx, &ropt);
672
- rb_scan_options(ropt, "type", &rtype);
673
-
674
- if ( ! NIL_P(rtype) ) {
675
- Check_Type(rtype, T_STRING);
676
- typename = StringValuePtr(rtype);
677
- }
678
-
679
- if ( typename == NULL || ! strncmp("cubic", typename, 5) ) {
680
- type = 3;
681
- }
682
- else if ( ! strncmp("linear", typename, 6) ) {
683
- type = 1;
684
- }
685
- else {
686
- volatile VALUE inspect = rb_inspect(rtype);
687
- rb_raise(rb_eRuntimeError,
688
- "invalid interpolation type <%s>", StringValuePtr(inspect));
689
- }
690
-
691
- if ( ! NIL_P(vsc) ) {
692
-
693
- cv = ca_wrap_readonly(rval, CA_DOUBLE);
694
- sc = ca_wrap_readonly(vsc, CA_DOUBLE);
695
-
696
- if ( ca_is_any_masked(cv) || ca_is_any_masked(sc) ) {
697
- rb_raise(rb_eRuntimeError,
698
- "can't calculate interpolation when masked elements exist");
699
- }
700
-
701
- if ( cv->elements != sc->elements ) {
702
- rb_raise(rb_eRuntimeError, "data num mismatch with scale");
703
- }
704
-
705
- cx = ca_wrap_readonly(vx, CA_DOUBLE);
706
-
707
- co0 = carray_new(ca->data_type, cx->rank, cx->dim, 0, NULL);
708
- out = out0 = ca_wrap_struct(co0);
709
- co = ca_wrap_writable(out, CA_DOUBLE);
710
-
711
- ca_attach_n(4, cv, sc, cx, co);
712
-
713
- px = (double*) cx->ptr;
714
- po = (double*) co->ptr;
715
-
716
- ca_update_mask(cx);
717
- if ( cx->mask ) {
718
- boolean8_t *mx, *mo;
719
- ca_create_mask(co);
720
- mx = (boolean8_t *) cx->mask->ptr;
721
- mo = (boolean8_t *) co->mask->ptr;
722
- if ( type == 3 ) {
723
- for (i=0; i<cx->elements; i++) {
724
- if ( ! *mx ) {
725
- *po = interpolate_cubic((double*)sc->ptr, (double*)cv->ptr,
726
- cv->elements, *px);
727
- }
728
- else {
729
- *mo = 1;
730
- }
731
- mx++; mo++; po++; px++;
732
- }
733
- }
734
- else {
735
- for (i=0; i<cx->elements; i++) {
736
- if ( ! *mx ) {
737
- *po = interpolate_linear((double*)sc->ptr, (double*)cv->ptr,
738
- cv->elements, *px);
739
- }
740
- else {
741
- *mo = 1;
742
- }
743
- mx++; mo++; po++; px++;
744
- }
745
- }
746
- }
747
- else {
748
- if ( type == 3 ) {
749
- for (i=0; i<cx->elements; i++) {
750
- *po++ = interpolate_cubic((double*)sc->ptr, (double*)cv->ptr,
751
- cv->elements, *px++);
752
- }
753
- }
754
- else {
755
- for (i=0; i<cx->elements; i++) {
756
- *po++ = interpolate_linear((double*)sc->ptr, (double*)cv->ptr,
757
- cv->elements, *px++);
758
- }
759
- }
760
- }
761
-
762
- ca_sync(co);
763
- ca_detach_n(4, cv, sc, cx, co);
764
-
765
- }
766
- else {
767
-
768
-
769
- cv = ca_wrap_readonly(rval, CA_DOUBLE);
770
-
771
-
772
- if ( ca_is_any_masked(cv) ) {
773
- rb_raise(rb_eRuntimeError,
774
- "can't calculate interpolation when masked elements exist");
775
- }
776
-
777
- cx = ca_wrap_readonly(vx, CA_DOUBLE);
778
-
779
- co0 = carray_new(ca->data_type, cx->rank, cx->dim, 0, NULL);
780
- out = out0 = ca_wrap_struct(co0);
781
- co = ca_wrap_writable(out, CA_DOUBLE);
782
-
783
- ca_attach_n(3, cv, cx, co);
784
-
785
- px = (double*) cx->ptr;
786
- po = (double*) co->ptr;
787
-
788
- ca_update_mask(cx);
789
- if ( cx->mask ) {
790
- boolean8_t *mx, *mo;
791
- ca_create_mask(co);
792
- mx = (boolean8_t *) cx->mask->ptr;
793
- mo = (boolean8_t *) co->mask->ptr;
794
- if ( type == 3 ) {
795
- for (i=0; i<cx->elements; i++) {
796
- if ( ! *mx ) {
797
- *po = interpolate_cubic(NULL, (double*)cv->ptr,
798
- cv->elements, *px);
799
- }
800
- else {
801
- *mo = 1;
802
- }
803
- mx++; mo++; po++; px++;
804
- }
805
- }
806
- else {
807
- for (i=0; i<cx->elements; i++) {
808
- if ( ! *mx ) {
809
- *po = interpolate_linear(NULL, (double*)cv->ptr,
810
- cv->elements, *px);
811
- }
812
- else {
813
- *mo = 1;
814
- }
815
- mx++; mo++; po++; px++;
816
- }
817
- }
818
- }
819
- else {
820
- if ( type == 3 ) {
821
- for (i=0; i<cx->elements; i++) {
822
- *po++ = interpolate_cubic(NULL, (double*)cv->ptr,
823
- cv->elements, *px++);
824
- }
825
- }
826
- else {
827
- for (i=0; i<cx->elements; i++) {
828
- *po++ = interpolate_linear(NULL, (double*)cv->ptr,
829
- cv->elements, *px++);
830
- }
831
- }
832
- }
833
-
834
- ca_sync(co);
835
- ca_detach_n(3, cv, cx, co);
836
-
837
- }
838
-
839
- if ( rb_ca_is_scalar(vx) ) {
840
- return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
841
- }
842
- else {
843
- return out0;
844
- }
845
- }
846
-
847
- static VALUE
848
- rb_ca_differentiate (volatile VALUE self,
849
- volatile VALUE vsc, volatile VALUE vx)
850
- {
851
- volatile VALUE rval = self;
852
- volatile VALUE out0, out;
853
- CArray *ca, *cv, *sc, *cx, *co0, *co;
854
- double *px, *po;
855
- ca_size_t i;
856
-
857
- Data_Get_Struct(self, CArray, ca);
858
-
859
- sc = ca_wrap_readonly(vsc, CA_DOUBLE);
860
-
861
- if ( ca_is_any_masked(ca) || ca_is_any_masked(sc) ) {
862
- rb_raise(rb_eRuntimeError,
863
- "can't calculate differentiation when masked elements exist");
864
- }
865
-
866
- if ( ca->elements != sc->elements ) {
867
- rb_raise(rb_eRuntimeError, "data num mismatch with scale");
868
- }
869
-
870
- cv = ca_wrap_readonly(rval, CA_DOUBLE);
871
- cx = ca_wrap_readonly(vx, CA_DOUBLE);
872
-
873
- co0 = carray_new(ca->data_type, cx->rank, cx->dim, 0, NULL);
874
- out = out0 = ca_wrap_struct(co0);
875
- co = ca_wrap_writable(out, CA_DOUBLE);
876
-
877
- ca_attach_n(4, cv, sc, cx, co);
878
-
879
- px = (double*) cx->ptr;
880
- po = (double*) co->ptr;
881
-
882
- ca_update_mask(cx);
883
- if ( cx->mask ) {
884
- boolean8_t *mx, *mo;
885
- ca_create_mask(co);
886
- mx = (boolean8_t *) cx->mask->ptr;
887
- mo = (boolean8_t *) co->mask->ptr;
888
- for (i=0; i<cx->elements; i++) {
889
- if ( ! *mx ) {
890
- *po = differentiate((double*)sc->ptr, (double*)cv->ptr,
891
- cv->elements, *px);
892
- }
893
- else {
894
- *mo = 1;
895
- }
896
- mx++; mo++; px++, po++;
897
- }
898
- }
899
- else {
900
- for (i=0; i<cx->elements; i++) {
901
- *po = differentiate((double*)sc->ptr, (double*)cv->ptr,
902
- cv->elements, *px);
903
- px++, po++;
904
- }
905
- }
906
-
907
- ca_sync(co);
908
- ca_detach_n(4, cv, sc, cx, co);
909
-
910
- if ( rb_ca_is_scalar(vx) ) {
911
- return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
912
- }
913
- else {
914
- return out0;
915
- }
916
- }
917
-
918
- void
919
- Init_carray_interpolate ();
920
-
921
- void
922
- Init_carray_calculus ()
923
- {
924
- rb_define_method(rb_cCArray, "section",
925
- rb_ca_binary_search_linear_index, 1);
926
- rb_define_method(rb_cCArray, "integrate", rb_ca_integrate, 1);
927
- rb_define_method(rb_cCArray, "interpolate", rb_ca_interpolate, -1);
928
- rb_define_method(rb_cCArray, "differentiate", rb_ca_differentiate, 2);
929
-
930
- Init_carray_interpolate();
931
- }