numru-misc 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,90 +1,64 @@
1
1
  =begin
2
- =module NumRu::Misc::MD_Iterators
3
2
 
4
- A Mixin.
5
- To be included in a class with multi-dimension indexing support
6
- (such as NArray).
7
-
8
- ==Index
9
-
10
- * ((<each_subary_at_dims>))
11
- * ((<each_subary_at_dims_with_index>))
12
-
13
- ==Methods
14
- ---each_subary_at_dims( *dims )
15
-
16
- Iterator for each sub-array (not each element) specified by dimensions.
17
-
18
- ARGUMENT
19
- * ((|dims|)) (integers) : specifies subsets at dimensions
20
- specified here with the beginning-to-end selection.
21
- For example, [0, 1] to specify the first 2 dimensions
22
- (subsets will be 2D then), and [2] to specify the 3rd
23
- dimension (subsets will be 1D). Duplication has no effect,
24
- so [0,0] and [0] are the same. Also, its order has no effect.
25
- See EXAMPLE below for more.
26
-
27
- RETURN VALUE
28
- * self
29
-
30
- POSSIBLE EXCEPTIONS
31
- * exception is raised if ( dims.min<0 || dims.max>=self.rank ).
32
-
33
- EXAMPLE
34
-
35
- * Suppose that you want to do something with 2D sub-arrays in a
36
- multi-dimension NArray. First, you include this module as follows:
37
-
38
- require "narray"
39
- class NArray
40
- include NumRu::Misc::MD_Iterators
41
- end
42
-
43
- And prepare the array if you have not (here, it is 4D):
44
-
45
- na = NArray.int(10,2,5,2).indgen!
46
-
47
- Then you do the job like this:
48
-
49
- na.each_subary_at_dims(0,2){ |sub|
50
- ... # do whatever with sub
51
- }
52
-
53
- This is equivalent to the following:
54
-
55
- (0...na.shape[3]).each{|j|
56
- (0...na.shape[1]).each{|i|
57
- sub = na[0..-1, i, 0..-1, j]
58
- ... # do whatever with sub
59
- }
60
- }
61
-
62
- Note that the loop must be nested 3 times when (('na')) is a 5D array,
63
- if the latter approach is used. On the other hand, it will still
64
- require the same single loop with the former.
65
-
66
- ---each_subary_at_dims_with_index( *dims )
67
- Like ((<each_subary_at_dims>)) but the block takes two arguments:
68
- subset and the subset specifier (index).
69
-
70
- EXAMPLE
71
- * Suppose the example above in ((<each_subary_at_dims>)) (EXAMPLE).
72
- And suppose that you want to overwrite (('na')) with the result
73
- you get. You can do it like this:
74
-
75
- na.each_subary_at_dims_with_index(0,2){ |sub,idx|
76
- result = (sub + 10) / 2
77
- na[*idx] = result
78
- }
79
-
80
- Here, (('idx')) is an Array to be fed in the []= or [] methods
81
- with asterisk (ungrouping).
82
3
  =end
83
4
 
84
5
  module NumRu
85
6
  module Misc
7
+ # A Mixin.
8
+ # To be included in a class with multi-dimension indexing support
9
+ # (such as NArray).
86
10
  module MD_Iterators
87
11
 
12
+ # Iterator for each sub-array (not each element) specified by
13
+ # dimensions.
14
+ #
15
+ # ARGUMENT
16
+ #
17
+ # <tt>*dims</tt> (integers) : specifies subsets
18
+ # at dimensions specified here with the beginning-to-end selection.
19
+ # For example, [0, 1] to specify the first 2 dimensions (subsets
20
+ # will be 2D then), and [2] to specify the 3rd dimension (subsets
21
+ # will be 1D). Duplication has no effect, so [0,0] and [0] are the
22
+ # same. Also, its order has no effect. See EXAMPLE below for more.
23
+ #
24
+ # RETURN VALUE
25
+ # * self
26
+ #
27
+ # POSSIBLE EXCEPTIONS
28
+ # * exception is raised if ( dims.min<0 || dims.max>=self.rank ).
29
+ #
30
+ # EXAMPLE
31
+ #
32
+ # * Suppose that you want to do something with 2D sub-arrays in a
33
+ # multi-dimension NArray. First, you include this module as follows:
34
+ #
35
+ # require "narray"
36
+ # class NArray
37
+ # include NumRu::Misc::MD_Iterators
38
+ # end
39
+ #
40
+ # And prepare the array if you have not (here, it is 4D):
41
+ #
42
+ # na = NArray.int(10,2,5,2).indgen!
43
+ #
44
+ # Then you do the job like this:
45
+ #
46
+ # na.each_subary_at_dims(0,2){ |sub|
47
+ # ... # do whatever with sub
48
+ # }
49
+ #
50
+ # This is equivalent to the following:
51
+ #
52
+ # (0...na.shape[3]).each{|j|
53
+ # (0...na.shape[1]).each{|i|
54
+ # sub = na[0..-1, i, 0..-1, j]
55
+ # ... # do whatever with sub
56
+ # }
57
+ # }
58
+ #
59
+ # Note that the loop must be nested 3 times when <tt>na>/tt> is a 5D array,
60
+ # if the latter approach is used. On the other hand, it will still
61
+ # require the same single loop with the former.
88
62
  def each_subary_at_dims( *dims )
89
63
  if dims.min<0 || dims.max>=rank
90
64
  raise ArguemntError,"Invalid dims #{dims.inspect} for #{rank}D array"
@@ -117,6 +91,22 @@ module NumRu
117
91
  self
118
92
  end
119
93
 
94
+ # Like #each_subary_at_dims but the block takes two arguments:
95
+ # subset and the subset specifier (index).
96
+ #
97
+ # EXAMPLE
98
+ # * Suppose the example above in #each_subary_at_dims (EXAMPLE).
99
+ # And suppose that you want to overwrite <tt>na</tt> with the result
100
+ # you get. You can do it like this:
101
+ #
102
+ # na.each_subary_at_dims_with_index(0,2){ |sub,idx|
103
+ # result = (sub + 10) / 2
104
+ # na[*idx] = result
105
+ # }
106
+ #
107
+ # Here, <tt>idx</tt> is an Array to be fed in the []= or [] methods
108
+ # with asterisk (ungrouping).
109
+ #
120
110
  def each_subary_at_dims_with_index( *dims )
121
111
  if dims.min<0 || dims.max>=rank
122
112
  raise ArguemntError,"Invalid dims #{dims.inspect} for #{rank}D array"
@@ -157,7 +147,7 @@ end
157
147
 
158
148
  if __FILE__ == $0
159
149
  require "narray"
160
- class NArray
150
+ class NArray # :nodoc:
161
151
  include NumRu::Misc::MD_Iterators
162
152
  end
163
153
  na = NArray.int(10,2,2,2).indgen!
@@ -1,98 +1,87 @@
1
- =begin
2
- = module NumRu::Misc
3
-
4
- == Overview
5
-
6
- Miscellaneous functions and classes to facilitate programming.
7
-
8
- == Index
9
-
10
- CLASSES
11
-
12
- * ((<class KeywordOpt|URL:keywordopt.html>))
13
- to support keyward arguments with default values.
14
- * ((<class NArray (enhancement of NArray made by M Tanaka)|URL:narray_ext.html>))
15
-
16
- MODULES
17
-
18
- * ((<module MD_Iterators|URL:md_iterators.html>)) A Mixin for classes with
19
- multi-dimension indexing support (such as NArray).
20
- * ((<module EMath|URL:emath.html>))
21
- To be included instead of the Math predefined module (or NMath in NArray).
22
- Unlike Math and NMath, EMath handles unknown classes by calling its
23
- native instance method (assuming the same name).
24
-
25
-
26
- MODULE FUNCTIONS
27
-
28
- * ((<check_shape_consistency>))
29
-
30
- == Module functions
31
- ---check_shape_consistency(cshapes, *args)
32
- Check the consistency of array shapes (multi-dim such as NArray).
33
- Exception is raised if inconsistent.
34
-
35
- ARGUMENTS
36
- * cshapes (String) : description of the shapes of the args.
37
- Delimited by one-or-more spaces between arrays,
38
- and the shape of each array is delimited by a comma. The lengths are
39
- expressed with string names as identifiers (in that case, length
40
- values are unquestioned) or specified as positive integers.
41
- Use '..' or '...' for repetition of the last shape.
42
- See EXAMPLES below.
43
-
44
- * args (multi-dim arrays such as NArray): arrays to be checked
45
-
46
- RETURN VALUE
47
- * nil
48
-
49
- POSSIBLE EXCEPTIONS
50
- * exception is raised if cshapes and args are inconsistent:
51
-
52
- * RuntimeError, if the arrays do not have shapes specified by cshapes.
53
-
54
- * ArgeumentError, if the number of args are inconsistent with cshapes.
55
- This is likely a coding error of the user.
56
-
57
- EXAMPLES
58
-
59
- * to check whether three arrays u, v, and w are shaped as
60
- u[nx], v[ny], and w[nx,ny], where nx and ny are any integer:
61
-
62
- NumRu::Misc.check_shape_consistency('nx ny nx,ny',u,v,w)
63
-
64
- Or equivalently,
65
-
66
- NumRu::Misc.check_shape_consistency('m n m,n',u,v,w)
67
-
68
- because actual strings does not matter.
69
-
70
- * To specify fixed lengths, use integers instead of names:
71
-
72
- NumRu::Misc.check_shape_consistency('4 n 4,n',u,v,w)
73
-
74
- In this case, u,v,w must have shapes [4], [ny], and [4,ny],
75
- where ny is any length.
76
-
77
- * Use '..' or '...' to repeat the same shape:
78
-
79
- NumRu::Misc.check_shape_consistency('nx,ny ...',u,v,w)
80
-
81
- This ensures that u, v, and w are 2D arrays with the same shape.
82
- Note: '..' and '...' are the same, so you can use whichever you like.
83
-
84
- =end
85
-
86
1
  require "narray"
