Example #1
0
        private void _Integrate(int part)
        {
            Result = (-func(a) + func(b)) / 2;

            int    partsSize = (int)quantity / parts;
            int    ost       = quantity - partsSize * parts;
            int    st        = part * partsSize + ((part < ost) ? part : ost);
            int    fn        = (part + 1) * partsSize + ((part + 1 < ost) ? part : (ost - 1));
            double s         = 0;

            for (int i = st; i <= fn; i++)
            {
                var f = func(a + h * (i + (1 / (double)2)));
                s           += f;
                donePercent += 1;
                EventProgress?.Invoke(donePercent);
                EventColumn?.Invoke((a + h * (i + (1 / (double)2))), f);
            }
            Monitor.Enter(res);
            try
            {
                Result += s;
            }
            finally
            {
                Monitor.Exit(res);
            }
        }
Example #2
0
        private void _Calculate(int part)
        {
            int partsSize = (int)(2 * n) / (parts);
            int ost       = ((int)(2 * n)) - partsSize * parts;
            int st        = part * partsSize + ((part < ost) ? part : ost);
            int fn        = (part + 1) * partsSize + ((part + 1 < ost) ? part : (ost - 1));

            //decimal sum2 = 0;
            //decimal sum4 = 0;
            for (int i = st; i < fn; i++)
            {
                decimal x = left + i * step;
                if (x != left && x != right)
                {
                    if (i % 2 == 0)
                    {
                        s2n = s2n + F(x);
                    }
                    else
                    {
                        sn = sn + F(x);
                    }
                }
                donePercent += 1;
                EventProgress?.Invoke(donePercent);
            }
        }
Example #3
0
        private void Produce()
        {
            while (true)
            {
                EventProgress?.Invoke(needBall[valNum - 1].Count);
                if (!EndMove && needBall[valNum - 1].Count != 0)
                {
                    EndMove = true;
                    Thread.Sleep(300);
                    var first = needBall[valNum - 1].Dequeue();
                    first.Start();
                }
                if (needBall[valNum - 1].Count <= 4)
                {
                    Thread.Sleep(1000);
                    if (needBall[valNum - 1].Count >= 2)
                    {
                        Thread.Sleep(5000);
                    }

                    var rect = new Rectangle(0, 0, width, heigth);
                    var b1   = new Ball(d, rect, valIndex, this);
                    Monitor.Enter(balls);
                    //balls.Add(b1);
                    needBall[valNum - 1].Enqueue(b1);
                    Monitor.Enter(balls);
                    balls.Add(b1);
                    Monitor.Exit(balls);
                    Ball.Count++;
                    Monitor.Exit(balls);
                }
            }
        }
Example #4
0
        /*private void Calculate(int i)
         * {
         *  //вычислить площадь для рузультата
         *  decimal x = left + i * step + step / 2;
         *  Result = Result + F(x) * step;
         *  //вычислить координаты точек
         *  var k = i * 4;
         *  MasPoint[k].X = x - step / 2;
         *  MasPoint[k].Y = 0;
         *  MasPoint[k+1].X = x - step / 2;
         *  MasPoint[k+1].Y = F(x);
         *  MasPoint[k+2].X = x + step / 2;
         *  MasPoint[k+2].Y = F(x);
         *  MasPoint[k+3].X = x + step / 2;
         *  MasPoint[k+3].Y = 0;
         *  donePercent += 1;
         *  EventProgress?.Invoke(donePercent);
         * }*/


        private void _Calculate(int part)
        {
            //Result = (-func(a) + func(b)) / 2;

            int    partsSize = (int)n / parts;
            int    ost       = (int)n - partsSize * parts;
            int    st        = part * partsSize + ((part < ost) ? part : ost);
            int    fn        = (part + 1) * partsSize + ((part + 1 < ost) ? part : (ost - 1));
            double s         = 0;

            for (int i = st; i <= fn; i++)
            {
                decimal x = left + i * step + step / 2;
                Result = Result + F(x) * step;
                //вычислить координаты точек
                var k = i * 4;
                MasPoint[k].X     = x - step / 2;
                MasPoint[k].Y     = 0;
                MasPoint[k + 1].X = x - step / 2;
                MasPoint[k + 1].Y = F(x);
                MasPoint[k + 2].X = x + step / 2;
                MasPoint[k + 2].Y = F(x);
                MasPoint[k + 3].X = x + step / 2;
                MasPoint[k + 3].Y = 0;
                donePercent      += 1;
                EventProgress?.Invoke(donePercent);
            }
        }
