annotate scripts/special-matrix/hankel.m @ 20170:af2b7695f1c4 draft default tip @

gallery.m: clean and vectorize part of qmult. * scripts/special-matrix/gallery.m: make qmult() follow Octave guidelines, vectorize last for loop in qmult().
author Antonio Pino Robles <data.script93@gmail.com>
date Thu, 28 May 2015 18:32:47 +0200
parents 4197fc428c7d
children 2645f9ef8c88
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: 19593
diff changeset
1 ## Copyright (C) 1993-2015 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## 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: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## 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: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 4
diff changeset
18
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
19 ## -*- texinfo -*-
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
20 ## @deftypefn {Function File} {} hankel (@var{c})
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
21 ## @deftypefnx {Function File} {} hankel (@var{c}, @var{r})
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
22 ## Return the Hankel matrix constructed from the first column @var{c}, and
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
23 ## (optionally) the last row @var{r}. If the last element of @var{c} is
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
24 ## not the same as the first element of @var{r}, the last element of
4922
cee8d88cf619 [project @ 2004-07-30 19:10:16 by jwe]
jwe
parents: 3426
diff changeset
25 ## @var{c} is used. If the second argument is omitted, it is assumed to
cee8d88cf619 [project @ 2004-07-30 19:10:16 by jwe]
jwe
parents: 3426
diff changeset
26 ## be a vector of zeros with the same size as @var{c}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
27 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
28 ## A Hankel matrix formed from an m-vector @var{c}, and an n-vector
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
29 ## @var{r}, has the elements
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
30 ## @tex
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
31 ## $$
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
32 ## H(i, j) = \cases{c_{i+j-1},&$i+j-1\le m$;\cr r_{i+j-m},&otherwise.\cr}
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
33 ## $$
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
34 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7411
diff changeset
35 ## @ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
36 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
37 ## @example
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
38 ## @group
4922
cee8d88cf619 [project @ 2004-07-30 19:10:16 by jwe]
jwe
parents: 3426
diff changeset
39 ## H(i,j) = c(i+j-1), i+j-1 <= m;
cee8d88cf619 [project @ 2004-07-30 19:10:16 by jwe]
jwe
parents: 3426
diff changeset
40 ## H(i,j) = r(i+j-m), otherwise
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
41 ## @end group
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
42 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
43 ##
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7411
diff changeset
44 ## @end ifnottex
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
45 ## @seealso{hadamard, toeplitz}
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3140
diff changeset
46 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
47
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
48 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
49
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
50 function retval = hankel (c, r)
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
51
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
52 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5731
diff changeset
53 print_usage ();
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
54 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
55
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2314
diff changeset
56 if (nargin == 1)
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
57
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
58 if (! isvector (c))
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
59 error ("hankel: C must be a vector");
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
60 endif
1396
1ff9a86e2ad5 [project @ 1995-09-14 19:46:14 by jwe]
jwe
parents: 1315
diff changeset
61
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
62 nr = length (c);
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
63 nc = nr;
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
64 data = [c(:) ; zeros(nr, 1)];
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
65
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
66 else
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
67
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
68 if (! (isvector (c) && isvector (r)))
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
69 error ("hankel: C and R must be vectors");
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
70 elseif (r(1) != c(end))
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
71 warning ("hankel: column wins anti-diagonal conflict");
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
72 endif
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
73
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
74 nr = length (c);
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
75 nc = length (r);
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
76 data = [c(:) ; r(2:end)(:)];
1396
1ff9a86e2ad5 [project @ 1995-09-14 19:46:14 by jwe]
jwe
parents: 1315
diff changeset
77
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
78 endif
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
79
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
80 slices = cellslices (data, 1:nc, nr:1:nc+nr-1);
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
81 retval = horzcat (slices{:});
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
82
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
83 endfunction
5731
c7d5a534afa5 [project @ 2006-04-03 19:33:26 by jwe]
jwe
parents: 5642
diff changeset
84
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
85
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
86 %!assert (hankel (1), [1])
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
87 %!assert (hankel ([1, 2]), [1, 2; 2, 0])
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
88 %!assert (hankel ([1, 2], [2; -1; -3]), [1, 2, -1; 2, -1, -3])
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
89 %!assert (hankel (1:3), [1,2,3;2,3,0;3,0,0])
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
90 %!assert (hankel (1:3,3:6), [1,2,3,4;2,3,4,5;3,4,5,6])
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
91 %!assert (hankel (1:3,3:4), [1,2;2,3;3,4])
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
92 %!assert (hankel (1:3,4:6), [1,2,3;2,3,5;3,5,6])
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
93
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
94 %!error hankel ()
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
95 %!error hankel (1, 2, 3)
13888
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
96 %!error <C must be a vector> hankel ([1, 2; 3, 4])
c78ac846fcbc hankel.m: Recode for 3.5X speedup
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
97 %!error <C and R must be vectors> hankel (1:4, [1, 2; 3, 4])
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
98