octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Some more functions for Octave core


From: David Bateman
Subject: Some more functions for Octave core
Date: Mon, 03 Sep 2007 11:38:38 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

I attach a patch with a couple of extra functions that exist in Matlab
2007b in the core (ie: mode, structfun and run). Note that the mode
function works better with sparse matrices with the patch for the sparse
sort I sent last night.

http://www.nabble.com/forum/ViewPost.jtp?post=12454315&framed=y

Regards
David

-- 
David Bateman                                address@hidden
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary

*** ./doc/interpreter/container.txi.orig18      2007-09-03 10:53:38.462934929 
+0200
--- ./doc/interpreter/container.txi     2007-09-03 10:53:41.766770177 +0200
***************
*** 200,205 ****
--- 200,206 ----
  * Structure Arrays::
  * Creating Structures::
  * Manipulating Structures::
+ * Processing Data in Structures::
  @end menu
  
  @node Structure Arrays
***************
*** 395,404 ****
  
  @DOCSTRING(getfield)
  
- @DOCSTRING(struct2cell)
- 
  @DOCSTRING(substruct)
  
  @node Cell Arrays
  @section Cell Arrays
  @cindex cell arrays
--- 396,418 ----
  
  @DOCSTRING(getfield)
  
  @DOCSTRING(substruct)
  
+ @node Processing Data in Structures
+ @subsection Processing Data in Structures
+ 
+ The simpliest way to process data in a structure is within a @code{for}
+ loop or othe means of iterating over the fields. A similar effect can be
+ achieved with the @code{structfun} function, where a user defined
+ function is applied to eacg field of the structure.
+ 
+ @DOCSTRING(structfun)
+ 
+ Alternatively, to process the data in a strcuture, the structure might
+ be converted to another type of container before being treated.
+ 
+ @DOCSTRING(struct2cell)
+ 
  @node Cell Arrays
  @section Cell Arrays
  @cindex cell arrays
*** ./doc/interpreter/stats.txi.orig18  2007-09-03 10:54:38.150952636 +0200
--- ./doc/interpreter/stats.txi 2007-09-03 10:54:40.355842241 +0200
***************
*** 52,57 ****
--- 52,59 ----
  
  @DOCSTRING(var)
  
+ @DOCSTRING(mode)
+ 
  @DOCSTRING(cov)
  
  @DOCSTRING(cor)
*** ./doc/interpreter/eval.txi.orig18   2007-09-03 11:13:49.359321478 +0200
--- ./doc/interpreter/eval.txi  2007-09-03 11:13:52.590155264 +0200
***************
*** 75,80 ****
--- 75,85 ----
  
  @DOCSTRING(feval)
  
+ A similar function @code{run} exists for calling user script files, that
+ are not necessarily on the user path
+ 
+ @DOCSTRING(run)
+ 
  @node Evaluation in a Different Context
  @section Evaluation in a Different Context
  
*** ./scripts/general/Makefile.in.orig18        2007-09-03 10:48:00.966155680 
+0200
--- ./scripts/general/Makefile.in       2007-09-03 10:48:21.059123910 +0200
***************
*** 30,36 ****
    nargchk.m nextpow2.m nthroot.m num2str.m perror.m pol2cart.m \
    polyarea.m postpad.m prepad.m quadl.m randperm.m rat.m rem.m \
    repmat.m rot90.m rotdim.m shift.m shiftdim.m sortrows.m \
!   sph2cart.m strerror.m sub2ind.m trapz.m tril.m triu.m
  
  DISTFILES = $(addprefix $(srcdir)/, Makefile.in $(SOURCES))
  
--- 30,36 ----
    nargchk.m nextpow2.m nthroot.m num2str.m perror.m pol2cart.m \
    polyarea.m postpad.m prepad.m quadl.m randperm.m rat.m rem.m \
    repmat.m rot90.m rotdim.m shift.m shiftdim.m sortrows.m \
!   sph2cart.m strerror.m structfun.m sub2ind.m trapz.m tril.m triu.m
  
  DISTFILES = $(addprefix $(srcdir)/, Makefile.in $(SOURCES))
  
