#include <unsupported/Eigen/FFT>

#include <ctime>
#include <iostream>
#include <iterator>
#include <complex>

using namespace std;

void test1();
void test2();
void test3();

int main() {

  test3();

  return 0;
}

void test1() {
  int nrow = 1000;
  int ncol = 10000;

  Eigen::ArrayXXcf d1 = Eigen::ArrayXXcf::Random(nrow, ncol);
  Eigen::ArrayXXcf d2 = Eigen::ArrayXXcf::Random(nrow, ncol);

  Eigen::ArrayXXcf d = Eigen::ArrayXXcf::Zero(nrow, ncol);

  std::clock_t start;
  double duration;

  {
    d = Eigen::ArrayXXcf::Zero(nrow, ncol);
    start = std::clock();

    d = d1 * d2;

    duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "array-wise: " << duration << '\n';
    cout << d(11, 97) << endl;
  }

  {
    d = Eigen::ArrayXXcf::Zero(nrow, ncol);
    start = std::clock();

    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        d(i, j) = d1(i, j) * d2(i, j);
      }
    }

    duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "element-wise: " << duration << '\n';
    cout << d(11, 97) << endl;
  }
}

void test2() {
  Eigen::ArrayXXcf d(10000, 1000);

  d *= 0;

  for (int i = 0; i < 1000; ++i) {
    for (int j = 0; j < 1000; ++j) {
      d(i, j) *= 0;
    }
  }

  cout << d(11, 13) << endl;
  cout << d(0, 0) << endl;
}

void test3() {
  int nrow = 800;
  int ncol = 6000;

  Eigen::ArrayXXcf d1 = Eigen::ArrayXXcf::Random(nrow, ncol);
  Eigen::ArrayXf d2 = Eigen::ArrayXf::Random(ncol);
  Eigen::ArrayXXcf d = Eigen::ArrayXXcf::Zero(nrow, ncol);

  typedef std::vector<float> realseq_t;
  typedef std::vector< std::vector<std::complex<float> > > mat_t;

  mat_t d1_t(nrow);

  for (int i = 0; i < nrow; ++i) {
    d1_t[i].resize(ncol);
    for (int j = 0; j < ncol; ++j) {
      d1_t[i][j] = d1(i, j);
    }
  }

  realseq_t d2_t(ncol);
  for (int i = 0; i < ncol; ++i) {
    d2_t[i] = d2(i);
  }

  std::clock_t start;
  double duration;

  // array-wise - Eigen
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int irow = 0; irow < nrow; ++irow) {
      d.row(irow) = d1.row(irow) + d2.transpose();
    }

    duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "array-wise - Eigen: \t\t" << duration << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }

  // element-wise- Eigen
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        d(i, j) = d1(i, j) + d2(j);
      }
    }

    duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "element-wise- Eigen: \t\t" << duration << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }

  // element-wise - Mix
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        d(i, j) = d1(i, j) + d2_t[j];
      }
    }

    duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "element-wise - Mix: \t\t" << duration << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }

  // element-wise - std::vector
  {
    d = Eigen::ArrayXXf::Zero(nrow, ncol);
    start = std::clock();

    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        d(i, j) = d1_t[i][j] + d2(j);
      }
    }

    duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "element-wise - std::vector: \t" << duration << '\n';
    if (nrow * ncol < 100)
      cout << d << endl;
  }
}