1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## Test whether d2_min() functions correctly
18 ## Gives a simple quadratic programming problem (function ff below).
20 ## Sets a ok variable to 1 in case of success, 0 in case of failure
22 ## If a variables "verbose" is set, then some comments are output.
26 if ! exist ("verbose"), verbose = 0; end
29 printf ("\n Testing d2_min () on a quadratic programming problem\n\n");
32 P = 10+floor(30*rand(1)) ; # Nparams
33 R = P+floor(30*rand(1)) ; # Nobses
42 obses = obsmat*truep ;
43 if noise, obses = adnois(obses,noise); end
50 v = msq (obses - obsmat*x ) ;
53 function [v,dv,d2v] = d2ff(x) # Return pseudo-inverse
56 er = -obses + obsmat*x ;
59 d2v = pinv (obsmat'*obsmat ) ;
62 function [v,dv,d2v] = d2ff_2(x) # Return 2nd derivs, not pseudo-inv
65 er = -obses + obsmat*x ;
68 d2v = obsmat'*obsmat ;
73 ## Returns the cputime since last call to 'mytic'.
76 persistent last_mytic = 0 ;
82 ## s = msq(x) - Mean squared value, ignoring nans
84 ## s == mean(x(:).^2) , but ignores NaN's
89 s = mean(x(find(!isnan(x))).^2);
98 ctl = nan*zeros(1,5); ctl(5) = 1;
101 printf ("Going to call d2_min\n");
104 [xlev,vlev,nev] = d2_min ("ff","d2ff",xinit,ctl);
108 printf("d2_min should find in one iteration + one more to check\n");
109 printf(["d2_min : niter=%-4d nev=%-4d nobs=%-4d,nparams=%-4d\n",...
110 " time=%-8.3g errx=%-8.3g minv=%-8.3g\n"],...
111 nev([2,1]),R,P,tlev,max(abs(xlev-truep )),vlev);
118 printf ("Too many iterations for this function\n");
123 printf ("Ok: single iteration (%i)\n",cnt);
127 if max (abs(xlev-truep )) > sqrt (eps),
129 printf ("Error is too big : %-8.3g\n", max (abs (xlev-truep)));
134 printf ("Ok: single error amplitude (%i)\n",cnt);
141 printf ("Going to call d2_min() \n");
144 [xlev,vlev,nev] = d2_min("ff","d2ff_2",xinit) ;
148 printf("d2_min should find in one iteration + one more to check\n");
149 printf(["d2_min : niter=%-4d nev=%-4d nobs=%-4d,nparams=%-4d\n",...
150 " time=%-8.3g errx=%-8.3g minv=%-8.3g\n"],...
151 nev([2,1]),R,P,tlev,max(abs(xlev-truep )),vlev);
157 printf ("Too many iterations for this function\n");
162 printf ("Ok: single iteration (%i)\n",cnt);
166 if max (abs(xlev-truep )) > sqrt (eps),
168 printf ("Error is too big : %-8.3g\n", max (abs (xlev-truep)));
173 printf ("Ok: single error amplitude (%i)\n",cnt);
179 printf ("All tests ok\n");
181 printf ("Some tests failed\n");