소스 검색

Доделать перестановку строк и обратную матрицу + диаграмму + немного дизайна

Никита Кашлач 3 년 전
부모
커밋
d64c71370e
3개의 변경된 파일186개의 추가작업 그리고 23개의 파일을 삭제
  1. 28 2
      MatrixXAMLOnly/MainWindow.xaml
  2. 83 19
      MatrixXAMLOnly/MainWindow.xaml.cs
  3. 75 2
      MatrixXAMLOnly/Matrix.cs

+ 28 - 2
MatrixXAMLOnly/MainWindow.xaml

@@ -489,7 +489,7 @@
                     <Grid>
                         <Grid.ColumnDefinitions>
                             <ColumnDefinition/>
-                            <ColumnDefinition Width="0.1*"/>
+                            <ColumnDefinition Width="0.2*"/>
                             <ColumnDefinition/>
                         </Grid.ColumnDefinitions>
 
@@ -497,7 +497,7 @@
 
                         </Grid>
 
-                        <TextBlock Text="=>" Grid.Column="1" Style="{StaticResource OperationSign}"/>
+                        <TextBlock Text="*=>" Grid.Column="1" Style="{StaticResource OperationSign}"/>
 
                         <Grid x:Name="GridMultiplicationOnTransposedResult" Grid.Column="2">
 
@@ -534,6 +534,25 @@
                     <ColumnDefinition/>
                 </Grid.ColumnDefinitions>
 
+                <Grid Grid.Column="1">
+                    <Grid.ColumnDefinitions>
+                        <ColumnDefinition/>
+                        <ColumnDefinition/>
+                        <ColumnDefinition/>
+                    </Grid.ColumnDefinitions>
+
+                    <StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
+                        <TextBlock Text="Количество строк: "/>
+                        <TextBox x:Name="TextBoxRowReplaceRowCount" Style="{StaticResource NonMatrix}" MaxLength="1"/>
+                    </StackPanel>
+
+                    <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
+                        <TextBlock Text="Количество столбцов: "/>
+                        <TextBox x:Name="TextBoxRowReplaceColumnCount" Style="{StaticResource NonMatrix}" MaxLength="1"/>
+                    </StackPanel>
+
+                    <Button x:Name="BtnRowReplaceCreateMatrix" Grid.Column="2" Content="Построить матрицу" Click="BtnRowReplaceCreateMatrix_Click"/>
+                </Grid>
             </Grid>
 
             <!-- MidlleBody -->
@@ -574,7 +593,14 @@
 
             <!-- BottomBody -->
             <Grid Grid.Row="2">
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition Width="2*"/>
+                    <ColumnDefinition/>
+                    <ColumnDefinition/>
+                </Grid.ColumnDefinitions>
 
+                <Button x:Name="BtnRowsReplaceDiagramm" Grid.Column="1" Content="Диаграмма"/>
+                <Button x:Name="BtnRowsReplaceCalculate" Grid.Column="2" Content="Вычислить" Click="BtnRowsReplaceCalculate_Click"/>
             </Grid>
         </Grid> <!-- RowsReplace -->
         

+ 83 - 19
MatrixXAMLOnly/MainWindow.xaml.cs

@@ -30,8 +30,6 @@ namespace MatrixXAMLOnly
             BtnRowsReplace.Content = "Перестановка строк";
             BtnRowsReplaceOnTranspositionVector.Content = "Перестановка строк согласно \nвектору транспозиции";
             BtnInverse.Content = "Обратная матрицы";
-
-
         }
 
         private void BtnHamburgerMenu_Click(object sender, RoutedEventArgs e)
@@ -248,6 +246,9 @@ namespace MatrixXAMLOnly
             GridContentTranspose.Visibility = Visibility.Collapsed;
             // GridContentRowReplaceOnTranspositionVector;
             // GridContentInverse;
+
+            GridMultiplicationOnTransposedTerm.Children.Add(CreateMatrix(5, 5, false));
+            GridMultiplicationOnTransposedResult.Children.Add(CreateMatrix(5, 5, true));
         }
 
         private void BtnRowsReplace_Click(object sender, RoutedEventArgs e)
@@ -261,8 +262,8 @@ namespace MatrixXAMLOnly
             // GridContentRowReplaceOnTranspositionVector;
             // GridContentInverse;
 
-            GridTransposeFrom.Children.Add(CreateMatrix(3, 5, false));
-            GridTransposed.Children.Add(CreateMatrix(5, 3, true));
+            GridRowsReplaceMatrix.Children.Add(CreateMatrix(3, 5, false));
+            GridRowsReplaceResult.Children.Add(CreateMatrix(5, 3, true));
         }
 
         private void BtnInverse_Click(object sender, RoutedEventArgs e)
