1 ## Copyright (C) 1994-2012 John W. Eaton
2 ## Copyright (C) 2009 Jaroslav Hajek
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} {} polyvalm (@var{c}, @var{x})
22 ## Evaluate a polynomial in the matrix sense.
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}.
28 ## The argument @var{x} must be a square matrix.
29 ## @seealso{polyval, roots, poly}
32 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
36 function y = polyvalm (c, x)
42 if (! (isvector (c) || isempty (c)))
43 error ("polyvalm: first argument must be a vector");
47 error ("polyvalm: second argument must be a square matrix");
52 y = zeros (rows (x), class (x));
54 id = eye (rows (x), class (x));
57 y = y * x + c(i) * id;
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])
67 %!error polyvalm ([1, 1, 1], [1, 2; 3, 4; 5, 6]);