#include <iostream>
#include "Function3D.h"
#include "Function2D.h"
#include "Function.h"

//必须在Eigen之前
#include "AuroraDefs.h"

using namespace Aurora;

Matrix Aurora::interp3(const Matrix& aX, const Matrix& aY, const Matrix& aZ, const Matrix& aV, const Matrix& aX1, const Matrix& aY1, const Matrix& aZ1,InterpnMethod aMethod)
{
    if (aV.getDims() != 3)
    {
        return Matrix();
    }

    if ( aX1.getDimSize(0) != aY1.getDimSize(0) ||
         aX1.getDimSize(0) != aZ1.getDimSize(0) ||
         aZ1.getDimSize(0) != aY1.getDimSize(0))
    {
        return Matrix();
    }
    int nx1 = aX1.getDimSize(0);
    int zAxisNum = aV.getDimSize(2);
    int columnNum = aV.getDimSize(1);
    int rowNum = aV.getDimSize(0);

    if(aX.getDimSize(0) != columnNum || aY.getDimSize(0) != rowNum || aZ.getDimSize(0) != zAxisNum)
    {
      return Matrix();
    }
    std::shared_ptr<double> resultData = std::shared_ptr<double>(new double[nx1], std::default_delete<double[]>());
    for (int i = 0; i < nx1; ++i)
    {
        std::shared_ptr<double> zResultData = std::shared_ptr<double>(new double[zAxisNum], std::default_delete<double[]>());
        for(int j =0; j < zAxisNum; ++j)
        {
            zResultData.get()[j] = interp2(aX,aY,aV($,$,j).toMatrix(),aX1(i).toMatrix(),aY1(i).toMatrix(),aMethod).getData()[0];
        }
        Matrix xResult(zResultData,std::vector<int>{columnNum});
        resultData.get()[i] = interp1(aZ,xResult,aZ1(i).toMatrix(),aMethod).getData()[0];
    }
    return Matrix(resultData,std::vector<int>{nx1});
}

Matrix Aurora::interpn(const Matrix& aX, const Matrix& aY, const Matrix& aZ, const Matrix& aV, const Matrix& aX1, const Matrix& aY1, const Matrix& aZ1,InterpnMethod aMethod)
{
    return Aurora::interp3(aY,aX,aZ,aV,aY1,aX1,aZ1,aMethod);
}

Matrix Aurora::ones(int aRow, int aColumn, int aSlice) {
    if (aRow == 0 || aColumn == 0)
    {
        std::cerr<<"ones function can create matrix with dim unit cont =0";
        return Matrix();
    }
    int rowSize =  aRow;
    int colSize =  aColumn;
    int sliceSize = aSlice == 0 ? 1 : aSlice;
    size_t arraySize  = rowSize * colSize* sliceSize;
    double* data = malloc(arraySize);
    double one = 1.0;
    cblas_dcopy(arraySize,&one,0,data,1);
    return Matrix::New(data,rowSize,colSize,aSlice);
}

Matrix Aurora::ones(int aSquareRow) {
    return Aurora::ones(aSquareRow, aSquareRow);
}

Matrix Aurora::zeros(int aRow, int aColumn, int aSlice) {
    if (aRow == 0 || aColumn == 0)
    {
        std::cerr<<"zeros function can create matrix with dim unit cont =0";
        return Matrix();
    }
    int rowSize =  aRow;
    int colSize =  aColumn;
    int sliceSize = aSlice == 0 ? 1 : aSlice;
    size_t arraySize  = rowSize * colSize* sliceSize;
    double* data = malloc(arraySize);
    double zero = 0.0;
    cblas_dcopy(arraySize,&zero,0,data,1);
    return Matrix::New(data,rowSize,colSize,sliceSize);
}

Matrix Aurora::zeros(int aSquareRow) {
    return Aurora::zeros(aSquareRow, aSquareRow);
}

Matrix Aurora::size(const Matrix &aMatrix)
{
    if (aMatrix.isScalar()){
        double * output = Aurora::malloc(1);
        output[0]=1;
        return Matrix::New(output,1,1,1);
    }
    else if (aMatrix.isVector()){
        double * output = Aurora::malloc(1);
        output[0]=aMatrix.getDataSize();
        return Matrix::New(output,1,1,1);
    }
    //3D
    else if (aMatrix.getDimSize(2)>1){
        double * output = Aurora::malloc(3);
        output[0]=aMatrix.getDimSize(0);
        output[1]=aMatrix.getDimSize(1);
        output[2]=aMatrix.getDimSize(2);
        return Matrix::New(output,3,1,1);
    }
    //2D matrix
    else{
        double * output = Aurora::malloc(2);
        output[0]=aMatrix.getDimSize(0);
        output[1]=aMatrix.getDimSize(1);
        return Matrix::New(output,2,1,1);
    }
}
