#include <gsl/gsl_test.h>
#include <gsl/gsl_ieee_utils.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_cblas.h>

#include "tests.h"

void
test_gemm (void) {
const double flteps = 1e-4, dbleps = 1e-6;
  {
   int order = 101;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = 1.0f;
   float beta = 0.0f;
   float A[] = { 0.199f, 0.237f, 0.456f, 0.377f };
   int lda = 4;
   float B[] = { 0.842f, -0.734f, 0.323f, -0.957f, -0.303f, -0.873f, -0.871f, -0.819f };
   int ldb = 2;
   float C[] = { 0.498f, -0.925f };
   int ldc = 2;
   float C_expected[] = { -0.222426f, -1.07973f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1466)");
     }
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = 1.0f;
   float beta = 0.0f;
   float A[] = { -0.83f, 0.922f, -0.228f, -0.003f };
   int lda = 1;
   float B[] = { 0.072f, 0.345f, 0.944f, -0.39f, -0.577f, 0.656f, -0.693f, -0.453f };
   int ldb = 4;
   float C[] = { 0.583f, 0.522f };
   int ldc = 1;
   float C_expected[] = { 0.044268f, 1.24311f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1467)");
     }
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = 0.1f;
   float beta = 0.1f;
   float A[] = { -0.838f, 0.622f, -0.494f, 0.304f };
   int lda = 4;
   float B[] = { 0.147f, 0.134f, 0.169f, 0.734f, -0.7f, 0.541f, -0.794f, -0.256f };
   int ldb = 4;
   float C[] = { -0.632f, -0.559f };
   int ldc = 2;
   float C_expected[] = { -0.0532188f, 0.0678514f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1468)");
     }
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = 0.1f;
   float beta = 0.1f;
   float A[] = { -0.937f, 0.635f, 0.596f, -0.51f };
   int lda = 1;
   float B[] = { -0.688f, -0.265f, 0.049f, 0.133f, -0.918f, -0.147f, 0.977f, -0.21f };
   int ldb = 2;
   float C[] = { 0.844f, 0.999f };
   int ldc = 1;
   float C_expected[] = { 0.0474373f, 0.135125f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1469)");
     }
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = -0.3f;
   float beta = 0.1f;
   float A[] = { -0.165f, 0.638f, 0.346f, -0.697f };
   int lda = 1;
   float B[] = { 0.499f, -0.73f, 0.262f, 0.759f, 0.664f, 0.997f, -0.702f, -0.839f };
   int ldb = 2;
   float C[] = { 0.17f, 0.425f };
   int ldc = 2;
   float C_expected[] = { -0.224158f, -0.417831f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1470)");
     }
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = -0.3f;
   float beta = 0.1f;
   float A[] = { -0.603f, -0.714f, -0.893f, 0.046f };
   int lda = 4;
   float B[] = { 0.859f, -0.694f, -0.868f, -0.98f, -0.103f, 0.567f, -0.277f, -0.734f };
   int ldb = 4;
   float C[] = { 0.517f, -0.622f };
   int ldc = 1;
   float C_expected[] = { -0.160575f, -0.0234604f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1471)");
     }
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = 0.1f;
   float beta = 1.0f;
   float A[] = { -0.087f, -0.047f, -0.051f, -0.615f };
   int lda = 1;
   float B[] = { -0.722f, -0.077f, 0.563f, 0.501f, 0.855f, 0.605f, 0.556f, -0.627f };
   int ldb = 4;
   float C[] = { -0.181f, -0.89f };
   int ldc = 2;
   float C_expected[] = { -0.208039f, -0.864557f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1472)");
     }
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha = 0.1f;
   float beta = 1.0f;
   float A[] = { -0.753f, -0.074f, -0.247f, -0.19f };
   int lda = 4;
   float B[] = { 0.061f, 0.743f, 0.22f, -0.682f, 0.733f, 0.417f, 0.772f, 0.665f };
   int ldb = 2;
   float C[] = { -0.253f, 0.972f };
   int ldc = 1;
   float C_expected[] = { -0.291994f, 0.898164f };
   cblas_sgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], flteps, "sgemm(case 1473)");
     }
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = 0;
   double beta = 0;
   double A[] = { 0.017, 0.191, 0.863, -0.97 };
   int lda = 4;
   double B[] = { -0.207, -0.916, -0.278, 0.403, 0.885, 0.409, -0.772, -0.27 };
   int ldb = 2;
   double C[] = { -0.274, -0.858 };
   int ldc = 2;
   double C_expected[] = { 0.0, 0.0 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1474)");
     }
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = 0;
   double beta = 0;
   double A[] = { 0.571, 0.081, 0.109, 0.988 };
   int lda = 1;
   double B[] = { -0.048, -0.753, -0.8, -0.89, -0.535, -0.017, -0.018, -0.544 };
   int ldb = 4;
   double C[] = { -0.876, -0.792 };
   int ldc = 1;
   double C_expected[] = { 0.0, 0.0 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1475)");
     }
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = -0.3;
   double beta = 1;
   double A[] = { 0.939, 0.705, 0.977, 0.4 };
   int lda = 4;
   double B[] = { -0.089, -0.822, 0.937, 0.159, 0.789, -0.413, -0.172, 0.88 };
   int ldb = 4;
   double C[] = { -0.619, 0.063 };
   int ldc = 2;
   double C_expected[] = { -0.7137904, -0.1270986 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1476)");
     }
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = -0.3;
   double beta = 1;
   double A[] = { -0.795, 0.81, 0.388, 0.09 };
   int lda = 1;
   double B[] = { -0.847, 0.031, -0.938, 0.09, -0.286, -0.478, -0.981, 0.881 };
   int ldb = 2;
   double C[] = { -0.242, -0.02 };
   int ldc = 1;
   double C_expected[] = { -0.1562981, -0.0026243 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1477)");
     }
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = -1;
   double beta = 0;
   double A[] = { -0.556, 0.532, 0.746, 0.673 };
   int lda = 1;
   double B[] = { -0.525, 0.967, 0.687, -0.024, 0.527, 0.485, 0.109, -0.46 };
   int ldb = 2;
   double C[] = { -0.495, 0.859 };
   int ldc = 2;
   double C_expected[] = { -1.123883, 0.49819 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1478)");
     }
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = -1;
   double beta = 0;
   double A[] = { -0.358, 0.224, -0.941, 0.513 };
   int lda = 4;
   double B[] = { -0.201, -0.159, -0.586, -0.016, -0.324, 0.411, 0.115, -0.229 };
   int ldb = 4;
   double C[] = { 0.558, 0.596 };
   int ldc = 1;
   double C_expected[] = { -0.57956, 0.017636 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1479)");
     }
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = -0.3;
   double beta = 1;
   double A[] = { -0.586, 0.809, 0.709, -0.524 };
   int lda = 1;
   double B[] = { 0.768, 0.7, 0.619, -0.478, -0.129, -0.778, -0.432, 0.454 };
   int ldb = 4;
   double C[] = { 0.042, 0.252 };
   int ldc = 2;
   double C_expected[] = { -0.1996785, 0.5813976 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1480)");
     }
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha = -0.3;
   double beta = 1;
   double A[] = { -0.164, 0.522, 0.948, -0.624 };
   int lda = 4;
   double B[] = { -0.142, 0.778, 0.359, 0.622, -0.637, -0.757, -0.282, -0.805 };
   int ldb = 2;
   double C[] = { -0.09, 0.183 };
   int ldc = 1;
   double C_expected[] = { -0.0248334, 0.1884672 };
   cblas_dgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[i], C_expected[i], dbleps, "dgemm(case 1481)");
     }
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 1.0f};
   float beta[2] = {0.0f, 0.0f};
   float A[] = { -0.082f, -0.281f, -0.096f, 0.913f, 0.974f, -0.706f, -0.773f, 0.522f };
   int lda = 4;
   float B[] = { 0.745f, -0.664f, 0.352f, -0.733f, 0.304f, -0.555f, -0.493f, -0.089f, 0.188f, 0.631f, 0.235f, 0.152f, -0.299f, -0.731f, -0.686f, -0.332f };
   int ldb = 2;
   float C[] = { -0.179f, -0.284f, -0.996f, -0.414f };
   int ldc = 2;
   float C_expected[] = { -1.06679f, 1.47116f, 0.599689f, 0.933532f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1482) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1482) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 1.0f};
   float beta[2] = {0.0f, 0.0f};
   float A[] = { 0.044f, -0.33f, 0.279f, 0.712f, -0.363f, -0.788f, -0.768f, -0.551f };
   int lda = 1;
   float B[] = { 0.138f, 0.927f, -0.178f, -0.864f, 0.888f, 0.844f, -0.199f, 0.706f, -0.034f, 0.483f, 0.499f, 0.664f, 0.648f, 0.324f, 0.97f, 0.609f };
   int ldb = 4;
   float C[] = { -0.129f, 0.842f, 0.214f, -0.626f };
   int ldc = 1;
   float C_expected[] = { 1.81122f, 1.76205f, 1.0574f, -0.564966f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1483) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1483) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.0f};
   float beta[2] = {-1.0f, 0.0f};
   float A[] = { 0.812f, -0.471f, 0.241f, 0.795f, 0.439f, 0.131f, -0.636f, 0.531f };
   int lda = 4;
   float B[] = { 0.062f, 0.807f, 0.873f, 0.372f, 0.239f, 0.804f, 0.537f, -0.954f, -0.396f, 0.838f, 0.081f, 0.15f, 0.489f, -0.438f, 0.165f, 0.429f };
   int ldb = 4;
   float C[] = { 0.868f, 0.329f, -0.509f, 0.724f };
   int ldc = 2;
   float C_expected[] = { -0.868f, -0.329f, 0.509f, -0.724f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1484) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1484) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.0f};
   float beta[2] = {-1.0f, 0.0f};
   float A[] = { 0.832f, 0.198f, 0.794f, -0.522f, -0.319f, 0.578f, 0.332f, 0.746f };
   int lda = 1;
   float B[] = { -0.361f, 0.187f, -0.163f, -0.781f, 0.536f, 0.888f, -0.969f, 0.899f, 0.961f, -0.583f, 0.753f, 0.29f, -0.997f, 0.729f, -0.352f, -0.2f };
   int ldb = 2;
   float C[] = { 0.864f, 0.735f, -0.074f, -0.228f };
   int ldc = 1;
   float C_expected[] = { -0.864f, -0.735f, 0.074f, 0.228f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1485) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1485) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 1.0f};
   float beta[2] = {0.0f, 0.1f};
   float A[] = { 0.149f, 0.187f, 0.263f, -0.715f, -0.882f, -0.907f, 0.87f, -0.527f };
   int lda = 4;
   float B[] = { -0.915f, -0.249f, -0.986f, -0.799f, -0.136f, 0.712f, 0.964f, 0.799f, -0.569f, 0.686f, 0.603f, 0.758f, 0.161f, -0.698f, -0.263f, -0.256f };
   int ldb = 4;
   float C[] = { 0.622f, -0.824f, -0.482f, -0.161f };
   int ldc = 2;
   float C_expected[] = { -0.246901f, 0.083044f, 1.25556f, 0.009106f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1486) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1486) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 1.0f};
   float beta[2] = {0.0f, 0.1f};
   float A[] = { 0.963f, -0.943f, -0.734f, -0.253f, 0.832f, 0.545f, -0.815f, -0.434f };
   int lda = 1;
   float B[] = { 0.23f, -0.211f, 0.906f, 0.232f, -0.339f, 0.597f, -0.919f, 0.793f, 0.535f, 0.526f, 0.119f, 0.053f, 0.751f, 0.044f, 0.752f, -0.469f };
   int ldb = 2;
   float C[] = { 0.483f, -0.266f, -0.224f, -0.692f };
   int ldc = 1;
   float C_expected[] = { -0.047537f, 0.667177f, 1.02025f, 0.823778f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1487) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1487) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {-0.3f, 0.1f};
   float beta[2] = {-1.0f, 0.0f};
   float A[] = { -0.657f, -0.497f, -0.293f, -0.168f, -0.943f, -0.181f, 0.569f, 0.91f };
   int lda = 1;
   float B[] = { -0.047f, 0.796f, -0.913f, 0.998f, 0.365f, 0.467f, -0.627f, -0.523f, 0.885f, 0.234f, -0.494f, 0.071f, -0.361f, -0.154f, -0.055f, -0.32f };
   int ldb = 2;
   float C[] = { 0.956f, 0.268f, 0.152f, 0.717f };
   int ldc = 2;
   float C_expected[] = { -0.668685f, 0.134477f, -0.715786f, -0.478065f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1488) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1488) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {-0.3f, 0.1f};
   float beta[2] = {-1.0f, 0.0f};
   float A[] = { 0.394f, -0.482f, 0.631f, -0.833f, 0.221f, 0.672f, 0.2f, 0.967f };
   int lda = 4;
   float B[] = { 0.708f, 0.695f, 0.111f, -0.912f, 0.376f, 0.606f, -0.997f, -0.741f, 0.349f, 0.543f, 0.372f, -0.563f, 0.129f, -0.295f, -0.672f, -0.95f };
   int ldb = 4;
   float C[] = { 0.436f, 0.752f, 0.074f, 0.209f };
   int ldc = 1;
   float C_expected[] = { -0.325083f, -0.301952f, -0.283022f, 0.339919f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1489) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1489) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {1.0f, 0.0f};
   float beta[2] = {-0.3f, 0.1f};
   float A[] = { 0.827f, -0.862f, 0.373f, -0.265f, -0.9f, 0.892f, -0.319f, 0.151f };
   int lda = 1;
   float B[] = { 0.603f, 0.816f, -0.511f, 0.831f, -0.36f, -0.954f, -0.978f, 0.485f, 0.675f, 0.186f, 0.463f, 0.144f, 0.851f, -0.458f, 0.766f, -0.213f };
   int ldb = 4;
   float C[] = { -0.335f, 0.333f, -0.4f, 0.422f };
   int ldc = 2;
   float C_expected[] = { 2.7126f, 0.702111f, 0.437661f, 0.691294f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1490) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1490) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {1.0f, 0.0f};
   float beta[2] = {-0.3f, 0.1f};
   float A[] = { 0.966f, 0.476f, -0.013f, -0.655f, 0.773f, -0.543f, -0.231f, -0.353f };
   int lda = 4;
   float B[] = { -0.684f, 0.144f, 0.018f, -0.77f, -0.688f, 0.909f, -0.094f, -0.938f, -0.757f, 0.574f, -0.479f, 0.473f, 0.0f, 0.064f, -0.168f, 0.858f };
   int ldb = 2;
   float C[] = { -0.912f, 0.54f, 0.756f, 0.024f };
   int ldc = 1;
   float C_expected[] = { -0.156236f, 0.839112f, -0.230206f, -0.106256f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1491) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1491) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.0f};
   float beta[2] = {1.0f, 0.0f};
   float A[] = { 0.66f, -0.113f, -0.663f, -0.856f, 0.614f, -0.344f, -0.964f, -0.532f };
   int lda = 1;
   float B[] = { -0.606f, -0.965f, -0.279f, -0.312f, 0.63f, 0.967f, 0.041f, -0.557f, 0.663f, 0.619f, -0.134f, 0.261f, -0.388f, 0.525f, 0.222f, 0.538f };
   int ldb = 4;
   float C[] = { 0.114f, -0.376f, -0.851f, -0.682f };
   int ldc = 2;
   float C_expected[] = { 0.114f, -0.376f, -0.851f, -0.682f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1492) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1492) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.0f};
   float beta[2] = {1.0f, 0.0f};
   float A[] = { 0.212f, -0.752f, 0.679f, 0.49f, -0.029f, -0.488f, 0.567f, 0.374f };
   int lda = 4;
   float B[] = { -0.914f, 0.734f, -0.845f, 0.059f, -0.297f, 0.152f, -0.417f, -0.669f, 0.831f, -0.544f, 0.022f, 0.102f, -0.379f, -0.357f, -0.394f, -0.588f };
   int ldb = 2;
   float C[] = { -0.584f, 0.373f, 0.235f, 0.521f };
   int ldc = 1;
   float C_expected[] = { -0.584f, 0.373f, 0.235f, 0.521f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1493) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1493) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 113;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.1f};
   float beta[2] = {-1.0f, 0.0f};
   float A[] = { 0.135f, 0.128f, 0.909f, -0.963f, 0.299f, -0.944f, 0.944f, 0.942f };
   int lda = 1;
   float B[] = { 0.924f, -0.317f, -0.992f, -0.854f, -0.435f, 0.102f, 0.126f, 0.862f, 0.952f, 0.68f, 0.545f, 0.168f, 0.752f, 0.549f, 0.687f, -0.76f };
   int ldb = 2;
   float C[] = { -0.369f, -0.33f, 0.849f, -0.632f };
   int ldc = 2;
   float C_expected[] = { 0.326537f, 0.37603f, -0.86067f, 0.529817f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1494) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1494) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 113;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.1f};
   float beta[2] = {-1.0f, 0.0f};
   float A[] = { 0.061f, -0.271f, -0.043f, -0.023f, 0.694f, 0.333f, 0.733f, -0.967f };
   int lda = 4;
   float B[] = { 0.088f, -0.607f, 0.589f, 0.375f, -0.897f, -0.954f, -0.216f, -0.195f, -0.865f, -0.511f, -0.219f, 0.535f, 0.976f, 0.582f, 0.464f, -0.041f };
   int ldb = 4;
   float C[] = { 0.533f, -0.63f, 0.405f, 0.667f };
   int ldc = 1;
   float C_expected[] = { -0.459906f, 0.552595f, -0.425391f, -0.533626f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1495) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1495) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 113;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.0f};
   float beta[2] = {1.0f, 0.0f};
   float A[] = { -0.676f, -0.116f, 0.707f, -0.256f, -0.893f, -0.966f, 0.159f, -0.246f };
   int lda = 1;
   float B[] = { 0.059f, 0.281f, -0.93f, -0.263f, 0.583f, -0.11f, 0.639f, -0.96f, -0.878f, 0.984f, 0.058f, 0.977f, -0.567f, 0.561f, -0.048f, -0.798f };
   int ldb = 4;
   float C[] = { 0.362f, -0.808f, 0.428f, -0.112f };
   int ldc = 2;
   float C_expected[] = { 0.362f, -0.808f, 0.428f, -0.112f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1496) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1496) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 113;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.0f};
   float beta[2] = {1.0f, 0.0f};
   float A[] = { -0.915f, 0.439f, 0.171f, -0.019f, 0.843f, 0.944f, -0.581f, 0.856f };
   int lda = 4;
   float B[] = { -0.284f, 0.207f, -0.27f, 0.832f, 0.894f, -0.626f, -0.305f, -0.006f, 0.562f, -0.744f, -0.533f, 0.126f, -0.375f, -0.333f, 0.275f, 0.748f };
   int ldb = 2;
   float C[] = { -0.763f, -0.829f, 0.708f, -0.613f };
   int ldc = 1;
   float C_expected[] = { -0.763f, -0.829f, 0.708f, -0.613f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1497) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1497) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 113;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.1f};
   float beta[2] = {0.0f, 1.0f};
   float A[] = { 0.496f, -0.9f, 0.825f, -0.678f, 0.41f, -0.585f, -0.264f, 0.308f };
   int lda = 1;
   float B[] = { 0.907f, 0.972f, -0.724f, 0.745f, -0.601f, 0.589f, 0.759f, -0.521f, -0.161f, -0.321f, 0.341f, -0.981f, -0.378f, -0.671f, -0.314f, -0.878f };
   int ldb = 4;
   float C[] = { -0.293f, 0.07f, 0.087f, -0.542f };
   int ldc = 2;
   float C_expected[] = { 0.10357f, -0.163927f, 0.444626f, -0.0076744f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1498) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1498) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 113;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   float alpha[2] = {0.0f, 0.1f};
   float beta[2] = {0.0f, 1.0f};
   float A[] = { -0.225f, -0.629f, -0.939f, -0.836f, -0.841f, -0.794f, 0.836f, -0.65f };
   int lda = 4;
   float B[] = { 0.869f, -0.453f, 0.8f, -0.947f, 0.545f, 0.716f, -0.507f, -0.228f, 0.722f, 0.372f, 0.77f, 0.317f, -0.153f, -0.524f, -0.465f, -0.684f };
   int ldb = 2;
   float C[] = { -0.896f, 0.91f, -0.973f, -0.269f };
   int ldc = 1;
   float C_expected[] = { -1.18974f, -1.0134f, 0.189027f, -1.14494f };
   cblas_cgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], flteps, "cgemm(case 1499) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], flteps, "cgemm(case 1499) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {1, 0};
   double beta[2] = {-1, 0};
   double A[] = { -0.33, 0.457, 0.428, -0.19, 0.86, -0.53, 0.058, -0.942 };
   int lda = 4;
   double B[] = { 0.434, 0.653, -0.124, 0.191, -0.112, -0.84, -0.72, 0.075, -0.503, -0.109, 0.3, -0.898, 0.489, 0.384, 0.993, -0.804 };
   int ldb = 2;
   double C[] = { -0.792, -0.155, -0.608, -0.243 };
   int ldc = 2;
   double C_expected[] = { 0.042563, -0.465908, -0.649991, -1.621116 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1500) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1500) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {1, 0};
   double beta[2] = {-1, 0};
   double A[] = { 0.726, -0.438, -0.23, -0.054, -0.019, 0.902, -0.883, -0.235 };
   int lda = 1;
   double B[] = { 0.159, -0.18, 0.386, -0.167, 0.971, -0.072, 0.87, -0.839, 0.474, 0.956, -0.235, 0.332, 0.826, -0.056, -0.941, 0.01 };
   int ldb = 4;
   double C[] = { -0.799, 0.973, -0.549, -0.177 };
   int ldc = 1;
   double C_expected[] = { -0.181084, 0.257841, 2.251901, 1.558195 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1501) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1501) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0.1};
   double beta[2] = {1, 0};
   double A[] = { 0.109, 0.892, -0.723, 0.793, 0.109, -0.419, -0.534, 0.448 };
   int lda = 4;
   double B[] = { -0.875, -0.31, -0.027, 0.067, 0.274, -0.126, -0.548, 0.497, 0.681, 0.388, 0.909, 0.889, 0.982, -0.074, -0.788, 0.233 };
   int ldb = 4;
   double C[] = { 0.503, 0.067, 0.239, 0.876 };
   int ldc = 2;
   double C_expected[] = { 0.6553584, 0.0864583, 0.2559136, 0.7518389 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1502) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1502) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0.1};
   double beta[2] = {1, 0};
   double A[] = { 0.334, 0.192, -0.992, -0.168, 0.154, -0.75, -0.797, -0.76 };
   int lda = 1;
   double B[] = { -0.82, 0.147, -0.237, 0.68, 0.317, 0.257, -0.406, -0.802, 0.058, 0.012, -0.832, 0.949, -0.263, -0.085, -0.064, 0.492 };
   int ldb = 2;
   double C[] = { 0.079, -0.602, -0.392, 0.316 };
   int ldc = 1;
   double C_expected[] = { 0.0980569, -0.6430449, -0.539207, 0.4226848 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1503) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1503) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 111;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0};
   double beta[2] = {-1, 0};
   double A[] = { -0.305, -0.698, -0.072, -0.383, 0.364, -0.656, 0.819, 0.194 };
   int lda = 4;
   double B[] = { 0.682, 0.498, -0.389, 0.923, -0.853, -0.558, -0.722, -0.085, -0.27, 0.026, -0.107, -0.036, 0.644, -0.327, -0.894, 0.34 };
   int ldb = 4;
   double C[] = { 0.981, -0.336, -0.377, -0.41 };
   int ldc = 2;
   double C_expected[] = { -0.981, 0.336, 0.377, 0.41 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1504) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1504) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 111;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0};
   double beta[2] = {-1, 0};
   double A[] = { -0.306, -0.709, -0.196, 0.285, 0.873, -0.802, 0.715, -0.179 };
   int lda = 1;
   double B[] = { 0.028, 0.109, 0.87, -0.446, 0.735, 0.731, 0.021, -0.186, 0.541, 0.97, -0.333, 0.002, -0.089, -0.01, 0.331, 0.851 };
   int ldb = 2;
   double C[] = { 0.902, -0.584, -0.695, -0.607 };
   int ldc = 1;
   double C_expected[] = { -0.902, 0.584, 0.695, 0.607 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1505) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1505) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {-1, 0};
   double beta[2] = {1, 0};
   double A[] = { 0.517, -0.136, 0.72, -0.237, 0.121, -0.66, 0.005, 0.759 };
   int lda = 1;
   double B[] = { -0.606, 0.049, 0.807, -0.236, -0.258, -0.412, 0.75, -0.659, 0.993, -0.029, -0.968, 0.707, -0.362, -0.005, 0.096, -0.241 };
   int ldb = 2;
   double C[] = { 0.63, 0.922, 0.025, -0.535 };
   int ldc = 2;
   double C_expected[] = { 1.117044, 1.983417, -1.276831, -0.447092 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1506) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1506) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {-1, 0};
   double beta[2] = {1, 0};
   double A[] = { 0.064, 0.371, -0.01, -0.262, 0.143, -0.081, 0.1, -0.062 };
   int lda = 4;
   double B[] = { -0.749, 0.289, -0.239, -0.226, 0.284, 0.668, 0.305, 0.075, -0.36, 0.166, -0.416, 0.234, -0.267, 0.525, 0.116, -0.561 };
   int ldb = 4;
   double C[] = { 0.671, 0.763, 0.444, -0.246 };
   int ldc = 1;
   double C_expected[] = { 0.753107, 0.896395, 0.481996, -0.263126 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1507) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1507) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {1, 0};
   double beta[2] = {-0.3, 0.1};
   double A[] = { -0.956, -0.751, 0.671, -0.633, 0.648, -0.042, 0.948, 0.826 };
   int lda = 1;
   double B[] = { 0.921, 0.506, -0.609, 0.817, -0.686, 0.991, 0.616, -0.482, -0.02, -0.34, 0.559, 0.976, 0.431, 0.385, -0.164, -0.778 };
   int ldb = 4;
   double C[] = { 0.074, -0.01, 0.165, 0.166 };
   int ldc = 2;
   double C_expected[] = { 0.166046, 0.491557, 1.473191, -0.033821 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1508) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1508) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {1, 0};
   double beta[2] = {-0.3, 0.1};
   double A[] = { -0.698, -0.062, 0.023, 0.704, 0.443, -0.46, 0.541, 0.296 };
   int lda = 4;
   double B[] = { 0.787, -0.199, 0.835, -0.276, -0.515, 0.467, -0.76, -0.483, 0.015, -0.394, -0.748, 0.02, 0.573, 0.3, -0.088, -0.238 };
   int ldb = 2;
   double C[] = { 0.935, -0.655, -0.797, 0.071 };
   int ldc = 1;
   double C_expected[] = { -1.070679, 0.178755, -0.344714, -0.308137 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1509) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1509) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 112;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0.1};
   double beta[2] = {-0.3, 0.1};
   double A[] = { -0.202, -0.219, 0.741, 0.527, 0.054, 0.16, -0.359, 0.338 };
   int lda = 1;
   double B[] = { -0.872, 0.995, 0.722, 0.618, -0.27, 0.939, -0.743, 0.547, -0.864, 0.376, -0.997, -0.63, 0.887, -0.454, 0.436, -0.039 };
   int ldb = 4;
   double C[] = { -0.684, 0.463, -0.386, -0.524 };
   int ldc = 2;
   double C_expected[] = { 0.1423153, -0.066679, 0.1175618, 0.0012949 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1510) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1510) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 112;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0.1};
   double beta[2] = {-0.3, 0.1};
   double A[] = { -0.855, -0.173, -0.679, 0.824, 0.469, 0.786, 0.757, -0.109 };
   int lda = 4;
   double B[] = { 0.483, -0.888, -0.757, 0.551, -0.81, 0.23, -0.078, 0.725, -0.592, 0.394, 0.884, 0.802, -0.813, -0.016, -0.853, 0.783 };
   int ldb = 2;
   double C[] = { 0.181, -0.368, -0.864, -0.784 };
   int ldc = 1;
   double C_expected[] = { 0.1728438, 0.1183508, 0.2526999, 0.3004174 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1511) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1511) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 113;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {-1, 0};
   double beta[2] = {-0.3, 0.1};
   double A[] = { 0.446, -0.65, -0.724, 0.014, 0.792, -0.695, -0.81, -0.358 };
   int lda = 1;
   double B[] = { -0.08, 0.216, 0.689, 0.699, 0.073, -0.346, 0.821, -0.668, -0.798, 0.869, 0.451, -0.061, -0.41, 0.316, 0.104, -0.514 };
   int ldb = 2;
   double C[] = { -0.476, 0.211, -0.912, -0.243 };
   int ldc = 2;
   double C_expected[] = { 1.372475, -0.135616, 0.549353, -1.968747 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1512) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1512) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 113;
   int transB = 111;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {-1, 0};
   double beta[2] = {-0.3, 0.1};
   double A[] = { 0.669, 0.046, -0.094, 0.666, 0.23, 0.448, -0.795, -0.142 };
   int lda = 4;
   double B[] = { 0.037, -0.154, -0.739, 0.905, 0.793, -0.53, -0.34, 0.428, 0.072, -0.263, -0.603, -0.905, 0.681, -0.083, -0.511, -0.337 };
   int ldb = 4;
   double C[] = { 0.247, 0.575, -0.836, -0.883 };
   int ldc = 1;
   double C_expected[] = { -0.975939, 0.415528, 0.275533, 0.002716 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1513) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1513) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 113;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0};
   double beta[2] = {-1, 0};
   double A[] = { 0.369, 0.506, 0.217, -0.739, -0.395, 0.16, -0.329, -0.954 };
   int lda = 1;
   double B[] = { -0.622, -0.945, 0.416, -0.884, 0.797, -0.74, 0.519, -0.789, -0.348, 0.563, -0.398, -0.956, 0.227, 0.84, -0.079, 0.847 };
   int ldb = 4;
   double C[] = { 0.833, 0.761, 0.074, -0.448 };
   int ldc = 2;
   double C_expected[] = { -0.833, -0.761, -0.074, 0.448 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1514) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1514) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 113;
   int transB = 112;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {0, 0};
   double beta[2] = {-1, 0};
   double A[] = { -0.141, 0.275, 0.717, 0.775, -0.701, -0.689, -0.883, -0.077 };
   int lda = 4;
   double B[] = { -0.526, -0.437, 0.133, -0.209, -0.83, 0.328, 0.916, -0.337, 0.762, -0.664, -0.566, 0.955, 0.168, 0.488, -0.172, -0.535 };
   int ldb = 2;
   double C[] = { -0.88, 0.945, 0.416, 0.99 };
   int ldc = 1;
   double C_expected[] = { 0.88, -0.945, -0.416, -0.99 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1515) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1515) imag");
     };
   };
  };

  {
   int order = 101;
   int transA = 113;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {-0.3, 0.1};
   double beta[2] = {0, 0.1};
   double A[] = { -0.534, -0.013, -0.258, -0.31, -0.211, -0.883, -0.89, -0.499 };
   int lda = 1;
   double B[] = { -0.185, -0.798, -0.34, 0.716, 0.035, 0.968, -0.26, 0.784, -0.889, -0.344, -0.685, -0.647, -0.764, 0.03, 0.626, -0.989 };
   int ldb = 4;
   double C[] = { -0.793, -0.551, 0.182, 0.838 };
   int ldc = 2;
   double C_expected[] = { -0.5507177, -0.0286821, 0.2222276, 0.5197398 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1516) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1516) imag");
     };
   };
  };

  {
   int order = 102;
   int transA = 113;
   int transB = 113;
   int M = 1;
   int N = 2;
   int K = 4;
   double alpha[2] = {-0.3, 0.1};
   double beta[2] = {0, 0.1};
   double A[] = { 0.575, -0.128, -0.702, 0.758, 0.383, -0.914, 0.157, 0.368 };
   int lda = 4;
   double B[] = { 0.572, -0.841, 0.223, -0.334, -0.823, -0.84, 0.671, -0.871, 0.241, 0.927, -0.344, 0.281, -0.034, -0.104, 0.587, -0.329 };
   int ldb = 2;
   double C[] = { -0.612, 0.167, 0.647, 0.447 };
   int ldc = 1;
   double C_expected[] = { -0.7876717, 0.0341179, -0.0800018, 0.5717566 };
   cblas_zgemm(order, transA, transB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
   {
     int i;
     for (i = 0; i < 2; i++) {
       gsl_test_rel(C[2*i], C_expected[2*i], dbleps, "zgemm(case 1517) real");
       gsl_test_rel(C[2*i+1], C_expected[2*i+1], dbleps, "zgemm(case 1517) imag");
     };
   };
  };

}