87
2
 
88
3
  module NumRu
4
+
5
+ # == Overview
6
+ #
7
+ # Miscellaneous functions and classes to facilitate programming.
8
+ #
9
+ # == Index
10
+ #
11
+ # CLASSES
12
+ #
13
+ # * class KeywordOpt : supports keyword arguments with default values.
14
+ # * class NArray (http://masa16.github.io/narray/index.ja.html) : used in EMath
15
+ #
16
+ # MODULES
17
+ #
18
+ # * module MD_Iterators : A Mixin for classes with
19
+ # multi-dimension indexing support (such as NArray).
20
+ # * module EMath :
21
+ # To be included instead of the Math predefined module (or NMath in NArray).
22
+ # Unlike Math and NMath, EMath handles unknown classes by calling its
23
+ # native instance method (assuming the same name).
24
+ #
25
+ #
26
+
89
27
  module Misc
90
28
  module_function
91
29
 
30
+ # Check the consistency of array shapes (multi-dim such as NArray).
31
+ # Exception is raised if inconsistent.
32
+ #
33
+ # ARGUMENTS
34
+ # * cshapes (String) : description of the shapes of the args.
35
+ # Delimited by one-or-more spaces between arrays,
36
+ # and the shape of each array is delimited by a comma. The lengths are
37
+ # expressed with string names as identifiers (in that case, length
38
+ # values are unquestioned) or specified as positive integers.
39
+ # Use '..' or '...' for repetition of the last shape.
40
+ # See EXAMPLES below.
41
+ #
42
+ # * args (multi-dim arrays such as NArray): arrays to be checked
43
+ #
44
+ # RETURN VALUE
45
+ # * nil
46
+ #
47
+ # POSSIBLE EXCEPTIONS
48
+ # * exception is raised if cshapes and args are inconsistent:
49
+ #
50
+ # * RuntimeError, if the arrays do not have shapes specified by cshapes.
51
+ #
52
+ # * ArgeumentError, if the number of args are inconsistent with cshapes.
53
+ # This is likely a coding error of the user.
54
+ #
55
+ # EXAMPLES
56
+ #
57
+ # * to check whether three arrays u, v, and w are shaped as
58
+ # u[nx], v[ny], and w[nx,ny], where nx and ny are any integer:
59
+ #
60
+ # NumRu::Misc.check_shape_consistency('nx ny nx,ny',u,v,w)
61
+ #
62
+ # Or equivalently,
63
+ #
64
+ # NumRu::Misc.check_shape_consistency('m n m,n',u,v,w)
65
+ #
66
+ # because actual strings does not matter.
67
+ #
68
+ # * To specify fixed lengths, use integers instead of names:
69
+ #
70
+ # NumRu::Misc.check_shape_consistency('4 n 4,n',u,v,w)
71
+ #
72
+ # In this case, u,v,w must have shapes [4], [ny], and [4,ny],
73
+ # where ny is any length.
74
+ #
75
+ # * Use '..' or '...' to repeat the same shape:
76
+ #
77
+ # NumRu::Misc.check_shape_consistency('nx,ny ...',u,v,w)
78
+ #
79
+ # This ensures that u, v, and w are 2D arrays with the same shape.
80
+ # Note: '..' and '...' are the same, so you can use whichever you like.
92
81
  def check_shape_consistency(cshapes, *args)
93
82
  ranks = Array.new
94
83
  elm2idx = Hash.new
95
- spl = cshapes.split(' +')
84
+ spl = cshapes.split(' ')
96
85
  if spl.length >= 2 && /^\.\.\.?$/ =~ spl[-1] # '..' or '...'
