#include <stdlib.h>
#include <stdio.h>
#include "NSSTools.h"
#include "SiconosBlas.h"
#include "SiconosLapack.h"


/* Auxiliary routines prototypes */
extern void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda);

/* Parameters */
#define M 6
#define N 5
#define LDA M
#define LDU M
#define LDVT N

/* Main program */
int main()
{
  /* Locals */
  lapack_int m = M, n = N, lda = LDA, ldu = LDU, ldvt = LDVT, info;
  double superb[min(M,N)-1];
  /* Local arrays */
  double s[N], u[LDU*M], vt[LDVT*N];
  double a[LDA*N] =
  {
    8.79,  6.11, -9.15,  9.57, -3.49,  9.84,
    9.93,  6.91, -7.93,  1.64,  4.02,  0.15,
    9.83,  5.04,  4.86,  8.83,  9.80, -8.99,
    5.45, -0.27,  4.85,  0.74, 10.00, -6.02,
    3.16,  7.98,  3.01,  5.80,  4.27, -5.31
  };
  /* Executable statements */
  printf("LAPACKE_dgesvd (column-major, high-level) Example Program Results\n");
  /* Compute SVD */
  info = LAPACKE_dgesvd(LAPACK_COL_MAJOR, 'A', 'A', m, n, a, lda,
                        s, u, ldu, vt, ldvt, superb);
  /* Check for convergence */
  if(info > 0)
  {
    printf("The algorithm computing SVD failed to converge.\n");
    exit(1);
  }
  /* Print singular values */
  print_matrix("Singular values", 1, n, s, 1);
  /* Print left singular vectors */
  print_matrix("Left singular vectors (stored columnwise)", m, n, u, ldu);
  /* Print right singular vectors */
  print_matrix("Right singular vectors (stored rowwise)", n, n, vt, ldvt);
  return 0;
} /* End of LAPACKE_dgesvd Example */

/* Auxiliary routine: printing a matrix */
void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda)
{
  MKL_INT i, j;
  printf("\n %s\n", desc);
  for(i = 0; i < m; i++)
  {
    for(j = 0; j < n; j++) printf(" %6.2f", a[i+j*lda]);
    printf("\n");
  }
}
