carray 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +56 -0
  3. data/GPL +340 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +33 -0
  6. data/LEGAL +50 -0
  7. data/NOTE +73 -0
  8. data/Rakefile +20 -0
  9. data/TODO +5 -0
  10. data/ca_iter_block.c +242 -0
  11. data/ca_iter_dimension.c +287 -0
  12. data/ca_iter_window.c +202 -0
  13. data/ca_obj_array.c +1189 -0
  14. data/ca_obj_bitarray.c +523 -0
  15. data/ca_obj_bitfield.c +636 -0
  16. data/ca_obj_block.c +885 -0
  17. data/ca_obj_fake.c +405 -0
  18. data/ca_obj_farray.c +482 -0
  19. data/ca_obj_field.c +625 -0
  20. data/ca_obj_grid.c +738 -0
  21. data/ca_obj_mapping.c +614 -0
  22. data/ca_obj_object.c +777 -0
  23. data/ca_obj_reduce.c +299 -0
  24. data/ca_obj_refer.c +627 -0
  25. data/ca_obj_repeat.c +640 -0
  26. data/ca_obj_select.c +558 -0
  27. data/ca_obj_shift.c +952 -0
  28. data/ca_obj_transpose.c +582 -0
  29. data/ca_obj_unbound_repeat.c +557 -0
  30. data/ca_obj_window.c +1023 -0
  31. data/carray.h +1381 -0
  32. data/carray_access.c +1798 -0
  33. data/carray_attribute.c +903 -0
  34. data/carray_call_cfunc.c +1107 -0
  35. data/carray_cast.c +1155 -0
  36. data/carray_cast_func.rb +498 -0
  37. data/carray_class.c +132 -0
  38. data/carray_conversion.c +518 -0
  39. data/carray_copy.c +453 -0
  40. data/carray_core.c +1307 -0
  41. data/carray_element.c +572 -0
  42. data/carray_generate.c +681 -0
  43. data/carray_iterator.c +630 -0
  44. data/carray_loop.c +462 -0
  45. data/carray_mask.c +1174 -0
  46. data/carray_math.rb +834 -0
  47. data/carray_numeric.c +257 -0
  48. data/carray_operator.c +582 -0
  49. data/carray_order.c +1040 -0
  50. data/carray_random.c +529 -0
  51. data/carray_sort_addr.c +261 -0
  52. data/carray_stat.c +2102 -0
  53. data/carray_stat_proc.rb +1990 -0
  54. data/carray_test.c +602 -0
  55. data/carray_undef.c +69 -0
  56. data/carray_utils.c +740 -0
  57. data/ext/calculus/carray_calculus.c +792 -0
  58. data/ext/calculus/carray_interp.c +355 -0
  59. data/ext/calculus/extconf.rb +12 -0
  60. data/ext/calculus/lib/autoload/autoload_math_calculus.rb +2 -0
  61. data/ext/calculus/lib/math/calculus.rb +119 -0
  62. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +31 -0
  63. data/ext/dataframe/API.txt +11 -0
  64. data/ext/dataframe/extconf.rb +3 -0
  65. data/ext/dataframe/lib/carray/autoload/autoload_dataframe_dataframe.rb +14 -0
  66. data/ext/dataframe/lib/carray/dataframe/dataframe.rb +1104 -0
  67. data/ext/dataframe/sample/test_uniq_sort.rb +5 -0
  68. data/ext/fortio/extconf.rb +3 -0
  69. data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +5 -0
  70. data/ext/fortio/lib/carray/io/fortran_format.rb +43 -0
  71. data/ext/fortio/lib/fortio.rb +3 -0
  72. data/ext/fortio/lib/fortio/fortran_format.rb +603 -0
  73. data/ext/fortio/lib/fortio/fortran_format.tab.rb +536 -0
  74. data/ext/fortio/lib/fortio/fortran_format.y +215 -0
  75. data/ext/fortio/lib/fortio/fortran_namelist.rb +151 -0
  76. data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +470 -0
  77. data/ext/fortio/lib/fortio/fortran_namelist.y +213 -0
  78. data/ext/fortio/lib/fortio/fortran_sequential.rb +345 -0
  79. data/ext/fortio/ruby_fortio.c +182 -0
  80. data/ext/fortio/test/test_H.rb +5 -0
  81. data/ext/fortio/test/test_T.rb +7 -0
  82. data/ext/fortio/test/test_fortran_format.rb +86 -0
  83. data/ext/fortio/test/test_namelist.rb +25 -0
  84. data/ext/fortio/test/test_sequential.rb +13 -0
  85. data/ext/fortio/test/test_sequential2.rb +13 -0
  86. data/ext/fortio/work/test.rb +10 -0
  87. data/ext/fortio/work/test_e.rb +19 -0
  88. data/ext/fortio/work/test_ep.rb +10 -0
  89. data/ext/fortio/work/test_parse.rb +12 -0
  90. data/ext/imagemap/carray_imagemap.c +495 -0
  91. data/ext/imagemap/doc/call_graph.dot +28 -0
  92. data/ext/imagemap/draw.c +567 -0
  93. data/ext/imagemap/extconf.rb +13 -0
  94. data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +1 -0
  95. data/ext/imagemap/lib/graphics/imagemap.rb +273 -0
  96. data/ext/imagemap/lib/image_map.rb +4 -0
  97. data/ext/imagemap/test/swath_index.rb +83 -0
  98. data/ext/imagemap/test/swath_warp.rb +99 -0
  99. data/ext/imagemap/test/test.rb +23 -0
  100. data/ext/imagemap/test/test_image.rb +42 -0
  101. data/ext/imagemap/test/test_line.rb +14 -0
  102. data/ext/imagemap/test/test_rotate.rb +17 -0
  103. data/ext/imagemap/test/test_triangle.rb +20 -0
  104. data/ext/imagemap/test/test_warp.rb +26 -0
  105. data/ext/mathfunc/carray_mathfunc.c +321 -0
  106. data/ext/mathfunc/extconf.rb +18 -0
  107. data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +1 -0
  108. data/ext/mathfunc/lib/math/mathfunc.rb +15 -0
  109. data/ext/mathfunc/test/test_hypot.rb +5 -0
  110. data/ext/mathfunc/test/test_j0.rb +22 -0
  111. data/ext/mathfunc/test/test_jn.rb +8 -0
  112. data/ext/mathfunc/test/test_sph.rb +9 -0
  113. data/ext/narray/README +22 -0
  114. data/ext/narray/ca_wrap_narray.c +491 -0
  115. data/ext/narray/carray_narray.c +21 -0
  116. data/ext/narray/extconf.rb +57 -0
  117. data/ext/narray/lib/autoload/autoload_math_narray.rb +1 -0
  118. data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +11 -0
  119. data/ext/narray/lib/math/narray.rb +17 -0
  120. data/ext/narray/lib/math/narray_miss.rb +45 -0
  121. data/extconf.rb +3 -25
  122. data/lib/carray.rb +28 -0
  123. data/lib/carray/autoload/autoload_base.rb +23 -0
  124. data/lib/carray/autoload/autoload_graphics_gnuplot.rb +2 -0
  125. data/lib/carray/autoload/autoload_io_csv.rb +14 -0
  126. data/lib/carray/autoload/autoload_io_excel.rb +5 -0
  127. data/lib/carray/autoload/autoload_io_imagemagick.rb +6 -0
  128. data/lib/carray/autoload/autoload_io_pg.rb +6 -0
  129. data/lib/carray/autoload/autoload_io_sqlite3.rb +12 -0
  130. data/lib/carray/autoload/autoload_io_table.rb +1 -0
  131. data/lib/carray/autoload/autoload_math_histogram.rb +5 -0
  132. data/lib/carray/autoload/autoload_math_interp.rb +4 -0
  133. data/lib/carray/autoload/autoload_math_recurrence.rb +6 -0
  134. data/lib/carray/autoload/autoload_object_iterator.rb +1 -0
  135. data/lib/carray/autoload/autoload_object_link.rb +1 -0
  136. data/lib/carray/autoload/autoload_object_pack.rb +2 -0
  137. data/lib/carray/base/autoload.rb +94 -0
  138. data/lib/carray/base/basic.rb +1051 -0
  139. data/lib/carray/base/inspect.rb +252 -0
  140. data/lib/carray/base/iterator.rb +367 -0
  141. data/lib/carray/base/math.rb +403 -0
  142. data/lib/carray/base/obsolete.rb +93 -0
  143. data/lib/carray/base/serialize.rb +260 -0
  144. data/lib/carray/base/struct.rb +634 -0
  145. data/lib/carray/graphics/gnuplot.rb +2116 -0
  146. data/lib/carray/info.rb +112 -0
  147. data/lib/carray/io/csv.rb +560 -0
  148. data/lib/carray/io/excel.rb +26 -0
  149. data/lib/carray/io/imagemagick.rb +231 -0
  150. data/lib/carray/io/pg.rb +101 -0
  151. data/lib/carray/io/sqlite3.rb +202 -0
  152. data/lib/carray/io/table.rb +77 -0
  153. data/lib/carray/math/histogram.rb +179 -0
  154. data/lib/carray/math/interp.rb +57 -0
  155. data/lib/carray/math/interp/adapter_gsl_spline.rb +47 -0
  156. data/lib/carray/math/recurrence.rb +95 -0
  157. data/lib/carray/mkmf.rb +145 -0
  158. data/lib/carray/object/ca_obj_iterator.rb +52 -0
  159. data/lib/carray/object/ca_obj_link.rb +52 -0
  160. data/lib/carray/object/ca_obj_pack.rb +101 -0
  161. data/mkmath.rb +731 -0
  162. data/mt19937ar.c +182 -0
  163. data/mt19937ar.h +86 -0
  164. data/rdoc_main.rb +27 -0
  165. data/rdoc_math.rb +5 -0
  166. data/rdoc_stat.rb +31 -0
  167. data/ruby_carray.c +242 -0
  168. data/ruby_ccomplex.c +497 -0
  169. data/ruby_float_func.c +83 -0
  170. data/spec/CABlockIterator/CABlockIterator_spec.rb +113 -0
  171. data/spec/CArray/bug/store_spec.rb +27 -0
  172. data/spec/CArray/index/repeat_spec.rb +10 -0
  173. data/spec/CArray/method/eq_spec.rb +80 -0
  174. data/spec/CArray/method/is_nan_spec.rb +12 -0
  175. data/spec/CArray/method/ne_spec.rb +18 -0
  176. data/spec/CArray/method/round_spec.rb +11 -0
  177. data/spec/CArray/object/_attribute_spec.rb +32 -0
  178. data/spec/CArray/object/s_new_spec.rb +31 -0
  179. data/spec/CArray/serialize/Serialization_spec.rb +89 -0
  180. data/spec/spec_all.rb +11 -0
  181. data/test/test_ALL.rb +50 -0
  182. data/test/test_CABitfield.rb +59 -0
  183. data/test/test_CABlock.rb +208 -0
  184. data/test/test_CAField.rb +40 -0
  185. data/test/test_CAGrid.rb +76 -0
  186. data/test/test_CAMapping.rb +106 -0
  187. data/test/test_CAMmap.rb +11 -0
  188. data/test/test_CARefer.rb +94 -0
  189. data/test/test_CARepeat.rb +66 -0
  190. data/test/test_CASelect.rb +23 -0
  191. data/test/test_CAShift.rb +17 -0
  192. data/test/test_CATranspose.rb +61 -0
  193. data/test/test_CAVirtual.rb +214 -0
  194. data/test/test_CAWindow.rb +55 -0
  195. data/test/test_CAWrap.rb +9 -0
  196. data/test/test_CArray.rb +228 -0
  197. data/test/test_CComplex.rb +83 -0
  198. data/test/test_CScalar.rb +91 -0
  199. data/test/test_attribute.rb +281 -0
  200. data/test/test_block_iterator.rb +17 -0
  201. data/test/test_boolean.rb +99 -0
  202. data/test/test_cast.rb +33 -0
  203. data/test/test_class.rb +85 -0
  204. data/test/test_complex.rb +43 -0
  205. data/test/test_composite.rb +125 -0
  206. data/test/test_convert.rb +79 -0
  207. data/test/test_copy.rb +141 -0
  208. data/test/test_creation.rb +85 -0
  209. data/test/test_element.rb +146 -0
  210. data/test/test_extream.rb +55 -0
  211. data/test/test_generate.rb +75 -0
  212. data/test/test_index.rb +71 -0
  213. data/test/test_mask.rb +578 -0
  214. data/test/test_math.rb +98 -0
  215. data/test/test_narray.rb +64 -0
  216. data/test/test_order.rb +147 -0
  217. data/test/test_random.rb +15 -0
  218. data/test/test_ref_store.rb +211 -0
  219. data/test/test_stat.rb +414 -0
  220. data/test/test_struct.rb +72 -0
  221. data/test/test_virtual.rb +49 -0
  222. data/utils/ca_ase.rb +21 -0
  223. data/utils/ca_methods.rb +15 -0
  224. data/utils/cast_checker.rb +30 -0
  225. data/utils/create_rdoc.sh +9 -0
  226. data/utils/diff_method.rb +52 -0
  227. data/utils/extract_rdoc.rb +27 -0
  228. data/utils/make_tgz.sh +3 -0
  229. data/utils/remove_resource_fork.sh +5 -0
  230. data/version.h +3 -3
  231. metadata +266 -1
