alglib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/History.txt +7 -0
  2. data/Manifest.txt +253 -0
  3. data/README.txt +33 -0
  4. data/Rakefile +27 -0
  5. data/ext/Rakefile +24 -0
  6. data/ext/alglib.i +24 -0
  7. data/ext/alglib/Makefile +157 -0
  8. data/ext/alglib/airyf.cpp +372 -0
  9. data/ext/alglib/airyf.h +81 -0
  10. data/ext/alglib/alglib.cpp +8558 -0
  11. data/ext/alglib/alglib_util.cpp +19 -0
  12. data/ext/alglib/alglib_util.h +14 -0
  13. data/ext/alglib/ap.cpp +877 -0
  14. data/ext/alglib/ap.english.html +364 -0
  15. data/ext/alglib/ap.h +666 -0
  16. data/ext/alglib/ap.russian.html +442 -0
  17. data/ext/alglib/apvt.h +754 -0
  18. data/ext/alglib/bdss.cpp +1500 -0
  19. data/ext/alglib/bdss.h +251 -0
  20. data/ext/alglib/bdsvd.cpp +1339 -0
  21. data/ext/alglib/bdsvd.h +164 -0
  22. data/ext/alglib/bessel.cpp +1226 -0
  23. data/ext/alglib/bessel.h +331 -0
  24. data/ext/alglib/betaf.cpp +105 -0
  25. data/ext/alglib/betaf.h +74 -0
  26. data/ext/alglib/bidiagonal.cpp +1328 -0
  27. data/ext/alglib/bidiagonal.h +350 -0
  28. data/ext/alglib/binomialdistr.cpp +247 -0
  29. data/ext/alglib/binomialdistr.h +153 -0
  30. data/ext/alglib/blas.cpp +576 -0
  31. data/ext/alglib/blas.h +132 -0
  32. data/ext/alglib/cblas.cpp +226 -0
  33. data/ext/alglib/cblas.h +57 -0
  34. data/ext/alglib/cdet.cpp +138 -0
  35. data/ext/alglib/cdet.h +92 -0
  36. data/ext/alglib/chebyshev.cpp +216 -0
  37. data/ext/alglib/chebyshev.h +76 -0
  38. data/ext/alglib/chisquaredistr.cpp +157 -0
  39. data/ext/alglib/chisquaredistr.h +144 -0
  40. data/ext/alglib/cholesky.cpp +285 -0
  41. data/ext/alglib/cholesky.h +86 -0
  42. data/ext/alglib/cinverse.cpp +298 -0
  43. data/ext/alglib/cinverse.h +111 -0
  44. data/ext/alglib/clu.cpp +337 -0
  45. data/ext/alglib/clu.h +120 -0
  46. data/ext/alglib/correlation.cpp +280 -0
  47. data/ext/alglib/correlation.h +77 -0
  48. data/ext/alglib/correlationtests.cpp +726 -0
  49. data/ext/alglib/correlationtests.h +134 -0
  50. data/ext/alglib/crcond.cpp +826 -0
  51. data/ext/alglib/crcond.h +148 -0
  52. data/ext/alglib/creflections.cpp +310 -0
  53. data/ext/alglib/creflections.h +165 -0
  54. data/ext/alglib/csolve.cpp +312 -0
  55. data/ext/alglib/csolve.h +99 -0
  56. data/ext/alglib/ctrinverse.cpp +387 -0
  57. data/ext/alglib/ctrinverse.h +98 -0
  58. data/ext/alglib/ctrlinsolve.cpp +297 -0
  59. data/ext/alglib/ctrlinsolve.h +81 -0
  60. data/ext/alglib/dawson.cpp +234 -0
  61. data/ext/alglib/dawson.h +74 -0
  62. data/ext/alglib/descriptivestatistics.cpp +436 -0
  63. data/ext/alglib/descriptivestatistics.h +112 -0
  64. data/ext/alglib/det.cpp +140 -0
  65. data/ext/alglib/det.h +94 -0
  66. data/ext/alglib/dforest.cpp +1819 -0
  67. data/ext/alglib/dforest.h +316 -0
  68. data/ext/alglib/elliptic.cpp +497 -0
  69. data/ext/alglib/elliptic.h +217 -0
  70. data/ext/alglib/estnorm.cpp +429 -0
  71. data/ext/alglib/estnorm.h +107 -0
  72. data/ext/alglib/expintegrals.cpp +422 -0
  73. data/ext/alglib/expintegrals.h +108 -0
  74. data/ext/alglib/faq.english.html +258 -0
  75. data/ext/alglib/faq.russian.html +272 -0
  76. data/ext/alglib/fdistr.cpp +202 -0
  77. data/ext/alglib/fdistr.h +163 -0
  78. data/ext/alglib/fresnel.cpp +211 -0
  79. data/ext/alglib/fresnel.h +91 -0
  80. data/ext/alglib/gammaf.cpp +338 -0
  81. data/ext/alglib/gammaf.h +104 -0
  82. data/ext/alglib/gqgengauss.cpp +235 -0
  83. data/ext/alglib/gqgengauss.h +92 -0
  84. data/ext/alglib/gqgenhermite.cpp +268 -0
  85. data/ext/alglib/gqgenhermite.h +63 -0
  86. data/ext/alglib/gqgenjacobi.cpp +297 -0
  87. data/ext/alglib/gqgenjacobi.h +72 -0
  88. data/ext/alglib/gqgenlaguerre.cpp +265 -0
  89. data/ext/alglib/gqgenlaguerre.h +69 -0
  90. data/ext/alglib/gqgenlegendre.cpp +300 -0
  91. data/ext/alglib/gqgenlegendre.h +62 -0
  92. data/ext/alglib/gqgenlobatto.cpp +305 -0
  93. data/ext/alglib/gqgenlobatto.h +97 -0
  94. data/ext/alglib/gqgenradau.cpp +232 -0
  95. data/ext/alglib/gqgenradau.h +95 -0
  96. data/ext/alglib/hbisinv.cpp +480 -0
  97. data/ext/alglib/hbisinv.h +183 -0
  98. data/ext/alglib/hblas.cpp +228 -0
  99. data/ext/alglib/hblas.h +64 -0
  100. data/ext/alglib/hcholesky.cpp +339 -0
  101. data/ext/alglib/hcholesky.h +91 -0
  102. data/ext/alglib/hermite.cpp +114 -0
  103. data/ext/alglib/hermite.h +49 -0
  104. data/ext/alglib/hessenberg.cpp +370 -0
  105. data/ext/alglib/hessenberg.h +152 -0
  106. data/ext/alglib/hevd.cpp +247 -0
  107. data/ext/alglib/hevd.h +107 -0
  108. data/ext/alglib/hsschur.cpp +1316 -0
  109. data/ext/alglib/hsschur.h +108 -0
  110. data/ext/alglib/htridiagonal.cpp +734 -0
  111. data/ext/alglib/htridiagonal.h +180 -0
  112. data/ext/alglib/ialglib.cpp +6 -0
  113. data/ext/alglib/ialglib.h +9 -0
  114. data/ext/alglib/ibetaf.cpp +960 -0
  115. data/ext/alglib/ibetaf.h +125 -0
  116. data/ext/alglib/igammaf.cpp +430 -0
  117. data/ext/alglib/igammaf.h +157 -0
  118. data/ext/alglib/inv.cpp +274 -0
  119. data/ext/alglib/inv.h +115 -0
  120. data/ext/alglib/inverseupdate.cpp +480 -0
  121. data/ext/alglib/inverseupdate.h +185 -0
  122. data/ext/alglib/jacobianelliptic.cpp +164 -0
  123. data/ext/alglib/jacobianelliptic.h +94 -0
  124. data/ext/alglib/jarquebera.cpp +2271 -0
  125. data/ext/alglib/jarquebera.h +80 -0
  126. data/ext/alglib/kmeans.cpp +356 -0
  127. data/ext/alglib/kmeans.h +76 -0
  128. data/ext/alglib/laguerre.cpp +94 -0
  129. data/ext/alglib/laguerre.h +48 -0
  130. data/ext/alglib/lbfgs.cpp +1167 -0
  131. data/ext/alglib/lbfgs.h +218 -0
  132. data/ext/alglib/lda.cpp +434 -0
  133. data/ext/alglib/lda.h +133 -0
  134. data/ext/alglib/ldlt.cpp +1130 -0
  135. data/ext/alglib/ldlt.h +124 -0
  136. data/ext/alglib/leastsquares.cpp +1252 -0
  137. data/ext/alglib/leastsquares.h +290 -0
  138. data/ext/alglib/legendre.cpp +107 -0
  139. data/ext/alglib/legendre.h +49 -0
  140. data/ext/alglib/linreg.cpp +1185 -0
  141. data/ext/alglib/linreg.h +380 -0
  142. data/ext/alglib/logit.cpp +1523 -0
  143. data/ext/alglib/logit.h +333 -0
  144. data/ext/alglib/lq.cpp +399 -0
  145. data/ext/alglib/lq.h +160 -0
  146. data/ext/alglib/lu.cpp +462 -0
  147. data/ext/alglib/lu.h +119 -0
  148. data/ext/alglib/mannwhitneyu.cpp +4490 -0
  149. data/ext/alglib/mannwhitneyu.h +115 -0
  150. data/ext/alglib/minlm.cpp +918 -0
  151. data/ext/alglib/minlm.h +312 -0
  152. data/ext/alglib/mlpbase.cpp +3375 -0
  153. data/ext/alglib/mlpbase.h +589 -0
  154. data/ext/alglib/mlpe.cpp +1369 -0
  155. data/ext/alglib/mlpe.h +552 -0
  156. data/ext/alglib/mlptrain.cpp +1056 -0
  157. data/ext/alglib/mlptrain.h +283 -0
  158. data/ext/alglib/nearunityunit.cpp +91 -0
  159. data/ext/alglib/nearunityunit.h +17 -0
  160. data/ext/alglib/normaldistr.cpp +377 -0
  161. data/ext/alglib/normaldistr.h +175 -0
  162. data/ext/alglib/nsevd.cpp +1869 -0
  163. data/ext/alglib/nsevd.h +140 -0
  164. data/ext/alglib/pca.cpp +168 -0
  165. data/ext/alglib/pca.h +87 -0
  166. data/ext/alglib/poissondistr.cpp +143 -0
  167. data/ext/alglib/poissondistr.h +130 -0
  168. data/ext/alglib/polinterpolation.cpp +685 -0
  169. data/ext/alglib/polinterpolation.h +206 -0
  170. data/ext/alglib/psif.cpp +173 -0
  171. data/ext/alglib/psif.h +88 -0
  172. data/ext/alglib/qr.cpp +414 -0
  173. data/ext/alglib/qr.h +168 -0
  174. data/ext/alglib/ratinterpolation.cpp +134 -0
  175. data/ext/alglib/ratinterpolation.h +72 -0
  176. data/ext/alglib/rcond.cpp +705 -0
  177. data/ext/alglib/rcond.h +140 -0
  178. data/ext/alglib/reflections.cpp +504 -0
  179. data/ext/alglib/reflections.h +165 -0
  180. data/ext/alglib/rotations.cpp +473 -0
  181. data/ext/alglib/rotations.h +128 -0
  182. data/ext/alglib/rsolve.cpp +221 -0
  183. data/ext/alglib/rsolve.h +99 -0
  184. data/ext/alglib/sbisinv.cpp +217 -0
  185. data/ext/alglib/sbisinv.h +171 -0
  186. data/ext/alglib/sblas.cpp +185 -0
  187. data/ext/alglib/sblas.h +64 -0
  188. data/ext/alglib/schur.cpp +156 -0
  189. data/ext/alglib/schur.h +102 -0
  190. data/ext/alglib/sdet.cpp +193 -0
  191. data/ext/alglib/sdet.h +101 -0
  192. data/ext/alglib/sevd.cpp +116 -0
  193. data/ext/alglib/sevd.h +99 -0
  194. data/ext/alglib/sinverse.cpp +672 -0
  195. data/ext/alglib/sinverse.h +138 -0
  196. data/ext/alglib/spddet.cpp +138 -0
  197. data/ext/alglib/spddet.h +96 -0
  198. data/ext/alglib/spdgevd.cpp +842 -0
  199. data/ext/alglib/spdgevd.h +200 -0
  200. data/ext/alglib/spdinverse.cpp +509 -0
  201. data/ext/alglib/spdinverse.h +122 -0
  202. data/ext/alglib/spdrcond.cpp +421 -0
  203. data/ext/alglib/spdrcond.h +118 -0
  204. data/ext/alglib/spdsolve.cpp +275 -0
  205. data/ext/alglib/spdsolve.h +105 -0
  206. data/ext/alglib/spline2d.cpp +1192 -0
  207. data/ext/alglib/spline2d.h +301 -0
  208. data/ext/alglib/spline3.cpp +1264 -0
  209. data/ext/alglib/spline3.h +290 -0
  210. data/ext/alglib/srcond.cpp +595 -0
  211. data/ext/alglib/srcond.h +127 -0
  212. data/ext/alglib/ssolve.cpp +895 -0
  213. data/ext/alglib/ssolve.h +139 -0
  214. data/ext/alglib/stdafx.h +0 -0
  215. data/ext/alglib/stest.cpp +131 -0
  216. data/ext/alglib/stest.h +94 -0
  217. data/ext/alglib/studenttdistr.cpp +222 -0
  218. data/ext/alglib/studenttdistr.h +115 -0
  219. data/ext/alglib/studentttests.cpp +377 -0
  220. data/ext/alglib/studentttests.h +178 -0
  221. data/ext/alglib/svd.cpp +620 -0
  222. data/ext/alglib/svd.h +126 -0
  223. data/ext/alglib/tdbisinv.cpp +2608 -0
  224. data/ext/alglib/tdbisinv.h +228 -0
  225. data/ext/alglib/tdevd.cpp +1229 -0
  226. data/ext/alglib/tdevd.h +115 -0
  227. data/ext/alglib/tridiagonal.cpp +594 -0
  228. data/ext/alglib/tridiagonal.h +171 -0
  229. data/ext/alglib/trigintegrals.cpp +490 -0
  230. data/ext/alglib/trigintegrals.h +131 -0
  231. data/ext/alglib/trinverse.cpp +345 -0
  232. data/ext/alglib/trinverse.h +98 -0
  233. data/ext/alglib/trlinsolve.cpp +926 -0
  234. data/ext/alglib/trlinsolve.h +73 -0
  235. data/ext/alglib/tsort.cpp +405 -0
  236. data/ext/alglib/tsort.h +54 -0
  237. data/ext/alglib/variancetests.cpp +245 -0
  238. data/ext/alglib/variancetests.h +134 -0
  239. data/ext/alglib/wsr.cpp +6285 -0
  240. data/ext/alglib/wsr.h +96 -0
  241. data/ext/ap.i +97 -0
  242. data/ext/correlation.i +24 -0
  243. data/ext/extconf.rb +6 -0
  244. data/ext/logit.i +89 -0
  245. data/lib/alglib.rb +71 -0
  246. data/lib/alglib/correlation.rb +26 -0
  247. data/lib/alglib/linearregression.rb +63 -0
  248. data/lib/alglib/logit.rb +42 -0
  249. data/test/test_alglib.rb +52 -0
  250. data/test/test_correlation.rb +44 -0
  251. data/test/test_correlationtest.rb +45 -0
  252. data/test/test_linreg.rb +35 -0
  253. data/test/test_logit.rb +43 -0
  254. data/test/test_pca.rb +27 -0
  255. metadata +326 -0
