1 ## Copyright (C) 1994-2012 John W. Eaton
2 ## Copyright (C) 2007 Ben Abbott
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {[@var{r}, @var{p}, @var{k}, @var{e}] =} residue (@var{b}, @var{a})
22 ## @deftypefnx {Function File} {[@var{b}, @var{a}] =} residue (@var{r}, @var{p}, @var{k})
23 ## @deftypefnx {Function File} {[@var{b}, @var{a}] =} residue (@var{r}, @var{p}, @var{k}, @var{e})
24 ## The first calling form computes the partial fraction expansion for the
25 ## quotient of the polynomials, @var{b} and @var{a}.
28 ## {B(s)\over A(s)} = \sum_{m=1}^M {r_m\over (s-p_m)^e_m}
29 ## + \sum_{i=1}^N k_i s^{N-i}.
37 ## ---- = SUM ------------- + SUM k(i)*s^(N-i)
38 ## A(s) m=1 (s-p(m))^e(m) i=1
44 ## where @math{M} is the number of poles (the length of the @var{r},
45 ## @var{p}, and @var{e}), the @var{k} vector is a polynomial of order @math{N-1}
46 ## representing the direct contribution, and the @var{e} vector specifies
47 ## the multiplicity of the m-th residue's pole.
54 ## a = [1, -5, 8, -4];
55 ## [r, p, k, e] = residue (b, a)
56 ## @result{} r = [-2; 7; 3]
57 ## @result{} p = [2; 2; 1]
58 ## @result{} k = [](0x0)
59 ## @result{} e = [1; 2; 1]
64 ## which represents the following partial fraction expansion
67 ## {s^2+s+1\over s^3-5s^2+8s-4} = {-2\over s-2} + {7\over (s-2)^2} + {3\over s-1}
75 ## ------------------- = ----- + ------- + -----
76 ## s^3 - 5s^2 + 8s - 4 (s-2) (s-2)^2 (s-1)
82 ## The second calling form performs the inverse operation and computes
83 ## the reconstituted quotient of polynomials, @var{b}(s)/@var{a}(s),
84 ## from the partial fraction expansion; represented by the residues,
85 ## poles, and a direct polynomial specified by @var{r}, @var{p} and
86 ## @var{k}, and the pole multiplicity @var{e}.
88 ## If the multiplicity, @var{e}, is not explicitly specified the multiplicity is
89 ## determined by the function @code{mpoles}.
98 ## [b, a] = residue (r, p, k)
99 ## @result{} b = [1, -5, 9, -3, 1]
100 ## @result{} a = [1, -5, 8, -4]
102 ## where mpoles is used to determine e = [1; 2; 1]
106 ## Alternatively the multiplicity may be defined explicitly, for example,
114 ## [b, a] = residue (r, p, k, e)
115 ## @result{} b = [1, -5, 9, -3, 1]
116 ## @result{} a = [1, -5, 8, -4]
121 ## which represents the following partial fraction expansion
124 ## {-2\over s-2} + {7\over (s-2)^2} + {3\over s-1} + s = {s^4-5s^3+9s^2-3s+1\over s^3-5s^2+8s-4}
131 ## -2 7 3 s^4 - 5s^3 + 9s^2 - 3s + 1
132 ## ----- + ------- + ----- + s = --------------------------
133 ## (s-2) (s-2)^2 (s-1) s^3 - 5s^2 + 8s - 4
138 ## @seealso{mpoles, poly, roots, conv, deconv}
141 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
142 ## Author: Ben Abbott <bpabbott@mac.com>
143 ## Created: June 1994
146 function [r, p, k, e] = residue (b, a, varargin)
148 if (nargin < 2 || nargin > 4)
160 ## The inputs are the residue, pole, and direct part. Solve for the
161 ## corresponding numerator and denominator polynomials
162 [r, p] = rresidue (b, a, varargin{1}, toler, e);
166 ## Make sure both polynomials are in reduced form.
177 ## Handle special cases here.
179 if (la == 0 || lb == 0)
193 ## Sort poles so that multiplicity loop will work.
195 [e, indx] = mpoles (p, toler, 1);
198 ## For each group of pole multiplicity, set the value of each
199 ## pole to the average of the group. This reduces the error in
200 ## the resulting poles.
202 p_group = cumsum (e == 1);
203 for ng = 1:p_group(end)
204 m = find (p_group == ng);
208 ## Find the direct term if there is one.
211 ## Also return the reduced numerator.
212 [k, b] = deconv (b, a);
218 ## Determine if the poles are (effectively) zero.
220 small = max (abs (p));
221 if (isa (a, "single") || isa (b, "single"))
222 small = max ([small, 1]) * eps ("single") * 1e4 * (1 + numel (p))^2;
224 small = max ([small, 1]) * eps * 1e4 * (1 + numel (p))^2;
226 p(abs (p) < small) = 0;
228 ## Determine if the poles are (effectively) real, or imaginary.
230 index = (abs (imag (p)) < small);
231 p(index) = real (p(index));
232 index = (abs (real (p)) < small);
233 p(index) = 1i * imag (p(index));
235 ## The remainder determines the residues. The case of one pole
243 ## Determine the order of the denominator and remaining numerator.
244 ## With the direct term removed the potential order of the numerator
245 ## is one less than the order of the denominator.
247 aorder = numel (a) - 1;
250 ## Construct a system of equations relating the individual
251 ## contributions from each residue to the complete numerator.
253 A = zeros (border+1, border+1);
254 B = prepad (reshape (b, [numel(b), 1]), border+1, 0);
256 ri = zeros (size (p));
258 A(:,ip) = prepad (rresidue (ri, p, [], toler), border+1, 0).';
261 ## Solve for the residues.
267 function [pnum, pden, e] = rresidue (r, p, k, toler, e)
269 ## Reconstitute the numerator and denominator polynomials from the
270 ## residues, poles, and direct term.
272 if (nargin < 2 || nargin > 5)
291 [e, indx] = mpoles (p, toler, 0);
303 pden = conv (pden, pn);
307 ## D is the order of the denominator
308 ## K is the order of the direct polynomial
309 ## N is the order of the resulting numerator
310 ## pnum(1:(N+1)) is the numerator's polynomial
311 ## pden(1:(D+1)) is the denominator's polynomial
312 ## pm is the multible pole for the nth residue
313 ## pn is the numerator contribution for the nth residue
315 D = numel (pden) - 1;
318 pnum = zeros (1, N+1);
319 for n = indx(abs (r) > 0)
328 pn = deconv (pden, pm);
330 pnum = pnum + prepad (pn, N+1, 0, 2);
333 ## Add the direct term.
336 pnum = pnum + conv (pden, k);
339 ## Check for leading zeros and trim the polynomial coefficients.
340 if (isa (r, "single") || isa (p, "single") || isa (k, "single"))
341 small = max ([max(abs(pden)), max(abs(pnum)), 1]) * eps ("single");
343 small = max ([max(abs(pden)), max(abs(pnum)), 1]) * eps;
346 pnum(abs (pnum) < small) = 0;
347 pden(abs (pden) < small) = 0;
349 pnum = polyreduce (pnum);
350 pden = polyreduce (pden);
356 %! a = [1, -5, 8, -4];
357 %! [r, p, k, e] = residue (b, a);
358 %! assert (abs (r - [-2; 7; 3]) < 1e-12
359 %! && abs (p - [2; 2; 1]) < 1e-12
361 %! && e == [1; 2; 1]);
363 %! b = conv (k, a) + prepad (b, numel (k) + numel (a) - 1, 0);
365 %! [br, ar] = residue (r, p, k);
366 %! assert ((abs (br - b) < 1e-12
367 %! && abs (ar - a) < 1e-12));
368 %! [br, ar] = residue (r, p, k, e);
369 %! assert ((abs (br - b) < 1e-12
370 %! && abs (ar - a) < 1e-12));
374 %! a = [1, 0, 18, 0, 81];
375 %! [r, p, k, e] = residue (b, a);
376 %! r1 = [-5i; 12; +5i; 12]/54;
377 %! p1 = [+3i; +3i; -3i; -3i];
378 %! assert (abs (r - r1) < 1e-12 && abs (p - p1) < 1e-12
380 %! && e == [1; 2; 1; 2]);
381 %! [br, ar] = residue (r, p, k);
382 %! assert ((abs (br - b) < 1e-12
383 %! && abs (ar - a) < 1e-12));
390 %! [b, a] = residue (r, p, k, e);
391 %! assert ((abs (b - [1, -5, 9, -3, 1]) < 1e-12
392 %! && abs (a - [1, -5, 8, -4]) < 1e-12));
393 %! [rr, pr, kr, er] = residue (b, a);
394 %! [jnk, n] = mpoles(p);
395 %! assert ((abs (rr - r(n)) < 1e-12
396 %! && abs (pr - p(n)) < 1e-12
397 %! && abs (kr - k) < 1e-12
398 %! && abs (er - e(n)) < 1e-12));
403 %! [r, p, k, e] = residue (b, a);
406 %! assert (abs (r - r1) < 1e-12 && abs (p - p1) < 1e-12
409 %! [br, ar] = residue (r, p, k);
410 %! assert ((abs (br - b) < 1e-12
411 %! && abs (ar - a) < 1e-12));
413 ## The following test is due to Bernard Grung (bug #34266)
415 %! z1 = 7.0372976777e6;
416 %! p1 = -3.1415926536e9;
417 %! p2 = -4.9964813512e8;
418 %! r1 = -(1 + z1/p1)/(1 - p1/p2)/p2/p1;
419 %! r2 = -(1 + z1/p2)/(1 - p2/p1)/p2/p1;
420 %! r3 = (1 + (p2 + p1)/p2/p1*z1)/p2/p1;
422 %! r = [r1; r2; r3; r4];
423 %! p = [p1; p2; 0; 0];
427 %! a = [1, -(p1 + p2), p1*p2, 0, 0];
428 %! [br, ar] = residue (r, p, k, e);
429 %! assert (br, b, 1e-8);
430 %! assert (ar, a, 1e-8);