/*
 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/* This file was automatically generated --- DO NOT EDIT */
/* Generated on Mon Mar  8 17:44:42 EST 1999 */

#include <fftw-int.h>
#include <fftw.h>

/* Generated by: ./genfft -magic-alignment-check -magic-twiddle-load-all -magic-variables 4 -magic-loopi -notwiddle 15 */

/*
 * This function contains 156 FP additions, 56 FP multiplications,
 * (or, 128 additions, 28 multiplications, 28 fused multiply/add),
 * 62 stack variables, and 60 memory accesses
 */
static const fftw_real K587785252 = FFTW_KONST(+0.587785252292473129168705954639072768597652438);
static const fftw_real K951056516 = FFTW_KONST(+0.951056516295153572116439333379382143405698634);
static const fftw_real K250000000 = FFTW_KONST(+0.250000000000000000000000000000000000000000000);
static const fftw_real K559016994 = FFTW_KONST(+0.559016994374947424102293417182819058860154590);
static const fftw_real K500000000 = FFTW_KONST(+0.500000000000000000000000000000000000000000000);
static const fftw_real K866025403 = FFTW_KONST(+0.866025403784438646763723170752936183471402627);

/*
 * Generator Id's :
 * $Id: exprdag.ml,v 1.36 1999/02/19 17:22:11 athena Exp $
 * $Id: fft.ml,v 1.41 1999/02/19 17:22:13 athena Exp $
 * $Id: to_c.ml,v 1.24 1999/02/19 17:22:17 athena Exp $
 */

