Exemplo n.º 1
0
        public double[] NonParallelSolve(double[,] matrix, double[] koeficients)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            System.Console.WriteLine("Solving by Kramer (Non-Parallel) is started!");
            System.Console.WriteLine("Proccessing...");

            int size = matrix.GetLength(0);

            double[] results = new double[size];
            D = Determinant.Calculate(matrix);
            double[] d = new double[matrix.GetLength(0)];
            double[,] tempMatrix;
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                tempMatrix = new double[size, size];
                for (int j = 0; j < size; j++)
                {
                    for (int k = 0; k < size; k++)
                    {
                        if (k == i)
                        {
                            tempMatrix[j, k] = koeficients[j];
                            continue;
                        }
                        tempMatrix[j, k] = matrix[j, k];
                    }
                }

                d[i] = Determinant.Calculate(tempMatrix);
                //System.Console.WriteLine($"d[{i + 1}] = {d[i]}");
            }

            for (int i = 0; i < size; i++)
            {
                results[i] = d[i] / D;
            }

            sw.Stop();
            System.Console.WriteLine($"Finished. Time elapsed: {sw.ElapsedMilliseconds}");
            return(results);
        }
Exemplo n.º 2
0
 private void GetPartialDeterminants(int start, int finish, int size, ref double[,] matrix, ref double[] koeficients, ref double[] d)
 {
     lock (obj)
     {
         double[,] tempMatrix = new double[size, size];
         for (int i = start; i < finish; i++)
         {
             for (int j = 0; j < size; j++)
             {
                 for (int k = 0; k < size; k++)
                 {
                     if (k == i)
                     {
                         tempMatrix[j, k] = koeficients[j];
                         continue;
                     }
                     tempMatrix[j, k] = matrix[j, k];
                 }
             }
             d[i] = Determinant.Calculate(tempMatrix);
             System.Console.WriteLine($"d[{i + 1}] = {d[i]}");
         }
     }
 }
Exemplo n.º 3
0
        public double[] Solve(double[,] matrix, double[] koeficients)
        {
            System.Console.WriteLine("Solving by Kramer (Parallel) is started!");
            System.Console.WriteLine("Proccessing...");
            Stopwatch sw = new Stopwatch();

            D = Determinant.Calculate(matrix);
            int size = matrix.GetLength(0);

            double[] results = new double[size];
            sw.Start();
            double[] d    = new double[size];
            Thread[] list = new Thread[THREADS_COUNT];
            for (int i = 0; i < THREADS_COUNT; ++i)
            {
                double diapazone = size / THREADS_COUNT;
                int    begin     = (int)(i * diapazone);
                int    end       = i == THREADS_COUNT - 1 ? size : (int)((i + 1) * diapazone);

                list[i] = new Thread(() => GetPartialDeterminants(begin, end, size, ref matrix, ref koeficients, ref d));
                list[i].Start();
            }

            for (int i = 0; i < THREADS_COUNT; i++)
            {
                list[i].Join();
            }

            for (int i = 0; i < size; i++)
            {
                results[i] = d[i] / D;
            }
            sw.Stop();
            System.Console.WriteLine($"Finished. Time elapsed: {sw.ElapsedMilliseconds}");
            return(results);
        }