@@ -512,8 +513,8 @@ namespace MatrixXAMLOnly
 
         private void BtnTranspositionCalculate_Click(object sender, RoutedEventArgs e)
         {
-            int rowCount = GridTransposeFrom.RowDefinitions.Count;
-            int columnCount = GridTransposeFrom.ColumnDefinitions.Count;
+            int rowCount = ((Grid)GridTransposeFrom.Children[0]).RowDefinitions.Count;
+            int columnCount = ((Grid)GridTransposeFrom.Children[0]).ColumnDefinitions.Count;
 
             double[,] frstTerm = new double[rowCount, columnCount];
 
@@ -532,12 +533,14 @@ namespace MatrixXAMLOnly
                 }
             }
 
-            double[,] result = new double[columnCount, rowCount];
-            for (int i = 0; i < columnCount; i++)
+            MyMatrix result = MyMatrix.Transpose(new MyMatrix(frstTerm));
+            for (int i = 0; i < result.Rows; i++)
             {
-                for (int j = 0; j < rowCount; j++)
+                for (int j = 0; j < result.Columns; j++)
                 {
-                    ((TextBox)((Grid)GridTransposed.Children[0]).Children[(i * rowCount) + j]).Text = result[i, j].ToString();
+                    Grid matrixGrid = (Grid)GridTransposed.Children[0];
+                    TextBox tb = (TextBox)matrixGrid.Children[(i * result.Columns) + j];
+                    tb.Text = result.data[i, j].ToString();
                 }
             }
         }
@@ -562,14 +565,13 @@ namespace MatrixXAMLOnly
             GridMultiplicationOnTransposedResult.Children.Clear();
 
             GridMultiplicationOnTransposedTerm.Children.Add(CreateMatrix(rowCount, columnCount, false));
-            GridMultiplicationOnTransposedResult.Children.Add(CreateMatrix(columnCount, rowCount, true));
+            GridMultiplicationOnTransposedResult.Children.Add(CreateMatrix(rowCount, rowCount, true));
         }
 
         private void BtnMultiplicationOnTransposedCalculate_Click(object sender, RoutedEventArgs e)
         {
-            // TODO: отредачить всё по феншую, тут траблы одновременно и с транспонирование и с умножение матрицы на матрицу (нужно с размера угадать)
-            int rowCount = GridMultiplicationOnTransposedTerm.RowDefinitions.Count;
-            int columnCount = GridTransposeFrom.ColumnDefinitions.Count;
+            int rowCount = ((Grid)GridMultiplicationOnTransposedTerm.Children[0]).RowDefinitions.Count;
+            int columnCount = ((Grid)GridMultiplicationOnTransposedTerm.Children[0]).ColumnDefinitions.Count;
 
             double[,] frstTerm = new double[rowCount, columnCount];
 
@@ -579,21 +581,83 @@ namespace MatrixXAMLOnly
                 {
                     try
                     {
-                        frstTerm[i, j] = Convert.ToDouble(((Grid)GridTransposeFrom.Children[0]).Children.Cast<TextBox>().First(e => Grid.GetColumn(e) == j && Grid.GetRow(e) == i).Text);
+                        frstTerm[i, j] = Convert.ToDouble(((Grid)GridMultiplicationOnTransposedTerm.Children[0]).Children.Cast<TextBox>().First(e => Grid.GetColumn(e) == j && Grid.GetRow(e) == i).Text);
                     }
                     catch
                     {
                         MessageBox.Show("Матрица указана некорректно!");
+                        return;
                     }
                 }
             }
 
