title: Solving a system of linear equations using Lapack's dgesv
body: <p>I want to solve a linear equation system using the Lapack package in C++.
  I plan to implement it like <a href="http://www-heller.harvard.edu/people/shaw/programs/dgesv.c" rel="nofollow">this</a> using the routines from <a href="http://www-heller.harvard.edu/people/shaw/programs/lapack.html" rel="nofollow">here</a>, namely dgesv.</p>
  
  <p>This is my code:</p>
  
  <pre><code>unsigned short int n=profentries.size();
  double **A, *b, *x;
  A= new double*[n];
  b=new double[n];
  x=new double[2];
  // Generate the matrices for my equation
  for(int i = 0; i &lt; n; ++i)
  {
      A[i] = new double[2];
  }
  
  for(int i=0; i&lt;n; i++)
  {
      A[i][0]=5;
      A[i][1]=1;
      b[i]=3;
  }
  
  std::cout&lt;&lt; "printing matrix A:"&lt;&lt;std::endl;
  for(int i=0; i&lt;n; i++)
  {
      std::cout&lt;&lt; A[i][0]&lt;&lt;" " &lt;&lt;A[i][1]&lt;&lt;std::endl;
  }
  // Call the LAPACK solver
      x = new double[n];//probably not necessary
  dgesv(A, b, 2, x); //wrong result for x!
  std::cout&lt;&lt; "printing vector x:"&lt;&lt;std::endl;
  
  /*prints 
  3
  3
  but that's wrong, the solution is (0.6, 0)!
  */
  for(int i=0; i&lt;2; i++) 
  {
      std::cout&lt;&lt; x[i]&lt;&lt;std::endl;
  }
  </code></pre>
  
  <p>I have the following problem:</p>
  
  <p>How can it be that dgesv calculates a vector x with the elements {3, 3}? The solution should be {0.6, 0} (checked it with matlab).</p>
  
  <p>Greetings </p>
  
  <p>Edit: dgesv might work for square matrices. My solution below shows how to solve overdetermined systems with dgels.</p>