Лабораторна робота №2 патерн програмування "одинак"



бет3/4
Дата05.12.2023
өлшемі344,35 Kb.
#134481
түріЛабораторна робота
1   2   3   4
Байланысты:
Звіт

Лістинг Matrix.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace OOP_LAB2


{
[Serializable]
public class Matrix
{
private double[,] Arr;
private int x;
public int X { get => this.x; }
private int y;
public int Y { get => this.y; }
public Matrix() { }
public Matrix(int x, int y)
{
this.x = x;
this.y = y;
this.Arr = new double[x, y];
}
public double this[int x, int y]
{
get
{
return this.Arr[x, y];
}
set
{
this.Arr[x, y] = value;
}
}
public void NumbersRandom()
{
Random rnd = new Random();
for (int i = 0; i < Arr.GetLength(0); i++)
{
for (int j = 0; j < Arr.GetLength(1); j++)
{
Arr[i, j] = rnd.Next(-10, 10);
}
}
Thread.Sleep(20);
}
[NonSerialized]
public bool NumberOfCharacters = false;
public override string ToString()
{
string res = "";
for (int k = 0; k < this.Y; k++)
{
for (int i = 0; i < this.X; i++)
{
res += NumberOfCharacters == false ? $"{Arr[i, k],10}" : $"{ Arr[i, k],15:F4}";
}
res += "\n";
}
return res;
}
public void Transpose()
{
double[,] temp = new double[Arr.GetLength(1), Arr.GetLength(0)];

for (int i = 0; i < temp.GetLength(0); i++)


{
for (int j = 0; j < temp.GetLength(1); j++)
{
temp[i, j] = Arr[j, i];
Console.Write($"{temp[i, j],8:F2}");
}
Console.WriteLine();
}
Console.WriteLine();
}
public static Matrix operator *(Matrix a, Matrix b)
{

if (a.Y != b.X)


{
return null;

}
Matrix c = new Matrix(a.X, b.Y);


for (int i = 0; i < a.X; i++)
{
for (int j = 0; j < a.Y; j++)
{
for (int m = 0; m < b.X; m++)
{
c[i, j] += a[i, m] * b[m, j];
}
}
}
return c;
}
public void Print()
{
for (int i = 0; i < Arr.GetLength(0); i++)
{
for (int j = 0; j < Arr.GetLength(1); j++)
{
Console.Write($"{Arr[i, j],8:F2}" + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
public double Determinant()
{
if (Arr.GetLength(0) != Arr.GetLength(1))
{
throw new InvalidOperationException("Determinant can be calculated only for sqaure matrix!");
}

double sum = 0;


if (Arr.GetLength(0) == 1)


{
return Arr[0, 0];
}

if (Arr.GetLength(0) == 2)


{
return Arr[0, 0] * Arr[1, 1] - Arr[0, 1] * Arr[1, 0];
}

for (int n = 0; n < Arr.GetLength(0); n++)


{
Matrix minor = new Matrix(Arr.GetLength(0) - 1, Arr.GetLength(0) - 1);

for (int i = 0; i < Arr.GetLength(0) - 1; i++)


{
for (int j = 0; j < Arr.GetLength(0); j++)
{
if (j < n)
{
minor.Arr[i, j] = Arr[i + 1, j];
}
else if (j > n)
{
minor.Arr[i, j - 1] = Arr[i + 1, j];
}
}
}

int sign = (n % 2 == 0) ? 1 : -1;


sum += sign * Arr[0, n] * minor.Determinant();
}

return sum;


}

public void Invert()


{
const double tiny = 0.00001;

double[,] augmented = new double[Arr.GetLength(0), 2 * Arr.GetLength(0)];


for (int row = 0; row < Arr.GetLength(0); row++)
{
for (int j = 0; j < Arr.GetLength(0); j++)
{
augmented[row, j] = Arr[row, j];
}

augmented[row, row + Arr.GetLength(0)] = 1;


}

int numCols = 2 * Arr.GetLength(0);


for (int row = 0; row < Arr.GetLength(0); row++)


{
if (Math.Abs(augmented[row, row]) < tiny)
{
for (int r2 = row + 1; r2 < Arr.GetLength(0); r2++)
{
if (Math.Abs(augmented[r2, row]) > tiny)
{
for (int c = 0; c < numCols; c++)
{
double tmp = augmented[row, c];
augmented[row, c] = augmented[r2, c];
augmented[r2, c] = tmp;
}

break;
}


}
}

if (Math.Abs(augmented[row, row]) > tiny)


{
for (int col = 0; col < numCols; col++)
{
if (col != row)
{
augmented[row, col] /= augmented[row, row];
}
}

augmented[row, row] = 1;


for (int row2 = 0; row2 < Arr.GetLength(0); row2++)


{
if (row2 != row)
{
double factor = augmented[row2, row] / augmented[row, row];
for (int col = 0; col < numCols; col++)
{
augmented[row2, col] -= factor * augmented[row, col];
}
}
}
}
}

if (augmented[Arr.GetLength(0) - 1, Arr.GetLength(0) - 1] == 0)


{
throw new Exception("null");
}

double[,] inverse = new double[Arr.GetLength(0), Arr.GetLength(0)];


for (int row = 0; row < Arr.GetLength(0); row++)
{
for (int col = 0; col < Arr.GetLength(0); col++)
{
inverse[row, col] = augmented[row, col + Arr.GetLength(0)];
}
}

Arr = inverse;


}
}
}




Достарыңызбен бөлісу:
1   2   3   4




©emirsaba.org 2024
әкімшілігінің қараңыз

    Басты бет