Mercurial > hg > octave-lyh
annotate scripts/special-matrix/vander.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
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 -*- |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
22 ## @deftypefn {Function File} {} vander (@var{c}) |
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
23 ## @deftypefnx {Function File} {} vander (@var{c}, @var{n}) |
3369 | 24 ## Return the Vandermonde matrix whose next to last column is @var{c}. |
9137
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
25 ## If @var{n} is specified, it determines the number of columns; |
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
26 ## otherwise, @var{n} is taken to be equal to the length of @var{c}. |
3426 | 27 ## |
5016 | 28 ## A Vandermonde matrix has the form: |
3369 | 29 ## @tex |
30 ## $$ | |
5016 | 31 ## \left[\matrix{c_1^{n-1} & \cdots & c_1^2 & c_1 & 1 \cr |
32 ## c_2^{n-1} & \cdots & c_2^2 & c_2 & 1 \cr | |
33 ## \vdots & \ddots & \vdots & \vdots & \vdots \cr | |
34 ## c_n^{n-1} & \cdots & c_n^2 & c_n & 1 }\right] | |
3369 | 35 ## $$ |
36 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
37 ## @ifnottex |
3426 | 38 ## |
3369 | 39 ## @example |
40 ## @group | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
41 ## 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
|
42 ## c(2)^(n-1) @dots{} c(2)^2 c(2) 1 |
5016 | 43 ## . . . . . |
44 ## . . . . . | |
45 ## . . . . . | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
46 ## c(n)^(n-1) @dots{} c(n)^2 c(n) 1 |
3369 | 47 ## @end group |
48 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
49 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
50 ## @end ifnottex |
5642 | 51 ## @seealso{hankel, sylvester_matrix, hilb, invhilb, toeplitz} |
3369 | 52 ## @end deftypefn |
4 | 53 |
2314 | 54 ## Author: jwe |
55 | |
9137
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
56 function retval = vander (c, n) |
4 | 57 |
9137
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
58 if (nargin == 1) |
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
59 n = length (c); |
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
60 elseif (nargin != 2) |
6046 | 61 print_usage (); |
4 | 62 endif |
63 | |
4030 | 64 if (isvector (c)) |
9137
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
65 retval = zeros (length (c), n, class (c)); |
9104 | 66 ## avoiding many ^s appears to be faster for n >= 100. |
67 d = 1; | |
68 c = c(:); | |
69 for i = n:-1:1 | |
70 retval(:,i) = d; | |
71 d = c .* d; | |
72 endfor | |
4 | 73 else |
74 error ("vander: argument must be a vector"); | |
75 endif | |
76 | |
77 endfunction | |
7387 | 78 |
79 %!test | |
80 %! c = [0,1,2,3]; | |
81 %! expect = [0,0,0,1; 1,1,1,1; 8,4,2,1; 27,9,3,1]; | |
82 %! result = vander(c); | |
83 %! assert(expect, result); | |
7411 | 84 |
85 %!assert((vander (1) == 1 && vander ([1, 2, 3]) == vander ([1; 2; 3]) | |
86 %! && vander ([1, 2, 3]) == [1, 1, 1; 4, 2, 1; 9, 3, 1] | |
87 %! && vander ([1, 2, 3]*i) == [-1, i, 1; -4, 2i, 1; -9, 3i, 1])); | |
88 | |
9140 | 89 %!assert(vander (2, 3), [4, 2, 1]) |
90 %!assert(vander ([2, 3], 3), [4, 2, 1; 9, 3, 1]) | |
91 | |
7411 | 92 %!error vander ([1, 2; 3, 4]); |
93 | |
94 %!error vander (); | |
95 | |
9140 | 96 %!error vander (1, 2, 3); |
7411 | 97 |