<?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 Here is code using LAPACKE in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003894#M18778</link>
    <description>&lt;P&gt;Here is code using LAPACKE_dgels(). I faked the "data" by calculating the values of f(x,y) over a rectangle from an assumed polynomial, and multiplying the resulting values by (1 + a small random perturbation). The program fits the polynomial and prints out the original and fitted values of the polynomial coefficients.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include "mkl_lapacke.h"

#define NX 100
#define NY 100
#define N 10
#define NRHS 1
#define LDA N
#define LDB NRHS

int main() {
	MKL_INT m = NX*NY, n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
	int i,j,k;
	double *A,*b,x,y;
	double c[]={1.5, 2.1,-2.7, -3.1,4.5,-6.7, 0.8,-0.7,4.1,-3.7};
	A=(double *)malloc(m*n*sizeof(double));
	b=(double *)malloc(m*sizeof(double));
	k=0;
	for(i=0; i&amp;lt;NX; i++){
	   x=(i+1)*0.01;
	   for(j=0; j&amp;lt;NY; j++){
	      y=(j+1)*0.02;
	      A[k*10]=1;
	      A[k*10+1]=x; A[k*10+2]=y;
	      A[k*10+3]=x*x; A[k*10+4]=x*y; A[k*10+5]=y*y;
	      A[k*10+6]=x*x*x; A[k*10+7]=x*x*y; A[k*10+8]=x*y*y; A[k*10+9]=y*y*y;
	      b&lt;K&gt;=(c[0]+
	            c[1]*x     + c[2]*y     +
	            c[3]*x*x   + c[4]*x*y   + c[5]*y*y   +
	      	    c[6]*x*x*x + c[7]*x*x*y + c[8]*x*y*y + c[9]*y*y*y
	      	    ) * (0.9995+0.001*rand()/(double)RAND_MAX);  // random noise added
	      k++;
	      }
	    }
	printf( "Fitting cubic polynomial in x,y using Lapacke_Dgels\n" );
	/* Solve the overdetermined equations A*X = B */
	info = LAPACKE_dgels( LAPACK_ROW_MAJOR, 'N', m, n, nrhs, A, lda,
			b, ldb );
	/* Check for errors */
	if( info &amp;gt; 0 ) {
		printf( "Error return from DGELS, info = %d\n",info );
		exit( 1 );
	}
	/* Print least squares solution */
	printf( "\nLeast squares solution\n i  Original Fitted\n");
	for(i=0; i&amp;lt; n; i++)printf("%2d %8.5f %8.5f\n",i,c&lt;I&gt;,b&lt;I&gt;);
	exit( 0 );
}
&lt;/I&gt;&lt;/I&gt;&lt;/K&gt;&lt;/PRE&gt;

&lt;P&gt;I have slighted the requirements of code efficiency, favoring clarity instead.&lt;/P&gt;</description>
    <pubDate>Fri, 28 Aug 2015 21:21:00 GMT</pubDate>
    <dc:creator>mecej4</dc:creator>
    <dc:date>2015-08-28T21:21:00Z</dc:date>
    <item>
      <title>Help Using Lapack for Getting Least Squares Fit Polynomial Coefficients</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003891#M18775</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;

&lt;P&gt;I am a first time MKL user trying to use the library to fit a&amp;nbsp;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;3rd-order 2-d polynomial function to&lt;/SPAN&gt;&amp;nbsp;f(x,y).&amp;nbsp;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;This algorithm works using pretty much the same exact approach in Python so I believe it to be conceptually sound.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;I'm trying to use LAPACKE_dgelsy but my program dies whenever it's called, and I'm sure my function arguments are &lt;EM&gt;incorrect&lt;/EM&gt;.&lt;/P&gt;

&lt;P&gt;I created my A matrix to be &amp;nbsp;10 x 60,000, with each of the 10 rows representing a coefficient for {1,x,y,x&lt;SUP&gt;2&lt;/SUP&gt;,xy,y&lt;SUP&gt;2&lt;/SUP&gt;,etc..} at each index of data, and B&lt;I&gt; = f(x&lt;I&gt;,y&lt;I&gt;) (B is size 1 x 60,000). dgelsy only takes a single pointer, so I've had "a" take the form of the array:&amp;nbsp;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;double * a = new double[n * 10];&lt;/PRE&gt;