void fftw_no_twiddle_15(const fftw_complex *input, fftw_complex *output, int istride, int ostride)
{
     fftw_real tmp5;
     fftw_real tmp33;
     fftw_real tmp57;
     fftw_real tmp145;
     fftw_real tmp100;
     fftw_real tmp124;
     fftw_real tmp21;
     fftw_real tmp26;
     fftw_real tmp27;
     fftw_real tmp49;
     fftw_real tmp54;
     fftw_real tmp55;
     fftw_real tmp136;
     fftw_real tmp137;
     fftw_real tmp147;
     fftw_real tmp61;
     fftw_real tmp62;
     fftw_real tmp63;
     fftw_real tmp112;
     fftw_real tmp113;
     fftw_real tmp126;
     fftw_real tmp83;
     fftw_real tmp88;
     fftw_real tmp94;
     fftw_real tmp10;
     fftw_real tmp15;
     fftw_real tmp16;
     fftw_real tmp38;
     fftw_real tmp43;
     fftw_real tmp44;
     fftw_real tmp139;
     fftw_real tmp140;
     fftw_real tmp146;
     fftw_real tmp58;
     fftw_real tmp59;
     fftw_real tmp60;
     fftw_real tmp115;
     fftw_real tmp116;
     fftw_real tmp125;
     fftw_real tmp72;
     fftw_real tmp77;
     fftw_real tmp93;
     ASSERT_ALIGNED_DOUBLE();
     {
	  fftw_real tmp1;
	  fftw_real tmp97;
	  fftw_real tmp4;
	  fftw_real tmp96;
	  fftw_real tmp32;
	  fftw_real tmp98;
	  fftw_real tmp29;
	  fftw_real tmp99;
	  ASSERT_ALIGNED_DOUBLE();
	  tmp1 = c_re(input[0]);
	  tmp97 = c_im(input[0]);
	  {
	       fftw_real tmp2;
	       fftw_real tmp3;
	       fftw_real tmp30;
	       fftw_real tmp31;
	       ASSERT_ALIGNED_DOUBLE();
	       tmp2 = c_re(input[5 * istride]);
	       tmp3 = c_re(input[10 * istride]);
	       tmp4 = tmp2 + tmp3;
	       tmp96 = K866025403 * (tmp3 - tmp2);
	       tmp30 = c_im(input[5 * istride]);
	       tmp31 = c_im(input[10 * istride]);
	       tmp32 = K866025403 * (tmp30 - tmp31);
	       tmp98 = tmp30 + tmp31;
	  }
	  tmp5 = tmp1 + tmp4;
	  tmp29 = tmp1 - (K500000000 * tmp4);
	  tmp33 = tmp29 - tmp32;
	  tmp57 = tmp29 + tmp32;
	  tmp145 = tmp97 + tmp98;
	  tmp99 = tmp97 - (K500000000 * tmp98);
	  tmp100 = tmp96 + tmp99;
	  tmp124 = tmp99 - tmp96;
     }
     {
	  fftw_real tmp17;
	  fftw_real tmp20;
	  fftw_real tmp45;
	  fftw_real tmp79;
	  fftw_real tmp80;
	  fftw_real tmp81;
	  fftw_real tmp48;
	  fftw_real tmp82;
	  fftw_real tmp22;
	  fftw_real tmp25;
	  fftw_real tmp50;
	  fftw_real tmp84;
	  fftw_real tmp85;
	  fftw_real tmp86;
	  fftw_real tmp53;
	  fftw_real tmp87;
	  ASSERT_ALIGNED_DOUBLE();
	  {
	       fftw_real tmp18;
	       fftw_real tmp19;
	       fftw_real tmp46;
	       fftw_real tmp47;
	       ASSERT_ALIGNED_DOUBLE();
	       tmp17 = c_re(input[6 * istride]);
	       tmp18 = c_re(input[11 * istride]);
	       tmp19 = c_re(input[istride]);
	       tmp20 = tmp18 + tmp19;
	       tmp45 = tmp17 - (K500000000 * tmp20);
	       tmp79 = K866025403 * (tmp19 - tmp18);
	       tmp80 = c_im(input[6 * istride]);
	       tmp46 = c_im(input[11 * istride]);
	       tmp47 = c_im(input[istride]);
	       tmp81 = tmp46 + tmp47;
	       tmp48 = K866025403 * (tmp46 - tmp47);
	       tmp82 = tmp80 - (K500000000 * tmp81);
	  }
	  {
	       fftw_real tmp23;
	       fftw_real tmp24;
	       fftw_real tmp51;
	       fftw_real tmp52;
	       ASSERT_ALIGNED_DOUBLE();
	       tmp22 = c_re(input[9 * istride]);
	       tmp23 = c_re(input[14 * istride]);
	       tmp24 = c_re(input[4 * istride]);
	       tmp25 = tmp23 + tmp24;
	       tmp50 = tmp22 - (K500000000 * tmp25);
	       tmp84 = K866025403 * (tmp24 - tmp23);
	       tmp85 = c_im(input[9 * istride]);
	       tmp51 = c_im(input[14 * istride]);
	       tmp52 = c_im(input[4 * istride]);
	       tmp86 = tmp51 + tmp52;
	       tmp53 = K866025403 * (tmp51 - tmp52);
	       tmp87 = tmp85 - (K500000000 * tmp86);
	  }
	  tmp21 = tmp17 + tmp20;
	  tmp26 = tmp22 + tmp25;
	  tmp27 = tmp21 + tmp26;
	  tmp49 = tmp45 - tmp48;
	  tmp54 = tmp50 - tmp53;
	  tmp55 = tmp49 + tmp54;
	  tmp136 = tmp80 + tmp81;
	  tmp137 = tmp85 + tmp86;
	  tmp147 = tmp136 + tmp137;
	  tmp61 = tmp45 + tmp48;
	  tmp62 = tmp50 + tmp53;
	  tmp63 = tmp61 + tmp62;
	  tmp112 = tmp82 - tmp79;
	  tmp113 = tmp87 - tmp84;
	  tmp126 = tmp112 + tmp113;
	  tmp83 = tmp79 + tmp82;
	  tmp88 = tmp84 + tmp87;
	  tmp94 = tmp83 + tmp88;
     }
     {
	  fftw_real tmp6;
	  fftw_real tmp9;
	  fftw_real tmp34;
	  fftw_real tmp68;
	  fftw_real tmp69;
	  fftw_real tmp70;
	  fftw_real tmp37;
	  fftw_real tmp71;
	  fftw_real tmp11;
	  fftw_real tmp14;
	  fftw_real tmp39;
	  fftw_real tmp73;
	  fftw_real tmp74;
	  fftw_real tmp75;
	  fftw_real tmp42;
	  fftw_real tmp76;
	  ASSERT_ALIGNED_DOUBLE();
	  {
	       fftw_real tmp7;
	       fftw_real tmp8;
	       fftw_real tmp35;
	       fftw_real tmp36;
	       ASSERT_ALIGNED_DOUBLE();
	       tmp6 = c_re(input[3 * istride]);
	       tmp7 = c_re(input[8 * istride]);
	       tmp8 = c_re(input[13 * istride]);
	       tmp9 = tmp7 + tmp8;
	       tmp34 = tmp6 - (K500000000 * tmp9);
	       tmp68 = K866025403 * (tmp8 - tmp7);
	       tmp69 = c_im(input[3 * istride]);
	       tmp35 = c_im(input[8 * istride]);
	       tmp36 = c_im(input[13 * istride]);
	       tmp70 = tmp35 + tmp36;
	       tmp37 = K866025403 * (tmp35 - tmp36);
	       tmp71 = tmp69 - (K500000000 * tmp70);
	  }
	  {
	       fftw_real tmp12;
	       fftw_real tmp13;
	       fftw_real tmp40;
	       fftw_real tmp41;
	       ASSERT_ALIGNED_DOUBLE();
	       tmp11 = c_re(input[12 * istride]);
	       tmp12 = c_re(input[2 * istride]);
	       tmp13 = c_re(input[7 * istride]);
	       tmp14 = tmp12 + tmp13;
	       tmp39 = tmp11 - (K500000000 * tmp14);
	       tmp73 = K866025403 * (tmp13 - tmp12);
	       tmp74 = c_im(input[12 * istride]);
	       tmp40 = c_im(input[2 * istride]);
	       tmp41 = c_im(input[7 * istride]);
	       tmp75 = tmp40 + tmp41;
	       tmp42 = K866025403 * (tmp40 - tmp41);
	       tmp76 = tmp74 - (K500000000 * tmp75);
	  }
	  tmp10 = tmp6 + tmp9;
	  tmp15 = tmp11 + tmp14;
	  tmp16 = tmp10 + tmp15;
	  tmp38 = tmp34 - tmp37;
	  tmp43 = tmp39 - tmp42;
	  tmp44 = tmp38 + tmp43;
	  tmp139 = tmp69 + tmp70;
	  tmp140 = tmp74 + tmp75;
	  tmp146 = tmp139 + tmp140;
	  tmp58 = tmp34 + tmp37;
	  tmp59 = tmp39 + tmp42;
	  tmp60 = tmp58 + tmp59;
	  tmp115 = tmp71 - tmp68;
	  tmp116 = tmp76 - tmp73;
	  tmp125 = tmp115 + tmp116;
	  tmp72 = tmp68 + tmp71;
	  tmp77 = tmp73 + tmp76;
	  tmp93 = tmp72 + tmp77;
     }
     {
	  fftw_real tmp134;
	  fftw_real tmp28;
	  fftw_real tmp133;
	  fftw_real tmp142;
	  fftw_real tmp144;
	  fftw_real tmp138;
	  fftw_real tmp141;
	  fftw_real tmp143;
	  fftw_real tmp135;
	  ASSERT_ALIGNED_DOUBLE();
	  tmp134 = K559016994 * (tmp16 - tmp27);
	  tmp28 = tmp16 + tmp27;
	  tmp133 = tmp5 - (K250000000 * tmp28);
	  tmp138 = tmp136 - tmp137;
	  tmp141 = tmp139 - tmp140;
	  tmp142 = (K951056516 * tmp138) - (K587785252 * tmp141);
	  tmp144 = (K951056516 * tmp141) + (K587785252 * tmp138);
	  c_re(output[0]) = tmp5 + tmp28;
	  tmp143 = tmp134 + tmp133;
	  c_re(output[9 * ostride]) = tmp143 - tmp144;
	  c_re(output[6 * ostride]) = tmp143 + tmp144;
	  tmp135 = tmp133 - tmp134;
	  c_re(output[12 * ostride]) = tmp135 - tmp142;
	  c_re(output[3 * ostride]) = tmp135 + tmp142;
     }
     {
	  fftw_real tmp110;
	  fftw_real tmp56;
	  fftw_real tmp109;
	  fftw_real tmp118;
	  fftw_real tmp120;
	  fftw_real tmp114;
	  fftw_real tmp117;
	  fftw_real tmp119;
	  fftw_real tmp111;
	  ASSERT_ALIGNED_DOUBLE();
	  tmp110 = K559016994 * (tmp44 - tmp55);
	  tmp56 = tmp44 + tmp55;
	  tmp109 = tmp33 - (K250000000 * tmp56);
	  tmp114 = tmp112 - tmp113;
	  tmp117 = tmp115 - tmp116;
	  tmp118 = (K951056516 * tmp114) - (K587785252 * tmp117);
	  tmp120 = (K951056516 * tmp117) + (K587785252 * tmp114);
	  c_re(output[5 * ostride]) = tmp33 + tmp56;
	  tmp119 = tmp110 + tmp109;
	  c_re(output[14 * ostride]) = tmp119 - tmp120;
	  c_re(output[11 * ostride]) = tmp119 + tmp120;
	  tmp111 = tmp109 - tmp110;
	  c_re(output[2 * ostride]) = tmp111 - tmp118;
	  c_re(output[8 * ostride]) = tmp111 + tmp118;
     }
     {
	  fftw_real tmp150;
	  fftw_real tmp148;
	  fftw_real tmp149;
	  fftw_real tmp154;
	  fftw_real tmp156;
	  fftw_real tmp152;
	  fftw_real tmp153;
	  fftw_real tmp155;
	  fftw_real tmp151;
	  ASSERT_ALIGNED_DOUBLE();
	  tmp150 = K559016994 * (tmp146 - tmp147);
	  tmp148 = tmp146 + tmp147;
	  tmp149 = tmp145 - (K250000000 * tmp148);
	  tmp152 = tmp21 - tmp26;
	  tmp153 = tmp10 - tmp15;
	  tmp154 = (K951056516 * tmp152) - (K587785252 * tmp153);
	  tmp156 = (K951056516 * tmp153) + (K587785252 * tmp152);
	  c_im(output[0]) = tmp145 + tmp148;
	  tmp155 = tmp150 + tmp149;
	  c_im(output[6 * ostride]) = tmp155 - tmp156;
	  c_im(output[9 * ostride]) = tmp156 + tmp155;
	  tmp151 = tmp149 - tmp150;
	  c_im(output[3 * ostride]) = tmp151 - tmp154;
	  c_im(output[12 * ostride]) = tmp154 + tmp151;
     }
     {
	  fftw_real tmp129;
	  fftw_real tmp127;
	  fftw_real tmp128;
	  fftw_real tmp123;
	  fftw_real tmp132;
	  fftw_real tmp121;
	  fftw_real tmp122;
	  fftw_real tmp131;
	  fftw_real tmp130;
	  ASSERT_ALIGNED_DOUBLE();
	  tmp129 = K559016994 * (tmp125 - tmp126);
	  tmp127 = tmp125 + tmp126;
	  tmp128 = tmp124 - (K250000000 * tmp127);
	  tmp121 = tmp49 - tmp54;
	  tmp122 = tmp38 - tmp43;
	  tmp123 = (K951056516 * tmp121) - (K587785252 * tmp122);
	  tmp132 = (K951056516 * tmp122) + (K587785252 * tmp121);
	  c_im(output[5 * ostride]) = tmp124 + tmp127;
	  tmp131 = tmp129 + tmp128;
	  c_im(output[11 * ostride]) = tmp131 - tmp132;
	  c_im(output[14 * ostride]) = tmp132 + tmp131;
	  tmp130 = tmp128 - tmp129;
	  c_im(output[2 * ostride]) = tmp123 + tmp130;
	  c_im(output[8 * ostride]) = tmp130 - tmp123;
     }
     {
	  fftw_real tmp95;
	  fftw_real tmp101;
	  fftw_real tmp102;
	  fftw_real tmp106;
	  fftw_real tmp107;
	  fftw_real tmp104;
	  fftw_real tmp105;
	  fftw_real tmp108;
	  fftw_real tmp103;
	  ASSERT_ALIGNED_DOUBLE();
	  tmp95 = K559016994 * (tmp93 - tmp94);
	  tmp101 = tmp93 + tmp94;
	  tmp102 = tmp100 - (K250000000 * tmp101);
	  tmp104 = tmp58 - tmp59;
	  tmp105 = tmp61 - tmp62;
	  tmp106 = (K951056516 * tmp104) + (K587785252 * tmp105);
	  tmp107 = (K951056516 * tmp105) - (K587785252 * tmp104);
	  c_im(output[10 * ostride]) = tmp100 + tmp101;
	  tmp108 = tmp102 - tmp95;
	  c_im(output[7 * ostride]) = tmp107 + tmp108;
	  c_im(output[13 * ostride]) = tmp108 - tmp107;
	  tmp103 = tmp95 + tmp102;
	  c_im(output[ostride]) = tmp103 - tmp106;
	  c_im(output[4 * ostride]) = tmp106 + tmp103;
     }
     {
	  fftw_real tmp65;
	  fftw_real tmp64;
	  fftw_real tmp66;
	  fftw_real tmp90;
	  fftw_real tmp92;
	  fftw_real tmp78;
	  fftw_real tmp89;
	  fftw_real tmp91;
	  fftw_real tmp67;
	  ASSERT_ALIGNED_DOUBLE();
	  tmp65 = K559016994 * (tmp60 - tmp63);
	  tmp64 = tmp60 + tmp63;
	  tmp66 = tmp57 - (K250000000 * tmp64);
	  tmp78 = tmp72 - tmp77;
	  tmp89 = tmp83 - tmp88;
	  tmp90 = (K951056516 * tmp78) + (K587785252 * tmp89);
	  tmp92 = (K951056516 * tmp89) - (K587785252 * tmp78);
	  c_re(output[10 * ostride]) = tmp57 + tmp64;
	  tmp91 = tmp66 - tmp65;
	  c_re(output[7 * ostride]) = tmp91 - tmp92;
	  c_re(output[13 * ostride]) = tmp91 + tmp92;
	  tmp67 = tmp65 + tmp66;
	  c_re(output[4 * ostride]) = tmp67 - tmp90;
	  c_re(output[ostride]) = tmp67 + tmp90;
     }
}

