#include <gtest/gtest.h>
#include <vector>
#include "TestUtility.h"

#include "Matrix.h"
#include "Function.h"
#include "Function1D.h"
#include "Function2D.h"
#include "Function3D.h"


class Function2D_Test: public ::testing::Test{
protected:
    static void SetUpFunction2DTester(){

    }
    static void TearDownTestCase(){
    }
    void SetUp(){
    }
    void TearDown(){
    }
};

TEST_F(Function2D_Test, immse){
    double *dataA = new double[9]{1,2,3,4,5,6,7,8,9};
    double *dataB = new double[9]{10,20,30,40,50,40,30,20,10};
    Aurora::Matrix A = Aurora::Matrix::fromRawData(dataA,3,3);
    Aurora::Matrix B = Aurora::Matrix::fromRawData(dataB,3,3);
    double v = immse(A,B);
    EXPECT_DOUBLE_EQ(fourDecimalRound(v),698.3333)<<" immse error;";
}

TEST_F(Function2D_Test, inv){
    //默认是column major排布数据
    double *dataA=new double[9]{2, 0, 2, 2, 3, 0, 3, 3, 3};
    double *dataB=new double[9]{1, 1, 1, 1, 1, 1, 1, 1, 1};
    Aurora::Matrix A = Aurora::Matrix::fromRawData(dataA,3,3);
    Aurora::Matrix B = Aurora::Matrix::fromRawData(dataB,3,3);
    auto invA = Aurora::inv(A);
    double* result = invA.getData();
    EXPECT_DOUBLE_EQ(0.75, fourDecimalRound(result[0]));
    EXPECT_DOUBLE_EQ(0.5, fourDecimalRound(result[1]));
    EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[2]));
    EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[3]));
    EXPECT_DOUBLE_EQ(.0, fourDecimalRound(result[4]));
    EXPECT_DOUBLE_EQ(0.3333, fourDecimalRound(result[5]));
    EXPECT_DOUBLE_EQ(-0.25, fourDecimalRound(result[6]));
    EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[7]));
    EXPECT_DOUBLE_EQ(0.5, fourDecimalRound(result[8]));
    invA = Aurora::inv(A*B);
    result = invA.getData();
    EXPECT_DOUBLE_EQ(0.75, fourDecimalRound(result[0]));
    EXPECT_DOUBLE_EQ(0.5, fourDecimalRound(result[1]));
    EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[2]));
    EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[3]));
    EXPECT_DOUBLE_EQ(.0, fourDecimalRound(result[4]));
    EXPECT_DOUBLE_EQ(0.3333, fourDecimalRound(result[5]));
    EXPECT_DOUBLE_EQ(-0.25, fourDecimalRound(result[6]));
    EXPECT_DOUBLE_EQ(-0.5, fourDecimalRound(result[7]));
    EXPECT_DOUBLE_EQ(0.5, fourDecimalRound(result[8]));
}

TEST_F(Function2D_Test, std){
    double *dataMA=  new double [9]{1, 2, 3, 2, 2, 6, 3, 3, 6};
    auto A = Aurora::Matrix::fromRawData(dataMA,3,3);
    auto D= Aurora::std(A);
    double* resultStd = D.getData();
    EXPECT_DOUBLE_EQ(1.0, resultStd[0]);
    EXPECT_DOUBLE_EQ(2.3094, fourDecimalRound(resultStd[1]));
    EXPECT_DOUBLE_EQ(1.7321, fourDecimalRound(resultStd[2]));

}

