forum.alglib.net

ALGLIB forum
It is currently Mon Mar 08, 2021 12:16 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Why doesn't minlmcreatefgh implementation work here?
PostPosted: Wed Mar 03, 2021 4:52 am 
Offline

Joined: Tue Sep 18, 2018 2:51 pm
Posts: 9
I am solving for rotational transformation coefficients. I gather that minlmcreatefgh wants you to calculate the sum of square of the residuals over all points (f), and calculate the gradient and hessian for the sum of squares function summed over all points:

Code:
void JPFITS::JPMath::alglib_WCSTransform2d_func(array<double>^ p, double %f, Object^ obj)
{
   array<Object^>^ objj = (array<Object^>^)obj;
   array<double>^ x_pix = (array<double>^)objj[0];
   array<double>^ y_pix = (array<double>^)objj[1];
   array<double>^ x_intrmdt = (array<double>^)objj[2];
   array<double>^ y_intrmdt = (array<double>^)objj[3];
   f = 0;

   double cosp1 = Math::Cos(p[1]);
   double sinp1 = Math::Sin(p[1]);
   double p2mxpixi, p3mypixi, xres, yres;

   for (int i = 0; i < x_intrmdt->Length; i++)
   {
      p2mxpixi = p[2] - x_pix[i];
      p3mypixi = p[3] - y_pix[i];
      xres = (x_intrmdt[i] + p[0] * (cosp1*p2mxpixi - sinp1 * p3mypixi));
      yres = (y_intrmdt[i] + p[0] * (cosp1*p3mypixi + sinp1 * p2mxpixi));
      f += xres * xres + yres * yres;
   }
}


Code:
void JPFITS::JPMath::alglib_WCSTransform2d_funcGrad(array<double>^ p, double %f, array<double>^ grad, Object^ obj)
{
   array<Object^>^ objj = (array<Object^>^)obj;
   array<double>^ x_pix = (array<double>^)objj[0];
   array<double>^ y_pix = (array<double>^)objj[1];
   array<double>^ x_intrmdt = (array<double>^)objj[2];
   array<double>^ y_intrmdt = (array<double>^)objj[3];

   for (int i = 0; i < grad->Length; i++)
      grad[i] = 0;

   alglib_WCSTransform2d_func(p, f, obj);

   double cosp1 = Math::Cos(p[1]);
   double sinp1 = Math::Sin(p[1]);
   double p2mxpixi, p3mypixi, xres, yres;

   for (int i = 0; i < x_intrmdt->Length; i++)
   {
      p2mxpixi = p[2] - x_pix[i];
      p3mypixi = p[3] - y_pix[i];
      xres = (x_intrmdt[i] + p[0] * (cosp1*p2mxpixi - sinp1 * p3mypixi));
      yres = (y_intrmdt[i] + p[0] * (cosp1*p3mypixi + sinp1 * p2mxpixi));

      grad[0] += 2 * (cosp1*p2mxpixi - sinp1 * p3mypixi)*xres + 2 * (cosp1*p3mypixi + sinp1 * p2mxpixi)*yres;
      grad[1] += 2 * p[0] * (cosp1*p2mxpixi - sinp1 * p3mypixi)*yres - 2 * p[0] * (cosp1*p3mypixi + sinp1 * p2mxpixi)*xres;
      grad[2] += 2 * p[0] * cosp1*xres + 2 * p[0] * sinp1*yres;
      grad[3] += 2 * p[0] * cosp1*yres - 2 * p[0] * sinp1*xres;
   }
}


