comparison scripts/special-matrix/pascal.m @ 11098:dcde7c5a1d29

new tests for special-matrix functions
author John W. Eaton <jwe@octave.org>
date Thu, 14 Oct 2010 14:43:59 -0400
parents 3140cb7a05a1
children fd0a3ac60b0e
comparison
equal deleted inserted replaced
11097:ffb2f1ef2097 11098:dcde7c5a1d29
18 ## <http://www.gnu.org/licenses/>. 18 ## <http://www.gnu.org/licenses/>.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} pascal (@var{n}) 21 ## @deftypefn {Function File} {} pascal (@var{n})
22 ## @deftypefnx {Function File} {} pascal (@var{n}, @var{t}) 22 ## @deftypefnx {Function File} {} pascal (@var{n}, @var{t})
23 ##
24 ## Return the Pascal matrix of order @var{n} if @code{@var{t} = 0}. 23 ## Return the Pascal matrix of order @var{n} if @code{@var{t} = 0}.
25 ## @var{t} defaults to 0. Return lower triangular Cholesky factor of 24 ## @var{t} defaults to 0. Return lower triangular Cholesky factor of
26 ## the Pascal matrix if @code{@var{t} = 1}. This matrix is its own 25 ## the Pascal matrix if @code{@var{t} = 1}. This matrix is its own
27 ## inverse, that is @code{pascal (@var{n}, 1) ^ 2 == eye (@var{n})}. 26 ## inverse, that is @code{pascal (@var{n}, 1) ^ 2 == eye (@var{n})}.
28 ## If @code{@var{t} = -1}, return its absolute value. This is the 27 ## If @code{@var{t} = -1}, return its absolute value. This is the
50 49
51 if (! isscalar (n) || ! isscalar (t)) 50 if (! isscalar (n) || ! isscalar (t))
52 error ("pascal: expecting scalar arguments, found something else"); 51 error ("pascal: expecting scalar arguments, found something else");
53 endif 52 endif
54 53
54 if (t < -1 || t > 2)
55 error ("pascal: expecting t to be -1, 0, 1, or 2, found %d", t);
56 endif
57
55 retval = zeros (n); 58 retval = zeros (n);
56 retval(:,1) = 1; 59 retval(:,1) = 1;
57 60
58 if (t == -1) 61 if (t == -1)
59 for j = 2:n 62 for j = 2:n
76 retval = -retval; 79 retval = -retval;
77 endif 80 endif
78 endif 81 endif
79 82
80 endfunction 83 endfunction
84
85 %!assert (pascal(3,-1), [1,0,0;1,1,0;1,2,1])
86 %!assert (pascal(3,0), [1,1,1;1,2,3;1,3,6])
87 %!assert (pascal(3,0), pascal(3))
88 %!assert (pascal(3,1), [1,0,0;1,-1,0;1,-2,1])
89 %!assert (pascal(3,2), [0,0,-1;0,-1,2;-1,-1,1])
90 %!error (pascal(3,4))
91 %!error (pascal(3,-2))
92 %!error (pascal())
93 %!error (pascal(1,2,3))