annotate scripts/special-matrix/pascal.m @ 12480:139f993936af

Uppercase variables in script error strings.
author Rik <octave@nomad.inbox5.com>
date Fri, 25 Feb 2011 20:12:05 -0800
parents f2c080bbd8a5
children f5a5e5252b5a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11098
diff changeset
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
3 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
5 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
9 ## your option) any later version.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
10 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
15 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
19
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
23 ## Return the Pascal matrix of order @var{n} if @code{@var{t} = 0}.
11593
1577c6f80926 Use non-breaking spaces between certain adjectives and their nouns in docstrings.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
24 ## @var{t} defaults to 0. Return lower triangular Cholesky@tie{}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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
26 ## inverse, that is @code{pascal (@var{n}, 1) ^ 2 == eye (@var{n})}.
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9096
diff changeset
27 ## If @code{@var{t} = -1}, return its absolute value. This is the
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9209
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
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
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
32 ##
12448
f2c080bbd8a5 Fix documentation bugs uncovered while rewriting munge-texi.
Rik <octave@nomad.inbox5.com>
parents: 11593
diff changeset
33 ## @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
34 ## hadamard, wilkinson, compan, rosser}
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
35 ## @end deftypefn
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
36
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
37 ## Author: Peter Ekberg
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
38 ## (peda)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
39
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
40 function retval = pascal (n, t)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
41
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
42 if (nargin > 2) || (nargin == 0)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
43 print_usage ();
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
44 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
45
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
46 if (nargin == 1)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
47 t = 0;
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
48 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
49
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
50 if (! isscalar (n) || ! isscalar (t))
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
51 error ("pascal: expecting scalar arguments, found something else");
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
52 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
53
11098
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
54 if (t < -1 || t > 2)
12480
139f993936af Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents: 12448
diff changeset
55 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
56 endif
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
57
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
58 retval = zeros (n);
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
59 retval(:,1) = 1;
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
60
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
61 if (t == -1)
9096
5235caf89e12 fix typo in pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9094
diff changeset
62 for j = 2:n
9094
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));
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
64 endfor
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
65 else
9096
5235caf89e12 fix typo in pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9094
diff changeset
66 for j = 2:n
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
67 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
68 endfor
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
69 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
70
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
71 if (t == 0)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
72 retval = retval*retval';
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
73 elseif (t == 2)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
74 retval = retval';
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
75 retval = retval(n:-1:1,:);
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
76 retval(:,n) = -retval(:,n);
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
77 retval(n,:) = -retval(n,:);
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
78 if (rem(n,2) != 1)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
79 retval = -retval;
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
80 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
81 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
82
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
83 endfunction
11098
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
84
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
85 %!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
86 %!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
87 %!assert (pascal(3,0), pascal(3))
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
88 %!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
89 %!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
90 %!error (pascal(3,4))
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
91 %!error (pascal(3,-2))
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
92 %!error (pascal())
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
93 %!error (pascal(1,2,3))