Ejemplo n.º 1
0
        public void Test3(int[] mas)
        {
            DiagonalMatrix <int> m = new DiagonalMatrix <int>(mas);

            Console.WriteLine(m.ToString());

            m.MatrixEvent += (o, e) => Console.WriteLine($"Chng {e.PreviousValue} -> {e.NewValue} \n");
            //m[0, 1] = 12;
            m[1, 1] = 14;
            m[2, 2] = 11;
            Console.WriteLine(m.ToString());
        }
        static void Main(string[] args)
        {
            int[,] array = new int[3, 3] { { 1, 4, 5 }, { 52, 34, 3 }, { 6, 734, 8 } };                                                                                    
            SquareMatrix<int> m = new SquareMatrix<int>(array);
            Console.WriteLine(m.ToString());
            m.Change += IsChange;
            m[1, 2] = 100;
            Thread.Sleep(1000);
            Console.WriteLine();
            Console.WriteLine(m.ToString());

            int[,] arrayM = new int[3, 3] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };                                        
            Matrix<int> firstM = new SquareMatrix<int>(array);
            Console.WriteLine(firstM.ToString());
            Matrix<int> secondM = new SymmetricMatrix<int>(arrayM);
            Console.WriteLine(secondM.ToString());
            var result = WorkWithMatrix.Add( firstM,secondM, (a, b) => a + b);
            System.Console.WriteLine(result.ToString());

            
            int [,] arD1 = new int[3,3]{{1,0,0},{0,1,0},{0,0,1}};
            int[,] arD2 = new int[3, 3] { { 2, 0, 0 }, { 0, 2, 0 }, { 0, 0, 2 } };
            var fisrtDM = new DiagonalMatrix<int>(arD1);
            Console.WriteLine(fisrtDM.ToString());
            var secondDM = new DiagonalMatrix<int>(arD2);
            Console.WriteLine(secondDM.ToString());
            result = WorkWithMatrix.Add(fisrtDM, secondDM, (a, b) => a + b);
            System.Console.WriteLine(result.ToString());


            Console.ReadKey();
        }
            public void DiagonalMatrixCtor_MormalSize()
            {
                DiagonalMatrix <int> matr = new DiagonalMatrix <int>(3);

                string res = "0 0 0 \n0 0 0 \n0 0 0 \n";

                Assert.AreEqual(res, matr.ToString());
            }
            public void DiagonalMatrixCtor_MormalDiagonalMatrix()
            {
                int[,] matr = new int[3, 3]
                {
                    { 0, 2, 4 },
                    { 1, 0, 2 },
                    { 9, 5, 0 }
                };

                DiagonalMatrix <int> sqmatr = new DiagonalMatrix <int>(matr);

                string res = "0 2 4 \n1 0 2 \n9 5 0 \n";

                Assert.AreEqual(res, sqmatr.ToString());
            }
        static void Main(string[] args)
        {
            int[,] squareArray = { { 1, 2, 3 }, { 1, 2, 9 }, { 2, 4, 1 } };
            int[,] diagonalArray = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
            int[,] semetricalArray = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 6 } };
            try
            {
                var squareMatrix = new SquareMatrix<int>(squareArray);
                var diagonalMatrix = new DiagonalMatrix<int>(diagonalArray);
                var semetricalMatrix = new SymmetricMatrix<int>(semetricalArray);
                Console.WriteLine(diagonalMatrix.ToString());
                Console.WriteLine(semetricalMatrix.ToString());
                var resultSum=semetricalMatrix.SumMatrix(diagonalMatrix, (a, b) => a + b);
                Console.WriteLine(resultSum.ToString());
                diagonalMatrix.Changes+= Change;
                diagonalMatrix[2, 2] = 123;
                diagonalMatrix[2, 2] = 0;

            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
               Console.ReadKey();
            }

        }