TEST_F(Function2D_Test, min) {
    double *dataA = new double[3]{1, 2, 3};
    double *dataB = new double[9]{2, 3, 3, 2, 2, -1, 3, 3, 3};
    double *dataC = new double[1]{1.5};
    auto A = Aurora::Matrix::fromRawData(dataA, 3, 1);
    auto B = Aurora::Matrix::fromRawData(dataB, 3, 3);
    auto C = Aurora::Matrix::fromRawData(dataC, 1);
    auto D = Aurora::Matrix::copyFromRawData(dataA, 1, 3);
    Aurora::Matrix ret = Aurora::min(B);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(3, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(2, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(-1, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(3, ret.getData()[2]);
    ret = Aurora::min(B, Aurora::All);
    EXPECT_DOUBLE_EQ(1, ret.getDataSize());
    EXPECT_DOUBLE_EQ(-1, ret.getData()[0]);
    ret = Aurora::min(B, Aurora::Row);
    EXPECT_DOUBLE_EQ(3, ret.getDataSize());
    EXPECT_EQ(3, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(2, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(2, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(-1, ret.getData()[2]);
    long r,c;
    ret = Aurora::min(A, Aurora::Column,r,c);
    EXPECT_DOUBLE_EQ(1, ret.getDataSize());
    EXPECT_DOUBLE_EQ(1, ret.getData()[0]);
    EXPECT_EQ(0, r);
    EXPECT_EQ(0, c);
    ret = Aurora::min(D);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(1, ret.getData()[0]);
    ret = Aurora::min(A, C);
    EXPECT_DOUBLE_EQ(3, ret.getDataSize());
    EXPECT_DOUBLE_EQ(1, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(1.5, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(1.5, ret.getData()[2]);
    ret = Aurora::min(B,D);
    EXPECT_DOUBLE_EQ(9, ret.getDataSize());
    EXPECT_DOUBLE_EQ(1, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(1, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(1, ret.getData()[2]);
}

TEST_F(Function2D_Test, max) {
    double *dataA = new double[3]{1, 2, 3};
    double *dataC = new double[3]{1, 2, 4};
    double *dataB = new double[9]{2, 3, 3, 2, 2, 1, 3, 3, 3};
    auto A = Aurora::Matrix::fromRawData(dataA, 3, 1);
    auto B = Aurora::Matrix::fromRawData(dataB, 3, 3);
    auto C = Aurora::Matrix::fromRawData(dataC, 3, 1);

    Aurora::Matrix ret = Aurora::max(B);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(3, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(3, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(2, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(3, ret.getData()[2]);
    ret = Aurora::max(B, Aurora::All);
    EXPECT_DOUBLE_EQ(1, ret.getDataSize());
    EXPECT_DOUBLE_EQ(3, ret.getData()[0]);
    ret = Aurora::max(B, Aurora::Row);
    EXPECT_DOUBLE_EQ(3, ret.getDataSize());
    EXPECT_EQ(3, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(3, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(3, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(3, ret.getData()[2]);
    long r,c;
    ret = Aurora::max(A, Aurora::Column,r,c);
    EXPECT_DOUBLE_EQ(1, ret.getDataSize());
    EXPECT_DOUBLE_EQ(3, ret.getData()[0]);
    EXPECT_EQ(2, r);
    EXPECT_EQ(0, c);
    auto D = Aurora::Matrix::copyFromRawData(dataA, 1, 3);
    ret = Aurora::max(D);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(3, ret.getData()[0]);

    ret = Aurora::max(A,C);
    EXPECT_DOUBLE_EQ(4, ret.getData()[2]);
}

TEST_F(Function2D_Test, sum) {
    double *dataB = new double[9]{2, 3, 3, 2, 2, 1, 3, 3, -3};
    auto B = Aurora::Matrix::fromRawData(dataB, 3, 3);
    Aurora::Matrix ret = Aurora::sum(B);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(3, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(8, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(5, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(3, ret.getData()[2]);
    ret = Aurora::sum(B, Aurora::All);
    EXPECT_DOUBLE_EQ(1, ret.getDataSize());
    EXPECT_DOUBLE_EQ(16, ret.getData()[0]);
    ret = Aurora::sum(B, Aurora::Row);
    EXPECT_DOUBLE_EQ(3, ret.getDataSize());
    EXPECT_EQ(3, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(7, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(8, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(1, ret.getData()[2]);
    double *dataA = new double[3]{1, 2, 3};
    auto D = Aurora::Matrix::copyFromRawData(dataA, 1, 3);
    ret = Aurora::sum(D);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(6, ret.getData()[0]);
}

TEST_F(Function2D_Test, mean) {

    {
        double *dataB = new double[16]{1.1, 2.6, 3.8, 6.2,
                                       4.3, 5.7, 6.9, 10.6,
                                       7.1, 8.3, 9.7, 11.2,
                                       17.8, 13.3, 26.5, -7.7};
        auto B = Aurora::Matrix::fromRawData(dataB, 4, 4);
        auto r = Aurora::mean(B, Aurora::All);
        EXPECT_EQ(1, r.getDimSize(0));
        EXPECT_EQ(1, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(7.9625, fourDecimalRound(r.getData()[0]));

        r = Aurora::mean(B);
        EXPECT_EQ(1, r.getDimSize(0));
        EXPECT_EQ(4, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(3.4250, fourDecimalRound(r.getData()[0]));
        EXPECT_DOUBLE_EQ(6.8750, fourDecimalRound(r.getData()[1]));
        EXPECT_DOUBLE_EQ(9.0750, fourDecimalRound(r.getData()[2]));
        EXPECT_DOUBLE_EQ(12.4750, fourDecimalRound(r.getData()[3]));

        r = Aurora::mean(B, Aurora::Row);
        EXPECT_EQ(4, r.getDimSize(0));
        EXPECT_EQ(1, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(7.5750, fourDecimalRound(r.getData()[0]));
        EXPECT_DOUBLE_EQ(7.4750, fourDecimalRound(r.getData()[1]));
        EXPECT_DOUBLE_EQ(11.7250, fourDecimalRound(r.getData()[2]));
        EXPECT_DOUBLE_EQ(5.0750, fourDecimalRound(r.getData()[3]));
    }
    //with nan
    {
        double tnan = std::nan("");
        double *dataB = new double[16]{1.1, 2.6, 3.8, 6.2,
                                       4.3, 5.7, 6.9, 10.6,
                                       7.1, 8.3, 9.7, 11.2,
                                       17.8, 13.3,tnan , -7.7};
        auto B = Aurora::Matrix::fromRawData(dataB, 4, 4);
        auto r = Aurora::mean(B, Aurora::All);
        EXPECT_EQ(1, r.getDimSize(0));
        EXPECT_EQ(1, r.getDimSize(1));
        EXPECT_TRUE(std::isnan(r.getData()[0]));

        r = Aurora::mean(B);
        EXPECT_EQ(1, r.getDimSize(0));
        EXPECT_EQ(4, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(3.4250, fourDecimalRound(r.getData()[0]));
        EXPECT_DOUBLE_EQ(6.8750, fourDecimalRound(r.getData()[1]));
        EXPECT_DOUBLE_EQ(9.0750, fourDecimalRound(r.getData()[2]));
        EXPECT_TRUE(std::isnan(r.getData()[3]));

        r = Aurora::mean(B, Aurora::Row);
        EXPECT_EQ(4, r.getDimSize(0));
        EXPECT_EQ(1, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(7.5750, fourDecimalRound(r.getData()[0]));
        EXPECT_DOUBLE_EQ(7.4750, fourDecimalRound(r.getData()[1]));
        EXPECT_TRUE(std::isnan(r.getData()[2]));
        EXPECT_DOUBLE_EQ(5.0750, fourDecimalRound(r.getData()[3]));

        r = Aurora::mean(B, Aurora::All,false);
        EXPECT_EQ(1, r.getDimSize(0));
        EXPECT_EQ(1, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(6.7267, fourDecimalRound(r.getData()[0]));

        r = Aurora::mean(B, Aurora::Column, false);
        EXPECT_EQ(1, r.getDimSize(0));
        EXPECT_EQ(4, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(3.4250, fourDecimalRound(r.getData()[0]));
        EXPECT_DOUBLE_EQ(6.8750, fourDecimalRound(r.getData()[1]));
        EXPECT_DOUBLE_EQ(9.0750, fourDecimalRound(r.getData()[2]));
        EXPECT_DOUBLE_EQ(7.8, fourDecimalRound(r.getData()[3]));

        r = Aurora::mean(B, Aurora::Row, false);
        EXPECT_EQ(4, r.getDimSize(0));
        EXPECT_EQ(1, r.getDimSize(1));
        EXPECT_DOUBLE_EQ(7.5750, fourDecimalRound(r.getData()[0]));
        EXPECT_DOUBLE_EQ(7.4750, fourDecimalRound(r.getData()[1]));
        EXPECT_DOUBLE_EQ(6.8, fourDecimalRound(r.getData()[2]));
        EXPECT_DOUBLE_EQ(5.0750, fourDecimalRound(r.getData()[3]));
    }
}

TEST_F(Function2D_Test, sort) {
    double *dataB = new double[16]{1.1, 2.6, 6.2, 3.8,
                                   4.3, 10.6, 5.7, 6.9,
                                   7.1, 8.3, 9.7, 11.2,
                                   17.8, 13.3,7 , -7.7};
    auto B = Aurora::Matrix::fromRawData(dataB, 4, 4);
    auto ret = Aurora::sort(B);
    EXPECT_EQ(4, ret.getDimSize(0));
    EXPECT_EQ(4, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(3.8, ret.getData()[2]);
    EXPECT_DOUBLE_EQ(5.7, ret.getData()[5]);
    EXPECT_DOUBLE_EQ(-7.7, ret.getData()[12]);
    ret = Aurora::sort(B*5);
    EXPECT_EQ(4, ret.getDimSize(0));
    EXPECT_EQ(4, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(19, ret.getData()[2]);
    EXPECT_DOUBLE_EQ(28.5, ret.getData()[5]);
    EXPECT_DOUBLE_EQ(-38.5, ret.getData()[12]);
    //big sort 10w以上多线程快
    double * dataA = Aurora::random(1000000*4);
    auto A = Aurora::Matrix::New(dataA, 1000000, 4);
    ret = Aurora::sort(A);
}

TEST_F(Function2D_Test, sortrows) {
    double *dataB = new double[16]{1.1, 2.6, 6.2, 3.8,
                                   4.3, 10.6, 5.7, 6.9,
                                   7.1, 8.3, 9.7, 11.2,
                                   17.8, 13.3,7 , -7.7};
    auto B = Aurora::Matrix::fromRawData(dataB, 4, 4);
    auto ret = Aurora::sortrows(B);
    EXPECT_EQ(4, ret.getDimSize(0));
    EXPECT_EQ(4, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(5.7, ret.getData()[2]);
    EXPECT_DOUBLE_EQ(8.3, ret.getData()[5]);
    EXPECT_DOUBLE_EQ(17.8, ret.getData()[12]);
    ret = Aurora::sortrows(B*5);
    EXPECT_EQ(4, ret.getDimSize(0));
    EXPECT_EQ(4, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(28.5, ret.getData()[2]);
    EXPECT_DOUBLE_EQ(41.5, ret.getData()[5]);
    EXPECT_DOUBLE_EQ(89, ret.getData()[12]);
    //big sort 10w以上多线程快
    double * dataA = Aurora::random(1000000*4);
    auto A = Aurora::Matrix::New(dataA, 4, 1000000);
    ret = Aurora::sortrows(A);
}

TEST_F(Function2D_Test, median) {
    double *dataB = new double[20]{1.1, 2.6, 3.8, 6.2,
                                   4.3, 5.7, 6.9, 10.6,
                                   7.1, 8.3, 9.7, 11.2,
                                   17.8, 13.3,26.5 , -7.7,
                                   9.9, 8.2, 6.3, 5.1};
    auto B = Aurora::Matrix::fromRawData(dataB, 4, 5);
    auto ret = Aurora::median(B);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(5, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(3.2, ret.getData()[0]);
    EXPECT_DOUBLE_EQ(6.3, ret.getData()[1]);
    EXPECT_DOUBLE_EQ(9, ret.getData()[2]);
    EXPECT_DOUBLE_EQ(15.55, ret.getData()[3]);
    EXPECT_DOUBLE_EQ(7.25, ret.getData()[4]);
    double *dataA = new double[3]{1, 2, 3};
    auto D = Aurora::Matrix::copyFromRawData(dataA, 1, 3);
    ret = Aurora::median(D);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(2, ret.getData()[0]);
}

TEST_F(Function2D_Test, fftAndComplexAndIfft){
    double *input =  new double[20]{1,1,0,2,2,0,1,1,0,2,1,1,0,2,2,0,1,1,0,2};
    auto ma = Aurora::Matrix::fromRawData(input,10,2);
    auto fftrett = Aurora::fft(ma);
    std::complex<double>* result = (std::complex<double>*)fftrett.getData();
    //检验fft结果与matlab是否对应
    EXPECT_DOUBLE_EQ(0.0729, fourDecimalRound(result[1].real()));
    EXPECT_DOUBLE_EQ(2.4899, fourDecimalRound(result[2].imag()));
    EXPECT_DOUBLE_EQ(0.0729, fourDecimalRound(result[11].real()));
    EXPECT_DOUBLE_EQ(2.4899, fourDecimalRound(result[12].imag()));
    //检验fft的结果是否共轭
    EXPECT_DOUBLE_EQ(0, result[4].imag()+result[6].imag());
    EXPECT_DOUBLE_EQ(0, result[4].real()-result[6].real());
    auto ret= Aurora::ifft(fftrett);
    std::complex<double>* ifftResult = (std::complex<double>*)ret.getData();
    EXPECT_DOUBLE_EQ(fourDecimalRound(ifftResult[1].real()),1.0);
    EXPECT_DOUBLE_EQ(fourDecimalRound(ifftResult[3].real()),2.0);
    EXPECT_DOUBLE_EQ(fourDecimalRound(ifftResult[11].real()),1.0);
    EXPECT_DOUBLE_EQ(fourDecimalRound(ifftResult[13].real()),2.0);
    Aurora::fftshift(fftrett);
    EXPECT_DOUBLE_EQ(0.0729, fourDecimalRound(result[6].real()));
    EXPECT_DOUBLE_EQ(2.4899, fourDecimalRound(result[7].imag()));
    EXPECT_DOUBLE_EQ(-2., fourDecimalRound(result[10].real()));
    EXPECT_DOUBLE_EQ(-0.2245, fourDecimalRound(result[11].imag()));

    auto fftrett2 = Aurora::fft(ma,5);
    auto result2 = (std::complex<double>*)fftrett2.getData();
    //检验fft结果与matlab是否对应
    EXPECT_DOUBLE_EQ(0.3090, fourDecimalRound(result2[1].real()));
    EXPECT_DOUBLE_EQ(-1.3143, fourDecimalRound(result2[2].imag()));
    EXPECT_DOUBLE_EQ(-0.8090, fourDecimalRound(result2[7].real()));
    EXPECT_DOUBLE_EQ(1.3143, fourDecimalRound(result2[8].imag()));

    auto fftrett3 = Aurora::fft(ma,12);
    auto result3 = (std::complex<double>*)fftrett3.getData();
    //检验fft结果与matlab是否对应
    EXPECT_DOUBLE_EQ(-1.0, fourDecimalRound(result3[1].real()));
    EXPECT_DOUBLE_EQ(-3.4641, fourDecimalRound(result3[4].imag()));
    EXPECT_DOUBLE_EQ(-1.0, fourDecimalRound(result3[13].real()));
    EXPECT_DOUBLE_EQ(-3.4641, fourDecimalRound(result3[16].imag()));
}

TEST_F(Function2D_Test, hilbert) {
    double *input =  new double[20]{1,1,0,2,2,0,1,1,0,2,1,1,0,2,2,0,1,1,0,2};
    auto ma = Aurora::Matrix::fromRawData(input,10,2);
    auto ret = Aurora::hilbert(ma);
    auto result = (std::complex<double>*)ret.getData();
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[1].real()),1.0);
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[1].imag()),0.3249);
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[11].real()),1.0);
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[11].imag()),0.3249);
}

TEST_F(Function2D_Test, ifft_symmetric) {
    double *input =  new double[18]{10,2,1,3,4,4,16,3,1,2,15,-2,1,-3,4,-4,1,-3};
    auto ma = Aurora::Matrix::fromRawData(input,9,1,1,Aurora::Complex);
    auto ret = Aurora::ifft_symmetric(ma,18);
    auto result = ret.getData();
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[0]),5.3333);
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[1]),1.1188);
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[11]),2.8506);
    EXPECT_DOUBLE_EQ(fourDecimalRound(result[17]),1.1188);
}

TEST_F(Function2D_Test, prod) {
    double *dataB = new double[20]{1.1, 2.6, 3.8, 6.2,
                                   4.3, 5.7, 6.9, 10.6,
                                   7.1, 8.3, 9.7, 11.2,
                                   17.8, 13.3,26.5 , -7.7,
                                   9.9, 8.2, 6.3, 5.1};
    auto B = Aurora::Matrix::fromRawData(dataB, 4, 5);
    auto ret = Aurora::prod(B);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(5, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(0.0067, fourDecimalRound(ret.getData()[0]/10000));
    EXPECT_DOUBLE_EQ(0.1793, fourDecimalRound(ret.getData()[1]/10000));
    EXPECT_DOUBLE_EQ(0.6402, fourDecimalRound(ret.getData()[2]/10000));
    EXPECT_DOUBLE_EQ(-4.8307, fourDecimalRound(ret.getData()[3]/10000));
    EXPECT_DOUBLE_EQ(0.2608, fourDecimalRound(ret.getData()[4]/10000));
    double *dataA = new double[3]{1, 2, 3};
    auto D = Aurora::Matrix::copyFromRawData(dataA, 1, 3);
    ret = Aurora::prod(D);
    EXPECT_EQ(1, ret.getDimSize(0));
    EXPECT_EQ(1, ret.getDimSize(1));
    EXPECT_DOUBLE_EQ(6, ret.getData()[0]);
}

TEST_F(Function2D_Test, dot) {
    {
        double *dataA = new double[8];
        double *dataB = new double[8];
        for (int i = 0; i < 8; ++i) {
            dataA[i] = (double) (i);
            dataB[i] = (double) (2);
        }
        Aurora::Matrix A = Aurora::Matrix::fromRawData(dataA, 4, 2);
        Aurora::Matrix B = Aurora::Matrix::fromRawData(dataB, 4, 2);
        auto C = Aurora::dot(A, B);
        EXPECT_EQ(1, C.getDimSize(0));
        EXPECT_EQ(2, C.getDimSize(1));
        EXPECT_DOUBLE_EQ(12, fourDecimalRound(C.getData()[0]));
        EXPECT_DOUBLE_EQ(44, fourDecimalRound(C.getData()[1]));
    }
    {
        double *dataB = new double[20]{1.1, 2.6, 3.8, 6.2,
                                       4.3, 5.7, 6.9, 10.6,
                                       7.1, 8.3, 9.7, 11.2,
                                       17.8, 13.3,26.5 , -7.7,
                                       9.9, 8.2, 6.3, 5.1};
        auto B = Aurora::Matrix::fromRawData(dataB, 4, 5);
        auto C = Aurora::dot(Aurora::fft(B),Aurora::fft(B));
        EXPECT_EQ(1, C.getDimSize(0));
        EXPECT_EQ(5, C.getDimSize(1));
        EXPECT_DOUBLE_EQ(0.2434, fourDecimalRound(C.getData()[0]/1000));
        EXPECT_DOUBLE_EQ(0., fourDecimalRound(C.getData()[1]/1000));
        EXPECT_DOUBLE_EQ(0.8438, fourDecimalRound(C.getData()[2]/1000));
        EXPECT_DOUBLE_EQ(0., fourDecimalRound(C.getData()[3]/1000));
        EXPECT_DOUBLE_EQ(1.3553, fourDecimalRound(C.getData()[4]/1000));
        EXPECT_DOUBLE_EQ(0., fourDecimalRound(C.getData()[5]/1000));
        EXPECT_DOUBLE_EQ(5.0211, fourDecimalRound(C.getData()[6]/1000));
        EXPECT_DOUBLE_EQ(0., fourDecimalRound(C.getData()[7]/1000));
        EXPECT_DOUBLE_EQ(0.9238, fourDecimalRound(C.getData()[8]/1000));
        EXPECT_DOUBLE_EQ(0., fourDecimalRound(C.getData()[9]/1000));
    }
}

TEST_F(Function2D_Test, interp2) {
    double* xD= new double[4]{1,2,3,4};
    Aurora::Matrix x(std::shared_ptr<double>(xD,std::default_delete<double[]>()),std::vector<int>{4});
    double* yD= new double[5]{1,4,9,16,25};
    Aurora::Matrix y(std::shared_ptr<double>(yD,std::default_delete<double[]>()),std::vector<int>{5});
    double* x1D= new double[3]{1.22,2.69,3.84};
    Aurora::Matrix x1(std::shared_ptr<double>(x1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* y1D= new double[3]{4.5823,10.552,18};
    Aurora::Matrix y1(std::shared_ptr<double>(y1D,std::default_delete<double[]>()),std::vector<int>{3});
    double* vD= new double[20]{158,256,332,428,22,56,487,54,26,333,541,554,445,48,22,54,36,98,11,25};
    Aurora::Matrix v(std::shared_ptr<double>(vD,std::default_delete<double[]>()),std::vector<int>{5,4});

    Aurora::Matrix result = interp2(x,y,v,x1,y1,Aurora::InterpnMethod::Spline);
    EXPECT_DOUBLE_AE(result.getData()[0],303.5663);
    EXPECT_DOUBLE_AE(result.getData()[1],243.6711);
    EXPECT_DOUBLE_AE(result.getData()[2],-47.32);

    result = interp2(x,y,v,x1,y1,Aurora::InterpnMethod::Linear);
    EXPECT_DOUBLE_AE(result.getData()[0],302.6298);
    EXPECT_DOUBLE_AE(result.getData()[1],261.1313);
    EXPECT_DOUBLE_AE(result.getData()[2],18.6089);
}

TEST_F(Function2D_Test, sub2ind) {
    double* dI1= new double[4]{1,2,1,2};
    Aurora::Matrix I1(std::shared_ptr<double>(dI1,std::default_delete<double[]>()),std::vector<int>{4});
    double* dI2= new double[4]{2,2,1,1};
    Aurora::Matrix I2(std::shared_ptr<double>(dI2,std::default_delete<double[]>()),std::vector<int>{4});
    double* dI4= new double[4]{1,1,2,2};
    Aurora::Matrix I3(std::shared_ptr<double>(dI4,std::default_delete<double[]>()),std::vector<int>{4});
    double* dsz= new double[3]{2,2,2};
    Aurora::Matrix sz(std::shared_ptr<double>(dsz,std::default_delete<double[]>()),std::vector<int>{3});
    auto ma = Aurora::sub2ind(sz, {I1, I2, I3});
    EXPECT_DOUBLE_EQ(3, ma.getData()[0]);
    EXPECT_DOUBLE_EQ(4, ma.getData()[1]);
    EXPECT_DOUBLE_EQ(5, ma.getData()[2]);
    EXPECT_DOUBLE_EQ(6, ma.getData()[3]);

}

