]> Creatis software - CreaPhase.git/blob - octave_packages/linear-algebra-2.2.0/circulant_matrix_vector_product.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / linear-algebra-2.2.0 / circulant_matrix_vector_product.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{y} =} circulant_matrix_vector_product (@var{v}, @var{x})
18 ##
19 ## Fast, compact calculation of the product of a circulant matrix with a vector@*
20 ## Given @var{n}*1 vectors @var{v} and @var{x}, return the matrix-vector product @var{y} = @var{C}@var{x}, where @var{C} is the @var{n}*@var{n} circulant matrix that has @var{v} as its first column
21 ##
22 ## Theoretically the same as @code{make_circulant_matrix(x) * v}, but does not form @var{C} explicitly; uses the discrete Fourier transform
23 ##
24 ## Because of roundoff, the returned @var{y} may have a small imaginary component even if @var{v} and @var{x} are real (use @code{real(y)} to remedy this)
25 ##
26 ## Reference: Gene H. Golub and Charles F. Van Loan, Matrix Computations, 3rd Ed., Section 4.7.7
27 ##
28 ## @seealso{circulant_make_matrix, circulant_eig, circulant_inv}
29 ## @end deftypefn
30
31 function y = circulant_matrix_vector_product (v, x)
32
33   xf = fft(x);
34   vf = fft(v);
35   z = vf .* xf;
36   y = ifft(z);
37
38 endfunction
39
40 %!shared v,x
41 %! v = [1 2 3]'; x = [2 5 6]';
42 %!assert (circulant_matrix_vector_product(v, x), circulant_make_matrix(v)*x, eps);