annotate scripts/special-matrix/pascal.m @ 20162:2645f9ef8c88 stable

doc: Update more docstrings to have one sentence summary as first line. Reviewed specfun, special-matrix, testfun, and time script directories. * scripts/specfun/expint.m, scripts/specfun/isprime.m, scripts/specfun/legendre.m, scripts/specfun/primes.m, scripts/specfun/reallog.m, scripts/specfun/realsqrt.m, scripts/special-matrix/gallery.m, scripts/special-matrix/hadamard.m, scripts/special-matrix/hankel.m, scripts/special-matrix/hilb.m, scripts/special-matrix/invhilb.m, scripts/special-matrix/magic.m, scripts/special-matrix/pascal.m, scripts/special-matrix/rosser.m, scripts/special-matrix/toeplitz.m, scripts/special-matrix/vander.m, scripts/special-matrix/wilkinson.m, scripts/testfun/assert.m, scripts/testfun/demo.m, scripts/testfun/example.m, scripts/testfun/fail.m, scripts/testfun/rundemos.m, scripts/testfun/runtests.m, scripts/testfun/speed.m, scripts/time/asctime.m, scripts/time/calendar.m, scripts/time/clock.m, scripts/time/ctime.m, scripts/time/datenum.m, scripts/time/datestr.m, scripts/time/datevec.m, scripts/time/etime.m, scripts/time/is_leap_year.m, scripts/time/now.m, scripts/time/weekday.m: Update more docstrings to have one sentence summary as first line.
author Rik <rik@octave.org>
date Sun, 03 May 2015 17:00:11 -0700
parents 9fc020886ae9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 1999-2015 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})
20162
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
23 ## Return the Pascal matrix of order @var{n} if @code{@var{t} = 0}.
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
24 ##
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
25 ## The default value of @var{t} is 0.
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
26 ##
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## When @code{@var{t} = 1}, return the pseudo-lower triangular
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
28 ## Cholesky@tie{}factor of the Pascal matrix (The sign of some columns may be
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ## negative). This matrix is its own inverse, that is
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## @code{pascal (@var{n}, 1) ^ 2 == eye (@var{n})}.
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ## If @code{@var{t} = -1}, return the true Cholesky@tie{}factor with strictly
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## positive values on the diagonal.
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ##
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## If @code{@var{t} = 2}, return a transposed and permuted version of
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
36 ## @code{pascal (@var{n}, 1)}, which is the cube root of the identity matrix.
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
37 ## 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
38 ##
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 12485
diff changeset
39 ## @seealso{chol}
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
40 ## @end deftypefn
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 ## Author: Peter Ekberg
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
43 ## (peda)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
44
13902
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
45 function retval = pascal (n, t = 0)
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
46
13902
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
47 if (nargin < 1 || nargin > 2)
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
48 print_usage ();
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
49 elseif (! (isscalar (n) && isscalar (t)))
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
50 error ("pascal: N and T must be scalars");
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
51 elseif (! any (t == [-1, 0, 1, 2]))
12480
139f993936af Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents: 12448
diff changeset
52 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
53 endif
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
54
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
55 retval = zeros (n);
13894
d4404589498c pascal.m: permutation compatibility with Matlab for n=2; fixed n=0 case (Bug #34365)
Vanya Sergeev <vsergeev@gmail.com>
parents: 12639
diff changeset
56 if (n > 0)
d4404589498c pascal.m: permutation compatibility with Matlab for n=2; fixed n=0 case (Bug #34365)
Vanya Sergeev <vsergeev@gmail.com>
parents: 12639
diff changeset
57 retval(:,1) = 1;
d4404589498c pascal.m: permutation compatibility with Matlab for n=2; fixed n=0 case (Bug #34365)
Vanya Sergeev <vsergeev@gmail.com>
parents: 12639
diff changeset
58 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
59
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
60 if (t == -1)
9096
5235caf89e12 fix typo in pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9094
diff changeset
61 for j = 2:n
13904
1c06f3713e0a * pascal.m: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 13902
diff changeset
62 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
63 endfor
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
64 else
9096
5235caf89e12 fix typo in pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9094
diff changeset
65 for j = 2:n
13904
1c06f3713e0a * pascal.m: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 13902
diff changeset
66 retval(j:n,j) = -cumsum (retval(j-1:n-1,j-1));
9094
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
67 endfor
2e35cfcf6a6a fix, optimize & extend pascal
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
68 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
69
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
70 if (t == 0)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
71 retval = retval*retval';
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
72 elseif (t == 2)
13902
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
73 retval = rot90 (retval, 3);
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
74 if (rem (n,2) != 1)
13907
755f6b340453 pascal.m: Use in-place assignment operator for faster negation
Rik <octave@nomad.inbox5.com>
parents: 13904
diff changeset
75 retval *= -1;
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
76 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
77 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
78
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
79 endfunction
11098
dcde7c5a1d29 new tests for special-matrix functions
John W. Eaton <jwe@octave.org>
parents: 10791
diff changeset
80
13902
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
81
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
82 %!assert (pascal (3,-1), [1,0,0;1,1,0;1,2,1])
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
83 %!assert (pascal (3,0), [1,1,1;1,2,3;1,3,6])
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
84 %!assert (pascal (3,0), pascal (3))
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
85 %!assert (pascal (3,1), [1,0,0;1,-1,0;1,-2,1])
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
86 %!assert (pascal (3,2), [1,1,1;-2,-1,0;1,0,0])
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
87 %!assert (pascal (0,2), [])
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
88
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
89 ## Test input validation
13902
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
90 %!error pascal ()
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
91 %!error pascal (1,2,3)
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
92 %!error <N and T must be scalars> pascal ([1 2])
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
93 %!error <N and T must be scalars> pascal (1, [1 2])
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
94 %!error <expecting T to be> pascal (3,-2)
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
95 %!error <expecting T to be> pascal (3,4)
3af19cfc2e0f pascal.m: Update coding style. Use in-place operations where possible.
Rik <octave@nomad.inbox5.com>
parents: 13894
diff changeset
96