Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static double[] SolveSystem(double[][] typeMatrix, double[] grades)
- {
- const double eps = 1e-9;
- double[][] matrix = typeMatrix.Select(x => x.ToArray()).ToArray();
- double[] column = grades.ToArray();
- int m = matrix.Length, n = matrix[0].Length;
- var result = Enumerable.Repeat(-1, n).ToArray();
- for (int col = 0, curRow = 0; col < n; col++)
- {
- double max = Enumerable.Range(curRow, m - curRow).Max(r => Math.Abs(matrix[r][col]));
- int row = Enumerable.Range(curRow, m - curRow).First(r => Math.Abs(matrix[r][col]) == max);
- if (Math.Abs(matrix[row][col]) < eps)
- continue;
- Swap(ref matrix[curRow], ref matrix[row]);
- Swap(ref column[curRow], ref column[row]);
- for (int r = 0; r < m; r++)
- if (r != curRow && Math.Abs(matrix[r][col]) >= eps)
- {
- double coef = matrix[r][col] / matrix[curRow][col];
- for (int c = col; c < n; c++)
- matrix[r][c] -= matrix[curRow][c] * coef;
- column[r] -= column[curRow] * coef;
- }
- result[col] = curRow;
- curRow++;
- }
- return result.Select((row, col) => row < 0 ? 0 : column[row] / matrix[row][col]).ToArray();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement