1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
43
#include "precomp.hpp"
47
This is translation to C++ of the Matlab's LMSolve package by Miroslav Balda.
48
Here is the original copyright:
49
============================================================================
51
Copyright (c) 2007, Miroslav Balda
54
Redistribution and use in source and binary forms, with or without
55
modification, are permitted provided that the following conditions are
58
* Redistributions of source code must retain the above copyright
59
notice, this list of conditions and the following disclaimer.
60
* Redistributions in binary form must reproduce the above copyright
61
notice, this list of conditions and the following disclaimer in
62
the documentation and/or other materials provided with the distribution
64
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
65
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
68
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
72
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
74
POSSIBILITY OF SUCH DAMAGE.
80
class LMSolverImpl : public LMSolver
83
LMSolverImpl() : maxIters(100) { init(); }
84
LMSolverImpl(const Ptr<LMSolver::Callback>& _cb, int _maxIters) : cb(_cb), maxIters(_maxIters) { init(); }
88
epsx = epsf = FLT_EPSILON;
92
int run(InputOutputArray _param0) const
94
Mat param0 = _param0.getMat(), x, xd, r, rd, J, A, Ap, v, temp_d, d;
95
int ptype = param0.type();
97
CV_Assert( (param0.cols == 1 || param0.rows == 1) && (ptype == CV_32F || ptype == CV_64F));
100
int lx = param0.rows + param0.cols - 1;
101
param0.convertTo(x, CV_64F);
106
if( !cb->compute(x, r, J) )
108
double S = norm(r, NORM_L2SQR);
111
mulTransposed(J, A, true);
112
gemm(J, r, 1, noArray(), 0, v, GEMM_1_T);
114
Mat D = A.diag().clone();
116
const double Rlo = 0.25, Rhi = 0.75;
117
double lambda = 1, lc = 0.75;
120
if( printInterval != 0 )
122
printf("************************************************************************************\n");
123
printf("\titr\tnfJ\t\tSUM(r^2)\t\tx\t\tdx\t\tl\t\tlc\n");
124
printf("************************************************************************************\n");
129
CV_Assert( A.type() == CV_64F && A.rows == lx );
131
for( i = 0; i < lx; i++ )
132
Ap.at<double>(i, i) += lambda*D.at<double>(i);
133
solve(Ap, v, d, DECOMP_EIG);
135
if( !cb->compute(xd, rd, noArray()) )
138
double Sd = norm(rd, NORM_L2SQR);
139
gemm(A, d, -1, v, 2, temp_d);
140
double dS = d.dot(temp_d);
141
double R = (S - Sd)/(fabs(dS) > DBL_EPSILON ? dS : 1);
151
// find new nu if R too low
153
double nu = (Sd - S)/(fabs(t) > DBL_EPSILON ? t : 1) + 2;
154
nu = std::min(std::max(nu, 2.), 10.);
157
invert(A, Ap, DECOMP_EIG);
158
double maxval = DBL_EPSILON;
159
for( i = 0; i < lx; i++ )
160
maxval = std::max(maxval, std::abs(Ap.at<double>(i,i)));
161
lambda = lc = 1./maxval;
172
if( !cb->compute(x, r, J) )
174
mulTransposed(J, A, true);
175
gemm(J, r, 1, noArray(), 0, v, GEMM_1_T);
179
bool proceed = iter < maxIters && norm(d, NORM_INF) >= epsx && norm(r, NORM_INF) >= epsf;
181
if( printInterval != 0 && (iter % printInterval == 0 || iter == 1 || !proceed) )
183
printf("%c%10d %10d %15.4e %16.4e %17.4e %16.4e %17.4e\n",
184
(proceed ? ' ' : '*'), iter, nfJ, S, x.at<double>(0), d.at<double>(0), lambda, lc);
191
if( param0.size != x.size )
194
x.convertTo(param0, ptype);
195
if( iter == maxIters )
201
void setCallback(const Ptr<LMSolver::Callback>& _cb) { cb = _cb; }
203
Ptr<LMSolver::Callback> cb;
212
Ptr<LMSolver> createLMSolver(const Ptr<LMSolver::Callback>& cb, int maxIters)
214
return makePtr<LMSolverImpl>(cb, maxIters);