mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-05-08 05:49:06 +08:00
use a plain matrix to store the upper triangular matrix 'R', instead
of the "compact inside a vector" scheme used by fortran/minpack. The most difficult part is to fix all related code. Tests pass.
This commit is contained in:
parent
4b859c8554
commit
91561cded4
@ -74,6 +74,8 @@ public:
|
|||||||
};
|
};
|
||||||
typedef Matrix< Scalar, Dynamic, 1 > FVectorType;
|
typedef Matrix< Scalar, Dynamic, 1 > FVectorType;
|
||||||
typedef Matrix< Scalar, Dynamic, Dynamic > JacobianType;
|
typedef Matrix< Scalar, Dynamic, Dynamic > JacobianType;
|
||||||
|
/* TODO: if eigen provides a triangular storage, use it here */
|
||||||
|
typedef Matrix< Scalar, Dynamic, Dynamic > UpperTriangularType;
|
||||||
|
|
||||||
Status hybrj1(
|
Status hybrj1(
|
||||||
FVectorType &x,
|
FVectorType &x,
|
||||||
@ -113,8 +115,9 @@ public:
|
|||||||
|
|
||||||
void resetParameters(void) { parameters = Parameters(); }
|
void resetParameters(void) { parameters = Parameters(); }
|
||||||
Parameters parameters;
|
Parameters parameters;
|
||||||
FVectorType fvec, R, qtf, diag;
|
FVectorType fvec, qtf, diag;
|
||||||
JacobianType fjac;
|
JacobianType fjac;
|
||||||
|
UpperTriangularType R;
|
||||||
int nfev;
|
int nfev;
|
||||||
int njev;
|
int njev;
|
||||||
int iter;
|
int iter;
|
||||||
@ -173,7 +176,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveInit(
|
|||||||
wa1.resize(n); wa2.resize(n); wa3.resize(n); wa4.resize(n);
|
wa1.resize(n); wa2.resize(n); wa3.resize(n); wa4.resize(n);
|
||||||
fvec.resize(n);
|
fvec.resize(n);
|
||||||
qtf.resize(n);
|
qtf.resize(n);
|
||||||
R.resize( (n*(n+1))/2);
|
|
||||||
fjac.resize(n, n);
|
fjac.resize(n, n);
|
||||||
if (mode != 2)
|
if (mode != 2)
|
||||||
diag.resize(n);
|
diag.resize(n);
|
||||||
@ -218,7 +220,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
|||||||
const int mode
|
const int mode
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int i, j, l;
|
int i, j;
|
||||||
jeval = true;
|
jeval = true;
|
||||||
|
|
||||||
/* calculate the jacobian matrix. */
|
/* calculate the jacobian matrix. */
|
||||||
@ -272,17 +274,10 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
|||||||
|
|
||||||
/* copy the triangular factor of the qr factorization into r. */
|
/* copy the triangular factor of the qr factorization into r. */
|
||||||
|
|
||||||
|
R = qrfac.matrixQR();
|
||||||
sing = false;
|
sing = false;
|
||||||
for (j = 0; j < n; ++j) {
|
for (j = 0; j < n; ++j)
|
||||||
l = j;
|
if (wa1[j] == 0.) sing = true;
|
||||||
for (i = 0; i < j; ++i) {
|
|
||||||
R[l] = fjac(i,j);
|
|
||||||
l = l + n - i -1;
|
|
||||||
}
|
|
||||||
R[l] = wa1[j];
|
|
||||||
if (wa1[j] == 0.)
|
|
||||||
sing = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* accumulate the orthogonal factor in fjac. */
|
/* accumulate the orthogonal factor in fjac. */
|
||||||
ei_qform<Scalar>(n, n, fjac.data(), fjac.rows(), wa1.data());
|
ei_qform<Scalar>(n, n, fjac.data(), fjac.rows(), wa1.data());
|
||||||
@ -328,13 +323,10 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
|||||||
|
|
||||||
/* compute the scaled predicted reduction. */
|
/* compute the scaled predicted reduction. */
|
||||||
|
|
||||||
l = 0;
|
|
||||||
for (i = 0; i < n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
sum = 0.;
|
sum = 0.;
|
||||||
for (j = i; j < n; ++j) {
|
for (j = i; j < n; ++j)
|
||||||
sum += R[l] * wa1[j];
|
sum += R(i,j) * wa1[j];
|
||||||
++l;
|
|
||||||
}
|
|
||||||
wa3[i] = qtf[i] + sum;
|
wa3[i] = qtf[i] + sum;
|
||||||
}
|
}
|
||||||
temp = wa3.stableNorm();
|
temp = wa3.stableNorm();
|
||||||
@ -421,7 +413,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
|||||||
|
|
||||||
/* compute the qr factorization of the updated jacobian. */
|
/* compute the qr factorization of the updated jacobian. */
|
||||||
|
|
||||||
ei_r1updt<Scalar>(n, n, R.data(), R.size(), wa1.data(), wa2.data(), wa3.data(), &sing);
|
ei_r1updt<Scalar>(n, n, R, wa1.data(), wa2.data(), wa3.data(), &sing);
|
||||||
ei_r1mpyq<Scalar>(n, n, fjac.data(), fjac.rows(), wa2.data(), wa3.data());
|
ei_r1mpyq<Scalar>(n, n, fjac.data(), fjac.rows(), wa2.data(), wa3.data());
|
||||||
ei_r1mpyq<Scalar>(1, n, qtf.data(), 1, wa2.data(), wa3.data());
|
ei_r1mpyq<Scalar>(1, n, qtf.data(), 1, wa2.data(), wa3.data());
|
||||||
|
|
||||||
@ -488,7 +480,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffInit(
|
|||||||
|
|
||||||
wa1.resize(n); wa2.resize(n); wa3.resize(n); wa4.resize(n);
|
wa1.resize(n); wa2.resize(n); wa3.resize(n); wa4.resize(n);
|
||||||
qtf.resize(n);
|
qtf.resize(n);
|
||||||
R.resize( (n*(n+1))/2);
|
|
||||||
fjac.resize(n, n);
|
fjac.resize(n, n);
|
||||||
fvec.resize(n);
|
fvec.resize(n);
|
||||||
if (mode != 2)
|
if (mode != 2)
|
||||||
@ -536,7 +527,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
|||||||
const int mode
|
const int mode
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int i, j, l;
|
int i, j;
|
||||||
jeval = true;
|
jeval = true;
|
||||||
if (parameters.nb_of_subdiagonals<0) parameters.nb_of_subdiagonals= n-1;
|
if (parameters.nb_of_subdiagonals<0) parameters.nb_of_subdiagonals= n-1;
|
||||||
if (parameters.nb_of_superdiagonals<0) parameters.nb_of_superdiagonals= n-1;
|
if (parameters.nb_of_superdiagonals<0) parameters.nb_of_superdiagonals= n-1;
|
||||||
@ -591,26 +582,13 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* copy the triangular factor of the qr factorization into r. */
|
/* copy the triangular factor of the qr factorization into r. */
|
||||||
|
R = qrfac.matrixQR();
|
||||||
sing = false;
|
sing = false;
|
||||||
for (j = 0; j < n; ++j) {
|
for (j = 0; j < n; ++j)
|
||||||
l = j;
|
if (wa1[j] == 0.) sing = true;
|
||||||
for (i = 0; i < j; ++i) {
|
|
||||||
R[l] = fjac(i,j);
|
|
||||||
l = l + n - i -1;
|
|
||||||
}
|
|
||||||
R[l] = wa1[j];
|
|
||||||
if (wa1[j] == 0.)
|
|
||||||
sing = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* accumulate the orthogonal factor in fjac. */
|
/* accumulate the orthogonal factor in fjac. */
|
||||||
ei_qform<Scalar>(n, n, fjac.data(), fjac.rows(), wa1.data());
|
ei_qform<Scalar>(n, n, fjac.data(), fjac.rows(), wa1.data());
|
||||||
#if 0
|
|
||||||
std::cout << "ei_qform<Scalar>: " << fjac << std::endl;
|
|
||||||
fjac = qrfac.matrixQ();
|
|
||||||
std::cout << "qrfac.matrixQ():" << fjac << std::endl;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* rescale if necessary. */
|
/* rescale if necessary. */
|
||||||
|
|
||||||
@ -653,13 +631,10 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
|||||||
|
|
||||||
/* compute the scaled predicted reduction. */
|
/* compute the scaled predicted reduction. */
|
||||||
|
|
||||||
l = 0;
|
|
||||||
for (i = 0; i < n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
sum = 0.;
|
sum = 0.;
|
||||||
for (j = i; j < n; ++j) {
|
for (j = i; j < n; ++j)
|
||||||
sum += R[l] * wa1[j];
|
sum += R(i,j) * wa1[j];
|
||||||
++l;
|
|
||||||
}
|
|
||||||
wa3[i] = qtf[i] + sum;
|
wa3[i] = qtf[i] + sum;
|
||||||
}
|
}
|
||||||
temp = wa3.stableNorm();
|
temp = wa3.stableNorm();
|
||||||
@ -747,7 +722,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
|||||||
|
|
||||||
/* compute the qr factorization of the updated jacobian. */
|
/* compute the qr factorization of the updated jacobian. */
|
||||||
|
|
||||||
ei_r1updt<Scalar>(n, n, R.data(), R.size(), wa1.data(), wa2.data(), wa3.data(), &sing);
|
ei_r1updt<Scalar>(n, n, R, wa1.data(), wa2.data(), wa3.data(), &sing);
|
||||||
ei_r1mpyq<Scalar>(n, n, fjac.data(), fjac.rows(), wa2.data(), wa3.data());
|
ei_r1mpyq<Scalar>(n, n, fjac.data(), fjac.rows(), wa2.data(), wa3.data());
|
||||||
ei_r1mpyq<Scalar>(1, n, qtf.data(), 1, wa2.data(), wa3.data());
|
ei_r1mpyq<Scalar>(1, n, qtf.data(), 1, wa2.data(), wa3.data());
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
|
|
||||||
template <typename Scalar>
|
template <typename Scalar>
|
||||||
void ei_dogleg(
|
void ei_dogleg(
|
||||||
Matrix< Scalar, Dynamic, 1 > &r,
|
const Matrix< Scalar, Dynamic, Dynamic > &qrfac,
|
||||||
const Matrix< Scalar, Dynamic, 1 > &diag,
|
const Matrix< Scalar, Dynamic, 1 > &diag,
|
||||||
const Matrix< Scalar, Dynamic, 1 > &qtb,
|
const Matrix< Scalar, Dynamic, 1 > &qtb,
|
||||||
Scalar delta,
|
Scalar delta,
|
||||||
Matrix< Scalar, Dynamic, 1 > &x)
|
Matrix< Scalar, Dynamic, 1 > &x)
|
||||||
{
|
{
|
||||||
/* Local variables */
|
/* Local variables */
|
||||||
int i, j, k, l, jj;
|
int i, j, k;
|
||||||
Scalar sum, temp, alpha, bnorm;
|
Scalar sum, temp, alpha, bnorm;
|
||||||
Scalar gnorm, qnorm;
|
Scalar gnorm, qnorm;
|
||||||
Scalar sgnorm;
|
Scalar sgnorm;
|
||||||
@ -20,26 +20,16 @@ void ei_dogleg(
|
|||||||
assert(n==qtb.size());
|
assert(n==qtb.size());
|
||||||
assert(n==x.size());
|
assert(n==x.size());
|
||||||
|
|
||||||
/* first, calculate the gauss-newton direction. */
|
|
||||||
|
|
||||||
jj = n * (n + 1) / 2;
|
|
||||||
for (k = 0; k < n; ++k) {
|
for (k = 0; k < n; ++k) {
|
||||||
j = n - k - 1;
|
j = n - k - 1;
|
||||||
jj -= k+1;
|
|
||||||
l = jj + 1;
|
|
||||||
sum = 0.;
|
sum = 0.;
|
||||||
for (i = j+1; i < n; ++i) {
|
for (i = j+1; i < n; ++i) {
|
||||||
sum += r[l] * x[i];
|
sum += qrfac(j,i) * x[i];
|
||||||
++l;
|
|
||||||
}
|
}
|
||||||
temp = r[jj];
|
temp = qrfac(j,j);
|
||||||
if (temp == 0.) {
|
if (temp == 0.) {
|
||||||
l = j;
|
for (i = 0; i <= j; ++i)
|
||||||
for (i = 0; i <= j; ++i) {
|
temp = std::max(temp,ei_abs(qrfac(i,j)));
|
||||||
/* Computing MAX */
|
|
||||||
temp = std::max(temp,ei_abs(r[l]));
|
|
||||||
l = l + n - i - 1;
|
|
||||||
}
|
|
||||||
temp = epsmch * temp;
|
temp = epsmch * temp;
|
||||||
if (temp == 0.)
|
if (temp == 0.)
|
||||||
temp = epsmch;
|
temp = epsmch;
|
||||||
@ -58,13 +48,10 @@ void ei_dogleg(
|
|||||||
/* the gauss-newton direction is not acceptable. */
|
/* the gauss-newton direction is not acceptable. */
|
||||||
/* next, calculate the scaled gradient direction. */
|
/* next, calculate the scaled gradient direction. */
|
||||||
|
|
||||||
l = 0;
|
|
||||||
for (j = 0; j < n; ++j) {
|
for (j = 0; j < n; ++j) {
|
||||||
temp = qtb[j];
|
temp = qtb[j];
|
||||||
for (i = j; i < n; ++i) {
|
for (i = j; i < n; ++i)
|
||||||
wa1[i] += r[l] * temp;
|
wa1[i] += qrfac(j,i) * temp;
|
||||||
++l;
|
|
||||||
}
|
|
||||||
wa1[j] /= diag[j];
|
wa1[j] /= diag[j];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,16 +68,12 @@ void ei_dogleg(
|
|||||||
/* at which the quadratic is minimized. */
|
/* at which the quadratic is minimized. */
|
||||||
|
|
||||||
wa1.array() /= (diag*gnorm).array();
|
wa1.array() /= (diag*gnorm).array();
|
||||||
l = 0;
|
|
||||||
for (j = 0; j < n; ++j) {
|
for (j = 0; j < n; ++j) {
|
||||||
sum = 0.;
|
sum = 0.;
|
||||||
for (i = j; i < n; ++i) {
|
for (i = j; i < n; ++i) {
|
||||||
sum += r[l] * wa1[i];
|
sum += qrfac(j,i) * wa1[i];
|
||||||
++l;
|
|
||||||
/* L100: */
|
|
||||||
}
|
}
|
||||||
wa2[j] = sum;
|
wa2[j] = sum;
|
||||||
/* L110: */
|
|
||||||
}
|
}
|
||||||
temp = wa2.stableNorm();
|
temp = wa2.stableNorm();
|
||||||
sgnorm = gnorm / temp / temp;
|
sgnorm = gnorm / temp / temp;
|
||||||
|
@ -1,34 +1,27 @@
|
|||||||
|
|
||||||
template <typename Scalar>
|
template <typename Scalar>
|
||||||
void ei_r1updt(int m, int n, Scalar *s, int /* ls */, const Scalar *u, Scalar *v, Scalar *w, bool *sing)
|
void ei_r1updt(int m, int n, Matrix< Scalar, Dynamic, Dynamic > &s, const Scalar *u, Scalar *v, Scalar *w, bool *sing)
|
||||||
{
|
{
|
||||||
/* Local variables */
|
/* Local variables */
|
||||||
int i, j, l, jj, nm1;
|
int i, j, nm1;
|
||||||
Scalar tan__;
|
Scalar tan__;
|
||||||
int nmj;
|
int nmj;
|
||||||
Scalar cos__, sin__, tau, temp, cotan;
|
Scalar cos__, sin__, tau, temp, cotan;
|
||||||
|
|
||||||
|
// ei_r1updt had a broader usecase, but we dont use it here. And, more
|
||||||
|
// importantly, we can not test it.
|
||||||
|
assert(m==n);
|
||||||
|
|
||||||
/* Parameter adjustments */
|
/* Parameter adjustments */
|
||||||
--w;
|
--w;
|
||||||
--u;
|
--u;
|
||||||
--v;
|
--v;
|
||||||
--s;
|
|
||||||
|
|
||||||
/* Function Body */
|
/* Function Body */
|
||||||
const Scalar giant = std::numeric_limits<Scalar>::max();
|
const Scalar giant = std::numeric_limits<Scalar>::max();
|
||||||
|
|
||||||
/* initialize the diagonal element pointer. */
|
|
||||||
|
|
||||||
jj = n * ((m << 1) - n + 1) / 2 - (m - n);
|
|
||||||
|
|
||||||
/* move the nontrivial part of the last column of s into w. */
|
/* move the nontrivial part of the last column of s into w. */
|
||||||
|
w[n] = s(n-1,n-1);
|
||||||
l = jj;
|
|
||||||
for (i = n; i <= m; ++i) {
|
|
||||||
w[i] = s[l];
|
|
||||||
++l;
|
|
||||||
/* L10: */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* rotate the vector v into a multiple of the n-th unit vector */
|
/* rotate the vector v into a multiple of the n-th unit vector */
|
||||||
/* in such a way that a spike is introduced into w. */
|
/* in such a way that a spike is introduced into w. */
|
||||||
@ -39,7 +32,6 @@ void ei_r1updt(int m, int n, Scalar *s, int /* ls */, const Scalar *u, Scalar *v
|
|||||||
}
|
}
|
||||||
for (nmj = 1; nmj <= nm1; ++nmj) {
|
for (nmj = 1; nmj <= nm1; ++nmj) {
|
||||||
j = n - nmj;
|
j = n - nmj;
|
||||||
jj -= m - j + 1;
|
|
||||||
w[j] = 0.;
|
w[j] = 0.;
|
||||||
if (v[j] == 0.) {
|
if (v[j] == 0.) {
|
||||||
goto L50;
|
goto L50;
|
||||||
@ -75,16 +67,12 @@ L30:
|
|||||||
|
|
||||||
/* apply the transformation to s and extend the spike in w. */
|
/* apply the transformation to s and extend the spike in w. */
|
||||||
|
|
||||||
l = jj;
|
|
||||||
for (i = j; i <= m; ++i) {
|
for (i = j; i <= m; ++i) {
|
||||||
temp = cos__ * s[l] - sin__ * w[i];
|
temp = cos__ * s(j-1,i-1) - sin__ * w[i];
|
||||||
w[i] = sin__ * s[l] + cos__ * w[i];
|
w[i] = sin__ * s(j-1,i-1) + cos__ * w[i];
|
||||||
s[l] = temp;
|
s(j-1,i-1) = temp;
|
||||||
++l;
|
|
||||||
/* L40: */
|
|
||||||
}
|
}
|
||||||
L50:
|
L50:
|
||||||
/* L60: */
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
L70:
|
L70:
|
||||||
@ -110,9 +98,9 @@ L70:
|
|||||||
/* determine a givens rotation which eliminates the */
|
/* determine a givens rotation which eliminates the */
|
||||||
/* j-th element of the spike. */
|
/* j-th element of the spike. */
|
||||||
|
|
||||||
if (ei_abs(s[jj]) >= ei_abs(w[j]))
|
if (ei_abs(s(j-1,j-1)) >= ei_abs(w[j]))
|
||||||
goto L90;
|
goto L90;
|
||||||
cotan = s[jj] / w[j];
|
cotan = s(j-1,j-1) / w[j];
|
||||||
/* Computing 2nd power */
|
/* Computing 2nd power */
|
||||||
sin__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(cotan));
|
sin__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(cotan));
|
||||||
cos__ = sin__ * cotan;
|
cos__ = sin__ * cotan;
|
||||||
@ -122,7 +110,7 @@ L70:
|
|||||||
}
|
}
|
||||||
goto L100;
|
goto L100;
|
||||||
L90:
|
L90:
|
||||||
tan__ = w[j] / s[jj];
|
tan__ = w[j] / s(j-1,j-1);
|
||||||
/* Computing 2nd power */
|
/* Computing 2nd power */
|
||||||
cos__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(tan__));
|
cos__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(tan__));
|
||||||
sin__ = cos__ * tan__;
|
sin__ = cos__ * tan__;
|
||||||
@ -131,13 +119,10 @@ L100:
|
|||||||
|
|
||||||
/* apply the transformation to s and reduce the spike in w. */
|
/* apply the transformation to s and reduce the spike in w. */
|
||||||
|
|
||||||
l = jj;
|
|
||||||
for (i = j; i <= m; ++i) {
|
for (i = j; i <= m; ++i) {
|
||||||
temp = cos__ * s[l] + sin__ * w[i];
|
temp = cos__ * s(j-1,i-1) + sin__ * w[i];
|
||||||
w[i] = -sin__ * s[l] + cos__ * w[i];
|
w[i] = -sin__ * s(j-1,i-1) + cos__ * w[i];
|
||||||
s[l] = temp;
|
s(j-1,i-1) = temp;
|
||||||
++l;
|
|
||||||
/* L110: */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store the information necessary to recover the */
|
/* store the information necessary to recover the */
|
||||||
@ -148,28 +133,17 @@ L120:
|
|||||||
|
|
||||||
/* test for zero diagonal elements in the output s. */
|
/* test for zero diagonal elements in the output s. */
|
||||||
|
|
||||||
if (s[jj] == 0.) {
|
if (s(j-1,j-1) == 0.) {
|
||||||
*sing = true;
|
*sing = true;
|
||||||
}
|
}
|
||||||
jj += m - j + 1;
|
|
||||||
/* L130: */
|
|
||||||
}
|
}
|
||||||
L140:
|
L140:
|
||||||
|
|
||||||
/* move w back into the last column of the output s. */
|
/* move w back into the last column of the output s. */
|
||||||
|
s(n-1,n-1) = w[n];
|
||||||
|
|
||||||
l = jj;
|
if (s(j-1,j-1) == 0.) {
|
||||||
for (i = n; i <= m; ++i) {
|
|
||||||
s[l] = w[i];
|
|
||||||
++l;
|
|
||||||
/* L150: */
|
|
||||||
}
|
|
||||||
if (s[jj] == 0.) {
|
|
||||||
*sing = true;
|
*sing = true;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
/* last card of subroutine r1updt. */
|
|
||||||
|
|
||||||
} /* r1updt_ */
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user