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,145 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/mkmf.rb
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
+ require 'mkmf'
14
+
15
+ def have_carray
16
+ begin
17
+ require 'carray'
18
+ rescue LoadError
19
+ abort "Ruby/CArray is not installed"
20
+ end
21
+ dir_config("carray", $sitearchdir, $sitearchdir)
22
+ if defined? Gem
23
+ if Gem::VERSION >= "1.7.0"
24
+ begin
25
+ Gem::Specification.find_all_by_name("carray").each do |spec|
26
+ dir = spec.full_gem_path
27
+ dir_config("carray", dir, dir)
28
+ end
29
+ rescue Gem::LoadError
30
+ end
31
+ else
32
+ Gem.all_load_paths.grep(/narray/).each do |dir|
33
+ dir_config("carray", dir, dir)
34
+ end
35
+ end
36
+ end
37
+ status = true
38
+ status &= have_header("carray.h")
39
+ if /cygwin|mingw/ =~ RUBY_PLATFORM
40
+ status &= have_library("carray")
41
+ end
42
+ status
43
+ end
44
+
45
+ def check_fortran
46
+
47
+ SRC_EXT << "f" << "f90" << "f95"
48
+
49
+ fortran = with_config("fortran", "gfortran")
50
+ fflags = with_config("fflags", "")
51
+
52
+ case fortran
53
+ when /\Ag77/
54
+ libdir = File.dirname(`#{fortran} --print-file-name=libg2c.a`)
55
+ dir_config("g2c", possible_includes, [libdir]+possible_libs)
56
+ have_library("g2c")
57
+ fc = fortran
58
+ fflags << " -O2 -g "
59
+ when /\Agfortran/
60
+ libdir = File.dirname(`#{fortran} --print-file-name=libgfortran.a`)
61
+ dir_config("gfortran", possible_includes, [libdir]+possible_libs)
62
+ have_library("gfortran")
63
+ fc = fortran
64
+ fflags << " -O2 -g -fPIC "
65
+ when /\Aintel/, /\Aifort/, /\Aifc/
66
+ libdir = File.join(File.dirname(File.dirname(`which ifort`)), "lib")
67
+ dir_config("ifcore", possible_includes, [libdir]+possible_libs)
68
+ have_library("ifcore")
69
+ have_library("ifport")
70
+ have_library("imf")
71
+ have_library("irc")
72
+ have_library("svml")
73
+ have_library("unwind")
74
+ fc = "ifort"
75
+ fflags << " -O2 -fPIC "
76
+ when /\Afujitsu/, /\Afrt/
77
+ libdir = File.join(File.dirname(File.dirname(`which frt`)), "lib")
78
+ dir_config("fj9f6", nil, libdir)
79
+ $LIBS = " -lfj9i6 -lfj9f6"
80
+ fc = "frt"
81
+ fflags = " -O -fw "
82
+ else
83
+ puts "unknown fortran option <#{fortran}>"
84
+ exit(1)
85
+ end
86
+ at_exit {
87
+ if File.file?("Makefile")
88
+ makefile = File.read("Makefile")
89
+ unless makefile =~ /^FC=/
90
+ makefile.sub!(/^COPY =.*$/,
91
+ '\0' + "\n\n" +
92
+ "FC=#{fc}\n" +
93
+ "FFLAGS=#{fflags}\n")
94
+ end
95
+ open("Makefile", "w") { |io| io.write(makefile) }
96
+ end
97
+ }
98
+
99
+ return "--with-fortran='#{fortran}' --with-fflags='#{fflags}'"
100
+
101
+ end
102
+
103
+ def possible_prefix (*postfixes)
104
+ dirs = [
105
+ File.expand_path("~/usr"), ### user's home / usr
106
+ File.expand_path("~/local"), ### user's home / local
107
+ File.expand_path("~"), ### user's home
108
+ "/opt/local", ### MacPorts
109
+ "/opt", ### UNIX
110
+ "/sw/local", ### Mac Fink
111
+ "/sw/", ### Mac Fink
112
+ "/usr/X11R6", ### UNIX X11
113
+ "/usr/local", ### UNIX
114
+ "/usr", ### UNIX
115
+ "/" ### UNIX
116
+ ]
117
+ if postfixes
118
+ dirs = postfixes.inject(dirs) { |list, postfix|
119
+ list + dirs.map{|d| File.join(d, postfix) }
120
+ }
121
+ end
122
+ return dirs.select{|d| File.directory?(d)}
123
+ end
124
+
125
+ POSSIBLE_PREFIX = possible_prefix()
126
+
127
+ def possible_libs (*postfixes)
128
+ dirs = possible_prefix().map{|prefix| File.join(prefix, "lib") }
129
+ if postfixes
130
+ dirs = postfixes.inject(dirs) { |list, postfix|
131
+ list + dirs.map{|d| File.join(d, postfix) }
132
+ }
133
+ end
134
+ return dirs.select{|d| File.directory?(d)}
135
+ end
136
+
137
+ def possible_includes (*postfixes)
138
+ dirs = possible_prefix().map{|prefix| File.join(prefix, "include") }
139
+ if postfixes
140
+ dirs = postfixes.inject(dirs) { |list, postfix|
141
+ list + dirs.map{|d| File.join(d, postfix) }
142
+ }
143
+ end
144
+ return dirs.select{|d| File.directory?(d)}
145
+ end
@@ -0,0 +1,52 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/object/ca_obj_iterator.rb
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
+ class CAIteratorArray < CAObject # :nodoc:
14
+
15
+ def initialize (it)
16
+ @it = it
17
+ super(CA_OBJECT, @it.dim)
18
+ end
19
+
20
+ private
21
+
22
+ def fetch_index (idx)
23
+ return @it.kernel_at_index(idx)
24
+ end
25
+
26
+ def store_index (idx, val)
27
+ @it.kernel_at_index(idx)[] = val
28
+ end
29
+
30
+ def copy_data (data)
31
+ data.each_index do |*idx|
32
+ data[*idx] = @it.kernel_at_index(idx)
33
+ end
34
+ end
35
+
36
+ def sync_data (data)
37
+ tmpl = @it.reference.template
38
+ data.each_index do |*idx|
39
+ @it.kernel_at_index(idx, tmpl)[] = data[*idx]
40
+ end
41
+ @it.reference[] = tmpl
42
+ end
43
+
44
+ def fill_data (data)
45
+ tmpl = @it.reference.template
46
+ CArray.each_index(*@it.dim) do |*idx|
47
+ @it.kernel_at_index(idx, tmpl)[] = data
48
+ end
49
+ @it.reference[] = tmpl
50
+ end
51
+
52
+ end
@@ -0,0 +1,52 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/object/ca_obj_link.rb
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
+ require "carray"
14
+
15
+ class CALink < CAObject # :nodoc:
16
+
17
+ def initialize (*argv, &block)
18
+ @evaluator = block
19
+ @argv = argv
20
+ @args = argv.map{|v| CScalar.new(v.data_type) }
21
+ val = @evaluator.call(*argv)
22
+ unless val.is_a?(CArray)
23
+ val = CA_OBJECT(val)
24
+ end
25
+ super(val.data_type, val.dim, :bytes=>val.bytes, :read_only=>true)
26
+ end
27
+
28
+ private
29
+
30
+ def fetch_addr (addr)
31
+ @argv.each_with_index do |v, i|
32
+ case v
33
+ when CScalar
34
+ @args[i][] = v[]
35
+ when CArray
36
+ @args[i][] = v[addr]
37
+ else
38
+ @args[i][] = v
39
+ end
40
+ end
41
+ return @evaluator.call(*@args)[0]
42
+ end
43
+
44
+ def copy_data (data)
45
+ data[] = @evaluator.call(*@argv)
46
+ end
47
+
48
+ def create_mask
49
+ end
50
+
51
+ end
52
+
@@ -0,0 +1,101 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/object/ca_obj_pack.rb
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
+ #
14
+ #
15
+ # [ca1, ca2, ca3] => ca4
16
+ #
17
+ #
18
+ #
19
+
20
+
21
+ class CAPack < CAObject # :nodoc:
22
+
23
+ def initialize (list, options={})
24
+ @list = list
25
+ @rank = options[:rank]
26
+ unless @rank
27
+ @rank = list.map{|m| m.rank}.min
28
+ end
29
+ @names = options[:names] || [nil]*@list.size
30
+ @dim = guess_dim(list)
31
+ @struct = guess_struct(list)
32
+ super(@struct, @dim, :read_only=>true)
33
+ freeze
34
+ end
35
+
36
+ private
37
+
38
+ def fetch_index (idx)
39
+ out = @struct.new
40
+ dummy = idx + [false]
41
+ @struct.members.each_with_index do |name, i|
42
+ out[name] = @list[i][*dummy]
43
+ end
44
+ return out
45
+ end
46
+
47
+ def copy_data (data)
48
+ data.fields.zip(@list).each do |mem, d|
49
+ mem[] = d
50
+ end
51
+ end
52
+
53
+ def guess_dim (list)
54
+ dim = nil
55
+ list.each do |mem|
56
+ case mem
57
+ when CArray
58
+ if mem.rank < @rank
59
+ raise "mem.rank < @rank"
60
+ else
61
+ newdim = mem.dim[0, @rank]
62
+ end
63
+ if dim
64
+ unless dim == newdim
65
+ raise "dim != newdim"
66
+ end
67
+ else
68
+ dim = newdim
69
+ end
70
+ end
71
+ end
72
+ return dim || [1]
73
+ end
74
+
75
+ def guess_struct (list)
76
+ st = CA.struct(:pack=>1) { |s|
77
+ list.each_with_index do |mem, i|
78
+ name = @names[i]
79
+ case mem
80
+ when CArray
81
+ if mem.rank == @rank
82
+ s.member mem.data_type, name, :bytes=>mem.bytes ### anonymous member
83
+ else
84
+ dummy = Array.new(@rank){0} + [false]
85
+ s.member mem[*dummy].to_ca, name ### anonymous member
86
+ end
87
+ else
88
+ raise "invalid object for CAPack"
89
+ end
90
+ end
91
+ }
92
+ return st
93
+ end
94
+
95
+ end
96
+
97
+ class CArray
98
+ def self.pack (*argv)
99
+ return CAPack.new(argv)
100
+ end
101
+ end
@@ -0,0 +1,731 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # mkmath.rb
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
+ require 'stringio'
14
+
15
+ def monfunc (op, name, hash)
16
+ io = StringIO.new
17
+ io.puts
18
+ io.puts "/*----------------------- #{name} --------------------------*/"
19
+ hash.each do |types, expr0|
20
+ if not expr0
21
+ next
22
+ end
23
+ expr0.gsub!(/#/, '*p')
24
+ types.each do |type|
25
+ if type
26
+ expr = expr0.gsub(/<type>/, type)
27
+ omp_ok = ( type != "VALUE" ) ? 1 : 0
28
+ io.print %{
29
+ static void
30
+ ca_monop_#{name}_#{type} (int32_t n, boolean8_t *m, char *ptr1, int32_t i1, char *ptr2, int32_t i2)
31
+ {
32
+ #{type} *q1 = (#{type} *) ptr1, *q2 = (#{type} *) ptr2;
33
+ #{type} *p1 = q1, *p2 = q2;
34
+ int32_t k;
35
+ if ( m ) {
36
+ boolean8_t *pm;
37
+ #if defined(_OPENMP) && #{omp_ok}
38
+ #pragma omp parallel for private(pm,p1,p2)
39
+ #endif
40
+ for (k=0; k<n; k++) {
41
+ pm = m + k;
42
+ if ( ! *pm ) {
43
+ p1 = q1 + k*i1;
44
+ p2 = q2 + k*i2;
45
+ {
46
+ #{expr}
47
+ }
48
+ }
49
+ }
50
+ }
51
+ else {
52
+ #if defined(_OPENMP) && #{omp_ok}
53
+ #pragma omp parallel for private(p1,p2)
54
+ #endif
55
+ for (k=0; k<n; k++) {
56
+ p1 = q1 + k*i1;
57
+ p2 = q2 + k*i2;
58
+ {
59
+ #{expr}
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+ end
66
+ end
67
+ end
68
+ io.puts
69
+ io.puts "ca_monop_func_t"
70
+ io.puts "ca_monop_#{name}[CA_NTYPE] = {"
71
+ ALL_TYPES.each_index do |i|
72
+ type = nil
73
+ hash.each do |types, expr|
74
+ if not expr
75
+ next
76
+ end
77
+ type ||= types[i]
78
+ end
79
+ if type
80
+ io.puts(" ca_monop_#{name}_#{type},")
81
+ else
82
+ io.puts(" ca_monop_not_implement,")
83
+ end
84
+ end
85
+ io.puts "};"
86
+ io.puts
87
+ if hash.has_key?(INT_TYPES) or hash.has_key?(ALL_TYPES)
88
+ io.print %{
89
+ static VALUE rb_ca_#{name} (VALUE self)
90
+ { return rb_ca_call_monop(self, ca_monop_#{name}); }
91
+ }
92
+ else
93
+ io.print %{
94
+ static VALUE rb_ca_#{name} (VALUE self)
95
+ {
96
+ if ( rb_ca_is_integer_type(self) ) {
97
+ self = rb_ca_wrap_readonly(self, INT2FIX(CA_FLOAT64));
98
+ }
99
+ return rb_ca_call_monop(self, ca_monop_#{name});
100
+ }
101
+ }
102
+ end
103
+ io.print %{
104
+ static VALUE rb_ca_#{name}_bang (VALUE self)
105
+ { return rb_ca_call_monop_bang(self, ca_monop_#{name}); }
106
+ }
107
+ DEFINITIONS << io.string
108
+ if op
109
+ METHODS << %{
110
+ rb_define_method(rb_cCArray, "#{op}", rb_ca_#{name}, 0);
111
+ }
112
+ end
113
+ METHODS << %{
114
+ rb_define_method(rb_cCArray, "#{name}!", rb_ca_#{name}_bang, 0);
115
+ }
116
+ DEFINITIONS << %{
117
+ static VALUE rb_cmath_#{name} (VALUE mod, VALUE arg)
118
+ { return ca_math_call(mod, arg, rb_intern("#{name}")); }
119
+ }
120
+ METHODS << %{
121
+ rb_define_module_function(rb_mCAMath, "#{name}", rb_cmath_#{name}, 1);
122
+ }
123
+ end
124
+
125
+
126
+ def monop (op, name, hash)
127
+ io = StringIO.new
128
+ io.puts
129
+ io.puts "/*----------------------- #{name} --------------------------*/"
130
+ hash.each do |types, expr0|
131
+ if not expr0
132
+ next
133
+ end
134
+ expr0.gsub!(/#/, '*p')
135
+ types.each do |type|
136
+ if type
137
+ expr = expr0.gsub(/<type>/, type)
138
+ omp_ok = ( type != "VALUE" ) ? 1 : 0
139
+ io.print %{
140
+ static void
141
+ ca_monop_#{name}_#{type} (int32_t n, boolean8_t *m, char *ptr1, int32_t i1, char *ptr2, int32_t i2)
142
+ {
143
+ #{type} *q1 = (#{type} *) ptr1, *q2 = (#{type} *) ptr2;
144
+ #{type} *p1 = q1, *p2 = q2;
145
+ int32_t k;
146
+ if ( m ) {
147
+ boolean8_t *pm;
148
+ #if defined(_OPENMP) && #{omp_ok}
149
+ #pragma omp parallel for private(pm,p1,p2)
150
+ #endif
151
+ for (k=0; k<n; k++) {
152
+ pm = m + k;
153
+ if ( ! *pm ) {
154
+ p1 = q1 + k*i1;
155
+ p2 = q2 + k*i2;
156
+ {
157
+ #{expr}
158
+ }
159
+ }
160
+ }
161
+ }
162
+ else {
163
+ #if defined(_OPENMP) && #{omp_ok}
164
+ #pragma omp parallel for private(p1,p2)
165
+ #endif
166
+ for (k=0; k<n; k++) {
167
+ p1 = q1 + k*i1;
168
+ p2 = q2 + k*i2;
169
+ {
170
+ #{expr}
171
+ }
172
+ }
173
+ }
174
+ }
175
+ }
176
+ end
177
+ end
178
+ end
179
+ io.puts
180
+ io.puts "ca_monop_func_t"
181
+ io.puts "ca_monop_#{name}[CA_NTYPE] = {"
182
+ ALL_TYPES.each_index do |i|
183
+ type = nil
184
+ hash.each do |types, expr|
185
+ if not expr
186
+ next
187
+ end
188
+ type ||= types[i]
189
+ end
190
+ if type
191
+ io.puts(" ca_monop_#{name}_#{type},")
192
+ else
193
+ io.puts(" ca_monop_not_implement,")
194
+ end
195
+ end
196
+ io.puts "};"
197
+ io.puts
198
+ io.print %{
199
+ static VALUE rb_ca_#{name} (VALUE self)
200
+ { return rb_ca_call_monop(self, ca_monop_#{name}); }
201
+ static VALUE rb_ca_#{name}_bang (VALUE self)
202
+ { return rb_ca_call_monop_bang(self, ca_monop_#{name}); }
203
+ }
204
+ DEFINITIONS << io.string
205
+ if op
206
+ METHODS << %{
207
+ rb_define_method(rb_cCArray, "#{op}", rb_ca_#{name}, 0);
208
+ }
209
+ end
210
+ METHODS << %{
211
+ rb_define_method(rb_cCArray, "#{name}!", rb_ca_#{name}_bang, 0);
212
+ }
213
+
214
+ end
215
+
216
+ def binop (op, name, hash)
217
+ io = StringIO.new
218
+ io.puts
219
+ io.puts "/*----------------------- #{name} --------------------------*/"
220
+ hash.each do |types, expr0|
221
+ if not expr0
222
+ next
223
+ end
224
+ expr0.gsub!(/#/, '*p')
225
+ types.each do |type|
226
+ if type
227
+ expr = expr0.gsub(/<type>/, type)
228
+ omp_ok = ( type != "VALUE" ) ? 1 : 0
229
+ io.print %{
230
+ static void
231
+ ca_binop_#{name}_#{type} (int32_t n, boolean8_t *m, char *ptr1, int32_t i1, char *ptr2, int32_t i2, char *ptr3, int32_t i3)
232
+ {
233
+ #{type} *q1 = (#{type} *) ptr1, *q2 = (#{type} *) ptr2, *q3 = (#{type} *) ptr3;
234
+ #{type} *p1 = q1, *p2 = q2, *p3 = q3;
235
+ int32_t k;
236
+ if ( m ) {
237
+ boolean8_t *pm;
238
+ #if defined(_OPENMP) && #{omp_ok}
239
+ #pragma omp parallel for private(pm,p1,p2,p3)
240
+ #endif
241
+ for (k=0; k<n; k++) {
242
+ pm = m + k;
243
+ if ( ! *pm ) {
244
+ p1 = q1 + k*i1;
245
+ p2 = q2 + k*i2;
246
+ p3 = q3 + k*i3;
247
+ {
248
+ #{expr}
249
+ }
250
+ }
251
+ }
252
+ }
253
+ else {
254
+ #if defined(_OPENMP) && #{omp_ok}
255
+ #pragma omp parallel for private(p1,p2,p3)
256
+ #endif
257
+ for (k=0; k<n; k++) {
258
+ p1 = q1 + k*i1;
259
+ p2 = q2 + k*i2;
260
+ p3 = q3 + k*i3;
261
+ {
262
+ #{expr}
263
+ }
264
+ }
265
+ }
266
+ }
267
+ }
268
+ end
269
+ end
270
+ end
271
+ io.puts
272
+ io.puts "ca_binop_func_t"
273
+ io.puts "ca_binop_#{name}[CA_NTYPE] = {"
274
+ ALL_TYPES.each_index do |i|
275
+ type = nil
276
+ hash.each do |types, expr|
277
+ if not expr
278
+ next
279
+ end
280
+ type ||= types[i]
281
+ end
282
+ if type
283
+ io.puts(" ca_binop_#{name}_#{type},")
284
+ else
285
+ io.puts(" ca_binop_not_implement,")
286
+ end
287
+ end
288
+ io.puts "};"
289
+ io.puts
290
+ io.print %{
291
+ static VALUE rb_ca_#{name} (VALUE self, VALUE other)
292
+ { return rb_ca_call_binop(self, other, ca_binop_#{name}); }
293
+ static VALUE rb_ca_#{name}_bang (VALUE self, VALUE other)
294
+ { return rb_ca_call_binop_bang(self, other, ca_binop_#{name}); }
295
+ }
296
+ DEFINITIONS << io.string
297
+ if op
298
+ METHODS << %{
299
+ rb_define_method(rb_cCArray, "#{op}", rb_ca_#{name}, 1);
300
+ }
301
+ end
302
+ METHODS << %{
303
+ rb_define_method(rb_cCArray, "#{op}!", rb_ca_#{name}_bang, 1);
304
+ }
305
+
306
+ end
307
+
308
+ def moncmp (op, name, hash)
309
+ io = StringIO.new
310
+ io.puts
311
+ io.puts "/*----------------------- #{name} --------------------------*/"
312
+ hash.each do |types, expr0|
313
+ if not expr0
314
+ next
315
+ end
316
+ expr0.gsub!(/#/, '*p')
317
+ types.each do |type|
318
+ if type
319
+ expr = expr0.gsub(/<type>/, type)
320
+ omp_ok = ( type != "VALUE" ) ? 1 : 0
321
+ io.print %{
322
+ static void
323
+ ca_moncmp_#{name}_#{type} (int32_t n, boolean8_t *m, char *ptr1, int32_t i1, boolean8_t *ptr2, int32_t i2)
324
+ {
325
+ #{type} *q1 = (#{type} *) ptr1;
326
+ #{type} *p1 = q1;
327
+ boolean8_t *q2 = (boolean8_t *) ptr2;
328
+ boolean8_t *p2 = q2;
329
+ int32_t k;
330
+ if ( m ) {
331
+ boolean8_t *pm = m;
332
+ #if defined(_OPENMP) && #{omp_ok}
333
+ #pragma omp parallel for private(pm,p1,p2)
334
+ #endif
335
+ for (k=0; k<n; k++) {
336
+ pm = m + k;
337
+ if ( ! *pm ) {
338
+ p1 = q1 + k*i1;
339
+ p2 = q2 + k*i2;
340
+ {
341
+ #{expr}
342
+ }
343
+ }
344
+ }
345
+ }
346
+ else {
347
+ #if defined(_OPENMP) && #{omp_ok}
348
+ #pragma omp parallel for private(p1,p2)
349
+ #endif
350
+ for (k=0; k<n; k++) {
351
+ p1=q1+k*i1;
352
+ p2=q2+k*i2;
353
+ {
354
+ #{expr}
355
+ }
356
+ }
357
+ }
358
+ }
359
+ }
360
+ end
361
+ end
362
+ end
363
+ io.puts
364
+ io.puts "ca_moncmp_func_t"
365
+ io.puts "ca_moncmp_#{name}[CA_NTYPE] = {"
366
+ ALL_TYPES.each_index do |i|
367
+ type = nil
368
+ hash.each do |types, expr|
369
+ if not expr
370
+ next
371
+ end
372
+ type ||= types[i]
373
+ end
374
+ if type
375
+ io.puts(" ca_moncmp_#{name}_#{type},")
376
+ else
377
+ io.puts(" ca_moncmp_not_implement,")
378
+ end
379
+ end
380
+ io.puts "};"
381
+ io.puts
382
+ io.print %{
383
+ static VALUE rb_ca_#{name} (VALUE self, VALUE other)
384
+ { return rb_ca_call_moncmp(self, ca_moncmp_#{name}); }
385
+ }
386
+ DEFINITIONS << io.string
387
+ if op
388
+ METHODS << %{
389
+ rb_define_method(rb_cCArray, "#{op}", rb_ca_#{name}, 0);
390
+ }
391
+ end
392
+ end
393
+
394
+ EPSILON = {
395
+ "float32_t" => "FLT_EPSILON",
396
+ "float64_t" => "DBL_EPSILON",
397
+ "float128_t" => "DBL_EPSILON",
398
+ }
399
+
400
+ def bincmp (op, name, hash)
401
+ io = StringIO.new
402
+ io.puts
403
+ io.puts "/*----------------------- #{name} --------------------------*/"
404
+ hash.each do |types, expr0|
405
+ if not expr0
406
+ next
407
+ end
408
+ expr0.gsub!(/#(\d)/, '*p\1')
409
+ types.each do |type|
410
+ if type
411
+ expr = expr0.clone
412
+ expr.gsub!(/<type>/, type)
413
+ expr.gsub!(/<epsilon>/, EPSILON[type]||"")
414
+ if type != "fixlen"
415
+ omp_ok = ( type != "VALUE" ) ? 1 : 0
416
+ io.print %{
417
+ static void
418
+ ca_bincmp_#{name}_#{type} (int32_t n, boolean8_t *m,
419
+ char *ptr1, int32_t b1, int32_t i1,
420
+ char *ptr2, int32_t b2, int32_t i2,
421
+ char *ptr3, int32_t b3, int32_t i3)
422
+ {
423
+ #{type} *q1 = (#{type} *) ptr1, *q2 = (#{type} *) ptr2;
424
+ #{type} *p1 = q1, *p2 = q2;
425
+ boolean8_t *q3 = (boolean8_t *) ptr3;
426
+ boolean8_t *p3 = q3;
427
+ int32_t k;
428
+ if ( m ) {
429
+ boolean8_t *pm = m;
430
+ #if defined(_OPENMP) && #{omp_ok}
431
+ #pragma omp parallel for private(pm,p1,p2,p3)
432
+ #endif
433
+ for (k=0; k<n; k++) {
434
+ pm = m + k;
435
+ if ( ! *pm ) {
436
+ p1 = q1 + k*i1;
437
+ p2 = q2 + k*i2;
438
+ p3 = q3 + k*i3;
439
+ {
440
+ #{expr}
441
+ }
442
+ }
443
+ }
444
+ }
445
+ else {
446
+ #if defined(_OPENMP) && #{omp_ok}
447
+ #pragma omp parallel for private(p1,p2,p3)
448
+ #endif
449
+ for (k=0; k<n; k++) {
450
+ p1=q1+k*i1;
451
+ p2=q2+k*i2;
452
+ p3=q3+k*i3;
453
+ {
454
+ #{expr}
455
+ }
456
+ }
457
+ }
458
+ }
459
+ }
460
+ else ### fixlen
461
+ omp_ok = 1
462
+ io.print %{
463
+ static void
464
+ ca_bincmp_#{name}_#{type} (int32_t n, boolean8_t *m,
465
+ char *ptr1, int32_t b1, int32_t i1,
466
+ char *ptr2, int32_t b2, int32_t i2,
467
+ char *ptr3, int32_t b3, int32_t i3)
468
+ {
469
+ char *q1 = ptr1, *q2 = ptr2;
470
+ char *p1 = q1, *p2 = q2;
471
+ boolean8_t *q3 = (boolean8_t *) ptr3;
472
+ boolean8_t *p3 = q3;
473
+ int32_t s1 = b1*i1, s2 = b2*i2, s3 = b3*i3;
474
+ int32_t k;
475
+ if ( m ) {
476
+ boolean8_t *pm = m;
477
+ #if defined(_OPENMP) && #{omp_ok}
478
+ #pragma omp parallel for private(pm,p1,p2,p3)
479
+ #endif
480
+ for (k=0; k<n; k++) {
481
+ pm = m + k;
482
+ if ( ! *pm ) {
483
+ p1 = q1 + k*s1;
484
+ p2 = q2 + k*s2;
485
+ p3 = q3 + k*s3;
486
+ {
487
+ #{expr}
488
+ }
489
+ }
490
+ }
491
+ }
492
+ else {
493
+ #if defined(_OPENMP) && #{omp_ok}
494
+ #pragma omp parallel for private(p1,p2,p3)
495
+ #endif
496
+ for (k=0; k<n; k++) {
497
+ p1=q1+k*s1;
498
+ p2=q2+k*s2;
499
+ p3=q3+k*s3;
500
+ {
501
+ #{expr}
502
+ }
503
+ }
504
+ }
505
+ }
506
+ }
507
+ end
508
+ end
509
+ end
510
+ end
511
+ io.puts
512
+ io.puts "ca_bincmp_func_t"
513
+ io.puts "ca_bincmp_#{name}[CA_NTYPE] = {"
514
+ ALL_TYPES.each_index do |i|
515
+ type = nil
516
+ hash.each do |types, expr|
517
+ if not expr
518
+ next
519
+ end
520
+ type ||= types[i]
521
+ end
522
+ if type
523
+ io.puts(" ca_bincmp_#{name}_#{type},")
524
+ else
525
+ io.puts(" ca_bincmp_not_implement,")
526
+ end
527
+ end
528
+ io.puts "};"
529
+ io.puts
530
+ io.print %{
531
+ static VALUE rb_ca_#{name} (VALUE self, VALUE other)
532
+ { return rb_ca_call_bincmp(self, other, ca_bincmp_#{name}); }
533
+ }
534
+ DEFINITIONS << io.string
535
+ if op
536
+ METHODS << %{
537
+ rb_define_method(rb_cCArray, "#{op}", rb_ca_#{name}, 1);
538
+ }
539
+ end
540
+ end
541
+
542
+ def alias_op (op, name)
543
+ METHODS << %{
544
+ rb_define_alias(rb_cCArray, "#{op}", "#{name}");
545
+ }
546
+ end
547
+
548
+ ALL_TYPES = [
549
+ nil,
550
+ nil,
551
+ "int8_t",
552
+ "uint8_t",
553
+ "int16_t",
554
+ "uint16_t",
555
+ "int32_t",
556
+ "uint32_t",
557
+ "int64_t",
558
+ "uint64_t",
559
+ "float32_t",
560
+ "float64_t",
561
+ "float128_t",
562
+ nil,
563
+ nil,
564
+ nil,
565
+ nil,
566
+ ]
567
+
568
+ BOOL_TYPES = [
569
+ nil,
570
+ "boolean8_t",
571
+ nil, #"int8_t",
572
+ nil, #"uint8_t",
573
+ nil,
574
+ nil,
575
+ nil,
576
+ nil,
577
+ nil,
578
+ nil,
579
+ nil,
580
+ nil,
581
+ nil,
582
+ nil,
583
+ nil,
584
+ nil,
585
+ nil,
586
+ ]
587
+
588
+ INT_TYPES = [
589
+ nil,
590
+ nil,
591
+ "int8_t",
592
+ "uint8_t",
593
+ "int16_t",
594
+ "uint16_t",
595
+ "int32_t",
596
+ "uint32_t",
597
+ "int64_t",
598
+ "uint64_t",
599
+ nil,
600
+ nil,
601
+ nil,
602
+ nil,
603
+ nil,
604
+ nil,
605
+ nil,
606
+ ]
607
+
608
+ FLOAT_TYPES = [
609
+ nil,
610
+ nil,
611
+ nil,
612
+ nil,
613
+ nil,
614
+ nil,
615
+ nil,
616
+ nil,
617
+ nil,
618
+ nil,
619
+ "float32_t",
620
+ "float64_t",
621
+ "float128_t",
622
+ nil,
623
+ nil,
624
+ nil,
625
+ nil,
626
+ ]
627
+
628
+ CMPLX_TYPES = [
629
+ nil,
630
+ nil,
631
+ nil,
632
+ nil,
633
+ nil,
634
+ nil,
635
+ nil,
636
+ nil,
637
+ nil,
638
+ nil,
639
+ nil,
640
+ nil,
641
+ nil,
642
+ "cmplx64_t",
643
+ "cmplx128_t",
644
+ "cmplx256_t",
645
+ nil,
646
+ ]
647
+
648
+ OBJ_TYPES = [
649
+ nil,
650
+ nil,
651
+ nil,
652
+ nil,
653
+ nil,
654
+ nil,
655
+ nil,
656
+ nil,
657
+ nil,
658
+ nil,
659
+ nil,
660
+ nil,
661
+ nil,
662
+ nil,
663
+ nil,
664
+ nil,
665
+ "VALUE",
666
+ ]
667
+
668
+ FIXLEN_TYPES = [
669
+ "fixlen",
670
+ nil,
671
+ nil,
672
+ nil,
673
+ nil,
674
+ nil,
675
+ nil,
676
+ nil,
677
+ nil,
678
+ nil,
679
+ nil,
680
+ nil,
681
+ nil,
682
+ nil,
683
+ nil,
684
+ nil,
685
+ nil,
686
+ ]
687
+
688
+ begin
689
+ open("carray_config.h") { |io|
690
+ have_complex = false
691
+ while line = io.gets
692
+ if line =~ /HAVE_COMPLEX_H\s+(\d*)\s*/
693
+ have_complex = ($1.to_i == 1)
694
+ break
695
+ end
696
+ end
697
+ HAVE_COMPLEX = have_complex
698
+ }
699
+ end
700
+
701
+ HEADERS = ""
702
+ DEFINITIONS = ""
703
+ METHODS = ""
704
+
705
+ CODETEXT = <<HERE
706
+ <headers>
707
+
708
+ #include <math.h>
709
+ #include "carray.h"
710
+
711
+ <definitions>
712
+
713
+ void
714
+ Init_<name> ()
715
+ {
716
+ <methods>
717
+ }
718
+
719
+ HERE
720
+
721
+ def create_code (name, filename)
722
+ code = CODETEXT.clone
723
+ code.sub!("<name>", name)
724
+ code.sub!("<headers>", HEADERS)
725
+ code.sub!("<definitions>", DEFINITIONS)
726
+ code.sub!("<methods>", METHODS)
727
+ open(filename, "w") { |io|
728
+ io.write code
729
+ }
730
+ end
731
+