97
86
  ((spl.length-1)...args.length).each{|i|
98
87
  spl[i]=spl[i-1]
@@ -156,7 +145,7 @@ if __FILE__ == $0
156
145
  begin
157
146
  Misc.check_shape_consistency('6 ny 6,ny',u,v,w)
158
147
  rescue
159
- puts " exception raised as expected\n"+$!
148
+ puts " exception raised as expected\n#{$!}"
160
149
  puts ' OK'
161
150
  end
162
151
 
@@ -171,7 +160,7 @@ if __FILE__ == $0
171
160
  w = NArray.float(3,5)
172
161
  Misc.check_shape_consistency('nx ny nx,ny',u,v,w)
173
162
  rescue
174
- puts " exception raised as expected\n"+$!
163
+ puts " exception raised as expected\n#{$!}"
175
164
  puts ' OK'
176
165
  end
177
166
 
@@ -182,7 +171,7 @@ if __FILE__ == $0
182
171
  w = NArray.float(3,5)
183
172
  Misc.check_shape_consistency('nx ny nx,ny',u,v,w)
184
173
  rescue
185
- puts " exception raised as expected\n"+$!
174
+ puts " exception raised as expected\n#{$!}"
186
175
  puts ' OK'
187
176
  end
188
177
 
@@ -0,0 +1,5 @@
1
+ module NumRu
2
+ module Misc
3
+ VERSION = "0.1.2"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ require "rubygems" unless defined?(Gem)
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require "numru/misc"
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "numru-misc"
10
+ spec.version = NumRu::Misc::VERSION
11
+ spec.authors = ["Takeshi Horinouchi"]
12
+ spec.email = ["horinout@gfd-dennou.org"]
13
+
14
+ spec.summary = %q{Collection of miscellaneous functions and classes to facilitate programming.}
15
+ spec.description = %q{Miscellaneous functions and classes to help Ruby programming. To be used in other NumRu libraries.}
16
+ spec.homepage = 'http://www.gfd-dennou.org/arch/ruby/products/numru-misc/'
17
+ spec.licenses = ["BSD-2-Clause"]
18
+
19
+ spec.files = `git ls-files -z`.split("\x0") + ["ChangeLog"]
20
+ #spec.test_files = spec.files.grep(%r{^demo/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = Gem::Requirement.new(">= 1.6")
24
+ spec.add_runtime_dependency(%q<narray>, [">= 0.5.5"])
25
+ end
metadata CHANGED
@@ -1,97 +1,83 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: numru-misc
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Takeshi Horinouchi
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-02-20 00:00:00 +09:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2015-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: narray
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.5.5
33
22
  type: :runtime
34
- version_requirements: *id001
35
- description: Miscellaneous functions and classes to help Ruby programming. To be used in other NumRu libraries.
36
- email:
37
- - eriko@gfd-dennou.org
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.5.5
30
+ description: Miscellaneous functions and classes to help Ruby programming. To be used
31
+ in other NumRu libraries.
32
+ email:
33
+ - horinout@gfd-dennou.org
38
34
  executables: []
39
-
40
35
  extensions: []
41
-
42
36
  extra_rdoc_files: []
43
-
44
- files:
37
+ files:
38
+ - .ChangeLog.until201414
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE.txt
45
42
  - Rakefile
43
+ - doc/emath.html
44
+ - doc/index.html
45
+ - doc/keywordopt.html
46
+ - doc/md_iterators.html
47
+ - doc/misc.html
46
48
  - install.rb
47
- - LICENSE.txt
48
- - ChangeLog
49
- - makedoc.csh
49
+ - lib/numru/misc.rb
50
+ - lib/numru/misc/emath.rb
50
51
  - lib/numru/misc/keywordopt.rb
51
52
  - lib/numru/misc/md_iterators.rb
52
53
  - lib/numru/misc/misc.rb
53
- - lib/numru/misc/emath.rb
54
- - lib/numru/misc.rb
55
- - doc/emath.html
56
- - doc/md_iterators.html
57
- - doc/keywordopt.html
58
- - doc/misc.html
59
- - doc/index.html
60
- has_rdoc: true
54
+ - lib/numru/misc/version.rb
55
+ - makedoc.csh
56
+ - numru-misc.gemspec
57
+ - ChangeLog
61
58
  homepage: http://www.gfd-dennou.org/arch/ruby/products/numru-misc/
62
- licenses:
63
- - Takeshi Horinouchi
64
- - GFD Dennou Club
59
+ licenses:
60
+ - BSD-2-Clause
65
61
  post_install_message:
66
62
  rdoc_options: []
67
-
68
- require_paths:
63
+ require_paths:
69
64
  - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
65
+ required_ruby_version: !ruby/object:Gem::Requirement
71
66
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: 3
76
- segments:
77
- - 1
78
- - 6
79
- version: "1.6"
80
- required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '1.6'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
72
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
- version: "0"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
89
77
  requirements: []
90
-
91
78
  rubyforge_project:
92
- rubygems_version: 1.3.7
79
+ rubygems_version: 1.8.23
93
80
  signing_key:
94
81
  specification_version: 3
95
82
  summary: Collection of miscellaneous functions and classes to facilitate programming.
96
83
  test_files: []
97
-