Mercurial > hg > octave-nkf
annotate scripts/polynomial/polyvalm.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | c174a1fc3fde |
children | 1bf0ce0930be |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004, |
8920 | 2 ## 2005, 2006, 2007, 2008 John W. Eaton |
8903
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
3 ## Copyright (C) 2009 Jaroslav Hajek |
2313 | 4 ## |
5 ## This file is part of Octave. | |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
2313 | 11 ## |
12 ## Octave is distributed in the hope that it will be useful, but | |
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 ## General Public License for more details. | |
16 ## | |
17 ## You should have received a copy of the GNU General Public License | |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
904 | 20 |
3368 | 21 ## -*- texinfo -*- |
22 ## @deftypefn {Function File} {} polyvalm (@var{c}, @var{x}) | |
2311 | 23 ## Evaluate a polynomial in the matrix sense. |
3426 | 24 ## |
3368 | 25 ## @code{polyvalm (@var{c}, @var{x})} will evaluate the polynomial in the |
26 ## matrix sense, i.e. matrix multiplication is used instead of element by | |
27 ## element multiplication as is used in polyval. | |
3426 | 28 ## |
3368 | 29 ## The argument @var{x} must be a square matrix. |
3457 | 30 ## @seealso{polyval, poly, roots, conv, deconv, residue, filter, |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
31 ## polyderiv, polyinteg} |
5642 | 32 ## @end deftypefn |
1025 | 33 |
3202 | 34 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 35 ## Created: June 1994 |
36 ## Adapted-By: jwe | |
561 | 37 |
2312 | 38 function y = polyvalm (c, x) |
561 | 39 |
3085 | 40 if (nargin != 2) |
6046 | 41 print_usage (); |
561 | 42 endif |
43 | |
4030 | 44 if (! (isvector (c) || isempty (c))) |
3458 | 45 error ("polyvalm: first argument must be a vector"); |
561 | 46 endif |
47 | |
4030 | 48 if (! issquare (x)) |
3458 | 49 error ("polyvalm: second argument must be a square matrix"); |
561 | 50 endif |
51 | |
8903
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
52 n = length (c); |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
53 if (n == 0) |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
54 y = zeros (rows (x), class (x)); |
3085 | 55 else |
8903
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
56 id = eye (rows (x), class (x)); |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
57 y = c(1) * id; |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
58 for i = 2:n |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
59 y = y * x + c(i) * id; |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
60 endfor |
3085 | 61 endif |
561 | 62 |
63 endfunction | |
7411 | 64 |
8903
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
65 |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
66 %!assert(! any (polyvalm ([], [1, 2; 3, 4]))(:)); |
c174a1fc3fde
reimplement polyvalm using Horner
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
67 %!assert(polyvalm ([1, 2, 3, 4], [3, -4, 1; -2, 0, 2; -1, 4, -3]), [117, -124, 11; -70, 36, 38; -43, 92, -45]) |
7411 | 68 |
69 %!error polyvalm ([1, 1, 1], [1, 2; 3, 4; 5, 6]); | |
70 |