1 2 3 4 5 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
<?xml version="1.0" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>lib/numru/misc/md_iterators.rb</title>
</head>
<body>
<h1><a name="label:0" id="label:0">module NumRu::Misc::MD_Iterators</a></h1><!-- RDLabel: "module NumRu::Misc::MD_Iterators" -->
<p>A Mixin.
To be included in a class with multi-dimension indexing support
(such as NArray).</p>
<h2><a name="label:1" id="label:1">Index</a></h2><!-- RDLabel: "Index" -->
<ul>
<li><a href="#label:3">each_subary_at_dims</a></li>
<li><a href="#label:4">each_subary_at_dims_with_index</a></li>
</ul>
<h2><a name="label:2" id="label:2">Methods</a></h2><!-- RDLabel: "Methods" -->
<dl>
<dt><a name="label:3" id="label:3"><code>each_subary_at_dims( *<var>dims</var> )</code></a></dt><!-- RDLabel: "each_subary_at_dims" -->
<dd>
<p>Iterator for each sub-array (not each element) specified by dimensions.</p>
<p>ARGUMENT</p>
<ul>
<li><var>dims</var> (integers) : specifies subsets at dimensions
specified here with the beginning-to-end selection.
For example, [0, 1] to specify the first 2 dimensions
(subsets will be 2D then), and [2] to specify the 3rd
dimension (subsets will be 1D). Duplication has no effect,
so [0,0] and [0] are the same. Also, its order has no effect.
See EXAMPLE below for more.</li>
</ul>
<p>RETURN VALUE</p>
<ul>
<li>self</li>
</ul>
<p>POSSIBLE EXCEPTIONS</p>
<ul>
<li>exception is raised if ( dims.min<0 || dims.max>=self.rank ).</li>
</ul>
<p>EXAMPLE</p>
<ul>
<li><p>Suppose that you want to do something with 2D sub-arrays in a
multi-dimension NArray. First, you include this module as follows:</p>
<pre>require "narray"
class NArray
include NumRu::Misc::MD_Iterators
end</pre>
<p>And prepare the array if you have not (here, it is 4D):</p>
<pre>na = NArray.int(10,2,5,2).indgen!</pre>
<p>Then you do the job like this:</p>
<pre>na.each_subary_at_dims(0,2){ |sub|
... # do whatever with sub
}</pre>
<p>This is equivalent to the following:</p>
<pre>(0...na.shape[3]).each{|j|
(0...na.shape[1]).each{|i|
sub = na[0..-1, i, 0..-1, j]
... # do whatever with sub
}
}</pre>
<p>Note that the loop must be nested 3 times when na is a 5D array,
if the latter approach is used. On the other hand, it will still
require the same single loop with the former. </p></li>
</ul></dd>
<dt><a name="label:4" id="label:4"><code>each_subary_at_dims_with_index( *<var>dims</var> )</code></a></dt><!-- RDLabel: "each_subary_at_dims_with_index" -->
<dd>
<p>Like <a href="#label:3">each_subary_at_dims</a> but the block takes two arguments:
subset and the subset specifier (index).</p>
<p>EXAMPLE</p>
<ul>
<li><p>Suppose the example above in <a href="#label:3">each_subary_at_dims</a> (EXAMPLE).
And suppose that you want to overwrite na with the result
you get. You can do it like this:</p>
<pre>na.each_subary_at_dims_with_index(0,2){ |sub,idx|
result = (sub + 10) / 2
na[*idx] = result
}</pre>
<p>Here, idx is an Array to be fed in the []= or [] methods
with asterisk (ungrouping).</p></li>
</ul></dd>
</dl>
</body>
</html>
|