线性代数矩阵化简器
Fraction 类:
input.jsp:
calculate.jsp:
1
package joey;
2
3
public class Fraction
4
{
5
private int numerator;
6
private int denominator;
7
8
public Fraction(int numerator, int denominator) throws Exception
9
{
10
if (denominator == 0)
11
{
12
throw new Exception("New fraction's denominator can not be 0");
13
}
14
this.numerator = numerator;
15
this.denominator = denominator;
16
}
17
18
public static Fraction toFraction(double numerator) throws Exception
19
{
20
return toFraction(numerator, 1);
21
}
22
23
public static Fraction toFraction(double numerator, int denominator)
24
throws Exception
25
{
26
if (numerator != (int) numerator)
27
{
28
return toFraction(numerator * 10, denominator * 10);
29
}
30
else
31
{
32
Fraction fraction = new Fraction((int) numerator, denominator);
33
return fraction;
34
}
35
}
36
37
public static Fraction getReciprocal(Fraction fraction) throws Exception
38
{
39
if (fraction.getDenominator() == 0)
40
{
41
throw new Exception("This fraction's denominator can not be 0");
42
}
43
if (fraction.getNumerator() == 0)
44
{
45
throw new Exception("This fraction's numerator can not be 0");
46
}
47
Fraction one = new Fraction(1, 1);
48
return Fraction.divide(one, fraction);
49
}
50
51
public static Fraction multiply(Fraction first, Fraction second)
52
throws Exception
53
{
54
if (first.getDenominator() == 0)
55
{
56
throw new Exception("First fraction's denominator can not be 0");
57
}
58
if (second.getDenominator() == 0)
59
{
60
throw new Exception("Second fraction's denominator can not be 0");
61
}
62
Fraction fraction = new Fraction(first.getNumerator()
63
* second.getNumerator(), first.getDenominator()
64
* second.getDenominator());
65
fraction = Fraction.reduce(fraction);
66
return fraction;
67
}
68
69
public static Fraction divide(Fraction first, Fraction second)
70
throws Exception
71
{
72
if (first.getDenominator() == 0)
73
{
74
throw new Exception("First fraction's denominator can not be 0");
75
}
76
if (second.getDenominator() == 0)
77
{
78
throw new Exception("Second fraction's denominator can not be 0");
79
}
80
if (second.getNumerator() == 0)
81
{
82
throw new Exception("Second fraction's numerator can not be 0");
83
}
84
Fraction fraction = new Fraction(first.getNumerator()
85
* second.getDenominator(), first.getDenominator()
86
* second.getNumerator());
87
fraction = Fraction.reduce(fraction);
88
return fraction;
89
}
90
91
public static Fraction add(Fraction first, Fraction second)
92
throws Exception
93
{
94
if (first.getDenominator() == 0)
95
{
96
throw new Exception("First fraction's denominator can not be 0");
97
}
98
if (second.getDenominator() == 0)
99
{
100
throw new Exception("Second fraction's denominator can not be 0");
101
}
102
Fraction fraction = new Fraction(first.getNumerator()
103
* second.getDenominator() + first.getDenominator()
104
* second.getNumerator(), first.getDenominator()
105
* second.getDenominator());
106
fraction = Fraction.reduce(fraction);
107
return fraction;
108
}
109
110
public static Fraction subtract(Fraction first, Fraction second)
111
throws Exception
112
{
113
if (first.getDenominator() == 0)
114
{
115
throw new Exception("First fraction's denominator can not be 0");
116
}
117
if (second.getDenominator() == 0)
118
{
119
throw new Exception("Second fraction's denominator can not be 0");
120
}
121
Fraction fraction = new Fraction(first.getNumerator()
122
* second.getDenominator() - first.getDenominator()
123
* second.getNumerator(), first.getDenominator()
124
* second.getDenominator());
125
fraction = Fraction.reduce(fraction);
126
return fraction;
127
}
128
129
private static int getAbsoluteValue(int input)
130
{
131
if (input >= 0)
132
{
133
return input;
134
}
135
else
136
{
137
return -input;
138
}
139
}
140
141
private static Fraction reduce(Fraction fraction) throws Exception
142
{
143
if (fraction.getDenominator() == 0)
144
{
145
throw new Exception("This fraction's denominator can not be 0");
146
}
147
if (fraction.getDenominator() < 0)
148
{
149
fraction.setNumerator(-fraction.getNumerator());
150
fraction.setDenominator(-fraction.getDenominator());
151
return reduce(fraction);
152
}
153
if (getAbsoluteValue(fraction.getNumerator()) > getAbsoluteValue(fraction
154
.getDenominator()))
155
{
156
for (int i = 2; i <= getAbsoluteValue(fraction.getDenominator()); i++)
157
{
158
if (fraction.getNumerator() % i == 0
159
&& fraction.getDenominator() % i == 0)
160
{
161
fraction.setDenominator(fraction.getDenominator() / i);
162
fraction.setNumerator(fraction.getNumerator() / i);
163
return reduce(fraction);
164
}
165
}
166
return fraction;
167
}
168
else
169
{
170
for (int i = 2; i <= getAbsoluteValue(fraction.getNumerator()); i++)
171
{
172
if (fraction.getNumerator() % i == 0
173
&& fraction.getDenominator() % i == 0)
174
{
175
fraction.setDenominator(fraction.getDenominator() / i);
176
fraction.setNumerator(fraction.getNumerator() / i);
177
return reduce(fraction);
178
}
179
}
180
return fraction;
181
}
182
}
183
184
public int getDenominator()
185
{
186
return denominator;
187
}
188
189
public void setDenominator(int denominator)
190
{
191
this.denominator = denominator;
192
}
193
194
public int getNumerator()
195
{
196
return numerator;
197
}
198
199
public void setNumerator(int numerator)
200
{
201
this.numerator = numerator;
202
}
203
204
public String toString()
205
{
206
if (this.getNumerator() == 0)
207
{
208
return "0";
209
}
210
if (this.getDenominator() == 1)
211
{
212
return String.valueOf(this.getNumerator());
213
}
214
else if (this.getDenominator() == -1)
215
{
216
return String.valueOf(-this.getNumerator());
217
}
218
else
219
{
220
return this.getNumerator() + "/" + this.getDenominator();
221
}
222
}
223
}
224
Gauss_JordanElimination 类:
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

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

1
package joey;
2
3
import java.util.*;
4
5
public class Gauss_JordanElimination
6
{
7
private ArrayList<ArrayList<Fraction>> table;
8
private int columns;
9
10
public Gauss_JordanElimination(ArrayList<ArrayList<Fraction>> table)
11
throws Exception
12
{
13
this.table = table;
14
this.columns = this.table.get(0).size();
15
}
16
17
public void reduceTheMatrix(int dealingColumn, int dealingRow)
18
throws Exception
19
{
20
if (dealingColumn <= 0 || dealingRow <= 0)
21
{
22
return;
23
}
24
if (this.table.get(dealingRow).get(dealingColumn).getNumerator() == 0)
25
{
26
reduceTheMatrix(dealingColumn - 1, dealingRow);
27
}
28
else if(this.table.get(dealingRow).get(dealingColumn).getNumerator() == 1&&this.table.get(dealingRow).get(dealingColumn).getDenominator()==1)
29
{
30
getLeadingZeros(dealingRow, dealingColumn);
31
reduceTheMatrix(dealingColumn - 1, dealingRow - 1);
32
}
33
else
34
{
35
reduceTheMatrix(dealingColumn - 1, dealingRow);
36
}
37
}
38
39
private void getLeadingZeros(int dealingRow, int dealingColumn)
40
throws Exception
41
{
42
ArrayList<Fraction> al;
43
for (int row = dealingRow - 1; row >= 0; row--)
44
{
45
if (this.table.get(row).get(dealingColumn).getNumerator() != 0)
46
{
47
al = new ArrayList<Fraction>();
48
for (int i = 0; i < this.table.get(dealingRow).size(); i++)
49
{
50
Fraction fraction = new Fraction(1, 1);
51
fraction.setDenominator(this.table.get(dealingRow).get(i)
52
.getDenominator());
53
fraction.setNumerator(this.table.get(dealingRow).get(i)
54
.getNumerator());
55
al.add(fraction);
56
}
57
multiplyTempRowByOneFraction(al, this.table.get(row).get(
58
dealingColumn));
59
subtractOneRowByTempRow(row, al);
60
}
61
}
62
}
63
64
private void subtractOneRowByTempRow(int row, ArrayList<Fraction> al)
65
throws Exception
66
{
67
for (int column = 0; column < columns; column++)
68
{
69
this.table.get(row).set(
70
column,
71
Fraction.subtract(this.table.get(row).get(column), al
72
.get(column)));
73
}
74
}
75
76
private void multiplyTempRowByOneFraction(ArrayList<Fraction> al,
77
Fraction fraction) throws Exception
78
{
79
for (int column = 0; column < al.size(); column++)
80
{
81
al.set(column, Fraction.multiply(al.get(column), fraction));
82
}
83
}
84
}
85

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

GaussianElimination 类:
1
package joey;
2
3
import java.util.*;
4
5
public class GaussianElimination
6
{
7
private ArrayList<ArrayList<Fraction>> table;
8
private int columns;
9
private int rows;
10
private int dealingColumn;
11
private int dealingRow;
12
13
public GaussianElimination(ArrayList<ArrayList<Fraction>> table)
14
throws Exception
15
{
16
this.table = table;
17
this.columns = this.table.get(0).size();
18
this.rows = this.table.size();
19
}
20
21
public int getDealingColumn()
22
{
23
return dealingColumn;
24
}
25
26
public int getDealingRow()
27
{
28
return dealingRow;
29
}
30
31
public void reduceTheMatrix() throws Exception
32
{
33
reduceTheMatrix(0, 0);
34
}
35
36
private void reduceTheMatrix(int dealingColumn, int dealingRow)
37
throws Exception
38
{
39
if (dealingColumn >= columns || dealingRow >= rows)
40
{
41
this.dealingColumn = dealingColumn - 1;
42
this.dealingRow = dealingRow - 1;
43
return;
44
}
45
int row;
46
for (row = dealingRow; row < rows; row++)
47
{
48
if (this.table.get(row).get(dealingColumn).getNumerator() != 0)
49
{
50
getLeadingOne(dealingRow, dealingColumn, row);
51
break;
52
}
53
}
54
if (row == rows)
55
{
56
reduceTheMatrix(dealingColumn + 1, dealingRow);
57
}
58
else
59
{
60
getLeadingZeros(dealingRow, dealingColumn);
61
reduceTheMatrix(dealingColumn + 1, dealingRow + 1);
62
}
63
}
64
65
private void getLeadingOne(int dealingRow, int dealingColumn,
66
int noLeadingZeroRow) throws Exception
67
{
68
if (dealingRow != noLeadingZeroRow)
69
{
70
swapTwoRows(dealingRow, noLeadingZeroRow);
71
}
72
multiplyOneRowByOneFraction(dealingRow, Fraction
73
.getReciprocal(this.table.get(dealingRow).get(dealingColumn)));
74
}
75
76
private void getLeadingZeros(int dealingRow, int dealingColumn)
77
throws Exception
78
{
79
for (int row = dealingRow + 1; row < rows; row++)
80
{
81
if (this.table.get(row).get(dealingColumn).getNumerator() != 0)
82
{
83
multiplyOneRowByOneFraction(row, Fraction
84
.getReciprocal(this.table.get(row).get(dealingColumn)));
85
subtractOneRowByAnotherRow(row, dealingRow);
86
}
87
}
88
}
89
90
private void subtractOneRowByAnotherRow(int rowOne, int rowTwo)
91
throws Exception
92
{
93
for (int column = 0; column < columns; column++)
94
{
95
this.table.get(rowOne).set(
96
column,
97
Fraction.subtract(this.table.get(rowOne).get(column),
98
this.table.get(rowTwo).get(column)));
99
}
100
}
101
102
private void multiplyOneRowByOneFraction(int row, Fraction fraction)
103
throws Exception
104
{
105
for (int column = 0; column < columns; column++)
106
{
107
this.table.get(row)
108
.set(
109
column,
110
Fraction.multiply(this.table.get(row).get(column),
111
fraction));
112
}
113
}
114
115
private void swapTwoRows(int rowOne, int rowTwo) throws Exception
116
{
117
Fraction temp;
118
for (int column = 0; column < columns; column++)
119
{
120
temp = this.table.get(rowOne).get(column);
121
this.table.get(rowOne).set(column,
122
this.table.get(rowTwo).get(column));
123
this.table.get(rowTwo).set(column, temp);
124
}
125
}
126
}
127

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

Matrix 类:
1
package joey;
2
3
import java.util.*;
4
5
public class Matrix
6
{
7
private ArrayList<ArrayList<Fraction>> table;
8
private int columns;
9
private int rows;
10
11
public Matrix(ArrayList<ArrayList<Fraction>> table) throws Exception
12
{
13
this.table = table;
14
this.columns = this.table.get(0).size();
15
this.rows = this.table.size();
16
}
17
18
public void printOut()
19
{
20
for (int row = 0; row < rows; row++)
21
{
22
StringBuilder sb = new StringBuilder();
23
for (int column = 0; column < columns; column++)
24
{
25
sb.append(leftAdjusting(this.table.get(row).get(column)
26
.toString(), 8));
27
}
28
System.out.println(sb.toString());
29
}
30
}
31
32
private String leftAdjusting(String input, int length)
33
{
34
try
35
{
36
return input.substring(0, length);
37
}
38
catch (Exception e)
39
{
40
StringBuilder sb = new StringBuilder();
41
for (int i = 0; i < length - input.length(); i++)
42
{
43
sb.append(" ");
44
}
45
return input + sb.toString();
46
}
47
}
48
49
public void reduceTheMatrix() throws Exception
50
{
51
GaussianElimination gaussian = new GaussianElimination(this.table);
52
gaussian.reduceTheMatrix();
53
Gauss_JordanElimination gauss_Jordan = new Gauss_JordanElimination(
54
this.table);
55
gauss_Jordan.reduceTheMatrix(gaussian.getDealingColumn(), gaussian
56
.getDealingRow());
57
}
58
}
59

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

input.jsp:
1
<%@ page language="java" contentType="text/html; charset=utf8"%>
2
<%@ page import="java.util.*"%>
3
<%@ page import="Joey.*"%>
4
<%
5
String content="";
6
int row=0;
7
int column=0;
8
if(request.getQueryString()!=null)
9
{
10
String[] array=request.getQueryString().split(";");
11
row = Integer.parseInt(array[0].split("=")[1]);
12
column = Integer.parseInt(array[1].split("=")[1]);
13
StringBuilder sb = new StringBuilder();
14
for(int i =0; i<row;i++)
15
{
16
sb.append("<tr>");
17
for(int j=0;j<column;j++)
18
{
19
if(j==column-1)
20
{
21
sb.append("<td>");
22
sb.append("=<input name=\"TextBox\" type=\"text\" id=\"TextBox\"/>");
23
sb.append("</td>");
24
}
25
else if(j!=column-2)
26
{
27
sb.append("<td>");
28
sb.append("<input name=\"TextBox\" type=\"text\" id=\"TextBox\"/>X");
29
sb.append(j+1);
30
sb.append(" + ");
31
sb.append("</td>");
32
}
33
else
34
{
35
sb.append("<td>");
36
sb.append("<input name=\"TextBox\" type=\"text\" id=\"TextBox\"/>X");
37
sb.append(j+1);
38
sb.append(" ");
39
sb.append("</td>");
40
}
41
}
42
sb.append("</tr>");
43
}
44
content=sb.toString();
45
}
46
%>

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

1
<form action="calculate.jsp" method=post>
2
<table>
3
<%=content%>
4
<input name="Row" type="hidden" id="Row" value="<%=row%>"/>
5
<input name="Column" type="hidden" id="Column" value="<%=column%>"/>
6
</table>
7
<input type="submit" value="Submit"> <input type="reset"
8
value="Reset">
9
</form>

2

3

4

5

6

7

8

9

calculate.jsp:
1
<%@ page language="java" contentType="text/html; charset=utf8"%>
2
<%@ page import="java.util.*"%>
3
<%@ page import="joey.*"%>
4
<%
5
String[] array=request.getParameterValues("TextBox");
6
ArrayList<ArrayList<Fraction>> table=new ArrayList<ArrayList<Fraction>>();
7
ArrayList<Fraction> al;
8
int row = Integer.parseInt(request.getParameter("Row"));
9
int column = Integer.parseInt(request.getParameter("Column"));
10
int pointer = 0;
11
for(int i=0;i<row;i++)
12
{
13
al=new ArrayList<Fraction>();
14
for(int j=0;j<column;j++)
15
{
16
al.add(Fraction.toFraction(Double.parseDouble(array[pointer])));
17
pointer++;
18
}
19
table.add(al);
20
}
21
try
22
{
23
Matrix matrix = new Matrix(table);
24
matrix.reduceTheMatrix();
25
}
26
catch(Exception e)
27
{
28
}
29
String content="";
30
StringBuilder sb = new StringBuilder();
31
for(int i =0; i<row;i++)
32
{
33
sb.append("<tr>");
34
for(int j=0;j<column;j++)
35
{
36
if(j==column-1)
37
{
38
sb.append("<td>");
39
sb.append("= "+table.get(i).get(j).toString());
40
sb.append("</td>");
41
}
42
else if(j!=column-2)
43
{
44
sb.append("<td>");
45
sb.append(table.get(i).get(j).toString()+" X");
46
sb.append(j+1);
47
sb.append(" + ");
48
sb.append("</td>");
49
}
50
else
51
{
52
sb.append("<td>");
53
sb.append(table.get(i).get(j).toString()+" X");
54
sb.append(j+1);
55
sb.append(" ");
56
sb.append("</td>");
57
}
58
}
59
sb.append("</tr>");
60
}
61
content=sb.toString();
62
%>

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

1
<table>
2
<%=content%>
3
</table>

2

3