@@ -0,0 +1,23 @@
1
+ require 'carray'
2
+
3
+ #sc = ImageMap::ColorScale.pm3d(13,10,14,256,:interpolation => false)
4
+ #sc = ImageMap::ColorScale.GMT("GMT_globe.cpt")
5
+ #sc.set_range(0..Math::PI)
6
+ #sc.set_upper_color(127,127,127)
7
+ #sc.set_lower_color(127,127,127)
8
+ #sc.set_mask_color( 127,127,127)
9
+
10
+ ca = CArray.float(640, 480)
11
+ ca[:i,nil].scale!(-0.1, Math::PI+0.1)
12
+ ca.mask = 0
13
+ ca.mask[90..110,90..110] = 1
14
+
15
+ if true
16
+ im = ImageMap.create(ca, sc)
17
+ require 'carray/RMagick'
18
+ il = Magick::Image.new_from_ca(im)
19
+ il.display
20
+ else
21
+ im = ImageMap.display(ca, sc)
22
+ end
23
+
@@ -0,0 +1,42 @@
1
+ require 'carray'
2
+ require 'carray/image_map'
3
+ require 'benchmark'
4
+
5
+ lon = CArray.float(37).span!(-90..180)
6
+ lat = CArray.float(12).span!(0..90)
7
+
8
+ u = lon[:%,lat.dim0]
9
+ v = lat[lon.dim0,:%]
10
+ val = u.rad.sin! + v.rad
11
+
12
+ image = ImageMap.new(CA_DOUBLE, [600, 600]) { -9999 }
13
+
14
+ image.set_xrange(-103..103)
15
+ image.set_yrange(-103..103)
16
+
17
+ Benchmark.bm do |o|
18
+ o.report {
19
+ image.warp(val, u, v, :grid=>"point", :gradation => true) {
20
+ |_u,_v,_x,_y|
21
+ _x[] = (100 - _v/3) * _u.rad.cos!
22
+ _y[] = (100 - _v/3) * _u.rad.sin!
23
+ }
24
+ }
25
+ end
26
+
27
+ #image.draw_line(0, 0, 0, 90, 0)
28
+ image.line(0, 0, 0, 90, 0)
29
+
30
+ poly = CArray.float(4,2) {[[-90,90],[0,0],[45,45],[0,90]]}
31
+
32
+ image.polyline(poly[nil,1],poly[nil,0], 0)
33
+
34
+ image[:lt, -9998] = UNDEF
35
+
36
+ #sc = nil
37
+ #sc = ImageMap::ColorScale.pm3d(11,7,22,12)
38
+ sc = ColorScale.GMT("GMT_polar.cpt")
39
+ #sc.set_mask_color(64,64,64)
40
+ sc.set_range(nil, nil)
41
+
42
+ ImageMap.display(image, sc)
@@ -0,0 +1,14 @@
1
+ require 'carray'
2
+ require 'carray/image_map'
3
+
4
+ p img = ImageMap.new(CA_INT8, [200,200]) {0}
5
+ p img.class
6
+
7
+ gx = CArray.float(2,2) { [[10,30],
8
+ [10,30]] }
9
+ gy = CArray.float(2,2) { [[10,10],
10
+ [80,50]] }
11
+
12
+ img.fill_rectangle(gy, gx, 255)
13
+
14
+ img.display_by_magick
@@ -0,0 +1,17 @@
1
+ require "carray"
2
+
3
+ img = ImageMap.new(:int16, [300, 300]) {0}
4
+ img.set_xrange 0...300
5
+ img.set_yrange 0...300
6
+
7
+ data = CArray.int16(16,16).seq!
8
+
9
+ x = CArray.float(16).seq![16,:%]
10
+ y = CArray.float(16).seq!.reverse![:%,16]
11
+
12
+ img.warp(data, x, y, :grid=>"area0", :gradation=>false) {|gx,gy,ix,iy|
13
+ ix[] = 8*(gy+gx) + 24
14
+ iy[] = 8*(gy-gx) + 150
15
+ }
16
+
17
+ img.as_uint8.display_by_magick
@@ -0,0 +1,20 @@
1
+ require 'carray'
2
+
3
+ image = CArray.float(200, 200) { nan }
4
+
5
+ y = CArray.float(2,2){[50,50, 65,100]}
6
+ x = CArray.float(2,2){[70,100,50,90]}
7
+ z = CArray.float(2,2){[30,40, 50,20]}
8
+ ImageMap::Image.draw_rectangle_gradation(image, y, x, z)
9
+
10
+ #image.draw_line(y[0], x[0], y[1], x[1])
11
+ #image.draw_line(y[1], x[1], y[2], x[2])
12
+ #image.draw_line(y[2], x[2], y[0], x[0])
13
+ #image.draw_line(y[2], x[2], y[0], x[0])
14
+
15
+ image.mask = image.is_nan
16
+
17
+ sc = ImageMap::ColorScale.pm3d(13,6,10)
18
+
19
+ ImageMap.display(image, sc)
20
+
@@ -0,0 +1,26 @@
1
+ require 'carray'
2
+
3
+ lon = CArray.float(121).span!(-180..180)
4
+ lat = CArray.float(12).span!(0..90)
5
+ p val = CArray.uint16(121,12).seq!.quantize(256)
6
+
7
+ img = ImageMap.uint16(600,600) { 9999 }
8
+
9
+ img.warp(val, lon[:%,lat.dim0], lat[lon.dim0,:%],
10
+ :grid=>"area0", :gradation=>false) { |u,v,x,y|
11
+ x[] = (100 - v/2) * u.rad.cos!
12
+ y[] = (100 - v/2) * u.rad.sin!
13
+ x.mul!(2).add!(300)
14
+ y.mul!(2).add!(300)
15
+ }
16
+
17
+ img[:eq,9999] = UNDEF
18
+
19
+ RGB = CA.struct{ uint8 :r,:g,:b }
20
+ rgb = CArray.new(RGB, [255,3])
21
+ rgb["r"].seq!
22
+
23
+ out = rgb.project(img)
24
+
25
+ out.display_by_magick("rgb")
26
+
@@ -0,0 +1,321 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ carray/mathfunc/carray_mathfunc.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
+
17
+ #ifdef HAVE_TGMATH_H
18
+ #include <tgmath.h>
19
+ #endif
20
+
21
+ /* ----------------------------------------------------------------------- */
22
+
23
+ static void
24
+ mathfunc_deg_360 (void *p0, void *p1)
25
+ {
26
+ double a = *(double*) p1;
27
+ double fa = a / 360.0;
28
+ if ( a >= 0 ) {
29
+ *(double*) p0 = (fa - floor(fa)) * 360.0;
30
+ }
31
+ else {
32
+ *(double*) p0 = (fa - ceil(fa) + 1) * 360.0;
33
+ }
34
+ }
35
+
36
+ static VALUE
37
+ rb_ca_deg_360 (VALUE self)
38
+ {
39
+ return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_360, self);
40
+ }
41
+
42
+ static VALUE
43
+ rb_ca_deg_360_bang (VALUE self)
44
+ {
45
+ volatile VALUE out;
46
+ out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_360, self);
47
+ rb_funcall(self, rb_intern("[]="), 1, out);
48
+ return self;
49
+ }
50
+
51
+ static VALUE
52
+ rb_num_deg_360 (VALUE self)
53
+ {
54
+ double v0, v1 = NUM2DBL(self);
55
+ mathfunc_deg_360(&v0, &v1);
56
+ return rb_float_new(v0);
57
+ }
58
+
59
+ /* ----------------------------------------------------------------------- */
60
+
61
+ static void
62
+ mathfunc_deg_180 (void *p0, void *p1)
63
+ {
64
+ double a = *(double*) p1, b;
65
+ double fa = (a+180.0) / 360.0;
66
+ if ( a >= 0 ) {
67
+ b = (fa - floor(fa)) * 360.0 - 180;
68
+ }
69
+ else {
70
+ b = (fa - ceil(fa)) * 360.0 - 180;
71
+ }
72
+ if ( b <= -180 ) {
73
+ b += 360.0;
74
+ }
75
+ *(double*) p0 = b;
76
+ }
77
+
78
+ static VALUE
79
+ rb_ca_deg_180 (VALUE self)
80
+ {
81
+ return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_180, self);
82
+ }
83
+
84
+ static VALUE
85
+ rb_ca_deg_180_bang (VALUE self)
86
+ {
87
+ volatile VALUE out;
88
+ out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_180, self);
89
+ rb_funcall(self, rb_intern("[]="), 1, out);
90
+ return self;
91
+ }
92
+
93
+ static VALUE
94
+ rb_num_deg_180 (VALUE self)
95
+ {
96
+ double v0, v1 = NUM2DBL(self);
97
+ mathfunc_deg_180(&v0, &v1);
98
+ return rb_float_new(v0);
99
+ }
100
+
101
+ /* ----------------------------------------------------------------------- */
102
+
103
+ static void
104
+ mathfunc_rad_2pi (void *p0, void *p1)
105
+ {
106
+ double a = *(double*) p1;
107
+ double fa = a / (2*M_PI);
108
+ if ( a >= 0 ) {
109
+ *(double*) p0 = (fa - floor(fa)) * 2*M_PI;
110
+ }
111
+ else {
112
+ *(double*) p0 = (fa - ceil(fa) + 1) * 2*M_PI;
113
+ }
114
+ }
115
+
116
+ static VALUE
117
+ rb_ca_rad_2pi (VALUE self)
118
+ {
119
+ return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_2pi, self);
120
+ }
121
+
122
+ static VALUE
123
+ rb_ca_rad_2pi_bang (VALUE self)
124
+ {
125
+ volatile VALUE out;
126
+ out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_2pi, self);
127
+ rb_funcall(self, rb_intern("[]="), 1, out);
128
+ return self;
129
+ }
130
+
131
+ static VALUE
132
+ rb_num_rad_2pi (VALUE self)
133
+ {
134
+ double v0, v1 = NUM2DBL(self);
135
+ mathfunc_rad_2pi(&v0, &v1);
136
+ return rb_float_new(v0);
137
+ }
138
+
139
+ /* ----------------------------------------------------------------------- */
140
+
141
+ static void
142
+ mathfunc_rad_pi (void *p0, void *p1)
143
+ {
144
+ double a = *(double*) p1, b;
145
+ double fa = (a+M_PI) / (2*M_PI);
146
+ if ( a >= 0 ) {
147
+ b = (fa - floor(fa)) * 2*M_PI - M_PI;
148
+ }
149
+ else {
150
+ b = (fa - ceil(fa)) * 2*M_PI - M_PI;
151
+ }
152
+ if ( b <= -M_PI ) {
153
+ b += 2*M_PI;
154
+ }
155
+ *(double*) p0 = b;
156
+ }
157
+
158
+ static VALUE
159
+ rb_ca_rad_pi (VALUE self)
160
+ {
161
+ return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_pi, self);
162
+ }
163
+
164
+ static VALUE
165
+ rb_ca_rad_pi_bang (VALUE self)
166
+ {
167
+ volatile VALUE out;
168
+ out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_pi, self);
169
+ rb_funcall(self, rb_intern("[]="), 1, out);
170
+ return self;
171
+ }
172
+
173
+ static VALUE
174
+ rb_num_rad_pi (VALUE self)
175
+ {
176
+ double v0, v1 = NUM2DBL(self);
177
+ mathfunc_rad_pi(&v0, &v1);
178
+ return rb_float_new(v0);
179
+ }
180
+
181
+ /* ----------------------------------------------------------------------- */
182
+
183
+ static void
184
+ mathfunc_sph_to_xyz (void *p0, void *p1, void *p2, void *p3, void *p4, void *p5)
185
+ {
186
+ double r = *(double*)p3, theta = *(double*)p4, phi = *(double*)p5;
187
+ *(double*) p0 = r * sin(theta) * cos(phi);
188
+ *(double*) p1 = r * sin(theta) * sin(phi);
189
+ *(double*) p2 = r * cos(theta);
190
+ }
191
+
192
+ static VALUE
193
+ rb_camath_sph_to_xyz (VALUE mod, VALUE rx1, VALUE rx2, VALUE rx3)
194
+ {
195
+ return ca_call_cfunc_3_3(CA_DOUBLE, CA_DOUBLE, CA_DOUBLE, CA_DOUBLE, CA_DOUBLE, CA_DOUBLE,
196
+ mathfunc_sph_to_xyz, rx1, rx2, rx3);
197
+ }
198
+
199
+ /* ----------------------------------------------------------------------- */
200
+
201
+ static void
202
+ mathfunc_xyz_to_sph (void *p0, void *p1, void *p2, void *p3, void *p4, void *p5)
203
+ {
204
+ #ifdef HAVE_ATAN2
205
+ double x = *(double*)p3, y = *(double*)p4, z = *(double*)p5;
206
+ double r;
207
+ *(double*) p0 = r = sqrt(x*x+y*y+z*z);
208
+ *(double*) p1 = acos(z/r);
209
+ *(double*) p2 = atan2(y, x);
210
+ #else
211
+ rb_raise(rb_eRuntimeError, "atan2 is not defined");
212
+ #endif
213
+ }
214
+
215
+ static VALUE
216
+ rb_camath_xyz_to_sph (VALUE mod, VALUE rx1, VALUE rx2, VALUE rx3)
217
+ {
218
+ return ca_call_cfunc_3_3(CA_DOUBLE, CA_DOUBLE, CA_DOUBLE, CA_DOUBLE, CA_DOUBLE, CA_DOUBLE,
219
+ mathfunc_xyz_to_sph, rx1, rx2, rx3);
220
+ }
221
+
222
+ /* ----------------------------------------------------------------------- */
223
+
224
+ static void
225
+ mathfunc_atan2 (void *p0, void *p1, void *p2)
226
+ {
227
+ #ifdef HAVE_ATAN2
228
+ *(double*)p0 = atan2(*(double*)p1, *(double*)p2);
229
+ #else
230
+ rb_raise(rb_eRuntimeError, "atan2 is not defined");
231
+ #endif
232
+ }
233
+
234
+ static VALUE
235
+ rb_camath_atan2 (VALUE mod, VALUE rx1, VALUE rx2)
236
+ {
237
+ return ca_call_cfunc_1_2(CA_DOUBLE, CA_DOUBLE, CA_DOUBLE,
238
+ mathfunc_atan2, rx1, rx2);
239
+ }
240
+
241
+ /* ----------------------------------------------------------------------- */
242
+
243
+ static void
244
+ mathfunc_hypot (void *p0, void *p1, void *p2)
245
+ {
246
+ #ifdef HAVE_HYPOT
247
+ *(double *)p0 = hypot(*(double*)p1, *(double*)p2);
248
+ #else
249
+ rb_raise(rb_eRuntimeError, "hypot is not defined");
250
+ #endif
251
+ }
252
+
253
+ static VALUE
254
+ rb_camath_hypot (VALUE mod, VALUE rx1, VALUE rx2)
255
+ {
256
+ return ca_call_cfunc_1_2(CA_DOUBLE, CA_DOUBLE, CA_DOUBLE,
257
+ mathfunc_hypot, rx1, rx2);
258
+ }
259
+
260
+ /* ----------------------------------------------------------------------- */
261
+
262
+ static void
263
+ mathfunc_lgamma (void *p0, void *p1)
264
+ {
265
+ #ifdef HAVE_LGAMMA
266
+ *(double *)p0 = lgamma(*(double*)p1);
267
+ #else
268
+ rb_raise(rb_eRuntimeError, "lgamma is not defined ");
269
+ #endif
270
+ }
271
+
272
+ static VALUE
273
+ rb_camath_lgamma (VALUE mod, VALUE rx1)
274
+ {
275
+ return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_lgamma, rx1);
276
+ }
277
+
278
+ /* ----------------------------------------------------------------------- */
279
+
280
+ static void
281
+ mathfunc_expm1 (void *p0, void *p1)
282
+ {
283
+ #ifdef HAVE_EXP1M
284
+ *(double *)p0 = expm1(*(double*)p1);
285
+ #else
286
+ rb_raise(rb_eRuntimeError, "expm1 is not defined ");
287
+ #endif
288
+ }
289
+
290
+ static VALUE
291
+ rb_camath_expm1 (VALUE mod, VALUE rx1)
292
+ {
293
+ return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_expm1, rx1);
294
+ }
295
+
296
+ void
297
+ Init_carray_mathfunc ()
298
+ {
299
+ rb_define_method(rb_cCArray, "deg_360", rb_ca_deg_360, 0);
300
+ rb_define_method(rb_cCArray, "deg_360!", rb_ca_deg_360_bang, 0);
301
+ rb_define_method(rb_cCArray, "deg_180", rb_ca_deg_180, 0);
302
+ rb_define_method(rb_cCArray, "deg_180!", rb_ca_deg_180_bang, 0);
303
+ rb_define_method(rb_cCArray, "rad_2pi", rb_ca_rad_2pi, 0);
304
+ rb_define_method(rb_cCArray, "rad_2pi!", rb_ca_rad_2pi_bang, 0);
305
+ rb_define_method(rb_cCArray, "rad_pi", rb_ca_rad_pi, 0);
306
+ rb_define_method(rb_cCArray, "rad_pi!", rb_ca_rad_pi_bang, 0);
307
+
308
+ rb_define_module_function(rb_mCAMath, "spherical_to_xyz", rb_camath_sph_to_xyz, 3);
309
+ rb_define_module_function(rb_mCAMath, "xyz_to_spherical", rb_camath_xyz_to_sph, 3);
310
+
311
+ rb_define_module_function(rb_mCAMath, "atan2", rb_camath_atan2, 2);
312
+ rb_define_module_function(rb_mCAMath, "hypot", rb_camath_hypot, 2);
313
+
314
+ rb_define_module_function(rb_mCAMath, "lgamma", rb_camath_lgamma, 1);
315
+ rb_define_module_function(rb_mCAMath, "expm1", rb_camath_expm1, 1);
316
+
317
+ rb_define_method(rb_cNumeric, "deg_360", rb_num_deg_360, 0);
318
+ rb_define_method(rb_cNumeric, "deg_180", rb_num_deg_180, 0);
319
+ rb_define_method(rb_cNumeric, "rad_2pi", rb_num_rad_2pi, 0);
320
+ rb_define_method(rb_cNumeric, "rad_pi", rb_num_rad_pi, 0);
321
+ }