@@ -0,0 +1,115 @@
1
+ /*************************************************************************
2
+ Copyright (c) 2007, Sergey Bochkanov (ALGLIB project).
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer listed
13
+ in this license in the documentation and/or other materials
14
+ provided with the distribution.
15
+
16
+ - Neither the name of the copyright holders nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *************************************************************************/
32
+
33
+ #ifndef _mannwhitneyu_h
34
+ #define _mannwhitneyu_h
35
+
36
+ #include "ap.h"
37
+ #include "ialglib.h"
38
+
39
+ #include "normaldistr.h"
40
+
41
+
42
+ /*************************************************************************
43
+ Mann-Whitney U-test
44
+
45
+ This test checks hypotheses about whether X and Y are samples of two
46
+ continuous distributions of the same shape and same median or whether
47
+ their medians are different.
48
+
49
+ The following tests are performed:
50
+ * two-tailed test (null hypothesis - the medians are equal)
51
+ * left-tailed test (null hypothesis - the median of the first sample
52
+ is greater than or equal to the median of the second sample)
53
+ * right-tailed test (null hypothesis - the median of the first sample
54
+ is less than or equal to the median of the second sample).
55
+
56
+ Requirements:
57
+ * the samples are independent
58
+ * X and Y are continuous distributions (or discrete distributions well-
59
+ approximating continuous distributions)
60
+ * distributions of X and Y have the same shape. The only possible
61
+ difference is their position (i.e. the value of the median)
62
+ * the number of elements in each sample is not less than 5
63
+ * the scale of measurement should be ordinal, interval or ratio (i.e.
64
+ the test could not be applied to nominal variables).
65
+
66
+ The test is non-parametric and doesn't require distributions to be normal.
67
+
68
+ Input parameters:
69
+ X - sample 1. Array whose index goes from 0 to N-1.
70
+ N - size of the sample. N>=5
71
+ Y - sample 2. Array whose index goes from 0 to M-1.
72
+ M - size of the sample. M>=5
73
+
74
+ Output parameters:
75
+ BothTails - p-value for two-tailed test.
76
+ If BothTails is less than the given significance level
77
+ the null hypothesis is rejected.
78
+ LeftTail - p-value for left-tailed test.
79
+ If LeftTail is less than the given significance level,
80
+ the null hypothesis is rejected.
81
+ RightTail - p-value for right-tailed test.
82
+ If RightTail is less than the given significance level
83
+ the null hypothesis is rejected.
84
+
85
+ To calculate p-values, special approximation is used. This method lets us
86
+ calculate p-values with satisfactory accuracy in interval [0.0001, 1].
87
+ There is no approximation outside the [0.0001, 1] interval. Therefore, if
88
+ the significance level outlies this interval, the test returns 0.0001.
89
+
90
+ Relative precision of approximation of p-value:
91
+
92
+ N M Max.err. Rms.err.
93
+ 5..10 N..10 1.4e-02 6.0e-04
94
+ 5..10 N..100 2.2e-02 5.3e-06
95
+ 10..15 N..15 1.0e-02 3.2e-04
96
+ 10..15 N..100 1.0e-02 2.2e-05
97
+ 15..100 N..100 6.1e-03 2.7e-06
98
+
99
+ For N,M>100 accuracy checks weren't put into practice, but taking into
100
+ account characteristics of asymptotic approximation used, precision should
101
+ not be sharply different from the values for interval [5, 100].
102
+
103
+ -- ALGLIB --
104
+ Copyright 09.04.2007 by Bochkanov Sergey
105
+ *************************************************************************/
106
+ void mannwhitneyutest(const ap::real_1d_array& x,
107
+ int n,
108
+ const ap::real_1d_array& y,
109
+ int m,
110
+ double& bothtails,
111
+ double& lefttail,
112
+ double& righttail);
113
+
114
+
115
+ #endif
@@ -0,0 +1,918 @@
1
+ /*************************************************************************
2
+ Copyright (c) 2009, Sergey Bochkanov (ALGLIB project).
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer listed
13
+ in this license in the documentation and/or other materials
14
+ provided with the distribution.
15
+
16
+ - Neither the name of the copyright holders nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *************************************************************************/
32
+
33
+ #include <stdafx.h>
34
+ #include "minlm.h"
35
+
36
+ static const int lmmodefj = 0;
37
+ static const int lmmodefgj = 1;
38
+ static const int lmmodefgh = 2;
39
+ static const int lmflagnoprelbfgs = 1;
40
+ static const int lmflagnointlbfgs = 2;
41
+ static const int lmprelbfgsm = 5;
42
+ static const int lmintlbfgsits = 5;
43
+ static const int lbfgsnorealloc = 1;
44
+
45
+ static void lmprepare(int n, int m, bool havegrad, lmstate& state);
46
+ static void lmclearrequestfields(lmstate& state);
47
+
48
+ /*************************************************************************
49
+ LEVENBERG-MARQUARDT-LIKE METHOD FOR NON-LINEAR OPTIMIZATION
50
+
51
+ Optimization using function gradient and Hessian. Algorithm - Levenberg-
52
+ Marquardt modification with L-BFGS pre-optimization and internal
53
+ pre-conditioned L-BFGS optimization after each Levenberg-Marquardt step.
54
+
55
+ Function F has general form (not "sum-of-squares"):
56
+
57
+ F = F(x[0], ..., x[n-1])
58
+
59
+ EXAMPLE
60
+
61
+ See HTML-documentation.
62
+
63
+ INPUT PARAMETERS:
64
+ N - dimension, N>1
65
+ X - initial solution, array[0..N-1]
66
+ EpsF - stopping criterion. Algorithm stops if
67
+ |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
68
+ EpsX - stopping criterion. Algorithm stops if
69
+ |X(k+1)-X(k)| <= EpsX*(1+|X(k)|)
70
+ MaxIts - stopping criterion. Algorithm stops after MaxIts iterations.
71
+ MaxIts=0 means no stopping criterion.
72
+
73
+ �������� ���������:
74
+ State - structure which stores algorithm state between subsequent
75
+ calls of MinLMIteration. Used for reverse communication.
76
+ This structure should be passed to MinLMIteration subroutine.
77
+
78
+ See also MinLMIteration, MinLMResults.
79
+
80
+ NOTE
81
+
82
+ Passing EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to automatic
83
+ stopping criterion selection (small EpsX).
84
+
85
+ -- ALGLIB --
86
+ Copyright 30.03.2009 by Bochkanov Sergey
87
+ *************************************************************************/
88
+ void minlmfgh(const int& n,
89
+ const ap::real_1d_array& x,
90
+ const double& epsf,
91
+ const double& epsx,
92
+ const int& maxits,
93
+ lmstate& state)
94
+ {
95
+
96
+
97
+ //
98
+ // Prepare RComm
99
+ //
100
+ state.rstate.ia.setbounds(0, 3);
101
+ state.rstate.ba.setbounds(0, 0);
102
+ state.rstate.ra.setbounds(0, 8);
103
+ state.rstate.stage = -1;
104
+
105
+ //
106
+ // prepare internal structures
107
+ //
108
+ lmprepare(n, 0, true, state);
109
+
110
+ //
111
+ // initialize, check parameters
112
+ //
113
+ state.xupdated = false;
114
+ state.n = n;
115
+ state.m = 0;
116
+ state.epsf = epsf;
117
+ state.epsx = epsx;
118
+ state.maxits = maxits;
119
+ state.flags = 0;
120
+ if( state.epsf==0&&state.epsx==0&&state.maxits==0 )
121
+ {
122
+ state.epsx = 1.0E-6;
123
+ }
124
+ state.usermode = lmmodefgh;
125
+ state.wrongparams = false;
126
+ if( n<1||epsf<0||epsx<0||maxits<0 )
127
+ {
128
+ state.wrongparams = true;
129
+ return;
130
+ }
131
+ ap::vmove(&state.x(0), &x(0), ap::vlen(0,n-1));
132
+ }
133
+
134
+
135
+ /*************************************************************************
136
+ LEVENBERG-MARQUARDT-LIKE METHOD FOR NON-LINEAR OPTIMIZATION
137
+
138
+ Optimization using function gradient and Jacobian. Algorithm - Levenberg-
139
+ Marquardt modification with L-BFGS pre-optimization and internal
140
+ pre-conditioned L-BFGS optimization after each Levenberg-Marquardt step.
141
+
142
+ Function F is represented as sum of squares:
143
+
144
+ F = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
145
+
146
+ EXAMPLE
147
+
148
+ See HTML-documentation.
149
+
150
+ INPUT PARAMETERS:
151
+ N - dimension, N>1
152
+ M - number of functions f[i]
153
+ X - initial solution, array[0..N-1]
154
+ EpsF - stopping criterion. Algorithm stops if
155
+ |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
156
+ EpsX - stopping criterion. Algorithm stops if
157
+ |X(k+1)-X(k)| <= EpsX*(1+|X(k)|)
158
+ MaxIts - stopping criterion. Algorithm stops after MaxIts iterations.
159
+ MaxIts=0 means no stopping criterion.
160
+
161
+ �������� ���������:
162
+ State - structure which stores algorithm state between subsequent
163
+ calls of MinLMIteration. Used for reverse communication.
164
+ This structure should be passed to MinLMIteration subroutine.
165
+
166
+ See also MinLMIteration, MinLMResults.
167
+
168
+ NOTE
169
+
170
+ Passing EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to automatic
171
+ stopping criterion selection (small EpsX).
172
+
173
+ -- ALGLIB --
174
+ Copyright 30.03.2009 by Bochkanov Sergey
175
+ *************************************************************************/
176
+ void minlmfgj(const int& n,
177
+ const int& m,
178
+ const ap::real_1d_array& x,
179
+ const double& epsf,
180
+ const double& epsx,
181
+ const int& maxits,
182
+ lmstate& state)
183
+ {
184
+
185
+
186
+ //
187
+ // Prepare RComm
188
+ //
189
+ state.rstate.ia.setbounds(0, 3);
190
+ state.rstate.ba.setbounds(0, 0);
191
+ state.rstate.ra.setbounds(0, 8);
192
+ state.rstate.stage = -1;
193
+
194
+ //
195
+ // prepare internal structures
196
+ //
197
+ lmprepare(n, m, true, state);
198
+
199
+ //
200
+ // initialize, check parameters
201
+ //
202
+ state.xupdated = false;
203
+ state.n = n;
204
+ state.m = m;
205
+ state.epsf = epsf;
206
+ state.epsx = epsx;
207
+ state.maxits = maxits;
208
+ state.flags = 0;
209
+ if( state.epsf==0&&state.epsx==0&&state.maxits==0 )
210
+ {
211
+ state.epsx = 1.0E-6;
212
+ }
213
+ state.usermode = lmmodefgj;
214
+ state.wrongparams = false;
215
+ if( n<1||m<1||epsf<0||epsx<0||maxits<0 )
216
+ {
217
+ state.wrongparams = true;
218
+ return;
219
+ }
220
+ ap::vmove(&state.x(0), &x(0), ap::vlen(0,n-1));
221
+ }
222
+
223
+
224
+ /*************************************************************************
225
+ CLASSIC LEVENBERG-MARQUARDT METHOD FOR NON-LINEAR OPTIMIZATION
226
+
227
+ Optimization using Jacobi matrix. Algorithm - classic Levenberg-Marquardt
228
+ method.
229
+
230
+ Function F is represented as sum of squares:
231
+
232
+ F = f[0]^2(x[0],...,x[n-1]) + ... + f[m-1]^2(x[0],...,x[n-1])
233
+
234
+ EXAMPLE
235
+
236
+ See HTML-documentation.
237
+
238
+ INPUT PARAMETERS:
239
+ N - dimension, N>1
240
+ M - number of functions f[i]
241
+ X - initial solution, array[0..N-1]
242
+ EpsF - stopping criterion. Algorithm stops if
243
+ |F(k+1)-F(k)| <= EpsF*max{|F(k)|, |F(k+1)|, 1}
244
+ EpsX - stopping criterion. Algorithm stops if
245
+ |X(k+1)-X(k)| <= EpsX*(1+|X(k)|)
246
+ MaxIts - stopping criterion. Algorithm stops after MaxIts iterations.
247
+ MaxIts=0 means no stopping criterion.
248
+
249
+ �������� ���������:
250
+ State - structure which stores algorithm state between subsequent
251
+ calls of MinLMIteration. Used for reverse communication.
252
+ This structure should be passed to MinLMIteration subroutine.
253
+
254
+ See also MinLMIteration, MinLMResults.
255
+
256
+ NOTE
257
+
258
+ Passing EpsF=0, EpsX=0 and MaxIts=0 (simultaneously) will lead to automatic
259
+ stopping criterion selection (small EpsX).
260
+
261
+ -- ALGLIB --
262
+ Copyright 30.03.2009 by Bochkanov Sergey
263
+ *************************************************************************/
264
+ void minlmfj(const int& n,
265
+ const int& m,
266
+ const ap::real_1d_array& x,
267
+ const double& epsf,
268
+ const double& epsx,
269
+ const int& maxits,
270
+ lmstate& state)
271
+ {
272
+
273
+
274
+ //
275
+ // Prepare RComm
276
+ //
277
+ state.rstate.ia.setbounds(0, 3);
278
+ state.rstate.ba.setbounds(0, 0);
279
+ state.rstate.ra.setbounds(0, 8);
280
+ state.rstate.stage = -1;
281
+
282
+ //
283
+ // prepare internal structures
284
+ //
285
+ lmprepare(n, m, true, state);
286
+
287
+ //
288
+ // initialize, check parameters
289
+ //
290
+ state.xupdated = false;
291
+ state.n = n;
292
+ state.m = m;
293
+ state.epsf = epsf;
294
+ state.epsx = epsx;
295
+ state.maxits = maxits;
296
+ state.flags = 0;
297
+ if( state.epsf==0&&state.epsx==0&&state.maxits==0 )
298
+ {
299
+ state.epsx = 1.0E-6;
300
+ }
301
+ state.usermode = lmmodefj;
302
+ state.wrongparams = false;
303
+ if( n<1||m<1||epsf<0||epsx<0||maxits<0 )
304
+ {
305
+ state.wrongparams = true;
306
+ return;
307
+ }
308
+ ap::vmove(&state.x(0), &x(0), ap::vlen(0,n-1));
309
+ }
310
+
311
+
312
+ /*************************************************************************
313
+ One Levenberg-Marquardt iteration.
314
+
315
+ Called after inialization of State structure with MinLMXXX subroutine.
316
+ See HTML docs for examples.
317
+
318
+ Input parameters:
319
+ State - structure which stores algorithm state between subsequent
320
+ calls and which is used for reverse communication. Must be
321
+ initialized with MinLMXXX call first.
322
+
323
+ If subroutine returned False, iterative algorithm has converged.
324
+
325
+ If subroutine returned True, then:
326
+ * if State.NeedF=True, - function value F at State.X[0..N-1]
327
+ is required
328
+ * if State.NeedFG=True - function value F and gradient G
329
+ are required
330
+ * if State.NeedFiJ=True - function vector f[i] and Jacobi matrix J
331
+ are required
332
+ * if State.NeedFGH=True - function value F, gradient G and Hesian H
333
+ are required
334
+
335
+ One and only one of this fields can be set at time.
336
+
337
+ Results are stored:
338
+ * function value - in LMState.F
339
+ * gradient - in LMState.G[0..N-1]
340
+ * Jacobi matrix - in LMState.J[0..M-1,0..N-1]
341
+ * Hessian - in LMState.H[0..N-1,0..N-1]
342
+
343
+ -- ALGLIB --
344
+ Copyright 10.03.2009 by Bochkanov Sergey
345
+ *************************************************************************/
346
+ bool minlmiteration(lmstate& state)
347
+ {
348
+ bool result;
349
+ int n;
350
+ int m;
351
+ int i;
352
+ double xnorm;
353
+ double stepnorm;
354
+ bool spd;
355
+ double fbase;
356
+ double fnew;
357
+ double lambda;
358
+ double nu;
359
+ double lambdaup;
360
+ double lambdadown;
361
+ int lbfgsflags;
362
+ double v;
363
+
364
+
365
+ //
366
+ // Reverse communication preparations
367
+ // I know it looks ugly, but it works the same way
368
+ // anywhere from C++ to Python.
369
+ //
370
+ // This code initializes locals by:
371
+ // * random values determined during code
372
+ // generation - on first subroutine call
373
+ // * values from previous call - on subsequent calls
374
+ //
375
+ if( state.rstate.stage>=0 )
376
+ {
377
+ n = state.rstate.ia(0);
378
+ m = state.rstate.ia(1);
379
+ i = state.rstate.ia(2);
380
+ lbfgsflags = state.rstate.ia(3);
381
+ spd = state.rstate.ba(0);
382
+ xnorm = state.rstate.ra(0);
383
+ stepnorm = state.rstate.ra(1);
384
+ fbase = state.rstate.ra(2);
385
+ fnew = state.rstate.ra(3);
386
+ lambda = state.rstate.ra(4);
387
+ nu = state.rstate.ra(5);
388
+ lambdaup = state.rstate.ra(6);
389
+ lambdadown = state.rstate.ra(7);
390
+ v = state.rstate.ra(8);
391
+ }
392
+ else
393
+ {
394
+ n = -983;
395
+ m = -989;
396
+ i = -834;
397
+ lbfgsflags = 900;
398
+ spd = true;
399
+ xnorm = 364;
400
+ stepnorm = 214;
401
+ fbase = -338;
402
+ fnew = -686;
403
+ lambda = 912;
404
+ nu = 585;
405
+ lambdaup = 497;
406
+ lambdadown = -271;
407
+ v = -581;
408
+ }
409
+ if( state.rstate.stage==0 )
410
+ {
411
+ goto lbl_0;
412
+ }
413
+ if( state.rstate.stage==1 )
414
+ {
415
+ goto lbl_1;
416
+ }
417
+ if( state.rstate.stage==2 )
418
+ {
419
+ goto lbl_2;
420
+ }
421
+ if( state.rstate.stage==3 )
422
+ {
423
+ goto lbl_3;
424
+ }
425
+ if( state.rstate.stage==4 )
426
+ {
427
+ goto lbl_4;
428
+ }
429
+ if( state.rstate.stage==5 )
430
+ {
431
+ goto lbl_5;
432
+ }
433
+ if( state.rstate.stage==6 )
434
+ {
435
+ goto lbl_6;
436
+ }
437
+
438
+ //
439
+ // Routine body
440
+ //
441
+ ap::ap_error::make_assertion(state.usermode==lmmodefj||state.usermode==lmmodefgj||state.usermode==lmmodefgh, "LM: internal error");
442
+ if( state.wrongparams )
443
+ {
444
+ state.repterminationtype = -1;
445
+ result = false;
446
+ return result;
447
+ }
448
+
449
+ //
450
+ // prepare params
451
+ //
452
+ n = state.n;
453
+ m = state.m;
454
+ lambdaup = 10;
455
+ lambdadown = 0.3;
456
+ nu = 2;
457
+ lbfgsflags = 0;
458
+
459
+ //
460
+ // if we have F and G
461
+ //
462
+ if( !((state.usermode==lmmodefgj||state.usermode==lmmodefgh)&&state.flags/lmflagnoprelbfgs%2==0) )
463
+ {
464
+ goto lbl_7;
465
+ }
466
+
467
+ //
468
+ // First stage of the hybrid algorithm: LBFGS
469
+ //
470
+ minlbfgs(n, ap::minint(n, lmprelbfgsm), state.x, 0.0, 0.0, 0.0, ap::maxint(25, n), 0, state.internalstate);
471
+ lbl_9:
472
+ if( !minlbfgsiteration(state.internalstate) )
473
+ {
474
+ goto lbl_10;
475
+ }
476
+
477
+ //
478
+ // RComm
479
+ //
480
+ ap::vmove(&state.x(0), &state.internalstate.x(0), ap::vlen(0,n-1));
481
+ lmclearrequestfields(state);
482
+ state.needfg = true;
483
+ state.rstate.stage = 0;
484
+ goto lbl_rcomm;
485
+ lbl_0:
486
+ state.repnfunc = state.repnfunc+1;
487
+ state.repngrad = state.repngrad+1;
488
+
489
+ //
490
+ // Call LBFGS
491
+ //
492
+ state.internalstate.f = state.f;
493
+ ap::vmove(&state.internalstate.g(0), &state.g(0), ap::vlen(0,n-1));
494
+ goto lbl_9;
495
+ lbl_10:
496
+ minlbfgsresults(state.internalstate, state.x, state.internalrep);
497
+ lbl_7:
498
+
499
+ //
500
+ // Second stage of the hybrid algorithm: LM
501
+ // Initialize quadratic model.
502
+ //
503
+ if( state.usermode!=lmmodefgh )
504
+ {
505
+ goto lbl_11;
506
+ }
507
+
508
+ //
509
+ // RComm
510
+ //
511
+ lmclearrequestfields(state);
512
+ state.needfgh = true;
513
+ state.rstate.stage = 1;
514
+ goto lbl_rcomm;
515
+ lbl_1:
516
+ state.repnfunc = state.repnfunc+1;
517
+ state.repngrad = state.repngrad+1;
518
+ state.repnhess = state.repnhess+1;
519
+
520
+ //
521
+ // generate raw quadratic model
522
+ //
523
+ for(i = 0; i <= n-1; i++)
524
+ {
525
+ ap::vmove(&state.rawmodel(i, 0), &state.h(i, 0), ap::vlen(0,n-1));
526
+ }
527
+ ap::vmove(&state.gbase(0), &state.g(0), ap::vlen(0,n-1));
528
+ fbase = state.f;
529
+ lbl_11:
530
+ if( !(state.usermode==lmmodefgj||state.usermode==lmmodefj) )
531
+ {
532
+ goto lbl_13;
533
+ }
534
+
535
+ //
536
+ // RComm
537
+ //
538
+ lmclearrequestfields(state);
539
+ state.needfij = true;
540
+ state.rstate.stage = 2;
541
+ goto lbl_rcomm;
542
+ lbl_2:
543
+ state.repnfunc = state.repnfunc+1;
544
+ state.repnjac = state.repnjac+1;
545
+
546
+ //
547
+ // generate raw quadratic model
548
+ //
549
+ matrixmatrixmultiply(state.j, 0, m-1, 0, n-1, true, state.j, 0, m-1, 0, n-1, false, 1.0, state.rawmodel, 0, n-1, 0, n-1, 0.0, state.work);
550
+ matrixvectormultiply(state.j, 0, m-1, 0, n-1, true, state.fi, 0, m-1, 1.0, state.gbase, 0, n-1, 0.0);
551
+ fbase = ap::vdotproduct(&state.fi(0), &state.fi(0), ap::vlen(0,m-1));
552
+ lbl_13:
553
+ lambda = 0.001;
554
+ lbl_15:
555
+ if( false )
556
+ {
557
+ goto lbl_16;
558
+ }
559
+
560
+ //
561
+ // 1. Model = RawModel+lambda*I
562
+ // 2. Try to solve (RawModel+Lambda*I)*dx = -g.
563
+ // Increase lambda if left part is not positive definite.
564
+ //
565
+ for(i = 0; i <= n-1; i++)
566
+ {
567
+ ap::vmove(&state.model(i, 0), &state.rawmodel(i, 0), ap::vlen(0,n-1));
568
+ state.model(i,i) = state.model(i,i)+lambda;
569
+ }
570
+ spd = spdmatrixcholesky(state.model, n, true);
571
+ state.repncholesky = state.repncholesky+1;
572
+ if( !spd )
573
+ {
574
+ lambda = lambda*lambdaup*nu;
575
+ nu = nu*2;
576
+ goto lbl_15;
577
+ }
578
+ if( !spdmatrixcholeskysolve(state.model, state.gbase, n, true, state.xdir) )
579
+ {
580
+ lambda = lambda*lambdaup*nu;
581
+ nu = nu*2;
582
+ goto lbl_15;
583
+ }
584
+ ap::vmul(&state.xdir(0), ap::vlen(0,n-1), -1);
585
+
586
+ //
587
+ // Candidate lambda found.
588
+ // 1. Save old w in WBase
589
+ // 1. Test some stopping criterions
590
+ // 2. If error(w+wdir)>error(w), increase lambda
591
+ //
592
+ ap::vmove(&state.xbase(0), &state.x(0), ap::vlen(0,n-1));
593
+ ap::vadd(&state.x(0), &state.xdir(0), ap::vlen(0,n-1));
594
+ xnorm = ap::vdotproduct(&state.xbase(0), &state.xbase(0), ap::vlen(0,n-1));
595
+ stepnorm = ap::vdotproduct(&state.xdir(0), &state.xdir(0), ap::vlen(0,n-1));
596
+ xnorm = sqrt(xnorm);
597
+ stepnorm = sqrt(stepnorm);
598
+ if( stepnorm<=state.epsx*(1+xnorm) )
599
+ {
600
+
601
+ //
602
+ // step size if small enough
603
+ //
604
+ state.repterminationtype = 2;
605
+ goto lbl_16;
606
+ }
607
+ lmclearrequestfields(state);
608
+ state.needf = true;
609
+ state.rstate.stage = 3;
610
+ goto lbl_rcomm;
611
+ lbl_3:
612
+ state.repnfunc = state.repnfunc+1;
613
+ fnew = state.f;
614
+ if( fabs(fnew-fbase)<=state.epsf*ap::maxreal(double(1), ap::maxreal(fabs(fbase), fabs(fnew))) )
615
+ {
616
+
617
+ //
618
+ // function change is small enough
619
+ //
620
+ state.repterminationtype = 1;
621
+ goto lbl_16;
622
+ }
623
+ if( fnew>fbase )
624
+ {
625
+
626
+ //
627
+ // restore state and continue out search for lambda
628
+ //
629
+ ap::vmove(&state.x(0), &state.xbase(0), ap::vlen(0,n-1));
630
+ lambda = lambda*lambdaup*nu;
631
+ nu = nu*2;
632
+ goto lbl_15;
633
+ }
634
+ if( !((state.usermode==lmmodefgj||state.usermode==lmmodefgh)&&state.flags/lmflagnointlbfgs%2==0) )
635
+ {
636
+ goto lbl_17;
637
+ }
638
+ ap::ap_error::make_assertion(state.usermode==lmmodefgh, "");
639
+
640
+ //
641
+ // Optimize using inv(cholesky(H)) as preconditioner
642
+ //
643
+ if( !rmatrixtrinverse(state.model, n, true, false) )
644
+ {
645
+ goto lbl_19;
646
+ }
647
+
648
+ //
649
+ // if matrix can be inverted use it.
650
+ // just silently move to next iteration otherwise.
651
+ // (will be very, very rare, mostly for specially
652
+ // designed near-degenerate tasks)
653
+ //
654
+ ap::vmove(&state.xbase(0), &state.x(0), ap::vlen(0,n-1));
655
+ for(i = 0; i <= n-1; i++)
656
+ {
657
+ state.xprec(i) = 0;
658
+ }
659
+ minlbfgs(n, ap::minint(n, lmintlbfgsits), state.xprec, 0.0, 0.0, 0.0, lmintlbfgsits, lbfgsflags, state.internalstate);
660
+ lbl_21:
661
+ if( !minlbfgsiteration(state.internalstate) )
662
+ {
663
+ goto lbl_22;
664
+ }
665
+
666
+ //
667
+ // convert XPrec to unpreconditioned form, then call RComm.
668
+ //
669
+ for(i = 0; i <= n-1; i++)
670
+ {
671
+ v = ap::vdotproduct(&state.internalstate.x(i), &state.model(i, i), ap::vlen(i,n-1));
672
+ state.x(i) = state.xbase(i)+v;
673
+ }
674
+ lmclearrequestfields(state);
675
+ state.needfg = true;
676
+ state.rstate.stage = 4;
677
+ goto lbl_rcomm;
678
+ lbl_4:
679
+ state.repnfunc = state.repnfunc+1;
680
+ state.repngrad = state.repngrad+1;
681
+
682
+ //
683
+ // 1. pass State.F to State.InternalState.F
684
+ // 2. convert gradient back to preconditioned form
685
+ //
686
+ state.internalstate.f = state.f;
687
+ for(i = 0; i <= n-1; i++)
688
+ {
689
+ state.internalstate.g(i) = 0;
690
+ }
691
+ for(i = 0; i <= n-1; i++)
692
+ {
693
+ v = state.g(i);
694
+ ap::vadd(&state.internalstate.g(i), &state.model(i, i), ap::vlen(i,n-1), v);
695
+ }
696
+
697
+ //
698
+ // next iteration
699
+ //
700
+ goto lbl_21;
701
+ lbl_22:
702
+
703
+ //
704
+ // change LBFGS flags to NoRealloc.
705
+ // L-BFGS subroutine will use memory allocated from previous run.
706
+ // it is possible since all subsequent calls will be with same N/M.
707
+ //
708
+ lbfgsflags = lbfgsnorealloc;
709
+
710
+ //
711
+ // back to unpreconditioned X
712
+ //
713
+ minlbfgsresults(state.internalstate, state.xprec, state.internalrep);
714
+ for(i = 0; i <= n-1; i++)
715
+ {
716
+ v = ap::vdotproduct(&state.xprec(i), &state.model(i, i), ap::vlen(i,n-1));
717
+ state.x(i) = state.xbase(i)+v;
718
+ }
719
+ lbl_19:
720
+ lbl_17:
721
+
722
+ //
723
+ // Accept new position.
724
+ // Calculate Hessian
725
+ //
726
+ if( state.usermode!=lmmodefgh )
727
+ {
728
+ goto lbl_23;
729
+ }
730
+
731
+ //
732
+ // RComm
733
+ //
734
+ lmclearrequestfields(state);
735
+ state.needfgh = true;
736
+ state.rstate.stage = 5;
737
+ goto lbl_rcomm;
738
+ lbl_5:
739
+ state.repnfunc = state.repnfunc+1;
740
+ state.repngrad = state.repngrad+1;
741
+ state.repnhess = state.repnhess+1;
742
+
743
+ //
744
+ // Update raw quadratic model
745
+ //
746
+ for(i = 0; i <= n-1; i++)
747
+ {
748
+ ap::vmove(&state.rawmodel(i, 0), &state.h(i, 0), ap::vlen(0,n-1));
749
+ }
750
+ ap::vmove(&state.gbase(0), &state.g(0), ap::vlen(0,n-1));
751
+ fbase = state.f;
752
+ lbl_23:
753
+ if( !(state.usermode==lmmodefgj||state.usermode==lmmodefj) )
754
+ {
755
+ goto lbl_25;
756
+ }
757
+
758
+ //
759
+ // RComm
760
+ //
761
+ lmclearrequestfields(state);
762
+ state.needfij = true;
763
+ state.rstate.stage = 6;
764
+ goto lbl_rcomm;
765
+ lbl_6:
766
+ state.repnfunc = state.repnfunc+1;
767
+ state.repnjac = state.repnjac+1;
768
+
769
+ //
770
+ // generate raw quadratic model
771
+ //
772
+ matrixmatrixmultiply(state.j, 0, m-1, 0, n-1, true, state.j, 0, m-1, 0, n-1, false, 1.0, state.rawmodel, 0, n-1, 0, n-1, 0.0, state.work);
773
+ matrixvectormultiply(state.j, 0, m-1, 0, n-1, true, state.fi, 0, m-1, 1.0, state.gbase, 0, n-1, 0.0);
774
+ fbase = ap::vdotproduct(&state.fi(0), &state.fi(0), ap::vlen(0,m-1));
775
+ lbl_25:
776
+ state.repiterationscount = state.repiterationscount+1;
777
+ if( state.repiterationscount>=state.maxits&&state.maxits>0 )
778
+ {
779
+ state.repterminationtype = 5;
780
+ goto lbl_16;
781
+ }
782
+
783
+ //
784
+ // Update lambda
785
+ //
786
+ lambda = lambda*lambdadown;
787
+ nu = 2;
788
+ goto lbl_15;
789
+ lbl_16:
790
+ result = false;
791
+ return result;
792
+
793
+ //
794
+ // Saving state
795
+ //
796
+ lbl_rcomm:
797
+ result = true;
798
+ state.rstate.ia(0) = n;
799
+ state.rstate.ia(1) = m;
800
+ state.rstate.ia(2) = i;
801
+ state.rstate.ia(3) = lbfgsflags;
802
+ state.rstate.ba(0) = spd;
803
+ state.rstate.ra(0) = xnorm;
804
+ state.rstate.ra(1) = stepnorm;
805
+ state.rstate.ra(2) = fbase;
806
+ state.rstate.ra(3) = fnew;
807
+ state.rstate.ra(4) = lambda;
808
+ state.rstate.ra(5) = nu;
809
+ state.rstate.ra(6) = lambdaup;
810
+ state.rstate.ra(7) = lambdadown;
811
+ state.rstate.ra(8) = v;
812
+ return result;
813
+ }
814
+
815
+
816
+ /*************************************************************************
817
+ Levenberg-Marquardt algorithm results
818
+
819
+ Called after MinLMIteration returned False.
820
+
821
+ Input parameters:
822
+ State - algorithm state (used by MinLMIteration).
823
+
824
+ Output parameters:
825
+ X - array[0..N-1], solution
826
+ Rep - optimization report:
827
+ * Rep.TerminationType completetion code:
828
+ * -1 incorrect parameters were specified
829
+ * 1 relative function improvement is no more than
830
+ EpsF.
831
+ * 2 relative step is no more than EpsX.
832
+ * 4 gradient norm is no more than EpsG
833
+ * 5 MaxIts steps was taken
834
+ * Rep.IterationsCount contains iterations count
835
+ * Rep.NFunc - number of function calculations
836
+ * Rep.NJac - number of Jacobi matrix calculations
837
+ * Rep.NGrad - number of gradient calculations
838
+ * Rep.NHess - number of Hessian calculations
839
+ * Rep.NCholesky - number of Cholesky decomposition calculations
840
+
841
+ -- ALGLIB --
842
+ Copyright 10.03.2009 by Bochkanov Sergey
843
+ *************************************************************************/
844
+ void minlmresults(const lmstate& state, ap::real_1d_array& x, lmreport& rep)
845
+ {
846
+
847
+ x.setbounds(0, state.n-1);
848
+ ap::vmove(&x(0), &state.x(0), ap::vlen(0,state.n-1));
849
+ rep.iterationscount = state.repiterationscount;
850
+ rep.terminationtype = state.repterminationtype;
851
+ rep.nfunc = state.repnfunc;
852
+ rep.njac = state.repnjac;
853
+ rep.ngrad = state.repngrad;
854
+ rep.nhess = state.repnhess;
855
+ rep.ncholesky = state.repncholesky;
856
+ }
857
+
858
+
859
+ /*************************************************************************
860
+ Prepare internal structures (except for RComm).
861
+
862
+ Note: M must be zero for FGH mode, non-zero for FJ/FGJ mode.
863
+ *************************************************************************/
864
+ static void lmprepare(int n, int m, bool havegrad, lmstate& state)
865
+ {
866
+
867
+ state.repiterationscount = 0;
868
+ state.repterminationtype = 0;
869
+ state.repnfunc = 0;
870
+ state.repnjac = 0;
871
+ state.repngrad = 0;
872
+ state.repnhess = 0;
873
+ state.repncholesky = 0;
874
+ if( n<0||m<0 )
875
+ {
876
+ return;
877
+ }
878
+ if( havegrad )
879
+ {
880
+ state.g.setbounds(0, n-1);
881
+ }
882
+ if( m!=0 )
883
+ {
884
+ state.j.setbounds(0, m-1, 0, n-1);
885
+ state.fi.setbounds(0, m-1);
886
+ state.h.setbounds(0, 0, 0, 0);
887
+ }
888
+ else
889
+ {
890
+ state.j.setbounds(0, 0, 0, 0);
891
+ state.fi.setbounds(0, 0);
892
+ state.h.setbounds(0, n-1, 0, n-1);
893
+ }
894
+ state.x.setbounds(0, n-1);
895
+ state.rawmodel.setbounds(0, n-1, 0, n-1);
896
+ state.model.setbounds(0, n-1, 0, n-1);
897
+ state.xbase.setbounds(0, n-1);
898
+ state.xprec.setbounds(0, n-1);
899
+ state.gbase.setbounds(0, n-1);
900
+ state.xdir.setbounds(0, n-1);
901
+ state.work.setbounds(1, ap::maxint(n, m));
902
+ }
903
+
904
+
905
+ /*************************************************************************
906
+ Clears request fileds (to be sure that we don't forgot to clear something)
907
+ *************************************************************************/
908
+ static void lmclearrequestfields(lmstate& state)
909
+ {
910
+
911
+ state.needf = false;
912
+ state.needfg = false;
913
+ state.needfgh = false;
914
+ state.needfij = false;
915
+ }
916
+
917
+
918
+