Mercurial > octave-nkf
comparison scripts/special-matrix/pascal.m @ 5827:1fe78adb91bc
[project @ 2006-05-22 06:25:14 by jwe]
author | jwe |
---|---|
date | Mon, 22 May 2006 06:25:14 +0000 |
parents | |
children | 93c65f2a5668 |
comparison
equal
deleted
inserted
replaced
5826:6c6ff9b82577 | 5827:1fe78adb91bc |
---|---|
1 ## Copyright (C) 1999 Peter Ekberg | |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 2, or (at your option) | |
8 ## any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, write to the Free | |
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 ## 02110-1301, USA. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} pascal (@var{n}, @var{t}) | |
22 ## | |
23 ## Return the Pascal matrix of order @var{n} if @code{@var{t} = 0}. | |
24 ## @var{t} defaults to 0. Return lower triangular Cholesky factor of | |
25 ## the Pascal matrix if @code{@var{t} = 1}. This matrix is its own | |
26 ## inverse, that is @code{pascal (@var{n}, 1) ^ 2 == eye (@var{n})}. | |
27 ## If @code{@var{t} = 2}, return a transposed and permuted version of | |
28 ## @code{pascal (@var{n}, 1)}, which is the cube-root of the identity | |
29 ## matrix. That is @code{pascal (@var{n}, 2) ^ 3 == eye (@var{n})}. | |
30 ## | |
31 ## @seealso{hankel, vander, sylvester_matrix, hilb, invhilb, toeplitz | |
32 ## hadamard, wilkinson, compan, rosser} | |
33 ## @end deftypefn | |
34 | |
35 ## Author: Peter Ekberg | |
36 ## (peda) | |
37 | |
38 function retval = pascal (n, t) | |
39 | |
40 if (nargin > 2) || (nargin == 0) | |
41 print_usage (); | |
42 endif | |
43 | |
44 if (nargin == 1) | |
45 t = 0; | |
46 endif | |
47 | |
48 if (! is_scalar (n) || ! is_scalar (t)) | |
49 error ("pascal: expecting scalar arguments, found something else"); | |
50 endif | |
51 | |
52 retval = diag ((-1).^[0:n-1]); | |
53 retval(:,1) = ones (n, 1); | |
54 | |
55 for j = 2:n-1 | |
56 for i = j+1:n | |
57 retval(i,j) = retval(i-1,j) - retval(i-1,j-1); | |
58 endfor | |
59 endfor | |
60 | |
61 if (t == 0) | |
62 retval = retval*retval'; | |
63 elseif (t == 2) | |
64 retval = retval'; | |
65 retval = retval(n:-1:1,:); | |
66 retval(:,n) = -retval(:,n); | |
67 retval(n,:) = -retval(n,:); | |
68 if (rem(n,2) != 1) | |
69 retval = -retval; | |
70 endif | |
71 endif | |
72 | |
73 endfunction |