Пример #1
0
        private void btnLinalg_Click(object sender, EventArgs e)
        {
            if (Linalg == null) Linalg = new CLCalc.CLPrograms.doubleLinearAlgebra();
            //CLCalc.Program.DefaultCQ = 0;

            //float[,] M = new float[5, 3] { { 1, 1, 1 }, { 1, -1, -1 }, { -2, 3, 4 }, { -1, 1, 5 }, { 2, 4, 7 } };
            //float[] b = new float[5] { 1, 2, 3, 4, 5 };
            //float[] err;
            //float[] x = new float[3];

            //x = Linalg.LeastSquaresGS(M, b, x, out err);

            int n = 1500;
            Random rnd = new Random();
            double[,] M = new double[n, n];
            double[] b = new double[n];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    M[i, j] = 1;
                    //M[i, j] = rnd.NextDouble();
                }
                M[i, i] += n;
                b[i] = i+1;
            }

            //double[] err;
            //double[] x = new double[n];

            //x = Linalg.LeastSquaresGS(M, b, x, out err);

            //Testes com LU decomp
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            //OpenCL
            sw.Start();
            double[] sol3 = Linalg.LinSolve(M, b, 1e-18, 20);
            sw.Stop();
            this.Text = " " + sw.Elapsed.TotalSeconds.ToString();
            sw.Reset();
            Application.DoEvents();

            //Convencional
            sw.Start();
            LinearAlgebra.Matrix MM = new LinearAlgebra.Matrix(M);
            double[] sol2 = MM.LinearSolve(b);
            sw.Stop();
            this.Text += " " + sw.Elapsed.TotalSeconds.ToString();
            sw.Reset();
            Application.DoEvents();

            double erro = 0;
            for (int i = 0; i < sol3.Length; i++) erro += (sol3[i] - sol2[i]) * (sol3[i] - sol2[i]);

            this.Text += " " + erro.ToString();
        }
Пример #2
0
        private void btnLinalgLU_Click(object sender, EventArgs e)
        {
            if (Linalg == null) Linalg = new CLCalc.CLPrograms.doubleLinearAlgebra();

            float[,] M = new float[,] { { 3, 100, 2, 3 }, { 4, -1, 1, 10 }, { 5, -20, 1, 1 }, { 6, 1, 200, 3 } };
            float[] b = new float[] { 1, 2, 3, 4 };

            double[,] M2 = new double[,] { { 3, 100, 2, 3 }, { 4, -1, 1, 10 }, { 5, -20, 1, 1 }, { 6, 1, 200, 3 } };
            double[] b2 = new double[] { 1, 2, 3, 4 };

            //float[,] M = new float[,] { { 3, 1, 1 }, { 2, -10, 1 }, { 15, 1, 1 } };
            //float[] b = new float[] { 1, 2, 3 };

            //double[,] M2 = new double[,] { { 3, 1, 1 }, { 2, -10, 1 }, { 15, 1, 1 } };
            //double[] b2 = new double[] { 1, 2, 3 };

            LinearAlgebra.Matrix MM = new LinearAlgebra.Matrix(M2);
            double[] sol2 = MM.LinearSolve(b2);

            double[] sol3 = Linalg.LinSolve(M2, b2, 1e-10, 10);

            float[] sol = floatLinalg.LinSolve(M, b, 1e-5f, 10);
        }