*** ./scripts/general/structfun.m.orig18        2007-09-03 10:47:58.791267331 
+0200
--- ./scripts/general/structfun.m       2007-09-03 10:47:53.242552157 +0200
***************
*** 0 ****
--- 1,87 ----
+ ## 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 2, 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, write to the Free
+ ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ ## 02110-1301, USA.
+ 
+ ## -*- texinfo -*-
+ ## @deftypefn {Function File} {} structfun (@var{func}, @var{s})
+ ## @deftypefnx {Function File} address@hidden, @var{b}] =} structfun (@dots{})
+ ## @deftypefnx {Function File} {} structfun (@dots{}, 'ErrorHandler', 
@var{errfunc})
+ ## @deftypefnx {Function File} {} structfun (@dots{}, 'UniformOutput', 
@var{val})
+ ## 
+ ## Evaluate the function named @var{name} on the fields of the structure
+ ## @var{s}. The fields of @var{s} are passed the the function @var{func}
+ ## individually.
+ ##
+ ## @code{structfun} accepts an arbitrary function @var{func} in the form of 
+ ## an inline function, function handle, or the name of a function (in a 
+ ## character string). In the case of a character string argument, the 
+ ## function must accept a single argument named @var{x}, and it must return 
+ ## a string value. If the function returns more than one argument, they are
+ ## returned as separate output variables.
+ ##
+ ## If the param 'UniformOutput' is set to true (the default), then the 
function
+ ## must return either a single element which will be concatenated into the
+ ## return value. If 'UniformOutput is false, the outputs placed in a structure
+ ## with the same fieldnames as the input structure.
+ ## 
+ ## @example
+ ## @group
+ ## s.name1 = "John Smith"; 
+ ## s.name2 = "Jill Jones"; 
+ ## structfun (@address@hidden regexp (x, '(\w+)$', 'matches')@address@hidden, 
s, 
+ ##            'UniformOutput', false)
+ ## @end group
+ ## @end example
+ ## 
+ ## Given the parameter 'ErrorHandler', then @var{errfunc} defines a function 
to
+ ## call in case @var{func} generates an error. The form of the function is
+ ## 
+ ## @example
+ ## function address@hidden = errfunc (@var{se}, @dots{})
+ ## @end example
+ ## 
+ ## where there is an additional input argument to @var{errfunc} relative to
+ ## @var{func}, given by @var{se}. This is a structure with the elements
+ ## 'identifier', 'message' and 'index', giving respectively the error
+ ## identifier, the error message, and the index into the input arguments
+ ## of the element that caused the error.
+ ## @seealso{cellfun, arrayfun}
+ ## @end deftypefn
+ 
+ function varargout = structfun (fun, s, varargin);
+   if (nargin < 2)
+     print_usage ();
+   endif
+ 
+   varargout = cell (max ([nargout, 1]), 1);
+   [varargout{:}] = cellfun (fun, struct2cell (s), varargin {:});
+ 
+   if (iscell (varargout{1}))
+     [varargout{:}] = cell2struct (varargout{1}, fieldnames(s), 1);
+   endif
+ endfunction
+ 
+ 
+ %!test
+ %! s.name1 = "John Smith"; 
+ %! s.name2 = "Jill Jones"; 
+ %! l.name1 = "Smith";
+ %! l.name2 = "Jones";
+ %! o = structfun (@(x) regexp (x, '(\w+)$', 'matches'){1}, s, 
+ %!              'UniformOutput', false);
+ %! assert (o, l);
*** ./scripts/miscellaneous/run.m.orig18        2007-09-03 10:47:13.544588808 
+0200
--- ./scripts/miscellaneous/run.m       2007-09-03 10:47:08.982822714 +0200
***************
*** 0 ****
--- 1,56 ----
+ ## 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 2, 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, write to the Free
+ ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ ## 02110-1301, USA.
+ 
+ ## -*- texinfo -*-
+ ## @deftypefn {Function File} {} run (@var{f})
+ ## @deftypefnx {Command} {} run @var{f}
+ ## Run scripts in the current workspace that are not necessarily on the
+ ## path. If @var{f} is the script to run, including its path, then @code{run}
+ ## change the directory to the directory where @var{f} is found. @code{run}
+ ## then executes the script, and returns to the original directory.
+ ## @seealso{system}
+ ## @end deftypefn
+ 
+ ## PKG_ADD: mark_as_commnd run
+ 
+ function run (s)
+   [d, f, ext] = fileparts (s);
+   if (! isempty (d))
+     if (exist (d, "dir"))
+       wd = pwd ();
+       unwind_protect
+       cd (d);
+       if (! exist (s, "file") ||  ! strcmp (ext, ".m"))
+         error ("run: file must exist and be a valid Octave script file");
+       endif
+       evalin ("caller", [f, ";"], "rethrow (lasterror ())");
+       unwind_protect_cleanup
+       cd (wd);
+       end_unwind_protect
+     else
+       error ("run: the path %s doesn't exist", d);
+     endif
+   else
+     if (exist (script, "file"))
+       evalin ("caller", [script, ";"], "rethrow (lasterror ())");
+     else
+       error ("run: %s not found", s);
+     endif
+   endif
+ endfunction
*** ./scripts/miscellaneous/Makefile.in.orig18  2007-09-03 10:47:19.614277542 
+0200
--- ./scripts/miscellaneous/Makefile.in 2007-09-03 10:47:37.500360027 +0200
***************
*** 27,33 ****
    inputname.m ispc.m isunix.m license.m list_primes.m ls.m \
    ls_command.m menu.m mex.m mexext.m mkoctfile.m movefile.m \
    news.m orderfields.m pack.m paren.m parseparams.m \
