Example #1
0
        public static void RunTest()
        {
            FixedPointSettings.GlobalDefaultRadix = 10;
            FixedPointSettings.GlobalOverflowMode = EOverflowMode.Fail;

            SFix s1 = SFix.FromDouble(1.0, 2, 2);
            SFix s2 = SFix.FromDouble(-0.75, 2, 2);
            SFix s3 = s1 / s2;

            Random rnd = new Random();
            int    n   = 10;
            int    iw  = 14;
            int    fw  = 14;
            double eps = Math.Pow(2.0, -fw);

            SFix[,] A    = new SFix[n, n];
            double[,] Ad = new double[n, n];
            SFix[]   b  = new SFix[n];
            double[] bd = new double[n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    double a = 2.0 * rnd.NextDouble() - 1.0;
                    Ad[i, j] = a;
                }
            }
            //Ad = new double[,] { { 4, -2, 1 }, { -3, -1, 4 }, { 1, -1, 3 } };
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    double a  = Ad[i, j];
                    SFix   c  = SFix.FromDouble(a, iw, fw);
                    string cs = c.ToString();
                    double ar = c.DoubleValue;
                    double d  = a - ar;
                    double da = Math.Abs(d);
                    Debug.Assert(da < eps);
                    A[i, j]  = c;
                    Ad[i, j] = a;
                }
                double bi = 2.0 * rnd.NextDouble() - 1.0;
                b[i]  = SFix.FromDouble(bi, 3, 33);
                bd[i] = bi;
            }
            SFix[,] Ac = (SFix[, ])A.Clone();
            Console.WriteLine("Original matrix:");
            PrintMatrix(A);
            Console.WriteLine();
            ComputeLU(A, iw, fw);
            Console.WriteLine("LU:");
            PrintMatrix(A);
            Console.WriteLine();

            SFix[] x = new SFix[n];
            SFix[] y = new SFix[n];
            SubstituteLU(A, b, y, x);

            Console.WriteLine("Precisions:");
            for (int i = 0; i < n; i++)
            {
                SFix bc = Ac[i, 0] * x[0];
                for (int j = 1; j < n; j++)
                {
                    bc += Ac[i, j] * x[j];
                }
                SFix d = bc - b[i];
                Console.Write("b[" + i + "] = " + b[i].ToString(10, 2));
                Console.Write(" / " + bc.ToString(10, 2));
                Console.WriteLine(" / d = " + d.ToString(10, 2));
            }
        }