Example #5
0
        private void _Integrate(int part)
        {
            int partsSize = (int)(quantity / parts);
            int ost       = quantity - partsSize * parts;
            int st        = part * partsSize + ((part < ost) ? part : ost);
            int fn        = (part + 1) * partsSize + ((part + 1 < ost) ? part : (ost - 1));
            int Count     = 0;

            for (int i = st; i <= fn; i++)
            {
                if (points[i].GetY() < func(points[i].GetX()))
                {
                    Thread.Sleep(100);
                    Count++;
                    EventNeedPoints?.Invoke(points[i].GetX(), points[i].GetY());
                }
                donePercent += 1;
                EventProgress?.Invoke(donePercent);
            }
            Monitor.Enter(res);
            try
            {
                Result += Count;
            }
            finally
            {
                Monitor.Exit(res);
            }
        }
Example #6
0
        public void Integrate()
        {
            max = maxi();
            Parallel.For(
                0,
                parts,
                new Action <int>(Generate)
                );
            Stopwatch sw = new Stopwatch();

            sw.Start();
            Parallel.For(
                0,
                parts,
                new Action <int>(_Integrate)
                );
            double I = Result / (double)(quantity) * Math.Abs(b - a) * 2;

            sw.Stop();
            if (donePercent != quantity)
            {
                EventProgress?.Invoke(quantity);
            }
            EventFinish?.Invoke(I);
            EventTime?.Invoke(sw.ElapsedMilliseconds);
        }
Example #7
0
        private void Schitaem(int i)
        {
            decimal x = Convert.ToDecimal(rnd.Next(left * 1000, right * 1000) / (1000.1));
            decimal y = rnd.Next((int)(Min * 1000), (int)(Max * 1000)) / 1000;

            MasPoint[i].X = x;
            MasPoint[i].Y = y;
            if (Math.Abs(y) <= Math.Abs(F(x)))
            {
                if ((F(x) > 0 && y > 0))
                {
                    MasPoint[i].InArea = true;
                    k++;
                }
                if (F(x) < 0 && y < 0)
                {
                    MasPoint[i].InArea = true;
                    p++;
                }
            }
            else
            {
                MasPoint[i].InArea = false;
            }
            donePercent += 1;
            EventProgress?.Invoke(donePercent);
        }
Example #8
0
        private void _Calculate(int part)
        {
            int partsSize = (int)((int)(n) / parts);
            int ost       = (int)n - partsSize * parts;
            int st        = part * partsSize + ((part < ost) ? part : ost);
            int fn        = (part + 1) * partsSize + ((part + 1 < ost) ? part : (ost - 1));

            //int Count = 0;
            for (int i = st; i <= fn; i++)
            {
                decimal x = Convert.ToDecimal(rnd.Next(left * 1000, right * 1000) / (1000.1));
                decimal y = rnd.Next((int)(Min * 1000), (int)(Max * 1000)) / 1000;
                MasPoint[i].X = x;
                MasPoint[i].Y = y;
                if (Math.Abs(y) <= Math.Abs(F(x)))
                {
                    if ((F(x) > 0 && y > 0))
                    {
                        MasPoint[i].InArea = true;
                        k++;
                    }
                    if (F(x) < 0 && y < 0)
                    {
                        MasPoint[i].InArea = true;
                        p++;
                    }
                }
                else
                {
                    MasPoint[i].InArea = false;
                }
                donePercent += 1;
                EventProgress?.Invoke(donePercent);
            }
        }
Example #9
0
        private void _Integrate(int part)
        {
            Result = -func(a) + func(b);

            int    partsSize = (int)(quantity / 2) / (parts);
            int    ost       = (quantity / 2) - partsSize * parts;
            int    st        = part * partsSize + ((part < ost) ? part : ost);
            int    fn        = (part + 1) * partsSize + ((part + 1 < ost) ? part : (ost - 1));
            double sum2      = 0;
            double sum4      = 0;

            for (int i = st; i <= fn; i++)
            {
                Thread.Sleep(100);
                var s2 = func(a + 2 * i * h);
                var s4 = func(a + h * (2 * i + 1));
                sum2        += s2;
                sum4        += s4;
                donePercent += 1;
                EventProgress?.Invoke(donePercent);
                // EventSpline?.Invoke(a + 2 * i * h, s2);
                //EventSpline?.Invoke(a + h * (2 * i + 1), s4);
            }
            Monitor.Enter(res);
            try
            {
                Result += 2 * sum2;
                Result += 4 * sum4;
            }
            finally
            {
                Monitor.Exit(res);
            }
        }
