Dienstag, 25. Dezember 2012

Lapack example - DGELS

Introduction

One of the Lapack routines is DGELS, for solving systems based on matrices.

From the manual

DGELS solves overdetermined or underdetermined systems for GE matrices

Problem setup

Let's assume there is a relationship in the global economic system, and we wish to find a suitable model for the relationship. The data to examine are the two independent variables CON(i) and EXP(i). The dependent variable in this case will be GDP(i). Data is taken from an arbitrary given year. In this example we will use a year for which all needed data is available to us (2011).

Symbol definitions

  • Let i be an integer which identifies a country. For example, 1=USA, 2=CHN, 3=JPN, 4=RUS. Additional integers may be assigned as needed to represent any countable number of countries.
  • Let the symbol CON(i) refer to the energy consumption of the country i for a given year measured in millions of MW*h/yr.
  • Let EXP(i) refer to the value of all exports of the country i in a given year , measured in billions of US$ per year.
  • Let GDP(i) refer to the Gross Domestic Product of country i in a given year, measured in billions of US$ per year.

Data and Matrix Definitions

Collecting the data as described above, we have the following table

Table 1: Sample data for consumptions, exports and GDP

i
Country
CON
EXP
GDP
1
USA
3741.000
1497.000
14991.300
2
CHN
4693.000
1904.000
7203.784
3
JPN
857.700
787.000
5870.357


To define the data, let A and B be matrices defined as follows, according to the data in the table:

To solve the problem, we need to find a matrix x satisfying

In some cases, a solution may be found analytically according to the formula

However, this is not always possible. Let us proceed using the DGELS function from Lapack to solve the stated problem using its internal numerical methods.

Code Listing: lapack20121225-dgels_example.c

 /* Calling DGELS using row-major order */  
 #include <stdio.h>  
 #include <lapacke.h>  
 #include <stdlib.h>  
 #include <assert.h>  
 int main ()  
 {  
   double a[3][3] = { {1, 3741, 1497}, {1, 4693, 1904}, {1, 859.7, 787} };  
   double b[3][1] = { {14991.3}, {7203.784}, {5870.357} };  
   lapack_int info,m,n,lda,ldb,nrhs;  
   int i,j;  
   m = 3;  // m: rows in the matrix a  
   n = 3;  // n: cols in the matrix a  
   nrhs = 1; // nrhs: number of right-hand sides (i.e. number of cols in b)  
   lda = 3; // lda: leading dimension of matrix a (i.e. number of cols in a)   
   ldb = 1; // ldb: leading dimension of matrix b (i.e. number of cols in b)  
   info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*b,ldb);  
   // if info==0, result is written into b and has n rows and nrhs cols  
   assert(info==0);  
   for(i=0;i<n;i++)  
   {  
    for(j=0;j<nrhs;j++)  
    {  
      printf("%lf ",b[i][j]);  
    }  
    printf("\n");  
   }  
   return(info);  
 }  

References

Lapack DGELS Example

http://netlib.org/lapack/lapacke.html#_calling_tt_dgels_tt

List of countries by electricity consumption

http://en.wikipedia.org/wiki/List_of_countries_by_electricity_consumption

List of countries by exports

http://en.wikipedia.org/wiki/List_of_countries_by_exports

List of countries by nominal GDP

http://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)

Keine Kommentare:

Kommentar veröffentlichen