Mercurial > hg > octave-nkf
annotate scripts/special-matrix/toeplitz.m @ 10509:ddbd812d09aa
properly compress sparse matrices after assembly
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Apr 2010 12:57:44 +0200 |
parents | f0c3d3fc4903 |
children | 95c3e38098bf |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004, |
8920 | 2 ## 2005, 2006, 2007, 2008, 2009 John W. Eaton |
9092 | 3 ## Copyright (C) 2009 VZLU Prague |
2313 | 4 ## |
5 ## This file is part of Octave. | |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
2313 | 11 ## |
12 ## Octave is distributed in the hope that it will be useful, but | |
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 ## General Public License for more details. | |
16 ## | |
17 ## You should have received a copy of the GNU General Public License | |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
245 | 20 |
3369 | 21 ## -*- texinfo -*- |
22 ## @deftypefn {Function File} {} toeplitz (@var{c}, @var{r}) | |
23 ## Return the Toeplitz matrix constructed given the first column @var{c}, | |
24 ## and (optionally) the first row @var{r}. If the first element of @var{c} | |
25 ## is not the same as the first element of @var{r}, the first element of | |
26 ## @var{c} is used. If the second argument is omitted, the first row is | |
27 ## taken to be the same as the first column. | |
3426 | 28 ## |
5016 | 29 ## A square Toeplitz matrix has the form: |
3369 | 30 ## @tex |
31 ## $$ | |
5016 | 32 ## \left[\matrix{c_0 & r_1 & r_2 & \cdots & r_n\cr |
33 ## c_1 & c_0 & r_1 & \cdots & r_{n-1}\cr | |
34 ## c_2 & c_1 & c_0 & \cdots & r_{n-2}\cr | |
35 ## \vdots & \vdots & \vdots & \ddots & \vdots\cr | |
36 ## c_n & c_{n-1} & c_{n-2} & \ldots & c_0}\right] | |
3369 | 37 ## $$ |
38 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
39 ## @ifnottex |
3426 | 40 ## |
3369 | 41 ## @example |
42 ## @group | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
43 ## c(0) r(1) r(2) @dots{} r(n) |
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
44 ## c(1) c(0) r(1) @dots{} r(n-1) |
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
45 ## c(2) c(1) c(0) @dots{} r(n-2) |
5016 | 46 ## . , , . . |
47 ## . , , . . | |
48 ## . , , . . | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
49 ## c(n) c(n-1) c(n-2) @dots{} c(0) |
3369 | 50 ## @end group |
51 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
52 ## @end ifnottex |
5781 | 53 ## @seealso{hankel, vander, sylvester_matrix, hilb, invhilb} |
3369 | 54 ## @end deftypefn |
4 | 55 |
9092 | 56 ## Author: jwe && jh |
2314 | 57 |
2311 | 58 function retval = toeplitz (c, r) |
4 | 59 |
60 if (nargin == 1) | |
61 r = c; | |
62 elseif (nargin != 2) | |
6046 | 63 print_usage (); |
4 | 64 endif |
65 | |
9092 | 66 if (! (isvector (c) && isvector (r))) |
1518 | 67 error ("toeplitz: expecting vector arguments"); |
4 | 68 endif |
69 | |
9092 | 70 nc = length (r); |
71 nr = length (c); | |
4 | 72 |
9092 | 73 if (nr == 0 || nc == 0) |
74 ## Empty matrix. | |
75 retval = zeros (nr, nc, class (c)); | |
76 return; | |
4 | 77 endif |
78 | |
79 if (r (1) != c (1)) | |
904 | 80 warning ("toeplitz: column wins diagonal conflict"); |
4 | 81 endif |
82 | |
2303 | 83 ## If we have a single complex argument, we want to return a |
84 ## Hermitian-symmetric matrix (actually, this will really only be | |
85 ## Hermitian-symmetric if the first element of the vector is real). | |
1016 | 86 |
9092 | 87 if (nargin == 1 && iscomplex (c)) |
1016 | 88 c = conj (c); |
89 c(1) = conj (c(1)); | |
90 endif | |
91 | |
9126
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
92 if (issparse(c) && issparse(r)) |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
93 c = c(:).'; |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
94 r = r(:).'; |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
95 cidx = find(c); |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
96 ridx = find(r); |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
97 |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
98 ## Ignore the first element in r. |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
99 ridx = ridx(ridx > 1); |
4 | 100 |
9126
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
101 ## Form matrix. |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
102 retval = spdiags(repmat(c(cidx),nr,1),1-cidx,nr,nc)+... |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
103 spdiags(repmat(r(ridx),nr,1),ridx-1,nr,nc); |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
104 else |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
105 ## Concatenate data into a single column vector. |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
106 data = [r(end:-1:2)(:); c(:)]; |
4 | 107 |
9126
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
108 ## Get slices. |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
109 slices = cellslices (data, nc:-1:1, nc+nr-1:-1:nr); |
4 | 110 |
9126
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
111 ## Form matrix. |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
112 retval = horzcat (slices{:}); |
5780b3b80425
optimize toeplitz for sparse args
Jaroslav Hajek <highegg@gmail.com>
parents:
9092
diff
changeset
|
113 endif |
4 | 114 endfunction |
7411 | 115 |
116 %!assert((toeplitz (1) == 1 | |
117 %! && toeplitz ([1, 2, 3], [1; -3; -5]) == [1, -3, -5; 2, 1, -3; 3, 2, 1] | |
118 %! && toeplitz ([1, 2, 3], [1; -3i; -5i]) == [1, -3i, -5i; 2, 1, -3i; 3, 2, 1])); | |
119 | |
120 %!error toeplitz ([1, 2; 3, 4], 1); | |
121 | |
122 %!error toeplitz (); | |
123 | |
124 %!error toeplitz (1, 2, 3); | |
125 |