Exemplo n.º 1
0
        private DataGridView InitSrcMatrixView(Matrix2d m, DataGridView view)
        {
            view.ColumnCount = m.XSize;
            view.RowCount = m.YSize;
            view.RowHeadersVisible = view.ColumnHeadersVisible = false;
            view.Dock = DockStyle.Fill;
            view.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            view.AllowUserToResizeColumns = view.AllowUserToResizeRows = false;

            for (int i = 0; i < m.XSize; i++)
            {
                for (int j = 0; j < m.YSize; j++)
                {
                    view.Rows[i].Cells[j].Value = m[i, j].ToString();
                    view.Rows[i].Cells[j].ReadOnly = true;
                    if (j > i)
                    {
                        view.Rows[i].Cells[j].ReadOnly = false;
                        view.Rows[i].Cells[j].Style.BackColor = Color.LightGreen;
                    }
                }
            }

            splitLeftPanel.Panel2.Controls.Add(view);
            pbLeft.Paint += new PaintEventHandler(pbLeft_Paint);
            UpdateGraphInfo();
            pbLeft.Refresh();
            saveToolStripMenuItem.Enabled = true;
            makeMinSpannedTreeToolStripMenuItem.Enabled = true;
            return view;
        }
Exemplo n.º 2
0
 /// <summary>
 /// copy ctor
 /// </summary>
 /// <param name="m">Matrix2d for copy</param>
 public Matrix2d(Matrix2d m)
     : this(m.XSize, m.YSize)
 {
     for (int i = 0; i < YSize; ++i)
     {
         for (int j = 0; j < XSize; ++j)
         {
             elems[j, i] = m[j, i];
         }
     }
 }
Exemplo n.º 3
0
        private DataGridView InitSpannedTreeView(Matrix2d m, DataGridView view)
        {
            view.ColumnCount = m.XSize;
            view.RowCount = m.YSize;
            view.RowHeadersVisible = view.ColumnHeadersVisible = false;
            view.Dock = DockStyle.Fill;
            view.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            view.ReadOnly = true;
            view.AllowUserToResizeColumns = view.AllowUserToResizeRows = false;

            for (int i = 0; i < m.XSize; i++)
            {
                for (int j = 0; j < m.YSize; j++)
                {
                    view.Rows[i].Cells[j].Value = m[i, j].ToString();
                }
            }
            splitRightPanel.Panel2.Controls.Add(view);
            pbRight.Paint += new PaintEventHandler(pbRight_Paint);
            pbRight.Refresh();
            return view;
        }
