Class: CArray
- Inherits:
-
Object
- Object
- CArray
- Defined in:
- lib/carray/base/math.rb,
lib/carray/io/pg.rb,
lib/carray/io/csv.rb,
lib/carray/io/table.rb,
lib/carray/base/math.rb,
lib/carray/base/math.rb,
lib/carray/base/basic.rb,
lib/carray/base/basic.rb,
lib/carray/io/sqlite3.rb,
lib/carray/base/struct.rb,
lib/carray/base/inspect.rb,
lib/carray/base/iterator.rb,
lib/carray/base/iterator.rb,
lib/carray/base/obsolete.rb,
lib/carray/graphics/zimg.rb,
lib/carray/base/serialize.rb,
lib/carray/io/imagemagick.rb,
lib/carray/math/histogram.rb,
lib/carray/math/recurrence.rb,
lib/carray/object/ca_obj_pack.rb,
lib/carray/autoload/autoload_base.rb,
lib/carray/autoload/autoload_io_pg.rb,
lib/carray/autoload/autoload_io_csv.rb,
lib/carray/autoload/autoload_io_sqlite3.rb,
lib/carray/autoload/autoload_graphics_zimg.rb,
lib/carray/autoload/autoload_io_imagemagick.rb,
lib/carray/autoload/autoload_math_histogram.rb,
lib/carray/autoload/autoload_math_recurrence.rb,
ext/calculus/lib/math/calculus.rb,
ext/narray/lib/math/narray_miss.rb,
ext/fortio/lib/carray/io/fortran_format.rb,
ext/narray/lib/autoload/autoload_math_narray_miss.rb,
ext/fortio/lib/carray/autoload/autoload_fortran_format.rb
Overview
carray/base/calculus.rb
This file is part of Ruby/CArray extension library.
You can redistribute it and/or modify it under the terms of
the Ruby Licence.
Copyright (C) 2005 Hiroki Motoyoshi
Direct Known Subclasses
Defined Under Namespace
Classes: Inspector, Serializer
Constant Summary collapse
- HAVE_NARRAY =
“narray” should be loaded in config.rb
Qtrue- @@zimg_count =
0
Class Method Summary collapse
-
.__new__(type, *args) ⇒ Object
Create new CArray object from the return value of the block with data type
type. -
.__new_fixlen__(bytes, v) ⇒ Object
:nodoc:.
- .bind(data_type, list, at = 0) ⇒ Object
- .combine(data_type, tdim, list, at = 0) ⇒ Object
- .composite(data_type, tdim, list, at = 0) ⇒ Object
- .dump(ca, opt = {}) ⇒ Object
-
.from_binary(io, opt = {}) ⇒ Object
:nodoc:.
- .from_bit_string(bstr, nb, data_type = CA_INT32, dim = nil) ⇒ Object
- .from_csv(io, option = {}, rs: $/, sep: ",", &block) ⇒ Object
- .from_fortran_format(fmt, io) ⇒ Object
- .join(*argv) ⇒ Object
- .load(input, opt = {}) ⇒ Object
-
.load_binary(filename, opt = {}) ⇒ Object
:nodoc:.
-
.load_binary_io(io, opt = {}) ⇒ Object
:nodoc:.
- .load_by_magick(filename, imap = "rgb", data_type = nil) ⇒ Object
- .load_csv(file, option = {}, rs: $/, sep: ",", &block) ⇒ Object
-
.load_from_file(filename, data_type, dim, opt = {}) ⇒ Object
depleted methods.
- .load_pg(expr, *args) ⇒ Object
- .load_sqlite3(expr, *args) ⇒ Object
- .merge(data_type, list, at = -1)) ⇒ Object
- .pack(*argv) ⇒ Object
- .pickup(data_type, ref, args) ⇒ Object
- .save(ca, output, opt = {}) ⇒ Object
-
.span(*argv) ⇒ Object
CArray.span(data_type, range[, step]) CArray.span(range[, step]) -> data_type guessed by range.first type.
- .summation(*dim) ⇒ Object
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
(also: #cmp)
comparison operators.
-
#address ⇒ Object
index / indices / axes.
- #anomaly(*argv) ⇒ Object (also: #anom)
- #asign(*idx) ⇒ Object
- #attribute ⇒ Object
- #attribute=(obj) ⇒ Object
- #bin(val, include_upper, include_lowest, offset = 0) ⇒ Object
-
#block_iterator(*argv) ⇒ Object
:nodoc:.
- #by(other) ⇒ Object
- #classes(classifier = nil, &block) ⇒ Object
-
#classify(klass, outlier = nil) ⇒ Object
:nodoc:.
- #code ⇒ Object
-
#compact ⇒ Object
Returns the array which rank is reduced by eliminating the dimensions which size == 1.
-
#compacted ⇒ Object
Reutrns the reference which rank is reduced by eliminating the dimensions which size == 1.
- #correlation(y, min_count = nil, fill_value = nil) ⇒ Object
-
#count_masked(*argv) ⇒ Object
Returns the number of masked elements.
-
#count_not_masked(*argv) ⇒ Object
Returns the number of not-masked elements.
- #covariance(y, min_count = nil, fill_value = nil) ⇒ Object
- #covariancep(y, min_count = nil, fill_value = nil) ⇒ Object
- #deg_180 ⇒ Object
- #deg_180! ⇒ Object
- #deg_360 ⇒ Object
- #deg_360! ⇒ Object
- #delete_block(offset, bsize) ⇒ Object
- #differentiate ⇒ Object
- #display_by_magick(image_type = nil, options = "") ⇒ Object
-
#duplicated_values ⇒ Object
Returns the array eliminated all the duplicated elements.
- #escape_bytea(db) ⇒ Object
- #extend_as_table(column_names) ⇒ Object
-
#fa ⇒ Object
:nodoc:.
-
#false ⇒ Object
Returns the 8-bit integer CArray object filled with 0 which dimension size is same as
self. - #first ⇒ Object
- #flatten ⇒ Object
-
#flattened ⇒ Object
flatten.
- #from_bit_string(bstr, nb) ⇒ Object
- #has_attribute? ⇒ Boolean
-
#imag ⇒ Object
Return the imaginary part of
self. - #imag=(val) ⇒ Object
- #index(n) ⇒ Object
- #indices ⇒ Object
-
#insert_block(offset, bsize, &block) ⇒ Object
insert.
- #inspect ⇒ Object
- #integrate ⇒ Object
- #interp_nd_linear ⇒ Object
- #interpolate ⇒ Object
- #interpolate2(x, y, x0, y0) ⇒ Object
- #is_close(other, atol) ⇒ Object
- #is_divisible(n) ⇒ Object
- #is_equiv(other, rtol) ⇒ Object
- #is_not_divisible(n) ⇒ Object
- #is_real ⇒ Object
-
#join(*argv) ⇒ Object
Array#join like method.
- #last ⇒ Object
-
#map(&block) ⇒ Object
Returns map.
-
#marshal_dump ⇒ Object
for Marshal.
- #marshal_load(data) ⇒ Object
- #maskout(*argv) ⇒ Object
- #maskout!(*argv) ⇒ Object
-
#matchup(ref) ⇒ Object
matchup.
- #matchup_nearest(ref) ⇒ Object
- #max_by(&block) ⇒ Object
- #median(*argv) ⇒ Object
- #min_by(&block) ⇒ Object
-
#na ⇒ Object
rdoc: class CArray def na end def nv end def nm end end.
-
#nm ⇒ Object
CArray -> NMatrix.
- #normalize(scale = nil) ⇒ Object
- #normalize!(scale = nil) ⇒ Object
-
#nv ⇒ Object
CArray -> NVector.
- #order(dir = 1) ⇒ Object
- #percentile(*argv) ⇒ Object
- #pull(*args) ⇒ Object
-
#pulled(*args) ⇒ Object
pulled.
- #quantile ⇒ Object
- #quo(other) ⇒ Object
- #rad_2pi ⇒ Object
- #rad_2pi! ⇒ Object
- #rad_pi ⇒ Object
- #rad_pi! ⇒ Object
-
#random(*argv) ⇒ Object
statistics.
- #randomn ⇒ Object
- #randomn! ⇒ Object
- #range ⇒ Object
-
#real ⇒ Object
Return the real part of
self. - #real=(val) ⇒ Object
- #real? ⇒ Boolean
- #recurrence(*argv, &block) ⇒ Object
- #recurrence!(init = {}, &block) ⇒ Object
-
#replace(from, to) ⇒ Object
replace.
-
#reshape(*newdim) ⇒ Object
reshape.
-
#resize(*newdim, &block) ⇒ Object
Returns the array resized to the dimension given as
newdim. -
#reversed ⇒ Object
reversed.
- #roll(*argv) ⇒ Object
- #roll!(*argv) ⇒ Object
- #rolled(*argv) ⇒ Object
-
#rotate(*argv) ⇒ Object
:nodoc:.
-
#rotate!(*argv) ⇒ Object
:nodoc:.
-
#rotated(*argv) ⇒ Object
:nodoc:.
-
#save_binary(filename, opt = {}) ⇒ Object
obsolete methods.
-
#save_binary_io(io, opt = {}) ⇒ Object
:nodoc:.
- #save_by_magick(filename, image_type = nil, options = "") ⇒ Object
- #save_csv(file, option = {}, rs: $/, sep: ",", fill: "", mode: "w", &block) ⇒ Object
- #scale(xa, xb) ⇒ Object
- #scale!(xa, xb) ⇒ Object
- #section ⇒ Object
-
#select(&block) ⇒ Object
:nodoc:.
- #shift(*argv, &block) ⇒ Object
-
#shift!(*argv, &block) ⇒ Object
roll / shift.
- #sign ⇒ Object
- #solve(sc, val, eps = 100 * Float::EPSILON) ⇒ Object
- #solve2(sc, eps = 100 * Float::EPSILON) ⇒ Object
-
#sort_by(type = nil, opt = {}, &block) ⇒ Object
Returns the array which elements are sorted by the comparison method given as block.
-
#sorted_by(type = nil, opt = {}, &block) ⇒ Object
Returns the reference which elements are sorted by the comparison method given as block.
- #span(range) ⇒ Object
- #span!(range) ⇒ Object
-
#split(*argv) ⇒ Object
Returns object carray has elements of splitted carray at dimensions which is given by arguments.
- #st ⇒ Object
-
#to_binary(io = "", opt = {}) ⇒ Object
:nodoc:.
- #to_bit_string(nb) ⇒ Object
-
#to_column ⇒ Object
Returns (n,1) array from 1-dimensional array.
- #to_csv(io = "", option = {}, rs: $/, sep: ",", fill: "", &block) ⇒ Object
- #to_fortran_format(fmt, input = "") ⇒ Object
-
#to_na ⇒ Object
rdoc: class CArray def to_na end def to_nv end def to_nm end end.
-
#to_na! ⇒ Object
rdoc: class CArray def to_na! end def to_nv! end def to_nm! end end.
- #to_nam ⇒ Object
- #to_nam! ⇒ Object
- #to_nm ⇒ Object
- #to_nm! ⇒ Object
- #to_nv ⇒ Object
- #to_nv! ⇒ Object
-
#to_row ⇒ Object
Returns (1,n) array from 1-dimensional array.
- #to_sqlite3(database: nil, table:, datatype: "numeric", schema: nil, transaction: true) ⇒ Object
- #to_tabular(option = {}) ⇒ Object
-
#transform(type, dim, opt = {}) ⇒ Object
:nodoc:.
- #transpose(*argv) ⇒ Object
- #transpose!(*argv) ⇒ Object
-
#true ⇒ Object
Returns the 8-bit integer CArray object filled with 1 which dimension size is same as
self. - #unblob(type, subdim = nil) ⇒ Object
- #unescape_bytea(type, subdim = nil) ⇒ Object
-
#uniq ⇒ Object
Returns the array eliminated all the duplicated elements.
- #where_range ⇒ Object
-
#window_iterator(*argv) ⇒ Object
:nodoc:.
- #windows(*args, &block) ⇒ Object
-
#zimg(option = {}) ⇒ Object
:nodoc:.
Class Method Details
.__new__(type, *args) ⇒ Object
Create new CArray object from the return value of the block with data type type. The dimensional size and the initialization value are guessed from the return value of the block. The block should return one of the following objects.
-
Numeric
-
Array
-
CArray
-
an object that has either method
to_caorto_aormap
When the return value of the block is a Numeric or CScalar object, CScalar object is returned.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/carray/base/basic.rb', line 52 def CArray.__new__ (type, *args) # :nodoc: case v = args.first when CArray return ( v.data_type == type ) ? v.to_ca : v.to_type(type) when Array return CArray.new(type, CArray.guess_array_shape(v)) { v } when Range return CArray.span(type, *args) when String if type == CA_OBJECT return CScalar.new(CA_OBJECT) { v } elsif type == CA_BOOLEAN v = v.dup v.tr!('^01',"1") v.tr!('01',"\x0\x1") return CArray.boolean(v.length).load_binary(v) else case v when /;/ v = v.strip.split(/\s*;\s*/). map{|s| s.split(/\s+|\s*,\s*/).map{|x| x=='_' ? UNDEF : x} } else v = v.strip.split(/\s+|\s*,\s*/).map{|x| x=='_' ? UNDEF : x} end return CArray.new(type, CArray.guess_array_shape(v)) { v } end when NilClass return CArray.new(type, [0]) else if v.respond_to?(:to_ca) ca = v.to_ca return ( ca.data_type == type ) ? ca : ca.to_type(type) else return CScalar.new(type) { v } end end end |
.__new_fixlen__(bytes, v) ⇒ Object
:nodoc:
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/carray/base/basic.rb', line 90 def CArray.__new_fixlen__ (bytes, v) # :nodoc: case v when CArray return ( v.data_type == :fixlen ) ? v.to_ca : v.to_type(:fixlen, :bytes=>bytes) when Array unless bytes bytes = v.map{|s| s.length}.max end return CArray.new(:fixlen, CArray.guess_array_shape(v), :bytes=>bytes) { v } when NilClass return CArray.new(type, [0]) else if v.respond_to?(:to_ca) ca = v.to_ca return ( ca.data_type == :fixlen ) ? ca : ca.to_type(:fixlen, :bytes=>bytes) else return CScalar.new(:fixlen, :bytes=>bytes) { v } end end end |
.bind(data_type, list, at = 0) ⇒ Object
898 899 900 |
# File 'lib/carray/base/basic.rb', line 898 def self.bind (data_type, list, at = 0) return CArray.combine(data_type, [list.size], list, at) end |
.combine(data_type, tdim, list, at = 0) ⇒ Object
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 |
# File 'lib/carray/base/basic.rb', line 834 def self.combine (data_type, tdim, list, at = 0) has_fill_value = false if block_given? fill_value = yield has_fill_value = true end if not tdim.is_a?(Array) or tdim.size == 0 raise "invalid binding dimension" end if not list.is_a?(Array) or list.size == 0 raise "invalid list" end list = list.map{|x| CArray.wrap_readonly(x, data_type) } ref = list.detect{|x| x.is_a?(CArray) or not x.scalar? } unless ref raise "at least one element in list should be a carray" end dim = ref.dim rank = ref.rank trank = tdim.size if at < 0 at += rank - trank + 1 end unless at.between?(0, rank - trank) raise "concatnating position out of range" end list.map! do |x| if x.scalar? rdim = dim.clone rdim[at] = :% x = x[*rdim] # convert CScalar to CARepeat end x end block = CArray.object(*tdim){ list } edim = tdim.clone idx = Array.new(tdim) offset = Array.new(tdim.size) { [] } tdim.each_with_index do |td, i| edim[i] = 0 idx.map!{0} idx[i] = nil block[*idx].each do |e| offset[i] << edim[i] edim[i] += e.dim[at+i] # extended dimension size end end newdim = dim.clone newdim[at,trank] = edim # extended dimension size if has_fill_value obj = CArray.new(data_type, newdim) { fill_value } else obj = CArray.new(data_type, newdim) end idx = newdim.map{0} block.each_with_index do |item, tidx| (at...at+trank).each_with_index do |d,i| idx[d] = offset[i][tidx[i]] end obj.paste(idx, item) end obj end |
.composite(data_type, tdim, list, at = 0) ⇒ Object
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
# File 'lib/carray/base/basic.rb', line 902 def self.composite (data_type, tdim, list, at = 0) if not tdim.is_a?(Array) or tdim.size == 0 raise "invalid tiling dimension" end if not list.is_a?(Array) or list.size == 0 raise "invalid carray list" end list = list.map{|x| CArray.wrap_readonly(x, data_type) } ref = list.detect{|x| x.is_a?(CArray) or not x.scalar? } unless ref raise "at least one element in list should be a carray" end dim = ref.dim rank = ref.rank if at < 0 at += rank + 1 # "+ 1" is needed here end unless at.between?(0,rank) raise "tiling position is out of range" end trank = tdim.size list.map! do |x| if x.scalar? rdim = dim.clone rdim[at] = :% x = x[*rdim] # convert CScalar to CARepeat end x end newdim = dim.clone newdim[at,0] = tdim obj = CArray.new(data_type, newdim) idx = Array.new(rank+trank) { nil } CArray.each_index(*tdim) do |*tidx| idx[at,trank] = tidx obj[*idx] = list.shift end obj end |
.dump(ca, opt = {}) ⇒ Object
195 196 197 198 199 |
# File 'lib/carray/base/serialize.rb', line 195 def self.dump (ca, opt={}) io = StringIO.new("") Serializer.new(io).save(ca, opt) return io.string end |
.from_binary(io, opt = {}) ⇒ Object
:nodoc:
248 249 250 251 |
# File 'lib/carray/base/serialize.rb', line 248 def self.from_binary (io, opt={}) # :nodoc: warn "CArray.from_binary will be obsolete, use CArray.load" return Serializer.new(io).load(opt) end |
.from_bit_string(bstr, nb, data_type = CA_INT32, dim = nil) ⇒ Object
786 787 788 789 790 791 792 793 794 795 |
# File 'lib/carray/base/basic.rb', line 786 def self.from_bit_string (bstr, nb, data_type=CA_INT32, dim=nil) if dim obj = CArray.new(data_type, dim) else dim0 = ((bstr.length*8)/nb.to_f).floor obj = CArray.new(data_type, [dim0]) end obj.from_bit_string(bstr, nb) return obj end |
.from_csv(io, option = {}, rs: $/, sep: ",", &block) ⇒ Object
527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/carray/io/csv.rb', line 527 def self.from_csv (io, option={}, rs: $/, sep: ",", &block) option = {:sep=>sep, :rs=>rs}.update(option) reader = CA::CSVReader.new(option[:sep], option[:rs], &block) case io when IO, StringIO return reader.read_io(io) when String return reader.read_string(io) else raise "invalid argument" end end |
.from_fortran_format(fmt, io) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'ext/fortio/lib/carray/io/fortran_format.rb', line 5 def self.from_fortran_format (fmt, io) case io when String io = StringIO.new(io) end case fmt when String fmt = FortranFormat.new(fmt) end data = [] begin data << fmt.read(io) end until io.eof? return data.to_ca end |
.join(*argv) ⇒ Object
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'lib/carray/base/basic.rb', line 946 def self.join (*argv) # get options case argv.first when Integer, Symbol, String type, = *CArray.guess_type_and_bytes(argv.shift, 0) else type = argv.flatten.first.data_type end # process conc = argv.map do |list| case list when CArray if list.rank == 1 list[:%,1] else list end when Array x0 = list.first if list.size == 1 and x0.is_a?(CArray) and x0.rank == 1 list = [x0[:%,1]] else list = list.map { |x| case x when CArray if x.rank == 1 x[:%,1] else x end when Array y = x.first if x.size == 1 and y.is_a?(CArray) and y.rank == 1 y[1,:%] else CArray.join(*x) end else x end } end if block_given? CArray.bind(type, list, 1, &block) else CArray.bind(type, list, 1) end else list end end if conc.size > 1 return CArray.bind(type, conc) else return conc.first end end |
.load(input, opt = {}) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/carray/base/serialize.rb', line 179 def self.load (input, opt={}) case input when String if input.length >= 256 and input =~ /\A_CARRAY_.{8}_(LE|BE)_/ io = StringIO.new(input) return Serializer.new(io).load(opt) else open(input, "rb:ASCII-8BIT") { |io| return Serializer.new(io).load(opt) } end else return Serializer.new(input).load(opt) end end |
.load_binary(filename, opt = {}) ⇒ Object
:nodoc:
225 226 227 228 229 230 |
# File 'lib/carray/base/serialize.rb', line 225 def self.load_binary (filename, opt={}) # :nodoc: warn "CArray.load_binary will be obsolete, use CArray.load" open(filename) { |io| return Serializer.new(io).load(opt) } end |
.load_binary_io(io, opt = {}) ⇒ Object
:nodoc:
237 238 239 240 |
# File 'lib/carray/base/serialize.rb', line 237 def self.load_binary_io (io, opt={}) # :nodoc: warn "CArray#load_binary_io will be obsolete, use CArray.load" return Serializer.new(io).load(opt) end |
.load_by_magick(filename, imap = "rgb", data_type = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/carray/io/imagemagick.rb', line 48 def self.load_by_magick (filename, imap = "rgb", data_type = nil) if not File.exist?(filename) raise "can't find image file '#{filename}'" end identify_command = [ "identify", "-format " + "'" + [ "---", "height: %h", "width: %w", "depth: %z", ].join("\n") + "'", filename, "2>/dev/null" ].join(" ") ident = YAML.load(`#{identify_command}`) if ident.empty? raise "ImageMagick's identify command failed to read image file '#{filename}'" end height, width, depth = ident.values_at('height', 'width', 'depth') unless data_type case depth when 8 data_type = CA_UINT8 when 16 data_type = CA_UINT16 when 32 data_type = CA_UINT32 end end storage_type = case data_type when CA_UINT8, CA_INT8 "char" when CA_UINT16, CA_INT16 "short" when CA_UINT32, CA_INT32 "integer" when CA_FLOAT32 "float" when CA_FLOAT64 "double" else raise "invalid data_type" end tempfile = "CA_Magick_#{$$}_#{@@magick_tempfile_count}.dat" @@magick_tempfile_count += 1 stream_command = [ "stream", "-storage-type #{storage_type}", "-map #{imap}", filename, tempfile, "2>/dev/null" ].join(" ") begin system stream_command return open(tempfile) { |io| if imap.size == 1 CArray.new(data_type, [height, width]).load_binary(io) else CArray.new(data_type, [height, width, imap.size]).load_binary(io) end } rescue raise "ImageMagick's stream command failed to read image file '#{filename}'" ensure if File.exist?(tempfile) File.unlink(tempfile) end end end |
.load_csv(file, option = {}, rs: $/, sep: ",", &block) ⇒ Object
521 522 523 524 525 |
# File 'lib/carray/io/csv.rb', line 521 def self.load_csv (file, option={}, rs: $/, sep: ",", &block) option = {:sep=>sep, :rs=>rs}.update(option) reader = CA::CSVReader.new(option[:sep], option[:rs], &block) return reader.read_file(file) end |
.load_from_file(filename, data_type, dim, opt = {}) ⇒ Object
depleted methods
255 256 257 |
# File 'lib/carray/base/serialize.rb', line 255 def self.load_from_file (filename, data_type, dim, opt={}) # :nodoc: raise "Sorry, CArray.load_from_file is depleted" end |
.load_pg(expr, *args) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/carray/io/pg.rb', line 6 def self.load_pg (expr, *args) case expr when Hash opts = expr sql, *vars = *args db = PG.connect(opts) begin result = db.async_exec(sql, *vars) names = result.fields table = result.values rescue Interrupt db.cancel raise ensure db.finish end when PG::Result result = expr names = result.fields table = result.values when PG::Connection db = expr sql, *vars = *args begin result = db.async_exec(sql, *vars) names = result.fields table = result.values rescue Interrupt db.cancel raise end else raise "invalid 1st arg" end table = table.to_ca table.instance_exec { @result = result @names = names } class << table attr_reader :names, :result def column (name) if self.size == 0 if name.is_a?(Integer) return CA_OBJECT([]) elsif @names if i = @names.index(name.to_s) return CA_OBJECT([]) end end return nil else if name.is_a?(Integer) return self[false, name] elsif @names if i = @names.index(name.to_s) return self[false, i] end end return nil end end end return table end |
.load_sqlite3(expr, *args) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/carray/io/sqlite3.rb', line 7 def self.load_sqlite3 (expr, *args) case expr when String file = expr sql, *vars = *args db = SQLite3::Database.new(file) names, *table = db.execute2(sql, *vars) db.close when SQLite3::Database db = expr sql, *vars = *args names, *table = db.execute2(sql, *vars) when SQLite3::Statement stmt = expr vars = args names = stmt.columns table = stmt.execute!(*vars) else raise "invalid 1st arg" end table = table.to_ca table.extend(CA::TableMethods) table.column_names = names return table end |
.merge(data_type, list, at = -1)) ⇒ Object
942 943 944 |
# File 'lib/carray/base/basic.rb', line 942 def self.merge (data_type, list, at = -1) return CArray.composite(data_type, [list.size], list, at) end |
.pack(*argv) ⇒ Object
98 99 100 |
# File 'lib/carray/object/ca_obj_pack.rb', line 98 def self.pack (*argv) return CAPack.new(argv) end |
.pickup(data_type, ref, args) ⇒ Object
a = CArray.int(3,3).seq(1)
b = CArray.int(3,3).seq(11)
c = CArray.int(3,3).seq(21)
CArray.pickup(CA_OBJECT, ref, [a,b,c])
=> <CArray.object(3,3): elem=9 mem=72b
[ [ 1, 12, 23 ],
[ 14, 25, 6 ],
[ 27, 8, 19 ] ]>
CArray.pickup(CA_OBJECT, ref, ["a","b","c"])
=> <CArray.object(3,3): elem=9 mem=36b
[ [ "a", "b", "c" ],
[ "b", "c", "a" ],
[ "c", "a", "b" ] ]>
820 821 822 823 824 825 826 827 828 829 830 831 832 |
# File 'lib/carray/base/basic.rb', line 820 def self.pickup (data_type, ref, args) out = ref.template(data_type) args.each_with_index do |v, i| s = ref.eq(i) case v when CArray out[s] = v[s] else out[s] = v end end return out end |
.save(ca, output, opt = {}) ⇒ Object
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/carray/base/serialize.rb', line 168 def self.save(ca, output, opt={}) case output when String open(output, "wb:ASCII-8BIT") { |io| return Serializer.new(io).save(ca, opt) } else return Serializer.new(output).save(ca, opt) end end |
.span(*argv) ⇒ Object
CArray.span(data_type, range[, step]) CArray.span(range[, step]) -> data_type guessed by range.first type
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/carray/base/basic.rb', line 618 def self.span (*argv) if argv.first.is_a?(Range) type = nil else type, = *CArray.guess_type_and_bytes(argv.shift, nil) end range, step = argv[0], argv[1] start, stop = range.begin, range.end if step == 0 raise "step should not be 0" end if not type case start when Integer type = CA_INT32 when Float type = CA_FLOAT64 else type = CA_OBJECT end end if type == CA_OBJECT and not step return CA_OBJECT(range.to_a) else step ||= 1 if range.exclude_end? n = ((stop - start).abs/step).floor else n = ((stop - start).abs/step).floor + 1 end if start <= stop return CArray.new(type, [n]).seq(start, step) else return CArray.new(type, [n]).seq(start, -step.abs) end end end |
.summation(*dim) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/carray/base/math.rb', line 378 def self.summation (*dim) out = nil first = true CArray.each_index(*dim) { |*idx| if first out = yield(*idx) first = false else out += yield(*idx) end } return out end |
Instance Method Details
#<=>(other) ⇒ Object Also known as: cmp
comparison operators
83 84 85 86 87 88 89 90 |
# File 'lib/carray/base/math.rb', line 83 def <=> (other) lower = self < other upper = self > other out = CArray.new(CA_INT8, lower.dim) out[lower] = -1 out[upper] = 1 return out end |
#address ⇒ Object
index / indices / axes
687 688 689 |
# File 'lib/carray/base/basic.rb', line 687 def address () return CArray.int32(*dim).seq! end |
#anomaly(*argv) ⇒ Object Also known as: anom
227 228 229 230 231 232 233 234 235 |
# File 'lib/carray/base/math.rb', line 227 def anomaly (*argv) opt = argv.last.is_a?(Hash) ? argv.pop : {} idxs = Array.new(self.rank) { |i| argv.include?(i) ? :* : nil } if mn = opt[:mean] return self - mn[*idxs] else return self - self.mean(*argv)[*idxs] end end |
#asign(*idx) ⇒ Object
199 200 201 202 |
# File 'lib/carray/base/basic.rb', line 199 def asign (*idx) self[*idx] = yield return self end |
#attribute ⇒ Object
182 183 184 185 |
# File 'lib/carray/base/basic.rb', line 182 def attribute @attribute ||= {} return @attribute end |
#attribute=(obj) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/carray/base/basic.rb', line 175 def attribute= (obj) unless obj.is_a?(Hash) raise "attribute should be a hash object" end @attribute = obj end |
#bin(val, include_upper, include_lowest, offset = 0) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/carray/math/histogram.rb', line 120 def bin (val, include_upper, include_lowest, offset=0) scales = CArray.wrap_readonly(self, CA_DOUBLE) x = scales.section(val) # x.inherit_mask(val) unless x.is_a?(CArray) x = CA_DOUBLE(x) end if include_upper if include_lowest x[:eq, 0] = 0.5 end xi = x.ceil.int32 - 1 else xi = x.floor.int32 end case offset when 0 xi[:gt, elements-1] = elements - 1 xi[:lt, 0] = UNDEF when 1 xi.add!(1) xi[:gt, elements] = elements xi[:lt, 1] = 0 else raise "invalid offset value" end return xi end |
#block_iterator(*argv) ⇒ Object
:nodoc:
9 10 11 12 |
# File 'lib/carray/base/obsolete.rb', line 9 def block_iterator (*argv) # :nodoc: warn "CArray#block_iterator will be obsolete, use CArray#blocks" return blocks(*argv) end |
#by(other) ⇒ Object
392 393 394 395 396 397 398 399 |
# File 'lib/carray/base/math.rb', line 392 def by (other) case other when CArray return (self[nil][nil,:*]*other[nil][:*,nil]).reshape(*(dim+other.dim)) else return self * other end end |
#classes(classifier = nil, &block) ⇒ Object
306 307 308 |
# File 'lib/carray/base/iterator.rb', line 306 def classes (classifier=nil, &block) return CAClassIterator.new(self, classifier).__build__(&block) end |
#classify(klass, outlier = nil) ⇒ Object
:nodoc:
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/carray/base/obsolete.rb', line 53 def classify (klass, outlier = nil) # :nodoc: warn "CArray#classify will be obsolete" b = CArray.int32(*dim) f = CArray.boolean(*dim) { 1 } attach { (klass.elements-1).times do |i| r = f.and(self < klass[i+1]) b[r] = i f[r] = 0 end if outlier b[self < klass[0]] = outlier b[f] = outlier else b[self < klass[0]] = -1 b[f] = klass.elements-1 end } return b end |
#code ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/carray/base/inspect.rb', line 238 def code text = [ desc, " { ", self.to_a.pretty_inspect.split("\n").map{|s| " " * (desc.length+3) + s }.join("\n").lstrip, " }" ].join return text end |
#compact ⇒ Object
Returns the array which rank is reduced by eliminating the dimensions which size == 1
383 384 385 386 |
# File 'lib/carray/base/basic.rb', line 383 def compact newdim = dim.reject{|x| x == 1 } return ( rank != newdim.size ) ? reshape(*newdim).to_ca : self.to_ca end |
#compacted ⇒ Object
Reutrns the reference which rank is reduced by eliminating the dimensions which size == 1
376 377 378 379 |
# File 'lib/carray/base/basic.rb', line 376 def compacted newdim = dim.reject{|x| x == 1 } return ( rank != newdim.size ) ? reshape(*newdim) : self[] end |
#correlation(y, min_count = nil, fill_value = nil) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/carray/base/math.rb', line 354 def correlation (y, min_count = nil, fill_value = nil) x = self.double y = y.double if x.has_mask? or y.has_mask? x.inherit_mask(y) y.inherit_mask(x) xm = x.mean(:min_count=>min_count) ym = y.mean(:min_count=>min_count) if ( xm.undef? or ym.undef? ) return fill_value || UNDEF else xd, yd = x-xm, y-ym return xd.wsum(yd)/(xd.wsum(xd)*yd.wsum(yd)).sqrt end else xd, yd = x-x.mean, y-y.mean return xd.wsum(yd)/(xd.wsum(xd)*yd.wsum(yd)).sqrt end end |
#count_masked(*argv) ⇒ Object
Returns the number of masked elements.
210 211 212 213 214 215 216 |
# File 'lib/carray/base/basic.rb', line 210 def count_masked (*argv) if has_mask? return mask.int32.accumulate(*argv) else return 0 end end |
#count_not_masked(*argv) ⇒ Object
Returns the number of not-masked elements.
221 222 223 224 225 226 227 |
# File 'lib/carray/base/basic.rb', line 221 def count_not_masked (*argv) if has_mask? return mask.not.int32.accumulate(*argv) else return elements end end |
#covariance(y, min_count = nil, fill_value = nil) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/carray/base/math.rb', line 335 def covariance (y, min_count = nil, fill_value = nil) x = self.double y = y.double if x.has_mask? or y.has_mask? x.inherit_mask(y) y.inherit_mask(x) count = x.count_not_masked xm = x.mean(:min_count=>min_count) ym = y.mean(:min_count=>min_count) if ( xm.undef? or ym.undef? ) return fill_value || UNDEF else return (x-xm).wsum(y-ym)/(count-1) end else return (x-x.mean).wsum(y-y.mean)/(elements-1) end end |
#covariancep(y, min_count = nil, fill_value = nil) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/carray/base/math.rb', line 316 def covariancep (y, min_count = nil, fill_value = nil) x = self.double y = y.double if x.has_mask? or y.has_mask? x.inherit_mask(y) y.inherit_mask(x) count = x.count_not_masked xm = x.mean(:min_count => min_count) ym = y.mean(:min_count => min_count) if ( xm.undef? or ym.undef? ) return fill_value || UNDEF else return (x-xm).wsum(y-ym)/count end else return (x-x.mean).wsum(y-y.mean)/elements end end |
#deg_180 ⇒ Object
78 79 80 81 82 |
# File 'ext/mathfunc/carray_mathfunc.c', line 78
static VALUE
rb_ca_deg_180 (VALUE self)
{
return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_180, self);
}
|
#deg_180! ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'ext/mathfunc/carray_mathfunc.c', line 84
static VALUE
rb_ca_deg_180_bang (VALUE self)
{
volatile VALUE out;
out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_180, self);
rb_funcall(self, rb_intern("[]="), 1, out);
return self;
}
|
#deg_360 ⇒ Object
36 37 38 39 40 |
# File 'ext/mathfunc/carray_mathfunc.c', line 36
static VALUE
rb_ca_deg_360 (VALUE self)
{
return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_360, self);
}
|
#deg_360! ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'ext/mathfunc/carray_mathfunc.c', line 42
static VALUE
rb_ca_deg_360_bang (VALUE self)
{
volatile VALUE out;
out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_deg_360, self);
rb_funcall(self, rb_intern("[]="), 1, out);
return self;
}
|
#delete_block(offset, bsize) ⇒ Object
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/carray/base/basic.rb', line 500 def delete_block (offset, bsize) if offset.size != rank or bsize.size != rank raise "rank mismatch" end newdim = dim grids = [] rank.times do |i| if offset[i] < 0 offset[i] += dim[i] end if bsize[i] >= 0 if offset[i] < 0 or offset[i] >= dim[i] raise "invalid offset or size" end newdim[i] -= bsize[i] else if offset[i] + bsize[i] + 1 < 0 or offset[i] + bsize[i] > dim[i] raise "invalid offset or size" end newdim[i] += bsize[i] end grids[i] = CArray.int32(newdim[i]) if bsize[i] >= 0 if offset[i] > 0 grids[i][0...offset[i]].seq! end if offset[i] + bsize[i] < dim[i] grids[i][offset[i]..-1].seq!(offset[i]+bsize[i]) end else if offset[i]+bsize[i] > 0 grids[i][0..offset[i]+bsize[i]].seq! end if offset[i]+bsize[i]+1 < dim[i]-1 grids[i][offset[i]+bsize[i]+1..-1].seq!(offset[i]+1) end end end return self[*grids].to_ca end |
#differentiate ⇒ Object
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'ext/calculus/carray_calculus.c', line 707
static VALUE
rb_ca_differentiate (volatile VALUE self,
volatile VALUE vsc, volatile VALUE vx)
{
volatile VALUE rval = self;
volatile VALUE out0, out;
CArray *ca, *cv, *sc, *cx, *co0, *co;
double *px, *po;
ca_size_t i;
Data_Get_Struct(self, CArray, ca);
cv = ca_wrap_readonly(rval, CA_DOUBLE);
sc = ca_wrap_readonly(vsc, CA_DOUBLE);
if ( ca_is_any_masked(cv) || ca_is_any_masked(sc) ) {
rb_raise(rb_eRuntimeError,
"can't calculate differentiation when masked elements exist");
}
if ( cv->elements != sc->elements ) {
rb_raise(rb_eRuntimeError, "data num mismatch with scale");
}
cx = ca_wrap_readonly(vx, CA_DOUBLE);
co0 = carray_new(ca->data_type, cx->rank, cx->dim, 0, NULL);
out = out0 = ca_wrap_struct(co0);
co = ca_wrap_writable(out, CA_DOUBLE);
ca_attach_n(4, cv, sc, cx, co);
px = (double*) cx->ptr;
po = (double*) co->ptr;
ca_update_mask(cx);
if ( cx->mask ) {
boolean8_t *mx, *mo;
ca_create_mask(co);
mx = (boolean8_t *) cx->mask->ptr;
mo = (boolean8_t *) co->mask->ptr;
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = differentiate((double*)sc->ptr, (double*)ca->ptr,
ca->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; px++, po++;
}
}
else {
for (i=0; i<cx->elements; i++) {
*po = differentiate((double*)sc->ptr, (double*)ca->ptr,
ca->elements, *px);
px++, po++;
}
}
ca_sync(co);
ca_detach_n(4, cv, sc, cx, co);
if ( rb_ca_is_scalar(vx) ) {
return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
}
else {
return out0;
}
}
|
#display_by_magick(image_type = nil, options = "") ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/carray/io/imagemagick.rb', line 202 def display_by_magick (image_type = nil, = "") unless image_type image_type = magick_guess_image_type() end unless image_type raise "please specify image_type" end quantum_format = self.float? ? "-define quantum:format=floating-point" : "" depth = fixlen? ? "-depth 8" : "-depth #{8*bytes}" display_command = [ "display", depth, "-size " + [dim1, dim0].join("x"), quantum_format, , "#{image_type}:-", ].join(" ") begin IO.popen(display_command, "w") { |io| if bytes > 1 and CArray.endian == CA_LITTLE_ENDIAN swap_bytes.dump_binary(io) else self.dump_binary(io) end } rescue raise "ImageMagick's display command failed to display image" end end |
#duplicated_values ⇒ Object
Returns the array eliminated all the duplicated elements.
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/carray/base/basic.rb', line 593 def duplicated_values if uniq.size == size return [] else hash = {} list = [] each_with_addr do |v, addr| if v == UNDEF next elsif hash[v] list << [v, addr, hash[v]] hash[v] += 1 else hash[v] = 0 end end return list end end |
#escape_bytea(db) ⇒ Object
96 97 98 |
# File 'lib/carray/io/pg.rb', line 96 def escape_bytea (db) return db.escape_bytea(self.to_s) end |
#extend_as_table(column_names) ⇒ Object
72 73 74 75 |
# File 'lib/carray/io/table.rb', line 72 def extend_as_table (column_names) self.extend CA::TableMethods self.column_names = column_names end |
#fa ⇒ Object
:nodoc:
4 5 6 7 |
# File 'lib/carray/base/obsolete.rb', line 4 def fa # :nodoc: warn "CArray#fa will be obsolete, use CArray#t" return self.t end |
#false ⇒ Object
Returns the 8-bit integer CArray object filled with 0 which dimension size is same as self. The resulted array represents the logical array which has false for its all elements.
725 726 727 |
# File 'lib/carray/base/basic.rb', line 725 def false () return template(:boolean) end |
#first ⇒ Object
187 188 189 |
# File 'lib/carray/base/basic.rb', line 187 def first self[0] end |
#flatten ⇒ Object
318 319 320 |
# File 'lib/carray/base/basic.rb', line 318 def flatten return reshape(elements).to_ca end |
#flattened ⇒ Object
flatten
314 315 316 |
# File 'lib/carray/base/basic.rb', line 314 def flattened return reshape(elements) end |
#from_bit_string(bstr, nb) ⇒ Object
778 779 780 781 782 783 784 |
# File 'lib/carray/base/basic.rb', line 778 def from_bit_string (bstr, nb) hex = CArray.uint8(bstr.length).load_binary(bstr) hex.bits[] = hex.bits[nil,[-1..0]] bits = hex.bits.flatten self.bits[false,[(nb-1)..0]][nil].paste([0], bits) return self end |
#has_attribute? ⇒ Boolean
167 168 169 170 171 172 173 |
# File 'lib/carray/base/basic.rb', line 167 def has_attribute? if ( not @attribute ) or @attribute.empty? return false else return true end end |
#imag ⇒ Object
Return the imaginary part of self. If self is a complex array, the resulted array is CAMember object refers the appropriate part of self. In this case, you change the resulted array, the original array is also changed.
Otherwise, the resulted array is a dummy CArray object filled with 0. In this case, the change in the resulted array does not affect the original array. For this purpose, you should explicitly convert the array to complex array.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/carray/base/math.rb', line 55 def imag if not @__imag__ if complex? @__imag__ = case data_type when CA_CMPLX64 field(4, CA_FLOAT32) when CA_CMPLX128 field(8, CA_FLOAT64) when CA_CMPLX128 field(16, CA_FLOAT128) end else @__imag__ = self.template { 0 } end end return @__imag__ end |
#imag=(val) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/carray/base/math.rb', line 73 def imag= (val) if complex? imag[] = val else raise "not a complex array" end end |
#index(n) ⇒ Object
691 692 693 694 695 696 697 698 699 700 701 |
# File 'lib/carray/base/basic.rb', line 691 def index (n) unless n.is_a?(Integer) raise ArgumentError, "argument should be an integer" end if n.between?(0, rank-1) return CArray.int32(dim[n]).seq! else raise ArgumentError, "invalid dimension specifier #{n} (0..#{self.rank-1})" end end |
#indices ⇒ Object
707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/carray/base/basic.rb', line 707 def indices list = Array.new(rank) {|i| rpt = self.dim rpt[i] = :% index(i)[*rpt] } if block_given? return yield(*list) else return list end end |
#insert_block(offset, bsize, &block) ⇒ Object
insert
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/carray/base/basic.rb', line 470 def insert_block (offset, bsize, &block) if offset.size != rank or bsize.size != rank raise "rank mismatch" end newdim = dim grids = dim.map{|d| CArray.int32(d) } rank.times do |i| if offset[i] < 0 offset[i] += dim[i] end if offset[i] < 0 or offset[i] >= dim[i] or bsize[i] < 0 raise "invalid offset or size" end if bsize[i] > 0 newdim[i] += bsize[i] end grids[i][0...offset[i]].seq! grids[i][offset[i]..-1].seq!(offset[i]+bsize[i]) end out = CArray.new(data_type, newdim) if block_given? sel = out.true sel[*grids] = 0 out[sel] = block.call end out[*grids] = self return out end |
#inspect ⇒ Object
217 218 219 |
# File 'lib/carray/base/inspect.rb', line 217 def inspect return CArray::Inspector.new(self).inspect_string end |
#integrate ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'ext/calculus/carray_calculus.c', line 73
static VALUE
rb_ca_integrate (volatile VALUE self, volatile VALUE vsc)
{
CArray *sc, *ca;
double ans;
ca = ca_wrap_readonly(self, CA_DOUBLE);
sc = ca_wrap_readonly(vsc, CA_DOUBLE);
if ( ca->elements != sc->elements ) {
rb_raise(rb_eRuntimeError, "data num mismatch");
}
if ( ca_is_any_masked(ca) || ca_is_any_masked(sc) ) {
rb_raise(rb_eRuntimeError,
"can't calculate integrattion when masked elements exist");
}
ca_attach_n(2, ca, sc);
ans = simpson((double*)sc->ptr, (double*)ca->ptr, ca->elements);
ca_detach_n(2, ca, sc);
return rb_float_new(ans);
}
|
#interp_nd_linear ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'ext/calculus/carray_interp.c', line 234
static VALUE
rb_ca_interpolate_bilinear (int argc, VALUE *argv, volatile VALUE self)
{
volatile VALUE vscales, vvalues, vs, out;
CArray *ca, *co, *cs;
double *scales[CA_RANK_MAX];
CArray *values[CA_RANK_MAX];
CArray *scales_ca[CA_RANK_MAX];
int8_t out_rank;
ca_size_t out_dim[CA_RANK_MAX];
int i;
rb_scan_args(argc, argv, "2", &vscales, &vvalues);
Check_Type(vscales, T_ARRAY);
Check_Type(vvalues, T_ARRAY);
if ( RARRAY_LEN(vscales) != RARRAY_LEN(vvalues) ) {
rb_raise(rb_eArgError, "invalid number of values or scales");
}
ca = ca_wrap_readonly(self, CA_DOUBLE);
if ( ca->rank != RARRAY_LEN(vvalues) ) {
rb_raise(rb_eArgError, "invalid number of values");
}
for (i=0; i<ca->rank; i++) {
vs = rb_ary_entry(vscales, i);
if ( NIL_P(vs) ) {
scales[i] = NULL;
}
else {
cs = ca_wrap_readonly(vs, CA_DOUBLE);
scales_ca[i] = cs;
ca_attach(cs);
scales[i] = (double *) cs->ptr;
rb_ary_store(vscales, i, vs);
}
}
out_rank = 0;
for (i=0; i<ca->rank; i++) {
vs = rb_ary_entry(vvalues, i);
if ( NIL_P(vs) ) {
out_dim[out_rank++] = ca->dim[i];
values[i] = NULL;
}
else {
values[i] = ca_wrap_readonly(vs, CA_DOUBLE);
if ( values[i]->obj_type != CA_OBJ_SCALAR ) {
out_dim[out_rank++] = values[i]->elements;
}
rb_ary_store(vvalues, i, vs);
}
}
if ( out_rank == 0 ) {
out = rb_cscalar_new(CA_DOUBLE, 0, NULL);
}
else {
out = rb_carray_new(CA_DOUBLE, out_rank, out_dim, 0, NULL);
}
Data_Get_Struct(out, CArray, co);
for (i=0; i<ca->rank; i++) {
if ( values[i] ) {
ca_attach(values[i]);
}
}
ca_attach(ca);
ca_interpolate(ca, scales, values, (double*) co->ptr);
ca_detach(ca);
for (i=0; i<ca->rank; i++) {
if ( values[i] ) {
ca_detach(values[i]);
}
if ( scales[i] ) {
ca_detach(scales_ca[i]);
}
}
if ( out_rank == 0 ) {
return rb_ca_fetch_addr(out, 0);
}
else {
return out;
}
}
|
#interpolate ⇒ Object
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'ext/calculus/carray_calculus.c', line 594
static VALUE
rb_ca_interpolate (int argc, VALUE *argv, VALUE self)
{
volatile VALUE rval = self;
volatile VALUE vsc, vx, ropt, rtype = Qnil, out0, out;
CArray *ca, *sc, *cv, *cx, *co0, *co;
char *typename = NULL;
int type = 0;
double *px, *po;
ca_size_t i;
Data_Get_Struct(self, CArray, ca);
rb_scan_args(argc, argv, "21", &vsc, &vx, &ropt);
rb_scan_options(ropt, "type", &rtype);
if ( ! NIL_P(rtype) ) {
Check_Type(rtype, T_STRING);
typename = StringValuePtr(rtype);
}
if ( typename == NULL || ! strncmp("cubic", typename, 5) ) {
type = 3;
}
else if ( ! strncmp("linear", typename, 6) ) {
type = 1;
}
else {
volatile VALUE inspect = rb_inspect(rtype);
rb_raise(rb_eRuntimeError,
"invalid interpolation type <%s>", StringValuePtr(inspect));
}
cv = ca_wrap_readonly(rval, CA_DOUBLE);
sc = ca_wrap_readonly(vsc, CA_DOUBLE);
if ( ca_is_any_masked(cv) || ca_is_any_masked(sc) ) {
rb_raise(rb_eRuntimeError,
"can't calculate interpolation when masked elements exist");
}
if ( cv->elements != sc->elements ) {
rb_raise(rb_eRuntimeError, "data num mismatch with scale");
}
cx = ca_wrap_readonly(vx, CA_DOUBLE);
co0 = carray_new(ca->data_type, cx->rank, cx->dim, 0, NULL);
out = out0 = ca_wrap_struct(co0);
co = ca_wrap_writable(out, CA_DOUBLE);
ca_attach_n(4, cv, sc, cx, co);
px = (double*) cx->ptr;
po = (double*) co->ptr;
ca_update_mask(cx);
if ( cx->mask ) {
boolean8_t *mx, *mo;
ca_create_mask(co);
mx = (boolean8_t *) cx->mask->ptr;
mo = (boolean8_t *) co->mask->ptr;
if ( type == 3 ) {
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = interpolate_cubic((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; po++; px++;
}
}
else {
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
*po = interpolate_linear((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px);
}
else {
*mo = 1;
}
mx++; mo++; po++; px++;
}
}
}
else {
if ( type == 3 ) {
for (i=0; i<cx->elements; i++) {
*po++ = interpolate_cubic((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px++);
}
}
else {
for (i=0; i<cx->elements; i++) {
*po++ = interpolate_linear((double*)sc->ptr, (double*)cv->ptr,
cv->elements, *px++);
}
}
}
ca_sync(co);
ca_detach_n(4, cv, sc, cx, co);
if ( rb_ca_is_scalar(vx) ) {
return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
}
else {
return out0;
}
}
|
#interpolate2(x, y, x0, y0) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'ext/calculus/lib/math/calculus.rb', line 103 def interpolate2 (x, y, x0, y0) if x0.is_a?(Numeric) and y0.is_a?(Numeric) return _interpolate2(x, y, x0, y0) else x0 = CArray.wrap_readonly(x0) y0 = CArray.wrap_readonly(y0) out = CArray.double(x0.size, y0.size) x0.each_with_index do |xi, i| y0.each_with_index do |yj, j| out[i,j] = _interpolate2(x, y, xi, yj) end end return out.compact end end |
#is_close(other, atol) ⇒ Object
100 101 102 |
# File 'lib/carray/base/math.rb', line 100 def is_close (other, atol) return ((self - other).abs <= atol) end |
#is_divisible(n) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/carray/base/math.rb', line 104 def is_divisible (n) unless integer? raise "data type of reciever of CArray#divisible? should be integer." end return (self % n).eq(0) end |
#is_equiv(other, rtol) ⇒ Object
94 95 96 97 98 |
# File 'lib/carray/base/math.rb', line 94 def is_equiv (other, rtol) exact_eq = self.eq(other) relative_eq = ((self - other).abs/CAMath.max(self.abs, other.abs) <= rtol) return (exact_eq).or(relative_eq) end |
#is_not_divisible(n) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/carray/base/math.rb', line 111 def is_not_divisible (n) unless integer? raise "data type of reciever of CArray#divisible? should be integer." end return (self % n).ne(0) end |
#is_real ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/carray/base/math.rb', line 128 def is_real if complex? imag.eq(0) elsif numeric? self.true else nil end end |
#join(*argv) ⇒ Object
Array#join like method
> a = CArray.object(3,3).seq(“a”,:succ)
> <CArray.object(3,3): elem=9 mem=72b
[ [ “a”, “b”, “c” ],
[ "d", "e", "f" ],
[ "g", "h", "i" ] ]>
> a.join(“n”,“,”)
> “a,b,cnd,e,fng,h,i”
755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/carray/base/basic.rb', line 755 def join (*argv) case argv.size when 0 return to_a.join() when 1 sep = argv.shift return to_a.join(sep) else sep = argv.shift return self[:i, false].map { |s| s[0, false].join(*argv) }.join(sep) end end |
#last ⇒ Object
191 192 193 |
# File 'lib/carray/base/basic.rb', line 191 def last self[-1] end |
#map(&block) ⇒ Object
Returns map
739 740 741 |
# File 'lib/carray/base/basic.rb', line 739 def map (&block) return self.convert(CA_OBJECT, &block).to_a end |
#marshal_dump ⇒ Object
for Marshal
203 204 205 206 207 208 |
# File 'lib/carray/base/serialize.rb', line 203 def marshal_dump () if self.class != CArray and self.class != CScalar raise TypeError, "can't dump a virtual or wrapped array." end return CArray.dump(self) end |
#marshal_load(data) ⇒ Object
210 211 212 213 214 |
# File 'lib/carray/base/serialize.rb', line 210 def marshal_load (data) io = StringIO.new(data) ca = CArray.load(io) initialize_copy(ca) end |
#maskout(*argv) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/carray/base/basic.rb', line 244 def maskout (*argv) obj = self.to_ca if argv.size == 1 val = argv.first case val when CArray, Symbol obj[val] = UNDEF else obj[:eq, val] = UNDEF end else obj[*argv] = UNDEF end return obj end |
#maskout!(*argv) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/carray/base/basic.rb', line 229 def maskout! (*argv) if argv.size == 1 val = argv.first case val when CArray, Symbol self[val] = UNDEF else self[:eq, val] = UNDEF end else self[*argv] = UNDEF end return self end |
#matchup(ref) ⇒ Object
matchup
262 263 264 265 266 267 |
# File 'lib/carray/base/basic.rb', line 262 def matchup (ref) ri = ref.sort_addr rs = ref[ri].to_ca si = rs.bsearch(self) return ri.project(si) end |
#matchup_nearest(ref) ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/carray/base/basic.rb', line 269 def matchup_nearest (ref) ri = ref.sort_addr rs = ref[ri].to_ca si = rs.section(self).round.int32 si.trim!(0,si.size) return ri[si].to_ca end |
#max_by(&block) ⇒ Object
406 407 408 409 410 411 412 413 |
# File 'lib/carray/base/basic.rb', line 406 def max_by (&block) if empty? return UNDEF else addr = convert(:object, &block).max_addr return self[addr] end end |
#median(*argv) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/carray/base/math.rb', line 239 def median (*argv) opt = argv.last.is_a?(Hash) ? argv.pop : {} min_count = opt[:mask_limit] if min_count and min_count < 0 min_count += elements end fill_value = opt[:fill_value] if argv.empty? if has_mask? if min_count and count_masked() > min_count return fill_value || UNDEF end c = self[:is_not_masked].sort n = self.count_not_masked else c = self.sort n = c.elements end if n == 0 return fill_value || UNDEF else return (c[(n-1)/2] + c[n/2])/2.0 end else raise "CArray#median is not implemented for multiple ranks" end end |
#min_by(&block) ⇒ Object
415 416 417 418 419 420 421 422 |
# File 'lib/carray/base/basic.rb', line 415 def min_by (&block) if empty? return UNDEF else addr = convert(:object, &block).min_addr return self[addr] end end |
#na ⇒ Object
rdoc:
class CArray
def na
end
def nv
end
def nm
end
end
188 189 190 191 192 |
# File 'ext/narray/ca_wrap_narray.c', line 188
static VALUE
rb_cary_na_ref_new (int argc, VALUE *argv, VALUE self)
{
return rb_cary_na_ref_new_i(argc, argv, self, cNArray);
}
|
#nm ⇒ Object
CArray -> NMatrix
200 201 202 203 204 |
# File 'ext/narray/ca_wrap_narray.c', line 200
static VALUE
rb_cary_nm_ref_new (int argc, VALUE *argv, VALUE self)
{
return rb_cary_na_ref_new_i(argc, argv, self, cNMatrix);
}
|
#normalize(scale = nil) ⇒ Object
15 16 17 18 19 20 21 |
# File 'ext/calculus/lib/math/calculus.rb', line 15 def normalize (scale = nil) if scale return self / self.integrate(scale) else return self / self.sum end end |
#normalize!(scale = nil) ⇒ Object
23 24 25 26 |
# File 'ext/calculus/lib/math/calculus.rb', line 23 def normalize! (scale = nil) self[] = normalize(scale) return self end |
#nv ⇒ Object
CArray -> NVector
194 195 196 197 198 |
# File 'ext/narray/ca_wrap_narray.c', line 194
static VALUE
rb_cary_nv_ref_new (int argc, VALUE *argv, VALUE self)
{
return rb_cary_na_ref_new_i(argc, argv, self, cNVector);
}
|
#order(dir = 1) ⇒ Object
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/carray/base/basic.rb', line 553 def order (dir = 1) if dir >= 0 ### ascending order if has_mask? obj = template(:int32) { UNDEF } sel = is_not_masked obj[sel][self[sel].sort_addr].seq! return obj else obj = template(:int32) obj[sort_addr].seq! return obj end else ### descending order if has_mask? obj = template(:int32) { UNDEF} sel = is_not_masked obj[sel][self[sel].sort_addr.reversed].seq! return obj else obj = template(:int32) obj[sort_addr.reversed].seq! return obj end end end |
#percentile(*argv) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/carray/base/math.rb', line 268 def percentile (*argv) opt = argv.last.is_a?(Hash) ? argv.pop : {} pers = argv min_count = opt[:mask_limit] if min_count and min_count < 0 min_count += elements end fill_value = opt[:fill_value] if has_mask? if min_count and count_masked() > min_count return argv.map { fill_value || UNDEF } end ca = self[:is_not_masked].sort n = self.count_not_masked else ca = self.sort n = ca.elements end out = [] begin pers.each do |per| if per == 100 out << ca[n-1] elsif per >= 0 and per < 100 if n > 1 f = (n-1)*per/100.0 k = f.floor r = f - k out << (1-r)*ca[k] + r*ca[k+1] else out << ca[0] end else out << CA_NAN end end rescue p self[:is_not_masked] p n raise end return out end |
#pull(*args) ⇒ Object
329 330 331 332 |
# File 'lib/carray/base/basic.rb', line 329 def pull (*args) idx = args.map{|s| s.nil? ? :% : s} return self[*idx].to_ca end |
#pulled(*args) ⇒ Object
pulled
324 325 326 327 |
# File 'lib/carray/base/basic.rb', line 324 def pulled (*args) idx = args.map{|s| s.nil? ? :% : s} return self[*idx] end |
#quantile ⇒ Object
312 313 314 |
# File 'lib/carray/base/math.rb', line 312 def quantile return percentile(0, 25, 50, 75, 100) end |
#quo(other) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/carray/base/math.rb', line 148 def quo (other) case when integer? return double/other when object? return quo_i(other) else return self/other end end |
#rad_2pi ⇒ Object
116 117 118 119 120 |
# File 'ext/mathfunc/carray_mathfunc.c', line 116
static VALUE
rb_ca_rad_2pi (VALUE self)
{
return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_2pi, self);
}
|
#rad_2pi! ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'ext/mathfunc/carray_mathfunc.c', line 122
static VALUE
rb_ca_rad_2pi_bang (VALUE self)
{
volatile VALUE out;
out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_2pi, self);
rb_funcall(self, rb_intern("[]="), 1, out);
return self;
}
|
#rad_pi ⇒ Object
158 159 160 161 162 |
# File 'ext/mathfunc/carray_mathfunc.c', line 158
static VALUE
rb_ca_rad_pi (VALUE self)
{
return ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_pi, self);
}
|
#rad_pi! ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'ext/mathfunc/carray_mathfunc.c', line 164
static VALUE
rb_ca_rad_pi_bang (VALUE self)
{
volatile VALUE out;
out = ca_call_cfunc_1_1(CA_DOUBLE, CA_DOUBLE, mathfunc_rad_pi, self);
rb_funcall(self, rb_intern("[]="), 1, out);
return self;
}
|
#random(*argv) ⇒ Object
statistics
200 201 202 |
# File 'lib/carray/base/math.rb', line 200 def random (*argv) return template.random!(*argv) end |
#randomn ⇒ Object
223 224 225 |
# File 'lib/carray/base/math.rb', line 223 def randomn return template.randomn! end |
#randomn! ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/carray/base/math.rb', line 204 def randomn! if elements == 1 self[0] = CArray.new(data_type,[2]).randomn![0] return self end x1 = CArray.new(data_type, [elements/2]) x2 = CArray.new(data_type, [elements/2]) fac = x1.random!.log!.mul!(-2.0).sqrt! ### fac = sqrt(-2*log(rnd())) x2.random!.mul!(2.0*Math::PI) ### x2 = 2*PI*rnd() x3 = x2.to_ca self2 = reshape(2,elements/2) self2[0,nil] = x2.cos!.mul!(fac) ### self[even] = fac*cos(x2) self2[1,nil] = x3.sin!.mul!(fac) ### self[odd] = fac*sin(x2) if elements % 2 == 1 self[[-1]].randomn! end return self end |
#range ⇒ Object
195 196 197 |
# File 'lib/carray/base/basic.rb', line 195 def range return (self.min)..(self.max) end |
#real ⇒ Object
Return the real part of self. If self is a complex array, the resulted array is CAMember object refers the appropriate part of self. Otherwise, the resulted array is CARefer object refers self. If you change the resulted array, the original array is also changed.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/carray/base/math.rb', line 23 def real if not @__real__ if complex? @__real__ = case data_type when CA_CMPLX64 field(0, CA_FLOAT32) when CA_CMPLX128 field(0, CA_FLOAT64) when CA_CMPLX128 field(0, CA_FLOAT128) end else @__real__ = self[] end end @__real__ end |
#real=(val) ⇒ Object
41 42 43 |
# File 'lib/carray/base/math.rb', line 41 def real= (val) real[] = val end |
#real? ⇒ Boolean
118 119 120 121 122 123 124 125 126 |
# File 'lib/carray/base/math.rb', line 118 def real? if complex? imag.all_equal?(0) elsif numeric? true else nil end end |
#recurrence(*argv, &block) ⇒ Object
73 74 75 |
# File 'lib/carray/math/recurrence.rb', line 73 def recurrence (*argv, &block) return self.template.recurrence!(*argv, &block) end |
#recurrence!(init = {}, &block) ⇒ Object
67 68 69 70 71 |
# File 'lib/carray/math/recurrence.rb', line 67 def recurrence! (init = {}, &block) lazy = CARecurrence.new(self, init, &block) CArray.attach(lazy) {} return self end |
#replace(from, to) ⇒ Object
replace
280 281 282 283 |
# File 'lib/carray/base/basic.rb', line 280 def replace (from, to) self[:eq, from] = to return self end |
#reshape(*newdim) ⇒ Object
reshape
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/carray/base/basic.rb', line 287 def reshape (*newdim) ifalse = nil i = 0 0.upto(newdim.size-1) do |i| if newdim[i].nil? newdim[i] = dim[i] elsif newdim[i] == false ifalse = i break end end k = 0 (newdim.size-1).downto(i+1) do |j| if newdim[j].nil? newdim[j] = dim[rank-1-k] end k += 1 end if ifalse newdim[ifalse] = elements/newdim.select{|x| x!=false}.inject(1){|s,x| s*x} end return refer(data_type, newdim, :bytes=>bytes) end |
#resize(*newdim, &block) ⇒ Object
Returns the array resized to the dimension given as newdim. The new area is filled by the value returned by the block.
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/carray/base/basic.rb', line 442 def resize (*newdim, &block) if newdim.size != rank raise "rank mismatch" end offset = Array.new(rank){0} rank.times do |i| d = newdim[i] case d when nil newdim[i] = dim[i] when Integer if d < 0 newdim[i] *= -1 offset[i] = newdim[i] - dim[i] end else raise "invalid dimension size" end end out = CArray.new(data_type, newdim, &block) if out.has_mask? out.mask.paste(offset, self.false) end out.paste(offset, self) return out end |
#reversed ⇒ Object
reversed
336 337 338 |
# File 'lib/carray/base/basic.rb', line 336 def reversed return self[*([-1..0]*rank)] end |
#roll(*argv) ⇒ Object
361 362 363 |
# File 'lib/carray/base/basic.rb', line 361 def roll (*argv) return self.rolled(*argv).to_ca end |
#roll!(*argv) ⇒ Object
356 357 358 359 |
# File 'lib/carray/base/basic.rb', line 356 def roll! (*argv) self[] = self.rolled(*argv) return self end |
#rolled(*argv) ⇒ Object
351 352 353 354 |
# File 'lib/carray/base/basic.rb', line 351 def rolled (*argv) argv.push({:roll => Array.new(rank){1} }) return shifted(*argv) end |
#rotate(*argv) ⇒ Object
:nodoc:
31 32 33 34 |
# File 'lib/carray/base/obsolete.rb', line 31 def rotate (*argv) # :nodoc: warn "CArray#rotate will be obsolete, use CArray#roll" return self.rolled(*argv).to_ca end |
#rotate!(*argv) ⇒ Object
:nodoc:
25 26 27 28 29 |
# File 'lib/carray/base/obsolete.rb', line 25 def rotate! (*argv) # :nodoc: warn "CArray#rotate! will be obsolete, use CArray#roll!" self[] = self.rolled(*argv) return self end |
#rotated(*argv) ⇒ Object
:nodoc:
19 20 21 22 23 |
# File 'lib/carray/base/obsolete.rb', line 19 def rotated (*argv) # :nodoc: warn "CArray#rotated will be obsolete, use CArray#rolled" argv.push({:roll => Array.new(rank){1} }) return shifted(*argv) end |
#save_binary(filename, opt = {}) ⇒ Object
obsolete methods
218 219 220 221 222 223 |
# File 'lib/carray/base/serialize.rb', line 218 def save_binary (filename, opt={}) # :nodoc: warn "CArray#save_binary will be obsolete, use CArray.save" open(filename, "w") { |io| return Serializer.new(io).save(self, opt) } end |
#save_binary_io(io, opt = {}) ⇒ Object
:nodoc:
232 233 234 235 |
# File 'lib/carray/base/serialize.rb', line 232 def save_binary_io (io, opt={}) # :nodoc: warn "CArray#save_binary_io will be obsolete, use CArray.save" return Serializer.new(io).save(self, opt) end |
#save_by_magick(filename, image_type = nil, options = "") ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/carray/io/imagemagick.rb', line 153 def save_by_magick (filename, image_type = nil, = "") unless image_type image_type = magick_guess_image_type() end unless image_type raise "please specify image_type" end quantum_format = self.float? ? "-define quantum:format=floating-point" : "" case self.data_type when CA_INT8, CA_UINT8 depth = "-depth 8" when CA_INT16, CA_UINT16 depth = "-depth 16" when CA_FLOAT32 depth = "-depth 32" when CA_FLOAT64 depth = "-depth 64" when CA_FIXLEN depth = "-depth #{8*bytes}" else depth = "-depth 8" end convert_command = [ "convert", depth, "-size " + [dim1, dim0].join("x"), quantum_format, , "#{image_type}:-", filename ].join(" ") begin IO.popen(convert_command, "w") { |io| if data_type != CA_FIXLEN and data_type != CA_OBJECT if bytes > 1 and CArray.endian == CA_LITTLE_ENDIAN self.dump_binary(io) # swap_bytes.dump_binary(io) else self.dump_binary(io) end else self.dump_binary(io) end } rescue raise "ImageMagick's convert command failed to write image file '#{filename}'" end end |
#save_csv(file, option = {}, rs: $/, sep: ",", fill: "", mode: "w", &block) ⇒ Object
540 541 542 543 544 |
# File 'lib/carray/io/csv.rb', line 540 def save_csv (file, option = {}, rs: $/, sep: ",", fill: "", mode: "w", &block) option = {:sep=>sep, :rs=>rs, :fill=>fill, :mode=>mode}.update(option) writer = CA::CSVWriter.new(option[:sep], option[:rs], option[:fill], &block) return writer.write_file(self, file, option[:mode]) end |
#scale(xa, xb) ⇒ Object
681 682 683 |
# File 'lib/carray/base/basic.rb', line 681 def scale (xa, xb) template.scale!(xa, xb) end |
#scale!(xa, xb) ⇒ Object
675 676 677 678 679 |
# File 'lib/carray/base/basic.rb', line 675 def scale! (xa, xb) xa = xa.to_f xb = xb.to_f seq!(xa, (xb-xa)/(elements-1)) end |
#section ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'ext/calculus/carray_calculus.c', line 191
static VALUE
rb_ca_binary_search_linear_index (volatile VALUE self, volatile VALUE vx)
{
volatile VALUE out, out0;
CArray *ca, *sc, *cx, *co0, *co;
ca_size_t n;
double *x;
double *px;
double *po;
ca_size_t i;
Data_Get_Struct(self, CArray, ca);
if ( rb_ca_is_any_masked(self) ) {
rb_raise(rb_eRuntimeError, "self should not have any masked elements");
}
sc = ca_wrap_readonly(self, CA_FLOAT64);
cx = ca_wrap_readonly(vx, CA_FLOAT64);
co0 = carray_new(ca->data_type, cx->rank, cx->dim, 0, NULL);
out = out0 = ca_wrap_struct(co0);
co = ca_wrap_writable(out, CA_FLOAT64);
ca_attach_n(3, sc, cx, co);
n = sc->elements;
x = (double*) sc->ptr;
px = (double*) cx->ptr;
po = (double*) co->ptr;
ca_update_mask(cx);
if ( cx->mask ) {
boolean8_t *mx, *mo;
ca_create_mask(co);
mx = (boolean8_t *) cx->mask->ptr;
mo = (boolean8_t *) co->mask->ptr;
for (i=0; i<cx->elements; i++) {
if ( ! *mx ) {
linear_index(n, x, *px, po);
}
else {
*mo = 1;
}
mx++; mo++; px++, po++;
}
}
else {
for (i=0; i<cx->elements; i++) {
linear_index(n, x, *px, po);
px++, po++;
}
}
ca_sync(co);
ca_detach_n(3, sc, cx, co);
if ( rb_ca_is_scalar(vx) ) {
return rb_funcall(out0, rb_intern("[]"), 1, INT2NUM(0));
}
else {
return out0;
}
}
|
#select(&block) ⇒ Object
:nodoc:
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/carray/base/obsolete.rb', line 36 def select (&block) # :nodoc: warn "CArray#select will be obsolete" case block.arity when 1 return self[*yield(self)] when -1, 0 return self[*instance_exec(&block)] else raise end end |
#shift(*argv, &block) ⇒ Object
347 348 349 |
# File 'lib/carray/base/basic.rb', line 347 def shift (*argv, &block) return self.shifted(*argv, &block).to_ca end |
#shift!(*argv, &block) ⇒ Object
roll / shift
342 343 344 345 |
# File 'lib/carray/base/basic.rb', line 342 def shift! (*argv, &block) self[] = self.shifted(*argv, &block) return self end |
#sign ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/carray/base/math.rb', line 138 def sign out = self.zero out[self.lt(0)] = -1 out[self.gt(0)] = 1 if float? out[self.is_nan] = 0.0/0.0 end return out end |
#solve(sc, val, eps = 100 * Float::EPSILON) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'ext/calculus/lib/math/calculus.rb', line 28 def solve (sc, val, eps = 100 * Float::EPSILON) func = self - val list, output = [], [] (0...dim0-1).each do |i| if func[i] == UNDEF elsif func[i].abs < eps and not list.include?(i-1) output.push(sc[i]) elsif func[i+1] == UNDEF elsif i < dim0 - 1 and func[i]*func[i+1] < 0 list.push(i) end end list.each do |i| sx = CArray.double(4) sy = CArray.double(4) sx[0], sx[3] = sc[i], sc[i+1] sy[0], sy[3] = func[i], func[i+1] sx[1], sx[2] = (2.0*sx[0]+sx[3])/3.0, (sx[0]+2.0*sx[3])/3.0 sy[1], sy[2] = func.interpolate(sc, sx[1], :type=>"linear"), func.interpolate(sc, sx[2], :type=>"linear") output.push(sx.interpolate(sy, 0)) end return output.uniq end |
#solve2(sc, eps = 100 * Float::EPSILON) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'ext/calculus/lib/math/calculus.rb', line 52 def solve2 (sc, eps = 100 * Float::EPSILON) retvals = [] self.dim1.times do |j| func = self[nil,j].to_ca list, output = [], [] (0...dim0-1).each do |i| if func[i] == UNDEF elsif func[i].abs < eps and not list.include?(i-1) output.push(sc[i]) elsif func[i+1] == UNDEF elsif i < dim0 - 1 and func[i]*func[i+1] < 0 list.push(i) end end list.each do |i| sx = CArray.double(4) sy = CArray.double(4) sx[0], sx[3] = sc[i], sc[i+1] sy[0], sy[3] = func[i], func[i+1] sx[1], sx[2] = (2*sx[0]+sx[3])/3, (sx[0]+2*sx[3])/3 sy[1], sy[2] = func.interpolate(sc, sx[1], :type=>"linear"), func.interpolate(sc, sx[2], :type=>"linear") output.push(sx.interpolate(sy, 0)) end retvals << output.uniq end retvals = retvals.map{|s| s.empty? ? [nil] : s} return retvals end |
#sort_by(type = nil, opt = {}, &block) ⇒ Object
Returns the array which elements are sorted by the comparison method given as block
399 400 401 402 403 404 |
# File 'lib/carray/base/basic.rb', line 399 def sort_by (type=nil, opt={}, &block) type, bytes = CArray.guess_type_and_bytes(type||data_type, opt[:bytes]||bytes) cmpary = convert(type, :bytes=>bytes, &block) return self[cmpary.sort_addr].to_ca end |
#sorted_by(type = nil, opt = {}, &block) ⇒ Object
Returns the reference which elements are sorted by the comparison method given as block
390 391 392 393 394 395 |
# File 'lib/carray/base/basic.rb', line 390 def sorted_by (type=nil, opt={}, &block) type, bytes = CArray.guess_type_and_bytes(type||data_type, opt[:bytes]||bytes) cmpary = convert(type, :bytes=>bytes, &block) return self[cmpary.sort_addr] end |
#span(range) ⇒ Object
667 668 669 |
# File 'lib/carray/base/basic.rb', line 667 def span (range) return template.span!(range) end |
#span!(range) ⇒ Object
656 657 658 659 660 661 662 663 664 665 |
# File 'lib/carray/base/basic.rb', line 656 def span! (range) first = range.begin.to_r last = range.end.to_r if range.exclude_end? seq!(first, (last-first)/elements) else seq!(first, (last-first)/(elements-1)) end return self end |
#split(*argv) ⇒ Object
Returns object carray has elements of splitted carray at dimensions
which is given by arguments
a = CA_INT([[1,2,3], [4,5,6], [7,8,9]])
a.split(0)
[1,2,3], [4,5,6], [7,8,9]
a.split(1)
[1,4,7], [2,5,8], [3,6,9]
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 |
# File 'lib/carray/base/basic.rb', line 1020 def split (*argv) odim = dim.values_at(*argv) out = CArray.object(*odim) idx = [nil] * rank attach { out.map_with_index! do |o, v| argv.each_with_index do |r, i| idx[r] = v[i] end self[*idx].to_ca end } return out end |
#st ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/carray/base/struct.rb', line 79 def st unless has_data_class? raise "should have data_class" end unless @struct struct_class = Struct.new(nil, *data_class::MEMBERS) members = data_class::MEMBERS.map{|name| self[name]} @struct = struct_class.new(*members) end return @struct end |
#to_binary(io = "", opt = {}) ⇒ Object
:nodoc:
242 243 244 245 246 |
# File 'lib/carray/base/serialize.rb', line 242 def to_binary (io="", opt={}) # :nodoc: warn "CArray#to_binary will be obsolete, use CArray.dump" Serializer.new(io).save(self, opt) return io end |
#to_bit_string(nb) ⇒ Object
771 772 773 774 775 776 |
# File 'lib/carray/base/basic.rb', line 771 def to_bit_string (nb) hex = CArray.uint8(((nb*elements)/8.0).ceil) hex.bits[nil].paste([0], self.bits[false,[(nb-1)..0]].flatten) hex.bits[] = hex.bits[nil,[-1..0]] return hex.to_s end |
#to_column ⇒ Object
Returns (n,1) array from 1-dimensional array
433 434 435 436 437 438 |
# File 'lib/carray/base/basic.rb', line 433 def to_column if rank != 1 raise "rank should be 1" end return self[:%,1] end |
#to_csv(io = "", option = {}, rs: $/, sep: ",", fill: "", &block) ⇒ Object
546 547 548 549 550 551 552 553 554 555 |
# File 'lib/carray/io/csv.rb', line 546 def to_csv (io="", option ={}, rs: $/, sep: ",", fill: "", &block) option = {:sep=>sep, :rs=>rs, :fill=>fill}.update(option) writer = CA::CSVWriter.new(option[:sep], option[:rs], option[:fill], &block) case io when IO, StringIO return writer.write_io(self, io) when String return writer.write_string(self, io) end end |
#to_fortran_format(fmt, input = "") ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'ext/fortio/lib/carray/io/fortran_format.rb', line 21 def to_fortran_format (fmt, input="") case input when String io = StringIO.new(input) else io = input end case fmt when String fmt = FortranFormat.new(fmt) end n = fmt.count_args attach { self[nil].blocks(0...n).each do |blk| fmt.write(io, *blk.to_a) end } return input end |
#to_na ⇒ Object
rdoc:
class CArray
def to_na
end
def to_nv
end
def to_nm
end
end
317 318 319 320 321 |
# File 'ext/narray/ca_wrap_narray.c', line 317
static VALUE
rb_cary_to_na (VALUE self, VALUE klass)
{
return rb_cary_to_na_i(self, cNArray);
}
|
#to_na! ⇒ Object
rdoc:
class CArray
def to_na!
end
def to_nv!
end
def to_nm!
end
end
254 255 256 257 258 |
# File 'ext/narray/ca_wrap_narray.c', line 254
static VALUE
rb_cary_to_na_bang (VALUE self)
{
return rb_cary_to_na_bang_i(self, cNArray);
}
|
#to_nam ⇒ Object
17 18 19 20 21 22 23 |
# File 'ext/narray/lib/math/narray_miss.rb', line 17 def to_nam if has_mask? return NArrayMiss.to_nam_no_dup(to_na, mask.bit_neg.na) else return NArrayMiss.to_nam_no_dup(to_na) end end |
#to_nam! ⇒ Object
25 26 27 28 29 30 31 |
# File 'ext/narray/lib/math/narray_miss.rb', line 25 def to_nam! if has_mask? return NArrayMiss.to_nam_no_dup(to_na!, mask.bit_neg.to_na!) else return NArrayMiss.to_nam_no_dup(to_na!) end end |
#to_nm ⇒ Object
329 330 331 332 333 |
# File 'ext/narray/ca_wrap_narray.c', line 329
static VALUE
rb_cary_to_nm (VALUE self, VALUE klass)
{
return rb_cary_to_na_i(self, cNMatrix);
}
|
#to_nm! ⇒ Object
266 267 268 269 270 |
# File 'ext/narray/ca_wrap_narray.c', line 266
static VALUE
rb_cary_to_nm_bang (VALUE self)
{
return rb_cary_to_na_bang_i(self, cNMatrix);
}
|
#to_nv ⇒ Object
323 324 325 326 327 |
# File 'ext/narray/ca_wrap_narray.c', line 323
static VALUE
rb_cary_to_nv (VALUE self, VALUE klass)
{
return rb_cary_to_na_i(self, cNVector);
}
|
#to_nv! ⇒ Object
260 261 262 263 264 |
# File 'ext/narray/ca_wrap_narray.c', line 260
static VALUE
rb_cary_to_nv_bang (VALUE self)
{
return rb_cary_to_na_bang_i(self, cNVector);
}
|
#to_row ⇒ Object
Returns (1,n) array from 1-dimensional array
425 426 427 428 429 430 |
# File 'lib/carray/base/basic.rb', line 425 def to_row if rank != 1 raise "rank should be 1" end return self[1,:%] end |
#to_sqlite3(database: nil, table:, datatype: "numeric", schema: nil, transaction: true) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/carray/io/sqlite3.rb', line 33 def to_sqlite3 (database: nil, table:, datatype: "numeric", schema: nil, transaction: true) unless rank <= 2 raise "to_sqlite3 is valid only for 2-dim array (table)" end case database when SQLite3::Database when String if File.exist? database database = SQLite3::Database.open(database) else database = SQLite3::Database.new(database) end else database = SQLite3::Database.new ":memory:" end if respond_to?(:column_names) and column_names vars = column_names else if rank == 1 vars = ["c0"] else vars = CArray.object(dim1).seq.map{|s| "c#{s}" } end end if schema database.execute "create table if not exists #{table} (" + schema + ")" else database.execute "create table if not exists #{table} (" + vars.map{|s| s + " " + datatype }.join(",") + ")" end insert = database.prepare %{ insert or replace into #{table} values (#{(["?"]*vars.size).join(",")}) } database.transaction if transaction if rank == 1 dim0.times do |i| insert.execute [self[i]] end else begin dim0.times do |i| begin insert.execute self[i,nil].to_a rescue puts self[i,nil].to_a raise $! end end end end database.commit if transaction insert.close return database end |
#to_tabular(option = {}) ⇒ Object
557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/carray/io/csv.rb', line 557 def to_tabular (option = {}) option = {:sep=>" ", :names=>nil}.update(option) if option[:names] names = option[:names] elsif self.respond_to?(:names) names = self.names end sep = option[:sep] data = self.to_ca.map! {|s| s.to_s } table = CArray.join([names.to_ca], [data]) length = table.convert{|s| s.length}.max(0) table.map_with_index! {|s, idx| s.rjust(length[idx[1]]) }.to_csv.gsub(/,/,sep) end |
#transform(type, dim, opt = {}) ⇒ Object
:nodoc:
48 49 50 51 |
# File 'lib/carray/base/obsolete.rb', line 48 def transform (type, dim, opt = {}) # :nodoc: warn("CArray#transform will be obsolete") return refer(type, dim, opt).to_ca end |
#transpose(*argv) ⇒ Object
370 371 372 |
# File 'lib/carray/base/basic.rb', line 370 def transpose (*argv) return self.transposed(*argv).to_ca end |
#transpose!(*argv) ⇒ Object
365 366 367 368 |
# File 'lib/carray/base/basic.rb', line 365 def transpose! (*argv) self[] = self.transposed(*argv) return self end |
#true ⇒ Object
Returns the 8-bit integer CArray object filled with 1 which dimension size is same as self. The resulted array represents the logical array which has true for its all elements.
734 735 736 |
# File 'lib/carray/base/basic.rb', line 734 def true () return template(:boolean) { 1 } end |
#unblob(type, subdim = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/carray/io/sqlite3.rb', line 87 def unblob (type, subdim = nil) bytes = CArray.sizeof(type) elem = self[0] if elem == nil and subdim == nil raise "first element is nil, please specify dimension." end unless subdim subdim = [elem.bytesize/bytes] end out = CArray.new(type, dim + subdim) needed_bytes = out.elements/self.elements*bytes ref = out.refer(:fixlen, dim, :bytes=>needed_bytes) if elem and needed_bytes != elem.bytesize self.each_addr do |addr| if self[addr].nil? ref[[addr]] = UNDEF else ref[[addr]].load_binary self[addr] end end elsif self.any_equal?(nil) copy = self.to_ca sel = self.eq(nil) copy[sel] = "\0" * needed_bytes ref.load_binary copy.join ref[sel] = UNDEF else ref.load_binary self.join end return out end |
#unescape_bytea(type, subdim = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/carray/io/pg.rb', line 72 def unescape_bytea (type, subdim = nil) bytes = CArray.sizeof(type) max_bytesize = self.convert{|v| v ? PG::Connection.unescape_bytea(v).bytesize : 0}.max unless subdim subdim = [max_bytesize/bytes] end out = CArray.new(type, dim + subdim) needed_bytes = out.elements/self.elements*bytes ref = out.refer(:fixlen, dim, :bytes=>needed_bytes) if needed_bytes == max_bytesize ref.load_binary self.convert {|v| PG::Connection.unescape_bytea(v) }.join else self.each_addr do |addr| if self[addr] text = PG::Connection.unescape_bytea(self[addr]) ref[[addr]].load_binary PG::Connection.unescape_bytea(self[addr]) else out[addr,false] = UNDEF end end end return out end |
#uniq ⇒ Object
Returns the array eliminated all the duplicated elements.
580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/carray/base/basic.rb', line 580 def uniq ary = to_a.uniq if has_mask? ary.delete(UNDEF) end if has_data_class? return CArray.new(data_class, [ary.length]) { ary } else return CArray.new(data_type, [ary.length], :bytes=>bytes) { ary } end end |
#where_range ⇒ Object
542 543 544 545 546 547 548 549 550 551 |
# File 'lib/carray/base/basic.rb', line 542 def where_range w = where x = (w - w.shifted(1){-2}).sub!(1).where y = (w - w.shifted(-1){-2}).add!(1).where list = [] x.each_addr do |i| list.push(w[x[i]]..w[y[i]]) end return list end |
#window_iterator(*argv) ⇒ Object
:nodoc:
14 15 16 17 |
# File 'lib/carray/base/obsolete.rb', line 14 def window_iterator (*argv) # :nodoc: warn "CArray#window_iterator will be obsolete, use CArray#windows" return windows(*argv) end |
#windows(*args, &block) ⇒ Object
297 298 299 |
# File 'lib/carray/base/iterator.rb', line 297 def windows (*args, &block) return CAWindowIterator.new(self.window(*args, &block)) end |
#zimg(option = {}) ⇒ Object
:nodoc:
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/carray/graphics/zimg.rb', line 48 def zimg (option={}) # :nodoc: tempfile = "%zimg_#{$$}_#{@@zimg_count}.dat" @@zimg_count += 1 if self.rank != 2 raise "zimg accept only 2-D data" end option.each do |k, v| if k.kind_of?(String) option[k.intern] = v end end if option[:output] output = option[:output] case output when /.ppm$/ outfmt = "--ppm" when /.pgm$/ outfmt = "--pgm" when /.jpg$/i, /.jpeg$/i if option[:quality] outfmt = "--jpeg=" + option[:quality].to_s else outfmt = "--jpeg" end else outfmt = "" end else output = "-" outfmt = "" end if option[:interlace] interlace = "--interlace" else interlace = "" end case self.data_type when CA_UINT8 type_spec = "--unsigned-char" when CA_INT8, CA_BOOLEAN type_spec = "--char" when CA_UINT16 type_spec = "--unsigned-short" when CA_INT16 type_spec = "--short" when CA_UINT32 type_spec = "--unsigned-int" when CA_INT32 type_spec = "--int" when CA_FLOAT32 type_spec = "--float" when CA_FLOAT64 type_spec = "--double" when CA_CMPLX64 type_spec = "--complex-float=" + ( option[:complex] || "abs" ) when CA_CMPLX128 type_spec = "--complex-double=" + ( option[:complex] || "abs" ) else raise "can't create image for invalid numerical data" end case option[:colormap] when "red", "blue", "gray" colormap = "--" + option[:colormap] when String if File.exist?(option[:colormap]) colormap = "--colormap=" + option[:colormap] else raise "can't find colomap file" end when Array colormap = "--colormap=" + option[:colormap].join(",") else colormap = "" end if option[:crange] crange = "--crange=" + option[:crange].join(',') else crange = '' end if option[:nda_color] if option[:fill_value] nda = "--nda=" + [option[:fill_value], option[:nda_color]].join(',') else nda = "--nda=" + ["Infinity", option[:nda_color]].join(',') end else nda = "" end if option[:scale] scale = "--scale=" + option[:scale].join(',') else scale = '' end if option[:align] if option[:align].last.kind_of?(String) align = "--align=" + option[:align][0..-2].join('x') "," + option[:align].last else align = "--align=" + option[:align].join('x') end else align = '' end if option[:colorbox] if option[:cbox_tics] colorbox = "--colorbox=" + option[:cbox_tics].to_s else colorbox = "--colorbox" end else colorbox = "" end if option[:cbox_format] cbox_format = option[:cbox_format] else cbox_format = "" end if option[:cbox_label] cbox_label = option[:cbox_label] else cbox_label = "" end geom = lambda{|x,y| [ (x > 0 ? "+" : '') + x.to_s, (y > 0 ? '+' :'') + y.to_s].join } label = "" if option[:label] option[:label].each do |x, y, s| label << "--label=" + [geom[x, y],s].join(',') + " " end end vlabel = "" if option[:vlabel] option[:vlabel].each do |x, y, s| vlabel << "--vlabel=" + [geom[x, y],s].join(',') + " " end end if option[:textfont] textfont = "--font=" + option[:textfont].to_s else textfont = "" end if option[:textcolor] textcolor = "--textcolor=" + option[:textcolor].to_s else textcolor = "" end case option[:smooth] when Numeric smooth = "--smooth=" + option[:smooth].to_s else if option[:smooth] smooth = "--smooth" else smooth = "" end end command = [ "zimg", "--output=#{output}", outfmt, interlace, type_spec, "--size=" + self.dim.reverse.join(","), scale, align, colormap, option[:invert] ? "--invert" : "", crange, nda, colorbox, cbox_label, cbox_format, label, vlabel, textfont, textcolor, smooth, tempfile ].join(" ") if option[:nda_color] and self.has_mask? if option[:fill_value] out = self.unmask(option[:fill_value]) else out = self.unmask(nan) end else out = self end open(tempfile, "w"){ |io| out.dump_binary(io) } if option[:debug] puts command end if option[:output] system(command) case option[:display] when String system([option[:display], output].join(" ")) else if option[:display] system(["display", output].join(" ")) end end else case option[:display] when String system(command + ["|", option[:display], "-"].join(" ")) else if option[:display] system(command + ["|", "display -"].join(" ")) end end end ensure if File.exist?(tempfile) File.unlink(tempfile) end end |