]> Creatis software - CreaPhase.git/blob - octave_packages/m/polynomial/polyvalm.m
update packages
[CreaPhase.git] / octave_packages / m / polynomial / polyvalm.m
1 ## Copyright (C) 1994-2012 John W. Eaton
2 ## Copyright (C) 2009 Jaroslav Hajek
3 ##
4 ## This file is part of Octave.
5 ##
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.
10 ##
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.
15 ##
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/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} polyvalm (@var{c}, @var{x})
22 ## Evaluate a polynomial in the matrix sense.
23 ##
24 ## @code{polyvalm (@var{c}, @var{x})} will evaluate the polynomial in the
25 ## matrix sense, i.e., matrix multiplication is used instead of element by
26 ## element multiplication as used in @code{polyval}.
27 ##
28 ## The argument @var{x} must be a square matrix.
29 ## @seealso{polyval, roots, poly}
30 ## @end deftypefn
31
32 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
33 ## Created: June 1994
34 ## Adapted-By: jwe
35
36 function y = polyvalm (c, x)
37
38   if (nargin != 2)
39     print_usage ();
40   endif
41
42   if (! (isvector (c) || isempty (c)))
43     error ("polyvalm: first argument must be a vector");
44   endif
45
46   if (! issquare (x))
47     error ("polyvalm: second argument must be a square matrix");
48   endif
49
50   n = length (c);
51   if (n == 0)
52     y = zeros (rows (x), class (x));
53   else
54     id = eye (rows (x), class (x));
55     y = c(1) * id;
56     for i = 2:n
57       y = y * x + c(i) * id;
58     endfor
59   endif
60
61 endfunction
62
63
64 %!assert(! any (polyvalm ([], [1, 2; 3, 4]))(:));
65 %!assert(polyvalm ([1, 2, 3, 4], [3, -4, 1; -2, 0, 2; -1, 4, -3]), [117, -124, 11; -70, 36, 38; -43, 92, -45])
66
67 %!error polyvalm ([1, 1, 1], [1, 2; 3, 4; 5, 6]);
68