&lt;P&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;and I just concatenated each row of data one after the other into this array. I'm under the impression that dgelsy's argument values "LAPACK_ROW_MAJOR","m", "n", and "nrhs" will order the data in a more typical A*x=B, m x n, matrix form for all the calculations (at least conceptually, not in actual memory). Do I have the dimensions and input form correct in this?&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;The rest of the code looks like this so far:&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;lapack_int matrixLayout = LAPACK_ROW_MAJOR;
lapack_int n = 60000;
lapack_int lda = n;
lapack_int ldb = n;
m = 10;
lapack_int nrhs = n;
int *jpvt = new int&lt;N&gt;;//No idea what this is
lapack_int rcond = -1; //This either
int *rank = new int&lt;N&gt;; // This is supposed to be an output variable, how should I initialize it?
int LapackResult = LAPACKE_dgelsy(matrixLayout, m, n, nrhs, a, lda, b, ldb, jpvt, rcond, rank);
&lt;/N&gt;&lt;/N&gt;&lt;/PRE&gt;

&lt;P&gt;It crashes the program at the last line obviously. I've looked at all the documentation and I can't figure out what it wants for the arguments: jpvt, rcond, and rank. Could anyone give me more info about what these mean?&lt;/P&gt;

&lt;P&gt;Finally, &lt;S&gt;if&lt;/S&gt;&amp;nbsp;WHEN I do get dgelsy to work, how can I interpret the output (i.e. rank pointer) to find the coefficients of my fitted polynomial?&lt;/P&gt;

&lt;P&gt;Sorry if I'm coming at this completely wrong. I've gotten the IPP library to work very well for me so I've been taking some of the things I learned there and trying to apply them here to no avail.&amp;nbsp;&lt;/P&gt;

&lt;P&gt;I appreciate any help you can offer!&lt;/P&gt;</description>
      <pubDate>Fri, 28 Aug 2015 18:03:04 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003891#M18775</guid>
      <dc:creator>Robert_G_2</dc:creator>
      <dc:date>2015-08-28T18:03:04Z</dc:date>
    </item>
    <item>
      <title>I gather that you have 10000</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003892#M18776</link>
      <description>&lt;P&gt;I gather that you have 60000 "observations" to which you wish to fit a 3rd order polynomial in x and y, i.e., m = 60000 and n=1+2+3+4=10 model parameters. The regressor matrix A should be of size m X n, with m &amp;gt; n, for the overdetermined least squares problem.&lt;/P&gt;

&lt;P&gt;1. You have the row and column sizes interchanged, even at the conceptual level.&amp;nbsp;&lt;/P&gt;

&lt;P&gt;2. GELSY is suited for obtaining the minumum norm x for the underdetermined problem. You should consider using GELS instead, which is intended for minimizing the norm of the error A.x - b.&lt;/P&gt;

&lt;P&gt;3. You are using allocated arrays as arguments where a pointer to a scalar is all that is required. While this is not necessarily harmful, it indicates uncertainty about the interface and the possibility of errors in the argument list.&lt;/P&gt;

&lt;P&gt;4. There is a pair of examples of using lapacke_dgels() in the MKL examples. Start with them and adapt them for your problem.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Aug 2015 18:54:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003892#M18776</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2015-08-28T18:54:00Z</dc:date>
    </item>
    <item>
      <title>Thanks a bunch for the</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003893#M18777</link>
      <description>&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;Thanks a bunch for the response!&lt;/SPAN&gt;&lt;/P&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;&lt;SPAN style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;You're right about point 1, I'm changing it now so that m=60,000 and n = 10.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;

&lt;BLOCKQUOTE style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;
	&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;&lt;SPAN style="font-size: 12px; line-height: 18px;"&gt;I gather that you have 60000 "observations" to which you wish to fit a 3rd order polynomial in x and y, i.e., m = 60000 and n=1+2+3+4=10 model parameters.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;Yes, I have 60,000 data points (f(x,y)) at locations (x,y). Each component of these observations is split into their own array of length 60,000. My matrix A is (now) size m x n.&amp;nbsp;&lt;/P&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;n=10 comes from the desired fitting equation's coefficients:&amp;nbsp;&lt;/P&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;f(x,y) = a&lt;SUB&gt;1&lt;/SUB&gt;*1 + a&lt;SUB&gt;2&lt;/SUB&gt;*x + a&lt;SUB&gt;3&lt;/SUB&gt;*y + a&lt;SUB&gt;4&lt;/SUB&gt;*x&lt;SUP&gt;2&lt;/SUP&gt;&amp;nbsp;+ a&lt;SUB&gt;5&lt;/SUB&gt;*xy + a&lt;SUB&gt;6&lt;/SUB&gt;*y&lt;SUP&gt;2&lt;/SUP&gt;&amp;nbsp;+ a&lt;SUB&gt;7&lt;/SUB&gt;*x&lt;SUP&gt;3&lt;/SUP&gt;&amp;nbsp;+ a&lt;SUB&gt;8&lt;/SUB&gt;*x&lt;SUP&gt;2&lt;/SUP&gt;y + a&lt;SUB&gt;9&lt;/SUB&gt;*xy&lt;SUP&gt;2&lt;/SUP&gt;&amp;nbsp;+ a&lt;SUB&gt;10&lt;/SUB&gt;*y&lt;SUP&gt;3&lt;/SUP&gt;&lt;/P&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;To your other points:&lt;/P&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;2. I will look into it, thanks.&lt;/P&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;3. I am aware that those arguments are incorrect. Could you point me to where I could find how to get those scalar values? Probably in the examples, I assume.&lt;/P&gt;

&lt;P style="font-size: 13.0080003738403px; line-height: 19.5120010375977px;"&gt;4. I will take a look at the example for GELS, I don't have any examples for GELSY so maybe I will have more luck with this.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Aug 2015 20:36:10 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003893#M18777</guid>
      <dc:creator>Robert_G_2</dc:creator>
      <dc:date>2015-08-28T20:36:10Z</dc:date>
    </item>
    <item>
      <title>Here is code using LAPACKE</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003894#M18778</link>
      <description>&lt;P&gt;Here is code using LAPACKE_dgels(). I faked the "data" by calculating the values of f(x,y) over a rectangle from an assumed polynomial, and multiplying the resulting values by (1 + a small random perturbation). The program fits the polynomial and prints out the original and fitted values of the polynomial coefficients.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include "mkl_lapacke.h"

#define NX 100
#define NY 100
#define N 10
#define NRHS 1
#define LDA N
#define LDB NRHS

int main() {
	MKL_INT m = NX*NY, n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
	int i,j,k;
	double *A,*b,x,y;
	double c[]={1.5, 2.1,-2.7, -3.1,4.5,-6.7, 0.8,-0.7,4.1,-3.7};
	A=(double *)malloc(m*n*sizeof(double));
	b=(double *)malloc(m*sizeof(double));
	k=0;
	for(i=0; i&amp;lt;NX; i++){
	   x=(i+1)*0.01;
	   for(j=0; j&amp;lt;NY; j++){
	      y=(j+1)*0.02;
	      A[k*10]=1;
	      A[k*10+1]=x; A[k*10+2]=y;
	      A[k*10+3]=x*x; A[k*10+4]=x*y; A[k*10+5]=y*y;
	      A[k*10+6]=x*x*x; A[k*10+7]=x*x*y; A[k*10+8]=x*y*y; A[k*10+9]=y*y*y;
	      b&lt;K&gt;=(c[0]+
	            c[1]*x     + c[2]*y     +
	            c[3]*x*x   + c[4]*x*y   + c[5]*y*y   +
	      	    c[6]*x*x*x + c[7]*x*x*y + c[8]*x*y*y + c[9]*y*y*y
	      	    ) * (0.9995+0.001*rand()/(double)RAND_MAX);  // random noise added
	      k++;
	      }
	    }
	printf( "Fitting cubic polynomial in x,y using Lapacke_Dgels\n" );
	/* Solve the overdetermined equations A*X = B */
	info = LAPACKE_dgels( LAPACK_ROW_MAJOR, 'N', m, n, nrhs, A, lda,
			b, ldb );
	/* Check for errors */
	if( info &amp;gt; 0 ) {
		printf( "Error return from DGELS, info = %d\n",info );
		exit( 1 );
	}
	/* Print least squares solution */
	printf( "\nLeast squares solution\n i  Original Fitted\n");
	for(i=0; i&amp;lt; n; i++)printf("%2d %8.5f %8.5f\n",i,c&lt;I&gt;,b&lt;I&gt;);
	exit( 0 );
}
&lt;/I&gt;&lt;/I&gt;&lt;/K&gt;&lt;/PRE&gt;

&lt;P&gt;I have slighted the requirements of code efficiency, favoring clarity instead.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Aug 2015 21:21:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003894#M18778</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2015-08-28T21:21:00Z</dc:date>
    </item>
    <item>
      <title>Got it working using dgels</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003895#M18779</link>
      <description>&lt;P&gt;Got it working using dgels and following the LAPACKE_dgels example as well as yours, thanks for the help!&lt;/P&gt;</description>
      <pubDate>Fri, 28 Aug 2015 22:36:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Help-Using-Lapack-for-Getting-Least-Squares-Fit-Polynomial/m-p/1003895#M18779</guid>
      <dc:creator>Robert_G_2</dc:creator>
      <dc:date>2015-08-28T22:36:00Z</dc:date>
    </item>
  </channel>
</rss>

