Example #1
0
 private void button2_Click(object sender, EventArgs e)
 {
     InputDialog InputDlg = new InputDialog();
     InputDlg.Text = "Ввод разреженной матрицы";
     InputDlg.label1.Text = "Введите матрицу в поле ниже и нажмите кнопу 'Отправить'";
     InputDlg.ShowDialog();
     matrix = new Matrix(InputDlg.richTextBox1.Text);
     this.richTextBox2.Text += "\n\nСоздана разряженная матрица:\n" + matrix.ToString();
     spMatrix = new SparseMatrix(matrix);
     this.richTextBox2.Text += "\n\nРазреженное представление:\n" + spMatrix.ToString();
 }
Example #2
0
        private void button7_Click(object sender, EventArgs e)
        {
            const int RepeatsCount = 100;
            Counter timer = new Counter();
            int RowsNum = Convert.ToInt32(this.maskedTextBox3.Text);
            int ColsNum = Convert.ToInt32(this.maskedTextBox2.Text);
            Matrix TestMatrix = new Matrix(RowsNum, ColsNum);
            int Percentage = Convert.ToInt32(this.maskedTextBox1.Text);
            TestMatrix.GenerateRandom(Percentage);
            SparseMatrix TestSpMatrix = new SparseMatrix(TestMatrix);
            Random rand = new Random();
            Double[] TestVector = new Double[ColsNum];
            for (int i = 0; i < ColsNum; ++i)
                TestVector[i] = rand.Next();
            this.progressBar1.Value = 0;
            int Step = 5;
            int PercentPart = 2 * Step * RepeatsCount / 100;

            // Время для обычных матриц
            timer.Reset();
            timer.Start();
            for (int i = 0; i < RepeatsCount; ++i)
            {
                TestMatrix.MulToVector(TestVector);
                if (i % PercentPart == 0)
                    this.progressBar1.Value += Step;
            }
            timer.Stop();

            float Time1 = timer.TotalSeconds;
            this.richTextBox3.Text += "\nВремя умножения для обычных матриц (" + this.maskedTextBox1.Text +
                "% ненулевых элементов): " + Time1.ToString("F3") + ";\nПамять: " + TestMatrix.getSizeOfThis().ToString() +
                " байт на матрицу.";
          
            // Время для разреженных матриц
            timer.Reset();
            timer.Start();
            for (int i = 0; i < RepeatsCount; ++i)
            {
                TestSpMatrix.MulToVector(TestVector);
                if (i % PercentPart == 0)
                    this.progressBar1.Value += Step;
            }
            timer.Stop();
            float Time2 = timer.TotalSeconds;
            this.richTextBox3.Text += "\nВремя умножения для разреженных матриц (" + this.maskedTextBox1.Text +
                "% ненулевых элементов): " + Time2.ToString("F3") + ";\nПамять: " + TestSpMatrix.getSizeOfThis().ToString() +
                " байт на матрицу.";

            int PercentDelta = (int)(((Double) (Time1 / Time2) - 1) * 100);
            this.richTextBox3.Text += "\nУмножение обычных матриц медленнее на : " + PercentDelta.ToString() + "%";

            Double temp = (Double)TestMatrix.getSizeOfThis() / (Double)TestSpMatrix.getSizeOfThis();
            int MemoryPercent = (int)((temp - 1) * 100);
            this.richTextBox3.Text += "\nОбычная матрица занимает памяти больше на : " + MemoryPercent.ToString() + "%\n";

        }
Example #3
0
        public SparseMatrix MulToVector(Double[] Vector)
        {
            Double[] ResultVector = new Double[RowsBegin.Count];
            int NumOfNotNullElements = 0;

            int RowEnd, RowBegin;
            Double temp;
            List<int>.Enumerator enumerator = RowsBegin.GetEnumerator();
            for (int i = 0; i < RowsBegin.Count; ++i)
            {
                enumerator.MoveNext();
                RowBegin = enumerator.Current;
                if (RowBegin == -1)
                {
                    ResultVector[i] = 0;
                    continue;
                }
                RowEnd = getRowEndIndex(i);
                temp = 0;
                for (int j = RowBegin; j < RowEnd; ++j)
                {
                    temp += Elements[j] * Vector[ColumnIndexes[j]];
                }
                ResultVector[i] = temp;
                if (temp != 0)
                    ++NumOfNotNullElements;
            }


            Double[] ResultElements = new Double[NumOfNotNullElements];
            int[] ResultColumnIndexes = new int[NumOfNotNullElements];
            List<int> ResultRowsBegin = new List<int>();
            for (int i = 0, j = 0; j < NumOfNotNullElements; ++i)
            {
                if (ResultVector[i] != 0)
                {
                    ResultElements[j] = ResultVector[i];
                    ResultColumnIndexes[j] = 0;
                    ResultRowsBegin.Add(j);
                    j++;
                }
                else
                {
                    ResultRowsBegin.Add(-1);
                }
            }

            SparseMatrix ResultMatrix = new SparseMatrix(ResultElements, ResultColumnIndexes, ResultRowsBegin);
            return ResultMatrix;
        }