]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/private/__collect_constraints__.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / private / __collect_constraints__.m
1 ## Copyright (C) 2010, 2011 Olaf Till <olaf.till@uni-jena.de>
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 function [mc, vc, f_gencstr, df_gencstr, user_df] = \
17       __collect_constraints__ (cstr, do_cstep, context)
18
19   mc = vc = f_gencstr = df_gencstr = [];
20   user_df = false;
21
22   if (isempty (cstr)) return; endif
23
24   if (ismatrix (tp = cstr{1}) || isstruct (tp))
25     mc = tp;
26     vc = cstr{2};
27     if ((tp = length (cstr)) > 2)
28       f_genstr = cstr{3};
29       if (tp > 3)
30         df_gencstr = cstr{4};
31         user_df = true;
32       endif
33     endif
34   else
35     lid = 0; # no linear constraints
36     f_gencstr = cstr{1};
37     if ((len = length (cstr)) > 1)
38       if (ismatrix (c = cstr{2}) || isstruct (c))
39         lid = 2;
40       else
41         df_gencstr = c;
42         user_df = true;
43         if (len > 2)
44           lid = 3;
45         endif
46       endif
47     endif
48     if (lid)
49       mc = cstr{lid};
50       vc = cstr{lid + 1};
51     endif
52   endif
53
54   if (! isempty (f_gencstr))
55     if (ischar (f_gencstr))
56       f_gencstr = str2func (f_gencstr);
57     endif
58     f_gencstr = @ (varargin) \
59         tf_gencstr (f_gencstr, varargin{:});
60
61     if (user_df)
62       if (do_cstep)
63         error ("both complex step derivative chosen and user Jacobian function specified for %s", context);
64       endif
65       if (ischar (df_gencstr))
66         df_gencstr = str2func (df_gencstr);
67       endif
68       df_gencstr = @ (p, func, idx, hook) \
69           df_gencstr (p, idx, hook);
70     else
71       if (do_cstep)
72         df_gencstr = @ (p, func, idx, hook) jacobs (p, func, hook);
73       else
74         __dfdp__ = @ __dfdp__; # for bug #31484 (Octave <= 3.2.4)
75         df_gencstr = @ (p, func, idx, hook) __dfdp__ (p, func, hook);
76       endif
77     endif
78   endif
79
80 endfunction
81
82 function ret = tf_gencstr (f, varargin) # varargin: p[, idx[, info]]
83
84   ## necessary since user function f_gencstr might return [] or a row
85   ## vector
86
87   if (isempty (ret = f (varargin{:})))
88     ret = zeros (0, 1);
89   elseif (columns (ret) > 1)
90     ret = ret(:);
91   endif
92
93 endfunction