fftw_codelet_desc fftw_no_twiddle_15_desc =
{
     "fftw_no_twiddle_15",
     (void (*)()) fftw_no_twiddle_15,
     15,
     FFTW_FORWARD,
     FFTW_NOTW,
     241,
     0,
     (const int *) 0,
};


#pragma once
#include <gsl/gsl_math.h>

double brent(gsl_function *F, double x_lo, double x_hi, int *status1, int *status2);


#ifndef TOURNAMENTPIVOTING_H
#define TOURNAMENTPIVOTING_H

#include <stdio.h>
#include <stdlib.h>
#include "SuiteSparseQR_C.h"
#include <string.h>
#include <math.h>
#include <mpi.h>

#ifdef MKL
  #include <mkl.h>
#else
  #include <lapacke.h>
#endif

#define ASSERT(t_) if(!(t_)) printf("WARNING %s, line %d\n",__FILE__,__LINE__)
#ifndef MAX
#define MAX(a_,b_) ((a_)>(b_)?(a_):(b_))
#endif
#define DBL_EPSILON 1e-15

int preAlps_tournamentPivoting(MPI_Comm comm, int *colPtr, int *rowInd, double *a, int m,  int n,  int nnz, long col_offset, int k, long *Jc,
   double *Sval, int printSVal, int ordering);

int preAlps_tournamentPivotingQR(MPI_Comm comm, int *colPtr, int *rowInd, double *a, int m,  int n,  int nnz,
  long col_offset, int k, long *Jc, double *Sval, int printSVal, int checkFact, int printFact, int ordering);

int preAlps_tournamentPivotingCUR(MPI_Comm comm, int *colPtr, int *rowInd, double *a, int m,  int n,  int nnz,
    long col_offset, int k, long *Jr, long *Jc, double *Sval, int printSVal, int checkFact, int printFact, int ordering);

#endif


/**
 * @file batchf_zgemm_stride.c
 *
 *  @brief BBLAS batchf_zgemm_stride double _Complex routine.
 *
 *  BBLAS is a software package provided by Univ. of Manchester,
 *  Univ. of Tennessee.
 *
 * @version 1.0.0
 * @author  Samuel  D. Relton
 * @author  Pedro   V. Lara
 * @author  Mawussi Zounon
 * @date    2016-04-06
 *
 **/

#ifndef DOXYGEN_SHOULD_SKIP_THIS
/**
 * Code generation
 * @precisions normal z -> c d s
 **/
#endif

#include <cblas.h>
#include "bblas.h"

#define COMPLEX

/**
    Purpose
    -------
    <b>batchf_zgemm_stride</b> is a batch version of zgemm for fixed size.
	It performs the matrix-matrix operations

	arrayC[i] = alpha[i]*op( arrayA[i] )*op( arrayB[i] ) + beta[i]*arrayC[i],

	where op( X ) is one of

	op( X ) = X      or
	op( X ) = X**T   or
	op( X ) = X**H,

	alpha[i] and beta[i] are scalars, and arrayA[i], arrayB[i] and C are matrices, with
	op( arrayA[i] ) an m by k matrix, op( arrayB[i] ) a k by n matrix and arrayC[i]
	an m by n matrix.

	Fixed Batch Operations
	-----------------------------------

	- all parameters that are arrays must have length at least batch_count.
	- all parameters that are arrays must have all values set.

	the values of transA, transB, M, N, K, alpha, beta, lda, ldb, and ldc
    are used for all computations.

	Parameters
	----------
	@param[in]
	transA   <tt>enum BBLAS_TRANS</tt>.
	         transA specifies the form of op( arrayA[i] ) to be used in
			 the matrix multiplication as follows:
      -     = BblasNoTrans:    op( arrayA[i] ) = arrayA[i].
      -     = BblasTrans:      op( arrayA[i] ) = arrayA[i]**T.
      -     = BblasConjTrans:  op( arrayA[i] ) = arrayA[i]**H.

	@param[in]
	transB   <tt>enum BBLAS_TRANS</tt>.
	         On entry, transB specifies the form of op( arrayB[i] ) to be used in
             the matrix multiplication as follows:
      -     = BblasNoTrans:    op( arrayB[i] ) = arrayB[i].
      -     = BblasTrans:      op( arrayB[i] ) = arrayB[i]**T.
      -     = BblasConjTrans:  op( arrayB[i] ) = arrayB[i]**H.

	@param[in]
	M       <tt>int</tt>.
            M specifies the number of rows of the matrix
            op( arrayA[i] ) and of the matrix arrayC[i].
            M must be greater than zero.

	@param[in]
	N       <tt>int</tt>.
            N specifies the number of columns of the matrix
            op( arrayB[i] ) and the number of columns of the matrix arrayC[i].
 		    N must be greater than zero.

    @param[in]
    K      <tt>int</tt>.
	       K specifies the number of columns of the matrix
           op( arrayA[i] ) and the number of rows of the matrix op( arrayB[i] ).
		   K must be greater than zero.

    @param[in]
    alpha   <tt>complex_16</tt>.

    @param[in]
    arrayA  Array of pointers.
            Each element arrayA[i] is a pointer to a COMPLEX_16 matrix of
 		    dimension lda by Ka, where Ka is K when transA = BblasNoTrans,
 		    and is M otherwise.
            When using transA = BblasNoTrans the leading M by K
            part of arrayA[i] must contain the matrix elements, otherwise
            the leading K by M part of arrayA[i] must contain the
            matrix elements.

    @param[in]
    lda     <tt>int</tt>.
            lda specifies the first dimension of arrayA[i] as declared
            in the calling (sub) program. When transA = BblasNoTrans then
            lda must be at least max( 1, M ), otherwise lda must be at
            least max( 1, K ).

    @param[in]
    strideA <tt>int</tt>.
            strideA specifies the storage spacing between A matrices.
                    strideA must be at least M by K.

    @param[in]
    arrayB  Array of pointers.
  		    Each element arrayB[i] is a pointer to a COMPLEX_16 matrix of
  		    dimension ldb by Kb, where Kb is N when transB = BblasNoTrans,
  		    and is K otherwise.
            When using transB = BblasNoTrans the leading K by N
            part of arrayB[i] must contain the matrix elements, otherwise
            the leading N by K part of arrayB[i] must contain the
            matrix elements.

    @param[in]
    ldb     <tt>int</tt>.
  		    ldb specifies the first dimension of arrayB[i] as declared
            in the calling (sub) program. When transB = BblasNoTrans then
            ldb must be at least max( 1, K ), otherwise ldb must be at
            least max( 1, N ).

    @param[in]
    strideB <tt>int</tt>.
            strideB specifies the storage spacing between A matrices.
            strideB must be at least K by N.

    @param[in]
    beta    <tt>complex_16</tt>.
            When beta is set to zero arrayC[i] need not be set on input.

    @param[in,out]
    arrayC  Array of pointers.
	        Each element arrayC[i] is a pointer to a COMPLEX_16 matrix of
      	    dimension ldc by N.
  	        Before entry, the leading M by N part of the arrayC[i] must
            contain a matrix C, except when beta is zero, in which
            case C need not be set on entry.
            On exit, the matrix arrayC[i] is overwritten by the M by N matrix
            ( alpha*op( arrayA[i] )*op( arrayB[i] ) + beta*arrayC[i] ).

    @param[in]
    ldc     <tt>int</tt>.
  	        Each element ldc specifies the first dimension of arrayC[i] as declared
  	        in the calling (sub) program. The value ldc must be at least
  	        max( 1, M )

    @param[in]
    strideC <tt>int</tt>.
            strideC specifies the storage spacing between A matrices.
            strideC must be at least M by N.

    @param[in]
    batch_count  <tt>int</tt>
                 The number of matrices to operate on.

    @param[in,out]
    info    <tt>int</tt>.
            info is the error return code of the batch.
  	        The error codes can be found in bblas_macros.h.
 **/
