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



Download 344,35 Kb.
bet3/4
Sana13.01.2023
Hajmi344,35 Kb.
#899246
TuriЛабораторна робота
1   2   3   4
Bog'liq
Звіт

Лістинг 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;


}
}
}



Download 344,35 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish