]> Creatis software - CreaPhase.git/blob - octave_packages/linear-algebra-2.2.0/circulant_eig.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / linear-algebra-2.2.0 / circulant_eig.m
1 ## Copyright (C) 2012 Nir Krakauer <nkrakauer@ccny.cuny.edu>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn{Function File} {@var{lambda} =} circulant_eig (@var{v})
18 ## @deftypefnx{Function File} {[@var{vs}, @var{lambda}] =} circulant_eig (@var{v})
19 ##
20 ## Fast, compact calculation of eigenvalues and eigenvectors of a circulant matrix@*
21 ## Given an @var{n}*1 vector @var{v}, return the eigenvalues @var{lambda} and optionally eigenvectors @var{vs} of the @var{n}*@var{n} circulant matrix @var{C} that has @var{v} as its first column
22 ##
23 ## Theoretically same as @code{eig(make_circulant_matrix(v))}, but many fewer computations; does not form @var{C} explicitly
24 ##
25 ## Reference: Robert M. Gray, Toeplitz and Circulant Matrices: A Review, Now Publishers, http://ee.stanford.edu/~gray/toeplitz.pdf, Chapter 3
26 ##
27 ## @seealso{circulant_make_matrix, circulant_matrix_vector_product, circulant_inv}
28 ## @end deftypefn
29
30 function [a, b] = circulant_eig (v)
31
32   ## FIXME when warning for broadcastin is turned off by default, this
33   ## unwind_protect block could be removed
34
35   ## we are using broadcasting on the code below so we turn off the
36   ## warnings but will restore to previous state at the end
37   bc_warn = warning ("query", "Octave:broadcast");
38   unwind_protect
39     warning ("off", "Octave:broadcast");
40
41     #find the eigenvalues
42     n = numel(v);
43     lambda = ones(n, 1);
44     s = (0:(n-1));
45     lambda = sum(v .* exp(-2*pi*i*s'*s/n))';
46
47     if nargout < 2
48       a = lambda;
49       return
50     endif
51
52     #find the eigenvectors (which in fact do not depend on v)
53     a = exp(-2*i*pi*s'*s/n) / sqrt(n);
54     b = diag(lambda);
55   unwind_protect_cleanup
56     ## restore broadcats warning status
57     warning (bc_warn.state, "Octave:broadcast");
58   end_unwind_protect
59
60 endfunction
61
62 %!shared v,C,vs,lambda
63 %! v = [1 2 3]';
64 %! C = circulant_make_matrix(v);
65 %! [vs lambda] = circulant_eig(v);
66 %!assert (vs*lambda, C*vs, 100*eps);