| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
% |
| 3 |
% This is a helper package with an elementary (full) matrix datastructure, |
| 4 |
% featuring O(1) index access and O(N) creation, deletion, copy. |
| 5 |
% |
| 6 |
% The following macros are supplied: |
| 7 |
% |
| 8 |
% \pgfplotsmatrixnewempty |
| 9 |
% \pgfplotsmatrixresize |
| 10 |
% \pgfplotsmatrixsize |
| 11 |
% \pgfplotsmatrixselect |
| 12 |
% \pgfplotsmatrixset |
| 13 |
% \pgfplotsmatrixletentry |
| 14 |
% \pgfplotsmatrixforeach |
| 15 |
% \pgfplotsmatrixLUdecomp |
| 16 |
% \pgfplotsmatrixLUsolve |
| 17 |
% |
| 18 |
% |
| 19 |
% Copyright 2007/2008 by Christian Feuersänger. |
| 20 |
% |
| 21 |
% This program is free software: you can redistribute it and/or modify |
| 22 |
% it under the terms of the GNU General Public License as published by |
| 23 |
% the Free Software Foundation, either version 3 of the License, or |
| 24 |
% (at your option) any later version. |
| 25 |
% |
| 26 |
% This program is distributed in the hope that it will be useful, |
| 27 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 |
% GNU General Public License for more details. |
| 30 |
% |
| 31 |
% You should have received a copy of the GNU General Public License |
| 32 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 33 |
% |
| 34 |
% |
| 35 |
% |
| 36 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 37 |
|
| 38 |
% Creates a new, empty matrix. |
| 39 |
\def\pgfplotsmatrixnewempty#1{% |
| 40 |
\pgfplotsarray@@def{#1@rows}{0}% |
| 41 |
\pgfplotsarray@@def{#1@cols}{1}% |
| 42 |
} |
| 43 |
|
| 44 |
% resizes (truncates) matrix #1 to #2 rows and #3 cols. |
| 45 |
% |
| 46 |
% the elements won't be initialised. Use 'set' for each element. |
| 47 |
\def\pgfplotsmatrixresize#1#2#3{% |
| 48 |
\pgfplotsarray@@edef{#1@rows}{#2}% |
| 49 |
\pgfplotsarray@@edef{#1@cols}{#3}% |
| 50 |
} |
| 51 |
|
| 52 |
% Invokes code '#2' if the matrix named '#1' exists and '#3' if it does |
| 53 |
% not exist. |
| 54 |
\def\pgfplotsmatrixifdefined#1#2#3{% |
| 55 |
\pgfutil@ifundefined{#1@rows}{#3}{#2}% |
| 56 |
}% |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
% Counts the number of rows/cols in matrix #1, storing it into the count |
| 61 |
% registers #2, #3. |
| 62 |
% Example: |
| 63 |
% \pgfplotsmatrixsize\foo\to{\count0}{\count1}% |
| 64 |
% \the\count0, \the\count1 |
| 65 |
\long\def\pgfplotsmatrixsize#1\to#2#3{% |
| 66 |
#2=\csname\string#1@rows\endcsname\relax |
| 67 |
#3=\csname\string#1@cols\endcsname\relax |
| 68 |
} |
| 69 |
|
| 70 |
\long\def\pgfplotsmatrixsizetomacro#1\to#2#3{% |
| 71 |
\expandafter\let\expandafter#2\csname\string#1@rows\endcsname |
| 72 |
\expandafter\let\expandafter#3\csname\string#1@cols\endcsname |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
% Returns the (#1,#2) element of matrix #3 into macro #4 |
| 77 |
% Arguments: |
| 78 |
% #1: a row index 0,...,N-1 where N is rowcount. |
| 79 |
% #1 must expand to an integer. |
| 80 |
% #2: a col index 0,...,N-1 where N is colcount. |
| 81 |
% #2 must expand to an integer. |
| 82 |
% #3: a matrix |
| 83 |
% #4: a macro name |
| 84 |
% Example: |
| 85 |
% Element 0: |
| 86 |
% \pgfplotsmatrixselect0,1\of\foo\to\elem |
| 87 |
% \elem |
| 88 |
% Element \count1: |
| 89 |
% \pgfplotsmatrixselect\count1,2\of\foo\to\elem |
| 90 |
\def\pgfplotsmatrixselect#1,#2\of#3\to#4{% |
| 91 |
\expandafter\let\expandafter#4\csname\string#3@#1,#2\endcsname% |
| 92 |
\ifx#4\relax |
| 93 |
\pgfplotsthrow{no such element}{#1,#2}{No such element: \string\pgfplotsmatrixselect#1,#2\string\of{\string#3}}\pgfeov% |
| 94 |
\fi |
| 95 |
} |
| 96 |
|
| 97 |
% Expands to the value (#1,#2) of matrix #3. |
| 98 |
% #1: a row index (not a register) |
| 99 |
% #2: a col index |
| 100 |
% #3: a matrix |
| 101 |
\def\pgfplotsmatrixvalueofelem#1,#2\of#3{\csname\string#3@#1,#2\endcsname}% |
| 102 |
|
| 103 |
% Sets element '#1,#2' of matrix '#3' to '#4'. |
| 104 |
\def\pgfplotsmatrixset#1,#2\of#3\to#4{% |
| 105 |
\pgfutil@namedef{\string#3@#1,#2}{#4}% |
| 106 |
} |
| 107 |
\long\def\pgfplotsmatrixletentry#1,#2\of#3=#4{% |
| 108 |
\expandafter\let\csname\string#3@#1,#2\endcsname=#4\relax |
| 109 |
} |
| 110 |
|
| 111 |
% During the loop, \pgfplotsmatrixforeachrowindex expands to the |
| 112 |
% current row index and \pgfplotsmatrixforeachcolindex to the actual |
| 113 |
% col index. It invokes \pgfplotsmatrixforeachrowend after each |
| 114 |
% complete row. |
| 115 |
% |
| 116 |
% This macro uses |
| 117 |
% \c@pgf@counta,\c@pgf@countb,\c@pgf@countc,\c@pgf@countd |
| 118 |
\long\def\pgfplotsmatrixforeach#1\as#2#3{% |
| 119 |
\pgfplotsmatrixsize#1\to\c@pgf@countc\c@pgf@countd |
| 120 |
\long\def\pgfplotsmatrixforeach@{#3}% |
| 121 |
\def\pgfplotsmatrixforeach@assign##1{\def#2{##1}}% |
| 122 |
\def\pgfplotsmat@select##1,##2\to{\pgfplotsmatrixselect##1,##2\of#1\to}% |
| 123 |
\def\pgfplotsmatrixforeachrowindex{\the\c@pgf@counta}% |
| 124 |
\def\pgfplotsmatrixforeachcolindex{\the\c@pgf@countb}% |
| 125 |
\c@pgf@counta=0 |
| 126 |
\pgfplotsmatrixforeach@loop |
| 127 |
\let\pgfplotsmatrixforeachcolindex\relax |
| 128 |
\let\pgfplotsmatrixforeachrowindex\relax% |
| 129 |
}% |
| 130 |
\let\pgfplotsmatrixforeachrowend=\relax |
| 131 |
\def\pgfplotsmatrixforeach@loop{% |
| 132 |
\ifnum\c@pgf@counta<\c@pgf@countc |
| 133 |
\c@pgf@countb=0 |
| 134 |
\pgfplotsmatrixforeach@loop@ |
| 135 |
% |
| 136 |
\pgfplotsmatrixforeachrowend |
| 137 |
% |
| 138 |
\advance\c@pgf@counta by1 |
| 139 |
\expandafter\pgfplotsmatrixforeach@loop |
| 140 |
\fi |
| 141 |
}% |
| 142 |
\def\pgfplotsmatrixforeach@loop@{% |
| 143 |
\ifnum\c@pgf@countb<\c@pgf@countd |
| 144 |
\pgfplotsmat@select\the\c@pgf@counta,\the\c@pgf@countb\to\pgfplotsmat@Aij |
| 145 |
\expandafter\pgfplotsmatrixforeach@assign\expandafter{\pgfplotsmat@Aij}% |
| 146 |
\pgfplotsmatrixforeach@ |
| 147 |
% |
| 148 |
\advance\c@pgf@countb by1 |
| 149 |
\expandafter\pgfplotsmatrixforeach@loop@ |
| 150 |
\fi |
| 151 |
}% |
| 152 |
|
| 153 |
% Defines \pgfplotsretval to be a text-representation of the matrix. |
| 154 |
% It will contain '\n' as newline macro and \t to separate cells. |
| 155 |
% |
| 156 |
\def\pgfplotsmatrixtotext#1{% |
| 157 |
\begingroup |
| 158 |
\pgfplotsapplistXnewempty\pgfplotsretval@ |
| 159 |
\def\pgfplotsmatrixforeachrowend{% |
| 160 |
\pgfplotsapplistXpushback\n\to\pgfplotsretval@ |
| 161 |
}% |
| 162 |
\pgfplotsmatrixforeach#1\as\entry{% |
| 163 |
\pgfmathfloatparsenumber\entry |
| 164 |
\pgfmathfloattosci\pgfmathresult |
| 165 |
\edef\entry{\pgfmathresult\noexpand\t}% |
| 166 |
\expandafter\pgfplotsapplistXpushback\entry\to\pgfplotsretval@ |
| 167 |
}% |
| 168 |
\pgfplotsapplistXlet\pgfplotsretval=\pgfplotsretval@ |
| 169 |
\pgfmath@smuggleone\pgfplotsretval |
| 170 |
\endgroup |
| 171 |
} |
| 172 |
|
| 173 |
% Takes a matrix #1 and replaces it by its LU decomposition. |
| 174 |
% The LU decomposition uses implicit pivoting; the pivoting |
| 175 |
% information is stored in a permutation array #2 and a sign macro #3. |
| 176 |
% |
| 177 |
% It is to be used together with \pgfplotsmatrixsolveLEQS. |
| 178 |
% |
| 179 |
% #1: the input matrix (square size) |
| 180 |
% #2: a macro name; will be used to store the permutation array for |
| 181 |
% the pivoting. |
| 182 |
% #3: a macro name, will contain the sign of the permutation (either |
| 183 |
% +1 or -1). |
| 184 |
% |
| 185 |
% The algorithm has been converted from Numerical Recipes in C (I did |
| 186 |
% not copy the comments, though). You find the complete reference in |
| 187 |
% Chapter 2 of Numerical Recipes. |
| 188 |
% |
| 189 |
% If the matrix is singular, an exception will be raised. |
| 190 |
% If the matrix is singular up to working precision, |
| 191 |
% \pgfplotsmatrixLUdecompwarnsingular will be invoked and the |
| 192 |
% algorithm continues with a small threshold. |
| 193 |
% |
| 194 |
% All arithmetics is computed with \pgfplotscoordmath{default} (which |
| 195 |
% is float in the initial configuration). Use |
| 196 |
% \pgfplotssetcoordmathfor{default}{pgfbasic} to switch it to standard |
| 197 |
% pgf arithmetics. |
| 198 |
% |
| 199 |
% ATTENTION. This routine re-uses the four counters |
| 200 |
% \c@pgf@counta,...\c@pgf@countd. |
| 201 |
% Furthermore, it does not free any memory. |
| 202 |
% Make sure you use it inside of local scopes. |
| 203 |
\def\pgfplotsmatrixLUdecomp#1\perm#2\sign#3{% |
| 204 |
\let\pgfplotsmat@i=\c@pgf@counta |
| 205 |
\let\pgfplotsmat@imax=\c@pgf@countb |
| 206 |
\let\pgfplotsmat@j=\c@pgf@countc |
| 207 |
\let\pgfplotsmat@k=\c@pgf@countd |
| 208 |
\countdef\pgfplotsmat@n=0 |
| 209 |
\let\pgfplotsmat@big=\pgfutil@empty |
| 210 |
\let\pgfplotsmat@dum=\pgfutil@empty |
| 211 |
\let\pgfplotsmat@sum=\pgfutil@empty |
| 212 |
\let\pgfplotsmat@temp=\pgfutil@empty |
| 213 |
\pgfplotsmatrixsize#1\to\pgfplotsmat@n\c@pgf@countd |
| 214 |
\ifnum\c@pgf@countd=\pgfplotsmat@n |
| 215 |
\else |
| 216 |
\pgfplots@error{Sorry, \string\pgfplotsmatrixLUdecomp\space expected an n x n matrix, but got \the\pgfplotsmat@n\space x \the\c@pgf@countd.}% |
| 217 |
\fi |
| 218 |
\pgfplotsarraynewempty\pgfplotsmat@vv |
| 219 |
\pgfplotsarrayresize\pgfplotsmat@vv\pgfplotsmat@n |
| 220 |
\pgfplotsarrayresize#2\pgfplotsmat@n |
| 221 |
\def\pgfplotsmat@parity{1}% |
| 222 |
\def\pgfplotsmat@select##1,##2\to{\pgfplotsmatrixselect##1,##2\of#1\to}% |
| 223 |
\def\pgfplotsmat@letentry##1,##2={\pgfplotsmatrixletentry##1,##2\of#1=}% |
| 224 |
\def\pgfplotsmat@letpermentry##1={\pgfplotsarrayletentry##1\of#2=}% |
| 225 |
% |
| 226 |
\pgfplotsmat@i=0 |
| 227 |
\pgfplotsmatrixLUdecomp@scalingloop |
| 228 |
% |
| 229 |
\ifnum\pgfplotsmat@n>0 % this is used for error recovery. |
| 230 |
% |
| 231 |
\pgfplotsmat@j=0 |
| 232 |
\pgfplotsmatrixLUdecomp@mainloop@j |
| 233 |
% |
| 234 |
\let#3=\pgfplotsmat@parity |
| 235 |
\fi |
| 236 |
}% |
| 237 |
\def\pgfplotsmatrixLUdecomp@scalingloop{% |
| 238 |
\ifnum\pgfplotsmat@i<\pgfplotsmat@n |
| 239 |
\pgfplotscoordmath{default}{zero}% |
| 240 |
\let\pgfplotsmat@big=\pgfmathresult |
| 241 |
% |
| 242 |
\pgfplotsmat@j=0 |
| 243 |
\pgfplotsmatrixLUdecomp@scalingloop@ |
| 244 |
% |
| 245 |
\pgfplotscoordmath{default}{if is}{\pgfplotsmat@big}{0}{% |
| 246 |
\pgfplotsthrow{invalid argument}{\pgfplots@loc@TMPa}{Singular matrix in \string\pgfplotsmatrixLUdecomp}\pgfeov% |
| 247 |
\pgfplotsmat@n=-1 |
| 248 |
}{% |
| 249 |
\pgfplotscoordmath{default}{op}{reciprocal}{{\pgfplotsmat@big}}% |
| 250 |
\pgfplotsarrayletentry\pgfplotsmat@i\of\pgfplotsmat@vv=\pgfmathresult |
| 251 |
}% |
| 252 |
% |
| 253 |
\advance\pgfplotsmat@i by1 |
| 254 |
\expandafter\pgfplotsmatrixLUdecomp@scalingloop |
| 255 |
\fi |
| 256 |
}% |
| 257 |
\def\pgfplotsmatrixLUdecomp@scalingloop@{% |
| 258 |
\ifnum\pgfplotsmat@j<\pgfplotsmat@n |
| 259 |
% |
| 260 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@j\to\pgfplotsmat@Aij |
| 261 |
\pgfplotscoordmath{default}{parsenumber}{\pgfplotsmat@Aij}% |
| 262 |
\let\pgfplotsmat@Aij=\pgfmathresult |
| 263 |
\pgfplotsmat@letentry\the\pgfplotsmat@i,\the\pgfplotsmat@j=\pgfplotsmat@Aij |
| 264 |
% |
| 265 |
\pgfplotscoordmath{default}{op}{abs}{{\pgfplotsmat@Aij}}% |
| 266 |
\let\pgfplotsmat@temp=\pgfmathresult |
| 267 |
\pgfplotscoordmath{default}{max}{\pgfplotsmat@temp}{\pgfplotsmat@big}% |
| 268 |
\let\pgfplotsmat@big=\pgfmathresult |
| 269 |
% |
| 270 |
\advance\pgfplotsmat@j by1 |
| 271 |
\expandafter\pgfplotsmatrixLUdecomp@scalingloop@ |
| 272 |
\fi |
| 273 |
}% |
| 274 |
|
| 275 |
\def\pgfplotsmatrixLUdecomp@mainloop@j{% |
| 276 |
\ifnum\pgfplotsmat@j<\pgfplotsmat@n |
| 277 |
% |
| 278 |
\pgfplotsmat@i=0 |
| 279 |
\pgfplotsmatrixLUdecomp@mainloop@j@i |
| 280 |
% |
| 281 |
\pgfplotscoordmath{default}{zero}% |
| 282 |
\let\pgfplotsmat@big=\pgfmathresult |
| 283 |
% |
| 284 |
\pgfplotsmat@i=\pgfplotsmat@j |
| 285 |
\pgfplotsmatrixLUdecomp@mainloop@j@i@second |
| 286 |
% |
| 287 |
\ifnum\pgfplotsmat@j=\pgfplotsmat@imax |
| 288 |
\else |
| 289 |
% interchange rows... |
| 290 |
\pgfplotsmat@k=0 |
| 291 |
\pgfplotsmatrixLUdecomp@mainloop@j@k |
| 292 |
% |
| 293 |
\c@pgfplotsarray@tmp=-\pgfplotsmat@parity\relax% |
| 294 |
\edef\pgfplotsmat@parity{\the\c@pgfplotsarray@tmp}% |
| 295 |
% |
| 296 |
% FIXME : this here is DIFFERENT. Seems there is something |
| 297 |
% missing in Numerical Recipes book |
| 298 |
\pgfplotsarrayselect\pgfplotsmat@j\of\pgfplotsmat@vv\to\pgfplotsmat@dum |
| 299 |
%\pgfplotsarrayselect\pgfplotsmat@imax\of\pgfplotsmat@vv\to\pgfplotsmat@temp |
| 300 |
%\pgfplotsarrayletentry\pgfplotsmat@j\of\pgfplotsmat@vv=\pgfplotsmat@temp |
| 301 |
\pgfplotsarrayletentry\pgfplotsmat@imax\of\pgfplotsmat@vv=\pgfplotsmat@dum |
| 302 |
\fi |
| 303 |
% |
| 304 |
\edef\pgfplotsmat@dum{\the\pgfplotsmat@imax}% |
| 305 |
\pgfplotsmat@letpermentry\pgfplotsmat@j=\pgfplotsmat@dum |
| 306 |
% |
| 307 |
\pgfplotsmat@select\the\pgfplotsmat@j,\the\pgfplotsmat@j\to\pgfplotsmat@Ajj |
| 308 |
\pgfplotscoordmath{default}{if is}{\pgfplotsmat@Ajj}{0}{% |
| 309 |
\pgfplotscoordmath{default}{parsenumber}{1e-15}% |
| 310 |
\pgfplotsmatrixLUdecompwarnsingular |
| 311 |
\pgfplotsmat@letentry\the\pgfplotsmat@j,\the\pgfplotsmat@j=\pgfmathresult |
| 312 |
}{% |
| 313 |
}% |
| 314 |
% |
| 315 |
% |
| 316 |
\advance\pgfplotsmat@j by1 |
| 317 |
\ifnum\pgfplotsmat@j=\pgfplotsmat@n |
| 318 |
\advance\pgfplotsmat@j by-1 |
| 319 |
\else |
| 320 |
\advance\pgfplotsmat@j by-1 |
| 321 |
\pgfplotsmat@select\the\pgfplotsmat@j,\the\pgfplotsmat@j\to\pgfplotsmat@Ajj |
| 322 |
\pgfplotscoordmath{default}{op}{reciprocal}{{\pgfplotsmat@Ajj}}% |
| 323 |
\let\pgfplotsmat@dum=\pgfmathresult |
| 324 |
\pgfplotsmat@i=\pgfplotsmat@j |
| 325 |
\advance\pgfplotsmat@i by1 |
| 326 |
\pgfplotsmatrixLUdecomp@mainloop@j@i@final |
| 327 |
\fi |
| 328 |
\advance\pgfplotsmat@j by1 |
| 329 |
\expandafter\pgfplotsmatrixLUdecomp@mainloop@j |
| 330 |
\fi |
| 331 |
}% |
| 332 |
\def\pgfplotsmatrixLUdecompwarnsingular{% |
| 333 |
\pgfplotswarning{linear system singular}\pgfeov% |
| 334 |
}% |
| 335 |
\def\pgfplotsmatrixLUdecomp@mainloop@j@i{% |
| 336 |
\ifnum\pgfplotsmat@i<\pgfplotsmat@j |
| 337 |
% |
| 338 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@j\to\pgfplotsmat@sum |
| 339 |
\pgfplotsmat@k=0 |
| 340 |
\pgfplotsmatrixLUdecomp@mainloop@j@i@k |
| 341 |
\pgfplotsmat@letentry\the\pgfplotsmat@i,\the\pgfplotsmat@j=\pgfplotsmat@sum |
| 342 |
% |
| 343 |
\advance\pgfplotsmat@i by1 |
| 344 |
\expandafter\pgfplotsmatrixLUdecomp@mainloop@j@i |
| 345 |
\fi |
| 346 |
}% |
| 347 |
\def\pgfplotsmatrixLUdecomp@mainloop@j@i@k{% |
| 348 |
\ifnum\pgfplotsmat@k<\pgfplotsmat@i |
| 349 |
% |
| 350 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@k\to\pgfplotsmat@Aik |
| 351 |
\pgfplotsmat@select\the\pgfplotsmat@k,\the\pgfplotsmat@j\to\pgfplotsmat@Akj |
| 352 |
\pgfplotscoordmath{default}{op}{multiply}{{\pgfplotsmat@Aik}{\pgfplotsmat@Akj}}% |
| 353 |
\pgfplotscoordmath{default}{op}{subtract}{{\pgfplotsmat@sum}{\pgfmathresult}}% |
| 354 |
\let\pgfplotsmat@sum=\pgfmathresult |
| 355 |
% |
| 356 |
\advance\pgfplotsmat@k by1 |
| 357 |
\expandafter\pgfplotsmatrixLUdecomp@mainloop@j@i@k |
| 358 |
\fi |
| 359 |
} |
| 360 |
\def\pgfplotsmatrixLUdecomp@mainloop@j@i@second{% |
| 361 |
\ifnum\pgfplotsmat@i<\pgfplotsmat@n |
| 362 |
% |
| 363 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@j\to\pgfplotsmat@sum |
| 364 |
% |
| 365 |
\pgfplotsmat@k=0 |
| 366 |
\pgfplotsmatrixLUdecomp@mainloop@j@i@second@k |
| 367 |
\pgfplotsmat@letentry\the\pgfplotsmat@i,\the\pgfplotsmat@j=\pgfplotsmat@sum |
| 368 |
% |
| 369 |
\pgfplotscoordmath{default}{op}{abs}{{\pgfplotsmat@sum}}% |
| 370 |
\pgfplotscoordmath{default}{op}{multiply}{{\pgfmathresult}{\pgfplotsarrayvalueofelem\the\pgfplotsmat@i\of\pgfplotsmat@vv}}% |
| 371 |
\let\pgfplotsmat@dum=\pgfmathresult |
| 372 |
\pgfplotscoordmath{default}{if less than}{\pgfplotsmat@dum}{\pgfplotsmat@big}{% |
| 373 |
}{% |
| 374 |
\let\pgfplotsmat@big=\pgfplotsmat@dum |
| 375 |
\pgfplotsmat@imax=\pgfplotsmat@i |
| 376 |
}% |
| 377 |
% |
| 378 |
\advance\pgfplotsmat@i by1 |
| 379 |
\expandafter\pgfplotsmatrixLUdecomp@mainloop@j@i@second |
| 380 |
\fi |
| 381 |
}% |
| 382 |
\def\pgfplotsmatrixLUdecomp@mainloop@j@i@second@k{% |
| 383 |
\ifnum\pgfplotsmat@k<\pgfplotsmat@j |
| 384 |
% |
| 385 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@k\to\pgfplotsmat@Aik |
| 386 |
\pgfplotsmat@select\the\pgfplotsmat@k,\the\pgfplotsmat@j\to\pgfplotsmat@Akj |
| 387 |
\pgfplotscoordmath{default}{op}{multiply}{{\pgfplotsmat@Aik}{\pgfplotsmat@Akj}}% |
| 388 |
\pgfplotscoordmath{default}{op}{subtract}{{\pgfplotsmat@sum}{\pgfmathresult}}% |
| 389 |
\let\pgfplotsmat@sum=\pgfmathresult |
| 390 |
% |
| 391 |
\advance\pgfplotsmat@k by1 |
| 392 |
\expandafter\pgfplotsmatrixLUdecomp@mainloop@j@i@second@k |
| 393 |
\fi |
| 394 |
}% |
| 395 |
|
| 396 |
\def\pgfplotsmatrixLUdecomp@mainloop@j@k{% |
| 397 |
\ifnum\pgfplotsmat@k<\pgfplotsmat@n |
| 398 |
% |
| 399 |
\pgfplotsmat@select\the\pgfplotsmat@imax,\the\pgfplotsmat@k\to\pgfplotsmat@dum |
| 400 |
\pgfplotsmat@select\the\pgfplotsmat@j,\the\pgfplotsmat@k\to\pgfplotsmat@Ajk |
| 401 |
\pgfplotsmat@letentry\the\pgfplotsmat@imax,\the\pgfplotsmat@k=\pgfplotsmat@Ajk |
| 402 |
\pgfplotsmat@letentry\the\pgfplotsmat@j,\the\pgfplotsmat@k=\pgfplotsmat@dum |
| 403 |
% |
| 404 |
\advance\pgfplotsmat@k by1 |
| 405 |
\expandafter\pgfplotsmatrixLUdecomp@mainloop@j@k |
| 406 |
\fi |
| 407 |
} |
| 408 |
\def\pgfplotsmatrixLUdecomp@mainloop@j@i@final{% |
| 409 |
\ifnum\pgfplotsmat@i<\pgfplotsmat@n |
| 410 |
% |
| 411 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@j\to\pgfplotsmat@Aij |
| 412 |
\pgfplotscoordmath{default}{op}{multiply}{{\pgfplotsmat@Aij}{\pgfplotsmat@dum}}% |
| 413 |
\pgfplotsmat@letentry\the\pgfplotsmat@i,\the\pgfplotsmat@j=\pgfmathresult |
| 414 |
% |
| 415 |
\advance\pgfplotsmat@i by1 |
| 416 |
\expandafter\pgfplotsmatrixLUdecomp@mainloop@j@i@final |
| 417 |
\fi |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
% Solves the set of n linear equations Ax = b where A = LU is given in |
| 423 |
% (#1,#2) and b = #3. |
| 424 |
% |
| 425 |
% #1: is a result of \pgfplotsmatrixLUdecomp |
| 426 |
% #2: is the permutation vector returned by \pgfplotsmatrixLUdecomp |
| 427 |
% #3: the right hand side. On output, it will be *overwritten* with |
| 428 |
% the solution. |
| 429 |
% |
| 430 |
% The algorithm has been converted from Numerical Recipes in C (I did |
| 431 |
% not copy the comments, though). You find the complete reference in |
| 432 |
% Chapter 2 of Numerical Recipes. |
| 433 |
\def\pgfplotsmatrixLUbacksubst#1\perm#2\inout#3{% |
| 434 |
\let\pgfplotsmat@i=\c@pgf@counta |
| 435 |
\let\pgfplotsmat@ii=\c@pgf@countb |
| 436 |
\let\pgfplotsmat@j=\c@pgf@countc |
| 437 |
\let\pgfplotsmat@n=\c@pgf@countd |
| 438 |
\let\pgfplotsmat@sum=\pgfutil@empty |
| 439 |
\pgfplotsmatrixsize#1\to\pgfplotsmat@n\c@pgf@counta |
| 440 |
\pgfplotsarraysize#3\to\c@pgf@counta |
| 441 |
\ifnum\c@pgf@counta=\pgfplotsmat@n |
| 442 |
\def\pgfplotsmat@select##1,##2\to{\pgfplotsmatrixselect##1,##2\of#1\to}% |
| 443 |
\def\pgfplotsmat@selectperm##1\to{\pgfplotsarrayselect##1\of#2\to}% |
| 444 |
\def\pgfplotsmat@selectb##1\to{\pgfplotsarrayselect##1\of#3\to}% |
| 445 |
\def\pgfplotsmat@letresult##1={\pgfplotsarrayletentry##1\of#3=}% |
| 446 |
% |
| 447 |
\pgfplotsmat@ii=-1 |
| 448 |
\pgfplotsmat@i=0 |
| 449 |
\pgfplotsmatrixLUbacksubst@loop@i |
| 450 |
% |
| 451 |
\pgfplotsmat@i=\pgfplotsmat@n |
| 452 |
\advance\pgfplotsmat@i by-1 |
| 453 |
\pgfplotsmatrixLUbacksubst@loop@i@backw |
| 454 |
\else |
| 455 |
\pgfplots@error{Sorry, \string\pgfplotsmatrixLUbacksubst\space expected a vector of length \the\pgfplotsmat@n\space, not \the\c@pgf@counta}% |
| 456 |
\fi |
| 457 |
}% |
| 458 |
\def\pgfplotsmatrixLUbacksubst@loop@i{% |
| 459 |
\ifnum\pgfplotsmat@i<\pgfplotsmat@n |
| 460 |
% |
| 461 |
\pgfplotsmat@selectperm\pgfplotsmat@i\to\pgfplotsmat@ip |
| 462 |
\pgfplotsmat@selectb\pgfplotsmat@ip\to\pgfplotsmat@sum |
| 463 |
\pgfplotsmat@selectb\pgfplotsmat@i\to\pgfplotsmat@temp |
| 464 |
% |
| 465 |
\pgfplotscoordmath{default}{parsenumber}{\pgfplotsmat@sum}% |
| 466 |
\let\pgfplotsmat@sum=\pgfmathresult |
| 467 |
\pgfplotscoordmath{default}{parsenumber}{\pgfplotsmat@temp}% |
| 468 |
\let\pgfplotsmat@temp=\pgfmathresult |
| 469 |
% |
| 470 |
\pgfplotsmat@letresult\pgfplotsmat@ip=\pgfplotsmat@temp |
| 471 |
% |
| 472 |
\ifnum\pgfplotsmat@ii<0 |
| 473 |
\pgfplotscoordmath{default}{if is}{\pgfplotsmat@sum}{0}{% |
| 474 |
}{% |
| 475 |
\pgfplotsmat@ii=\pgfplotsmat@i |
| 476 |
}% |
| 477 |
\else |
| 478 |
\pgfplotsmat@j=\pgfplotsmat@ii |
| 479 |
\pgfplotsmatrixLUbacksubst@loop@i@j |
| 480 |
\fi |
| 481 |
\pgfplotsmat@letresult\pgfplotsmat@i=\pgfplotsmat@sum |
| 482 |
% |
| 483 |
\advance\pgfplotsmat@i by1 |
| 484 |
\expandafter\pgfplotsmatrixLUbacksubst@loop@i |
| 485 |
\fi |
| 486 |
}% |
| 487 |
\def\pgfplotsmatrixLUbacksubst@loop@i@j{% |
| 488 |
\ifnum\pgfplotsmat@j<\pgfplotsmat@i |
| 489 |
% |
| 490 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@j\to\pgfplotsmat@Aij |
| 491 |
\pgfplotsmat@selectb\pgfplotsmat@j\to\pgfplotsmat@temp |
| 492 |
\pgfplotscoordmath{default}{op}{multiply}{{\pgfplotsmat@temp}{\pgfplotsmat@Aij}}% |
| 493 |
\pgfplotscoordmath{default}{op}{subtract}{{\pgfplotsmat@sum}{\pgfmathresult}}% |
| 494 |
\let\pgfplotsmat@sum=\pgfmathresult |
| 495 |
% |
| 496 |
\advance\pgfplotsmat@j by1 |
| 497 |
\expandafter\pgfplotsmatrixLUbacksubst@loop@i@j |
| 498 |
\fi |
| 499 |
}% |
| 500 |
|
| 501 |
\def\pgfplotsmatrixLUbacksubst@loop@i@backw{% |
| 502 |
\ifnum\pgfplotsmat@i<0 |
| 503 |
\else |
| 504 |
% |
| 505 |
\pgfplotsmat@selectb\pgfplotsmat@i\to\pgfplotsmat@sum |
| 506 |
\pgfplotsmat@j=\pgfplotsmat@i |
| 507 |
\advance\pgfplotsmat@j by1 |
| 508 |
\pgfplotsmatrixLUbacksubst@loop@i@backw@j |
| 509 |
% |
| 510 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@i\to\pgfplotsmat@Aii |
| 511 |
\pgfplotscoordmath{default}{op}{divide}{{\pgfplotsmat@sum}{\pgfplotsmat@Aii}}% |
| 512 |
\pgfplotsmat@letresult\pgfplotsmat@i=\pgfmathresult |
| 513 |
% |
| 514 |
\advance\pgfplotsmat@i by-1 |
| 515 |
\expandafter\pgfplotsmatrixLUbacksubst@loop@i@backw |
| 516 |
\fi |
| 517 |
}% |
| 518 |
\def\pgfplotsmatrixLUbacksubst@loop@i@backw@j{% |
| 519 |
\ifnum\pgfplotsmat@j<\pgfplotsmat@n |
| 520 |
% |
| 521 |
\pgfplotsmat@select\the\pgfplotsmat@i,\the\pgfplotsmat@j\to\pgfplotsmat@Aij |
| 522 |
\pgfplotsmat@selectb\pgfplotsmat@j\to\pgfplotsmat@temp |
| 523 |
\pgfplotscoordmath{default}{op}{multiply}{{\pgfplotsmat@temp}{\pgfplotsmat@Aij}}% |
| 524 |
\pgfplotscoordmath{default}{op}{subtract}{{\pgfplotsmat@sum}{\pgfmathresult}}% |
| 525 |
\let\pgfplotsmat@sum=\pgfmathresult |
| 526 |
% |
| 527 |
\advance\pgfplotsmat@j by1 |
| 528 |
\expandafter\pgfplotsmatrixLUbacksubst@loop@i@backw@j |
| 529 |
\fi |
| 530 |
}% |
| 531 |
|
| 532 |
% Solves the linear equation system Ax = b. |
| 533 |
% #1: the matrix A |
| 534 |
% #2: the right-hand-side b. On output, #2 will contain the solution |
| 535 |
% and A will be overwritten. |
| 536 |
% |
| 537 |
% ATTENTION. This routine re-uses the four counters |
| 538 |
% \c@pgf@counta,...\c@pgf@countd. |
| 539 |
% Furthermore, it does not free any memory. |
| 540 |
% Make sure you use it inside of local scopes. |
| 541 |
\def\pgfplotsmatrixsolveLEQS#1=#2{% |
| 542 |
\pgfplotsmatrixLUdecomp#1\perm\pgfplotsmatrix@perm\sign\pgfplotsmatrix@sign |
| 543 |
\pgfplotsmatrixLUbacksubst#1\perm\pgfplotsmatrix@perm\inout#2% |
| 544 |
}% |