Mercurial > hg > octave-lyh
annotate scripts/general/diff.m @ 9141:c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Split section into "Exponents and Logarithms" and "Utility Functions"
Use Tex in many more of the doc strings for pretty printing in pdf format.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Mon, 20 Apr 2009 17:16:09 -0700 |
parents | 1bf0ce0930be |
children | 923c7cb7f13f |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2004, 2005, 2006, 2007, |
2 ## 2008, 2009 Kurt Hornik | |
3426 | 3 ## |
3922 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
2539 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
2539 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2539 | 19 |
3369 | 20 ## -*- texinfo -*- |
4869 | 21 ## @deftypefn {Function File} {} diff (@var{x}, @var{k}, @var{dim}) |
3369 | 22 ## If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the |
23 ## vector of first differences | |
24 ## @iftex | |
25 ## @tex | |
26 ## $x_2 - x_1, \ldots{}, x_n - x_{n-1}$. | |
27 ## @end tex | |
28 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
29 ## @ifnottex |
3369 | 30 ## @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1). |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
31 ## @end ifnottex |
3426 | 32 ## |
3369 | 33 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column |
4869 | 34 ## differences along the first non-singleton dimension. |
3426 | 35 ## |
3369 | 36 ## The second argument is optional. If supplied, @code{diff (@var{x}, |
37 ## @var{k})}, where @var{k} is a nonnegative integer, returns the | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## @var{k}-th differences. It is possible that @var{k} is larger than |
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
39 ## then first non-singleton dimension of the matrix. In this case, |
4869 | 40 ## @code{diff} continues to take the differences along the next |
41 ## non-singleton dimension. | |
42 ## | |
43 ## The dimension along which to take the difference can be explicitly | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
44 ## stated with the optional variable @var{dim}. In this case the |
4869 | 45 ## @var{k}-th order differences are calculated along this dimension. |
46 ## In the case where @var{k} exceeds @code{size (@var{x}, @var{dim})} | |
47 ## then an empty matrix is returned. | |
3369 | 48 ## @end deftypefn |
2539 | 49 |
5428 | 50 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 51 ## Created: 2 February 1995 |
52 ## Adapted-By: jwe | |
53 | |
4869 | 54 function x = diff (x, k, dim) |
3426 | 55 |
4869 | 56 if (nargin < 1 || nargin > 3) |
6046 | 57 print_usage (); |
4869 | 58 endif |
59 | |
60 if (nargin < 2 || isempty(k)) | |
2539 | 61 k = 1; |
4869 | 62 else |
4030 | 63 if (! (isscalar (k) && k == round (k) && k >= 0)) |
2539 | 64 error ("diff: k must be a nonnegative integer"); |
65 elseif (k == 0) | |
66 return; | |
67 endif | |
4869 | 68 endif |
69 | |
70 nd = ndims (x); | |
71 sz = size (x); | |
72 if (nargin != 3) | |
73 %% Find the first non-singleton dimension | |
74 dim = 1; | |
75 while (dim < nd + 1 && sz (dim) == 1) | |
76 dim = dim + 1; | |
77 endwhile | |
78 if (dim > nd) | |
79 dim = 1; | |
80 endif | |
2539 | 81 else |
4869 | 82 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
83 dim < (nd + 1)) | |
84 error ("diff: dim must be an integer and valid dimension"); | |
85 endif | |
2539 | 86 endif |
3426 | 87 |
5443 | 88 if (ischar (x)) |
2539 | 89 error ("diff: symbolic differentiation not (yet) supported"); |
4869 | 90 endif |
91 | |
92 | |
93 if (nargin == 3) | |
94 if (sz (dim) <= k) | |
95 sz(dim) = 0; | |
96 x = zeros (sz); | |
2539 | 97 else |
4869 | 98 n = sz (dim); |
99 idx1 = cell (); | |
100 for i = 1:nd | |
7208 | 101 idx1{i} = 1:sz(i); |
2539 | 102 endfor |
4869 | 103 idx2 = idx1; |
104 for i = 1 : k; | |
7208 | 105 idx1{dim} = 2 : (n - i + 1); |
106 idx2{dim} = 1 : (n - i); | |
107 x = x(idx1{:}) - x(idx2{:}); | |
2539 | 108 endfor |
109 endif | |
110 else | |
4869 | 111 if (sum (sz - 1) < k) |
112 x = []; | |
113 else | |
114 idx1 = cell (); | |
115 for i = 1:nd | |
7208 | 116 idx1{i} = 1:sz(i); |
4869 | 117 endfor |
118 idx2 = idx1; | |
119 while (k) | |
120 n = sz (dim); | |
121 for i = 1 : min (k, n - 1) | |
7208 | 122 idx1{dim} = 2 : (n - i + 1); |
123 idx2{dim} = 1 : (n - i); | |
124 x = x(idx1{:}) - x(idx2{:}); | |
4869 | 125 endfor |
7208 | 126 idx1{dim} = idx2{dim} = 1; |
4869 | 127 k = k - min (k, n - 1); |
128 dim = dim + 1; | |
129 endwhile | |
130 endif | |
2539 | 131 endif |
132 | |
133 endfunction | |
7411 | 134 |
135 %!assert((diff ([1, 2, 3, 4]) == [1, 1, 1] | |
136 %! && diff ([1, 3, 7, 19], 2) == [2, 8] | |
137 %! && diff ([1, 2; 5, 4; 8, 7; 9, 6; 3, 1]) == [4, 2; 3, 3; 1, -1; -6, -5] | |
138 %! && diff ([1, 2; 5, 4; 8, 7; 9, 6; 3, 1], 3) == [-1, -5; -5, 0] | |
139 %! && isempty (diff (1)))); | |
140 | |
141 %!error diff ([1, 2; 3, 4], -1); | |
142 | |
143 %!error diff ("foo"); | |
144 | |
145 %!error diff (); | |
146 | |
147 %!error diff (1, 2, 3, 4); | |
148 |