~ubuntu-branches/ubuntu/saucy/octave-miscellaneous/saucy-proposed

« back to all changes in this revision

Viewing changes to inst/map.m

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot, Sébastien Villemot, Rafael Laboissiere
  • Date: 2012-04-02 13:20:23 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120402132023-t41xaso7sl5cex90
Tags: 1.1.0-1
[ Sébastien Villemot ]
* Imported Upstream version 1.1.0
* debian/patches/match-cell-array.patch: remove patch (applied upstream)
* debian/patches/waitbar-rename.patch: remove patch (applied upstream)
* debian/patches/no-flexml.patch: remove obsolete patch, flex no longer used
  (Closes: #666294)
* debian/copyright: reflect upstream changes
* debian/octave-miscellaneous.docs: remove, no more docs in the package
* debian/clean: remove obsolete file
* debian/rules: remove hack for wrong permissions in upstream tarball

[ Rafael Laboissiere ]
* debian/watch: Use the SourceForge redirector

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
## Copyright (C) 2003 Tomer Altman
2
 
## Copyright (C) 2007 Muthiah Annamalai
3
 
##
4
 
## This program is free software; you can redistribute it and/or
5
 
## modify it under the terms of the GNU General Public
6
 
## License as published by the Free Software Foundation;
7
 
## either version 2, or (at your option) any later version.
8
 
##
9
 
## This software is distributed in the hope that it will be useful,
10
 
## but WITHOUT ANY WARRANTY; without even the implied
11
 
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
 
## PURPOSE.  See the GNU General Public License for more
 
1
## Copyright (C) 2003 Tomer Altman <taltman@lbl.gov>
 
2
## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@mavs.uta.edu>
 
3
## Copyright (C) 2012 Carnë Draug <carandraug+dev@gmail.com>
 
4
## Copyright (C) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
 
5
##
 
6
## This program is free software; you can redistribute it and/or modify it under
 
7
## the terms of the GNU General Public License as published by the Free Software
 
8
## Foundation; either version 3 of the License, or (at your option) any later
 
9
## version.
 
10
##
 
11
## This program is distributed in the hope that it will be useful, but WITHOUT
 
12
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
14
## details.
14
15
##
15
 
## You should have received a copy of the GNU General Public
16
 
## License along with this software; see the file COPYING.  If not,
17
 
## see <http://www.gnu.org/licenses/>.
18
 
 
 
16
## You should have received a copy of the GNU General Public License along with
 
17
## this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 
20
19
## -*- texinfo -*-
21
 
## @deftypefn{Function File} {@var{result} = } map ( fun_handle, varargin ) 
22
 
## @cindex  
23
 
## 
24
 
## usage: result = map ( FUN_HANDLE, ARG1, ... )
 
20
## @deftypefn{Function File} {@var{result} =} map (@var{function}, @var{iterable}, @dots{})
 
21
## Apply @var{function} to every item of @var{iterable} and return the results.
25
22
##
26
23
## @code{map}, like Lisp's ( & numerous other language's ) function for
27
24
## iterating the result of a function applied to each of the data
36
33
## @example
37
34
##
38
35
## octave> A
39
 
## A 
 
36
## A
40
37
## @{
41
38
##   [1,1] = 0.0096243
42
39
##   [2,1] = 0.82781
60
57
##   [2,2] = 0.84645
61
58
## @}
62
59
## @end example
63
 
## @seealso{reduce, match, apply}
 
60
## @seealso{reduce, match}
64
61
## @end deftypefn
65
62
 
66
 
## Author: Tomer Altman
67
 
## Keywords: map matrix cell 
68
 
## Maintainer: Tomer Altman
69
 
## Created: November 15, 2003
70
 
## Version: 0.1
71
 
 
72
63
function return_type = map (fun_handle, data_struct, varargin)
73
 
  if (nargin >= 1)
74
 
    try
75
 
      if (ischar (fun_handle))
76
 
        fun_handle = eval (strcat ("@", fun_handle));
77
 
      end
78
 
      fstr = typeinfo (fun_handle);
79
 
    catch
80
 
      error ("Error: Cannot find function handle, or funtion doesnt exist");
81
 
    end_try_catch
 
64
 
 
65
  persistent warned = false;
 
66
  if (! warned)
 
67
    warned = true;
 
68
    warning ("Octave:deprecated-function",
 
69
             "map has been deprecated, and will be removed in the future. Use `arrayfun' or `cellfun' instead.");
82
70
  endif
83
71
 
84
72
  if (nargin < 2)
85
 
    error ("map: incorrect number of arguments; expecting at least two");
86
 
  elseif (strcmp (fstr, "function handle") == 0)
87
 
    error ("map: first argument is not a valid function handle");
 
73
    print_usage;
88
74
  elseif (!(isnumeric (data_struct) || iscell (data_struct)))
89
 
    error ("map: second argument must be either a matrix or a cell object");
90
 
  endif
91
 
 
92
 
  [rows, cols] = size (data_struct);
93
 
  typecell = 0;
94
 
  
 
75
    error ("second argument must be either a matrix or a cell object");
 
76
  endif
 
77
 
 
78
  if (isa (fun_handle, "function_handle"))
 
79
    ##do nothing
 
80
  elseif (ischar (fun_handle))
 
81
    fun_handle = str2func (fun_handle);
 
82
  else
 
83
    error ("fun_handle must either be a function handle or the name of a function");
 
84
  endif
 
85
 
95
86
  if (iscell (data_struct))
96
 
    typecell = 1;
97
 
    return_type = cell (rows, cols);
98
 
  else
99
 
    typecell = 0;
100
 
    return_type = zeros (rows, cols);
101
 
  endif
102
 
  
103
 
  otherdata = length (varargin);
104
 
  val = cell (1, otherdata+1);
105
 
  val (:) = 0;
106
 
 
107
 
  if (typecell)
108
 
    if (otherdata >= 1)
109
 
      for i = 1:rows      
110
 
        for j = 1:cols
111
 
          val {1} = data_struct {i, j};
112
 
          for idx = 2:otherdata+1
113
 
            val {idx} = varargin {idx-1}{i,j};
114
 
          endfor
115
 
          return_type {i,j} = apply (fun_handle, val);
116
 
        endfor
117
 
      endfor
118
 
    else
119
 
      for i = 1:rows      
120
 
        for j = 1:cols      
121
 
          return_type {i,j} = fun_handle (data_struct {i,j});
122
 
        endfor
123
 
      endfor
124
 
    endif
125
 
  else
126
 
    if (otherdata >= 1)
127
 
      for i = 1:rows
128
 
        for j = 1:cols
129
 
          val {1} = data_struct (i,j);
130
 
          for idx = 2:otherdata+1
131
 
            val {idx} = varargin {idx-1}(i,j);
132
 
          endfor
133
 
          return_type (i, j) = apply (fun_handle, val);
134
 
        endfor
135
 
      endfor
136
 
    else
137
 
      for i = 1:rows
138
 
        for j = 1:cols
139
 
          return_type (i, j) = fun_handle (data_struct (i, j));
140
 
        endfor
141
 
      endfor
142
 
    endif
 
87
    return_type = cellfun (fun_handle, data_struct,varargin{:});
 
88
  else
 
89
    return_type = arrayfun (fun_handle, data_struct,varargin{:});
143
90
  endif
144
91
 
145
92
endfunction
146
93
 
147
 
%!test
148
 
%! assert(map(@min,[1 2 3 4 5],[5 4 3 2 1]), [1 2 3 2 1])
149
 
%! assert(map(@min,rand(1,5),[0 0 0 0 0]), [0 0 0 0 0])
150
 
%! assert(map(@(x,y) (sin(x).^2 + cos(y).^2),-pi:0.5:+pi,-pi:0.5:+pi),ones(1,13))
151
 
 
 
94
%!assert(map(@min,[1 2 3 4 5],[5 4 3 2 1]), [1 2 3 2 1])
 
95
%!assert(map(@min,rand(1,5),[0 0 0 0 0]), [0 0 0 0 0])
 
96
%!assert(map(@(x,y) (sin(x).^2 + cos(y).^2),-pi:0.5:+pi,-pi:0.5:+pi),ones(1,13))