-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
318 lines (277 loc) · 9.35 KB
/
Program.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright 2024 Himanshu Bansal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
* ==============================================================================
*/
using Linear_Equation_Solver_High_Precision_;
using System.Buffers.Text;
GaussEliminationClass test = new GaussEliminationClass();
//Fraction[,] array = {
// { new Fraction(2,1), new Fraction(0,1), new Fraction(-2,1), new Fraction(0,1) },
// { new Fraction(0,1), new Fraction(2,1), new Fraction(-1,1), new Fraction(0,1) }
// };
Fraction[,] array = test.TakeInput();
array = test.EndResult(array);
if (array.GetLength(0) == 0)
{
Console.WriteLine("\nEvery variable is allowed to have any value");
Environment.Exit(0);
}
var result = test.clean_coloumn(array);
array = result.Item1;
List<int> preserved_columns = result.Item2;
List<int> zero_columns = result.Item3;
//foreach(int i in preserved_columns)
//{
// Console.WriteLine(i);
//}
//test.print_matrix(array);
//Environment.Exit(0);
Dictionary<int,Fraction> answer = new Dictionary<int,Fraction>();
Dictionary<int, int> keepTrackColumn = new Dictionary<int, int>();
for (int j = 0; j < array.GetLength(1) - 1; j++)
{
keepTrackColumn.Add(j, j);
}
Fraction[,] array2;
do
{
if (array.GetLength(1) <= 2)
{
break;
}
if (test.invalid_check(array)== "invalid")
{
throw new Exception("Not solvable");
}
array2 = new Fraction[array.GetLength(0) - 1, array.GetLength(1) - 1];
bool available_fixed_value = false;
int non_zero_index_main = -1;
int non_zero_count = 0;
List<int> non_zero_index = new List<int>();
//part 1
{
for (int j = 0; j < array.GetLength(1) - 1; j++) //exluding constant as it can be zero like x=0
{
if (array[array.GetLength(0) - 1, j] != 0)
{
non_zero_count++;
non_zero_index.Add(j);
}
}
if (non_zero_count == 1)
{
available_fixed_value = true;
non_zero_index_main = non_zero_index[0];
}
}
//Fraction[,] array2 = new Fraction[array.GetLength(0) - 1, array.GetLength(1) - 1];
if (available_fixed_value)
{
Fraction base1 = -1 * array[array.GetLength(0) - 1, array.GetLength(1) - 1] / array[array.GetLength(0) - 1, non_zero_index_main];
answer.Add(keepTrackColumn[non_zero_index_main], base1);
Miscs.ModifyKey(keepTrackColumn, non_zero_index_main, -1);
Dictionary<int, int> modifiedkeepTrackColumn = new Dictionary<int, int>();
foreach (KeyValuePair<int, int> item in keepTrackColumn)
{
if (item.Key > non_zero_index_main)
{
modifiedkeepTrackColumn.Add(item.Key - 1, item.Value);
}
else
{
modifiedkeepTrackColumn.Add(item.Key, item.Value);
}
}
keepTrackColumn = modifiedkeepTrackColumn;
for (int i = 0; i < array.GetLength(0) - 1; i++)
{
for (int j = 0; j < array.GetLength(1) - 1; j++)
{
if (j == non_zero_index_main)
{
array2[i, array.GetLength(1) - 2] = array[i, array.GetLength(1) - 1] + base1 * array[i, j];
}
else if (j > non_zero_index_main)
{
array2[i, j] = array[i, j + 1];
}
else if (j < non_zero_index_main)
{
array2[i, j] = array[i, j];
}
//array2[i, non_zero_index_main]
}
}
}
else if (array.GetLength(1) - 1 > array.GetLength(0))
{
List<int> non_zero_in_last_row_and_non_zero_only_once_in_its_column = new List<int>();
for (int j = 0; j < non_zero_count; j++)
{
bool applicable = true;
for (int i = 0; i < array.GetLength(0) - 1; i++)
{
if (array[i, non_zero_index[j]] != 0)
{
applicable = false;
}
}
if (applicable)
{
non_zero_in_last_row_and_non_zero_only_once_in_its_column.Add(non_zero_index[j]);
}
}
Fraction change_in_value = new Fraction(0, 1);
for (int j = 0; j < non_zero_in_last_row_and_non_zero_only_once_in_its_column.Count() - 1; j++)
{
//modify array matrix by changing constant,add answer
change_in_value -= array[array.GetLength(0) - 1, non_zero_in_last_row_and_non_zero_only_once_in_its_column[j]];
answer.Add(keepTrackColumn[non_zero_in_last_row_and_non_zero_only_once_in_its_column[j]], new Fraction(1,1));
}
List<int> possibly_no_solution_but_can_be_valid = new List<int>();
possibly_no_solution_but_can_be_valid = Miscs.GetUncommonElements(non_zero_in_last_row_and_non_zero_only_once_in_its_column, non_zero_index);
Fraction constant_rhs = change_in_value - array[array.GetLength(0) - 1, array.GetLength(1) - 1];
for (int j = 0; j < possibly_no_solution_but_can_be_valid.Count(); j++)
{
change_in_value -= array[array.GetLength(0) - 1, possibly_no_solution_but_can_be_valid[j]];
constant_rhs = change_in_value - array[array.GetLength(0) - 1, array.GetLength(1) - 1];
answer.Add(keepTrackColumn[possibly_no_solution_but_can_be_valid[j]], new Fraction(1,1));
for (int i = 0; i < array.GetLength(0); i++)
{
//array[i, array.GetLength(1) - 1] is constant
array[i, array.GetLength(1) - 1] += array[i, possibly_no_solution_but_can_be_valid[j]]; //because constant is on lhs for now, we make negative when shifting to rhs
array[i, possibly_no_solution_but_can_be_valid[j]] = new Fraction(0, 1);
}
break;
}
if (non_zero_in_last_row_and_non_zero_only_once_in_its_column.Count() > 0)
{
Fraction answer_temp = constant_rhs / array[array.GetLength(0) - 1, non_zero_in_last_row_and_non_zero_only_once_in_its_column[non_zero_in_last_row_and_non_zero_only_once_in_its_column.Count() - 1]];
answer.Add(keepTrackColumn[non_zero_in_last_row_and_non_zero_only_once_in_its_column[non_zero_in_last_row_and_non_zero_only_once_in_its_column.Count() - 1]], answer_temp);
}
//adding for deletion, though not come in non_zero_in_last_row_and_non_zero_only_once_in_its_column class
if (possibly_no_solution_but_can_be_valid.Count() > 0)
{
non_zero_in_last_row_and_non_zero_only_once_in_its_column.Add(possibly_no_solution_but_can_be_valid[0]);
}
//remove used column
array2 = new Fraction[array.GetLength(0), array.GetLength(1) - non_zero_in_last_row_and_non_zero_only_once_in_its_column.Count()];
for (int i = 0; i < array.GetLength(0); i++)
{
int k = 0;
for (int j = 0; j < array.GetLength(1); j++)
{
if (!non_zero_in_last_row_and_non_zero_only_once_in_its_column.Contains(j))
{
array2[i, k] = array[i, j];
}
else
{
k -= 1;
}
k++;
}
}
//update key indexes
for (int j = 0; j < non_zero_in_last_row_and_non_zero_only_once_in_its_column.Count(); j++)
{
Miscs.ModifyKey(keepTrackColumn, non_zero_in_last_row_and_non_zero_only_once_in_its_column[j], -1);
}
for (int j = 0; j < non_zero_in_last_row_and_non_zero_only_once_in_its_column.Count(); j++)
{
Dictionary<int, int> modifiedkeepTrackColumn = new Dictionary<int, int>();
foreach (KeyValuePair<int, int> item in keepTrackColumn)
{
if (item.Key > non_zero_in_last_row_and_non_zero_only_once_in_its_column[j])
{
modifiedkeepTrackColumn.Add(item.Key - 1, item.Value);
}
else
{
modifiedkeepTrackColumn.Add(item.Key, item.Value);
}
}
keepTrackColumn = modifiedkeepTrackColumn;
}
}
else if (array.GetLength(1) - 1 < array.GetLength(0))
{
throw new Exception("Over determined equations");
}
else
{
if (non_zero_count == 0 && array[array.GetLength(0) - 1, array.GetLength(1) - 1] != 0)
{
throw new Exception("Not solvable");
}
}
array = array2;
} while (array2.GetLength(1) > 2);
List<int> unflagged_keys = new List<int>();
foreach (KeyValuePair<int, int> item in keepTrackColumn)
{
if (item.Key != -1)
{
unflagged_keys.Add(item.Key);
}
}
if (unflagged_keys.Count()>1)
{
throw new Exception("More than one variable unsolved at end");
}
if (unflagged_keys.Count() == 1 && array[0, 0] != 0)
{
answer.Add(keepTrackColumn[unflagged_keys[0]],-1 * array[0, 1] / array[0, 0]);
}
List<int> unknown_answers = new List<int>();
for (int i = preserved_columns.Count() - 2; i >= 0; i--)
{
if (i != preserved_columns[i])
{
Miscs.ModifyKey(answer, i, preserved_columns[i]);
}
if (!preserved_columns.Contains(i) && !zero_columns.Contains(i))
{
unknown_answers.Add(i);
}
}
foreach (KeyValuePair<int,Fraction> specific_answer in answer)
{
Console.WriteLine($"x{specific_answer.Key} = {specific_answer.Value}");
}
foreach (int i in zero_columns)
{
Console.WriteLine($"x{i} is allowed to have any value");
}
foreach (int i in unknown_answers)
{
Console.WriteLine($"x{i} is not solvable");
}
//Fraction[,] array2 = new Fraction[array.GetLength(0)-1, array.GetLength(1)-1];
//for (int i = 0; i < array.GetLength(0); i++)
//{
// for (int j = 0; j < array.GetLength(1)-1; j++)
// {
// if (j != array.GetLength(1) - 2)
// {
// array2[i, j] = array[i, j];
// }
// else
// {
// array2[i, j] = array[i, j + 1] - array[i, j];
// }
// }
//}
Console.ReadLine();