Code:
void JPFITS::JPMath::alglib_WCSTransform2d_funcHess(array<double>^ p, double %f, array<double>^ grad, array<double, 2>^ hess, Object^ obj)
{
   array<Object^>^ objj = (array<Object^>^)obj;
   array<double>^ x_pix = (array<double>^)objj[0];
   array<double>^ y_pix = (array<double>^)objj[1];
   array<double>^ x_intrmdt = (array<double>^)objj[2];
   array<double>^ y_intrmdt = (array<double>^)objj[3];

   for (int i = 0; i < hess->GetLength(0); i++)
      for (int j = 0; j < hess->GetLength(1); j++)
         hess[i, j] = 0;

   alglib_WCSTransform2d_funcGrad(p, f, grad, obj);

   double cosp1 = Math::Cos(p[1]);
   double sinp1 = Math::Sin(p[1]);
   double p0sq = p[0] * p[0];
   double p2mxpixi, p3mypixi, xres, yres;      

   for (int i = 0; i < x_intrmdt->Length; i++)
   {
      p2mxpixi = p[2] - x_pix[i];
      p3mypixi = p[3] - y_pix[i];
      xres = cosp1*p3mypixi + sinp1 * p2mxpixi;
      yres = cosp1*p2mxpixi - sinp1 * p3mypixi;

      hess[0, 0] += 2 * xres * xres + 2 * yres * yres;
      hess[1, 1] += 2 *p0sq * xres * xres + 2 *p0sq * yres * yres - 2 * p[0]*yres*(x_intrmdt[i] + p[0] * yres) - 2 * p[0]*xres*(y_intrmdt[i] + p[0] * xres);
      hess[2, 2] += 2 *p0sq * cosp1 * cosp1 + 2 *p0sq * sinp1 * sinp1;
      hess[3, 3] += 2 *p0sq * cosp1 * cosp1 + 2 *p0sq * sinp1 * sinp1;
      hess[0, 1] += 2 * yres*(y_intrmdt[i] + p[0] * xres) - 2 * xres*(x_intrmdt[i] + p[0] * yres);
      hess[0, 2] += 2 * sinp1*(y_intrmdt[i] + p[0] * xres) + 2 * cosp1*(x_intrmdt[i] + p[0] * yres) + 2 * p[0]*cosp1*yres + 2 * p[0]*sinp1*xres;
      hess[0, 3] += 2 * cosp1*(y_intrmdt[i] + p[0] * xres) - 2 * sinp1*(x_intrmdt[i] + p[0] * yres) + 2 * p[0]*cosp1*xres - 2 * p[0]*sinp1*yres;
      hess[1, 2] += 2 *p0sq * sinp1*yres - 2 *p0sq * cosp1*xres + 2 * p[0]*cosp1*(y_intrmdt[i] + p[0] * xres) - 2 * p[0]*sinp1*(x_intrmdt[i] + p[0] * yres);
      hess[1, 3] += 2 *p0sq * cosp1*yres + 2 *p0sq * sinp1*xres - 2 * p[0]*cosp1*(x_intrmdt[i] + p[0] * yres) - 2 * p[0]*sinp1*(y_intrmdt[i] + p[0] * xres);
   }
   hess[1, 0] = hess[0, 1];
   hess[2, 0] = hess[0, 2];
   hess[3, 0] = hess[0, 3];
   hess[2, 1] = hess[1, 2];
   hess[3, 1] = hess[1, 3];
}


It just doesn't want to solve, and I can't figure out why, unless I'm using the implementation incorrectly?

I have this working via the fvec minlmcreatevj method, but I would like to know why the fgh implementation isn't working.

The minlmcreatevj is this, if you would like to see:

Code:
void JPFITS::JPMath::alglib_WCSTransform2d_fvec(array<double>^ p, array<double>^ f, Object^ obj)
{
   array<Object^>^ objj = (array<Object^>^)obj;
   array<double>^ x_pix = (array<double>^)objj[0];
   array<double>^ y_pix = (array<double>^)objj[1];
   array<double>^ x_intrmdt = (array<double>^)objj[2];
   array<double>^ y_intrmdt = (array<double>^)objj[3];

   double xres, yres;

   double cosp1 = Math::Cos(p[1]);
   double sinp1 = Math::Sin(p[1]);
   double xpiximp2, ypiximp3;

   for (int i = 0; i < x_intrmdt->Length; i++)
   {
      xpiximp2 = x_pix[i] - p[2];
      ypiximp3 = y_pix[i] - p[3];

      xres = p[0] * (cosp1 * xpiximp2 - sinp1 * ypiximp3) - x_intrmdt[i];
      yres = p[0] * (sinp1 * xpiximp2 + cosp1 * ypiximp3) - y_intrmdt[i];

      f[i] = Math::Sqrt(xres * xres + yres * yres);
   }
}


Code:
void JPFITS::JPMath::alglib_WCSTransform2d_jac(array<double>^ p, array<double>^ f, array<double, 2>^ jac, Object^ obj)
{
   array<Object^>^ objj = (array<Object^>^)obj;
   array<double>^ x_pix = (array<double>^)objj[0];
   array<double>^ y_pix = (array<double>^)objj[1];
   array<double>^ x_intrmdt = (array<double>^)objj[2];
   array<double>^ y_intrmdt = (array<double>^)objj[3];
   
   alglib_WCSTransform2d_fvec(p, f, obj);

   double cosp1 = Math::Cos(p[1]);
   double sinp1 = Math::Sin(p[1]);
   double p2mxpixi, p3mypixi, cosp1p2mxpixisinp1p3mypixi, twox_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisqroot, x_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisq, cosp1p3mypixisinp1p2mxpixi, x_intrmdtip0cosp1p2mxpixisinp1p3mypixi, y_intrmdtip0cosp1p3mypixisinp1p2mxpixi, x_intrmdtip0cosp1p2mxpixisinp1p3mypixisq, y_intrmdtip0cosp1p3mypixisinp1p2mxpixisq;

   for (int i = 0; i < x_intrmdt->Length; i++)
   {
      p2mxpixi = p[2] - x_pix[i];
      p3mypixi = p[3] - y_pix[i];
      cosp1p2mxpixisinp1p3mypixi = cosp1 * p2mxpixi - sinp1 * p3mypixi;
      cosp1p3mypixisinp1p2mxpixi = cosp1 * p3mypixi + sinp1 * p2mxpixi;
      x_intrmdtip0cosp1p2mxpixisinp1p3mypixi = x_intrmdt[i] + p[0] * cosp1p2mxpixisinp1p3mypixi;
      y_intrmdtip0cosp1p3mypixisinp1p2mxpixi = y_intrmdt[i] + p[0] * cosp1p3mypixisinp1p2mxpixi;
      x_intrmdtip0cosp1p2mxpixisinp1p3mypixisq = x_intrmdtip0cosp1p2mxpixisinp1p3mypixi * x_intrmdtip0cosp1p2mxpixisinp1p3mypixi;
      y_intrmdtip0cosp1p3mypixisinp1p2mxpixisq = y_intrmdtip0cosp1p3mypixisinp1p2mxpixi * y_intrmdtip0cosp1p3mypixisinp1p2mxpixi;
      x_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisq = x_intrmdtip0cosp1p2mxpixisinp1p3mypixisq + y_intrmdtip0cosp1p3mypixisinp1p2mxpixisq;
      twox_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisqroot = 2 * Math::Sqrt(x_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisq);

      jac[i, 0] = (2 * cosp1p2mxpixisinp1p3mypixi*x_intrmdtip0cosp1p2mxpixisinp1p3mypixi + 2 * cosp1p3mypixisinp1p2mxpixi*y_intrmdtip0cosp1p3mypixisinp1p2mxpixi) / twox_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisqroot;
      jac[i, 1] = -(2 * p[0] * cosp1p3mypixisinp1p2mxpixi*x_intrmdtip0cosp1p2mxpixisinp1p3mypixi - 2 * p[0] * cosp1p2mxpixisinp1p3mypixi*y_intrmdtip0cosp1p3mypixisinp1p2mxpixi) / twox_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisqroot;
      jac[i, 2] = (2 * p[0] * cosp1*x_intrmdtip0cosp1p2mxpixisinp1p3mypixi + 2 * p[0] * sinp1*y_intrmdtip0cosp1p3mypixisinp1p2mxpixi) / twox_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisqroot;
      jac[i, 3] = (2 * p[0] * cosp1*y_intrmdtip0cosp1p3mypixisinp1p2mxpixi - 2 * p[0] * sinp1*x_intrmdtip0cosp1p2mxpixisinp1p3mypixi) / twox_intrmdtip0cosp1p2mxpixisinp1p3mypixisqy_intrmdtip0cosp1p3mypixisinp1p2mxpixisqroot;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group