void batchf_zgemm_stride(
    const enum BBLAS_TRANS transA, const enum BBLAS_TRANS transB,
    const int M,  const int N, const int K,
    const BBLAS_Complex64_t alpha,
    const BBLAS_Complex64_t *arrayA, const int lda, const int strideA,
    const BBLAS_Complex64_t *arrayB, const int ldb, const int strideB,
    const BBLAS_Complex64_t beta,
    BBLAS_Complex64_t *arrayC, const int ldc, const int strideC,
    const int batch_count, int *info)
{
    /* Local variables */
    int batch_iter;
    int LDA,  LDB;
    char func_name[15] = "batchf_zgemm_op";

	/* Check input arguments */
    if (batch_count < 0)
    {
	    xerbla_batch(func_name, BBLAS_ERR_BATCH_COUNT, -1);
    }
    if ((transA != BblasNoTrans) &&
        (transA != BblasTrans) &&
        (transA != BblasConjTrans))
    {
        xerbla_batch(func_name, BBLAS_ERR_TRANSA, -1);
        *info  = BBLAS_ERR_TRANSA;
        return;
    }
    if ((transB != BblasNoTrans) &&
        (transB != BblasTrans) &&
        (transB != BblasConjTrans))
    {
        xerbla_batch(func_name, BBLAS_ERR_TRANSB, -1);
        *info = BBLAS_ERR_TRANSB;
        return;
    }
    if ( transA == BblasNoTrans )
    {
        LDA = M;
    } else
    {
        LDA = K;
    }
    if ( transB == BblasNoTrans )
    {
        LDB = K;
    } else
    {
        LDB = N;
    }
    if (M < 0)
    {
        xerbla_batch(func_name, BBLAS_ERR_M, -1);
        *info = BBLAS_ERR_M;
        return;
    }
    if (N < 0) {
        xerbla_batch(func_name, BBLAS_ERR_N, -1);
        *info = BBLAS_ERR_N;
        return;
    }
    if (K < 0) {
        xerbla_batch(func_name, BBLAS_ERR_K, -1);
        *info = BBLAS_ERR_K;
        return;
    }
    if (lda < max(1, LDA)) {
        xerbla_batch(func_name, BBLAS_ERR_LDA, -1);
        *info =  BBLAS_ERR_LDA;
        return;
    }
    if (ldb < max(1, LDB)) {
        xerbla_batch(func_name, BBLAS_ERR_LDB, -1);
        *info = BBLAS_ERR_LDB;
        return;
    }
    if (ldc < max(1, M)) {
        xerbla_batch(func_name, BBLAS_ERR_LDC, -1);
        *info = BBLAS_ERR_LDC;
        return;
    }
    if (strideA < M*K) {
        xerbla_batch(func_name, BBLAS_ERR_STRIDEA, -1);
        *info =  BBLAS_ERR_STRIDEA;
        return;
    }
    if (strideB < K*N) {
        xerbla_batch(func_name, BBLAS_ERR_STRIDEB, -1);
        *info =  BBLAS_ERR_STRIDEB;
        return;
    }
    if (strideC < M*N) {
        xerbla_batch(func_name, BBLAS_ERR_STRIDEC, -1);
        *info =  BBLAS_ERR_STRIDEC;
        return;
    }

    /* Skip subproblems where nothing needs to be done */
    if (M == 0 || N == 0     ||
	    ((alpha == (BBLAS_Complex64_t)0.0 || K == 0) &&
	    beta == (BBLAS_Complex64_t)1.0 ))
    {
        *info =  BBLAS_SUCCESS;
        return;
    }
    for (batch_iter = 0; batch_iter < batch_count; batch_iter++)
    {
        /* Call to cblas_zgemm */
        cblas_zgemm(
        BblasColMajor,
        transA,
        transB,
        M,
        N,
        K,
        CBLAS_SADDR(alpha),
        //&arrayA[batch_iter*M*K],
        &arrayA[batch_iter*strideA],
        lda,
        //&arrayB[batch_iter*K*N],
        &arrayB[batch_iter*strideB],
        ldb,
        CBLAS_SADDR(beta),
        //&arrayC[batch_iter*M*N],
        &arrayC[batch_iter*strideC],
        ldc);
    }
    /* Successful */
    *info = BBLAS_SUCCESS;
}
#undef COMPLEX


/**
 * Cascade simulation based on pair production and bremsstrahlung interactions
 *
 * copyright  (C) 2007
 * the icecube collaboration
 *
 * $Id: I3CascadeSimulation.h 35916 2007-08-23 09:34:12Z bvoigt $
 *
 * @version $Revision: 35916 $
 * @date $LastChangedDate: 2007-08-23 03:34:12 -0600 (Thu, 23 Aug 2007) $
 * @author Bernhard Voigt <bernhard.voigt@desy.de>   Last changed by: $LastChangedBy: bvoigt $
 */

#ifndef I3_CASCADE_SIMULATION_H_INCLUDED
#define I3_CASCADE_SIMULATION_H_INCLUDED

//Standard C/C++ includes
#include <vector>
#include <stack>

// Gnu Scientific Library
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_integration.h>

// local includes
#include "cmc/I3CascadeMCCommon.h"
#include "cmc/I3CascadeDevelopment.h"
#include "cmc/I3CascadeParametrization.h"
#include "cmc/I3MetropolisHastings.h"

// icetray includes
#include "phys-services/I3RandomService.h"

/**
 * @brief Local definition of a Particle struct
 *
 * The particle struct holds energy and postion informations
 * of particles in the simulation.
 */
struct Particle {
  static const short ELECTRON = 1;
  static const short PHOTON = 0;
  double energy;
  double x;
  short type;
  /**
   * Particle with energy, at position x (1-dimensional), of
   * type type (1 is electron, 0 is photon)
   *
   * @param energy
   * @param x
   * @param type
   */
  Particle(double energy, double x, short type) : energy(energy), x(x), type(type) { }
};

/**
 * @brief Local definition of beta distribution
 *
 * The struct defines a pdf function, ie. the beta distribtuion
 * not normed, though.
 * It provides a rvs method to draw random samples from
 * the beta distribution using gsl_rng
 *
 * The parameters used for the beta distribibution (alpha and beta)
 * are defined.
 *
 * Functions are passed to I3MetropolisHastings and need to be static.
 */
struct beta_dist {
  // define the logger name, use the same as for the simulation class
  SET_LOGGER("I3CascadeSimulation");

  // parameter to beta function
  static double alpha;
  static double beta;

  /**
   * Beta Function \f$ x^{\alpha-1} (1-x)^{\beta-1}\f$
   *
   * @param x
   */
  static double pdf(const double& x) {
    log_trace("alpha %.2f, beta %.2f", alpha, beta);
    return pow(x, alpha-1) * pow(1-x, beta-1);
  }

  /**
   * Returns a random variate drawn from the beta distribution
   *
   * Uses the gsl_ran_beta method
   *
   * @param random gsl_rng instance
   */
  static double rvs(const gsl_rng* random) {
    log_trace("alpha %.2f, beta %.2f", alpha, beta);
    return gsl_ran_beta(random, alpha, beta);
  }
};

/**
 *@brief This class implements a Cascade development simulation including
 *       the LPM effect
 *
 * Description of the simulation can be found here:
 * http://wiki.icecube.wisc.edu/index.php/Cascade_Simulation
 *
 * Cross section formulars used are defined in I3CascadeSimulationCrossSections
 *
 *
 * @author Bernhard Voigt
 */
class I3CascadeSimulation : public I3CascadeDevelopment, public I3CascadeMCCommon::Sampler {

  // define the logger name
  SET_LOGGER("I3CascadeSimulation");

 public:

  /**
   * @brief Constructor
   *
   * @param random random number service from icetray frame
   */
  I3CascadeSimulation(I3RandomServicePtr random);

  /**
   * @brief Destructor
   *
   * Frees the gsl objects
   */
  ~I3CascadeSimulation();

  virtual void SetRandomNumberGenerator(I3RandomServicePtr);

  /**
   * Simulates a cascade of the given energy
   *
   * @param energy
   */
  void Simulate(I3Particle::ParticleType type, double energy);

  /**
   * Sets the energy threshold down to which particles are tracked (Default is 50 TeV)
   *
   * This shouldn't be lower than 1 GeV, since this is the limit for the Bremsstrahlung
   * interaction
   *
   * @param thresholdEnergy
   */
  void SetThreshold(double thresholdEnergy) {
    threshold_ = thresholdEnergy;
  }

