17 #if defined(DEBUG) || defined(WARNINGMSG) 
   29 double inner_product(
const LAVector&, 
const LAVector&);
 
   30 double similarity(
const LAVector&, 
const LASymMatrix&);
 
   31 double sum_of_elements(
const LASymMatrix&);
 
   37 class LASquareMatrix {
 
   39    LASquareMatrix(
unsigned int n) :
 
   41       fData(std::vector<double> (n*n) )
 
   44    double operator()(
unsigned int row, 
unsigned int col)
 const {
 
   45       assert(row<fNRow && col < fNRow);
 
   46       return fData[col+row*fNRow];
 
   49    double& operator()(
unsigned int row, 
unsigned int col) {
 
   50       assert(row<fNRow && col < fNRow);
 
   51       return fData[col+row*fNRow];
 
   54    unsigned int Nrow()
 const { 
return fNRow; }
 
   57    std::vector<double> fData;
 
   61 LASquareMatrix OuterProduct(
const LAVector& v1, 
const LAVector& v2) {
 
   62    assert(v1.size() == v2.size() );
 
   63    LASquareMatrix a(v1.size() );
 
   64    for (
unsigned int i = 0; i < v1.size() ; ++i) {
 
   65       for (
unsigned int j = 0; j < v2.size() ; ++j) {
 
   73 LASquareMatrix MatrixProduct(
const LASymMatrix& m1, 
const LASquareMatrix& m2) {
 
   74    unsigned int n = m1.Nrow();
 
   75    assert(n == m2.Nrow() );
 
   76    LASquareMatrix a( n );
 
   77    for (
unsigned int i = 0; i < n ; ++i) {
 
   78       for (
unsigned int j = 0; j < n ; ++j) {
 
   80          for (
unsigned int k = 0; k < n ; ++k) {
 
   81             a(i,j) += m1(i,k)*m2(k,j);
 
   90 MinimumError BFGSErrorUpdator::Update(
const MinimumState& s0,
 
   91                                          const MinimumParameters& p1,
 
   92                                          const FunctionGradient& g1)
 const {
 
   98    const MnAlgebraicSymMatrix& v0 = s0.Error().InvHessian();
 
   99    MnAlgebraicVector dx = p1.Vec() - s0.Vec();
 
  100    MnAlgebraicVector dg = g1.Vec() - s0.Gradient().Vec();
 
  102    double delgam = inner_product(dx, dg);   
 
  103    double gvg = similarity(dg, v0);   
 
  107    std::cout << 
"dx = " << dx << std::endl;
 
  108    std::cout << 
"dg = " << dg << std::endl;
 
  109    std::cout<<
"delgam= "<<delgam<<
" gvg= "<<gvg<<std::endl;
 
  114       MN_INFO_MSG(
"BFGSErrorUpdator: delgam = 0 : cannot update - return same matrix ");
 
  119    if (delgam < 0)  MN_INFO_MSG(
"BFGSErrorUpdator: delgam < 0 : first derivatives increasing along search line");
 
  125       MN_INFO_MSG(
"BFGSErrorUpdator: gvg <= 0  ");
 
  135    LASquareMatrix a = OuterProduct(dg,dx);
 
  136    LASquareMatrix b = MatrixProduct(v0, a);
 
  138    unsigned int n = v0.Nrow(); 
 
  139    MnAlgebraicSymMatrix v2( n );
 
  140    for (
unsigned int i = 0; i < n; ++i)  { 
 
  141       for (
unsigned int j = i; j < n; ++j)  {
 
  142          v2(i,j) = (b(i,j) + b(j,i))/(delgam);
 
  146    MnAlgebraicSymMatrix vUpd = ( delgam + gvg) * Outer_product(dx) / (delgam * delgam);
 
  150    double sum_upd = sum_of_elements(vUpd);
 
  153    double dcov = 0.5*(s0.Error().Dcovar() + sum_upd/sum_of_elements(vUpd));
 
  156    std::cout << 
"BFGSErrorUpdator - dcov is " << dcov << std::endl;
 
  159    return MinimumError(vUpd, dcov);