-            double[,] result = new double[columnCount, rowCount];
-            for (int i = 0; i < columnCount; i++)
+            MyMatrix matrixFrst = new MyMatrix(frstTerm);
+            MyMatrix matrixTransposed = MyMatrix.Transpose(matrixFrst);
+
+            MyMatrix result = matrixFrst * matrixTransposed;
+            for (int i = 0; i < result.Rows; i++)
             {
-                for (int j = 0; j < rowCount; j++)
+                for (int j = 0; j < result.Columns; j++)
                 {
-                    ((TextBox)((Grid)GridTransposed.Children[0]).Children[(i * rowCount) + j]).Text = result[i, j].ToString();
+                    ((TextBox)((Grid)GridMultiplicationOnTransposedResult.Children[0]).Children[(i * result.Columns) + j]).Text = result.data[i, j].ToString();
+                }
+            }
+        }
+        #endregion
+
+        #region RowReplace
+        private void BtnRowReplaceCreateMatrix_Click(object sender, RoutedEventArgs e)
+        {
+            int rowCount = 0;
+            int columnCount = 0;
+            try
+            {
+                rowCount = Convert.ToInt32(TextBoxRowReplaceRowCount.Text);
+                columnCount = Convert.ToInt32(TextBoxRowReplaceColumnCount.Text);
+            }
+            catch
+            {
+                MessageBox.Show("Количество строк/столбцов указано некорректно!");
+            }
+
+            GridRowsReplaceMatrix.Children.Clear();
+            GridRowsReplaceResult.Children.Clear();
+
+            GridRowsReplaceMatrix.Children.Add(CreateMatrix(rowCount, columnCount, false));
+            GridRowsReplaceResult.Children.Add(CreateMatrix(rowCount, columnCount, true));
+        }
+
+        private void BtnRowsReplaceCalculate_Click(object sender, RoutedEventArgs e)
+        {
+            int rowCount = ((Grid)GridRowsReplaceMatrix.Children[0]).RowDefinitions.Count;
+            int columnCount = ((Grid)GridRowsReplaceMatrix.Children[0]).ColumnDefinitions.Count;
+
+            double[,] frstTerm = new double[rowCount, columnCount];
+
+            for (int i = 0; i < rowCount; i++)
+            {
+                for (int j = 0; j < columnCount; j++)
+                {
+                    try
+                    {
+                        frstTerm[i, j] = Convert.ToDouble(((Grid)GridRowsReplaceMatrix.Children[0]).Children.Cast<TextBox>().First(e => Grid.GetColumn(e) == j && Grid.GetRow(e) == i).Text);
+                    }
+                    catch
+                    {
+                        MessageBox.Show("Матрица указана некорректно!");
+                        return;
+                    }
+                }
+            }
+
+            MyMatrix matrixFrstTerm = new MyMatrix(frstTerm);
+
+            MyMatrix result = MyMatrix.RowReplace(matrixFrstTerm, Convert.ToInt32(TextBoxRowReplaceFrstTerm.Text), Convert.ToInt32(TextBoxRowReplaceScndTerm.Text));
+            for (int i = 0; i < rowCount; i++)
+            {
+                for (int j = 0; j < columnCount; j++)
+                {
+                    ((TextBox)((Grid)GridDifferenceResult.Children[0]).Children[(i * columnCount) + j]).Text = result.data[i, j].ToString();
                 }
             }
         }

+ 75 - 2
MatrixXAMLOnly/Matrix.cs

@@ -7,7 +7,7 @@ using System.Windows;
 
 namespace Matrix
 {
-    class MyMatrix
+    class MyMatrix : Object
     {
         public double[,] data; // 
 
@@ -70,9 +70,34 @@ namespace Matrix
             }
         }
 
-        public static MyMatrix operator *(MyMatrix matrix1, MyMatrix matrix2)
+        public static MyMatrix operator *(MyMatrix matrixA, MyMatrix matrixB)
         {
+            if (matrixA.Rows != matrixB.Columns)
+            {
+                throw new Exception("Умножение матриц A и B возможно только в том случае, когда число столбцов матрицы A совпадает с числом строк в матрице B");
+            }
+
+
+            int K = matrixA.Columns;
+            int M = matrixA.Rows;
+            int N = matrixB.Columns;
+            double[,] array = new double[M, N];
+
+            for (int i = 0; i < M; i++)
+            {
+                for (int j = 0; j < N; j++)
+                {
+                    double num = 0;
+                    for (int k = 0; k < K; k++)
+                    {
+                        num += matrixA.data[i, k] * matrixB.data[k, j];
+                    }
+
+                    array[i, j] = num;
+                }
+            }
 
+            return new MyMatrix(array);
         }
 
         public static MyMatrix operator *(MyMatrix matrix1, double num)
@@ -95,6 +120,54 @@ namespace Matrix
             return matrix1 * num;
         }
 
+        public static MyMatrix Transpose(MyMatrix matrix)
+        {
+            double[,] array = new double[matrix.Columns, matrix.Rows];
+
+            for (int i = 0; i < matrix.Rows; i++)
+            {
+                for (int j = 0; j < matrix.Columns; j++)
+                {
+                    array[j, i] = matrix.data[i, j];
+                }
+            }
+
+            return new MyMatrix(array);
+        }
+
+        public static MyMatrix RowReplace(MyMatrix matrix, int frstIndex, int scndIndex)
+        {
+            double[,] array = matrix.data;
+            double[] boof = new double[matrix.Columns];
+
+            for (int i = 0; i < matrix.Columns; i++)
+            {
+                boof[i] = matrix.data[frstIndex, i];
+            }
+
+            for (int i = 0; i < matrix.Rows; i++)
+            {
+                for (int j = 0; j < matrix.Columns; j++)
+                {
+                    if (i != frstIndex)
+                    {
+                        array[i, j] = matrix.data[i, j];
+                    }
+                    else
+                    {
+                        array[i, j] = matrix.data[scndIndex, j];
+                    }
+                }
+            }
+
+            for (int i = 0; i < matrix.Columns; i++)
+            {
+                array[scndIndex, i] = boof[i];
+            }
+
+            return new MyMatrix(array);
+        }
+
         public override string ToString()
         {
             StringBuilder sb = new StringBuilder("", Rows * Columns * 2);