Exemplo n.º 4
0
        private void addRowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xyView.Rows.Add(1);
            resView.Rows.Add(1);
            int x = xyView.ColumnCount;
            int y = xyView.RowCount;
            srcModel = new Matrix2d(x, y, 0);

            for (int i = 0; i < y; i++)
            {
                for (int j = 0; j < x; j++)
                {
                    try
                    {
                        srcModel[j, i] = double.Parse(xyView.Rows[i].Cells[j].Value.ToString());
                    }
                    catch (Exception)
                    {
                        xyView.Rows[i].Cells[j].Value = 0;
                        srcModel[j, i] = 0;
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create a transpose matrix
        /// http://en.wikipedia.org/wiki/Transpose
        /// </summary>
        /// <returns>Tranpose matrix</returns>
        public Matrix2d Tranpose()
        {
            Matrix2d t = new Matrix2d(this.YSize, this.XSize);

            for (int i = 0; i < YSize; ++i)
            {
                for (int j = 0; j < XSize; ++j)
                {
                    t[i,j] = this[j,i];
                }
            }
            return t;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Created a Cofactor
        /// http://en.wikipedia.org/wiki/Cofactor_(linear_algebra)
        /// </summary>
        /// <param name="x">x index</param>
        /// <param name="y">y index</param>
        /// <returns>Matrix of cofactor</returns>
        public Matrix2d Cofactor(int x, int y)
        {
            Matrix2d C = new Matrix2d(this.XSize -1 , this.YSize-1 );

            int ci = 0, cj = 0;

            for (int i = 0; i < YSize; i++)
            {
                cj = 0;
                for (int j = 0; j < XSize; j++)
                {
                    if (j != x && i != y)
                    {
                        C[cj, ci] = this[j, i];
                        cj++;
                    }
                }
                if (i != y) ci++;
            }
            return C;
        }
Exemplo n.º 7
0
 public static Matrix2d operator -(Matrix2d A, Matrix2d B)
 {
     if (A.XSize != B.XSize || A.YSize != B.YSize)
     {
         throw A.sizeError;
     }
     else
     {
         Matrix2d C = new Matrix2d(A.XSize, A.YSize);
         for (int i = 0; i < C.YSize; i++)
         {
             for (int j = 0; j < C.XSize; j++)
             {
                 C[j, i] = A[j, i] - B[j, i];
             }
         }
         return C;
     }
 }
Exemplo n.º 8
0
        public static Matrix2d operator *(double scalar, Matrix2d A)
        {
            Matrix2d C = new Matrix2d(A.XSize, A.YSize);

                for (int i = 0; i < C.YSize; i++)
                {
                    for (int j = 0; j < C.XSize; j++)
                    {
                        C[j, i] = (A[j,i]*scalar);
                    }
                }
                return C;
        }
Exemplo n.º 9
0
        public static Matrix2d operator *(Matrix2d B, Matrix2d A)
        {
            if (A.YSize != B.XSize)
            {
                throw new System.Exception("impossible to multiply matrices: A[m1*n1], B[m2*n2] where n1 must be equal m2 !!!\n");
            }
            else
            {
                Matrix2d C = new Matrix2d(A.XSize, B.YSize);

                for (int i = 0; i < C.XSize; i++)
                {
                    for (int j = 0; j < C.YSize; j++)
                    {
                        double elem = 0;
                        for (int k = 0; k < A.YSize; k++)
                        {
                            elem += (A[i, k] * B[k, j]);
                        }
                        C[i, j] = elem;
                    }
                }
                return C;
            }
        }
Exemplo n.º 10
0
 public SquareMatrix2d(Matrix2d m)
     : base(m)
 {
 }
Exemplo n.º 11
0
        private void Calc()
        {
            calcModel = new Matrix2d(srcModel.XSize, srcModel.YSize, 0);

            for (int i = 0; i < calcModel.XSize; i++)
            {
                double max = srcModel.MaxValueAtColumn(i);
                for (int j = 0; j < calcModel.YSize; j++)
                {
                    calcModel[i,j] = max - srcModel[i,j];
                }
            }

            for (int i = 0; i < srcModel.YSize; i++)
            {
                double s = 0;
                for (int j = 0; j < srcModel.XSize; j++)
                {
                    double a = srcModel[j, i];
                    double b = Convert.ToDouble(pView[j, 0].Value);
                    s += a*b;
                }
                resView[0, i].Value = s;
            }

            for (int i = 0; i < resView.RowCount; i++)
            {
                resView[1, i].Value = calcModel.MaxValueAtRow(i);
            }

            for (int i = 0; i < resView.RowCount; i++)
            {
                double s = 0;
                for (int j = 0; j < calcModel.XSize; j++)
                {
                    s+=(calcModel[j,i]*calcModel[j,i]);
                }
                resView[2, i].Value = Math.Sqrt(s).ToString();
            }
        }
Exemplo n.º 12
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            if (of.ShowDialog() == DialogResult.OK)
            {
                StreamReader f = new StreamReader(of.FileName);
                int x = Convert.ToInt32(f.ReadLine());
                int y = Convert.ToInt32(f.ReadLine());

                xyView.RowCount = y;
                xyView.ColumnCount = x;
                pView.ColumnCount = x;
                pView.RowCount = 1;
                resView.RowCount = y;

                srcModel = new Matrix2d(x, y, 0);

                for (int i = 0; i < y; i++)
                {
                    string[] s = f.ReadLine().Split();
                    for (int j = 0; j < x; j++)
                    {
                        xyView.Rows[i].Cells[j].Value = double.Parse(s[j]);
                        srcModel[j,i] = double.Parse(s[j]);
                    }
                }

                string[] ss = f.ReadLine().Split();
                for (int j = 0; j < x; j++)
                {
                    pView.Rows[0].Cells[j].Value = double.Parse(ss[j]);
                }
                xyView.CellValueChanged += new DataGridViewCellEventHandler(xyView_CellValueChanged);
            }
        }