高斯消元法求方程

来源:互联网 发布:c语言高级编程视频 编辑:程序博客网 时间:2024/04/28 07:21
/** 高斯消元法求方程
* Gaussian elimination with partial pivoting
* @param A为系数矩阵
* @param b

* @return 计算结果

*/

    public static float[] GSolveE(float[][] A, float[] b)
    {

        private static final float EPSILON = (float) 1e-10;


int N  = b.length;



        for (int p = 0; p < N; p++)
        {


            // find pivot row and swap
            int max = p;
            for (int i = p + 1; i < N; i++)
            {
                if (Math.abs(A[i][p]) > Math.abs(A[max][p]))
                {
                    max = i;
                }
            }
            float[] temp = A[p]; A[p] = A[max]; A[max] = temp;
            float   t    = b[p]; b[p] = b[max]; b[max] = t;


            // singular or nearly singular
            if (Math.abs(A[p][p]) <= EPSILON)
            {
                throw new RuntimeException("Matrix is singular or nearly singular");
            }


            // pivot within A and b
            for (int i = p + 1; i < N; i++)
            {
            float alpha = A[i][p] / A[p][p];
                b[i] -= alpha * b[p];
                for (int j = p; j < N; j++)
                {
                    A[i][j] -= alpha * A[p][j];
                }
            }
        }


        // back substitution
        float[] x = new float[N];
        for (int i = N - 1; i >= 0; i--)
        {
        float sum = 0.0f;
            for (int j = i + 1; j < N; j++)
            {
                sum += A[i][j] * x[j];
            }
            x[i] = (b[i] - sum) / A[i][i];
        }
        return x;
    }
0 0
原创粉丝点击