Changeset 901
- Timestamp:
- 09/23/05 11:13:49 (3 years ago)
- Files:
-
- nbdoc/trunk/COPYING (added)
- nbdoc/trunk/scipy_tutorial/chapter01.pybk (modified) (3 diffs)
- nbdoc/trunk/scipy_tutorial/chapter02.pybk (modified) (9 diffs)
- nbdoc/trunk/scipy_tutorial/chapter04.pybk (modified) (12 diffs)
- nbdoc/trunk/scipy_tutorial/chapter05.pybk (modified) (18 diffs)
- nbdoc/trunk/scipy_tutorial/chapter06.pybk (modified) (6 diffs)
- nbdoc/trunk/scipy_tutorial/chapter07.pybk (modified) (2 diffs)
- nbdoc/trunk/scipy_tutorial/chapter10.pybk (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
nbdoc/trunk/scipy_tutorial/chapter01.pybk
r845 r901 26 26 the command 27 27 </para> 28 <p ara>XXX: ipython28 <programlisting>XXX: ipython 29 29 >>> from scipy import * 30 </p ara>30 </programlisting> 31 31 32 32 <section> … … 51 51 in that module is printed. For example: 52 52 </para> 53 <p ara>XXX: ipython53 <programlisting>XXX: ipython 54 54 >>> info(optimize.fmin) 55 55 fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, … … 88 88 printmessg -- non-zero to print convergence messages. 89 89 90 </p ara>90 </programlisting> 91 91 <para>Another useful command is <code>source</code>. When given a function written in 92 92 Python as an argument, it prints out a listing of the source code for that nbdoc/trunk/scipy_tutorial/chapter02.pybk
r854 r901 31 31 have been modified to return complex numbers instead of NaN's where appropriate. 32 32 </para> 33 <p ara>XXX: ipython34 >>> scipy.sqrt(-1) </p ara>33 <programlisting>XXX: ipython 34 >>> scipy.sqrt(-1) </programlisting> 35 35 </section> 36 36 <section> … … 122 122 its usage: 123 123 </para> 124 <p ara>XXX:124 <programlisting>XXX: 125 125 >>> mgrid[0:5,0:5] 126 126 array([[[0, 0, 0, 0, 0], … … 143 143 [ 0. , 1.6667, 3.3333, 5. ], 144 144 [ 0. , 1.6667, 3.3333, 5. ]]]) 145 </p ara>145 </programlisting> 146 146 <para>Having meshed arrays like this is sometimes very useful. However, it is 147 147 not always needed just to evaluate some N-dimensional function over a grid due … … 179 179 differentiated, and evaluated. It even prints like a polynomial: 180 180 </para> 181 <p ara> XXX: ipython181 <programlisting> XXX: ipython 182 182 >>> p = 183 183 poly1d([3,4,5]) … … 195 195 >>> p([4,5]) 196 196 array([ 69, 100]) 197 </p ara>197 </programlisting> 198 198 <para>The other way to handle polynomials is as an array of coefficients with 199 199 the first element of the array giving the coefficient of the highest power. … … 212 212 have a Python function named addsubtract defined as: 213 213 </para> 214 <p ara>XXX: ipython214 <programlisting>XXX: ipython 215 215 >>> def addsubtract(a,b): 216 216 if a > b: … … 218 218 else: 219 219 return a + b 220 </p ara>220 </programlisting> 221 221 <para>which defines a function of two scalar variables and returns a scalar 222 222 result. The class vectorize can be used to "vectorize" this function so that 223 223 returns a function which takes array arguments and returns an array result: 224 224 </para> 225 <p ara> XXX: ipython225 <programlisting> XXX: ipython 226 226 >>> vec_addsubtract([0,3,6,9],[1,3,5,7]) 227 227 array([1, 6, 1, 2]) 228 </p ara>228 </programlisting> 229 229 <para>This particular function could have been written in vector form without 230 230 the use of vectorize. But, what if the function you have written is the result … … 254 254 corresponding to the first condition in condlist that is true. For example 255 255 </para> 256 <p ara> XXX: ipython256 <programlisting> XXX: ipython 257 257 >>> x = r_[-2:3] 258 258 >>> x … … 260 260 >>> select([x > 3, x >= 0],[0,x+2]) 261 261 array([0, 0, 2, 3, 4]) 262 </p ara>262 </programlisting> 263 263 </section> 264 264 </section> nbdoc/trunk/scipy_tutorial/chapter04.pybk
r853 r901 5 5 module is provided by the help command: 6 6 </para> 7 <p ara>XXX: ipython7 <programlisting>XXX: ipython 8 8 >>> help(integrate) 9 9 Methods for Integrating Functions … … 18 18 See the orthogonal module (integrate.orthogonal) for Gaussian 19 19 quadrature roots and weights. 20 </p ara>20 </programlisting> 21 21 <section> 22 22 <title>General integration (integrate.quad)</title> … … 30 30 This could be computed using quad: 31 31 </para> 32 <p ara>XXX: ipython32 <programlisting>XXX: ipython 33 33 >>> result = integrate.quad(lambda x: special.jv(2.5,x), 0, 4.5) 34 34 >>> print result … … 42 42 >>> print abs(result[0]-I) 43 43 1.03761443881e-11 44 </p ara>44 </programlisting> 45 45 <para>The first argument to quad is a "callable" Python object (i.e a function, 46 46 method, or class instance). Notice the use of a lambda-function in this case as … … 72 72 defining a new function vec_expint based on the routine quad: 73 73 </para> 74 <p ara> XXX: ipython74 <programlisting> XXX: ipython 75 75 >>> from integrate import quad, Inf 76 76 >>> def integrand(t,n,x): … … 86 86 >>> special.expn(3,arange(1.0,4.0,0.5)) 87 87 array([ 0.1097, 0.0567, 0.0301, 0.0163, 0.0089, 0.0049]) 88 </p ara>88 </programlisting> 89 89 <para>The function which is integrated can even use the quad argument (though 90 90 the error bound may underestimate the error due to possible numerical error in … … 94 94 dx=\frac{1}{n}</ipython-equation>. 95 95 </para> 96 <p ara>XXX: ipython96 <programlisting>XXX: ipython 97 97 >>> result = quad(lambda x: expint(3, x), 0, Inf) 98 98 >>> print result … … 105 105 >>> print I3 - result[0] 106 106 8.77306560731e-11 107 </p ara>107 </programlisting> 108 108 <para>This last example shows that multiple integration can be handled using 109 109 repeated calls to quad. The mechanics of this for double and triple integration … … 115 115 <ipython-inlineequation>I_{n}</ipython-inlineequation> is shown below: 116 116 </para> 117 <p ara>XXX: ipython117 <programlisting>XXX: ipython 118 118 >>> from __future__ import nested_scopes 119 119 >>> from integrate import quad, dblquad, Inf … … 127 127 >>> print I(2) 128 128 (0.49999999999857514, 1.8855523253868967e-09) 129 </p ara>129 </programlisting> 130 130 </section> 131 131 <section> … … 237 237 The following example illustrates the use of odeint including the usage of the 238 238 Dfun option which allows the user to specify a gradient (with respect to 239 <ipython-inlineequation>\mathbf{y} )</ipython-inlineequation>of the function,239 <ipython-inlineequation>\mathbf{y}</ipython-inlineequation>) of the function, 240 240 <ipython-inlineequation>\mathbf{f}\left(\mathbf{y},t\right)</ipython-inlineequation>. 241 241 </para> 242 <p ara>XXX: ipython242 <programlisting>XXX: ipython 243 243 >>> from integrate import odeint 244 244 >>> from special import gamma, airy … … 268 268 >>> print y2[:36:6,1] 269 269 [ 0.355028 0.339511 0.324067 0.308763 0.293658 0.278806] 270 </p ara>270 </programlisting> 271 271 272 272 </section> nbdoc/trunk/scipy_tutorial/chapter05.pybk
r845 r901 5 5 pydoc.help): 6 6 </para> 7 <p ara>XXX: ipython7 <programlisting>XXX: ipython 8 8 >>> info(optimize) 9 9 Optimization Tools … … 42 42 43 43 fixed_point -- Single-variable fixed-point solver. 44 </p ara>44 </programlisting> 45 45 <para> 46 46 The first four algorithms are unconstrained minimization algorithms (fmin: … … 69 69 minimum can be found using the fmin routine as shown in the example below: 70 70 </para> 71 <p ara>XXX: ipython71 <programlisting>XXX: ipython 72 72 >>> from scipy.optimize import fmin 73 73 >>> def rosen(x): # The Rosenbrock function … … 83 83 >>> print xopt 84 84 [ 1. 1. 1. 1. 1.] 85 </p ara>85 </programlisting> 86 86 <para>Another optimization algorithm that needs only function calls to find the 87 87 minimum is Powell's method available as optimize.fmin_powell. … … 123 123 gradient is constructed by the code-segment: 124 124 </para> 125 <p ara>XXX: ipython125 <programlisting>XXX: ipython 126 126 >>> def rosen_der(x): 127 127 xm = x[1:-1] … … 133 133 der[-1] = 200*(x[-1]-x[-2]**2) 134 134 return der 135 </p ara>135 </programlisting> 136 136 <para> 137 137 The calling signature for the BFGS minimization algorithm is similar to fmin … … 139 139 in the following example which minimizes the Rosenbrock function. 140 140 </para> 141 <p ara>XXX: ipython141 <programlisting>XXX: ipython 142 142 >>> from scipy.optimize import fmin_bfgs 143 143 … … 151 151 >>> print xopt 152 152 [ 1. 1. 1. 1. 1.] 153 </p ara>153 </programlisting> 154 154 </section> 155 155 … … 217 217 the following example: 218 218 </para> 219 <p ara>XXX: ipython219 <programlisting>XXX: ipython 220 220 >>> from scipy.optimize import fmin_ncg 221 221 >>> def rosen_hess(x): … … 239 239 >>> print xopt 240 240 [ 0.9999 0.9999 0.9998 0.9996 0.9991] 241 </p ara>241 </programlisting> 242 242 </section> 243 243 <section> … … 271 271 fhess_p keyword to minimize the Rosenbrock function using fmin_ncg follows: 272 272 </para> 273 <p ara>XXX: ipython273 <programlisting>XXX: ipython 274 274 >>> from scipy.optimize import fmin_ncg 275 275 >>> def rosen_hess_p(x,p): … … 292 292 >>> print xopt 293 293 [ 1. 1. 1. 0.9999 0.9999] 294 </p ara>294 </programlisting> 295 295 </section> 296 296 </section> … … 350 350 example and a plot of the results is shown in Figure [fig:least_squares_fit]. 351 351 </para> 352 <p ara>XXX: ipython and figure352 <programlisting>XXX: ipython and figure 353 353 >>> x = arange(0,6e-2,6e-2/30) 354 354 >>> A,k,theta = 10, 1.0/3e-2, pi/6 … … 381 381 >>> legend(['Fit', 'Noisy', 'True']) 382 382 >>> gist.eps('leastsqfit') # Make epsi file. 383 </p ara>383 </programlisting> 384 384 </section> 385 385 … … 426 426 <ipython-inlineequation>x_{\textrm{min}}=5.3314</ipython-inlineequation>: 427 427 </para> 428 <p ara> XXX: ipython428 <programlisting> XXX: ipython 429 429 >>> from scipy.special import j1 430 430 … … 433 433 >>> print xmin 434 434 5.33144184241 435 </p ara>435 </programlisting> 436 436 </section> 437 437 </section> … … 456 456 <ipython-inlineequation>x_{0}=6.5041,\, x_{1}=0.9084</ipython-inlineequation>. 457 457 </para> 458 <p ara>XXX: ipython458 <programlisting>XXX: ipython 459 459 >>> def func(x): 460 460 return x + 2*cos(x) … … 473 473 >>> print x02 474 474 [ 6.5041 0.9084] 475 </p ara>475 </programlisting> 476 476 </section> 477 477 <section> nbdoc/trunk/scipy_tutorial/chapter06.pybk
r845 r901 19 19 be specified at instantiation time. The following example demonstrates it's use. 20 20 </para> 21 <p ara> XXX: ipython21 <programlisting> XXX: ipython 22 22 >>> x = arange(0,10) 23 23 >>> y = exp(-x/3.0) … … 38 38 >>> xnew = arange(0,9,0.1) 39 39 >>> xplt.plot(x,y,'x',xnew,f(xnew),'.') 40 </p ara>40 </programlisting> 41 41 <para> 42 42 Figure shows the result: [Graphics file: inter_1d.epsi] … … 96 96 [fig:spline-1d]Examples of using cubic-spline interpolation. 97 97 </para> 98 <p ara> XXX: ipython98 <programlisting> XXX: ipython 99 99 >>> # Cubic-spline 100 100 >>> x = arange(0,2*pi+pi/4,2*pi/8) … … 149 149 >>> xplt.title('Spline of parametrically-defined curve') 150 150 >>> xplt.eps('interp_cubic_param') 151 </p ara>151 </programlisting> 152 152 </section> 153 153 … … 190 190 indexing objects passed in mgrid[]. 191 191 </para> 192 <p ara> XXX: ipython192 <programlisting> XXX: ipython 193 193 >>> # Define function over sparse 20x20 grid 194 194 >>> x,y = mgrid[-1:1:20j,-1:1:20j] … … 205 205 >>> xplt.title3("Interpolated function.") 206 206 >>> xplt.eps("2d_interp") 207 </p ara>207 </programlisting> 208 208 <para> 209 209 [Graphics file: 2d_func.epsi] nbdoc/trunk/scipy_tutorial/chapter07.pybk
r845 r901 95 95 and allows for choosing mirror-symmetric boundary conditions. 96 96 </para> 97 <p ara>XXX: ipython97 <programlisting>XXX: ipython 98 98 >>> image = lena().astype(Float32) 99 99 >>> derfilt = array([1.0,-2,1.0],Float32) … … 112 112 >>> xplt.title('Output of spline edge filter') 113 113 >>> xplt.eps('lena_edge') 114 </p ara>114 </programlisting> 115 115 <para> 116 116 [Graphics file: lena_image.epsi] nbdoc/trunk/scipy_tutorial/chapter10.pybk
r853 r901 50 50 The following example demonstrates this computation in SciPy 51 51 </para> 52 <p ara> XXX: ipython52 <programlisting> XXX: ipython 53 53 >>> A = mat('[1 3 5; 2 5 1; 2 3 8]') 54 54 >>> A … … 64 64 [ 0.56, 0.08, -0.36], 65 65 [ 0.16, -0.12, 0.04]]) 66 </p ara>66 </programlisting> 67 67 </section> 68 68 … … 103 103 answer as shown in the following example: 104 104 </para> 105 <p ara>XXX: ipython105 <programlisting>XXX: ipython 106 106 >>> A = mat('[1 3 5; 2 5 1; 2 3 8]') 107 107 >>> b = mat('[10;8;3]') … … 114 114 [ 5.16], 115 115 [ 0.76]]) 116 </p ara>116 </programlisting> 117 117 </section> 118 118 119 119 <section> 120 120 <title>Finding Determinant</title> 121 <para>The determinant of a square matrix \mathbf{A} is often denoted 122 \left|\mathbf{A}\right| and is a quantity often used in linear algebra. Suppose 123 a_{ij} are the elements of the matrix \mathbf{A} and let 124 M_{ij}=\left|\mathbf{A}_{ij}\right| be the determinant of the matrix left by 125 removing the i^{\textrm{th}} row and j^{\textrm{th}}column from \mathbf{A}. Then 126 for any row i,\left|\mathbf{A}\right|=\sum_{j}\left(-1\right)^{i+j}a_{ij}M_{ij}. 121 <para>The determinant of a square matrix 122 <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> is often denoted 123 <ipython-inlineequation>\left|\mathbf{A}\right|</ipython-inlineequation> and is a quantity often used in linear algebra. Suppose 124 <ipython-inlineequation>a_{ij}</ipython-inlineequation> are the elements of the matrix <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> and let 125 <ipython-inlineequation>M_{ij}=\left|\mathbf{A}_{ij}\right|</ipython-inlineequation> be the determinant of the matrix left by 126 removing the <ipython-inlineequation>i^{\textrm{th}}</ipython-inlineequation> row and <ipython-inlineequation>j^{\textrm{th}}column</ipython-inlineequation> from <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>. Then 127 for any row <ipython-equation>i,\left|\mathbf{A}\right|=\sum_{j}\left(-1\right)^{i+j}a_{ij}M_{ij}</ipython-equation>. 127 128 This is a recursive way to define the determinant where the base case is defined 128 129 by accepting that the determinant of a 1\times1 matrix is the only matrix 129 130 element. In SciPy the determinant can be calculated with linalg.det. For 130 example, the determinant of \mathbf{A=}\left[\begin{array}{ccc} 131 example, the determinant of 132 <ipython-equation>\mathbf{A=}\left[\begin{array}{ccc} 131 133 1 & 3 & 5\\ 132 134 2 & 5 & 1\\ 133 2 & 3 & 8\end{array}\right]is \left|\mathbf{A}\right| In SciPy this is 135 2 & 3 & 8\end{array}\right]</ipython-equation> is 136 <ipython-equation verb="1">\begin{eqnarray*} 137 \left|\mathbf{A}\right| & = & 1\left|\begin{array}{cc} 138 5 & 1\\ 139 3 & 8\end{array}\right|-3\left|\begin{array}{cc} 140 2 & 1\\ 141 2 & 8\end{array}\right|+5\left|\begin{array}{cc} 142 2 & 5\\ 143 2 & 3\end{array}\right|\\ 144 & = & 1\left(5\cdot8-3\cdot1\right)-3\left(2\cdot8-2\cdot1\right)+5\left(2\cdot3-2\cdot5\right)=-25 145 .\end{eqnarray*} 146 </ipython-equation> In SciPy this is 134 147 computed as shown in this example: 135 148 </para> 136 <p ara> XXX: ipython149 <programlisting> XXX: ipython 137 150 >>> A = mat('[1 3 5; 2 5 1; 2 3 8]') 138 151 >>> linalg.det(A) 139 152 -25.000000000000004 140 </p ara>153 </programlisting> 141 154 </section> 142 155 … … 150 163 </para> 151 164 <para> 152 For vector x, the order parameter can be any real number including inf or -inf. The computed norm is \left\Vert \mathbf{x}\right\Vert =\left\{ \begin{array}{cc} 165 For vector x, the order parameter can be any real number including inf or -inf. The computed norm is 166 <ipython-equation>\left\Vert \mathbf{x}\right\Vert =\left\{ \begin{array}{cc} 153 167 \max\left|x_{i}\right| & \textrm{ord}=\textrm{inf}\\ 154 168 \min\left|x_{i}\right| & \textrm{ord}=-\textrm{inf}\\ 155 169 \left(\sum_{i}\left|x_{i}\right|^{\textrm{ord}}\right)^{1/\textrm{ord}} & 156 \left|\textrm{ord}\right|<\infty.\end{array}\right. 170 \left|\textrm{ord}\right|<\infty.\end{array}\right.</ipython-equation> 157 171 </para> 158 172 <para> 159 For matrix \mathbf{A} the only valid values for norm are \pm2,\pm1, \pm inf, and160 'fro' (or 'f') Thus,\left\Vert \mathbf{A}\right\Vert =\left\{ \begin{array}{cc}173 For matrix <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> the only valid values for norm are <ipython-inlineequation>\pm2</ipython-inlineequation>,<ipython-inlineequation>\pm1</ipython-inlineequation>, <ipython-inlineequation>\pm inf</ipython-inlineequation>, and 174 'fro' (or 'f'). Thus, <ipython-equation>\left\Vert \mathbf{A}\right\Vert =\left\{ \begin{array}{cc} 161 175 \max_{i}\sum_{j}\left|a_{ij}\right| & \textrm{ord}=\textrm{inf}\\ 162 176 \min_{i}\sum_{j}\left|a_{ij}\right| & \textrm{ord}=-\textrm{inf}\\ … … 166 180 \min\sigma_{i} & \textrm{ord}=-2\\ 167 181 \sqrt{\textrm{trace}\left(\mathbf{A}^{H}\mathbf{A}\right)} & 168 \textrm{ord}=\textrm{'fro'}\end{array}\right . where \sigma_{i}are the singular169 values of \mathbf{A}.182 \textrm{ord}=\textrm{'fro'}\end{array}\right</ipython-equation> where <ipython-inlineequation>\sigma_{i}</ipython-inlineequation> are the singular 183 values of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>. 170 184 </para> 171 185 </section> … … 175 189 <para>Linear least-squares problems occur in many branches of applied 176 190 mathematics. In this problem a set of linear scaling coefficients is sought that 177 allow a model to fit data. In particular it is assumed that data y_{i} is 178 related to data \mathbf{x}_{i} through a set of coefficients c_{j} and model 179 functions f_{j}\left(\mathbf{x}_{i}\right) via the model 180 y_{i}=\sum_{j}c_{j}f_{j}\left(\mathbf{x}_{i}\right)+\epsilon_{i} where 181 \epsilon_{i} represents uncertainty in the data. The strategy of least squares 182 is to pick the coefficients c_{j} to minimize 183 J\left(\mathbf{c}\right)=\sum_{i}\left|y_{i}-\sum_{j}c_{j}f_{j}\left(x_{i}\right)\right|^{2}. 184 </para> 185 <para>Theoretically, a global minimum will occur when \frac{\partial J}{\partial 186 c_{n}^{*}}=0=\sum_{i}\left(y_{i}-\sum_{j}c_{j}f_{j}\left(x_{i}\right)\right)\left(-f_{n}^{*}\left(x_{i}\right)\right)or 187 \sum_{j}c_{j}\sum_{i}f_{j}\left(x_{i}\right)f_{n}^{*}\left(x_{i}\right) where 188 \left\{ \mathbf{A}\right\} _{ij}=f_{j}\left(x_{i}\right). When \mathbf{A^{H}A} 191 allow a model to fit data. In particular it is assumed that data <ipython-inlineequation>y_{i}</ipython-inlineequation> is 192 related to data <ipython-inlineequation>\mathbf{x}_{i}</ipython-inlineequation> through a set of coefficients <ipython-inlineequation>c_{j}</ipython-inlineequation> and model 193 functions <ipython-inlineequation>f_{j}\left(\mathbf{x}_{i}\right)</ipython-inlineequation> via the model 194 <ipython-equation>y_{i}=\sum_{j}c_{j}f_{j}\left(\mathbf{x}_{i}\right)+\epsilon_{i}</ipython-equation> where 195 <ipython-inlineequation>\epsilon_{i}</ipython-inlineequation> represents uncertainty in the data. The strategy of least squares 196 is to pick the coefficients <ipython-inlineequation>c_{j}</ipython-inlineequation> to minimize 197 <ipython-equation> 198 J\left(\mathbf{c}\right)=\sum_{i}\left|y_{i}-\sum_{j}c_{j}f_{j}\left(x_{i}\right)\right|^{2}.\] 199 </ipython-equation></para> 200 <para>Theoretically, a global minimum will occur when 201 <ipython-equation>\frac{\partial J}{\partial 202 c_{n}^{*}}=0=\sum_{i}\left(y_{i}-\sum_{j}c_{j}f_{j}\left(x_{i}\right)\right)\left(-f_{n}^{*}\left(x_{i}\right)\right)</ipython-equation> or 203 <ipython-equation verb="1">\begin{eqnarray*} 204 \sum_{j}c_{j}\sum_{i}f_{j}\left(x_{i}\right)f_{n}^{*}\left(x_{i}\right) & = & \sum_{i}y_{i}f_{n}^{*} 205 \left(x_{i}\right)\\ 206 \mathbf{A}^{H}\mathbf{Ac} & = & \mathbf{A}^{H}\mathbf{y}\end{eqnarray*} 207 </ipython-equation> where 208 <ipython-equation>\left\{ \mathbf{A}\right\} _{ij}=f_{j}\left(x_{i}\right)</ipython-equation>. When <ipython-inlineequation>\mathbf{A^{H}A}</ipython-inlineequation> 189 209 is invertible, then 190 \mathbf{c}=\left(\mathbf{A}^{H}\mathbf{A}\right)^{-1}\mathbf{A}^{H}\mathbf{y}=\mathbf{A}^{\dagger}\mathbf{y} 191 where \mathbf{A}^{\dagger} is called the pseudo-inverse of \mathbf{A}. Notice192 that using this definition of \mathbf{A}the model can be written193 \mathbf{y}=\mathbf{Ac}+\boldsymbol{\epsilon}.The command linalg.lstsq will solve194 the linear least squares problem for \mathbf{c} given \mathbf{A} and \mathbf{y}.210 <ipython-equation>\mathbf{c}=\left(\mathbf{A}^{H}\mathbf{A}\right)^{-1}\mathbf{A}^{H}\mathbf{y}=\mathbf{A}^{\dagger}\mathbf{y}</ipython-equation> 211 where <ipython-inlineequation>\mathbf{A}^{\dagger}</ipython-inlineequation> is called the pseudo-inverse of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>. Notice 212 that using this definition of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> the model can be written 213 <ipython-equation>\mathbf{y}=\mathbf{Ac}+\boldsymbol{\epsilon}.</ipython-equation> The command linalg.lstsq will solve 214 the linear least squares problem for <ipython-inlineequation>\mathbf{c}</ipython-inlineequation> given <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> and <ipython-inlineequation>\mathbf{y}</ipython-inlineequation>. 195 215 In addition linalg.pinv or linalg.pinv2 (uses a different method based on 196 singular value decomposition) will find \mathbf{A}^{\dagger} given \mathbf{A}.216 singular value decomposition) will find <ipython-inlineequation>\mathbf{A}^{\dagger}</ipython-inlineequation> given <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>. 197 217 </para> 198 218 <para>The following example and figure demonstrate the use of linalg.lstsq and 199 219 linalg.pinv for solving a data-fitting problem. The data shown below were 200 generated using the model: y_{i}=c_{1}e^{-x_{i}}+c_{2}x_{i} where x_{i}=0.1ifor201 i=1\ldots10, c_{1}=5, and c_{2}=4. Noise is added to y_{i}and the coefficients202 c_{1} and c_{2}are estimated using linear least squares.203 </para> 204 <p ara> XXX: ipython220 generated using the model: <ipython-equation>y_{i}=c_{1}e^{-x_{i}}+c_{2}x_{i}</ipython-equation> where <ipython-inlineequation>x_{i}=0.1i</ipython-inlineequation> for 221 <ipython-inlineequation>i=1\ldots10</ipython-inlineequation>, <ipython-inlineequation>c_{1}=5</ipython-inlineequation>, and <ipython-inlineequation>c_{2}=4</ipython-inlineequation>. Noise is added to <ipython-inlineequation>y_{i}</ipython-inlineequation> and the coefficients 222 <ipython-inlineequation>c_{1}</ipython-inlineequation> and <ipython-inlineequation>c_{2}</ipython-inlineequation> are estimated using linear least squares. 223 </para> 224 <programlisting> XXX: ipython 205 225 c1,c2= 5.0,2.0 206 226 i = r_[1:11] … … 220 240 xplt.title('Data fitting with linalg.lstsq') 221 241 xplt.eps('lstsq_fit') 222 </p ara>242 </programlisting> 223 243 <para> 224 244 [Graphics file: lstsq_fit.eps] … … 231 251 or linalg.pinv2. These two commands differ in how they compute the generalized 232 252 inverse. The first uses the linalg.lstsq algorithm while the second uses 233 singular value decomposition. Let \mathbf{A} be an M\times N matrix, then if M>N253 singular value decomposition. Let <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> be an <ipython-inlineequation>M\times N</ipython-inlineequation> matrix, then if <ipython-inlineequation>M>N</ipython-inlineequation> 234 254 the generalized inverse is 235 \mathbf{A}^{\dagger}=\left(\mathbf{A}^{H}\mathbf{A}\right)^{-1}\mathbf{A}^{H} 236 while if M<Nmatrix the generalized inverse is237 \mathbf{A}^{\#}=\mathbf{A}^{H}\left(\mathbf{A}\mathbf{A}^{H}\right)^{-1}.In238 both cases for M=N, then \mathbf{A}^{\dagger}=\mathbf{A}^{\#}=\mathbf{A}^{-1}as239 long as \mathbf{A}is invertible.255 <ipython-equation>\mathbf{A}^{\dagger}=\left(\mathbf{A}^{H}\mathbf{A}\right)^{-1}\mathbf{A}^{H}</ipython-equation> 256 while if <ipython-inlineequation>M<N</ipython-inlineequation> matrix the generalized inverse is 257 <ipython-equation>\mathbf{A}^{\#}=\mathbf{A}^{H}\left(\mathbf{A}\mathbf{A}^{H}\right)^{-1}.</ipython-equation> In 258 both cases for <ipython-inlineequation>M=N</ipython-inlineequation>, then <ipython-equation>\mathbf{A}^{\dagger}=\mathbf{A}^{\#}=\mathbf{A}^{-1}</ipython-equation> as 259 long as <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> is invertible. 240 260 </para> 241 261 </section> … … 252 272 <para>The eigenvalue-eigenvector problem is one of the most commonly 253 273 employed linear algebra operations. In one popular form, the 254 eigenvalue-eigenvector problem is to find for some square matrix \mathbf{A}255 scalars \lambda and corresponding vectors \mathbf{v}such that256 \mathbf{Av}=\lambda\mathbf{v}. For an N\times Nmatrix, there are N (not274 eigenvalue-eigenvector problem is to find for some square matrix <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> 275 scalars <ipython-inlineequation>\lambda</ipython-inlineequation> and corresponding vectors <ipython-inlineequation>\mathbf{v}</ipython-inlineequation> such that 276 <ipython-equation>\mathbf{Av}=\lambda\mathbf{v}.</ipython-equation> For an <ipython-inlineequation>N\times N</ipython-inlineequation> matrix, there are N (not 257 277 necessarily distinct) eigenvalues --- roots of the (characteristic) polynomial 258 \left|\mathbf{A}-\lambda\mathbf{I}\right|=0. 259 </para> 260 <para>The eigenvectors, \mathbf{v}, are also sometimes called right eigenvectors to278 <ipython-equation>\left|\mathbf{A}-\lambda\mathbf{I}\right|=0.</ipython-equation> 279 </para> 280 <para>The eigenvectors, <ipython-inlineequation>\mathbf{v}</ipython-inlineequation>, are also sometimes called right eigenvectors to 261 281 distinguish them from another set of left eigenvectors that satisfy 262 \mathbf{v}_{L}^{H}\mathbf{A}=\lambda\mathbf{v}_{L}^{H}or263 \mathbf{A}^{H}\mathbf{v}_{L}=\lambda^{*}\mathbf{v}_{L}.With it's default264 optional arguments, the command linalg.eig returns \lambda and \mathbf{v}.265 However, it can also return \mathbf{v}_{L} and just \lambdaby itself266 (linalg.eigvals returns just \lambdaas well).282 <ipython-equation>\mathbf{v}_{L}^{H}\mathbf{A}=\lambda\mathbf{v}_{L}^{H}</ipython-equation> or 283 <ipython-equation>\mathbf{A}^{H}\mathbf{v}_{L}=\lambda^{*}\mathbf{v}_{L}.</ipython-equation> With it's default 284 optional arguments, the command linalg.eig returns <ipython-inlineequation>\lambda</ipython-inlineequation> and <ipython-inlineequation>\mathbf{v}</ipython-inlineequation>. 285 However, it can also return <ipython-inlineequation>\mathbf{v}_{L}</ipython-inlineequation> and just <ipython-inlineequation>\lambda</ipython-inlineequation> by itself 286 (linalg.eigvals returns just <ipython-inlineequation>\lambda</ipython-inlineequation> as well). 267 287 </para> 268 288 <para>In addtion, linalg.eig can also solve the more general eigenvalue problem 269 \mathbf{Av}for square matrices \mathbf{A} and \mathbf{B}. The standard 289 <ipython-equation verb="1">\begin{eqnarray*} 290 \mathbf{Av} & = & \lambda\mathbf{Bv}\\ 291 \mathbf{A}^{H}\mathbf{v}_{L} & = & \lambda^{*}\mathbf{B}^{H}\mathbf{v}_{L}\end{eqnarray*}</ipython-equation> 292 for square matrices <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> and <ipython-inlineequation>\mathbf{B}</ipython-inlineequation>. The standard 270 293 eigenvalue problem is an example of the general eigenvalue problem for 271 \mathbf{B}=\mathbf{I}. When a generalized eigenvalue problem can be solved, then272 it provides a decomposition of \mathbf{A}as273 \mathbf{A}=\mathbf{BV}\boldsymbol{\Lambda}\mathbf{V}^{-1} where \mathbf{V}is274 the collection of eigenvectors into columns and \boldsymbol{\Lambda}is a294 <ipython-inlineequation>\mathbf{B}=\mathbf{I}</ipython-inlineequation>. When a generalized eigenvalue problem can be solved, then 295 it provides a decomposition of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> as 296 <ipython-equation>\mathbf{A}=\mathbf{BV}\boldsymbol{\Lambda}\mathbf{V}^{-1}</ipython-equation> where <ipython-inlineequation>\mathbf{V}</ipython-inlineequation> is 297 the collection of eigenvectors into columns and <ipython-inlineequation>\boldsymbol{\Lambda}</ipython-inlineequation> is a 275 298 diagonal matrix of eigenvalues. 276 299 </para> 277 300 <para>By definition, eigenvectors are only defined up to a constant scale factor. In 278 SciPy, the scaling factor for the eigenvectors is chosen so that \left\Vert279 \mathbf{v}\right\Vert ^{2}=\sum_{i}v_{i}^{2}=1 .301 SciPy, the scaling factor for the eigenvectors is chosen so that <ipython-inlineequation>\left\Vert 302 \mathbf{v}\right\Vert ^{2}=\sum_{i}v_{i}^{2}=1</ipython-inlineequation>. 280 303 </para> 281 304 <para>As an example, consider finding the eigenvalues and eigenvectors of the matrix 282 \mathbf{A}=\left[\begin{array}{ccc}305 <ipython-equation>\mathbf{A}=\left[\begin{array}{ccc} 283 306 1 & 5 & 2\\ 284 307 2 & 4 & 1\\ 285 3 & 6 & 2\end{array}\right]. The characteristic polynomial is 286 \left|\mathbf{A}-\lambda\mathbf{I}\right| The roots of this polynomial are the 287 eigenvalues of \mathbf{A}: \lambda_{1} The eigenvectors corresponding to each 308 3 & 6 & 2\end{array}\right].</ipython-equation> The characteristic polynomial is 309 <ipython-equation verb="1"> 310 \begin{eqnarray*} 311 \left|\mathbf{A}-\lambda\mathbf{I}\right| & = & \left(1-\lambda\right)\left[\left(4-\lambda\right)\left(2-\lambda\right)-6\right]-\\ 312 & & 5\left[2\left(2-\lambda\right)-3\right]+2\left[12-3\left(4-\lambda\right)\right]\\ 313 & = & -\lambda^{3}+7\lambda^{2}+8\lambda-3.\end{eqnarray*} 314 </ipython-equation> 315 The roots of this polynomial are the 316 eigenvalues of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>: 317 <ipython-equation verb="1"> 318 \begin{eqnarray*} 319 \lambda_{1} & = & 7.9579\\ 320 \lambda_{2} & = & -1.2577\\ 321 \lambda_{3} & = & 0.2997.\end{eqnarray*} 322 </ipython-equation> 323 The eigenvectors corresponding to each 288 324 eigenvalue can be found using the original equation. The eigenvectors associated 289 325 with these eigenvalues can then be found. 290 326 </para> 291 <p ara>XXX: ipython327 <programlisting>XXX: ipython 292 328 >>> A = mat('[1 5 2; 2 4 1; 3 6 2]') 293 329 >>> la,v = linalg.eig(A) … … 308 344 >>> print max(ravel(abs(A*v1-l1*v1))) 309 345 4.4408920985e-16 310 </p ara>346 </programlisting> 311 347 </section> 312 348 … … 314 350 <title>Singular value decomposition</title> 315 351 <para>Singular Value Decompostion (SVD) can be thought of as an extension of the 316 eigenvalue problem to matrices that are not square. Let \mathbf{A} be an M\times317 N matrix with M and N arbitrary. The matrices \mathbf{A}^{H}\mathbf{A}and318 \mathbf{A}\mathbf{A}^{H}are square hermitian matrices<footnote><para>A hermition matrix319 \mathbf{D} satisfies \mathbf{D}^{H}=\mathbf{D}.</para></footnote> of size N\times N and M\times M 352 eigenvalue problem to matrices that are not square. Let <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> be an 353 <ipython-inlineequation>M\times N</ipython-inlineequation> matrix with <ipython-inlineequation>M</ipython-inlineequation> and <ipython-inlineequation>N</ipython-inlineequation> arbitrary. The matrices <ipython-inlineequation>\mathbf{A}^{H}\mathbf{A}</ipython-inlineequation> and 354 <ipython-inlineequation>\mathbf{A}\mathbf{A}^{H}</ipython-inlineequation> are square hermitian matrices<footnote><para>A hermition matrix 355 <ipython-inlineequation>\mathbf{D}</ipython-inlineequation> satisfies <ipython-inlineequation>\mathbf{D}^{H}=\mathbf{D}</ipython-inlineequation>.</para></footnote> of size <ipython-inlineequation>N\times N</ipython-inlineequation> and <ipython-inlineequation>M\times M</ipython-inlineequation> 320 356 respectively. It is known that the eigenvalues of square hermitian matrices are 321 real and non-negative. In addtion, there are at most \min\left(M,N\right)322 identical non-zero eigenvalues of \mathbf{A}^{H}\mathbf{A}and323 \mathbf{A}\mathbf{A}^{H}. Define these positive eigenvalues as \sigma_{i}^{2}.324 The square-root of these are called singular values of \mathbf{A}. The325 eigenvectors of \mathbf{A}^{H}\mathbf{A}are collected by columns into an326 N\times N unitary<footnote><para>A unitary matrix \mathbf{D}satisfies327 \mathbf{D}^{H}\mathbf{D}=\mathbf{I}=\mathbf{D}\mathbf{D}^{H}so that328 \mathbf{D}^{-1}=\mathbf{D}^{H}.</para></footnote> matrix \mathbf{V}while the eigenvectors of329 \mathbf{A}\mathbf{A}^{H}are collected by columns in the unitary matrix330 \mathbf{U}, the singular values are collected in an M\times Nzero matrix331 \mathbf{\boldsymbol{\Sigma}}with main diagonal entries set to the singular332 values. Then \mathbf{A=U}\boldsymbol{\Sigma}\mathbf{V}^{H}is the singular-value333 decomposition of \mathbf{A}. Every matrix has a singular value decomposition.334 Sometimes, the singular values are called the spectrum of \mathbf{A}. The335 command linalg.svd will return \mathbf{U}, \mathbf{V}^{H}, and \sigma_{i}as an336 array of the singular values. To obtain the matrix \mathbf{\Sigma}use357 real and non-negative. In addtion, there are at most <ipython-inlineequation>\min\left(M,N\right)</ipython-inlineequation> 358 identical non-zero eigenvalues of <ipython-inlineequation>\mathbf{A}^{H}\mathbf{A}</ipython-inlineequation> and 359 <ipython-inlineequation>\mathbf{A}\mathbf{A}^{H}</ipython-inlineequation>. Define these positive eigenvalues as <ipython-inlineequation>\sigma_{i}^{2}</ipython-inlineequation>. 360 The square-root of these are called singular values of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>. The 361 eigenvectors of <ipython-inlineequation>\mathbf{A}^{H}\mathbf{A}</ipython-inlineequation> are collected by columns into an 362 <ipython-inlineequation>N\times N</ipython-inlineequation> unitary<footnote><para>A unitary matrix <ipython-inlineequation>\mathbf{D}</ipython-inlineequation> satisfies 363 <ipython-inlineequation>\mathbf{D}^{H}\mathbf{D}=\mathbf{I}=\mathbf{D}\mathbf{D}^{H}</ipython-inlineequation> so that 364 <ipython-inlineequation>\mathbf{D}^{-1}=\mathbf{D}^{H}</ipython-inlineequation>.</para></footnote> matrix <ipython-inlineequation>\mathbf{V}</ipython-inlineequation> while the eigenvectors of 365 <ipython-inlineequation>\mathbf{A}\mathbf{A}^{H}</ipython-inlineequation> are collected by columns in the unitary matrix 366 <ipython-inlineequation>\mathbf{U}</ipython-inlineequation>, the singular values are collected in an <ipython-inlineequation>M\times N</ipython-inlineequation> zero matrix 367 <ipython-inlineequation>\mathbf{\boldsymbol{\Sigma}}</ipython-inlineequation> with main diagonal entries set to the singular 368 values. Then <ipython-inlineequation>\mathbf{A=U}\boldsymbol{\Sigma}\mathbf{V}^{H}</ipython-inlineequation> is the singular-value 369 decomposition of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>. Every matrix has a singular value decomposition. 370 Sometimes, the singular values are called the spectrum of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>. The 371 command linalg.svd will return <ipython-inlineequation>\mathbf{U}</ipython-inlineequation>, <ipython-inlineequation>\mathbf{V}^{H}</ipython-inlineequation>, and <ipython-inlineequation>\sigma_{i}</ipython-inlineequation> as an 372 array of the singular values. To obtain the matrix <ipython-inlineequation>\mathbf{\Sigma}</ipython-inlineequation> use 337 373 linalg.diagsvd. The following example illustrates the use of linalg.svd. 338 374 </para> 339 <p ara>XXX: ipython375 <programlisting>XXX: ipython 340 376 >>> A = mat('[1 3 2; 1 2 3]') 341 377 >>> M,N = A.shape … … 360 396 Matrix([[ 1., 3., 2.], 361 397 [ 1., 2., 3.]]) 362 </p ara>398 </programlisting> 363 399 </section> 364 400 365 401 <section> 366 402 <title>LU decomposition</title> 367 <para>The LU decompostion finds a representation for the M\times Nmatrix368 \mathbf{A} as \mathbf{A}=\mathbf{PLU} where \mathbf{P} is an M\times M 403 <para>The LU decompostion finds a representation for the <ipython-inlineequation>M\times N</ipython-inlineequation> matrix 404 <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> as <ipython-inlineequation>\mathbf{A}=\mathbf{PLU}</ipython-inlineequation> where <ipython-inlineequation>\mathbf{P}</ipython-inlineequation> is an <ipython-inlineequation>M\times M</ipython-inlineequation> 369 405 permutation matrix (a permutation of the rows of the identity matrix), 370 \mathbf{L} is in M\times Klower triangular or trapezoidal matrix371 ( K=\min\left(M,N\right)) with unit-diagonal, and \mathbf{U}is an upper406 <ipython-inlineequation>\mathbf{L}</ipython-inlineequation> is in <ipython-inlineequation>M\times K</ipython-inlineequation> lower triangular or trapezoidal matrix 407 (<ipython-inlineequation>K=\min\left(M,N\right)</ipython-inlineequation>) with unit-diagonal, and <ipython-inlineequation>\mathbf{U}</ipython-inlineequation> is an upper 372 408 triangular or trapezoidal matrix. The SciPy command for this decomposition is 373 409 linalg.lu. … … 376 412 Such a decomposition is often useful for solving many simultaneous equations 377 413 where the left-hand-side does not change but the right hand side does. For 378 example, suppose we are going to solve \mathbf{A}\mathbf{x}_{i}=\mathbf{b}_{i}379 for many different \mathbf{b}_{i}. The LU decomposition allows this to be380 written as \mathbf{PLUx}_{i}=\mathbf{b}_{i}. Because \mathbf{L}is381 lower-triangular, the equation can be solved for \mathbf{U}\mathbf{x}_{i}and382 finally \mathbf{x}_{i}very rapidly using forward- and back-substitution. An383 initial time spent factoring \mathbf{A}allows for very rapid solution of414 example, suppose we are going to solve <ipython-inlineequation>\mathbf{A}\mathbf{x}_{i}=\mathbf{b}_{i}</ipython-inlineequation> 415 for many different <ipython-inlineequation>\mathbf{b}_{i}</ipython-inlineequation>. The LU decomposition allows this to be 416 written as <ipython-inlineequation>\mathbf{PLUx}_{i}=\mathbf{b}_{i}</ipython-inlineequation>. Because <ipython-inlineequation>\mathbf{L}</ipython-inlineequation> is 417 lower-triangular, the equation can be solved for <ipython-inlineequation>\mathbf{U}\mathbf{x}_{i}</ipython-inlineequation> and 418 finally <ipython-inlineequation>\mathbf{x}_{i}</ipython-inlineequation> very rapidly using forward- and back-substitution. An 419 initial time spent factoring <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> allows for very rapid solution of 384 420 similar systems of equations in the future. If the intent for performing LU 385 421 decomposition is for solving linear systems then the command linalg.lu_factor … … 393 429 <para>Cholesky decomposition is a special case of LU decomposition 394 430 applicable to Hermitian positive definite matrices. When 395 \mathbf{A}=\mathbf{A}^{H} and \mathbf{x}^{H}\mathbf{Ax}\geq0 for all \mathbf{x}, 396 then decompositions of \mathbf{A} can be found so that \mathbf{A}where 397 \mathbf{L} is lower-triangular and \mathbf{U} is upper triangular. Notice that 398 \mathbf{L}=\mathbf{U}^{H}. The command linagl.cholesky computes the cholesky 431 <ipython-inlineequation>\mathbf{A}=\mathbf{A}^{H}</ipython-inlineequation> and <ipython-inlineequation>\mathbf{x}^{H}\mathbf{Ax}\geq0</ipython-inlineequation> for all <ipython-inlineequation>\mathbf{x}</ipython-inlineequation>, 432 then decompositions of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> can be found so that 433 <ipython-equation verb="1"> 434 \begin{eqnarray*} 435 \mathbf{A} & = & \mathbf{U}^{H}\mathbf{U}\\ 436 \mathbf{A} & = & \mathbf{L}\mathbf{L}^{H}\end{eqnarray*} 437 </ipython-equation> 438 where 439 <ipython-inlineequation>\mathbf{L}</ipython-inlineequation> is lower-triangular and <ipython-inlineequation>\mathbf{U}</ipython-inlineequation> is upper triangular. Notice that 440 <ipython-inlineequation>\mathbf{L}=\mathbf{U}^{H}</ipython-inlineequation>. The command linagl.cholesky computes the cholesky 399 441 factorization. For using cholesky factorization to solve systems of equations 400 442 there are also linalg.cho_factor and linalg.cho_solve routines that work … … 406 448 <title>QR decomposition</title> 407 449 <para>The QR decomposition (sometimes called a polar decomposition) works for 408 any M\times N array and finds an M\times M unitary matrix \mathbf{Q}and an409 M\times N upper-trapezoidal matrix \mathbf{R} such that \mathbf{A=QR}. Notice410 that if the SVD of \mathbf{A}is known then the QR decomposition can be found411 \mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^{H}=\mathbf{QR}implies that412 \mathbf{Q}=\mathbf{U} and \mathbf{R}=\boldsymbol{\Sigma}\mathbf{V}^{H}. Note,450 any <ipython-inlineequation>M\times N</ipython-inlineequation> array and finds an <ipython-inlineequation>M\times M</ipython-inlineequation> unitary matrix <ipython-inlineequation>\mathbf{Q}</ipython-inlineequation> and an 451 <ipython-inlineequation>M\times N</ipython-inlineequation> upper-trapezoidal matrix <ipython-inlineequation>\mathbf{R}</ipython-inlineequation> such that <ipython-inlineequation>\mathbf{A=QR}</ipython-inlineequation>. Notice 452 that if the SVD of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> is known then the QR decomposition can be found 453 <ipython-equation>\mathbf{A}=\mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^{H}=\mathbf{QR}</ipython-equation> implies that 454 <ipython-inlineequation>\mathbf{Q}=\mathbf{U}</ipython-inlineequation> and <ipython-inlineequation>\mathbf{R}=\boldsymbol{\Sigma}\mathbf{V}^{H}</ipython-inlineequation>. Note, 413 455 however, that in SciPy independent algorithms are used to find QR and SVD 414 456 decompositions. The command for QR decomposition is linalg.qr. … … 418 460 <section> 419 461 <title>Schur decomposition</title> 420 <para>For a square N\times N matrix, \mathbf{A}, the Schur decomposition finds421 (not-necessarily unique) matrices \mathbf{T} and \mathbf{Z}such that422 \mathbf{A}=\mathbf{ZT}\mathbf{Z}^{H} where \mathbf{Z}is a unitary matrix and423 \mathbf{T}is either upper-triangular or quasi-upper triangular depending on462 <para>For a square <ipython-inlineequation>N\times N</ipython-inlineequation> matrix, <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>, the Schur decomposition finds 463 (not-necessarily unique) matrices <ipython-inlineequation>\mathbf{T}</ipython-inlineequation> and <ipython-inlineequation>\mathbf{Z}</ipython-inlineequation> such that 464 <ipython-equation>\mathbf{A}=\mathbf{ZT}\mathbf{Z}^{H}</ipython-equation> where <ipython-inlineequation>\mathbf{Z}</ipython-inlineequation> is a unitary matrix and 465 <ipython-inlineequation>\mathbf{T}</ipython-inlineequation> is either upper-triangular or quasi-upper triangular depending on 424 466 whether or not a real schur form or complex schur form is requested. For a real 425 schur form both \mathbf{T} and \mathbf{Z} are real-valued when \mathbf{A}is426 real-valued. When \mathbf{A}is a real-valued matrix the real schur form is only427 quasi-upper triangular because 2\times2blocks extrude from the main diagonal467 schur form both <ipython-inlineequation>\mathbf{T}</ipython-inlineequation> and <ipython-inlineequation>\mathbf{Z}</ipython-inlineequation> are real-valued when <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> is 468 real-valued. When <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> is a real-valued matrix the real schur form is only 469 quasi-upper triangular because <ipython-inlineequation>2\times2</ipython-inlineequation> blocks extrude from the main diagonal 428 470 corresponding to any complex-valued eigenvalues. The command linalg.schur finds 429 the Schur decomposition while the command linalg.rsf2csf converts \mathbf{T}and430 \mathbf{Z}from a real Schur form to a complex Schur form. The Schur form is471 the Schur decomposition while the command linalg.rsf2csf converts <ipython-inlineequation>\mathbf{T}</ipython-inlineequation> and 472 <ipython-inlineequation>\mathbf{Z}</ipython-inlineequation> from a real Schur form to a complex Schur form. The Schur form is 431 473 especially useful in calculating functions of matrices. 432 474 </para> 433 475 <para>The following example illustrates the schur decomposition: 434 476 </para> 435 <p ara>XXX: ipython477 <programlisting>XXX: ipython 436 478 >>> A = mat('[1 3 2; 1 4 5; 2 3 6]') 437 479 >>> T,Z = linalg.schur(A) … … 467 509 [ 0., 0., 0.], 468 510 [ 0., 0., 0.]]) 469 </p ara>511 </programlisting> 470 512 </section> 471 513 </section> … … 473 515 <section> 474 516 <title>Matrix Functions</title> 475 <para>Consider the function f\left(x\right)with Taylor series expansion476 f\left(x\right)=\sum_{k=0}^{\infty}\frac{f^{\left(k\right)}\left(0\right)}{k!}x^{k}. 517 <para>Consider the function <ipython-inlineequation>f\left(x\right)</ipython-inlineequation> with Taylor series expansion 518 <ipython-equation>f\left(x\right)=\sum_{k=0}^{\infty}\frac{f^{\left(k\right)}\left(0\right)}{k!}x^{k}.</ipython-equation> 477 519 A matrix function can be defined using this Taylor series for the square matrix 478 \mathbf{A}as479 f\left(\mathbf{A}\right)=\sum_{k=0}^{\infty}\frac{f^{\left(k\right)}\left(0\right)}{k!}\mathbf{A}^{k}. 520 <ipython-inlineequation>\mathbf{A}</ipython-inlineequation> as 521 <ipython-equation>f\left(\mathbf{A}\right)=\sum_{k=0}^{\infty}\frac{f^{\left(k\right)}\left(0\right)}{k!}\mathbf{A}^{k}.</ipython-equation> 480 522 While, this serves as a useful representation of a matrix function, it is rarely 481 523 the best way to calculate a matrix function. … … 486 528 <para>The matrix exponential is one of the more common matrix functions. It can 487 529 be defined for square matrices as 488 e^{\mathbf{A}}=\sum_{k=0}^{\infty}\frac{1}{k!}\mathbf{A}^{k}.The command530 <ipython-equation>e^{\mathbf{A}}=\sum_{k=0}^{\infty}\frac{1}{k!}\mathbf{A}^{k}.</ipython-equation> The command 489 531 linalg.expm3 uses this Taylor series definition to compute the matrix 490 532 exponential. Due to poor convergence properties it is not often used. 491 533 </para> 492 534 <para>Another method to compute the matrix exponential is to find an eigenvalue 493 decomposition of \mathbf{A}:494 \mathbf{A}=\mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1}and note that495 e^{\mathbf{A}}=\mathbf{V}e^{\boldsymbol{\Lambda}}\mathbf{V}^{-1}where the496 matrix exponential of the diagonal matrix \boldsymbol{\Lambda}is just the535 decomposition of <ipython-inlineequation>\mathbf{A}</ipython-inlineequation>: 536 <ipython-equation>\mathbf{A}=\mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1}</ipython-equation> and note that 537 <ipython-equation>e^{\mathbf{A}}=\mathbf{V}e^{\boldsymbol{\Lambda}}\mathbf{V}^{-1}</ipython-equation> where the 538 matrix exponential of the diagonal matrix <ipython-inlineequation>\boldsymbol{\Lambda}</ipython-inlineequation> is just the 497 539 exponential of its elements. This method is implemented in linalg.expm2. 498 540 </para> 499 541 <para>The preferred method for implementing the matrix exponential is to use scaling 500 and a Padé approximation for e^{x}. This algorithm is implemented as542 and a Padé approximation for <ipython-inlineequation&g
