composed_operator.html 20 KB
Newer Older
Philipp Frank's avatar
Philipp Frank committed
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
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

<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>nifty.operators.composed_operator.composed_operator &#8212; NIFTY 3.0.x documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '3.0.x',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
   
  <link rel="stylesheet" href="../../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for nifty.operators.composed_operator.composed_operator</h1><div class="highlight"><pre>
<span></span><span class="c1"># NIFTy</span>
<span class="c1"># Copyright (C) 2017  Theo Steininger</span>
<span class="c1">#</span>
<span class="c1"># Author: Theo Steininger</span>
<span class="c1">#</span>
<span class="c1"># This program is free software: you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU General Public License as published by</span>
<span class="c1"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c1"># (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># This program is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c1"># GNU General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU General Public License</span>
<span class="c1"># along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>

<span class="kn">from</span> <span class="nn">nifty.operators.linear_operator</span> <span class="k">import</span> <span class="n">LinearOperator</span>


<div class="viewcode-block" id="ComposedOperator"><a class="viewcode-back" href="../../../../composed_operator.html#nifty.ComposedOperator">[docs]</a><span class="k">class</span> <span class="nc">ComposedOperator</span><span class="p">(</span><span class="n">LinearOperator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; NIFTY class for composed operators.</span>

<span class="sd">    The  NIFTY composed operator class combines multiple linear operators.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    operators : tuple of NIFTy Operators</span>
<span class="sd">        The tuple of LinearOperators.</span>
<span class="sd">    default_spaces : tuple of ints *optional*</span>
<span class="sd">        Defines on which space(s) of a given field the Operator acts by</span>
<span class="sd">        default (default: None)</span>


<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    domain : tuple of DomainObjects, i.e. Spaces and FieldTypes</span>
<span class="sd">        The NIFTy.space in which the operator is defined.</span>
<span class="sd">    target : tuple of DomainObjects, i.e. Spaces and FieldTypes</span>
<span class="sd">        The NIFTy.space in which the outcome of the operator lives</span>
<span class="sd">    unitary : boolean</span>
<span class="sd">        Indicates whether the Operator is unitary or not.</span>

<span class="sd">    Raises</span>
<span class="sd">    ------</span>
<span class="sd">    TypeError</span>
<span class="sd">        Raised if</span>
<span class="sd">            * an element of the operator list is not an instance of the</span>
<span class="sd">              LinearOperator-baseclass.</span>

<span class="sd">    Notes</span>
<span class="sd">    -----</span>
<span class="sd">    Very usefull in case one has to transform a Field living over a product</span>
<span class="sd">    space (see example below).</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    Minimal example of transforming a Field living on two domains into its</span>
<span class="sd">    harmonic space.</span>

<span class="sd">    &gt;&gt;&gt; x1 = RGSpace(5)</span>
<span class="sd">    &gt;&gt;&gt; x2 = RGSpace(10)</span>
<span class="sd">    &gt;&gt;&gt; k1 = RGRGTransformation.get_codomain(x1)</span>
<span class="sd">    &gt;&gt;&gt; k2 = RGRGTransformation.get_codomain(x2)</span>
<span class="sd">    &gt;&gt;&gt; FFT1 = FFTOperator(domain=x1, target=k1,</span>
<span class="sd">                           domain_dtype=np.float64, target_dtype=np.complex128)</span>
<span class="sd">    &gt;&gt;&gt; FFT2 = FFTOperator(domain=x2, target=k2,</span>
<span class="sd">                           domain_dtype=np.float64, target_dtype=np.complex128)</span>
<span class="sd">    &gt;&gt;&gt; FFT = ComposedOperator((FFT1, FFT2)</span>
<span class="sd">    &gt;&gt;&gt; f = Field.from_random(&#39;normal&#39;, domain=(x1,x2))</span>
<span class="sd">    &gt;&gt;&gt; FFT.times(f)</span>

<span class="sd">    See Also</span>
<span class="sd">    --------</span>
<span class="sd">    EndomorphicOperator, ProjectionOperator,</span>
<span class="sd">    DiagonalOperator, SmoothingOperator, ResponseOperator,</span>
<span class="sd">    PropagatorOperator, ComposedOperator</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># ---Overwritten properties and methods---</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operators</span><span class="p">,</span> <span class="n">default_spaces</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ComposedOperator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">default_spaces</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_operator_store</span> <span class="o">=</span> <span class="p">()</span>
        <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="n">operators</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">op</span><span class="p">,</span> <span class="n">LinearOperator</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;The elements of the operator list must be&quot;</span>
                                <span class="s2">&quot;instances of the LinearOperator-baseclass&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_operator_store</span> <span class="o">+=</span> <span class="p">(</span><span class="n">op</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">_check_input_compatibility</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">inverse</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The input check must be disabled for the ComposedOperator, since it</span>
<span class="sd">        is not easily forecasteable what the output of an operator-call</span>
<span class="sd">        will look like.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">spaces</span>

    <span class="c1"># ---Mandatory properties and methods---</span>
    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">domain</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_domain&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_domain</span> <span class="o">=</span> <span class="p">()</span>
            <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operator_store</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_domain</span> <span class="o">+=</span> <span class="n">op</span><span class="o">.</span><span class="n">domain</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_domain</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_target&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_target</span> <span class="o">=</span> <span class="p">()</span>
            <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operator_store</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_target</span> <span class="o">+=</span> <span class="n">op</span><span class="o">.</span><span class="n">target</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_target</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">unitary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">_times</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_times_helper</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="s1">&#39;times&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_adjoint_times</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_inverse_times_helper</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="s1">&#39;adjoint_times&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_inverse_times</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_inverse_times_helper</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="s1">&#39;inverse_times&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_adjoint_inverse_times</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_times_helper</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="s1">&#39;adjoint_inverse_times&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_inverse_adjoint_times</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_times_helper</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="s1">&#39;inverse_adjoint_times&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_times_helper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="n">space_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">spaces</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">spaces</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">domain</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operator_store</span><span class="p">:</span>
            <span class="n">active_spaces</span> <span class="o">=</span> <span class="n">spaces</span><span class="p">[</span><span class="n">space_index</span><span class="p">:</span><span class="n">space_index</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">op</span><span class="o">.</span><span class="n">domain</span><span class="p">)]</span>
            <span class="n">space_index</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">op</span><span class="o">.</span><span class="n">domain</span><span class="p">)</span>

            <span class="n">x</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">op</span><span class="p">,</span> <span class="n">func</span><span class="p">)(</span><span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="o">=</span><span class="n">active_spaces</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span>

    <span class="k">def</span> <span class="nf">_inverse_times_helper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="n">space_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">spaces</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">spaces</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="p">))[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_operator_store</span><span class="p">):</span>
            <span class="n">active_spaces</span> <span class="o">=</span> <span class="n">spaces</span><span class="p">[</span><span class="n">space_index</span><span class="p">:</span><span class="n">space_index</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">op</span><span class="o">.</span><span class="n">target</span><span class="p">)]</span>
            <span class="n">space_index</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">op</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>

            <span class="n">x</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">op</span><span class="p">,</span> <span class="n">func</span><span class="p">)(</span><span class="n">x</span><span class="p">,</span> <span class="n">spaces</span><span class="o">=</span><span class="n">active_spaces</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../../index.html">
              <img class="logo" src="../../../../_static/nifty_logo_black.png" alt="Logo"/>
            </a></p><div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../../index.html">Documentation overview</a><ul>
  <li><a href="../../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2017, Theo Steininger.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
      
    </div>

    

    
  </body>
</html>