1. Determinantni hisoblash funksiyasi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Matritsa o'lchamini kiriting");
int n = Int32.Parse(Console.ReadLine());
int[,] Matrix = new int[n, n];
for(int i=0;i
{
for (int j = 0; j < n; j++)
{
Console.Write((i + 1) + " satr " + (j + 1) + " ustun elemntlarini kiriting ");
Matrix[i, j] = Int32.Parse(Console.ReadLine());
}
}
Console.WriteLine("Matritsa determinanti " + Determinant(Matrix));
Console.ReadLine();
}
static double Determinant(int[,] matrix)
{
int n = Convert.ToInt32(Math.Sqrt(matrix.Length));
int isaret = 1;
double toplam = 0;
if (n == 1)
{
return matrix[0, 0];
}
for (int i = 0; i < n; i++)
{
int[,] altmatritsa = new int[n - 1, n - 1];
for (int satir = 1; satir < n; satir++)
{
for (int ustun = 0; ustun < n; ustun++)
{
if (ustun < i)
{
altmatritsa[satir - 1, ustun] = matrix[satir, ustun];
}
else if (ustun > i)
{
altmatritsa[satir - 1, ustun - 1] = matrix[satir, ustun];
}
}
}
if (i % 2 == 0)
{
isaret = 1;
}
else
{
isaret = -1;
}
toplam += isaret * matrix[0, i] * (Determinant(altmatritsa));
}
return toplam;
}
}
}
2. Teskari matritsani hisoblash funksiyasi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inverse
{
class Program
{
static readonly int N = 4;// Matritsa o'lchami
static void getCofactor(int[,] A, int[,] temp, int p, int q, int n)
{
int i = 0, j = 0;
// Matritsaning har bir elementi uchun takrorlash
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
// Vaqtinchalik matritsaga nusxa ko'chirish
// berilgan satr va ustun uchun
if (row != p && col != q)
{
temp[i, j++] = A[row, col];
// Satr to'ldirish uchun satr indeksi oshiriladi
// ustun o'chirish indeksi
if (j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
// Determinantni hisoblash funksiyasi
static int determinant(int[,] A, int n)
{
int D = 0;
if (n == 1)// agar asosiy matritsa bitta elementdan iborat bo'lsa
return A[0, 0];
int[,] temp = new int[N, N];
int sign = 1; // Matritsani juft va toq holatlari
for (int f = 0; f < n; f++)// Birinchi qatorning har bir elementi uchun takrorlash
{
getCofactor(A, temp, 0, f, n);//funksiyaga murojat
D += sign * A[0, f] * determinant(temp, n - 1);
sign = -sign;
}
return D;
}
static void adjoint(int[,] A, int[,] adj)// Teskari maritsa shartini tekshirish
{
if (N == 1)
{
adj[0, 0] = 1;
return;
}
int sign = 1;
int[,] temp = new int[N, N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
getCofactor(A, temp, i, j, N);// funksiyaga ma'lumot yuborish
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j, i] = (sign) * (determinant(temp, N - 1));
}
}
}
static bool inverse(int[,] A, float[,] inverse)// Teskari matritsani determinanti 0 ga teng holatlarini tekshirish. Agar 0 bo'lsa matritsa singular deyiladi
{
int det = determinant(A, N);
if (det == 0)
{
Console.Write("Singular matritsa, Teskari matritsa mavjud bo'lmaydi");
return false;
}
int[,] adj = new int[N, N];
adjoint(A, adj);
// Find Inverse using formula "inverse(A) = adj(A)/det(A)"
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
inverse[i, j] = adj[i, j] / (float)det;
return true;
}
static void display(int[,] A)// Oynaga natijalarni chiqarish
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
Console.Write(A[i, j] + " ");
Console.WriteLine();
}
}
static void display(float[,] A)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
Console.Write("{0:F6} ", A[i, j]);
Console.WriteLine();
}
}
static void Main(string[] args)
{
int[,] A = { {5, -2, 2, 7},
{1, 0, 0, 3},
{-3, 1, 5, 0},
{3, -1, -9, 4}};
int[,] adj = new int[N, N];
float[,] inv = new float[N, N];
Console.Write("Matritsa elementlari :\n");
display(A);
Console.Write("\n adjoint funksiyasi natijasi :\n");
adjoint(A, adj);
display(adj);
Console.Write("\nTeskarisi :\n");
if (inverse(A, inv))
display(inv);
Console.ReadLine();
}
}
}
Do'stlaringiz bilan baham: |