Example #10
0
        private void _Integrate(int part)
        {
            Result = -func(a) + func(b);
            int partsSize = (int)(q / 2) / (parts);
            int ost       = (q / 2) - partsSize * parts;
            int st        = part * partsSize;

            if (part < ost)
            {
                st += part;
            }
            else
            {
                st += ost;
            }
            int fn = (part + 1) * partsSize;

            if (part + 1 < ost)
            {
                fn += part;
            }
            else
            {
                fn += ost - 1;
            }
            double sum2 = 0;
            double sum4 = 0;

            for (int i = st; i <= fn; i++)
            {
                Thread.Sleep(100);
                var s2 = func(a + 2 * i * h);
                var s4 = func(a + h * (2 * i + 1));
                sum2 += s2;
                sum4 += s4;
                dP   += 1;
                EventProgress?.Invoke(dP);
            }
            Monitor.Enter(res);
            try
            {
                Result += 2 * sum2;
                Result += 4 * sum4;
            }
            finally
            {
                Monitor.Exit(res);
            }
        }
Example #11
0
        private void Work()
        {
            donePercent = 0;
            while (donePercent < 100)
            {
                Thread.Sleep(1000);
                donePercent += 10;
                EventProgress?.Invoke(donePercent);
            }
            Random r   = new Random();
            bool   b   = r.Next(100) % 2 == 0;
            int    res = r.Next(2000, 3000);

            EventFinish?.Invoke(b, res);
        }
Example #12
0
        public void Integrate()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            Parallel.For(0, parts, new Action <int>(_Integrate));
            double I = (h / 3) * Result;

            sw.Stop();
            if (dP != q)
            {
                EventProgress?.Invoke(q / 2);
            }
            EventFinish?.Invoke(I);
            EventTime?.Invoke(sw.ElapsedMilliseconds);
        }
Example #13
0
        public double RectangularI(double left, double right, int n)
        {
            sw.Start();  //Замеряем время
            donePercent = 0;
            Random random = new Random();
            //Rectangular Integration
            double dt        = (right - left) / n;
            double result    = 0;
            int    partsSize = (int)n / parts;
            int    reminder  = n - partsSize * parts;
            object block     = new object();


            void calculating(int part)
            {
                int step   = 10;
                int start  = part * partsSize + ((part < reminder) ? part : reminder);
                int finish = (part + 1) * partsSize + ((part + 1 < reminder) ? part : (reminder - 1));
                //Console.WriteLine("Для {0} потока начальный индекс - {1}, конечный - {2}", part, start, finish);
                int percentsDelta = (int)(Math.Round((double)(100 / n)));
                //Console.WriteLine("{0}: Start - {1}, Finish - {2}, PercentDt - {3}",part, start, finish, percentsDelta);
                double x           = left + (start + 0.5) * dt;
                double part_result = 0;

                for (int i = start; i <= finish; i++)
                {
                    part_result += function(x) * dt;
                    EventDraw?.Invoke(x, function(x), part);
                    x           += dt;
                    donePercent += percentsDelta;
                    if (donePercent > step)
                    {
                        EventProgress?.Invoke(donePercent);
                        step += 10;
                    }
                }
                Monitor.Enter(block);
                result += part_result;
                Monitor.Exit(block);
            }

            Parallel.For(0, parts, calculating);
            sw.Stop();
            EventFinish?.Invoke(result, sw.ElapsedMilliseconds);
            return(result);
        }
Example #14
0
        private void _Integrate(int part)
        {
            Result = (-func(a) + func(b)) / 2;
            int partsSize = (int)q / parts;
            int ost       = q - partsSize * parts;
            int st        = part * partsSize;

            if (part < ost)
            {
                st += part;
            }
            else
            {
                st += ost;
            }
            int fn = (part + 1) * partsSize;

            if (part + 1 < ost)
            {
                fn += part;
            }
            else
            {
                fn += ost - 1;
            }
            double s = 0;

            for (int i = st; i <= fn; i++)
            {
                var f = func(a + h * (i + (1 / (double)2)));
                s  += f;
                dP += 1;
                EventProgress?.Invoke(dP);
                EventColumn?.Invoke((a + h * (i + (1 / (double)2))), f);
            }
            Monitor.Enter(res);
            try
            {
                Result += s;
            }
            finally
            {
                Monitor.Exit(res);
            }
        }
