package org.jeecg.modules.warning.common;

public class Fitting {
    public static double[] MultiLine(Double[] arrX, Double[] arrY, int length, int dimension)//二元多次线性方程拟合曲线
    {
        int n = dimension + 1;                  //dimension次方程需要求 dimension+1个 系数
        Double[][] Guass = new Double[n][n + 1];      //高斯矩阵 例如：y=a0+a1*x+a2*x*x
        for (int i = 0; i < n; i++)
        {
            int j;
            for (j = 0; j < n; j++)
            {
                Guass[i][j] = SumArr(arrX, j + i, length);
            }
            Guass[i][j] = SumArr(arrX, i, arrY, 1, length);
        }

        return ComputGauss(Guass, n);

    }
    private static double SumArr(Double[] arr, int n, int length) //求数组的元素的n次方的和
    {
        double s = 0;
        for (int i = 0; i < length; i++)
        {
            if (arr[i] != 0 || n != 0)
                s = s + Math.pow(arr[i], n);
            else
                s = s + 1;
        }
        return s;
    }
    private static double SumArr(Double[] arr1, int n1, Double[] arr2, int n2, int length)
    {
        double s = 0;
        for (int i = 0; i < length; i++)
        {
            if ((arr1[i] != 0 || n1 != 0) && (arr2[i] != 0 || n2 != 0))
                s = s + Math.pow(arr1[i], n1) * Math.pow(arr2[i], n2);
            else
                s = s + 1;
        }
        return s;

    }
    private static double[] ComputGauss(Double[][] Guass, int n)
    {
        int i, j;
        int k, m;
        double temp;
        double max;
        double s;
        double[] x = new double[n];
        for (i = 0; i < n; i++) x[i] = 0.0;//初始化
        for (j = 0; j < n; j++)
        {
            max = 0;
            k = j;
            for (i = j; i < n; i++)
            {
                if (Math.abs(Guass[i][j]) > max)
                {
                    max = Guass[i][j];
                    k = i;
                }
            }
            if (k != j)
            {
                for (m = j; m < n + 1; m++)
                {
                    temp = Guass[j][m];
                    Guass[j][m] = Guass[k][m];
                    Guass[k][m] = temp;
                }
            }
            if (0 == max)
            {
                // "此线性方程为奇异线性方程"
                return x;
            }
            for (i = j + 1; i < n; i++)
            {
                s = Guass[i][j];
                for (m = j; m < n + 1; m++)
                {
                    Guass[i][m] = Guass[i][m] - Guass[j][m] * s / (Guass[j][j]);
                }
            }
        }//结束for (j=0;j<n;j++)
        for (i = n - 1; i >= 0; i--)
        {
            s = 0;
            for (j = i + 1; j < n; j++)
            {
                s = s + Guass[i][j] * x[j];
            }
            x[i] = (Guass[i][n] - s) / Guass[i][i];
        }
        return x;
    }//返回值是函数的系数
}
