Ejemplo n.º 1
0
        public static void SubstituteLU(SFix[,] LU, SFix[] b, SFix[] y, SFix[] x)
        {
            int n = LU.GetLength(0);


            for (int i = 0; i < n; i++)
            {
                x[i] = b[i];
            }

            /* do forward substitution, replacing x vector. */

            x[0] /= LU[0, 0];
            for (int i = 1; i < n; i++)
            {
                SFix sum = SFix.FromLong(0, 1);
                for (int j = 0; j < i; j++)
                {
                    sum += LU[i, j] * x[j];
                }
                x[i] = (x[i] - sum) / LU[i, i];
            }

            /* now get the solution vector, x[n-1] is already done */

            for (int i = n - 2; i >= 0; i--)
            {
                SFix sum = SFix.FromLong(0, 1);
                for (int j = i + 1; j < n; j++)
                {
                    sum += LU[i, j] * x[j];
                }
                x[i] -= sum;
            }
        }
Ejemplo n.º 2
0
        /*
         * Code partially ported from:
         *
         * Chapter 2, Programs 3-5, Fig. 2.8-2.10
         * Gerald/Wheatley, APPLIED NUMERICAL ANALYSIS (fourth edition)
         * Addison-Wesley, 1989
         */

        public static void ComputeLU(SFix[,] A, int iw, int fw)
        {
            int n = A.GetLength(0);

            SFix sum;
            SFix diag = (SFix.FromLong(1, iw, fw) / A[0, 0]).Resize(iw, fw);

            for (int i = 1; i < n; i++)
            {
                A[0, i] = (A[0, i] * diag).Resize(iw, fw);
            }

            /*
             *  Now complete the computing of L and U elements.
             *  The general plan is to compute a column of L's, then
             *  call pivot to interchange rows, and then compute
             *  a row of U's.
             */

            int nm1 = n - 1;

            for (int j = 1; j < nm1; j++)
            {
                /* column of L's */
                for (int i = j; i < n; i++)
                {
                    sum = SFix.FromLong(0, iw, fw);
                    for (int k = 0; k < j; k++)
                    {
                        sum = (sum + A[i, k] * A[k, j]).Resize(iw, fw);
                    }
                    A[i, j] = (A[i, j] - sum).Resize(iw, fw);
                }
                /* row of U's */
                diag = (SFix.FromLong(1, iw, fw) / A[j, j]).Resize(iw, fw);
                for (int k = j + 1; k < n; k++)
                {
                    sum = SFix.FromLong(0, iw, fw);
                    for (int i = 0; i < j; i++)
                    {
                        sum = (sum + A[j, i] * A[i, k]).Resize(iw, fw);
                    }
                    A[j, k] = ((A[j, k] - sum) * diag).Resize(iw, fw);
                }
            }

            /* still need to get last element in L Matrix */

            sum = SFix.FromLong(0, iw, fw);
            for (int k = 0; k < nm1; k++)
            {
                sum = (sum + A[nm1, k] * A[k, nm1]).Resize(iw, fw);
            }
            A[nm1, nm1] = (A[nm1, nm1] - sum).Resize(iw, fw);
        }