Mercurial > hg > octave-lyh
annotate scripts/special-matrix/vander.m @ 9104:e0250e2b60ed
optimize vander
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 08 Apr 2009 13:16:23 +0200 |
parents | 853f96e8008f |
children | eebc7f8e7398 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, |
8920 | 2 ## 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton |
9104 | 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} {} vander (@var{c}) | |
23 ## Return the Vandermonde matrix whose next to last column is @var{c}. | |
3426 | 24 ## |
5016 | 25 ## A Vandermonde matrix has the form: |
3369 | 26 ## @iftex |
27 ## @tex | |
28 ## $$ | |
5016 | 29 ## \left[\matrix{c_1^{n-1} & \cdots & c_1^2 & c_1 & 1 \cr |
30 ## c_2^{n-1} & \cdots & c_2^2 & c_2 & 1 \cr | |
31 ## \vdots & \ddots & \vdots & \vdots & \vdots \cr | |
32 ## c_n^{n-1} & \cdots & c_n^2 & c_n & 1 }\right] | |
3369 | 33 ## $$ |
34 ## @end tex | |
35 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
36 ## @ifnottex |
3426 | 37 ## |
3369 | 38 ## @example |
39 ## @group | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## c(1)^(n-1) @dots{} c(1)^2 c(1) 1 |
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
41 ## c(2)^(n-1) @dots{} c(2)^2 c(2) 1 |
5016 | 42 ## . . . . . |
43 ## . . . . . | |
44 ## . . . . . | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
45 ## c(n)^(n-1) @dots{} c(n)^2 c(n) 1 |
3369 | 46 ## @end group |
47 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
48 ## @end ifnottex |
5642 | 49 ## @seealso{hankel, sylvester_matrix, hilb, invhilb, toeplitz} |
3369 | 50 ## @end deftypefn |
4 | 51 |
2314 | 52 ## Author: jwe |
53 | |
2311 | 54 function retval = vander (c) |
4 | 55 |
56 if (nargin != 1) | |
6046 | 57 print_usage (); |
4 | 58 endif |
59 | |
4030 | 60 if (isvector (c)) |
4 | 61 n = length (c); |
9104 | 62 retval = zeros (n, n, class (c)); |
63 ## avoiding many ^s appears to be faster for n >= 100. | |
64 d = 1; | |
65 c = c(:); | |
66 for i = n:-1:1 | |
67 retval(:,i) = d; | |
68 d = c .* d; | |
69 endfor | |
4 | 70 else |
71 error ("vander: argument must be a vector"); | |
72 endif | |
73 | |
74 endfunction | |
7387 | 75 |
76 %!test | |
77 %! c = [0,1,2,3]; | |
78 %! expect = [0,0,0,1; 1,1,1,1; 8,4,2,1; 27,9,3,1]; | |
79 %! result = vander(c); | |
80 %! assert(expect, result); | |
7411 | 81 |
82 %!assert((vander (1) == 1 && vander ([1, 2, 3]) == vander ([1; 2; 3]) | |
83 %! && vander ([1, 2, 3]) == [1, 1, 1; 4, 2, 1; 9, 3, 1] | |
84 %! && vander ([1, 2, 3]*i) == [-1, i, 1; -4, 2i, 1; -9, 3i, 1])); | |
85 | |
86 %!error vander ([1, 2; 3, 4]); | |
87 | |
88 %!error vander (); | |
89 | |
90 %!error vander (1, 2); | |
91 |