Mercurial > hg > octave-nkf
annotate scripts/special-matrix/pascal.m @ 9094:2e35cfcf6a6a
fix, optimize & extend pascal
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 06 Apr 2009 11:16:20 +0200 |
parents | 1bf0ce0930be |
children | 5235caf89e12 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1999, 2006, 2007 Peter Ekberg |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
2 ## Copyright (C) 2009 VZLU Prague |
5827 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5827 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5827 | 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
25 ## the Pascal matrix if @code{@var{t} = 1}. This matrix is its own |
5827 | 26 ## inverse, that is @code{pascal (@var{n}, 1) ^ 2 == eye (@var{n})}. |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
27 ## If @code{@var{t} = -1}, return its absolute value. This is the |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
28 ## standard pascal triangle as a lower-triangular matrix. |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
29 ## If @code{@var{t} = 2}, return a transposed and permuted version of |
5827 | 30 ## @code{pascal (@var{n}, 1)}, which is the cube-root of the identity |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
31 ## matrix. That is @code{pascal (@var{n}, 2) ^ 3 == eye (@var{n})}. |
5827 | 32 ## |
33 ## @seealso{hankel, vander, sylvester_matrix, hilb, invhilb, toeplitz | |
34 ## hadamard, wilkinson, compan, rosser} | |
35 ## @end deftypefn | |
36 | |
37 ## Author: Peter Ekberg | |
38 ## (peda) | |
39 | |
40 function retval = pascal (n, t) | |
41 | |
42 if (nargin > 2) || (nargin == 0) | |
43 print_usage (); | |
44 endif | |
45 | |
46 if (nargin == 1) | |
47 t = 0; | |
48 endif | |
49 | |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
50 if (! isscalar (n) || ! isscalar (t)) |
5827 | 51 error ("pascal: expecting scalar arguments, found something else"); |
52 endif | |
53 | |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
54 retval = zeros (n); |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
55 retval(:,1) = 1; |
5827 | 56 |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
57 if (t == -1) |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
58 for j = 2:n-1 |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 retval(j:n,j) = cumsum (retval (j-1:n-1,j-1)); |
5827 | 60 endfor |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
61 else |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
62 for j = 2:n-1 |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
63 retval(j:n,j) = -cumsum (retval (j-1:n-1,j-1)); |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
64 endfor |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
65 endif |
5827 | 66 |
67 if (t == 0) | |
68 retval = retval*retval'; | |
69 elseif (t == 2) | |
70 retval = retval'; | |
71 retval = retval(n:-1:1,:); | |
72 retval(:,n) = -retval(:,n); | |
73 retval(n,:) = -retval(n,:); | |
74 if (rem(n,2) != 1) | |
75 retval = -retval; | |
76 endif | |
77 endif | |
78 | |
79 endfunction |