Mercurial > hg > octave-nkf
annotate scripts/special-matrix/vander.m @ 9209:923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
spellchecked all .txi and .texi files.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 17 May 2009 12:18:06 -0700 |
parents | c309e028185e |
children | f0c3d3fc4903 |
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 -*- |
9137
eebc7f8e7398
extend vander to allow specified number of columns
Jaroslav Hajek <highegg@gmail.com>
parents:
9104
diff
changeset
|
22 ## @deftypefn {Function File} {} vander (@var{c}, @var{n}) |
3369 | 23 ## 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
|
24 ## 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
|
25 ## otherwise, @var{n} is taken to be equal to the length of @var{c}. |
3426 | 26 ## |
5016 | 27 ## A Vandermonde matrix has the form: |
3369 | 28 ## @iftex |
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 | |
37 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
38 ## @ifnottex |
3426 | 39 ## |
3369 | 40 ## @example |
41 ## @group | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
42 ## 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
|
43 ## c(2)^(n-1) @dots{} c(2)^2 c(2) 1 |
5016 | 44 ## . . . . . |
45 ## . . . . . | |
46 ## . . . . . | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
47 ## c(n)^(n-1) @dots{} c(n)^2 c(n) 1 |
3369 | 48 ## @end group |
49 ## @end example | |
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 |