Changeset 845
- Timestamp:
- 09/15/05 07:38:12 (3 years ago)
- Files:
-
- nbdoc/trunk/notabene/docbook.py (modified) (6 diffs)
- nbdoc/trunk/notabene/notebook.py (modified) (2 diffs)
- nbdoc/trunk/notabene/styles.py (modified) (1 diff)
- nbdoc/trunk/scipy_tutorial/chapter01.pybk (modified) (2 diffs)
- nbdoc/trunk/scipy_tutorial/chapter02.pybk (modified) (3 diffs)
- nbdoc/trunk/scipy_tutorial/chapter03.pybk (modified) (1 diff)
- nbdoc/trunk/scipy_tutorial/chapter04.pybk (modified) (8 diffs)
- nbdoc/trunk/scipy_tutorial/chapter05.pybk (modified) (13 diffs)
- nbdoc/trunk/scipy_tutorial/chapter06.pybk (modified) (5 diffs)
- nbdoc/trunk/scipy_tutorial/chapter07.pybk (modified) (14 diffs)
- nbdoc/trunk/scipy_tutorial/chapter10.pybk (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
nbdoc/trunk/notabene/docbook.py
r833 r845 6 6 from cgi import escape 7 7 8 from lxml import etree as ET 9 8 10 from notabene.formatter import Formatter 9 10 from lxml import etree as ET11 12 11 from PyFontify import fontify 13 12 … … 92 91 93 92 def transform_figure(self, fig): 94 #logid = elem.xpath("../../@id")[0]95 ET.dump(fig)96 93 number = fig.get('number') 97 94 dbfig = ET.Element('figure', label="Fig%s"%(number)) … … 109 106 strong.tail = caption.strip() 110 107 dbfig.tail = fig.tail 111 ET.dump(dbfig)112 108 return dbfig 113 109 114 110 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 125 140 return dbeq 126 141 … … 182 197 transform_elements('block', self.transform_block) 183 198 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] = img193 194 199 transform_elements('equation', self.transform_equation) 200 transform_elements('inlineequation', self.transform_inlineequation) 195 201 196 202 sheet2.tag = nodetype 197 198 203 return sheet2 199 204 … … 224 229 225 230 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 """ 226 235 if style is None: 227 236 from notabene.styles import LightBGStyle as style … … 231 240 getattr(self, 'prep_%s' % kind)(article_tree) 232 241 newtree = xslt.apply(article_tree) 233 return newtree242 return xslt.tostring(newtree) nbdoc/trunk/notabene/notebook.py
r835 r845 344 344 tmpf = os.fdopen(tmpfid, 'w+b') 345 345 try: 346 doc.write(tmpf)346 tmpf.write(doc) 347 347 finally: 348 348 tmpf.close() … … 376 376 377 377 else: 378 doc.write(filename, 'utf-8') 378 f = open(filename, 'wb') 379 f.write(doc) 380 f.close() 379 381 return filename 380 382 nbdoc/trunk/notabene/styles.py
r827 r845 139 139 ET.SubElement(sheet, xsl['import'], 140 140 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") 141 145 142 146 # turn on fancyvrb nbdoc/trunk/scipy_tutorial/chapter01.pybk
r844 r845 26 26 the command 27 27 </para> 28 <para>XXX: ipython</para> 28 <para>XXX: ipython 29 >>> from scipy import * 30 </para> 29 31 30 32 <section> … … 49 51 in that module is printed. For example: 50 52 </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 58 Minimize a function using the simplex algorithm. 59 60 Description: 61 62 Uses a Nelder-Mead simplex algorithm to find the minimum of function 63 of one or more variables. 64 65 Inputs: 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 72 Outputs: (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 81 Additional 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> 52 91 <para>Another useful command is <code>source</code>. When given a function written in 53 92 Python as an argument, it prints out a listing of the source code for that nbdoc/trunk/scipy_tutorial/chapter02.pybk
r844 r845 31 31 have been modified to return complex numbers instead of NaN's where appropriate. 32 32 </para> 33 <para>XXX: scipy.sqrt(-1) </para> 33 <para>XXX: ipython 34 >>> scipy.sqrt(-1) </para> 34 35 </section> 35 36 36 37 <section> 37 38 <title>scipy_base routines</title> 38 <para> The purpose of scipy_base is to collect general-purpose routines39 <para>The purpose of scipy_base is to collect general-purpose routines 39 40 that the other sub-packages can use and to provide a simple replacement for 40 41 Numeric. Anytime you might think to import Numeric, you can import scipy_base … … 265 266 functions that might have been placed in scipy_base except for their dependence 266 267 on 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> 268 compute <ipython-inlineequation>n!</ipython-inlineequation> 269 and <ipython-inlineequation>n!/k!(n-k)!</ipython-inlineequation> 275 270 using either exact integer arithmetic (thanks to 276 271 Python's Long integer object), or by using floating-point precision and the … … 291 286 automatically evaluate the object at the correct points to obtain an N-point 292 287 approximation 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 289 given point. 296 290 </para> 297 291 </section> nbdoc/trunk/scipy_tutorial/chapter03.pybk
r844 r845 10 10 rules as other math functions in Numerical Python. Many of these functions also 11 11 accept complex-numbers as input. For a complete list of the available functions 12 with a one-line description type >>>info(special). Each function also has it's 12 with a one-line description type <code>>>>info(special)</code>. Each 13 function also has it's 13 14 own documentation accessible using help. If you don't see a function you need, 14 15 consider writing it and contributing it to the library. You can write the nbdoc/trunk/scipy_tutorial/chapter04.pybk
r844 r845 22 22 <title>General integration (integrate.quad)</title> 23 23 <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 24 between two points. The points can be 25 <ipython-inlineequation>\pm\infty</ipython-inlineequation> 26 (<ipython-inlineequation>\pm</ipython-inlineequation> integrate.inf) to indicate 25 27 infinite 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>. 30 This could be computed using quad: 28 31 </para> 29 32 <para>XXX: ipython … … 46 49 integral and the second element holding an upper bound on the error. Notice, 47 50 that in this case, the true value of this integral is 51 <ipython-equation> 48 52 I=\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> 49 54 where 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>. 51 58 is 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 59 within <ipython-inlineequation>1.04\times10^{-11}</ipython-inlineequation> of 60 the exact result --- well below the reported error 53 61 bound. 54 62 </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 56 65 inf) as one of the arguments. For example, suppose that a numerical value for 57 66 the exponential 58 integral:E_{n}\left(x\right)=\int_{1}^{\infty}\frac{e^{-xt}}{t^{n}}\, dt. is 67 integral: 68 <ipython-equation>E_{n}\left(x\right)=\int_{1}^{\infty}\frac{e^{-xt}}{t^{n}}\, dt 69 </ipython-equation>. is 59 70 desired (and the fact that this integral can be computed as special.expn(n,x) is 60 71 forgotten). The functionality of the function special.expn can be replicated by … … 79 90 the error bound may underestimate the error due to possible numerical error in 80 91 the integrand from the use of quad). The integral in this case is 92 <ipython-equation> 81 93 I_{n}=\int_{0}^{\infty}\int_{1}^{\infty}\frac{e^{-xt}}{t^{n}}\, dt\, 82 dx=\frac{1}{n} .94 dx=\frac{1}{n}</ipython-equation>. 83 95 </para> 84 96 <para>XXX: ipython … … 100 112 arguments are defined in the correct order. In addition, the limits on all inner 101 113 integrals 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: 114 using double integration to compute several values of 115 <ipython-inlineequation>I_{n}<ipython-inlineequation> is shown below: 103 116 </para> 104 117 <para>XXX: ipython … … 139 152 </para> 140 153 <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 142 156 high-precision estimates of the integral using the available samples. Romberg 143 157 integration uses the trapezoid rule at step-sizes related by a power of two and … … 153 167 conditions is another useful example. The function odeint is available in SciPy 154 168 for 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 169 equation: 170 <ipython-equation>\frac{d\mathbf{y}}{dt}=\mathbf{f}\left(\mathbf{y},t\right),</ipython-equation> 171 given 172 initial 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 158 179 higher-order ordinary differential equation can always be reduced to a 159 180 differential equation of this type by introducing intermediate derivatives into 160 the \mathbf{y}vector.181 the <ipython-inlineequation>\mathbf{y}</ipython-inlineequation> vector. 161 182 </para> 162 183 <para> 163 184 For 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 185 second-order differential equation: 186 <ipython-equation>\frac{d^{2}w}{dz^{2}}-zw(z)=0</ipython-equation> with initial 165 187 conditions 166 w\left(0\right)=\frac{1}{\sqrt[3]{3^{2}}\Gamma\left(\frac{2}{3}\right)}and167 \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>. 168 190 It 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 means191 conditions is the Airy function <ipython-equation>w=\textrm{Ai}\left(z\right)</ipython-equation>, which gives a means 170 192 to check the integrator using special.airy. 171 193 </para> 172 194 <para> 173 195 First, 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> 197 and <ipython-inlineequation>t=z</ipython-inlineequation>. Thus, the differential equation 198 becomes 199 <ipython-equation>\frac{d\mathbf{y}}{dt}=\left[\begin{array}{c} 176 200 ty_{1}\\ 177 201 y_{0}\end{array}\right]=\left[\begin{array}{cc} … … 181 205 y_{1}\end{array}\right]=\left[\begin{array}{cc} 182 206 0 & t\\ 183 1 & 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, 207 1 & 0\end{array}\right]\mathbf{y}.</ipython-equation> 208 In 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> 213 As 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)\, 216 d\tau</ipython-inlineequation> under matrix multiplication, 189 217 then this linear differential equation has an exact solution using the matrix 190 218 exponential: 219 <ipython-equation> 191 220 \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> 222 However, in this case, 223 <ipython-inlineequation>\mathbf{A}\left(t\right)</ipython-inlineequation> and its integral do not commute. 193 224 </para> 194 225 <para> … … 206 237 The following example illustrates the use of odeint including the usage of the 207 238 Dfun 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>. 209 241 </para> 210 242 <para>XXX: ipython nbdoc/trunk/scipy_tutorial/chapter05.pybk
r844 r845 61 61 demonstrate the minimization function consider the problem of minimizing the 62 62 Rosenbrock 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 63 variables: 64 <ipython-equation> 65 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}. 66 </ipython-equation> 67 The minimum value of this function is 0 which is achieved when 68 <ipython-inlineequation>x_{i}=1</ipython-inlineequation>. This 65 69 minimum can be found using the fmin routine as shown in the example below: 66 70 </para> … … 96 100 <para> 97 101 To 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 102 gradient 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 & = & 108 200\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> 111 This expression is valid for the interior derivatives. Special cases 112 are 113 <ipython-equation verb="1"> 114 \begin{eqnarray*} 115 \frac{\partial f}{\partial x_{0}} & = & 116 -400x_{0}\left(x_{1}-x_{0}^{2}\right)-2\left(1-x_{0}\right),\\ 117 \frac{\partial 118 f}{\partial x_{N-1}} & = & 119 200\left(x_{N-1}-x_{N-2}^{2}\right). 120 \end{eqnarray*} 121 </ipython-equation> 122 A Python function which computes this 101 123 gradient is constructed by the code-segment: 102 124 </para> … … 139 161 algorithm to (approximately) invert the local Hessian. Newton's method is based 140 162 on fitting the function locally to a quadratic 141 form:f\left(\mathbf{x}\right)\approx f\left(\mathbf{x}_{0}\right)+\nabla 163 form: 164 <ipython-equation>f\left(\mathbf{x}\right)\approx f\left(\mathbf{x}_{0}\right)+\nabla 142 165 f\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> 167 where 168 <ipython-inlineequation>\mathbf{H}\left(\mathbf{x}_{0}\right)</ipython-inlineequation> is a matrix of second-derivatives 144 169 (the Hessian). If the Hessian is positive definite then the local minimum of 145 170 this function can be found by setting the gradient of the quadratic form to 146 171 zero, 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 148 175 the Hessian is evaluted using the conjugate-gradient method. An example of 149 176 employing this method to minimizing the Rosenbrock function is given below. To … … 158 185 <title>Full Hessian example:</title> 159 186 <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*} 188 H_{ij}=\frac{\partial^{2}f}{\partial x_{i}\partial x_{j}} & = & 189 200\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 & = & 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> 197 matrix. Other non-zero entries of the matrix are 198 <ipython-equation verb="1">\begin{eqnarray*} 199 \frac{\partial^{2}f}{\partial x_{0}^{2}} & = & 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 } & = & -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}} & = & -400x_{N-2},\\ 204 \frac{\partial^{2}f}{\partial x_{N-1}^{2}} & = & 200. 205 \end{eqnarray*}</ipython-equation> 206 For example, the Hessian when N=5 is 207 <ipython-equation> 164 208 \mathbf{H}=\left[\begin{array}{ccccc} 165 209 1200x_{0}^{2}-400x_{1}+2 & -400x_{0} & 0 & 0 & 0\\ … … 167 211 0 & -400x_{1} & 202+1200x_{2}^{2}-400x_{3} & -400x_{2} & 0\\ 168 212 0 & & -400x_{2} & 202+1200x_{3}^{2}-400x_{4} & -400x_{3}\\ 169 0 & 0 & 0 & -400x_{3} & 200\end{array}\right]. The code which computes this 213 0 & 0 & 0 & -400x_{3} & 200\end{array}\right]. 214 </ipython-equation> 215 The code which computes this 170 216 Hessian along with the code to minimize the function using fmin_ncg is shown in 171 217 the following example: … … 210 256 <para> 211 257 In 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: 258 not 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> 261 has elements: 262 <ipython-equation> 214 263 \mathbf{H}\left(\mathbf{x}\right)\mathbf{p}=\left[\begin{array}{c} 215 264 \left(1200x_{0}^{2}-400x_{1}+2\right)p_{0}-400x_{0}p_{1}\\ … … 217 266 -400x_{i-1}p_{i-1}+\left(202+1200x_{i}^{2}-400x_{i+1}\right)p_{i}-400x_{i}p_{i+1}\\ 218 267 \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> 270 Code which makes use of the 220 271 fhess_p keyword to minimize the Rosenbrock function using fmin_ncg follows: 221 272 </para> … … 249 300 <para>All of the previously-explained minimization procedures can be used to 250 301 solve 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 302 constructed. 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> 306 where <ipython-inlineequation>\mathbf{p}</ipython-inlineequation> is a 254 307 vector of parameters for the model that need to be found. A common method for 255 308 determining which parameter vector gives the best fit to the data is to minimize 256 309 the sum of squares of the residuals. The residual is usually defined for each 257 310 observed data-point as 311 <ipython-equation> 258 312 e_{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 260 316 objective function to pass to any of the previous minization algorithms to 261 317 obtain a least-squares fit is. 318 <ipython-equation> 262 319 J\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 residuals320 </ipython-equation> 321 The leastsq algorithm performs this squaring and summing of the residuals 265 322 automatically. 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> 324 and returns the value of 325 <ipython-inlineequation>\mathbf{p}</ipython-inlineequation> which 326 minimizes 327 <ipython-inlineequation>J\left(\mathbf{p}\right)=\mathbf{e}^{T}\mathbf{e}</ipython-inlineequation> 328 directly. The user 268 329 is also encouraged to provide the Jacobian matrix of the function (with 269 330 derivatives down the columns or across the rows). If the Jacobian is not … … 271 332 </para> 272 333 <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 334 data follow a sinusoidal pattern 335 <ipython-equation>y_{i}=A\sin\left(2\pi kx_{i}+\theta\right) 336 </ipython-equation> 337 where 338 the 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> 342 e_{i}=\left|y_{i}-A\sin\left(2\pi kx_{i}+\theta\right)\right|. 343 </ipython-equation> 344 By defining a 276 345 function to compute the residuals and (selecting an appropriate starting 277 346 position), 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 347 parameters 348 <ipython-inlineequation>\hat{A},\,\hat{k},\,\hat{\theta}</ipython-inlineequation>. 349 This is shown in the following 279 350 example and a plot of the results is shown in Figure [fig:least_squares_fit]. 280 351 </para> … … 327 398 rarely be used. The brent method uses Brent's algorithm for locating a minimum. 328 399 Optimally 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)>f\left(b\right)<f\left(c\right) and a <b <c. If this is not given, 400 bracket is a triple 401 <ipython-inlineequation>\left(a,b,c\right)</ipython-inlineequation> such that 402 <ipython-inlineequation>f\left(a\right)>f\left(b\right)<f\left(c\right) 403 </ipython-inlineequation>and <ipython-inlineequation>a <b <c</ipython-inlineequation>. If this is not given, 331 404 then alternatively two starting points can be chosen and a bracket will be found 332 405 from these points using a simple marching algorithm. If these two starting … … 346 419 </para> 347 420 <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: 421 For 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 424 be 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>: 351 427 </para> 352 428 <para> XXX: ipython … … 368 444 root of a set of non-linear equations, the command optimize.fsolve is needed. 369 445 For 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. 446 transcendental equation 447 <ipython-equation> 448 x+2\cos\left(x\right)=0, 449 </ipython-equation> 450 and the set of non-linear 451 equations 452 <ipython-equation> 453 x_{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>. 373 457 </para> 374 458 <para>XXX: ipython … … 405 489 problem of finding a fixed-point of a function. A fixed point of a function is 406 490 the 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 492 fixed 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>. 494 Equivalently, the root of <ipython-inlineequation>f</ipython-inlineequation> is the 495 fixed_point of 496 <ipython-inlineequation>g\left(x\right)=f\left(x\right)+x</ipython-inlineequation>. The routine fixed_point 410 497 provides a simple iterative method using Aitkens sequence acceleration to 411 498 estimate the fixed point of g given a starting point. nbdoc/trunk/scipy_tutorial/chapter06.pybk
r844 r845 53 53 directly and parametrically. The direct method finds the spline representation 54 54 of 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. 55 first two arguments are the only ones required, and these provide the 56 <ipython-inlineequation>x</ipython-inlineequation> and 57 <ipython-inlineequation>y</ipython-inlineequation> 58 components of the curve. The normal output is a 3-tuple, 59 <ipython-inlineequation>\left(t,c,k\right)</ipython-inlineequation>, 60 containing the knot-points, <ipython-inlineequation>t</ipython-inlineequation>, 61 the coefficients <ipython-inlineequation>c</ipython-inlineequation> and the 62 order <ipython-inlineequation>k</ipython-inlineequation> of the spline. 58 63 The default spline order is cubic, but this can be changed with the input 59 keyword, k.64 keyword, <ipython-inlineequation>k</ipython-inlineequation>. 60 65 </para> 61 66 <para>For curves in N-dimensional space the function interpolate.splprep allows … … 66 71 variable is given with the keword argument, u, which defaults to an 67 72 equally-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 73 of two objects: a 3-tuple, 74 <ipython-inlineequation>\left(t,c,k\right)</ipython-inlineequation>, containing the spline 69 75 representation and the parameter variable u. 70 76 </para> 71 77 <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 78 perform during the spline fit. The default value of s is 79 <ipython-inlineequation>s=m-\sqrt{2m}</ipython-inlineequation> where m 73 80 is the number of data-points being fit. Therefore, if no smoothing is desired a 74 value of \mathbf{s}=0should be passed to the routines.81 value of <ipython-inlineequation>\mathbf{s}=0</ipython-inlineequation> should be passed to the routines. 75 82 </para> 76 83 <para>Once the spline representation of the data has been determined, functions … … 78 85 (interpolate.splev, interpolate.splade) at any point and the integral of the 79 86 spline 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 estimated87 splines (<ipython-inlineequation>k=3</ipython-inlineequation>) with 8 or more knots, the roots of the spline can be estimated 81 88 (interpolate.sproot). These functions are demonstrated in the example that 82 89 follows (see also Figure [fig:spline-1d]). … … 150 157 interpolate.bisplrep is available. This function takes as required inputs the 151 158 1-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 160 default output is a list 161 <ipython-inlineequation>\left[tx,ty,c,kx,ky\right]</ipython-inlineequation> 153 162 whose entries represent respectively, the components of the knot positions, the 154 163 coefficients of the spline, and the order of the spline in each coordinate. It … … 156 165 passed easily to the function interpolate.bisplev. The keyword, s, can be used 157 166 to 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 167 appropriate spline. The default value is 168 <ipython-inlineequation>s=m-\sqrt{2m}</ipython-inlineequation> where m is the number of 159 169 data points in the x, y, and z vectors. As a result, if no smoothing is desired, 160 then s=0should be passed to interpolate.bisplrep.170 then <ipython-inlineequation>s=0</ipython-inlineequation> should be passed to interpolate.bisplrep. 161 171 </para> 162 172 <para>To evaluate the two-dimensional spline and it's partial derivatives (up to nbdoc/trunk/scipy_tutorial/chapter07.pybk
r844 r845 15 15 <para>A B-spline is an approximation of a continuous function over a 16 16 finite-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 17 knot-points are equally spaced with spacing <ipython-inlineequation>\Delta 18 x</ipython-inlineequation>, then the B-spline 18 19 approximation 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 21 x}-j\right).</ipython-equation> 22 In two dimensions with knot-spacing <ipython-inlineequation>\Delta 23 x</ipython-inlineequation> and <ipython-inlineequation>\Delta 24 y</ipython-inlineequation>, the function 21 25 representation is 22 z\left(x,y\right)\approx\sum_{j}\sum_{k}c_{jk}\beta^{o}\left(\frac{x}{\Delta23 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 of26 <ipython-equation>z\left(x,y\right)\approx\sum_{j}\sum_{k}c_{jk}\beta^{o}\left(\frac{x}{\Delta 27 x}-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 25 29 order, o. The requirement of equally-spaced knot-points and equally-spaced data 26 30 points, allows the development of fast (inverse-filtering) algorithms for 27 determining the coefficients, c_{j}, from sample-values, y_{n}. Unlike the 31 determining the coefficients, 32 <ipython-inlineequation>c_{j}</ipython-inlineequation>, from sample-values, 33 <ipython-inlineequation>y_{n}</ipython-inlineequation>. Unlike the 28 34 general spline interpolation algorithms, these algorithms can quickly find the 29 35 spline coefficients for large images. … … 34 40 function can be computed with relative ease from the spline coefficients. For 35 41 example, 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 43 x^{2}}\sum_{j}c_{j}\beta^{o\prime\prime}\left(\frac{x}{\Delta 44 x}-j\right).</ipython-equation> Using 38 45 the 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> 47 it can be seen that 48 <ipython-equation>y^{\prime\prime}\left(x\right)=\frac{1}{\Delta 41 49 x^{2}}\sum_{j}c_{j}\left[\beta^{o-2}\left(\frac{x}{\Delta 42 50 x}-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 51 x}-j\right)+\beta^{o-2}\left(\frac{x}{\Delta x}-j-1\right)\right]. 52 </ipython-equation> 53 If <ipython-inlineequation>o=3</ipython-inlineequation>, then 54 at the sample points, 55 <ipython-equation verb="1">\begin{eqnarray*} 56 \Delta 57 x^{2}\left.y^{\prime}\left(x\right)\right|_{x=n\Delta x} & = & 58 \sum_{j}c_{j}\delta_{n-j+1}-2c 59 _{j}\delta_{n-j}+c_{j}\delta_{n-j-1},\\ 60 & = & 61 c_{n+1}-2c_{n}+c_{n-1}.\end{eqnarray*}</ipython-equation> Thus, the 46 62 second-derivative signal can be easily calculated from the spline fit. if 47 63 desired, smoothing splines can be found to make the second-derivative less … … 63 79 two-dimensions (signal.qspline1d, signal.qspline2d, signal.cspline1d, 64 80 signal.cspline2d). The package also supplies a function (signal.bspline) for 65 evaluating the bspline basis function, \beta^{o}\left(x\right) for arbitrary 81 evaluating the bspline basis function, 82 <ipython-inlineequation>\beta^{o}\left(x\right)</ipython-inlineequation> for arbitrary 66 83 order and x. For large o, the B-spline basis function can be approximated well 67 84 by 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> 69 87 A function to compute this Gaussian for arbitrary x and o is also available 70 88 (signal.gauss_spline). The following code and Figure uses spline-filtering to … … 111 129 matrix resulting in another flattened Numeric array. Of course, this is not 112 130 usually 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 131 may be huge. For example filtering a 132 <ipython-inlineequation>512\times512</ipython-inlineequation> image with this method would 133 require multiplication of a 134 <ipython-inlineequation>512^{2}\times512^{2}matrix</ipython-inlineequation> with 135 a <ipython-inlineequation>512^{2}</ipython-inlineequation> vector. 136 Just trying to store the 137 <ipython-inlineequation>512^{2}\times512^{2}</ipython-inlineequation> matrix using a standard Numeric 116 138 array 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 of139 require <ipython-inlineequation>256\textrm{GB}</ipython-inlineequation> of memory. In most applications most of the elements of 118 140 this matrix are zero and a different method for computing the output of the 119 141 filter is employed. … … 129 151 </para> 130 152 <para> 131 Let x\left[n\right]define a one-dimensional signal indexed by the integer n.153 Let <ipython-inlineequation>x\left[n\right]</ipython-inlineequation> define a one-dimensional signal indexed by the integer n. 132 154 Full 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]. This155 <ipython-inlineequation>y\left[n\right]=\sum_{k=-\infty}^{\infty}x\left[k\right]h\left[n-k\right]</ipython-inlineequation>. This 134 156 equation 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>K+1 and M+1 be that value for which 138 x\left[n\right]=0 for all n>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. 157 support sequences that can be stored in a computer, choose 158 <ipython-inlineequation>n=0</ipython-inlineequation> to be the 159 starting 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>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>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> 167 For convenience assume <ipython-inlineequation>K\geq M</ipython-inlineequation>. Then, more explicitly the output of this 168 operation is 169 <ipython-equation verb="1"> 170 \begin{eqnarray*} 171 y\left[0\right] & = & x\left[0\right]h\left[0\right]\\ 172 y\left[1\right] & = & x\left[0\right]h\left[1\right]+x\left[1\right]h\left[0\right]\\ 173 y\left[2\right] & = & 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 & \vdots & \vdots\\ 176 y\left[M\right] & = & x\left[0\right]h\left[M\right]+x\left[1\right]h\left[M-1\right]+\cdots+x\left[ 177 M\right]h\left[0\right]\\ 178 y\left[M+1\right] & = & 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 & \vdots & \vdots\\ 180 y\left[K\right] & = & x\left[K-M\right]h\left[M\right]+\cdots+x\left[K\right]h\left[0\right]\\ 181 y\left[K+1\right] & = & x\left[K+1-M\right]h\left[M\right]+\cdots+x\left[K\right]h\left[1\right]\\ 182 \vdots & \vdots & \vdots\\ 183 y\left[K+M-1\right] & = & x\left[K-1\right]h\left[M\right]+x\left[K\right]h\left[M-1\right]\\ 184 y\left[K+M\right] & = & x\left[K\right]h\left[M\right].\end{eqnarray*} 185 </ipython-equation> 186 Thus, the full discrete convolution of two finite 187 sequences of lengths <ipython-inlineequation>K+1</ipython-inlineequation> and 188 <ipython-inlineequation>M+1</ipython-inlineequation> respectively results in a finite sequence of 189 length <ipython-inlineequation>K+M+1=\left(K+1\right)+\left(M+1\right)-1</ipython-inlineequation>. 144 190 </para> 145 191 <para> … … 149 195 which part of the output signal to return. The default value of 'full' returns 150 196 the 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\rfloor152 \right] so that the output has the same length as the largest input. If the flag197 values 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 153 199 has 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> 201 output values are returned where 202 <ipython-inlineequation>z</ipython-inlineequation> 203 depends 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 206 only the values <ipython-inlineequation>y\left[M\right]</ipython-inlineequation> to 207 <ipython-inlineequation>y\left[K\right]</ipython-inlineequation> inclusive are returned. 158 208 </para> 159 209 <para>This same function signal.convolve can actually take N-dimensional arrays … … 164 214 Correlation is very similar to convolution except for the minus sign becomes a 165 215 plus 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> 217 w\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 223 the range <ipython-inlineequation>\left[0,K\right]</ipython-inlineequation> and 224 <ipython-inlineequation>x\left[n\right]=0</ipython-inlineequation> 225 outside of the range <ipython-inlineequation>\left[0,M\right]</ipython-inlineequation>
