Changeset 845

Show
Ignore:
Timestamp:
09/15/05 07:38:12 (3 years ago)
Author:
rkern
Message:

* Added inline equations.
* Fixed tail handling with equations.
* Added "verb" attribute for the equation elements to handle cases like

\begin{eqnarray*} which replace \begin{equation}

* Added equation elements to more chapters of the Scipy tutorial.
* Made DBFormatter.to_formatted() return a string rather than an ElementTree?.

The XSLT object needs to convert the ElementTree? to a string, otherwise we end
up with LaTeX files with &'s littered all over.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nbdoc/trunk/notabene/docbook.py

    r833 r845  
    66from cgi import escape 
    77 
     8from lxml import etree as ET 
     9 
    810from notabene.formatter import Formatter 
    9  
    10 from lxml import etree as ET 
    11  
    1211from PyFontify import fontify 
    1312 
     
    9291 
    9392    def transform_figure(self, fig): 
    94         #logid = elem.xpath("../../@id")[0] 
    95         ET.dump(fig) 
    9693        number = fig.get('number') 
    9794        dbfig = ET.Element('figure', label="Fig%s"%(number)) 
     
    109106            strong.tail = caption.strip() 
    110107        dbfig.tail = fig.tail 
    111         ET.dump(dbfig) 
    112108        return dbfig 
    113109 
    114110    def transform_equation(self, equ): 
    115         """This uses the old method that uses graphic for image. It is unfortunate because otherwise the graphic-element is not used for images anymore, but mediaobject imageobjects instead. Perhaps we should just fix db2latex to get this right?""" 
    116         dbeq = ET.Element('equation') 
    117         title = equ.get('title') 
    118         tex = equ.get('tex') 
    119         if title is not None: 
    120             dbtit = ET.SubElement(dbeq, 'title') 
    121             dbtit.text = title 
    122         dbmath = ET.SubElement(dbeq, 'alt', role="tex") 
    123         dbmath.text = tex 
    124         #XXX add image support (for html & nbshell) here .. somewhere 
     111        # This uses the old method that uses graphic for image. It is 
     112        # unfortunate because otherwise the graphic-element is not used for images 
     113        # anymore, but mediaobject imageobjects instead. Perhaps we should just 
     114        # fix db2latex to get this right? 
     115        dbeq = ET.Element('informalequation') 
     116        tex = equ.text 
     117        verbatim = equ.get('verb', None) 
     118        if verbatim != '1': 
     119            tex = r'\begin{equation}%s\end{equation}' % tex 
     120        mo = ET.SubElement(dbeq, 'mediaobject') 
     121        to = ET.SubElement(mo, 'textobject') 
     122        phrase = ET.SubElement(to, 'phrase', role="latex") 
     123        phrase.text = tex 
     124        # XXX: add image support (for html & nbshell) here .. somewhere 
     125        dbeq.tail = equ.tail 
     126        return dbeq 
     127 
     128    def transform_inlineequation(self, equ): 
     129        dbeq = ET.Element('inlineequation') 
     130        tex = equ.text 
     131        verbatim = equ.get('verb', None) 
     132        if verbatim != '1': 
     133            tex = r'$%s$' % tex 
     134        mo = ET.SubElement(dbeq, 'inlinemediaobject') 
     135        to = ET.SubElement(mo, 'textobject') 
     136        phrase = ET.SubElement(to, 'phrase', role="latex") 
     137        phrase.text = tex 
     138        # XXX: add image support (for html & nbshell) here .. somewhere 
     139        dbeq.tail = equ.tail 
    125140        return dbeq 
    126141 
     
    182197        transform_elements('block', self.transform_block) 
    183198        transform_elements('figure', self.transform_figure) 
    184  
    185         ## figs = sheet2.xpath('.//ipython-figure') 
    186 ##         for fig in figs: 
    187 ##             parent, idx = parent_and_index(fig) 
    188 ##             number = fig.get('number') 
    189 ##             #logid = fig.get('logid', 'default-log') 
    190 ##             #elem = self.notebook.get_from_log('figure', number, logid=logid) 
    191 ##             img = self.transform_figure(fig) 
    192 ##             parent[idx] = img 
    193  
    194199        transform_elements('equation', self.transform_equation) 
     200        transform_elements('inlineequation', self.transform_inlineequation) 
    195201 
    196202        sheet2.tag = nodetype 
    197                      
    198203        return sheet2 
    199204 
     
    224229 
    225230    def to_formatted(self, sheet, kind='html', style=None): 
     231        """Convert a sheet to the desired output format. 
     232 
     233        Returns a string containing the new document. 
     234        """ 
    226235        if style is None: 
    227236            from notabene.styles import LightBGStyle as style 
     
    231240        getattr(self, 'prep_%s' % kind)(article_tree) 
    232241        newtree = xslt.apply(article_tree) 
    233         return newtree 
     242        return xslt.tostring(newtree) 
  • nbdoc/trunk/notabene/notebook.py

    r835 r845  
    344344            tmpf = os.fdopen(tmpfid, 'w+b') 
    345345            try: 
    346                 doc.write(tmpf
     346                tmpf.write(doc
    347347            finally: 
    348348                tmpf.close() 
     
    376376 
    377377        else: 
    378             doc.write(filename, 'utf-8') 
     378            f = open(filename, 'wb') 
     379            f.write(doc) 
     380            f.close() 
    379381            return filename 
    380382 
  • nbdoc/trunk/notabene/styles.py

    r827 r845  
    139139        ET.SubElement(sheet, xsl['import'], 
    140140                      href=os.path.join(XSLDIR, "latex", "docbook.xsl")) 
     141 
     142        # Add template for <code> 
     143        t = ET.SubElement(sheet, xsl.template, match="code") 
     144        ET.SubElement(t, xsl['call-template'], name="inline.monoseq") 
    141145 
    142146        # turn on fancyvrb 
  • nbdoc/trunk/scipy_tutorial/chapter01.pybk

    r844 r845  
    2626the command 
    2727</para> 
    28 <para>XXX: ipython</para> 
     28<para>XXX: ipython 
     29>>> from scipy import * 
     30</para> 
    2931 
    3032<section> 
     
    4951in that module is printed. For example: 
    5052</para> 
    51 <para>XXX: ipython</para> 
     53<para>XXX: ipython 
     54>>> info(optimize.fmin) 
     55 fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, 
     56      full_output=0, printmessg=1) 
     57 
     58Minimize a function using the simplex algorithm. 
     59 
     60Description: 
     61 
     62  Uses a Nelder-Mead simplex algorithm to find the minimum of function 
     63  of one or more variables. 
     64 
     65Inputs: 
     66 
     67  func -- the Python function or method to be minimized. 
     68  x0 -- the initial guess. 
     69  args -- extra arguments for func. 
     70  xtol -- relative tolerance 
     71 
     72Outputs: (xopt, {fopt, warnflag}) 
     73 
     74  xopt -- minimizer of function 
     75 
     76  fopt -- value of function at minimum: fopt = func(xopt) 
     77  warnflag -- Integer warning flag: 
     78              1 : 'Maximum number of function evaluations.' 
     79              2 : 'Maximum number of iterations.' 
     80 
     81Additional Inputs: 
     82 
     83  xtol -- acceptable relative error in xopt for convergence. 
     84  ftol -- acceptable relative error in func(xopt) for convergence. 
     85  maxiter -- the maximum number of iterations to perform. 
     86  maxfun -- the maximum number of function evaluations. 
     87  full_output -- non-zero if fval and warnflag outputs are desired. 
     88  printmessg -- non-zero to print convergence messages. 
     89   
     90</para> 
    5291<para>Another useful command is <code>source</code>. When given a function written in 
    5392Python as an argument, it prints out a listing of the source code for that 
  • nbdoc/trunk/scipy_tutorial/chapter02.pybk

    r844 r845  
    3131have been modified to return complex numbers instead of NaN's where appropriate. 
    3232</para> 
    33 <para>XXX: scipy.sqrt(-1) </para> 
     33<para>XXX: ipython 
     34>>> scipy.sqrt(-1) </para> 
    3435</section> 
    3536 
    3637<section> 
    3738<title>scipy_base routines</title> 
    38 <para>The purpose of scipy_base is to collect general-purpose routines 
     39<para>The purpose of scipy_base is to collect general-purpose routines 
    3940that the other sub-packages can use and to provide a simple replacement for 
    4041Numeric. Anytime you might think to import Numeric, you can import scipy_base 
     
    265266functions that might have been placed in scipy_base except for their dependence 
    266267on other sub-packages of SciPy. For example the factorial and comb functions 
    267 compute  
    268 <inlineequation> 
    269 <alt role="latex">n!</alt> 
    270 </inlineequation> 
    271  and  
    272 <inlineequation> 
    273 <alt role="latex">n!/k!(n-k)!</alt> 
    274 </inlineequation>  
     268compute <ipython-inlineequation>n!</ipython-inlineequation> 
     269 and <ipython-inlineequation>n!/k!(n-k)!</ipython-inlineequation>  
    275270using either exact integer arithmetic (thanks to 
    276271Python's Long integer object), or by using floating-point precision and the 
     
    291286automatically evaluate the object at the correct points to obtain an N-point 
    292287approximation to the  
    293 <inlineequation> 
    294 <alt role="latex">o^{\textrm{th}}</alt> 
    295 </inlineequation>-derivative at a given point.  
     288<ipython-inlineequation>o^{\textrm{th}}</ipython-inlineequation>-derivative at a 
     289given point.  
    296290</para> 
    297291</section> 
  • nbdoc/trunk/scipy_tutorial/chapter03.pybk

    r844 r845  
    1010rules as other math functions in Numerical Python. Many of these functions also 
    1111accept complex-numbers as input. For a complete list of the available functions 
    12 with a one-line description type &gt;&gt;&gt;info(special). Each function also has it's 
     12with a one-line description type <code>&gt;&gt;&gt;info(special)</code>. Each 
     13function also has it's 
    1314own documentation accessible using help. If you don't see a function you need, 
    1415consider writing it and contributing it to the library. You can write the 
  • nbdoc/trunk/scipy_tutorial/chapter04.pybk

    r844 r845  
    2222<title>General integration (integrate.quad)</title> 
    2323<para>The function quad is provided to integrate a function of one variable 
    24 between two points. The points can be \pm\infty  (\pm integrate.inf) to indicate 
     24between two points. The points can be  
     25<ipython-inlineequation>\pm\infty</ipython-inlineequation> 
     26(<ipython-inlineequation>\pm</ipython-inlineequation> integrate.inf) to indicate 
    2527infinite limits. For example, suppose you wish to integrate a bessel function 
    26 jv(2.5,x) along the interval [0,4.5]. I=\int_{0}^{4.5}J_{2.5}\left(x\right)\, 
    27 dx. This could be computed using quad: 
     28<code>jv(2.5,x)</code> along the interval [0,4.5].  
     29<ipython-equation>I=\int_{0}^{4.5}J_{2.5}\left(x\right)\,dx</ipython-equation>. 
     30This could be computed using quad: 
    2831</para> 
    2932<para>XXX: ipython 
     
    4649integral and the second element holding an upper bound on the error. Notice, 
    4750that in this case, the true value of this integral is 
     51<ipython-equation> 
    4852I=\sqrt{\frac{2}{\pi}}\left(\frac{18}{27}\sqrt{2}\cos\left(4.5\right)-\frac{4}{27}\sqrt{2}\sin\left(4.5\right)+\sqrt{2\pi}\textrm{Si}\left(\frac{3}{\sqrt{\pi}}\right)\right), 
     53</ipython-equation> 
    4954where 
    50 \textrm{Si}\left(x\right)=\int_{0}^{x}\sin\left(\frac{\pi}{2}t^{2}\right)\, dt. 
     55<ipython-equation> 
     56\textrm{Si}\left(x\right)=\int_{0}^{x}\sin\left(\frac{\pi}{2}t^{2}\right)\, dt 
     57</ipython-equation>. 
    5158is the Fresnel sine integral. Note that the numerically-computed integral is 
    52 within 1.04\times10^{-11} of the exact result --- well below the reported error 
     59within <ipython-inlineequation>1.04\times10^{-11}</ipython-inlineequation> of 
     60the exact result --- well below the reported error 
    5361bound.  
    5462</para> 
    55 <para>Infinite inputs are also allowed in quad by using \pm integrate.inf (or 
     63<para>Infinite inputs are also allowed in quad by using  
     64<ipython-inlineequation>\pm</ipython-inlineequation> integrate.inf (or 
    5665inf) as one of the arguments. For example, suppose that a numerical value for 
    5766the exponential 
    58 integral:E_{n}\left(x\right)=\int_{1}^{\infty}\frac{e^{-xt}}{t^{n}}\, dt. is 
     67integral: 
     68<ipython-equation>E_{n}\left(x\right)=\int_{1}^{\infty}\frac{e^{-xt}}{t^{n}}\, dt 
     69</ipython-equation>. is 
    5970desired (and the fact that this integral can be computed as special.expn(n,x) is 
    6071forgotten). The functionality of the function special.expn can be replicated by 
     
    7990the error bound may underestimate the error due to possible numerical error in 
    8091the integrand from the use of quad). The integral in this case is 
     92<ipython-equation> 
    8193I_{n}=\int_{0}^{\infty}\int_{1}^{\infty}\frac{e^{-xt}}{t^{n}}\, dt\, 
    82 dx=\frac{1}{n}
     94dx=\frac{1}{n}</ipython-equation>
    8395</para> 
    8496<para>XXX: ipython 
     
    100112arguments are defined in the correct order. In addition, the limits on all inner 
    101113integrals are actually functions which can be constant functions. An example of 
    102 using double integration to compute several values of I_{n} is shown below: 
     114using double integration to compute several values of 
     115<ipython-inlineequation>I_{n}<ipython-inlineequation> is shown below: 
    103116</para> 
    104117<para>XXX: ipython 
     
    139152</para> 
    140153<para>If the samples are equally-spaced and the number of samples available is 
    141 2^{k}+1 for some integer k, then Romberg integration can be used to obtain 
     154<ipython-inlineequation>2^{k}+1</ipython-inlineequation> for some integer  
     155<ipython-inlineequation>k</ipython-inlineequation>, then Romberg integration can be used to obtain 
    142156high-precision estimates of the integral using the available samples. Romberg 
    143157integration uses the trapezoid rule at step-sizes related by a power of two and 
     
    153167conditions is another useful example. The function odeint is available in SciPy 
    154168for integrating a first-order vector differential 
    155 equation:\frac{d\mathbf{y}}{dt}=\mathbf{f}\left(\mathbf{y},t\right), given 
    156 initial conditions \mathbf{y}\left(0\right)=y_{0}, where \mathbf{y} is a length 
    157 N vector and \mathbf{f} is a mapping from \mathcal{R}^{N} to \mathcal{R}^{N}. A 
     169equation: 
     170<ipython-equation>\frac{d\mathbf{y}}{dt}=\mathbf{f}\left(\mathbf{y},t\right),</ipython-equation>  
     171given 
     172initial conditions 
     173<ipython-inlineequation>\mathbf{y}\left(0\right)=y_{0}</ipython-inlineequation>, where  
     174<ipython-inlineequation>\mathbf{y}</ipython-inlineequation> is a length 
     175<ipython-inlineequation>N</ipython-inlineequation> vector and  
     176<ipython-inlineequation>\mathbf{f}</ipython-inlineequation> is a mapping from  
     177<ipython-inlineequation>\mathcal{R}^{N}</ipython-inlineequation> to  
     178<ipython-inlineequation>\mathcal{R}^{N}</ipython-inlineequation>. A 
    158179higher-order ordinary differential equation can always be reduced to a 
    159180differential equation of this type by introducing intermediate derivatives into 
    160 the \mathbf{y} vector.  
     181the <ipython-inlineequation>\mathbf{y}</ipython-inlineequation> vector.  
    161182</para> 
    162183<para> 
    163184For example suppose it is desired to find the solution to the following 
    164 second-order differential equation:\frac{d^{2}w}{dz^{2}}-zw(z)=0 with initial 
     185second-order differential equation: 
     186<ipython-equation>\frac{d^{2}w}{dz^{2}}-zw(z)=0</ipython-equation> with initial 
    165187conditions 
    166 w\left(0\right)=\frac{1}{\sqrt[3]{3^{2}}\Gamma\left(\frac{2}{3}\right)} and 
    167 \left.\frac{dw}{dz}\right|_{z=0}=-\frac{1}{\sqrt[3]{3}\Gamma\left(\frac{1}{3}\right)}
     188<ipython-inlineequation>w\left(0\right)=\frac{1}{\sqrt[3]{3^{2}}\Gamma\left(\frac{2}{3}\right)}</ipython-inlineequation> and 
     189<ipython-inlineequation>\left.\frac{dw}{dz}\right|_{z=0}=-\frac{1}{\sqrt[3]{3}\Gamma\left(\frac{1}{3}\right)}</ipython-inlineequation>
    168190It is known that the solution to this differential equation with these boundary 
    169 conditions is the Airy function w=\textrm{Ai}\left(z\right), which gives a means 
     191conditions is the Airy function <ipython-equation>w=\textrm{Ai}\left(z\right)</ipython-equation>, which gives a means 
    170192to check the integrator using special.airy.  
    171193</para> 
    172194<para> 
    173195First, convert this ODE into standard form by setting 
    174 \mathbf{y}=\left[\frac{dw}{dz},w\right] and t=z. Thus, the differential equation 
    175 becomes\frac{d\mathbf{y}}{dt}=\left[\begin{array}{c} 
     196<ipython-inlineequation>\mathbf{y}=\left[\frac{dw}{dz},w\right]</ipython-inlineequation> 
     197and <ipython-inlineequation>t=z</ipython-inlineequation>. Thus, the differential equation 
     198becomes 
     199<ipython-equation>\frac{d\mathbf{y}}{dt}=\left[\begin{array}{c} 
    176200ty_{1}\\ 
    177201y_{0}\end{array}\right]=\left[\begin{array}{cc} 
     
    181205y_{1}\end{array}\right]=\left[\begin{array}{cc} 
    1822060 &amp; t\\ 
    183 1 &amp; 0\end{array}\right]\mathbf{y}. In other words, 
    184 \mathbf{f}\left(\mathbf{y},t\right)=\mathbf{A}\left(t\right)\mathbf{y}.  
    185 </para> 
    186 <para> 
    187 As an interesting reminder, if \mathbf{A}\left(t\right) commutes with 
    188 \int_{0}^{t}\mathbf{A}\left(\tau\right)\, d\tau  under matrix multiplication, 
     2071 &amp; 0\end{array}\right]\mathbf{y}.</ipython-equation>  
     208In other words, 
     209<ipython-equation>\mathbf{f}\left(\mathbf{y},t\right)=\mathbf{A}\left(t\right)\mathbf{y}.  
     210</ipython-equation> 
     211</para> 
     212<para> 
     213As an interesting reminder, if 
     214<ipython-inlineequation>\mathbf{A}\left(t\right)</ipython-inlineequation> commutes with 
     215<ipython-inlineequation>\int_{0}^{t}\mathbf{A}\left(\tau\right)\, 
     216d\tau</ipython-inlineequation>  under matrix multiplication, 
    189217then this linear differential equation has an exact solution using the matrix 
    190218exponential: 
     219<ipython-equation> 
    191220\mathbf{y}\left(t\right)=\exp\left(\int_{0}^{t}\mathbf{A}\left(\tau\right)d\tau\right)\mathbf{y}\left(0\right), 
    192 However, in this case, \mathbf{A}\left(t\right) and its integral do not commute. 
     221</ipython-equation> 
     222However, in this case, 
     223<ipython-inlineequation>\mathbf{A}\left(t\right)</ipython-inlineequation> and its integral do not commute. 
    193224</para> 
    194225<para> 
     
    206237The following example illustrates the use of odeint including the usage of the 
    207238Dfun option which allows the user to specify a gradient (with respect to 
    208 \mathbf{y}) of the function, \mathbf{f}\left(\mathbf{y},t\right). 
     239<ipython-inlineequation>\mathbf{y})</ipython-inlineequation> of the function, 
     240<ipython-inlineequation>\mathbf{f}\left(\mathbf{y},t\right)</ipython-inlineequation>. 
    209241</para> 
    210242<para>XXX: ipython 
  • nbdoc/trunk/scipy_tutorial/chapter05.pybk

    r844 r845  
    6161demonstrate the minimization function consider the problem of minimizing the 
    6262Rosenbrock function of N 
    63 variables:f\left(\mathbf{x}\right)=\sum_{i=1}^{N-1}100\left(x_{i}-x_{i-1}^{2}\right)^{2}+\left(1-x_{i-1}\right)^{2}. 
    64 The minimum value of this function is 0 which is achieved when x_{i}=1. This 
     63variables: 
     64<ipython-equation> 
     65f\left(\mathbf{x}\right)=\sum_{i=1}^{N-1}100\left(x_{i}-x_{i-1}^{2}\right)^{2}+\left(1-x_{i-1}\right)^{2}. 
     66</ipython-equation> 
     67The minimum value of this function is 0 which is achieved when 
     68<ipython-inlineequation>x_{i}=1</ipython-inlineequation>. This 
    6569minimum can be found using the fmin routine as shown in the example below: 
    6670</para> 
     
    96100<para> 
    97101To demonstrate this algorithm, the Rosenbrock function is again used. The 
    98 gradient of the Rosenbrock function is the vector: \frac{\partial f}{\partial 
    99 x_{j}}This expression is valid for the interior derivatives. Special cases 
    100 are\frac{\partial f}{\partial x_{0}} A Python function which computes this 
     102gradient of the Rosenbrock function is the vector:  
     103<ipython-equation verb="1"><![CDATA[ 
     104\begin{eqnarray*} 
     105\frac{\partial f}{\partial x_{j}} & = & 
     106\sum_{i=1}^{N}200\left(x_{i}-x_{i-1}^{2}\right)\left(\delta_{i,j}-2x_{i-1}\delta_{i-1,j}\right)-2\left(1-x_{i-1}\right)\delta_{i-1,j}.\\ 
     107& = & 
     108200\left(x_{j}-x_{j-1}^{2}\right)-400x_{j}\left(x_{j+1}-x_{j}^{2}\right)-2\left(1-x_{j}\right). 
     109\end{eqnarray*}]]> 
     110</ipython-equation> 
     111This expression is valid for the interior derivatives. Special cases 
     112are 
     113<ipython-equation verb="1"> 
     114\begin{eqnarray*} 
     115\frac{\partial f}{\partial x_{0}} &amp; = &amp; 
     116-400x_{0}\left(x_{1}-x_{0}^{2}\right)-2\left(1-x_{0}\right),\\ 
     117\frac{\partial 
     118f}{\partial x_{N-1}} &amp; = &amp; 
     119200\left(x_{N-1}-x_{N-2}^{2}\right). 
     120\end{eqnarray*} 
     121</ipython-equation> 
     122A Python function which computes this 
    101123gradient is constructed by the code-segment: 
    102124</para> 
     
    139161algorithm to (approximately) invert the local Hessian. Newton's method is based 
    140162on fitting the function locally to a quadratic 
    141 form:f\left(\mathbf{x}\right)\approx f\left(\mathbf{x}_{0}\right)+\nabla 
     163form: 
     164<ipython-equation>f\left(\mathbf{x}\right)\approx f\left(\mathbf{x}_{0}\right)+\nabla 
    142165f\left(\mathbf{x}_{0}\right)\cdot\left(\mathbf{x}-\mathbf{x}_{0}\right)+\frac{1}{2}\left(\mathbf{x}-\mathbf{x}_{0}\right)^{T}\mathbf{H}\left(\mathbf{x}_{0}\right)\left(\mathbf{x}-\mathbf{x}_{0}\right). 
    143 where \mathbf{H}\left(\mathbf{x}_{0}\right) is a matrix of second-derivatives 
     166</ipython-equation> 
     167where 
     168<ipython-inlineequation>\mathbf{H}\left(\mathbf{x}_{0}\right)</ipython-inlineequation> is a matrix of second-derivatives 
    144169(the Hessian). If the Hessian is positive definite then the local minimum of 
    145170this function can be found by setting the gradient of the quadratic form to 
    146171zero, resulting in 
    147 \mathbf{x}_{\textrm{opt}}=\mathbf{x}_{0}-\mathbf{H}^{-1}\nabla f. The inverse of 
     172<ipython-equation> 
     173\mathbf{x}_{\textrm{opt}}=\mathbf{x}_{0}-\mathbf{H}^{-1}\nabla f. 
     174</ipython-equation> The inverse of 
    148175the Hessian is evaluted using the conjugate-gradient method. An example of 
    149176employing this method to minimizing the Rosenbrock function is given below. To 
     
    158185<title>Full Hessian example:</title> 
    159186<para>The Hessian of the Rosenbrock function is 
    160 H_{ij}=\frac{\partial^{2}f}{\partial x_{i}\partial x_{j}} if 
    161 i,j\in\left[1,N-2\right] with i,j\in\left[0,N-1\right] defining the N\times N 
    162 matrix. Other non-zero entries of the matrix are \frac{\partial^{2}f}{\partial 
    163 x_{0}^{2}} For example, the Hessian when N=5 is 
     187<ipython-equation verb="1">\begin{eqnarray*} 
     188H_{ij}=\frac{\partial^{2}f}{\partial x_{i}\partial x_{j}} &amp; = &amp; 
     189200\left(\delta_{i,j}-2x_{i-1}\delta_{i-1,j}\right)-400x_{i}\left(\delta_{i+1,j}-2x_{i}\delta_{i,j}\right)-400\delta_{i,j}\left(x_{i+1}-x_{i}^{2}\right)+2\delta_{i,j},\\ 
     190&amp; = &amp; 
     191\left(202+1200x_{i}^{2}-400x_{i+1}\right)\delta_{i,j}-400x_{i}\delta_{i+1,j}-400x_{i-1}\delta_{i-1,j},\end{eqnarray*} 
     192</ipython-equation> 
     193 if 
     194<ipython-inlineequation>i,j\in\left[1,N-2\right]</ipython-inlineequation> with  
     195<ipython-inlineequation>i,j\in\left[0,N-1\right]</ipython-inlineequation> defining the  
     196<ipython-inlineequation>N\times N</ipython-inlineequation>  
     197matrix. Other non-zero entries of the matrix are  
     198<ipython-equation verb="1">\begin{eqnarray*} 
     199\frac{\partial^{2}f}{\partial x_{0}^{2}} &amp; = &amp; 1200x_{0}^{2}-400x_{1}+2,\\ 
     200\frac{\partial^{2}f}{\partial x_{0}\partial x_{1}}=\frac{\partial^{2}f}{\partial x_{1}\partial x_{0} 
     201} &amp; = &amp; -400x_{0},\\ 
     202\frac{\partial^{2}f}{\partial x_{N-1}\partial x_{N-2}}=\frac{\partial^{2}f}{\partial x_{N-2}\partial 
     203 x_{N-1}} &amp; = &amp; -400x_{N-2},\\ 
     204\frac{\partial^{2}f}{\partial x_{N-1}^{2}} &amp; = &amp; 200. 
     205\end{eqnarray*}</ipython-equation> 
     206For example, the Hessian when N=5 is 
     207<ipython-equation> 
    164208\mathbf{H}=\left[\begin{array}{ccccc} 
    1652091200x_{0}^{2}-400x_{1}+2 &amp; -400x_{0} &amp; 0 &amp; 0 &amp; 0\\ 
     
    1672110 &amp; -400x_{1} &amp; 202+1200x_{2}^{2}-400x_{3} &amp; -400x_{2} &amp; 0\\ 
    1682120 &amp;  &amp; -400x_{2} &amp; 202+1200x_{3}^{2}-400x_{4} &amp; -400x_{3}\\ 
    169 0 &amp; 0 &amp; 0 &amp; -400x_{3} &amp; 200\end{array}\right]. The code which computes this 
     2130 &amp; 0 &amp; 0 &amp; -400x_{3} &amp; 200\end{array}\right]. 
     214</ipython-equation> 
     215 The code which computes this 
    170216Hessian along with the code to minimize the function using fmin_ncg is shown in 
    171217the following example: 
     
    210256<para> 
    211257In this case, the product of the Rosenbrock Hessian with an arbitrary vector is 
    212 not difficult to compute. If \mathbf{p} is the arbitrary vector, then 
    213 \mathbf{H}\left(\mathbf{x}\right)\mathbf{p} has elements: 
     258not difficult to compute. If 
     259<ipython-inlineequation>\mathbf{p}</ipython-inlineequation> is the arbitrary vector, then 
     260<ipython-inlineequation>\mathbf{H}\left(\mathbf{x}\right)\mathbf{p}</ipython-inlineequation> 
     261has elements: 
     262<ipython-equation> 
    214263\mathbf{H}\left(\mathbf{x}\right)\mathbf{p}=\left[\begin{array}{c} 
    215264\left(1200x_{0}^{2}-400x_{1}+2\right)p_{0}-400x_{0}p_{1}\\ 
     
    217266-400x_{i-1}p_{i-1}+\left(202+1200x_{i}^{2}-400x_{i+1}\right)p_{i}-400x_{i}p_{i+1}\\ 
    218267\vdots\\ 
    219 -400x_{N-2}p_{N-2}+200p_{N-1}\end{array}\right]. Code which makes use of the 
     268-400x_{N-2}p_{N-2}+200p_{N-1}\end{array}\right].  
     269</ipython-equation> 
     270Code which makes use of the 
    220271fhess_p keyword to minimize the Rosenbrock function using fmin_ncg follows: 
    221272</para> 
     
    249300<para>All of the previously-explained minimization procedures can be used to 
    250301solve a least-squares problem provided the appropriate objective function is 
    251 constructed. For example, suppose it is desired to fit a set of data \left\{ 
    252 \mathbf{x}_{i},\mathbf{y}_{i}\right\}  to a known model, 
    253 \mathbf{y}=\mathbf{f}\left(\mathbf{x},\mathbf{p}\right) where \mathbf{p} is a 
     302constructed. For example, suppose it is desired to fit a set of data  
     303<ipython-inlineequation>\left\{ 
     304\mathbf{x}_{i},\mathbf{y}_{i}\right\}</ipython-inlineequation>  to a known model, 
     305<ipython-inlineequation>\mathbf{y}=\mathbf{f}\left(\mathbf{x},\mathbf{p}\right)</ipython-inlineequation> 
     306where <ipython-inlineequation>\mathbf{p}</ipython-inlineequation> is a 
    254307vector of parameters for the model that need to be found. A common method for 
    255308determining which parameter vector gives the best fit to the data is to minimize 
    256309the sum of squares of the residuals. The residual is usually defined for each 
    257310observed data-point as 
     311<ipython-equation> 
    258312e_{i}\left(\mathbf{p},\mathbf{y}_{i},\mathbf{x}_{i}\right)=\left\Vert 
    259 \mathbf{y}_{i}-\mathbf{f}\left(\mathbf{x}_{i},\mathbf{p}\right)\right\Vert . An 
     313\mathbf{y}_{i}-\mathbf{f}\left(\mathbf{x}_{i},\mathbf{p}\right)\right\Vert . 
     314</ipython-equation> 
     315 An 
    260316objective function to pass to any of the previous minization algorithms to 
    261317obtain a least-squares fit is. 
     318<ipython-equation> 
    262319J\left(\mathbf{p}\right)=\sum_{i=0}^{N-1}e_{i}^{2}\left(\mathbf{p}\right). 
    263 </para
    264 <para>The leastsq algorithm performs this squaring and summing of the residuals 
     320</ipython-equation
     321The leastsq algorithm performs this squaring and summing of the residuals 
    265322automatically. It takes as an input argument the vector function 
    266 \mathbf{e}\left(\mathbf{p}\right) and returns the value of \mathbf{p} which 
    267 minimizes J\left(\mathbf{p}\right)=\mathbf{e}^{T}\mathbf{e} directly. The user 
     323<ipython-inlineequation>\mathbf{e}\left(\mathbf{p}\right)</ipython-inlineequation> 
     324and returns the value of 
     325<ipython-inlineequation>\mathbf{p}</ipython-inlineequation> which 
     326minimizes 
     327<ipython-inlineequation>J\left(\mathbf{p}\right)=\mathbf{e}^{T}\mathbf{e}</ipython-inlineequation> 
     328directly. The user 
    268329is also encouraged to provide the Jacobian matrix of the function (with 
    269330derivatives down the columns or across the rows). If the Jacobian is not 
     
    271332</para> 
    272333<para>An example should clarify the usage. Suppose it is believed some measured 
    273 data follow a sinusoidal patterny_{i}=A\sin\left(2\pi kx_{i}+\theta\right) where 
    274 the parameters A, k, and \theta  are unknown. The residual vector is 
    275 e_{i}=\left|y_{i}-A\sin\left(2\pi kx_{i}+\theta\right)\right|. By defining a 
     334data follow a sinusoidal pattern 
     335<ipython-equation>y_{i}=A\sin\left(2\pi kx_{i}+\theta\right)  
     336</ipython-equation>  
     337where 
     338the parameters <ipython-inlineequation>A</ipython-inlineequation>, 
     339<ipython-inlineequation>k</ipython-inlineequation>, and 
     340<ipython-inlineequation>\theta</ipython-inlineequation>  are unknown. The residual vector is 
     341<ipython-equation> 
     342e_{i}=\left|y_{i}-A\sin\left(2\pi kx_{i}+\theta\right)\right|.  
     343</ipython-equation> 
     344By defining a 
    276345function to compute the residuals and (selecting an appropriate starting 
    277346position), the least-squares fit routine can be used to find the best-fit 
    278 parameters \hat{A},\,\hat{k},\,\hat{\theta}. This is shown in the following 
     347parameters 
     348<ipython-inlineequation>\hat{A},\,\hat{k},\,\hat{\theta}</ipython-inlineequation>. 
     349This is shown in the following 
    279350example and a plot of the results is shown in Figure [fig:least_squares_fit]. 
    280351</para> 
     
    327398rarely be used. The brent method uses Brent's algorithm for locating a minimum. 
    328399Optimally a bracket should be given which contains the minimum desired. A 
    329 bracket is a triple \left(a,b,c\right) such that 
    330 f\left(a\right)&gt;f\left(b\right)&lt;f\left(c\right) and a &lt;b &lt;c. If this is not given, 
     400bracket is a triple 
     401<ipython-inlineequation>\left(a,b,c\right)</ipython-inlineequation> such that 
     402<ipython-inlineequation>f\left(a\right)&gt;f\left(b\right)&lt;f\left(c\right) 
     403</ipython-inlineequation>and <ipython-inlineequation>a &lt;b &lt;c</ipython-inlineequation>. If this is not given, 
    331404then alternatively two starting points can be chosen and a bracket will be found 
    332405from these points using a simple marching algorithm. If these two starting 
     
    346419</para> 
    347420<para> 
    348 For example, to find the minimum of J_{1}\left(x\right) near x=5, fminbound can 
    349 be called using the interval \left[4,7\right] as a constraint. The result is 
    350 x_{\textrm{min}}=5.3314: 
     421For example, to find the minimum of 
     422<ipython-inlineequation>J_{1}\left(x\right)</ipython-inlineequation> near 
     423<ipython-inlineequation>x=5</ipython-inlineequation>, fminbound can 
     424be called using the interval 
     425<ipython-inlineequation>\left[4,7\right]</ipython-inlineequation> as a constraint. The result is 
     426<ipython-inlineequation>x_{\textrm{min}}=5.3314</ipython-inlineequation>: 
    351427</para> 
    352428<para> XXX: ipython 
     
    368444root of a set of non-linear equations, the command optimize.fsolve is needed. 
    369445For example, the following example finds the roots of the single-variable 
    370 transcendental equationx+2\cos\left(x\right)=0, and the set of non-linear 
    371 equationsx_{0}\cos\left(x_{1}\right) The results are x=-1.0299 and 
    372 x_{0}=6.5041,\, x_{1}=0.9084. 
     446transcendental equation 
     447<ipython-equation> 
     448x+2\cos\left(x\right)=0, 
     449</ipython-equation> 
     450 and the set of non-linear 
     451equations 
     452<ipython-equation> 
     453x_{0}\cos\left(x_{1}\right) 
     454</ipython-equation> 
     455 The results are <ipython-inlineequation>x=-1.0299</ipython-inlineequation> and 
     456<ipython-inlineequation>x_{0}=6.5041,\, x_{1}=0.9084</ipython-inlineequation>. 
    373457</para> 
    374458<para>XXX: ipython 
     
    405489problem of finding a fixed-point of a function. A fixed point of a function is 
    406490the point at which evaluation of the function returns the point: 
    407 g\left(x\right)=x. Clearly the fixed point of g is the root of 
    408 f\left(x\right)=g\left(x\right)-x.  Equivalently, the root of f is the 
    409 fixed_point of g\left(x\right)=f\left(x\right)+x. The routine fixed_point 
     491<ipython-inlineequation>g\left(x\right)=x</ipython-inlineequation>. Clearly the 
     492fixed point of <ipython-inlineequation>g</ipython-inlineequation> is the root of 
     493<ipython-inlineequation>f\left(x\right)=g\left(x\right)-x</ipython-inlineequation>. 
     494Equivalently, the root of <ipython-inlineequation>f</ipython-inlineequation> is the 
     495fixed_point of 
     496<ipython-inlineequation>g\left(x\right)=f\left(x\right)+x</ipython-inlineequation>. The routine fixed_point 
    410497provides a simple iterative method using Aitkens sequence acceleration to 
    411498estimate the fixed point of g given a starting point.  
  • nbdoc/trunk/scipy_tutorial/chapter06.pybk

    r844 r845  
    5353directly and parametrically. The direct method finds the spline representation 
    5454of a curve in a two-dimensional plane using the function interpolate.splrep. The 
    55 first two arguments are the only ones required, and these provide the x and y 
    56 components of the curve. The normal output is a 3-tuple, \left(t,c,k\right), 
    57 containing the knot-points, t, the coefficients c and the order k of the spline. 
     55first two arguments are the only ones required, and these provide the 
     56<ipython-inlineequation>x</ipython-inlineequation> and 
     57<ipython-inlineequation>y</ipython-inlineequation>  
     58components of the curve. The normal output is a 3-tuple, 
     59<ipython-inlineequation>\left(t,c,k\right)</ipython-inlineequation>, 
     60containing the knot-points, <ipython-inlineequation>t</ipython-inlineequation>, 
     61the coefficients <ipython-inlineequation>c</ipython-inlineequation> and the 
     62order <ipython-inlineequation>k</ipython-inlineequation> of the spline. 
    5863The default spline order is cubic, but this can be changed with the input 
    59 keyword, k
     64keyword, <ipython-inlineequation>k</ipython-inlineequation>
    6065</para> 
    6166<para>For curves in N-dimensional space the function interpolate.splprep allows 
     
    6671variable is given with the keword argument, u, which defaults to an 
    6772equally-spaced monotonic sequence between 0 and 1. The default output consists 
    68 of two objects: a 3-tuple, \left(t,c,k\right), containing the spline 
     73of two objects: a 3-tuple, 
     74<ipython-inlineequation>\left(t,c,k\right)</ipython-inlineequation>, containing the spline 
    6975representation and the parameter variable u.  
    7076</para> 
    7177<para>The keyword argument, s, is used to specify the amount of smoothing to 
    72 perform during the spline fit. The default value of s is s=m-\sqrt{2m} where m 
     78perform during the spline fit. The default value of s is 
     79<ipython-inlineequation>s=m-\sqrt{2m}</ipython-inlineequation> where m 
    7380is the number of data-points being fit. Therefore, if no smoothing is desired a 
    74 value of \mathbf{s}=0 should be passed to the routines.  
     81value of <ipython-inlineequation>\mathbf{s}=0</ipython-inlineequation> should be passed to the routines.  
    7582</para> 
    7683<para>Once the spline representation of the data has been determined, functions 
     
    7885(interpolate.splev, interpolate.splade) at any point and the integral of the 
    7986spline between any two points (interpolate.splint). In addition, for cubic 
    80 splines (k=3) with 8 or more knots, the roots of the spline can be estimated 
     87splines (<ipython-inlineequation>k=3</ipython-inlineequation>) with 8 or more knots, the roots of the spline can be estimated 
    8188(interpolate.sproot). These functions are demonstrated in the example that 
    8289follows (see also Figure [fig:spline-1d]). 
     
    150157interpolate.bisplrep is available. This function takes as required inputs the 
    1511581-D arrays x, y, and z which represent points on the surface 
    152 z=f\left(x,y\right). The default output is a list \left[tx,ty,c,kx,ky\right] 
     159<ipython-inlineequation>z=f\left(x,y\right)</ipython-inlineequation>. The 
     160default output is a list 
     161<ipython-inlineequation>\left[tx,ty,c,kx,ky\right]</ipython-inlineequation>  
    153162whose entries represent respectively, the components of the knot positions, the 
    154163coefficients of the spline, and the order of the spline in each coordinate. It 
     
    156165passed easily to the function interpolate.bisplev. The keyword, s, can be used 
    157166to change the amount of smoothing performed on the data while determining the 
    158 appropriate spline. The default value is s=m-\sqrt{2m} where m is the number of 
     167appropriate spline. The default value is 
     168<ipython-inlineequation>s=m-\sqrt{2m}</ipython-inlineequation> where m is the number of 
    159169data points in the x, y, and z vectors. As a result, if no smoothing is desired, 
    160 then s=0 should be passed to interpolate.bisplrep. 
     170then <ipython-inlineequation>s=0</ipython-inlineequation> should be passed to interpolate.bisplrep. 
    161171</para> 
    162172<para>To evaluate the two-dimensional spline and it's partial derivatives (up to 
  • nbdoc/trunk/scipy_tutorial/chapter07.pybk

    r844 r845  
    1515<para>A B-spline is an approximation of a continuous function over a 
    1616finite-domain in terms of B-spline coefficients and knot points. If the 
    17 knot-points are equally spaced with spacing \Delta x, then the B-spline 
     17knot-points are equally spaced with spacing <ipython-inlineequation>\Delta 
     18x</ipython-inlineequation>, then the B-spline 
    1819approximation to a 1-dimensional function is the finite-basis expansion. 
    19 y\left(x\right)\approx\sum_{j}c_{j}\beta^{o}\left(\frac{x}{\Delta x}-j\right). 
    20 In two dimensions with knot-spacing \Delta x and \Delta y, the function 
     20<ipython-equation>y\left(x\right)\approx\sum_{j}c_{j}\beta^{o}\left(\frac{x}{\Delta 
     21x}-j\right).</ipython-equation>  
     22In two dimensions with knot-spacing <ipython-inlineequation>\Delta 
     23x</ipython-inlineequation> and <ipython-inlineequation>\Delta 
     24y</ipython-inlineequation>, the function 
    2125representation is 
    22 z\left(x,y\right)\approx\sum_{j}\sum_{k}c_{jk}\beta^{o}\left(\frac{x}{\Delta 
    23 x}-j\right)\beta^{o}\left(\frac{y}{\Delta y}-k\right). In these expressions, 
    24 \beta^{o}\left(\cdot\right) is the space-limited B-spline basis function of 
     26<ipython-equation>z\left(x,y\right)\approx\sum_{j}\sum_{k}c_{jk}\beta^{o}\left(\frac{x}{\Delta 
     27x}-j\right)\beta^{o}\left(\frac{y}{\Delta y}-k\right).</ipython-equation> In these expressions, 
     28<ipython-inlineequation>\beta^{o}\left(\cdot\right)</ipython-inlineequation> is the space-limited B-spline basis function of 
    2529order, o. The requirement of equally-spaced knot-points and equally-spaced data 
    2630points, allows the development of fast (inverse-filtering) algorithms for 
    27 determining the coefficients, c_{j}, from sample-values, y_{n}. Unlike the 
     31determining the coefficients, 
     32<ipython-inlineequation>c_{j}</ipython-inlineequation>, from sample-values, 
     33<ipython-inlineequation>y_{n}</ipython-inlineequation>. Unlike the 
    2834general spline interpolation algorithms, these algorithms can quickly find the 
    2935spline coefficients for large images.  
     
    3440function can be computed with relative ease from the spline coefficients. For 
    3541example, the second-derivative of a spline is 
    36 y{}^{\prime\prime}\left(x\right)=\frac{1}{\Delta 
    37 x^{2}}\sum_{j}c_{j}\beta^{o\prime\prime}\left(\frac{x}{\Delta x}-j\right). Using 
     42<ipython-equation>y{}^{\prime\prime}\left(x\right)=\frac{1}{\Delta 
     43x^{2}}\sum_{j}c_{j}\beta^{o\prime\prime}\left(\frac{x}{\Delta 
     44x}-j\right).</ipython-equation> Using 
    3845the property of B-splines that 
    39 \frac{d^{2}\beta^{o}\left(w\right)}{dw^{2}}=\beta^{o-2}\left(w+1\right)-2\beta^{o-2}\left(w\right)+\beta^{o-2}\left(w-1\right) 
    40 it can be seen that y^{\prime\prime}\left(x\right)=\frac{1}{\Delta 
     46<ipython-equation>\frac{d^{2}\beta^{o}\left(w\right)}{dw^{2}}=\beta^{o-2}\left(w+1\right)-2\beta^{o-2}\left(w\right)+\beta^{o-2}\left(w-1\right)</ipython-equation>  
     47it can be seen that  
     48<ipython-equation>y^{\prime\prime}\left(x\right)=\frac{1}{\Delta 
    4149x^{2}}\sum_{j}c_{j}\left[\beta^{o-2}\left(\frac{x}{\Delta 
    4250x}-j+1\right)-2\beta^{o-2}\left(\frac{x}{\Delta 
    43 x}-j\right)+\beta^{o-2}\left(\frac{x}{\Delta x}-j-1\right)\right]. If o=3, then 
    44 at the sample points, \Delta 
    45 x^{2}\left.y^{\prime}\left(x\right)\right|_{x=n\Delta x} Thus, the 
     51x}-j\right)+\beta^{o-2}\left(\frac{x}{\Delta x}-j-1\right)\right].  
     52</ipython-equation> 
     53If <ipython-inlineequation>o=3</ipython-inlineequation>, then 
     54at the sample points,  
     55<ipython-equation verb="1">\begin{eqnarray*} 
     56\Delta 
     57x^{2}\left.y^{\prime}\left(x\right)\right|_{x=n\Delta x} &amp; = &amp; 
     58\sum_{j}c_{j}\delta_{n-j+1}-2c 
     59_{j}\delta_{n-j}+c_{j}\delta_{n-j-1},\\ 
     60 &amp; = &amp; 
     61c_{n+1}-2c_{n}+c_{n-1}.\end{eqnarray*}</ipython-equation> Thus, the 
    4662second-derivative signal can be easily calculated from the spline fit. if 
    4763desired, smoothing splines can be found to make the second-derivative less 
     
    6379two-dimensions (signal.qspline1d, signal.qspline2d, signal.cspline1d, 
    6480signal.cspline2d). The package also supplies a function (signal.bspline) for 
    65 evaluating the bspline basis function, \beta^{o}\left(x\right) for arbitrary 
     81evaluating the bspline basis function, 
     82<ipython-inlineequation>\beta^{o}\left(x\right)</ipython-inlineequation> for arbitrary 
    6683order and x. For large o, the B-spline basis function can be approximated well 
    6784by a zero-mean Gaussian function with standard-deviation equal to 
    68 \sigma_{o}=\left(o+1\right)/12:\beta^{o}\left(x\right)\approx\frac{1}{\sqrt{2\pi\sigma_{o}^{2}}}\exp\left(-\frac{x^{2}}{2\sigma_{o}}\right). 
     85<ipython-inlineequation>\sigma_{o}=\left(o+1\right)/12</ipython-inlineequation>: 
     86<ipython-equation>\beta^{o}\left(x\right)\approx\frac{1}{\sqrt{2\pi\sigma_{o}^{2}}}\exp\left(-\frac{x^{2}}{2\sigma_{o}}\right).</ipython-equation>  
    6987A function to compute this Gaussian for arbitrary x and o is also available 
    7088(signal.gauss_spline). The following code and Figure uses spline-filtering to 
     
    111129matrix resulting in another flattened Numeric array. Of course, this is not 
    112130usually the best way to compute the filter as the matrices and vectors involved 
    113 may be huge. For example filtering a 512\times512 image with this method would 
    114 require multiplication of a 512^{2}\times512^{2}matrix with a 512^{2} vector. 
    115 Just trying to store the 512^{2}\times512^{2} matrix using a standard Numeric 
     131may be huge. For example filtering a 
     132<ipython-inlineequation>512\times512</ipython-inlineequation> image with this method would 
     133require multiplication of a 
     134<ipython-inlineequation>512^{2}\times512^{2}matrix</ipython-inlineequation> with 
     135a <ipython-inlineequation>512^{2}</ipython-inlineequation> vector. 
     136Just trying to store the 
     137<ipython-inlineequation>512^{2}\times512^{2}</ipython-inlineequation> matrix using a standard Numeric 
    116138array would require 68,719,476,736 elements. At 4 bytes per element this would 
    117 require 256\textrm{GB} of memory. In most applications most of the elements of 
     139require <ipython-inlineequation>256\textrm{GB}</ipython-inlineequation> of memory. In most applications most of the elements of 
    118140this matrix are zero and a different method for computing the output of the 
    119141filter is employed. 
     
    129151</para> 
    130152<para> 
    131 Let x\left[n\right] define a one-dimensional signal indexed by the integer n. 
     153Let <ipython-inlineequation>x\left[n\right]</ipython-inlineequation> define a one-dimensional signal indexed by the integer n. 
    132154Full convolution of two one-dimensional signals can be expressed as 
    133 y\left[n\right]=\sum_{k=-\infty}^{\infty}x\left[k\right]h\left[n-k\right]. This 
     155<ipython-inlineequation>y\left[n\right]=\sum_{k=-\infty}^{\infty}x\left[k\right]h\left[n-k\right]</ipython-inlineequation>. This 
    134156equation can only be implemented directly if we limit the sequences to finite 
    135 support sequences that can be stored in a computer, choose n=0 to be the 
    136 starting point of both sequences, let K+1 be that value for which 
    137 y\left[n\right]=0 for all n&gt;K+1 and M+1 be that value for which 
    138 x\left[n\right]=0 for all n&gt;M+1, then the discrete convolution expression is 
    139 y\left[n\right]=\sum_{k=\max\left(n-M,0\right)}^{\min\left(n,K\right)}x\left[k\right]h\left[n-k\right]. 
    140 For convenience assume K\geq M. Then, more explicitly the output of this 
    141 operation is y\left[0\right] Thus, the full discrete convolution of two finite 
    142 sequences of lengths K+1 and M+1 respectively results in a finite sequence of 
    143 length K+M+1=\left(K+1\right)+\left(M+1\right)-1.  
     157support sequences that can be stored in a computer, choose 
     158<ipython-inlineequation>n=0</ipython-inlineequation> to be the 
     159starting point of both sequences, let 
     160<ipython-inlineequation>K+1</ipython-inlineequation> be that value for which 
     161<ipython-inlineequation>y\left[n\right]=0</ipython-inlineequation> for all 
     162<ipython-inlineequation>n&gt;K+1</ipython-inlineequation> and 
     163<ipython-inlineequation>M+1</ipython-inlineequation> be that value for which 
     164<ipython-inlineequation>x\left[n\right]=0</ipython-inlineequation> for all 
     165<ipython-inlineequation>n&gt;M+1</ipython-inlineequation>, then the discrete convolution expression is 
     166<ipython-equation>y\left[n\right]=\sum_{k=\max\left(n-M,0\right)}^{\min\left(n,K\right)}x\left[k\right]h\left[n-k\right].</ipython-equation>  
     167For convenience assume <ipython-inlineequation>K\geq M</ipython-inlineequation>. Then, more explicitly the output of this 
     168operation is  
     169<ipython-equation verb="1"> 
     170\begin{eqnarray*} 
     171y\left[0\right] &amp; = &amp; x\left[0\right]h\left[0\right]\\ 
     172y\left[1\right] &amp; = &amp; x\left[0\right]h\left[1\right]+x\left[1\right]h\left[0\right]\\ 
     173y\left[2\right] &amp; = &amp; x\left[0\right]h\left[2\right]+x\left[1\right]h\left[1\right]+x\left[2\right]h 
     174\left[0\right]\\ 
     175\vdots &amp; \vdots &amp; \vdots\\ 
     176y\left[M\right] &amp; = &amp; x\left[0\right]h\left[M\right]+x\left[1\right]h\left[M-1\right]+\cdots+x\left[ 
     177M\right]h\left[0\right]\\ 
     178y\left[M+1\right] &amp; = &amp; x\left[1\right]h\left[M\right]+x\left[2\right]h\left[M-1\right]+\cdots+x\left[M+1\right]h\left[0\right]\\ 
     179\vdots &amp; \vdots &amp; \vdots\\ 
     180y\left[K\right] &amp; = &amp; x\left[K-M\right]h\left[M\right]+\cdots+x\left[K\right]h\left[0\right]\\ 
     181y\left[K+1\right] &amp; = &amp; x\left[K+1-M\right]h\left[M\right]+\cdots+x\left[K\right]h\left[1\right]\\ 
     182\vdots &amp; \vdots &amp; \vdots\\ 
     183y\left[K+M-1\right] &amp; = &amp; x\left[K-1\right]h\left[M\right]+x\left[K\right]h\left[M-1\right]\\ 
     184y\left[K+M\right] &amp; = &amp; x\left[K\right]h\left[M\right].\end{eqnarray*} 
     185</ipython-equation> 
     186 Thus, the full discrete convolution of two finite 
     187sequences of lengths <ipython-inlineequation>K+1</ipython-inlineequation> and 
     188<ipython-inlineequation>M+1</ipython-inlineequation> respectively results in a finite sequence of 
     189length <ipython-inlineequation>K+M+1=\left(K+1\right)+\left(M+1\right)-1</ipython-inlineequation>.  
    144190</para> 
    145191<para> 
     
    149195which part of the output signal to return. The default value of 'full' returns 
    150196the entire signal. If the flag has a value of 'same' then only the middle K 
    151 values are returned starting at y\left[\left\lfloor \frac{M-1}{2}\right\rfloor 
    152 \right] so that the output has the same length as the largest input. If the flag 
     197values are returned starting at <ipython-inlineequation>y\left[\left\lfloor \frac{M-1}{2}\right\rfloor 
     198\right]</ipython-inlineequation> so that the output has the same length as the largest input. If the flag 
    153199has a value of 'valid' then only the middle 
    154 K-M+1=\left(K+1\right)-\left(M+1\right)+1 output values are returned where z 
    155 depends on all of the values of the smallest input from h\left[0\right] to 
    156 h\left[M\right]. In other words only the values y\left[M\right] to 
    157 y\left[K\right] inclusive are returned. 
     200<ipython-inlineequation>K-M+1=\left(K+1\right)-\left(M+1\right)+1</ipython-inlineequation> 
     201output values are returned where 
     202<ipython-inlineequation>z</ipython-inlineequation>  
     203depends on all of the values of the smallest input from 
     204<ipython-inlineequation>h\left[0\right]</ipython-inlineequation> to 
     205<ipython-inlineequation>h\left[M\right]</ipython-inlineequation>. In other words 
     206only the values <ipython-inlineequation>y\left[M\right]</ipython-inlineequation> to 
     207<ipython-inlineequation>y\left[K\right]</ipython-inlineequation> inclusive are returned. 
    158208</para> 
    159209<para>This same function signal.convolve can actually take N-dimensional arrays 
     
    164214Correlation is very similar to convolution except for the minus sign becomes a 
    165215plus sign. Thus 
    166 w\left[n\right]=\sum_{k=-\infty}^{\infty}y\left[k\right]x\left[n+k\right] is the 
    167 (cross) correlation of the signals y and x. For finite-length signals with 
    168 y\left[n\right]=0 outside of the range \left[0,K\right] and x\left[n\right]=0 
    169 outside of the range \left[0,M\right], the summation can simplify to 
    170 w\left[n\right]=\sum_{k=\max\left(0,-n\right)}^{\min\left(K,M-n\right)}y\left[k\right]x\left[n+k\right].Assuming 
    171 again that K\geq M this is w\left[-K\right] 
     216<ipython-equation> 
     217w\left[n\right]=\sum_{k=-\infty}^{\infty}y\left[k\right]x\left[n+k\right] 
     218</ipython-equation> is the 
     219(cross) correlation of the signals 
     220<ipython-inlineequation>y</ipython-inlineequation> and 
     221<ipython-inlineequation>x</ipython-inlineequation>. For finite-length signals with 
     222<ipython-inlineequation>y\left[n\right]=0</ipython-inlineequation> outside of 
     223the range <ipython-inlineequation>\left[0,K\right]</ipython-inlineequation> and 
     224<ipython-inlineequation>x\left[n\right]=0</ipython-inlineequation>  
     225outside of the range <ipython-inlineequation>\left[0,M\right]</ipython-inlineequation>