Example #15
0
    /// <summary>
    /// 当从网络接收数据时的回调,每帧调用一次
    /// </summary>
    /// <param name="data">接收到的数据字节流,总长度为构造函数定义的200kb,并非所有的数据都是新的</param>
    /// <param name="dataLength">接收到的数据长度,表示data字节流数组中有多少数据是新接收到的,即0-dataLength之间的数据是刚接收到的</param>
    /// <returns>返回true表示当下载正在进行,返回false表示下载中止</returns>
    protected override bool ReceiveData(byte[] data, int dataLength)
    {
        //Debug.Log("接收到的数据长度:" + ((float)(dataLength / 1024)).ToString("0.0") + "KB/" + ((float)(data.Length / 1024)).ToString("0.0") + "KB");
        if (data == null || data.Length == 0)
        {
            Debug.Log("没有获取到数据缓存!");
            return(false);
        }
        fs.Write(data, 0, dataLength);
        _DownedLength += dataLength;
        //Debug.Log("数据进度=>" + ((float)(_downedLength / 1024)).ToString("0.0") + "KB/" + ((float)(_contentLength / 1024)).ToString("0.0") + "KB");

        if (EventProgress != null)
        {
            EventProgress.Invoke((float)_DownedLength / _ContentLength);   //通知进度消息
        }
        return(true);
    }
Example #16
0
        private void Calculate(int i)
        {
            donePercent += 1;
            EventProgress?.Invoke(donePercent);
            decimal x = left + i * step;

            if (x != left && x != right)
            {
                if (i % 2 == 0)
                {
                    s2n = s2n + F(x);
                }
                else
                {
                    sn = sn + F(x);
                }
            }
        }
Example #17
0
        public double SimpsonI(double left, double right, int n)
        {
            sw.Start();  //Замеряем время
            object block  = new object();
            double result = 0;

            void calculate(int part)
            {
                int    percents_step = 10;
                double width         = (right - left) / (n);
                double local_left    = left + (right - left) * part / parts;

                Console.WriteLine("{0} local_left = {1}", part, local_left);
                double local_result  = 0;
                int    steps         = n / parts;
                int    percentsDelta = (int)(Math.Round((double)(100 / n)));

                for (int step = 0; step < steps; step++)
                {
                    double x1 = local_left + step * width;
                    double x2 = local_left + (step + 1) * width;
                    local_result += (x2 - x1) / 6.0 * (function(x1) + 4.0 * function(0.5 * (x1 + x2)) + function(x2));
                    EventDraw?.Invoke(x1, x2, part);
                    donePercent += percentsDelta;
                    if (donePercent > percents_step)
                    {
                        EventProgress?.Invoke(donePercent);
                        percents_step += 10;
                    }
                }
                Monitor.Enter(block);
                result += local_result;
                Monitor.Exit(block);
            }

            Parallel.For(0, parts, calculate);
            sw.Stop();
            EventFinish?.Invoke(result, sw.ElapsedMilliseconds);
            return(result);
        }
