Skip to content

Commit

Permalink
Элементарные преобразования и матрицы
Browse files Browse the repository at this point in the history
  • Loading branch information
AsTeFu authored Dec 27, 2018
1 parent 5c90bfe commit f3bbed7
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 100 deletions.
253 changes: 170 additions & 83 deletions Early access/ElementaryTransformations.cs
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;
// }
//}
}
31 changes: 14 additions & 17 deletions Early access/MatrixCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@
using System.Threading.Tasks;

namespace MatrixCalculator {
class Program {
static void Main(string[] args) {

Matrix A = new Matrix(new double[,] { { 1, 13, 5, 7 }, { 2, 4, 6, 28 } });
Matrix B = new Matrix(new double[,] { { 0, 13, -2, -4 }, { -1, 2, 3, -5 } });

Console.WriteLine((A-2*B).Transpose());

Console.Read();

}
}


class Matrix {

public int Lines {
Expand Down Expand Up @@ -47,6 +35,9 @@ public Matrix() {
get {
return matrix[i, j];
}
set {
matrix[i, j] = value;
}
}

public static Matrix empty {
Expand Down Expand Up @@ -118,8 +109,7 @@ public bool isSquare {
return Lines == Columns;
}
}



public static Matrix operator +(Matrix leftOperand, Matrix rightOperand) {
if (leftOperand.Lines == rightOperand.Lines && leftOperand.Columns == rightOperand.Columns) {
Matrix newMatrix = new Matrix(leftOperand.Lines, leftOperand.Columns);
Expand Down Expand Up @@ -243,8 +233,7 @@ public static Matrix GetSingleMatrix(int lines, int columns) {

return new Matrix(E);
}



public string GetElemet(int i, int j) {
if (i <= Lines && j <= Columns)
return $"Элемент [{i}, {j}] = {matrix[i - 1, j - 1]}";
Expand All @@ -263,5 +252,13 @@ public override string ToString() {

return tmp;
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}

0 comments on commit f3bbed7

Please sign in to comment.