2539
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2539
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
2539
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2539
|
18 |
3369
|
19 ## -*- texinfo -*- |
4869
|
20 ## @deftypefn {Function File} {} diff (@var{x}, @var{k}, @var{dim}) |
3369
|
21 ## If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the |
|
22 ## vector of first differences |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $x_2 - x_1, \ldots{}, x_n - x_{n-1}$. |
|
26 ## @end tex |
|
27 ## @end iftex |
|
28 ## @ifinfo |
|
29 ## @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1). |
|
30 ## @end ifinfo |
3426
|
31 ## |
3369
|
32 ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column |
4869
|
33 ## differences along the first non-singleton dimension. |
3426
|
34 ## |
3369
|
35 ## The second argument is optional. If supplied, @code{diff (@var{x}, |
|
36 ## @var{k})}, where @var{k} is a nonnegative integer, returns the |
4869
|
37 ## @var{k}-th differences. It is possible that @var{k} is larger than |
|
38 ## then first non-singleton dimension of the matrix. In this case, |
|
39 ## @code{diff} continues to take the differences along the next |
|
40 ## non-singleton dimension. |
|
41 ## |
|
42 ## The dimension along which to take the difference can be explicitly |
|
43 ## stated with the optional variable @var{dim}. In this case the |
|
44 ## @var{k}-th order differences are calculated along this dimension. |
|
45 ## In the case where @var{k} exceeds @code{size (@var{x}, @var{dim})} |
|
46 ## then an empty matrix is returned. |
3369
|
47 ## @end deftypefn |
2539
|
48 |
5428
|
49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539
|
50 ## Created: 2 February 1995 |
|
51 ## Adapted-By: jwe |
|
52 |
4869
|
53 function x = diff (x, k, dim) |
3426
|
54 |
4869
|
55 if (nargin < 1 || nargin > 3) |
6046
|
56 print_usage (); |
4869
|
57 endif |
|
58 |
|
59 if (nargin < 2 || isempty(k)) |
2539
|
60 k = 1; |
4869
|
61 else |
4030
|
62 if (! (isscalar (k) && k == round (k) && k >= 0)) |
2539
|
63 error ("diff: k must be a nonnegative integer"); |
|
64 elseif (k == 0) |
|
65 return; |
|
66 endif |
4869
|
67 endif |
|
68 |
|
69 nd = ndims (x); |
|
70 sz = size (x); |
|
71 if (nargin != 3) |
|
72 %% Find the first non-singleton dimension |
|
73 dim = 1; |
|
74 while (dim < nd + 1 && sz (dim) == 1) |
|
75 dim = dim + 1; |
|
76 endwhile |
|
77 if (dim > nd) |
|
78 dim = 1; |
|
79 endif |
2539
|
80 else |
4869
|
81 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
82 dim < (nd + 1)) |
|
83 error ("diff: dim must be an integer and valid dimension"); |
|
84 endif |
2539
|
85 endif |
3426
|
86 |
5443
|
87 if (ischar (x)) |
2539
|
88 error ("diff: symbolic differentiation not (yet) supported"); |
4869
|
89 endif |
|
90 |
|
91 |
|
92 if (nargin == 3) |
|
93 if (sz (dim) <= k) |
|
94 sz(dim) = 0; |
|
95 x = zeros (sz); |
2539
|
96 else |
4869
|
97 n = sz (dim); |
|
98 idx1 = cell (); |
|
99 for i = 1:nd |
|
100 idx1 {i} = 1:sz(i); |
2539
|
101 endfor |
4869
|
102 idx2 = idx1; |
|
103 for i = 1 : k; |
|
104 idx1 {dim} = 2 : (n - i + 1); |
|
105 idx2 {dim} = 1 : (n - i); |
|
106 x = x (idx1 {:}) - x (idx2 {:}); |
2539
|
107 endfor |
|
108 endif |
|
109 else |
4869
|
110 if (sum (sz - 1) < k) |
|
111 x = []; |
|
112 else |
|
113 idx1 = cell (); |
|
114 for i = 1:nd |
|
115 idx1 {i} = 1:sz(i); |
|
116 endfor |
|
117 idx2 = idx1; |
|
118 while (k) |
|
119 n = sz (dim); |
|
120 for i = 1 : min (k, n - 1) |
|
121 idx1 {dim} = 2 : (n - i + 1); |
|
122 idx2 {dim} = 1 : (n - i); |
|
123 x = x (idx1 {:}) - x (idx2 {:}); |
|
124 endfor |
|
125 idx1 {dim} = idx2 {dim} = 1; |
|
126 k = k - min (k, n - 1); |
|
127 dim = dim + 1; |
|
128 endwhile |
|
129 endif |
2539
|
130 endif |
|
131 |
|
132 endfunction |