-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Элементарные преобразования и матрицы
- Loading branch information
Showing
2 changed files
with
184 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,246 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using MatrixCalculator; | ||
|
||
namespace Stupeni { | ||
class Program { | ||
static void Main(string[] args) { | ||
Matrix matrix = new Matrix(new double[,] { { 1, 5, -4 }, | ||
{ -7, 1, 2 }, | ||
{ -1, 0, 2 }, | ||
{ -5, 1, 7 } }, 4, 3); | ||
|
||
ElementaryTransformations matrix = new ElementaryTransformations(new Matrix(new double[,] { { 1, 0, 2, 3 }, | ||
{ 0, 1, 4, 0 }, | ||
{ 5, 0, 2, 1 }, })); | ||
|
||
Console.WriteLine(matrix); | ||
|
||
matrix.DiffLines(2, 1, -7); | ||
matrix.DiffLines(3, 1, -1); | ||
matrix.DiffLines(4, 1, -5); | ||
|
||
matrix.DiffLines(4, 2); | ||
matrix.DiffLines(3, 1, 5); | ||
matrix.MultiplyConst(3, -1d / 8); | ||
matrix.DiffLines(2, 3, 4); | ||
matrix.DiffLines(1, 3, 2); | ||
|
||
|
||
Console.Read(); | ||
} | ||
} | ||
|
||
class ElementaryTransformations { | ||
|
||
class Matrix { | ||
Matrix matrix; | ||
|
||
double[,] data; | ||
int column; | ||
int row; | ||
|
||
public Matrix(double[,] data, int row, int column) { | ||
this.data = data; | ||
this.column = column; | ||
this.row = row; | ||
public ElementaryTransformations(Matrix matrix) { | ||
this.matrix = matrix; | ||
} | ||
|
||
public void DiffLines(int line1, int line2) { | ||
line1 -= 1; | ||
line2 -= 1; | ||
|
||
Console.WriteLine($"Вычетаем из {line1 + 1} строку {line2 + 1}"); | ||
|
||
for (int j = 0; j < column; j++) { | ||
data[line1, j] -= data[line2, j]; | ||
for (int j = 0; j < matrix.Columns; j++) { | ||
matrix[line1 - 1, j] -= matrix[line2 - 1, j]; | ||
} | ||
|
||
Console.WriteLine(this); | ||
} | ||
|
||
public void DiffLines(int line1, int line2, double constant2) { | ||
public void DiffLines(int line1, int line2, double constant2) | ||
{ | ||
line1 -= 1; | ||
line2 -= 1; | ||
Console.WriteLine($"Вычетаем из {line1 + 1} строку {line2 + 1} умноженную на {constant2}"); | ||
for (int j = 0; j < column; j++) { | ||
data[line1, j] -= data[line2, j] * constant2; | ||
for (int j = 0; j < matrix.Columns; j++) { | ||
matrix[line1, j] -= matrix[line2, j] * constant2; | ||
} | ||
|
||
Console.WriteLine(this); | ||
} | ||
|
||
public void DiffLines(int line1, int line2, double constant1, double constant2) { | ||
public void DiffLines(int line1, int line2, double constant1, double constant2) | ||
{ | ||
line1 -= 1; | ||
line2 -= 1; | ||
|
||
Console.WriteLine($"Вычетаем из {line1 + 1} умноженной на {constant1} строку {line2 + 1} умноженную на {constant2}"); | ||
|
||
for (int j = 0; j < column; j++) { | ||
data[line1, j] = data[line1, j] * constant1 - data[line2, j] * constant2; | ||
for (int j = 0; j < matrix.Columns; j++) { | ||
matrix[line1, j] = matrix[line1, j] * constant1 - matrix[line2, j] * constant2; | ||
} | ||
|
||
Console.WriteLine(this); | ||
} | ||
|
||
public void MultiplyConst(int line, double constant) { | ||
public void MultiplyConst(int line, double constant) | ||
{ | ||
line -= 1; | ||
Console.WriteLine($"Умножаем строку {line + 1} на {constant}"); | ||
for (int j = 0; j < column; j++) { | ||
data[line, j] *= constant; | ||
for (int j = 0; j < matrix.Columns; j++) { | ||
matrix[line, j] *= constant; | ||
} | ||
|
||
Console.WriteLine(this); | ||
} | ||
|
||
public void SwapLines(int line1, int line2) { | ||
public void SwapLines(int line1, int line2) | ||
{ | ||
line1 -= 1; | ||
line2 -= 1; | ||
Console.WriteLine($"Меняем строки местами {line1 + 1} и {line2 + 1}"); | ||
|
||
for (int j = 0; j < column; j++) { | ||
double tmp = data[line1, j]; | ||
data[line1, j] = data[line2, j]; | ||
data[line2, j] = tmp; | ||
for (int j = 0; j < matrix.Columns; j++) { | ||
double tmp = matrix[line1, j]; | ||
matrix[line1, j] = matrix[line2, j]; | ||
matrix[line2, j] = tmp; | ||
} | ||
|
||
Console.WriteLine(this); | ||
} | ||
|
||
|
||
//public void Solve() { | ||
|
||
|
||
// bool common = true; | ||
// //int keyElements = 0; | ||
// List<int> indexKeyElements = new List<int>(); | ||
// for (int j = 0, i = 0; j < column && i < row; j++, i++) { | ||
// if (data[i, j] != 0) { | ||
// MultiplyConst(i + 1, 1 / data[i, j]); | ||
|
||
// indexKeyElements.Add(j); | ||
|
||
// if (j == column - 1) | ||
// common = false; | ||
public override string ToString() | ||
{ | ||
string tmp = ""; | ||
for (int i = 0; i < matrix.Lines; i++) { | ||
for (int j = 0; j < matrix.Columns; j++) { | ||
tmp += matrix[i, j] + "\t"; | ||
} | ||
tmp += "\n"; | ||
} | ||
tmp += "\n"; | ||
return tmp; | ||
} | ||
} | ||
|
||
// for (int k = i + 1; k < row; k++) { | ||
// DiffLines(k + 1, i + 1, data[k, j]); | ||
// } | ||
// } else { | ||
// i--; | ||
// continue; | ||
// } | ||
// } | ||
//class MatrStup { | ||
|
||
// if (common) { | ||
// double[,] data; | ||
// int column; | ||
// int row; | ||
|
||
// if (indexKeyElements.Count == column - 1) { | ||
// Console.WriteLine("СЛУ совместная определенная"); | ||
// public MatrStup(double[,] data, int row, int column) { | ||
// this.data = data; | ||
// this.column = column; | ||
// this.row = row; | ||
// } | ||
|
||
// } else { | ||
// Console.WriteLine("СЛУ совместная неопределенная"); | ||
// } | ||
// public void DiffLines(int line1, int line2) { | ||
// line1 -= 1; | ||
// line2 -= 1; | ||
|
||
// for (int j = column - 1; j >= 0; j--) { | ||
// if (indexKeyElements.Contains(j)) { | ||
// for (int i = ) | ||
// } | ||
// } | ||
// Console.WriteLine($"Вычетаем из {line1 + 1} строку {line2 + 1}"); | ||
|
||
// for (int j = 0; j < column; j++) { | ||
// data[line1, j] -= data[line2, j]; | ||
// } | ||
|
||
// } else { | ||
// Console.WriteLine("СЛУ несовместна"); | ||
// } | ||
// Console.WriteLine(this); | ||
// } | ||
|
||
//} | ||
// public void DiffLines(int line1, int line2, double constant2) { | ||
// line1 -= 1; | ||
// line2 -= 1; | ||
// Console.WriteLine($"Вычетаем из {line1 + 1} строку {line2 + 1} умноженную на {constant2}"); | ||
// for (int j = 0; j < column; j++) { | ||
// data[line1, j] -= data[line2, j] * constant2; | ||
// } | ||
|
||
// Console.WriteLine(this); | ||
// } | ||
|
||
// public void DiffLines(int line1, int line2, double constant1, double constant2) { | ||
// line1 -= 1; | ||
// line2 -= 1; | ||
|
||
public override string ToString() { | ||
string tmp = ""; | ||
for (int i = 0; i < row; i++) { | ||
for (int j = 0; j < column; j++) { | ||
tmp += data[i, j] + "\t"; | ||
} | ||
tmp += "\n"; | ||
} | ||
tmp += "\n"; | ||
return tmp; | ||
} | ||
} | ||
// Console.WriteLine($"Вычетаем из {line1 + 1} умноженной на {constant1} строку {line2 + 1} умноженную на {constant2}"); | ||
|
||
// for (int j = 0; j < column; j++) { | ||
// data[line1, j] = data[line1, j] * constant1 - data[line2, j] * constant2; | ||
// } | ||
|
||
// Console.WriteLine(this); | ||
// } | ||
|
||
// public void MultiplyConst(int line, double constant) { | ||
// line -= 1; | ||
// Console.WriteLine($"Умножаем строку {line + 1} на {constant}"); | ||
// for (int j = 0; j < column; j++) { | ||
// data[line, j] *= constant; | ||
// } | ||
|
||
// Console.WriteLine(this); | ||
// } | ||
|
||
// public void SwapLines(int line1, int line2) { | ||
// line1 -= 1; | ||
// line2 -= 1; | ||
// Console.WriteLine($"Меняем строки местами {line1 + 1} и {line2 + 1}"); | ||
|
||
// for (int j = 0; j < column; j++) { | ||
// double tmp = data[line1, j]; | ||
// data[line1, j] = data[line2, j]; | ||
// data[line2, j] = tmp; | ||
// } | ||
|
||
// Console.WriteLine(this); | ||
// } | ||
|
||
|
||
// //public void Solve() { | ||
|
||
// // bool common = true; | ||
// // //int keyElements = 0; | ||
// // List<int> indexKeyElements = new List<int>(); | ||
// // for (int j = 0, i = 0; j < column && i < row; j++, i++) { | ||
// // if (data[i, j] != 0) { | ||
// // MultiplyConst(i + 1, 1 / data[i, j]); | ||
|
||
// // indexKeyElements.Add(j); | ||
|
||
// // if (j == column - 1) | ||
// // common = false; | ||
|
||
// // for (int k = i + 1; k < row; k++) { | ||
// // DiffLines(k + 1, i + 1, data[k, j]); | ||
// // } | ||
// // } else { | ||
// // i--; | ||
// // continue; | ||
// // } | ||
// // } | ||
|
||
// // if (common) { | ||
|
||
// // if (indexKeyElements.Count == column - 1) { | ||
// // Console.WriteLine("СЛУ совместная определенная"); | ||
|
||
// // } else { | ||
// // Console.WriteLine("СЛУ совместная неопределенная"); | ||
// // } | ||
|
||
// // for (int j = column - 1; j >= 0; j--) { | ||
// // if (indexKeyElements.Contains(j)) { | ||
// // for (int i = ) | ||
// // } | ||
// // } | ||
|
||
|
||
// // } else { | ||
// // Console.WriteLine("СЛУ несовместна"); | ||
// // } | ||
|
||
// //} | ||
|
||
|
||
|
||
// public override string ToString() { | ||
// string tmp = ""; | ||
// for (int i = 0; i < row; i++) { | ||
// for (int j = 0; j < column; j++) { | ||
// tmp += data[i, j] + "\t"; | ||
// } | ||
// tmp += "\n"; | ||
// } | ||
// tmp += "\n"; | ||
// return tmp; | ||
// } | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters