標題:n階矩陣相乘
C# (MainWindow.xaml.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace matrix_multiplied
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public int ma = 0;
public int na = 0;
public int mb = 0;
public int nb = 0;
public MainWindow()
{
InitializeComponent();
}
public void CreateMatrix()
{
int m = 0, n = 0;
//Clear virtual Matrix
this.MatrixGrid.Children.Clear();
for (m = 0; m < ma; m++)
for (n = 0; n < na; n++)
this.UnregisterName("MA_" + m.ToString() + "_" + n.ToString());
for (m = 0; m < mb; m++)
for (n = 0; n < nb; n++)
this.UnregisterName("MB_" + m.ToString() + "_" + n.ToString());
//Create Matrix A B Textbox
ma = Convert.ToInt32(mA.Text);
na = Convert.ToInt32(nA.Text);
mb = Convert.ToInt32(mB.Text);
nb = Convert.ToInt32(nB.Text);
if (na != mb) //Check Matrix
{
MessageBox.Show("Can't do the Multiplication!!");
}
else
{
//Create Matrix A
for (m = 0; m < ma; m++)
{
for (n = 0; n < na; n++)
{
TextBox newtbx = new TextBox();
newtbx.Name = "MA_" + m.ToString() + "_" + n.ToString();
newtbx.Text = "1.0";
newtbx.VerticalAlignment = VerticalAlignment.Top;
newtbx.HorizontalAlignment = HorizontalAlignment.Left;
newtbx.Margin = new Thickness((10 + 40 * n), (10 + 40 * m), 0, 0);
newtbx.Width = 30;
newtbx.Height = 30;
this.MatrixGrid.Children.Add(newtbx);
this.RegisterName(newtbx.Name, newtbx);
}
}
//Add multiply symbol
TextBlock newtbk = new TextBlock();
newtbk.Name = "Multiply";
newtbk.Text = "×";
newtbk.Margin = new Thickness((15 + 40 * na), (20 * ma), 0, 0);
this.MatrixGrid.Children.Add(newtbk);
//Create Matrix B
for (m = 0; m < mb; m++)
{
for (n = 0; n < nb; n++)
{
TextBox newtbx = new TextBox();
newtbx.Name = "MB_" + m.ToString() + "_" + n.ToString();
newtbx.Text = "1.0";
newtbx.VerticalAlignment = VerticalAlignment.Top;
newtbx.HorizontalAlignment = HorizontalAlignment.Left;
newtbx.Margin = new Thickness((40 + 40 * (n + na)), (10 + 40 * m), 0, 0);
newtbx.Width = 30;
newtbx.Height = 30;
this.MatrixGrid.Children.Add(newtbx);
this.RegisterName(newtbx.Name, newtbx);
}
}
}
}
public void DetermineMatrix()
{
int m = 0, n = 0, i=0;
Double sum = 0;
Double[,] MaArray = new Double[ma, na];
Double[,] MbArray = new Double[mb, nb];
Double[,] McArray = new Double[ma, nb];
//Get Number
for (m = 0; m < ma; m++)
{
for (n = 0; n < na; n++)
{
TextBox MA = (TextBox)MatrixGrid.FindName("MA_" + m.ToString() + "_" + n.ToString());
MaArray[m, n] = (Double)Convert.ToDouble(MA.Text);
}
}
for (m = 0; m < mb; m++)
{
for (n = 0; n < nb; n++)
{
TextBox MB = (TextBox)MatrixGrid.FindName("MB_" + m.ToString() + "_" + n.ToString());
MbArray[m, n] = (Double)Convert.ToDouble(MB.Text);
}
}
//Determine Matrix
for (m = 0; m < ma; m++)
{
for (n = 0; n < nb; n++)
{
sum = 0.0;
for (i = 0; i < mb; i++)
{
sum += MaArray[m,i] * MbArray[i,n];
}
McArray[m,n] = sum;
}
}
//Add equal symbol
TextBlock newtbk = new TextBlock();
newtbk.Name = "Anser";
newtbk.Text = "=";
newtbk.Margin = new Thickness((50 + 40 * (na + nb)), (20 * ma), 0, 0);
this.MatrixGrid.Children.Add(newtbk);
//Create Matrix C
for (m = 0; m < ma; m++)
{
for (n = 0; n < nb; n++)
{
sum = 0.0;
for (i = 0; i < mb; i++)
{
TextBox newtbx = new TextBox();
newtbx.Name = "MC" + m.ToString() + "_" + n.ToString();
newtbx.Text = Convert.ToString(McArray[m,n]);
newtbx.VerticalAlignment = VerticalAlignment.Top;
newtbx.HorizontalAlignment = HorizontalAlignment.Left;
newtbx.Margin = new Thickness((70 + 40 * (n + na + nb)), (10 + 40 * m), 0, 0);
newtbx.Width = 30;
newtbx.Height = 30;
this.MatrixGrid.Children.Add(newtbx);
}
}
}
}
private void CreateMatrix_Click(object sender, RoutedEventArgs e)
{
CreateMatrix();
}
private void DetermineMatrix_Click(object sender, RoutedEventArgs e)
{
DetermineMatrix();
}
}
}
WPF (MainWindow.xaml):
<Window x:Class="matrix_multiplied.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="600">
<Grid>
<TextBlock Height="30" HorizontalAlignment="Left" Margin="20,20,0,0" Name="MatrixATitle" Text="Matrix A" VerticalAlignment="Top" Width="122" TextAlignment="Left" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="20,50,0,0" Name="textBlock1" Text="m:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="90,50,0,0" Name="textBlock2" Text="n:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
<TextBox Height="30" Margin="40,50,0,0" Name="mA" VerticalAlignment="Top" Text="4" HorizontalAlignment="Left" Width="30" />
<TextBox Height="30" Margin="110,50,0,0" Name="nA" VerticalAlignment="Top" Text="4" HorizontalAlignment="Left" Width="30" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="180,20,0,0" Name="MatrixBTitle" Text="Matrix B" VerticalAlignment="Top" Width="122" TextAlignment="Left" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="180,50,0,0" Name="textBlock3" Text="m:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="250,50,0,0" Name="textBlock4" Text="n:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
<TextBox Height="30" Margin="200,50,0,0" Name="mB" VerticalAlignment="Top" Text="4" HorizontalAlignment="Left" Width="30" />
<TextBox Height="30" Margin="270,50,0,0" Name="nB" VerticalAlignment="Top" Text="1" HorizontalAlignment="Left" Width="30" />
<Button Content="Create Matrix" Height="30" Margin="340,50,0,0" Name="CreateMatrixBtn" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" Click="CreateMatrix_Click" />
<Button Content="Determine" Height="30" Margin="460,50,0,0" Name="DetermineMatrixBtn" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" Click="DetermineMatrix_Click" />
<Grid Margin="10,100,10,10" Name="MatrixGrid" HorizontalAlignment="Stretch">
</Grid>
</Grid>
</Window>
結果:
說明:
使用WPF撰寫計算n階矩陣相乘印出結果。
首先按下Create Matrix按鈕虛擬建立需要的TextBox陣列數量,
再按下Determine去擷取經過RegisterName後的虛擬TextBox中的值,
並計算出矩陣相乘的結果後虛擬印出。