Example #18
0
        //------------------------------------------------------------------------------------
        // Implementierung des Verzeichnisscanners

        // Routine, welche den Dateibaum rekursiv durchläuft
        protected bool traverse_exe(string root_path)
        {
            try
            {
                if (!stop)
                {
                    // Aufrufen der Ereignisse, wenn Eventhandler registriert wurden
                    if (!EnterDir(root_path))
                    {
                        throw new ErrDirTree(ErrNo.EnterDir);
                    }

                    m_dir_count++;

                    string[] files = Directory.GetFiles(root_path);
                    m_file_count += files.Length;

                    foreach (string file in files)
                    {
                        if (!TouchFile(file))
                        {
                            throw new ErrDirTree(ErrNo.TouchFile, file);
                        }
                    }

                    string[] dirs = Directory.GetDirectories(root_path);

                    foreach (string dir in dirs)
                    {
                        if (!traverse_exe(dir))
                        {
                            return(false);
                        }
                    }

                    // Arbeitsforschritt anzeigen: CallBack aufrufen

                    if (EventProgress != null)
                    {
                        EventProgress.Invoke(MakeProgressInfo());
                    }

                    if (!ExitDir(root_path))
                    {
                        throw new ErrDirTree(ErrNo.ExitDir);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (ErrDirTree ex)
            {
                switch (ex.ErrorNo)
                {
                case ErrNo.EnterDir:
                    if (DirTreeTraceSwitch.TraceError)
                    {
                        Trace.WriteLine("Err DirTree: EnterDir im Pfad " + root_path + " abgebrochen");
                    }
                    throw;

                case ErrNo.ExitDir:
                    if (DirTreeTraceSwitch.TraceError)
                    {
                        Trace.WriteLine("Err DirTree: ExitDir im Pfad " + root_path + " abgebrochen");
                    }
                    throw;

                case ErrNo.TouchFile:
                    if (DirTreeTraceSwitch.TraceError)
                    {
                        Trace.WriteLine("Err DirTree: TouchFile im Pfad " + root_path + "\\" + ex.file + " abgebrochen");
                    }
                    throw;
                }
                throw;
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Err DirTree: Es ist ein allgemeiner Fehler in aufgetreten: " + ex.Message);
                throw;
            }

            return(true);
        }
        public double Monte_Carlo(double left, double right, int throwsMultiplier, double sup, double inf, int strats_count)
        {
            sw.Start();                                                                                //Замеряем время
            double dt         = (right - left) / strats_count;                                         //Console.WriteLine("dt: {0}", dt);
            int    throws     = (int)(Math.Round((sup - inf) / dt)) * strats_count * throwsMultiplier; //Console.WriteLine("Всего будет сделано {0} бросков", throws);
            double result     = 0;
            double partLength = (right - left) / parts;
            int    partsSize  = strats_count / parts;
            int    reminder   = strats_count - partsSize * parts;
            object block      = new object();

            void calculating(int part)
            {
                int    percentsDelta = throws / 100;
                Random randomX       = new Random(DateTime.Now.Millisecond * (part + 1));
                Random randomY       = new Random(DateTime.Now.Millisecond * (part + 2));                        //Console.WriteLine("[Thread №{0}] Начаты вычисления в потоке...", part);
                int    count         = 0;                                                                        //Кол-во точек под графиком
                int    start         = part * partsSize + ((part < reminder) ? part : reminder);
                int    finish        = (part + 1) * partsSize + ((part + 1 < reminder) ? part : (reminder - 1)); //Console.WriteLine("[Thread №{0}] Задана область интегрирования: [{1}; {2}]x[{3}, {4}]", part, start*dt, finish*dt + dt, inf, sup);
                double x_id          = left + start * dt;
                double y_id          = inf;

                for (int i = 0; i < throws / parts; i++)
                {                                                //Console.WriteLine("[Thread №{0}] Страта - [{1}; {2}]x[{3}, {4}]", part, x_id, x_id + dt, y_id, y_id + dt);
                    double x = randomX.NextDouble() * dt + x_id; // Генерируем случайную точку в диапазоне (x_id + dx, y_id + dy)
                    double y = randomY.NextDouble() * dt + y_id;
                    if (i % 117 == 0)
                    {
                        EventDraw?.Invoke(x, y, part);               //Проверяем вхождение точки в область под графиком
                    }
                    if (function(x) >= 0)
                    {
                        if (y <= function(x) && y >= 0)
                        {
                            count++;
                        }
                    }
                    else
                    {
                        if (y >= function(x) && y < 0)
                        {
                            count--;                            //Console.WriteLine("[{2}] ({1}) Y[{5}, {6}]", x, y, part, x_id, x_id + dx, y_id, y_id + dy);
                        }
                    } //Дальше Изменение области генерации
                    x_id += dt;                    //Сдвигаемся по Х
                    if (x_id > left + finish * dt) //При выходе за границу по Х
                    {
                        y_id += dt;                //Поднимаемся на уровень выше
                        x_id  = left + start * dt; //Возвращаемся на начальное значения Х
                        if (y_id >= sup)
                        {
                            y_id = inf;              //При выходе за границу по Y и по Х начинаем сначала
                        }
                    }
                    if ((i) % (percentsDelta) == 0)
                    {
                        donePercent++;
                        EventProgress?.Invoke(donePercent);
                    }
                } //Console.WriteLine("[Thread №{0}]: Кол-во попаданий = {1}\n" + "\t Всего бросков: {2}", part, count, throws / parts);
                Monitor.Enter(block);
                result += count;
                Monitor.Exit(block);
            }

            Parallel.For(0, parts, calculating);
            result = (result / throws) * (right - left) * (sup - inf);
            sw.Stop();        //   EventFinish?.Invoke(result, sw.ElapsedMilliseconds);
            return(result);
        }