!   semicolon.m setfield.m single.m substruct.m tar.m \
    tempdir.m tempname.m texas_lotto.m unix.m unpack.m untar.m \
    unzip.m ver.m version.m warning_ids.m xor.m zip.m
  
--- 27,33 ----
    inputname.m ispc.m isunix.m license.m list_primes.m ls.m \
    ls_command.m menu.m mex.m mexext.m mkoctfile.m movefile.m \
    news.m orderfields.m pack.m paren.m parseparams.m \
!   run.m semicolon.m setfield.m single.m substruct.m tar.m \
    tempdir.m tempname.m texas_lotto.m unix.m unpack.m untar.m \
    unzip.m ver.m version.m warning_ids.m xor.m zip.m
  
*** ./scripts/statistics/base/Makefile.in.orig18        2007-09-03 
10:46:08.865902541 +0200
--- ./scripts/statistics/base/Makefile.in       2007-09-03 10:46:26.293010268 
+0200
***************
*** 22,28 ****
  
  SOURCES = center.m cloglog.m cor.m corrcoef.m cov.m cut.m gls.m \
    iqr.m kendall.m kurtosis.m logit.m mahalanobis.m mean.m meansq.m \
!   median.m moment.m ols.m ppplot.m probit.m qqplot.m range.m \
    ranks.m run_count.m skewness.m spearman.m statistics.m std.m \
    studentize.m table.m values.m var.m
  
--- 22,28 ----
  
  SOURCES = center.m cloglog.m cor.m corrcoef.m cov.m cut.m gls.m \
    iqr.m kendall.m kurtosis.m logit.m mahalanobis.m mean.m meansq.m \
!   median.m mode.m moment.m ols.m ppplot.m probit.m qqplot.m range.m \
    ranks.m run_count.m skewness.m spearman.m statistics.m std.m \
    studentize.m table.m values.m var.m
  
2007-09-01  David Bateman  <address@hidden>

        * interpreter/container.txi: Document struct2cell.
        * interpreter/stats.txi: Document mode.
        * interpreter/eval.txi: Document run.

2007-09-01  David Bateman  <address@hidden>

        * general/structfun.m: New function.
        * general/Makefile.in (SOURCES): Add it to sources.
        * miscellaneous/run.m: New function.
        * miscellaneous/Makefile.in (SOURCES): Add it to sources.
        * statistics/base/mode.m: New function.
        * statistics/base//Makefile.in (SOURCES): Add it to sources.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]