Mercurial > octave-antonio
annotate scripts/special-matrix/pascal.m @ 12485:f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 27 Feb 2011 18:21:46 -0800 |
parents | 139f993936af |
children | 4d777e05d47c |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1999-2011 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 -*- | |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
21 ## @deftypefn {Function File} {} pascal (@var{n}) |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
22 ## @deftypefnx {Function File} {} pascal (@var{n}, @var{t}) |
5827 | 23 ## Return the Pascal matrix of order @var{n} if @code{@var{t} = 0}. |
12485
f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
24 ## @var{t} defaults to 0. Return the pseudo-lower triangular |
f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
25 ## Cholesky@tie{}factor of the Pascal matrix if @code{@var{t} = 1} (The sign |
f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
26 ## of some columns may be negative). This matrix is its own |
5827 | 27 ## inverse, that is @code{pascal (@var{n}, 1) ^ 2 == eye (@var{n})}. |
12485
f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
28 ## If @code{@var{t} = -1}, return the true Cholesky@tie{}factor with strictly |
f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
29 ## positive values on the diagonal. |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
30 ## If @code{@var{t} = 2}, return a transposed and permuted version of |
12485
f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
31 ## @code{pascal (@var{n}, 1)}, which is the cube root of the identity |
f5a5e5252b5a
Fix incorrect statement in documentation for pascal.m. Bug #32523.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
32 ## matrix. That is, @code{pascal (@var{n}, 2) ^ 3 == eye (@var{n})}. |
5827 | 33 ## |
12448
f2c080bbd8a5
Fix documentation bugs uncovered while rewriting munge-texi.
Rik <octave@nomad.inbox5.com>
parents:
11593
diff
changeset
|
34 ## @seealso{hankel, vander, sylvester_matrix, hilb, invhilb, toeplitz, |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
9209
diff
changeset
|
35 ## hadamard, wilkinson, compan, rosser} |
5827 | 36 ## @end deftypefn |
37 | |
38 ## Author: Peter Ekberg | |
39 ## (peda) | |
40 | |
41 function retval = pascal (n, t) | |
42 | |
43 if (nargin > 2) || (nargin == 0) | |
44 print_usage (); | |
45 endif | |
46 | |
47 if (nargin == 1) | |
48 t = 0; | |
49 endif | |
50 | |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
51 if (! isscalar (n) || ! isscalar (t)) |
5827 | 52 error ("pascal: expecting scalar arguments, found something else"); |
53 endif | |
54 | |
11098
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
55 if (t < -1 || t > 2) |
12480
139f993936af
Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents:
12448
diff
changeset
|
56 error ("pascal: expecting T to be -1, 0, 1, or 2, found %d", t); |
11098
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
57 endif |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
58 |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 retval = zeros (n); |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
60 retval(:,1) = 1; |
5827 | 61 |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
62 if (t == -1) |
9096 | 63 for j = 2:n |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
64 retval(j:n,j) = cumsum (retval (j-1:n-1,j-1)); |
5827 | 65 endfor |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
66 else |
9096 | 67 for j = 2:n |
9094
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
68 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
|
69 endfor |
2e35cfcf6a6a
fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
70 endif |
5827 | 71 |
72 if (t == 0) | |
73 retval = retval*retval'; | |
74 elseif (t == 2) | |
75 retval = retval'; | |
76 retval = retval(n:-1:1,:); | |
77 retval(:,n) = -retval(:,n); | |
78 retval(n,:) = -retval(n,:); | |
79 if (rem(n,2) != 1) | |
80 retval = -retval; | |
81 endif | |
82 endif | |
83 | |
84 endfunction | |
11098
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
85 |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
86 %!assert (pascal(3,-1), [1,0,0;1,1,0;1,2,1]) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
87 %!assert (pascal(3,0), [1,1,1;1,2,3;1,3,6]) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
88 %!assert (pascal(3,0), pascal(3)) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
89 %!assert (pascal(3,1), [1,0,0;1,-1,0;1,-2,1]) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
90 %!assert (pascal(3,2), [0,0,-1;0,-1,2;-1,-1,1]) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
91 %!error (pascal(3,4)) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
92 %!error (pascal(3,-2)) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
93 %!error (pascal()) |
dcde7c5a1d29
new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents:
10791
diff
changeset
|
94 %!error (pascal(1,2,3)) |