Ejemplo n.º 6
0
        void wyrównanie()
        {
            double bkier = Properties.Settings.Default.bladkierunkucc;
            double ro    = 2000000 / Math.PI;
            var    dt    = new DataTable();

            var        dt2 = new DataTable();
            DataColumn newColumnNazwadt2 = new DataColumn("Nazwa", typeof(double));
            DataColumn newColumnXdt2     = new DataColumn("X", typeof(double));
            DataColumn newColumnYdt2     = new DataColumn("Y", typeof(double));
            DataColumn newColumnHdt2     = new DataColumn("H", typeof(double));

            newColumnXdt2.DefaultValue = 0;
            newColumnHdt2.DefaultValue = 0;
            newColumnYdt2.DefaultValue = 0;
            dt2.Columns.Add(newColumnNazwadt2);
            dt2.Columns.Add(newColumnXdt2);
            dt2.Columns.Add(newColumnYdt2);
            dt2.Columns.Add(newColumnHdt2);

            var listapkt   = new List <string>();
            var lList      = new List <double>();
            var pList      = new List <double>();
            var obliczenia = new Wspprzyb();

            foreach (DataRow row in database1DataSet.PunktyNieznane.Rows)
            {
                if (Convert.ToBoolean(row.ItemArray[2]) == true && Convert.ToBoolean(row.ItemArray[3]) == false)
                {//tworzy tabele macierz A, pustą
                    string     nazwa      = row.ItemArray[1].ToString();
                    string     nazwaX     = "__X__" + nazwa;
                    string     nazwaY     = "__Y__" + nazwa;
                    string     nazwaK     = "__K__" + nazwa;
                    DataColumn newColumnX = new DataColumn(nazwaX, typeof(double));
                    DataColumn newColumnY = new DataColumn(nazwaY, typeof(double));
                    DataColumn newColumnK = new DataColumn(nazwaK, typeof(double));
                    newColumnX.DefaultValue = 0;
                    newColumnK.DefaultValue = 0;
                    newColumnY.DefaultValue = 0;
                    dt.Columns.Add(newColumnX);
                    dt.Columns.Add(newColumnY);
                    dt.Columns.Add(newColumnK);
                    listapkt.Add(row["Nazwa"].ToString());
                }
            }
            dataGridView1.DataSource = dt;

            foreach (DataRow rowtachimetr in database1DataSet.Tachimetr.Rows)
            {
                //Wypełnianie macierzy równaniami poprawek
                string  cel         = rowtachimetr.ItemArray[2].ToString();
                string  stanowisko  = rowtachimetr.ItemArray[1].ToString();
                double  dlugosc     = Convert.ToDouble(rowtachimetr.ItemArray[5]);
                DataRow celRow      = database1DataSet.PunktyNieznane.Where(rowcel => rowcel.Nazwa.Equals(cel)).FirstOrDefault();
                DataRow stRow       = database1DataSet.PunktyNieznane.AsEnumerable().Where(rowst => rowst.Nazwa == stanowisko).FirstOrDefault();
                double  sx          = Convert.ToDouble(stRow[4]); //wspolrzedne, s- stanowsiko, c- cel
                double  sy          = Convert.ToDouble(stRow[5]);
                double  cx          = Convert.ToDouble(celRow[4]);
                double  cy          = Convert.ToDouble(celRow[5]);
                double  dx          = cx - sx;                      //przyrosty
                double  dy          = cy - sy;
                double  d           = Math.Sqrt(dx * dx + dy * dy); //długość ze wsp
                double  rownKierStX = -dy / Math.Pow(d, 2);         //równania do macierzy A, St-stanowisko, C-cel,Dl-Długość
                double  rownKierStY = dx / Math.Pow(d, 2);
                double  rownKierCx  = dy / Math.Pow(d, 2);
                double  rownKierCy  = -dx / Math.Pow(d, 2);
                double  rownDlStx   = -dx / d;
                double  rownDlSty   = -dy / d;
                double  rownDlCx    = dx / d;
                double  rownDlCy    = -dy / d;
                double  azymut      = obliczenia.Azymut(sx, sy, cx, cy, 2);


                var  row         = dt.NewRow();
                var  row2        = dt.NewRow();
                bool celistnieje = database1DataSet.PunktyNieznane.Any(punkt => punkt.Nazwa.Equals(cel) && punkt.Staly == false);
                if (celistnieje)
                {
                    row["__X__" + cel]  = rownKierCx * ro;
                    row["__Y__" + cel]  = rownKierCy * ro;
                    row2["__X__" + cel] = rownDlCx;
                    row2["__Y__" + cel] = rownDlCy;
                }

                bool stanowiskoistnieje =
                    database1DataSet.PunktyNieznane.Any(
                        punkt => punkt.Nazwa.Equals(stanowisko) && punkt.Staly == false);
                if (stanowiskoistnieje)
                {
                    row["__X__" + stanowisko]  = rownKierStX * ro;
                    row["__Y__" + stanowisko]  = rownKierStY * ro;
                    row["__K__" + stanowisko]  = -1;
                    row2["__X__" + stanowisko] = rownDlStx;
                    row2["__Y__" + stanowisko] = rownDlSty;
                }
                //if (stanowiskoistnieje || celistnieje)
                // {
                dt.Rows.Add(row);
                dt.Rows.Add(row2);
                //  }
                //lista macierz l
                lList.Add(azymut - Convert.ToDouble(rowtachimetr.ItemArray[3]));
                lList.Add(d - dlugosc);

                //lista macierz p
                pList.Add(1 / Math.Pow(bkier, 2));
                pList.Add(1 / Math.Pow(dlugosc, 2));
            }
            int             liczbakolumn  = dt.Columns.Count;
            int             liczbawierszy = dt.Rows.Count;
            Matrix <double> A             = Matrix <double> .Build.Dense(liczbawierszy, liczbakolumn);

            for (int i = 0; i < liczbawierszy; i++)
            {
                for (int j = 0; j < liczbakolumn; j++)
                {
                    if (dt.Rows[i][j] == DBNull.Value)
                    {
                        A[i, j] = 0;
                    }
                    else
                    {
                        A[i, j] = Convert.ToDouble(dt.Rows[i][j]);
                    }
                }
            }
            Matrix <double> L = Matrix <double> .Build.Dense(lList.Count, 1);

            for (int i = 0; i < lList.Count; i++)
            {
                L[i, 0] = lList[i];
            }
            var parray = pList.ToArray();
            // var P = Matrix<double>.Build;

            Matrix <double> P = new DiagonalMatrix(pList.Count, pList.Count, parray);

            FormOtworzRTB macierza = new FormOtworzRTB();

            macierza.Show();
            macierza.richTextBox1.AppendText(A.ToMatrixString());

            var AT          = A.Transpose();
            var ATP         = AT.Multiply(P);
            var ATPA        = ATP.Multiply(A);
            var ATPAinverse = ATPA.Inverse();
            var ATPL        = ATP.Multiply(L);
            var X           = ATPAinverse.Multiply(ATPL);
            var X2          = A.Solve(L);

            macierza.richTextBox1.AppendText("\r\nA\r\n" + X.ToMatrixString() + "\r\n");
            var xTab = X.ToArray();

            macierza.richTextBox1.AppendText("\r\nX\r\n" + X.ToString() + "\r\n");
            macierza.richTextBox1.AppendText("\r\nX2\r\n" + X2.ToString() + "\r\n");
            var ax  = A.Multiply(X);
            var axl = ax - L;

            macierza.richTextBox1.AppendText("\r\nAx+L\r\n" + axl.ToString() + "\r\n");

            macierza.richTextBox1.AppendText("\r\nP\r\n" + P.ToString() + "\r\n");

            int i2 = 0;

            foreach (DataRow row in database1DataSet.PunktyNieznane.Rows)
            {
                if (Convert.ToBoolean(row.ItemArray[2]) == true && Convert.ToBoolean(row.ItemArray[3]) == false)
                {
                    var nowywiersz = dt2.NewRow();
                    nowywiersz["Nazwa"] = row.ItemArray[1];
                    nowywiersz["X"]     = Convert.ToDouble(row.ItemArray[4]) + pList[i2];
                    i2++;
                    nowywiersz["Y"] = Convert.ToDouble(row.ItemArray[5]) + pList[i2];
                    i2 += 2;
                    nowywiersz["H"] = Convert.ToDouble(row.ItemArray[6]);
                    dt2.Rows.Add(nowywiersz);
                }
            }

            dataGridView2.DataSource = dt2;
            dataGridView2.Columns["Nazwa"].DefaultCellStyle.Format = "#";
        }