Show toolbar

2012年10月12日 星期五

Matrix Multiplied

標題:n階矩陣相乘
C# (MainWindow.xaml.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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中的值,
並計算出矩陣相乘的結果後虛擬印出。