Mercurial > hg > octave-lyh
changeset 4869:b92d59213e63
[project @ 2004-04-21 17:03:02 by jwe]
author | jwe |
---|---|
date | Wed, 21 Apr 2004 17:03:02 +0000 |
parents | 0d7b436d0e87 |
children | 2cbc6f37b0c2 |
files | doc/interpreter/matrix.txi doc/interpreter/preface.txi scripts/ChangeLog scripts/general/diff.m scripts/general/fliplr.m scripts/general/flipud.m scripts/general/rot90.m src/ChangeLog src/DLD-FUNCTIONS/fft.cc src/DLD-FUNCTIONS/minmax.cc src/pr-output.cc test/octave.test/io/io.exp test/octave.test/matrix/diff-5.m test/octave.test/system/system.exp |
diffstat | 14 files changed, 160 insertions(+), 71 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/interpreter/matrix.txi +++ b/doc/interpreter/matrix.txi @@ -73,8 +73,12 @@ @DOCSTRING(flipud) +@DOCSTRING(flipdim) + @DOCSTRING(rot90) +@DOCSTRING(rotdim) + @DOCSTRING(cat) @DOCSTRING(horzcat)
--- a/doc/interpreter/preface.txi +++ b/doc/interpreter/preface.txi @@ -71,10 +71,11 @@ @code{async_system}. @item -David Bateman @email{dbateman@@free.fr} converted several built-in -functions to use Lapack instead of Linpack, and split the -functionality of @file{load-save.cc} out into the @code{octave_value} -classes. +David Bateman @email{dbateman@@free.fr} improved the sort and min/max +functions, made many functions N-d aware, converted several built-in +functions to use Lapack instead of Linpack, split the functionality of +@file{load-save.cc} out into the @code{octave_value} classes, and has +contributed in many other ways. @item Karl Berry @email{karl@@cs.umb.edu} wrote the @code{kpathsea} library
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,19 @@ +2004-04-21 David Bateman <dbateman@free.fr> + + * general/diff.m: Make the code N-d array aware. Allow an + optional argument to define the dimension along which to perform + the differences and allow the order of the differences to be larger + than the dimension itself. + + * general/rotdim.m: New function for rotation of an N-d array in an + arbitrary plane. + + * general/flipdim.m: New function to flip an N-d array about an + arbitrary axis. + + * general/rot90.m, general/fliplr.m, general/flipud.m: Limit the + use of these functions to 1- and 2-d arrays. + 2004-04-16 John W. Eaton <jwe@octave.org> * elfun/gcd.m: Delete.
--- a/scripts/general/diff.m +++ b/scripts/general/diff.m @@ -18,7 +18,7 @@ ## 02111-1307, USA. ## -*- texinfo -*- -## @deftypefn {Function File} {} diff (@var{x}, @var{k}) +## @deftypefn {Function File} {} diff (@var{x}, @var{k}, @var{dim}) ## If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the ## vector of first differences ## @iftex @@ -31,53 +31,103 @@ ## @end ifinfo ## ## If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column -## differences. +## differences along the first non-singleton dimension. ## ## The second argument is optional. If supplied, @code{diff (@var{x}, ## @var{k})}, where @var{k} is a nonnegative integer, returns the -## @var{k}-th differences. +## @var{k}-th differences. It is possible that @var{k} is larger than +## then first non-singleton dimension of the matrix. In this case, +## @code{diff} continues to take the differences along the next +## non-singleton dimension. +## +## The dimension along which to take the difference can be explicitly +## stated with the optional variable @var{dim}. In this case the +## @var{k}-th order differences are calculated along this dimension. +## In the case where @var{k} exceeds @code{size (@var{x}, @var{dim})} +## then an empty matrix is returned. ## @end deftypefn ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> ## Created: 2 February 1995 ## Adapted-By: jwe -function x = diff (x, k) +function x = diff (x, k, dim) - if (nargin == 1) + if (nargin < 1 || nargin > 3) + usage ("diff (x, k"); + endif + + if (nargin < 2 || isempty(k)) k = 1; - elseif (nargin == 2) + else if (! (isscalar (k) && k == round (k) && k >= 0)) error ("diff: k must be a nonnegative integer"); elseif (k == 0) return; endif + endif + + nd = ndims (x); + sz = size (x); + if (nargin != 3) + %% Find the first non-singleton dimension + dim = 1; + while (dim < nd + 1 && sz (dim) == 1) + dim = dim + 1; + endwhile + if (dim > nd) + dim = 1; + endif else - usage ("diff (x, k"); + if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && + dim < (nd + 1)) + error ("diff: dim must be an integer and valid dimension"); + endif endif if (isstr (x)) error ("diff: symbolic differentiation not (yet) supported"); - elseif (isvector (x)) - n = length (x); - if (n <= k) - x = []; + endif + + + if (nargin == 3) + if (sz (dim) <= k) + sz(dim) = 0; + x = zeros (sz); else - for i = 1 : k - x = x (2 : (n - i + 1)) - x (1 : (n - i)); + n = sz (dim); + idx1 = cell (); + for i = 1:nd + idx1 {i} = 1:sz(i); endfor - endif - elseif (ismatrix (x)) - n = rows (x); - if (n <= k) - x = []; - else - for i = 1 : k - x = x (2 : (n - i + 1), :) - x (1: (n - i), :); + idx2 = idx1; + for i = 1 : k; + idx1 {dim} = 2 : (n - i + 1); + idx2 {dim} = 1 : (n - i); + x = x (idx1 {:}) - x (idx2 {:}); endfor endif else - x = []; + if (sum (sz - 1) < k) + x = []; + else + idx1 = cell (); + for i = 1:nd + idx1 {i} = 1:sz(i); + endfor + idx2 = idx1; + while (k) + n = sz (dim); + for i = 1 : min (k, n - 1) + idx1 {dim} = 2 : (n - i + 1); + idx2 {dim} = 1 : (n - i); + x = x (idx1 {:}) - x (idx2 {:}); + endfor + idx1 {dim} = idx2 {dim} = 1; + k = k - min (k, n - 1); + dim = dim + 1; + endwhile + endif endif endfunction
--- a/scripts/general/fliplr.m +++ b/scripts/general/fliplr.m @@ -30,7 +30,11 @@ ## @end group ## @end example ## @end deftypefn -## @seealso{flipud and rot90} +## +## Due to the difficult of define which axis about which to flip the +## matrix @code{fliplr} only work with 2-D arrays. To flip N-D arrays +## use @code{flipdim} instead. +## @seealso{flipud, flipdim, rot90 and rotdim} ## Author: jwe @@ -40,6 +44,10 @@ usage ("fliplr (x)"); endif + if (ndims (x) > 2) + error ("fliplr: Only works with 2-D arrays") + endif + nc = columns (x); y = x (:, nc:-1:1);
--- a/scripts/general/flipud.m +++ b/scripts/general/flipud.m @@ -29,8 +29,12 @@ ## 1 2 ## @end group ## @end example +## +## Due to the difficulty of defining which axis about which to flip the +## matrix @code{flipud} only work with 2-d arrays. To flip N-d arrays +## use @code{flipdim} instead. ## @end deftypefn -## @seealso{fliplr and rot90} +## @seealso{fliplr, flipdim, rot90 and rotdim} ## Author: jwe @@ -40,6 +44,10 @@ usage ("flipud (x)"); endif + if (ndims (x) > 2) + error ("flipud: Only works with 2-d arrays") + endif + nr = rows (x); y = x (nr:-1:1, :);
--- a/scripts/general/rot90.m +++ b/scripts/general/rot90.m @@ -46,26 +46,36 @@ ## rot90 ([1, 2; 3, 4], 7) ## @end group ## @end example +## +## Due to the difficulty of defining an axis about which to rotate the +## matrix @code{rot90} only work with 2-D arrays. To rotate N-d arrays +## use @code{rotdim} instead. ## @end deftypefn -## @seealso{flipud and fliplr} +## @seealso{rotdim, flipud, fliplr and flipdim} ## Author: jwe function y = rot90 (x, k) - if (nargin < 2) - k = 1; - endif + if (nargin == 1 || nargin == 2) + if (nargin < 2) + k = 1; + endif - if (imag (k) != 0 || fix (k) != k) - error ("rot90: k must be an integer"); - endif + if (ndims (x) > 2) + error ("rot90: Only works with 2-D arrays") + endif - if (nargin == 1 || nargin == 2) + if (imag (k) != 0 || fix (k) != k) + error ("rot90: k must be an integer"); + endif + k = rem (k, 4); + if (k < 0) k = k + 4; endif + if (k == 0) y = x; elseif (k == 1)
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2004-04-21 David Bateman <dbateman@free.fr> + + * DLD_FUNCTIONS/fft.cc(do_fft): Correctly initialize the variable dim + for scalar arguments. + + * DLD-FUNCTIONS/minmax.cc: Handle single vector arg correctly. + 2004-04-20 John W. Eaton <jwe@octave.org> * ls-mat-ascii.cc (read_mat_ascii_data): Prepend "X" to keywords.
--- a/src/DLD-FUNCTIONS/fft.cc +++ b/src/DLD-FUNCTIONS/fft.cc @@ -95,12 +95,18 @@ return retval; if (dim < 0) - for (int i = 0; i < dims.length (); i++) - if ( dims(i) > 1) - { - dim = i; - break; - } + { + for (int i = 0; i < dims.length (); i++) + if ( dims(i) > 1) + { + dim = i; + break; + } + + // And if the first argument is scalar? + if (dim < 0) + dim = 1; + } if (n_points < 0) n_points = dims (dim);
--- a/src/DLD-FUNCTIONS/minmax.cc +++ b/src/DLD-FUNCTIONS/minmax.cc @@ -100,20 +100,6 @@ \ bool single_arg = (nargin == 1) || arg2.is_empty(); \ \ - if (single_arg) \ - { \ - dv(dim) = 1; \ - int n_dims = dv.length (); \ - for (int i = n_dims; i > 1; i--) \ - { \ - if (dv(i-1) == 1) \ - n_dims--; \ - else \ - break; \ - } \ - dv.resize (n_dims); \ - } \ - \ if (single_arg && (nargout == 1 || nargout == 0)) \ { \ if (arg1.is_real_type ()) \ @@ -123,7 +109,6 @@ if (! error_state) \ { \ NDArray n = m. FCN (dim); \ - n.resize (dv); \ retval(0) = n; \ } \ } \ @@ -134,7 +119,6 @@ if (! error_state) \ { \ ComplexNDArray n = m. FCN (dim); \ - n.resize (dv); \ retval(0) = n; \ } \ } \ @@ -152,7 +136,6 @@ if (! error_state) \ { \ NDArray n = m. FCN (index, dim); \ - n.resize (dv); \ retval(0) = n; \ } \ } \ @@ -163,7 +146,6 @@ if (! error_state) \ { \ ComplexNDArray n = m. FCN (index, dim); \ - n.resize (dv); \ retval(0) = n; \ } \ } \
--- a/src/pr-output.cc +++ b/src/pr-output.cc @@ -2142,17 +2142,14 @@ Display the value of @var{x} on the stream @var{fid}. For example,\n\ \n\ @example\n\ -disp (stdout, \"The value of pi is:\"), disp (stdout, pi)\n\ +fdisp (stdout, \"The value of pi is:\"), fdisp (stdout, pi)\n\ \n\ @print{} the value of pi is:\n\ @print{} 3.1416\n\ @end example\n\ \n\ @noindent\n\ -Note that the output from @code{disp} always ends with a newline.\n\ -\n\ -If an output value is requested, @code{disp} prints nothing and\n\ -returns the formatted output in a string.\n\ +Note that the output from @code{fdisp} always ends with a newline.\n\ @end deftypefn\n\ @seealso{disp}") {
--- a/test/octave.test/io/io.exp +++ b/test/octave.test/io/io.exp @@ -71,7 +71,7 @@ do_test sprintf-3.m set test fopen-1 -set prog_output "^ans = 1" +set prog_output "ans = 1" do_test fopen-1.m set test fopen-2 @@ -83,7 +83,7 @@ do_test fopen-3.m set test fopen-4 -set prog_output "^error:.*" +set prog_output "error:.*" do_test fopen-4.m set test fopen-5
--- a/test/octave.test/matrix/diff-5.m +++ b/test/octave.test/matrix/diff-5.m @@ -1,1 +1,1 @@ -diff (1, 2, 3) +diff (1, 2, 3, 4)
--- a/test/octave.test/system/system.exp +++ b/test/octave.test/system/system.exp @@ -156,7 +156,7 @@ do_test usleep-3.m set test rename-1 -set prog_output "^ans = 1" +set prog_output "ans = 1" do_test rename-1.m set test rename-2 @@ -168,7 +168,7 @@ do_test rename-3.m set test unlink-1 -set prog_output "^ans = 1" +set prog_output "ans = 1" do_test unlink-1.m set test unlink-2 @@ -212,7 +212,7 @@ do_test rmdir-2.m set test umask-1 -set prog_output "^ans = 1" +set prog_output "ans = 1" do_test umask-1.m set test umask-2