  /**
   * Returns the threshold energy down to which particles are tracked
   *
   * @return threshold energy
   */
  double GetThreshold() {
    return threshold_;
  }

  /**
   * @brief default energy threshold down to which particles are tracked
   */
  static const double DEFAULT_THRESHOLD;

 private:

  I3CascadeSimulation(const I3CascadeSimulation&);

  /**
   * Cacluates the pair productio mean free path for the given energy
   *
   * @param energy
   */
  double PairProductionMeanFreePath(double energy);

  /**
   * Cacluates the bremsstrahlung radiation length (cut off energy is defined as 1 GeV)
   *
   * @param energy
   */
  double BremsstrahlungMeanFreePath(double energy);

  /**
   * Samples the bremsstrahlung cross section to get a fractional energy
   * of a secondary particle produced in an interaction with a incident particle
   * of the given energy
   *
   * @see I3MetropolisHastings::Sample
   *
   * @param energy
   */
  double SampleBremsstrahlungCrossSection(double energy);

  /**
   * Samples the pair production cross section to get a fractional energy
   * of a secondary particle produced in an interaction with a incident particle
   * of the given energy
   *
   * @see I3MetropolisHastings::Sample
   *
   * @param energy
   */
  double SamplePairProductionCrossSection(double energy);

  /**
   * Inits the simulation
   *
   * Interpolation of the mean free paths is performed
   * The metropolis hastings random samplers are initialized
   */
  void Init();

  /**
   * Inits the metropolis hastings random samplers
   */
  void InitMetropolisHastings();

  /**
   * Draw and discard samples from the MH samplers
   * to make them independent of initial conditions
   */
  void BurnIn();

  /**
   * Calcuation and interpolation of the pair production mean free path
   * in an energy range specified by lower and upper (in log10)
   * The interpolation used the given number of points.
   * Default energy range is from 100 GeV to 10 EeV
   *
   * Uses the integration and interpolation routines from gsl
   *
   * @param lower
   * @param upper
   * @param points
   */
  void InterpolPairProductionMeanFreePath(double lower=2., double upper=13.0, int points=100);

  /**
   * Calcuation and interpolation of the bremsstrahlung radiation length
   * in an energy range specified by lower and upper (in log10)
   * The interpolation used the given number of points.
   * Default energy range is from 100 GeV to 10 EeV
   *
   * Uses the integration and interpolation routines from gsl
   *
   * The integration of the cross section is done down to a lower edge of
   * I3CascadeSimulation::BREMSSTRAHLUNGCUTOFF
   * this is due to the infrared limit, where the cross section raises to infinite values.
   *
   * @param lower
   * @param upper
   * @param points
   */
  void InterpolBremsstrahlungMeanFreePath(double lower=2., double upper=13.0, int points=100);

  /**
   * Returns the mean free path of a particle
   *
   * @param particle
   */
  double MeanFreePath(Particle particle);

  /**
   * Propagates a particle
   *
   * Draws the next interaction point
   * Calls the interaction method to produce secondaries
   *
   * @param particle
   */
  void Propagate(Particle particle);

  /**
   * Creates secondaries produced in an interaction of the given particle
   *
   * @param particle
   */
  void Interaction(Particle particle);

  /**
   * Produces a secondary gamma
   */
  void BremsstrahlungInteraction(Particle electron);

  /**
   * Produces a electron, positron pair
   */
  void PairProductionInteraction(Particle photon);

  /**
   * Deposits the particle
   *
   * Calculates the energy loss profile of the given particle
   * and adds it to the total energy loss profile of this simulation
   */
  void Deposit(Particle particle);

  /**
   * @brief stack holding particles that have to be propagated
   */
  std::stack<Particle> particles_;

  /**
   * @brief counter how many particles have been created
   */
  unsigned int particlesCreated_;

  /**
   * @brief counter how many particles have been deleted
   */
  unsigned int particlesDeleted_;

  /**
   * @brief struct holding beta distribution parameters, provides function
   * evaluation and random numbers
   */
  beta_dist betaDist_;

  /**
   * @brief random number sampler for low energy bremsstrahlung
   */
  I3MetropolisHastings lowBremsSampler_;

  /**
   * @brief random number sampler for high energy bremsstrahlung
   */
  I3MetropolisHastings highBremsSampler_;

  /**
   * @brief random number sampler for low energy pair production
   */
  I3MetropolisHastings lowPairSampler_;

  /**
   * @brief random number sampler for high energy pair production
   */
  I3MetropolisHastings highPairSampler_;

  /**
   * @brief parametrization of energy loss profile for low energy particles
   */
  I3CascadeParametrization parametrization_;

  /**
   * @brief gsl spline accelerator object used for spline evaluation (bremsstrahlung)
   */
  gsl_interp_accel* interpolAccBrems_;

  /**
   * @brief gsl spline - interpolation of Bresmstrahlung mean free path
   */
  gsl_spline* splineBrems_;

  /**
   * @brief gsl spline accelerator object used for spline evaluation (pair production)
   */
  gsl_interp_accel* interpolAccPair_;

  /**
   * @brief gsl spline - interpolation of Pairproduction mean free path
   */
  gsl_spline* splinePair_;

  /**
   * @brief threshold down to which particles are tracked, default is 50 TeV
   */
  double threshold_;

  /**
   * @brief repeat burn-in for each event
   *
   * This ensures that the events are truly independent and can be reproduced
   * given the state of the random number generator.
   */
  bool perEventBurnIn_;

  /**
   * @brief lowest energy of bremsstrahlung photons
   */
  static const double BREMSSTRAHLUNGCUTOFF;

};
#endif


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <assert.h>
#include <nlopt.h>
// The "procedural" interface to Reduce/CSL.
#include "C_call_CSL.h"

// activate by setting to 1 or 2
int DEBUG = 0;

// The optimization "object":
static nlopt_opt opt       = NULL;
// The result computed by NLOPT_optimize:
static double *result;
// The local optimization sub-algorithm/object, for algorithms that use it:
static nlopt_opt local_opt = NULL;

// Nlopt return codes, see nlopt_result in nlopt.h.  Used only in few
// places. nlopt_main.red does error checking at the Reduce level.
char *nlopt_result_mess(nlopt_result r) {
  char *s;
  switch (r) {
  case -1: s = "nlopt failure!";  break;
  case -2: s = "invalid arguments or unsupported constraints (perhaps for this algorithm)!";  break;
  case -3: s = "out of memory!";  break;
  case -4: s = "progress limited by roundoff";  break;
  case -5: s = "forced stop";  break;
  case 1:  s = "nlopt success";  break;
  case 2:  s = "stop value reached";  break;
  case 3:  s=  "f-tolerance reached";  break;
  case 4:  s = "x-tolerance reached";  break;
  case 5:  s = "max evaluations reached";  break;
  case 6:  s = "max time reached";  break;
  };
  return s;
}

#define ERRORCHECK(who,r) if (r != NLOPT_SUCCESS) \
    {fprintf(stderr, "*%s: %s\n", who, nlopt_result_mess(r));}

// =======================================================================
// Basics
// =======================================================================

void NLOPT_create(int32_t anum, int32_t n) {
  opt = nlopt_create(anum, n);
  if (opt == NULL)
    fprintf(stderr, "Couldn't create '%s'!\n", nlopt_algorithm_name(anum));
    // nlopt_algorithm_name is built-in to the NLopt library
}

void NLOPT_destroy() {
  nlopt_destroy(opt);
}

int32_t NLOPT_get_algorithm() {
  // The only reason for this 'NLOPT_' is to convert from nlopt_algorithm to 'int32_t'!
  // Isn't there a better way?
  int32_t a = nlopt_get_algorithm(opt);
  return a;
}

int32_t NLOPT_get_dimension() {
  // The only reason for this 'NLOPT_' is to convert from "unsigned" to 'int32_t'!
  int32_t d = nlopt_get_dimension(opt);
  return d;
}

// =======================================================================
// Setting upper and lower bounds (hyper-rectangle)
// =======================================================================

int32_t NLOPT_set_lower_bounds(const double *lb) {
  int32_t r = nlopt_set_lower_bounds(opt, lb);
  return r;
}
int32_t NLOPT_set_upper_bounds(const double *ub) {
  int32_t r = nlopt_set_upper_bounds(opt, ub);
  return r;
}

// acts like a Icon generator
double NLOPT_get_lower_bound(int32_t i, int32_t n) {
  static double *slb = NULL;
  if (i == 0) {
    // Contortion 1:
    slb = (double *) malloc(n*sizeof(double));
    int32_t r = nlopt_get_lower_bounds(opt, slb);
    ERRORCHECK("NLOPT_get_lower_bound",r);
  }
  // Contortion 2:
  double lbi = slb[i];
  if (i == n-1) free(slb);
  return lbi;
}

// acts like a Icon generator
double NLOPT_get_upper_bound(int32_t i, int32_t n) {
  static double *sub = NULL;
  if (i == 0) {
    // Contortion 1:
    sub = (double *) malloc(n*sizeof(double));
    int32_t r = nlopt_get_upper_bounds(opt, sub);
    ERRORCHECK("NLOPT_get_upper_bound",r);
  }
  // Contortion 2:
  double ubi = sub[i];
  if (i == n-1) free(sub);
  return ubi;
}

// =======================================================================
// Stopping criteria
// =======================================================================

int32_t NLOPT_set_xtol_rel(double t) {
  return nlopt_set_xtol_rel(opt, t);
}
int32_t NLOPT_set_ftol_rel(double t) {
  return nlopt_set_ftol_rel(opt, t);
}
int32_t NLOPT_set_ftol_abs(double t) {
  return nlopt_set_ftol_abs(opt, t);
}
int32_t NLOPT_set_maxeval(int32_t m) {
  return nlopt_set_maxeval(opt, m);
}
int32_t NLOPT_set_maxtime(double m) {
  return nlopt_set_maxtime(opt, m);
}
int32_t NLOPT_set_stopval(double v) {
  return nlopt_set_stopval(opt, v);
}

double NLOPT_get_xtol_rel() {
  return nlopt_get_xtol_rel(opt);
}
double NLOPT_get_ftol_rel() {
  return nlopt_get_ftol_rel(opt);
}
double NLOPT_get_ftol_abs() {
  return nlopt_get_ftol_abs(opt);
}
int32_t NLOPT_get_maxeval() {
  return nlopt_get_maxeval(opt);
}
double NLOPT_get_maxtime() {
  return nlopt_get_maxtime(opt);
}

// ===========================================================================
// Functions *internal* to the interface for dealing with arrays of doubles
// To understand what's going on see the comments in nlopt_set_lower_bounds()
// in nlopt_main.red.
// ===========================================================================
double *newDoubleArray(int32_t len) {
  double *a;
  a = (double *) malloc(len*sizeof(double));
  return a;
}
void setDoubleArray(double a[], int32_t i, double x) {
  a[i] = x;
}
void freeDoubleArray(double a[]) {
  free(a);
}
// This internal function turns a C array of doubles into a Lisp list and
// leaves it at the top of the stack.  The "stack" is used by the "procedural"
// interface to Reduce, see file "C_call_CSL.h".
void C_darray_to_Lisp_list(int32_t n, const double x[]) {
  // Push the x[i] on the stack.
  for (int i = 0; i < n; i++) PROC_push_floating(x[i]);
  // Make a list out of the x[i]; it is left at the top of the stack.
  PROC_make_function_call("list", n);
  // Now having put 'list on the front, get rid of it.
  // (This perhaps shows that that ACN should really extend the procedural
  // interface to make this slightly nicer. But with luck this will suffice
  // for now.)
  PROC_lisp_eval();
}

