Mercurial > hg > octave-nkf
annotate scripts/general/diff.m @ 12121:87237a866c71 release-3-2-x
this branch is no longer maintained and is closed for further development
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 22 Jan 2011 01:00:54 -0500 |
parents | f0c3d3fc4903 |
children |
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 ## @tex | |
25 ## $x_2 - x_1, \ldots{}, x_n - x_{n-1}$. | |
26 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
27 ## @ifnottex |
3369 | 28 ## @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
|
29 ## @end ifnottex |
3426 | 30 ## |
3369 | 31 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column |
4869 | 32 ## differences along the first non-singleton dimension. |
3426 | 33 ## |
3369 | 34 ## The second argument is optional. If supplied, @code{diff (@var{x}, |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
35 ## @var{k})}, where @var{k} is a non-negative integer, returns the |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
36 ## @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
|
37 ## then first non-singleton dimension of the matrix. In this case, |
4869 | 38 ## @code{diff} continues to take the differences along the next |
39 ## non-singleton dimension. | |
40 ## | |
41 ## 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
|
42 ## stated with the optional variable @var{dim}. In this case the |
4869 | 43 ## @var{k}-th order differences are calculated along this dimension. |
44 ## In the case where @var{k} exceeds @code{size (@var{x}, @var{dim})} | |
45 ## then an empty matrix is returned. | |
3369 | 46 ## @end deftypefn |
2539 | 47 |
5428 | 48 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 49 ## Created: 2 February 1995 |
50 ## Adapted-By: jwe | |
51 | |
4869 | 52 function x = diff (x, k, dim) |
3426 | 53 |
4869 | 54 if (nargin < 1 || nargin > 3) |
6046 | 55 print_usage (); |
4869 | 56 endif |
57 | |
58 if (nargin < 2 || isempty(k)) | |
2539 | 59 k = 1; |
4869 | 60 else |
4030 | 61 if (! (isscalar (k) && k == round (k) && k >= 0)) |
2539 | 62 error ("diff: k must be a nonnegative integer"); |
63 elseif (k == 0) | |
64 return; | |
65 endif | |
4869 | 66 endif |
67 | |
68 nd = ndims (x); | |
69 sz = size (x); | |
70 if (nargin != 3) | |
71 %% Find the first non-singleton dimension | |
72 dim = 1; | |
73 while (dim < nd + 1 && sz (dim) == 1) | |
74 dim = dim + 1; | |
75 endwhile | |
76 if (dim > nd) | |
77 dim = 1; | |
78 endif | |
2539 | 79 else |
4869 | 80 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
81 dim < (nd + 1)) | |
82 error ("diff: dim must be an integer and valid dimension"); | |
83 endif | |
2539 | 84 endif |
3426 | 85 |
5443 | 86 if (ischar (x)) |
2539 | 87 error ("diff: symbolic differentiation not (yet) supported"); |
4869 | 88 endif |
89 | |
90 | |
91 if (nargin == 3) | |
92 if (sz (dim) <= k) | |
93 sz(dim) = 0; | |
94 x = zeros (sz); | |
2539 | 95 else |
4869 | 96 n = sz (dim); |
97 idx1 = cell (); | |
98 for i = 1:nd | |
7208 | 99 idx1{i} = 1:sz(i); |
2539 | 100 endfor |
4869 | 101 idx2 = idx1; |
102 for i = 1 : k; | |
7208 | 103 idx1{dim} = 2 : (n - i + 1); |
104 idx2{dim} = 1 : (n - i); | |
105 x = x(idx1{:}) - x(idx2{:}); | |
2539 | 106 endfor |
107 endif | |
108 else | |
4869 | 109 if (sum (sz - 1) < k) |
110 x = []; | |
111 else | |
112 idx1 = cell (); | |
113 for i = 1:nd | |
7208 | 114 idx1{i} = 1:sz(i); |
4869 | 115 endfor |
116 idx2 = idx1; | |
117 while (k) | |
118 n = sz (dim); | |
119 for i = 1 : min (k, n - 1) | |
7208 | 120 idx1{dim} = 2 : (n - i + 1); |
121 idx2{dim} = 1 : (n - i); | |
122 x = x(idx1{:}) - x(idx2{:}); | |
4869 | 123 endfor |
7208 | 124 idx1{dim} = idx2{dim} = 1; |
4869 | 125 k = k - min (k, n - 1); |
126 dim = dim + 1; | |
127 endwhile | |
128 endif | |
2539 | 129 endif |
130 | |
131 endfunction | |
7411 | 132 |
133 %!assert((diff ([1, 2, 3, 4]) == [1, 1, 1] | |
134 %! && diff ([1, 3, 7, 19], 2) == [2, 8] | |
135 %! && diff ([1, 2; 5, 4; 8, 7; 9, 6; 3, 1]) == [4, 2; 3, 3; 1, -1; -6, -5] | |
136 %! && diff ([1, 2; 5, 4; 8, 7; 9, 6; 3, 1], 3) == [-1, -5; -5, 0] | |
137 %! && isempty (diff (1)))); | |
138 | |
139 %!error diff ([1, 2; 3, 4], -1); | |
140 | |
141 %!error diff ("foo"); | |
142 | |
143 %!error diff (); | |
144 | |
145 %!error diff (1, 2, 3, 4); | |
146 |