/* L-17 MCS 572 Fri 30 Sep 2016 : triangularsolve_qd.cpp */

/* The program generates a random lower triangular system so that
 * the solution is a vector of all ones.  Interestingly, once the
 * dimension gets larger than 800, roundoff error with doubles has
 * accumulated so much that we have lost all significant digits.
 * Therefore, we use quad double arithmetic. */

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <qd/qd_real.h>

using namespace std;

void random_triangular_system
 ( int n, qd_real **L, qd_real *b );
/*
 * Generates an n-dimensional lower triangular
 * system L y = b with random numbers in [0,1],
 * so all components of the solution y
 * are equal to 1. */

void solve_triangular_system
 ( int n, qd_real **L, qd_real *b, qd_real *y );
/*
 * Solves the system L*y = b. */

void solve_triangular_system_swapped
 ( int n, qd_real **L, qd_real *b, qd_real *y );
/*
 * Solves the system L*y = b, with swapped
 * loops for better parallelization. */

int main ( int argc, char *argv[] )
{
   int n,v;
   char swapped = 'y';
   if(argc > 1)
   {
      n = atoi(argv[1]);
      v = 0;
   }
   else
   {
      cout << "Give the dimension : ";
      cin >> n;
      v = 1;
      cout << "With swapped loops ? (y/n) ";
      cin >> swapped;
   }
   {
      qd_real b[n],y[n];
      int i,j;

      qd_real **L;
      L = (qd_real**) calloc(n,sizeof(qd_real*));
      for(i=0; i<n; i++)
         L[i] = (qd_real*) calloc(n,sizeof(qd_real));

      srand(time(NULL));
      random_triangular_system(n,L,b);
      clock_t tstart,tstop;
      tstart = clock();
      if(swapped == 'y')
         solve_triangular_system_swapped(n,L,b,y);
      else
         solve_triangular_system(n,L,b,y);
      tstop = clock();

      if(v > 0)
      {
         cout << scientific << setprecision(1);
         for(i=0; i<n; i++) cout << " " << y[i];
         cout << endl;
      }
      cout << scientific << setprecision(64) 
           << y[n-1] << endl;

      cout << fixed << setprecision(3);
      cout << "elapsed cpu time "
           << double(tstop-tstart)/CLOCKS_PER_SEC
           << endl;
   }

   return 0;
}

void random_triangular_system
 ( int n, qd_real **L, qd_real *b )
{
   int i,j;
   for(i=0; i<n; i++)
   {
      L[i][i] = 1.0;
      for(j=0; j<i; j++)
      {
         double r = ((double) rand())/RAND_MAX;
         L[i][j] = qd_real(r);
      }
      for(j=i+1; j<n; j++)
         L[i][j] = qd_real(0.0);
   }
   for(i=0; i<n; i++)
   {
      b[i] = qd_real(0.0);
      for(j=0; j<n; j++)
         b[i] = b[i] + L[i][j];
   }
}

void solve_triangular_system
 ( int n, qd_real **L, qd_real *b, qd_real *y )
{
   int i,j;

   for(i=0; i<n; i++)
   {
      y[i] = b[i];
      for(j=0; j<i; j++)
         y[i] = y[i] - L[i][j]*y[j];
   }
}

void solve_triangular_system_swapped
 ( int n, qd_real **L, qd_real *b, qd_real *y )
{
   int i,j;

   for(i=0; i<n; i++) y[i] = b[i];
     
   for(i=1; i<n; i++)
   {
      for(j=i; j<n; j++)
         y[j] = y[j] - L[j][i-1]*y[i-1];
   }
}