// ================================================================
// Setting the optimization objective
// ================================================================
// This is the most complex and difficult part of the interface so far.

// This function is internal to the interface (static), and is of type nlopt_func.
// It is modelled after f77_func_wrap in api/f77api.c.  It is used as a wrapper
// for scalar functions, which can represent either the optimization objective,
// or a general equality or inequality constraint.
// The PROC_ code is borrowed from cuba_integrand in redcuba.c of the 'cuba' package.
static double red_func_wrap(unsigned n, const double *x, double *grad, void *f_data) {
  double z;
  char *fn = (char *) f_data;
  if (DEBUG > 1) {
    fprintf(stderr, "red_func_wrap: %s", fn);
    if (grad) fprintf(stderr, " [G]");
    fprintf(stderr, "\n");
  }
  // Convert the function name (string) to a symbol, push it on the stack, and
  // wrap it in a quote:
  PROC_push_symbol(fn);
  PROC_make_function_call("quote", 1);
  // Turn the array 'x' into a Lisp list and leave it at the top of the "stack".
  C_darray_to_Lisp_list(n,x);
  // Wrap it in a quote:
  PROC_make_function_call("quote", 1);
  // Evaluate a call to the Reduce algebraic function 'fn' that takes an n-list
  // of reals 'x', and delivers either a single real, f(x), or, if 'grad' is not
  // NULL, the flattened (n+1)-list of reals {f(x),grad(x)}.
  int32_t r = PROC_make_function_call("call_alg_fn_list2list", 2);
  PROC_lisp_eval();
  if (DEBUG > 1) {
    fprintf(stderr, "  %s(", fn);
    for (int i=0; i<n; i++) {
      fprintf(stderr, "%.4g", x[i]);
      if (i < n-1) fprintf(stderr, ",");
    }
  };
  if (grad) {
    // If 'grad' is not NULL, the Reduce function 'fn' is expected to return a
    // flattened list {f(x),grad(x)}, where grad(x) is an n-list which should
    // be assigned to 'grad'. (Also see the function red_mfunc_wrap.)
    PROC_handle y = PROC_get_value();
    z = PROC_floating_value(PROC_first(y));
    if (DEBUG > 1) {
      fprintf(stderr, ") = %.5g\n", z);
      fprintf(stderr, "  grad = {");
    };
    y = PROC_rest(y);
    for (int i = 0; i < n; i++) {
      grad[i] = PROC_floating_value(PROC_first(y));
      y = PROC_rest(y);
      if (DEBUG > 1) {
	fprintf(stderr, "%.4g", grad[i]);
	if (i < n-1) fprintf(stderr, ",");
      }
    }
    if (DEBUG > 1) fprintf(stderr, "}\n");
  }
  else {  // grad is NULL, result is just f(x)
    z = PROC_floating_value(PROC_get_value());
    if (DEBUG > 1) fprintf(stderr, ") = %.5g\n", z);
  }
  return z;
}

// Modelled after F77_nlo_set_min_objective in api/f77funcs_.h.
int32_t NLOPT_set_min_objective(char *fname) {
  // In the call to nlopt_set_min_objective:
  //   1. The wrapper red_func_wrap is the nlopt_func argument,
  //   2. The name of the Reduce function defining the objective is the *f_data argument,
  //   3. We can't just pass fname to to this argument, we need the string rfun on the heap!
  char *rfun = malloc(strlen(fname)*sizeof(char)+1);
  strcpy(rfun,fname);
  return nlopt_set_min_objective(opt, red_func_wrap, rfun);
}

int32_t NLOPT_set_max_objective(char *fname) {
  char *rfun = malloc(strlen(fname)*sizeof(char)+1);
  strcpy(rfun,fname);
  return nlopt_set_max_objective(opt, red_func_wrap, rfun);
}

int32_t NLOPT_optimize(double *x0, int32_t n) {
  double *x, f;
  x = (double *) malloc(n*sizeof(double));
  for (int j=0; j<n; j++) x[j] = x0[j];
  if (DEBUG) fprintf(stderr, "NLOPT_optimize:\n");
  int32_t r = nlopt_optimize(opt, x, &f);
  // assign results to the static 'result' array:
  result = (double *) malloc((n+1)*sizeof(double));
  result[0] = f;
  for (int j=0; j<n; j++) result[j+1] = x[j];
  free(x);
  if (DEBUG) {
    fprintf(stderr, "  f = %f\n", result[0]);
    fprintf(stderr, "  result =");
    for (int j=0; j<n; j++)
      fprintf(stderr, " %.5g", result[j+1]);
    fprintf(stderr, "\n");
  }
  return r;
}

double get_result(int32_t i) {
  return result[i];
}

void free_result() {
  free(result);
}

// ================================================================
// Setting the local optimization sub-algorithm
// ================================================================

int32_t NLOPT_set_local_optimizer(int32_t anum) {
  local_opt = nlopt_create(anum, nlopt_get_dimension(opt));
  return nlopt_set_local_optimizer(opt, local_opt);
}

// =============================================================================
// Defining scalar constraints
// =============================================================================

// Note: in the next two functions
// 1. The wrapper function red_func_wrap is the nlopt_func argument,
// 2. The name of the Reduce function defining the constraint is the *f_data argument,
// 3. We can't just pass c_fname to to this argument, we need the string cfun on the heap!

int32_t NLOPT_add_eq_constraint(char *c_fname, double tol) {
  char *cfun = malloc(strlen(c_fname)*sizeof(char)+1);
  assert(cfun != NULL);
  strcpy(cfun,c_fname);
  if (DEBUG) fprintf(stderr, "added '%s' equality constraint, %.3g\n", cfun, tol);
  return nlopt_add_equality_constraint(opt, red_func_wrap, cfun, tol);
}
int32_t NLOPT_add_ineq_constraint(char *c_fname, double tol) {
  char *cfun = malloc(strlen(c_fname)*sizeof(char)+1);
  assert(cfun != NULL);
  strcpy(cfun,c_fname);
  if (DEBUG) fprintf(stderr, "added '%s' inequality constraint, %.3g\n", cfun, tol);
  return nlopt_add_inequality_constraint(opt, red_func_wrap, cfun, tol);
}

// =============================================================================
// Defining vector-valued constraints
// =============================================================================

// This function is internal to the interface (static), and is of type nlopt_mfunc.
// It is modelled after f77_mfunc_wrap in api/f77api.c.  It is used as a wrapper for
// functions that, given an n-vector 'x', return an m-vector in 'result'.
static void red_mfunc_wrap(unsigned m, double *result, unsigned n, const double *x,
			   double *grad, void *f_data) {
  char *fn = (char *) f_data;
  if (DEBUG > 1) {
    fprintf(stderr, "red_mfunc_wrap: %s", fn);
    if (grad) fprintf(stderr, " [G]");
    fprintf(stderr, "\n");
  }
  // Convert the function name (string) to a symbol, push it on the stack, and
  // wrap it in a quote:
  PROC_push_symbol(fn);
  PROC_make_function_call("quote", 1);
  // Turn the array x into a Lisp list and leave it at the top of the "stack".
  C_darray_to_Lisp_list(n,x);
  // Wrap it in a quote:
  PROC_make_function_call("quote", 1);
  // Evaluate a call to the Reduce m-dimensional algebraic function fn: R^n -> R^m
  int32_t r = PROC_make_function_call("call_alg_fn_list2list", 2);
  PROC_lisp_eval();
  if (DEBUG > 1) {
    fprintf(stderr, "  %s(", fn);
    for (int i=0; i<n; i++) {
      fprintf(stderr, "%.4g", x[i]);
      if (i < n-1) fprintf(stderr, ",");
    }
    fprintf(stderr, ") = {");
  }
  // If 'grad' is NULL, the result is a an m-list, which we assign to the C
  // array 'result' element-by-element.  If 'grad' is non-NULL, then it points
  // to an array of length m*n, and the result of 'fn' also contains a list of
  // length m*n, the gradients of the constraint functions with respect to x,
  // and 'grad' should be set to this list.  It is assumed that ∂c_i/∂x_j is
  // stored in grad[i*n + j].
  // Irrespective of 'grad', we first get the first m elements of the list and
  // assign them to 'result'.
  PROC_handle y = PROC_get_value();
  for (int i = 0; i < m; i++) {
    result[i] = PROC_floating_value(PROC_first(y));
    y = PROC_rest(y);
    if (DEBUG > 1) {
      fprintf(stderr, "%.4g", result[i]);
      if (i < m-1)  fprintf(stderr, ",");
    }
  }
  if (DEBUG > 1) fprintf(stderr, "}\n");
  if (grad) {
    // Now handle the last m*n elements of what 'fn' has returned
    if (DEBUG > 1) fprintf(stderr, "  grad = {");
    for (int i = 0; i < m*n; i++) {
      grad[i] = PROC_floating_value(PROC_first(y));
      y = PROC_rest(y);
      if (DEBUG > 1) {
	fprintf(stderr, "%.4g", grad[i]);
	if (i < m*n-1) fprintf(stderr, ",");
      }
    }
    if (DEBUG > 1) fprintf(stderr, "}\n");
  }
}

// Note: look at the notes for the functions for scalar constraints
int32_t NLOPT_add_eq_mconstraint(char *c_fname, int32_t m, double *tol) {
  char *cfun = malloc(strlen(c_fname)*sizeof(char)+1);
  assert(cfun != NULL);
  strcpy(cfun,c_fname);
  if (DEBUG) fprintf(stderr, "added '%s' equality m-constraint\n", cfun);
  return nlopt_add_equality_mconstraint(opt, m, red_mfunc_wrap, cfun, tol);
}
int32_t NLOPT_add_ineq_mconstraint(char *c_fname, int32_t m, double *tol) {
  char *cfun = malloc(strlen(c_fname)*sizeof(char)+1);
  assert(cfun != NULL);
  strcpy(cfun,c_fname);
  if (DEBUG) fprintf(stderr, "added '%s' inequality m-constraint\n", cfun);
  return nlopt_add_inequality_mconstraint(opt, m, red_mfunc_wrap, cfun, tol);
}

// =============================================================================
// Removing constraints
// =============================================================================

int32_t NLOPT_remove_eq_constraints() {
  return nlopt_remove_equality_constraints(opt);
}
int32_t NLOPT_remove_ineq_constraints() {
  return nlopt_remove_inequality_constraints(opt);
}

// =============================================================================
// Step size for algorithms without derivatives
// =============================================================================

int32_t NLOPT_set_initial_step(const double *dx) {
  return nlopt_set_initial_step(opt, dx);
}
int32_t NLOPT_set_initial_step1(double dx) {
  return nlopt_set_initial_step1(opt, dx);
}

// ============================================================================
// Stochastic search algorithms
// ============================================================================

int32_t NLOPT_set_population(int p) {
  return nlopt_set_population(opt, (unsigned) p);
}

void NLOPT_srand(int64_t seed) {
  nlopt_srand((unsigned long) seed);
}