<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic strnlsq related routines for nonlinear fitting in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016017#M19488</link>
    <description>&lt;P style="font-size: 12px;"&gt;I want to solve a nonlinear least square fitting problem to estimate a and b.&amp;nbsp;g(x)=a(x^2)+bx.&amp;nbsp;I have two vectors working as the training data. fx1={1.0, 2.0, 3.0, 4.0}; fx2={2.0, 4.0, 6.0, 8.0}. I defined my objective&amp;nbsp;function&amp;nbsp;as f(x) = |g(fx1)-fx2|. It's obvious&amp;nbsp;that&amp;nbsp;the optimal a and b, in this case, should be 0 and 2 respectively, to minimize the objective&amp;nbsp;function.&lt;/P&gt;

&lt;P style="font-size: 12px;"&gt;I used the MKL ?trnlsp related routines without boundary constraints to conduct the fitting. However, it didn't work. My code and the result are attached. It would be great if you guys can help to give some suggestions. Thanks.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;cstdlib&amp;gt;
#include &amp;lt;cmath&amp;gt;
#include &amp;lt;cstdio&amp;gt;
#include &amp;lt;complex&amp;gt;

#include "mkl_rci.h"
#include "mkl_types.h"
#include "mkl_service.h"

using namespace std;

float fx1[4] = {1.0, 2.0, 3.0, 4.0};
float fy1[4] = {2.0, 4.0, 6.0, 8.0};

int main(){

    extern void rss(MKL_INT *, MKL_INT *, float *, float *);
    
    MKL_INT n = 2, m = 4;
    float eps[6];
    float *x = NULL;
    MKL_INT iter1 = 1000, iter2 = 100;
    float rs = 1.0;
    MKL_INT RCI_Request;      // reverse communication interface variable
    MKL_INT successful;
    float *fvec = NULL;
    float *fjac = NULL;
    MKL_INT iter;
    MKL_INT st_cr;
    float r1, r2;
    _TRNSP_HANDLE_t handle;   // TR solver handle
    MKL_INT i;
    MKL_INT error;
    MKL_INT info[6];

    x = (float *) malloc (sizeof (float) * n);
    fvec = (float *) malloc (sizeof (float) * m);
    fjac = (float *) malloc (sizeof (float) * m * n);
    for (i = 0; i &amp;lt; 6; i++)
    {
        eps&lt;I&gt; = 0.00001;
    }
    x[0] = 1.0;
    x[1] = 0.0;

    for (i = 0; i &amp;lt; m; i++)
        fvec&lt;I&gt; = 0.0;
    for (i = 0; i &amp;lt; m * n; i++)
        fjac&lt;I&gt; = 0.0;
    strnlsp_init (&amp;amp;handle, &amp;amp;n, &amp;amp;m, x, eps, &amp;amp;iter1, &amp;amp;iter2, &amp;amp;rs);
    RCI_Request = 0;
    successful = 0;
    while (successful == 0)
    {
        strnlsp_solve (&amp;amp;handle, fvec, fjac, &amp;amp;RCI_Request);
        
        if (RCI_Request == -1 ||
            RCI_Request == -2 ||
            RCI_Request == -3 ||
            RCI_Request == -4 || RCI_Request == -5 || RCI_Request == -6)
            successful = 1;
        if (RCI_Request == 1)
        {
            rss (&amp;amp;m, &amp;amp;n, x, fvec);
        }
        cout &amp;lt;&amp;lt; "RCI_Request = " &amp;lt;&amp;lt; RCI_Request &amp;lt;&amp;lt; endl;
        cout &amp;lt;&amp;lt; "successful = " &amp;lt;&amp;lt; successful &amp;lt;&amp;lt; endl;
    }
    strnlsp_get (&amp;amp;handle, &amp;amp;iter, &amp;amp;st_cr, &amp;amp;r1, &amp;amp;r2);

    for(int i=0; i&amp;lt;n; i++){
        cout &amp;lt;&amp;lt; x&lt;I&gt; &amp;lt;&amp;lt; endl;
    }
    cout &amp;lt;&amp;lt; "iter = " &amp;lt;&amp;lt; iter &amp;lt;&amp;lt; endl;

    strnlsp_delete (&amp;amp;handle);
    free (fjac);
    free (fvec);
    free (x);
    MKL_Free_Buffers ();
    if (r2 &amp;lt; 0.00001)
    {
        printf ("Succeeded\n");
        return 0;
    }
    else
    {
        printf ("Failed\n");
        return 1;
    }
}

/* nonlinear system equations without constraints */
/* routine for extendet powell function calculation
   m     in:     dimension of function value
   n     in:     number of function variables
   x     in:     vector for function calculating
   f     out:    function value f(x) */
void rss(MKL_INT * m, MKL_INT * n, float *x, float *f){

    double temp;
    for(int j=0; j&amp;lt;(*m); j++){
        temp = x[0]*fx1&lt;J&gt;*fx1&lt;J&gt; + x[1]*fx1&lt;J&gt;;
        f&lt;J&gt; = abs(temp - fy1&lt;J&gt;);
        cout &amp;lt;&amp;lt; "f = " &amp;lt;&amp;lt; f&lt;J&gt; &amp;lt;&amp;lt; endl;
    }

    return;
}
&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;f = 1
f = 0
f = 3
f = 8
RCI_Request = 1
successful = 0
RCI_Request = 2
successful = 0
RCI_Request = -4
successful = 1
1
0
iter = 0
Failed&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 15 May 2014 20:05:11 GMT</pubDate>
    <dc:creator>Cheng_C_</dc:creator>
    <dc:date>2014-05-15T20:05:11Z</dc:date>
    <item>
      <title>strnlsq related routines for nonlinear fitting</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016017#M19488</link>
      <description>&lt;P style="font-size: 12px;"&gt;I want to solve a nonlinear least square fitting problem to estimate a and b.&amp;nbsp;g(x)=a(x^2)+bx.&amp;nbsp;I have two vectors working as the training data. fx1={1.0, 2.0, 3.0, 4.0}; fx2={2.0, 4.0, 6.0, 8.0}. I defined my objective&amp;nbsp;function&amp;nbsp;as f(x) = |g(fx1)-fx2|. It's obvious&amp;nbsp;that&amp;nbsp;the optimal a and b, in this case, should be 0 and 2 respectively, to minimize the objective&amp;nbsp;function.&lt;/P&gt;

&lt;P style="font-size: 12px;"&gt;I used the MKL ?trnlsp related routines without boundary constraints to conduct the fitting. However, it didn't work. My code and the result are attached. It would be great if you guys can help to give some suggestions. Thanks.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;cstdlib&amp;gt;
#include &amp;lt;cmath&amp;gt;
#include &amp;lt;cstdio&amp;gt;
#include &amp;lt;complex&amp;gt;

#include "mkl_rci.h"
#include "mkl_types.h"
#include "mkl_service.h"

using namespace std;

float fx1[4] = {1.0, 2.0, 3.0, 4.0};
float fy1[4] = {2.0, 4.0, 6.0, 8.0};

int main(){

    extern void rss(MKL_INT *, MKL_INT *, float *, float *);
    
    MKL_INT n = 2, m = 4;
    float eps[6];
    float *x = NULL;
    MKL_INT iter1 = 1000, iter2 = 100;
    float rs = 1.0;
    MKL_INT RCI_Request;      // reverse communication interface variable
    MKL_INT successful;
    float *fvec = NULL;
    float *fjac = NULL;
    MKL_INT iter;
    MKL_INT st_cr;
    float r1, r2;
    _TRNSP_HANDLE_t handle;   // TR solver handle
    MKL_INT i;
    MKL_INT error;
    MKL_INT info[6];

    x = (float *) malloc (sizeof (float) * n);
    fvec = (float *) malloc (sizeof (float) * m);
    fjac = (float *) malloc (sizeof (float) * m * n);
    for (i = 0; i &amp;lt; 6; i++)
    {
        eps&lt;I&gt; = 0.00001;
    }
    x[0] = 1.0;
    x[1] = 0.0;

    for (i = 0; i &amp;lt; m; i++)
        fvec&lt;I&gt; = 0.0;
    for (i = 0; i &amp;lt; m * n; i++)
        fjac&lt;I&gt; = 0.0;
    strnlsp_init (&amp;amp;handle, &amp;amp;n, &amp;amp;m, x, eps, &amp;amp;iter1, &amp;amp;iter2, &amp;amp;rs);
    RCI_Request = 0;
    successful = 0;
    while (successful == 0)
    {
        strnlsp_solve (&amp;amp;handle, fvec, fjac, &amp;amp;RCI_Request);
        
        if (RCI_Request == -1 ||
            RCI_Request == -2 ||
            RCI_Request == -3 ||
            RCI_Request == -4 || RCI_Request == -5 || RCI_Request == -6)
            successful = 1;
        if (RCI_Request == 1)
        {
            rss (&amp;amp;m, &amp;amp;n, x, fvec);
        }
        cout &amp;lt;&amp;lt; "RCI_Request = " &amp;lt;&amp;lt; RCI_Request &amp;lt;&amp;lt; endl;
        cout &amp;lt;&amp;lt; "successful = " &amp;lt;&amp;lt; successful &amp;lt;&amp;lt; endl;
    }
    strnlsp_get (&amp;amp;handle, &amp;amp;iter, &amp;amp;st_cr, &amp;amp;r1, &amp;amp;r2);

    for(int i=0; i&amp;lt;n; i++){
        cout &amp;lt;&amp;lt; x&lt;I&gt; &amp;lt;&amp;lt; endl;
    }
    cout &amp;lt;&amp;lt; "iter = " &amp;lt;&amp;lt; iter &amp;lt;&amp;lt; endl;

    strnlsp_delete (&amp;amp;handle);
    free (fjac);
    free (fvec);
    free (x);
    MKL_Free_Buffers ();
    if (r2 &amp;lt; 0.00001)
    {
        printf ("Succeeded\n");
        return 0;
    }
    else
    {
        printf ("Failed\n");
        return 1;
    }
}

/* nonlinear system equations without constraints */
/* routine for extendet powell function calculation
   m     in:     dimension of function value
   n     in:     number of function variables
   x     in:     vector for function calculating
   f     out:    function value f(x) */
void rss(MKL_INT * m, MKL_INT * n, float *x, float *f){

    double temp;
    for(int j=0; j&amp;lt;(*m); j++){
        temp = x[0]*fx1&lt;J&gt;*fx1&lt;J&gt; + x[1]*fx1&lt;J&gt;;
        f&lt;J&gt; = abs(temp - fy1&lt;J&gt;);
        cout &amp;lt;&amp;lt; "f = " &amp;lt;&amp;lt; f&lt;J&gt; &amp;lt;&amp;lt; endl;
    }

    return;
}
&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;f = 1
f = 0
f = 3
f = 8
RCI_Request = 1
successful = 0
RCI_Request = 2
successful = 0
RCI_Request = -4
successful = 1
1
0
iter = 0
Failed&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 May 2014 20:05:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016017#M19488</guid>
      <dc:creator>Cheng_C_</dc:creator>
      <dc:date>2014-05-15T20:05:11Z</dc:date>
    </item>
    <item>
      <title>The ?trnlsp solvers are</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016018#M19489</link>
      <description>&lt;P&gt;The ?trnlsp solvers are intended to minimize the euclidean norm, and are not likely to work with the absolute norm. Furthermore, you have not written code to handle the RCI request code 2, which requires you to evaluate the Jacobian.&lt;/P&gt;</description>
      <pubDate>Thu, 15 May 2014 21:57:57 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016018#M19489</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2014-05-15T21:57:57Z</dc:date>
    </item>
    <item>
      <title>Thanks very much for the</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016019#M19490</link>
      <description>&lt;P&gt;Thanks very much for the reply! Your suggestion works.&lt;/P&gt;

&lt;P&gt;I simply added the following sentences and it produced a reasonable result, though not as accurate as I expect, maybe because of the form of the objective function as you pointed out.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt; if (RCI_Request == 2){ 
    sjacobi (&amp;amp;rss, &amp;amp;n, &amp;amp;m, fjac, x, eps);
} &lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 May 2014 22:27:07 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/strnlsq-related-routines-for-nonlinear-fitting/m-p/1016019#M19490</guid>
      <dc:creator>Cheng_C_</dc:creator>
      <dc:date>2014-05-15T22:27:07Z</dc:date>
    </item>
  </channel>
</rss>

