Mercurial > hg > octave-lojdl
changeset 7322:40a17a87155e
[project @ 2007-12-18 21:32:10 by jwe]
author | jwe |
---|---|
date | Tue, 18 Dec 2007 21:34:26 +0000 |
parents | 359f464342b3 |
children | c5bf3e423b34 |
files | liboctave/ChangeLog liboctave/Sparse.cc scripts/plot/compass.m scripts/plot/feather.m scripts/plot/rose.m test/ChangeLog test/build_sparse_tests.sh |
diffstat | 7 files changed, 371 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,12 @@ +2007-12-18 David Bateman <dbateman@free.fr> + + * Sparse.cc (template <class T> Sparse<T> Sparse<T>::index + (idx_vector&, int) const): Fix case indexing of non zero scalar + stored as a sparse matrix. + (template <class T> Sparse<T> Sparse<T>::index (idx_vector&, + idx_vector&, int) const): For the non permutated indexing case, + fix link list calculation and use. + 2007-12-17 John W. Eaton <jwe@octave.org> * Array.cc (Array<T>::indexN): Correctly handle scalar indexed by
--- a/liboctave/Sparse.cc +++ b/liboctave/Sparse.cc @@ -1537,7 +1537,7 @@ for (octave_idx_type i = 0; i < n; i++) { if (i % new_nr == 0) - retval.xcidx(i % new_nr) = ic; + retval.xcidx(i / new_nr) = ic; octave_idx_type ii = idx_arg.elem (i); if (ii == 0) @@ -1946,10 +1946,9 @@ start_nodes[ii] = i; else { - struct idx_node inode = nodes[node]; - while (inode.next) - inode = *inode.next; - inode.next = nodes + i; + while (nodes[node].next) + node = nodes[node].next->i; + nodes[node].next = nodes + i; } } @@ -1974,8 +1973,8 @@ while (true) { - if (inode.i >= 0 && - idx_i.elem (inode.i) < nc) + if (idx_i.elem (inode.i) >= 0 && + idx_i.elem (inode.i) < nr) new_nzmx ++; if (inode.next == 0) break; @@ -2011,8 +2010,8 @@ while (true) { - if (inode.i >= 0 && - idx_i.elem (inode.i) < nc) + if (idx_i.elem (inode.i) >= 0 && + idx_i.elem (inode.i) < nr) { X [inode.i] = data (i); retval.xridx (kk++) = inode.i;
new file mode 100644 --- /dev/null +++ b/scripts/plot/compass.m @@ -0,0 +1,118 @@ +## Copyright (C) 2007 David Bateman +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} compass (@var{u}, @var{v}) +## @deftypefnx {Function File} {} compass (@var{z}) +## @deftypefnx {Function File} {} compass (@dots{}, @var{style}) +## @deftypefnx {Function File} {} compass (@var{h}, @dots{}) +## @deftypefnx {Function File} {@var{h} =} compass (@dots{}) +## +## Plot the @code{(@var{u}, @var{v})} components of a vector field emanating +## from the origin of a polar polt. If a single complex argument @var{z} is +## given, then @code{@var{u} = real (@var{z})} and @code{@var{v} = imag +## (@var{z})}. +## +## The style to use for the plot can be defined with a line style @var{style} +## in a similar manner to the line styles used with the @code{plot} command. +## +## The optional return value @var{h} provides a list of handles to the +## the parts of the vector field (body, arrow and marker). +## +## @example +## @group +## a = toeplitz([1;randn(9,1)],[1,randn(1,9)]); +## compass (eig (a)) +## @end group +## @end example +## +## @seealso{plot, polar, quiver, feather} +## @end deftypefn + +function retval = compass (varargin) + + [h, varargin, nargin] = __plt_get_axis_arg__ ("compass", varargin{:}); + + arrowsize = 0.25; + firstnonnumeric = Inf; + for i = 1:nargin + if (! isnumeric (varargin{i})) + firstnonnumeric = i; + break; + endif + endfor + + if (nargin < 2 || firstnonnumeric < 2) + ioff = 2; + z = varargin {1} (:) .'; + u = real (z); + v = imag (z); + else + ioff = 3; + u = varargin {1} (:) .'; + v = varargin {2} (:) .'; + endif + + line_spec = "b-"; + while (ioff <= nargin) + arg = varargin{ioff++}; + if ((isstr (arg) || iscell (arg)) && ! have_line_spec) + [linespec, valid] = __pltopt__ ("compass", arg, false); + if (valid) + line_spec = arg; + break; + else + error ("compass: invalid linespec"); + endif + else + error ("compass: unrecognized argument"); + endif + endwhile + + ## Matlab draws compass plots, with the arrow head as one continous + ## line, and each arrow separately. This is completely different than + ## quiver and quite ugly. + n = length (u); + xend = u; + xtmp = u .* (1 - arrowsize); + yend = v; + ytmp = v .* (1 - arrowsize); + x = [zeros(1, n); xend; xtmp - v * arrowsize / 3; xend; ... + xtmp + v * arrowsize / 3]; + y = [zeros(1, n); yend; ytmp + u * arrowsize / 3; yend; ... + ytmp - u * arrowsize / 3]; + [r, p] = cart2pol (x, y); + + oldh = gca (); + unwind_protect + axes (h); + newplot (); + hlist = polar (h, r, p, line_spec); + unwind_protect_cleanup + axes (oldh); + end_unwind_protect + + if (nargout > 0) + retval = hlist; + endif + +endfunction + +%!demo +%! a = toeplitz([1;randn(9,1)],[1,randn(1,9)]); +%! compass (eig (a))
new file mode 100644 --- /dev/null +++ b/scripts/plot/feather.m @@ -0,0 +1,117 @@ +## Copyright (C) 2007 David Bateman +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} feather (@var{u}, @var{v}) +## @deftypefnx {Function File} {} feather (@var{z}) +## @deftypefnx {Function File} {} feather (@dots{}, @var{style}) +## @deftypefnx {Function File} {} feather (@var{h}, @dots{}) +## @deftypefnx {Function File} {@var{h} =} feather (@dots{}) +## +## Plot the @code{(@var{u}, @var{v})} components of a vector field emanating +## from equidistant points on the x-axis. If a single complex argument +## @var{z} is given, then @code{@var{u} = real (@var{z})} and +## @code{@var{v} = imag (@var{z})}. +## +## The style to use for the plot can be defined with a line style @var{style} +## in a similar manner to the line styles used with the @code{plot} command. +## +## The optional return value @var{h} provides a list of handles to the +## the parts of the vector field (body, arrow and marker). +## +## @example +## @group +## phi = [0 : 15 : 360] * pi / 180; +## feather (sin (phi), cos (phi)) +## @end group +## @end example +## +## @seealso{plot, quiver, compass} +## @end deftypefn + +function retval = feather (varargin) + + [h, varargin, nargin] = __plt_get_axis_arg__ ("feather", varargin{:}); + + arrowsize = 0.25; + firstnonnumeric = Inf; + for i = 1:nargin + if (! isnumeric (varargin{i})) + firstnonnumeric = i; + break; + endif + endfor + + if (nargin < 2 || firstnonnumeric < 2) + ioff = 2; + z = varargin {1} (:) .'; + u = real (z); + v = imag (z); + else + ioff = 3; + u = varargin {1} (:) .'; + v = varargin {2} (:) .'; + endif + + line_spec = "b-"; + while (ioff <= nargin) + arg = varargin{ioff++}; + if ((isstr (arg) || iscell (arg)) && ! have_line_spec) + [linespec, valid] = __pltopt__ ("feather", arg, false); + if (valid) + line_spec = arg; + break; + else + error ("feather: invalid linespec"); + endif + else + error ("feather: unrecognized argument"); + endif + endwhile + + ## Matlab draws feather plots, with the arrow head as one continous + ## line, and each arrow separately. This is completely different than + ## quiver and quite ugly. + n = length (u); + xend = [1 : n] + u; + xtmp = [1 : n] + u .* (1 - arrowsize); + yend = v; + ytmp = v .* (1 - arrowsize); + x = [[1 : n]; xend; xtmp - v * arrowsize; xend; ... + xtmp + v * arrowsize]; + y = [zeros(1, n); yend; ytmp + u * arrowsize / 3; yend; ... + ytmp - u * arrowsize / 3]; + + oldh = gca (); + unwind_protect + axes (h); + newplot (); + hlist = plot (h, x, y, line_spec, [1, n], [0, 0], line_spec); + unwind_protect_cleanup + axes (oldh); + end_unwind_protect + + if (nargout > 0) + retval = hlist; + endif + +endfunction + +%!demo +%! phi = [0 : 15 : 360] * pi / 180; +%! feather (sin (phi), cos (phi))
new file mode 100644 --- /dev/null +++ b/scripts/plot/rose.m @@ -0,0 +1,107 @@ +## Copyright (C) 2007 David Bateman +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} rose (@var{th}, @var{r}) +## @deftypefnx {Function File} {} rose (@var{h}, @dots{}) +## @deftypefnx {Function File} {@var{h} =} compass (@dots{}) +## @deftypefnx {Function File} {[@var{r}, @var{th}] =} compass (@dots{}) +## +## Plot an angular histogram. With one vector argument @var{th}, plots the +## histogram with 20 angular bins. If @var{th} is a matrix, then each column +## of @var{th} produces a separate histogram. +## +## If @var{r} is given and is a scalar, then the histogram is produced with +## @var{r} bins. If @var{r} is a vector, then the center of each bin are +## defined by the values of @var{r}. +## +## The optional return value @var{h} provides a list of handles to the +## the parts of the vector field (body, arrow and marker). +## +## If two output arguments are requested, then rather than plotting the +## histogram, the polar vectors necessary to plot the histogram are +## returned. +# +## @example +## [r, t] = rose ([2*randn(1e5,1), pi + 2 * randn(1e5,1)]); +## polar (r, t); +## @end example +## +## +## @seealso{plot, compass, polar, hist} +## @end deftypefn + +function [thout, rout] = rose (varargin) + + [h, varargin, nargin] = __plt_get_axis_arg__ ((nargout > 1), "rose", + varargin{:}); + + if (nargin < 1) + print_usage (); + endif + + ## Force theta to [0,2*pi] range + th = varargin {1}; + th = atan2 (sin (th), cos (th)) + pi; + + if (nargin > 1) + x = varargin {2}; + if (isscalar (x)) + x = [0.5/x : 1/x : 1] * 2 * pi; + else + ## Force theta to [0,2*pi] range + x = atan2 (sin (x), cos (x)) + pi; + endif + else + x = [1/40 : 1/20 : 1] * 2 * pi; + endif + + [nn, xx] = hist (th, x); + xx = xx(:).'; + if (isvector (nn)) + nn = nn (:); + endif + x1 = xx(1:end-1) + diff (xx, 1) / 2; + x1 = [x1 ; x1; x1; x1](:); + th = [0; 0; x1; 2*pi ; 2*pi]; + r = zeros (4 * size (nn, 1), size (nn, 2)); + r(2:4:end, :) = nn; + r(3:4:end, :) = nn; + + if (nargout < 2) + oldh = gca (); + unwind_protect + axes (h); + newplot (); + hlist = polar (h, th, r); + unwind_protect_cleanup + axes (oldh); + end_unwind_protect + + if (nargout > 0) + thout = hlist; + endif + else + thout = th; + rout = r; + endif + +endfunction + +%!demo +%! rose ([2*randn(1e5,1), pi + 2 * randn(1e5,1)])
--- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,8 @@ +2007-12-18 David Bateman <dbateman@free.fr> + + * build_sparse_tests.sh: Add tests for indexing like a([1,1],:), + a(:,[1,1]) and sparse(42)([1,1]). + 2007-12-11 David Bateman <dbateman@free.fr> * build_sparse_tests.sh: Drop argument to Fsparse to force mutation.
--- a/test/build_sparse_tests.sh +++ b/test/build_sparse_tests.sh @@ -811,6 +811,13 @@ %!assert(as((size(as,1):-1:1),:),sparse(af((size(af,1):-1:1),:))) %!assert(as(:,(size(as,2):-1:1)),sparse(af(:,(size(af,2):-1:1)))) +%% Indexing tests +%!assert(full(as([1,1],:)), af([1,1],:)) +%!assert(full(as(:,[1,1])), af(:,[1,1])) +%!test +%! [i,j,v] = find (as); +%! assert (as(i(1),j(1))([1,1]), sparse([v(1),v(1)])) + %% Assignment test %!test %! ts=as;ts(:,:)=ts(fliplr(1:size(as,1)),:);